该程序使用ArrayList存储停车记录,并通过switch-case语句实现菜单选择功能。主要功能包括:
停车:输入车牌号和进入时间,自动分配停车位编号,
结算:根据停车位编号计算停车费用,计费标准为停车时长(秒)乘以每秒费用0.05元,同时记录车辆离开时间和费用;
查看记录:显示所有停车记录的编号、车牌号、进入时间、离开时间和费用;
退出程序。文章来源:https://www.toymoban.com/news/detail-510803.html
当停车场已满时,无法再停车;当用户输入不存在的停车位编号或已经结算过的车辆编号时,系统会提示用户相应的信息。文章来源地址https://www.toymoban.com/news/detail-510803.html
import java.util.ArrayList;
import java.util.Scanner;
class ParkingRecord {
int id;
String licensePlate;
long timeIn;
long timeOut;
double cost;
}
public class ParkingLot {
private ArrayList<ParkingRecord> parkingLot;
private int currentCapacity;
private final int MAX_CAPACITY = 10;
public ParkingLot() {
parkingLot = new ArrayList<ParkingRecord>(MAX_CAPACITY);
currentCapacity = 0;
}
private void showMenu() {
System.out.println("Parking Lot Management System");
System.out.println("=============================");
System.out.println("1. Park a car");
System.out.println("2. Check out a car");
System.out.println("3. Show parking records");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
}
private void parkCar() {
if (currentCapacity < MAX_CAPACITY) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter license plate: ");
String licensePlate = scanner.nextLine();
long timeIn = System.currentTimeMillis();
ParkingRecord record = new ParkingRecord();
record.id = currentCapacity + 1;
record.licensePlate = licensePlate;
record.timeIn = timeIn;
parkingLot.add(record);
currentCapacity++;
System.out.println("Car parked successfully. Parking slot number is " + record.id);
} else {
System.out.println("Parking lot is full.");
}
}
private void checkOutCar() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter parking slot number: ");
int id = scanner.nextInt();
ParkingRecord record = parkingLot.get(id - 1);
if (record.timeOut > 0) {
System.out.println("This car has already been checked out.");
return;
}
record.timeOut = System.currentTimeMillis();
long timeElapsed = record.timeOut - record.timeIn;
double cost = timeElapsed / 1000.0 * 0.05;
record.cost = cost;
System.out.println("Checked out successfully. Parking fee is " + cost);
}
private void showParkingRecords() {
System.out.println("ID\tLicense Plate\tTime In\tTime Out\tCost");
for (ParkingRecord record : parkingLot) {
System.out.printf("%d\t%s\t\t%d\t%d\t%.2f\n", record.id, record.licensePlate, record.timeIn, record.timeOut, record.cost);
}
}
public static void main(String[] args) {
ParkingLot parkingLot = new ParkingLot();
Scanner scanner = new Scanner(System.in);
int choice;
do {
parkingLot.showMenu();
choice = scanner.nextInt();
switch (choice) {
case 1:
parkingLot.parkCar();
break;
case 2:
parkingLot.checkOutCar();
break;
case 3:
parkingLot.showParkingRecords();
break;
case 4:
System.out.println("Thank you for using the Parking Lot Management System.");
break;
default:
System.out.println("Invalid choice.");
break;
}
System.out.println();
} while (choice != 4);
}
}
到了这里,关于用JAVA实现停车场管理系统的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!