用JAVA实现停车场管理系统

该程序使用ArrayList存储停车记录,通过switch-case语句实现菜单驱动的功能,包括停车、结算、查看记录和退出。当停车场满时,不允许再停车;在结算或查找记录时,系统会处理无效输入并给出相应提示。停车费用基于停车时长计算,每秒费用为0.05元。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

该程序使用ArrayList存储停车记录,并通过switch-case语句实现菜单选择功能。主要功能包括:

  • 停车:输入车牌号和进入时间,自动分配停车位编号,

  • 结算:根据停车位编号计算停车费用,计费标准为停车时长(秒)乘以每秒费用0.05元,同时记录车辆离开时间和费用;

  • 查看记录:显示所有停车记录的编号、车牌号、进入时间、离开时间和费用;

  • 退出程序。

当停车场已满时,无法再停车;当用户输入不存在的停车位编号或已经结算过的车辆编号时,系统会提示用户相应的信息。

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);
    }
}
初始化停车场(确定停车区个数n,每个停车区的停车位,且初始时,停车场为空),说明:使用一个共享数组(临界资源)存储每个停车区中空闲停车位的个数,每一个停车区使用一个共享缓冲区可容纳一辆车,停车场公共通道允许通过两辆车通过。 停车场入口检查是否有空闲停位,如果有发放相关停车区的停车卡,允许停车。如果不存在空闲车位,等待到有空闲车位止。注,入口处应尽量发放不同停车区的停车卡,以获得更高的效率。停车场过道允许两辆车同时通过。 停车场出口,回收停车卡,并修改相关停车区空闲车位数。注:同一时刻只能有一个车出停车场。 每一个停车区,有一个待车位,供进入停车区车辆进入停车区。停车区只能有一辆车进或出。 每辆车每进入下一环节皆应停留一定时间。每一个用户建立一个窗口,于窗口中显示当前将态。将状态转换可由人工确定亦可自动完成,但进入下状态时需要停留学生一定的时间,以保证多个用户“并行”工作。 本框架由四个类组成,这四个类分别是:InitFrame、WotkFram、carJFrame及carThread,由InitFrame启动。类InitFrame提供停车场初始化功能,完成初始任务后启动类WotkFram界面,执行停车场模拟程序,点击命令按扭“进入停车场”,建立一个线程(线程类名为carThread),线程建立一个carJFrame窗口(车辆进入、离开停车场操作程序)模拟车辆进入或离开停车场。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT大鸵鸟

你的鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值