利用java编写一个电话扣费的程序public class Test extends PhoneKouFei{
public Test(String startTime, String stopTime) {
super(startTime, stopTime);
}
public static void main(String[] args) {
String start = "00:12:23";
String stop = "00:15:23";
new Test(start,stop);
}
}
abstract class PhoneKouFei{
private final double fei = 1.5;//1分钟扣1毛5
public String startTime; //通话开始时间
public String stopTime; //通话结束时间
public PhoneKouFei(String startTime, String stopTime){
this.startTime = startTime;
this.stopTime = stopTime;
System.out.println("通话时常java模拟收费代码:" +getKouFei()+ "分钟");
System.out.println("应扣费java模拟收费代码:" + (double)getKouFei() * fei);
}
//扣费方法
private final long getKouFei(){
java.text.SimpleDateFormat sim = new java.text.SimpleDateFormat("HH:mm:ss");
try {
java.util.Date start = sim.parse(this.startTime);
java.util.Date stop = sim.parse(this.stopTime);
return (stop.getTime() - start.getTime()) / 60000;
} catch (java.text.ParseException e) {
throw new RuntimeException("时间格式错误..");
}
}
}
//输出结果:
通话时常:3分钟
应扣费:4.5
java 编程,停车收费简易系统代码如下,仅实现功能,未考虑安全等情况.
//ParkingTest.java
/**
* 停车程序入口.本示例为考虑数据安全等.仅作为示例,未经调试.
* @author menglg
*
*/
public class ParkingTest {
public static void main(String[] args) {
// 程序入口,可以安排停车出车
for (int i = 0; i100; i++) {
if (ParkingManage.hasPosition()) {
ParkingManage.enterParking(i + "");
continue;
}
break;
}
for (int i = 0; i100; i++) {
ParkingManage.outParking(i + "");
}
System.out.println(ParkingManage.totalCost());
}
}
//ParkingManage.java
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class ParkingManage {
private static ListParking parkings = new ArrayList();
private static final int maxNumber = 100;
private static int currentNumber;
public static void enterParking(String lpn) {
if (lpn == null || lpn.length() == 0) {
return;
}
Parking p = new Parking(lpn);
p.setEnterDate(new Date());
parkings.add(p);
}
public static void outParking(String lpn) {
if (lpn == null || lpn.length() == 0) {
return;
}
for (Parking p : parkings) {
if (!lpn.equals(p.getLpn()))
continue;
Date now = new Date();
p.setOutDate(now);
parkings.remove(p);
currentNumber += p.getCost();
break;
}
}
public static int totalCost() {
return currentNumber;
}
public static boolean hasPosition() {
return parkings.size()maxNumber;
}
}
//Parking.java
import java.util.Date;
public class Parking {
private String lpn;
private Date enterDate;
private Date outDate;
private int cost;
public Parking(String lpn) {
this.lpn = lpn;
}
public String getLpn() {
return lpn;
}
public Date getEnterDate() {
return enterDate;
}
public Date getOutDate() {
return outDate;
}
public int getCost() {
if(outDate == null || enterDate == null){
return 0;
}
//停车时间,单位毫秒
long time = outDate.getTime() - enterDate.getTime();
long hour = 60*60*1000;
if(timehour){
cost = 3;
}else if(time%hour == 0){
cost = (int)(time/hour * 2);
}else{
cost = (int)((time/hour+1) * 2);
推荐阅读
- 科创未来绘画chatgpt,科创未来绘画内容
- python查看进程使用cpu,python运行结果
- 开心飞行棋小游戏在线玩,开心飞起图片大全
- 百米拍视频号怎么拍,100米怎么拍
- c语言编程求幂函数 c语言计算幂函数
- 圆点html代码,html的圆点标记怎么打
- 电脑建立虚拟机,电脑创建虚拟机
- 自然科学类直播话术,自然科学播音稿
- php并发获取数据库 php接口并发问题