写出停车场计时收费操作界面程序代码
由于缺乏具体的需求和细节,以下代码仅为参考,实际应根据具体需求进行修改和完善。
// 定义停车场计时收费操作界面类 public class ParkingLot { // 定义私有变量,存储停车场信息 private int totalSpaces; // 总车位数 private int usedSpaces; // 已使用车位数 private double hourlyRate; // 每小时收费率 private double totalIncome; // 总收入 private Date startTime; // 开始计时时间
// 构造函数,初始化停车场信息
public ParkingLot(int totalSpaces, double hourlyRate) {
this.totalSpaces = totalSpaces;
this.usedSpaces = 0;
this.hourlyRate = hourlyRate;
this.totalIncome = 0.0;
}
// 停车方法,返回停车状态(成功或失败)
public boolean park() {
if (usedSpaces < totalSpaces) {
usedSpaces++;
startTime = new Date();
return true;
} else {
return false;
}
}
// 取车方法,返回停车费用
public double retrieve() {
usedSpaces--;
Date endTime = new Date();
long duration = endTime.getTime() - startTime.getTime();
double hours = duration / (1000.0 * 60 * 60);
double cost = hours * hourlyRate;
totalIncome += cost;
return cost;
}
// 获取停车场状态(总车位数、已使用车位数、空余车位数、总收入)
public String getStatus() {
int freeSpaces = totalSpaces - usedSpaces;
String status = "总车位数:" + totalSpaces + "\n";
status += "已使用车位数:" + usedSpaces + "\n";
status += "空余车位数:" + freeSpaces + "\n";
status += "总收入:" + totalIncome + "\n";
return status;
}
}
// 操作界面代码(示例) public class ParkingLotUI { // 定义停车场对象 private ParkingLot lot;
// 构造函数,创建停车场对象
public ParkingLotUI(int totalSpaces, double hourlyRate) {
lot = new ParkingLot(totalSpaces, hourlyRate);
}
// 进入停车场方法,调用停车方法并返回停车状态
public boolean enter() {
return lot.park();
}
// 离开停车场方法,调用取车方法并返回停车费用
public double leave() {
return lot.retrieve();
}
// 获取停车场状态方法,调用获取状态方法并返回状态信息
public String getStatus() {
return lot.getStatus();
}
// 主程序方法,创建停车场操作界面对象并运行操作界面
public static void main(String[] args) {
ParkingLotUI ui = new ParkingLotUI(100, 10.0);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请输入操作代码:");
System.out.println("1. 进入停车场");
System.out.println("2. 离开停车场");
System.out.println("3. 获取停车场状态");
System.out.println("0. 退出程序");
int code = scanner.nextInt();
switch (code) {
case 1:
if (ui.enter()) {
System.out.println("停车成功!");
} else {
System.out.println("停车失败,停车场已满!");
}
break;
case 2:
double cost = ui.leave();
System.out.println("停车费用为:" + cost);
break;
case 3:
String status = ui.getStatus();
System.out.println(status);
break;
case 0:
System.out.println("程序已退出!");
return;
default:
System.out.println("无效操作代码,请重新输入!");
break;
}
}
}
原文地址: https://www.cveoy.top/t/topic/ehQ5 著作权归作者所有。请勿转载和采集!