java编写停车场管理系统
对于停车场管理系统,我们可以考虑以下几个方面的需求:
- 停车场的基本信息,包括停车位数量、车位费用、停车场名称等;
- 车辆的基本信息,包括车牌号、车型、进入时间、离开时间等;
- 停车场的收费规则,包括计费方式、优惠政策等;
- 停车场的管理功能,包括车位管理、车辆管理、收费管理等;
- 停车场的查询功能,包括查询空余车位、查询已停车辆、查询车辆停留时间、查询收费情况等。
基于以上需求,我们可以设计出如下的停车场管理系统:
- 停车场类
停车场类包含了停车场的基本信息和车位管理功能,具体实现如下:
public class ParkingLot {
private int totalSpace; // 停车位总数
private int usedSpace; // 已用停车位数
private int fee; // 停车费用
private String name; // 停车场名称
private List<ParkingSpace> spaces; // 停车位列表
public ParkingLot(int totalSpace, int fee, String name) {
this.totalSpace = totalSpace;
this.usedSpace = 0;
this.fee = fee;
this.name = name;
this.spaces = new ArrayList<>();
for (int i = 1; i <= totalSpace; i++) {
this.spaces.add(new ParkingSpace(i));
}
}
// 停车
public boolean park(Car car) {
if (this.usedSpace < this.totalSpace) {
for (ParkingSpace space : this.spaces) {
if (!space.isOccupied()) {
space.setCar(car);
this.usedSpace++;
return true;
}
}
}
return false;
}
// 取车
public Car unpark(int spaceId) {
for (ParkingSpace space : this.spaces) {
if (space.getSpaceId() == spaceId && space.isOccupied()) {
Car car = space.getCar();
space.setCar(null);
this.usedSpace--;
return car;
}
}
return null;
}
// 查询空余车位
public int getAvailableSpace() {
return this.totalSpace - this.usedSpace;
}
// 查询已停车辆
public List<Car> getParkedCars() {
List<Car> cars = new ArrayList<>();
for (ParkingSpace space : this.spaces) {
if (space.isOccupied()) {
cars.add(space.getCar());
}
}
return cars;
}
}
- 车位类
车位类包含了车位的基本信息和车辆管理功能,具体实现如下:
public class ParkingSpace {
private int spaceId; // 车位编号
private Car car; // 停放车辆
public ParkingSpace(int spaceId) {
this.spaceId = spaceId;
this.car = null;
}
public int getSpaceId() {
return spaceId;
}
public boolean isOccupied() {
return this.car != null;
}
public void setCar(Car car) {
this.car = car;
}
public Car getCar() {
return this.car;
}
}
- 车辆类
车辆类包含了车辆的基本信息和收费管理功能,具体实现如下:
public class Car {
private String plateNumber; // 车牌号
private String type; // 车型
private Date enterTime; // 进入时间
private Date leaveTime; // 离开时间
public Car(String plateNumber, String type) {
this.plateNumber = plateNumber;
this.type = type;
this.enterTime = new Date();
this.leaveTime = null;
}
public String getPlateNumber() {
return plateNumber;
}
public String getType() {
return type;
}
public Date getEnterTime() {
return enterTime;
}
public Date getLeaveTime() {
return leaveTime;
}
// 计算停车费用
public int calculateFee(int fee) {
long duration = (this.leaveTime.getTime() - this.enterTime.getTime()) / 1000 / 60; // 停车时长,单位为分钟
return (int) (duration / 60) * fee + (int) (duration % 60) * fee / 60;
}
// 离开停车场
public void leaveParkingLot() {
this.leaveTime = new Date();
}
}
- 停车场管理系统类
停车场管理系统类包含了收费管理和查询功能,具体实现如下:
public class ParkingLotManagementSystem {
private ParkingLot parkingLot;
public ParkingLotManagementSystem(ParkingLot parkingLot) {
this.parkingLot = parkingLot;
}
// 停车
public boolean park(String plateNumber, String type) {
Car car = new Car(plateNumber, type);
return this.parkingLot.park(car);
}
// 取车
public int unpark(String plateNumber, int spaceId) {
Car car = null;
List<Car> parkedCars = this.parkingLot.getParkedCars();
for (Car c : parkedCars) {
if (c.getPlateNumber().equals(plateNumber)) {
car = c;
break;
}
}
if (car != null) {
car.leaveParkingLot();
int fee = car.calculateFee(this.parkingLot.getFee());
this.parkingLot.unpark(spaceId);
return fee;
}
return -1;
}
// 查询空余车位
public int getAvailableSpace() {
return this.parkingLot.getAvailableSpace();
}
// 查询已停车辆
public List<Car> getParkedCars() {
return this.parkingLot.getParkedCars();
}
}
以上就是一个简单的停车场管理系统的设计,可以根据实际需求进行扩展和优化。
原文地址: https://www.cveoy.top/t/topic/bCQ0 著作权归作者所有。请勿转载和采集!