開發(fā)
?
以下為停車場管理系統(tǒng)的代碼:
?
首先定義Car類,記錄車輛信息:
?
```java
public class Car {
? ? private String plateNumber;? //?車牌號
? ? private long enterTime;? //?進入停車場時間
? ? private long exitTime;? //?離開停車場時間
?
? ? public Car(String plateNumber, long enterTime) {
? ? ? ? this.plateNumber = plateNumber;
? ? ? ? this.enterTime = enterTime;
? ? }
?
? ? public String getPlateNumber() {
? ? ? ? return plateNumber;
? ? }
?
? ? public long getEnterTime() {
? ? ? ? return enterTime;
? ? }
?
? ? public long getExitTime() {
? ? ? ? return exitTime;
? ? }
?
? ? public void setExitTime(long exitTime) {
? ? ? ? this.exitTime = exitTime;
? ? }
}
```文章來源地址http://www.zghlxwxcb.cn/news/detail-484825.html
?
定義一個接口,包含小型車庫的基本操作:
?
```java
public interface CarGarage {
? ? boolean isFull();? //?是否已滿
? ? void addCar(Car car);? //?將車輛加入車庫
? ? Car removeCar(String plateNumber);? //?將車輛從車庫中移除
? ? boolean contains(String plateNumber);? //?判斷車庫中是否包含指定車輛
? ? int getSize();? //?返回車庫當(dāng)前的車輛數(shù)量
}
```
?
實現(xiàn)CarGarage接口,使用散列表(Node)進行模擬:
?
```java
public class CarGarageImpl implements CarGarage {
? ? private static final int GARAGE_CAPACITY = 5;? //?小型車庫的容量
? ? private Map<Integer, Node> garageMap = new HashMap<>();
?
? ? //?散列表節(jié)點類
? ? private class Node {
? ? ? ? Car car;
? ? ? ? Node next;
?
? ? ? ? Node(Car car, Node next) {
? ? ? ? ? ? this.car = car;
? ? ? ? ? ? this.next = next;
? ? ? ? }
? ? }
?
? ? //?獲取指定車輛應(yīng)該存儲的散列表下標(biāo)
? ? private int getIndex(String plateNumber) {
? ? ? ? return Math.abs(plateNumber.hashCode()) % GARAGE_CAPACITY;
? ? }
?
? ? @Override
? ? public boolean isFull() {
? ? ? ? int size = 0;
? ? ? ? for (Node node : garageMap.values()) {
? ? ? ? ? ? for (; node != null; node = node.next) {
? ? ? ? ? ? ? ? size++;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return size == GARAGE_CAPACITY;
? ? }
?
? ? @Override
? ? public void addCar(Car car) {
? ? ? ? int index = getIndex(car.getPlateNumber());
? ? ? ? Node node = garageMap.get(index);
? ? ? ? garageMap.put(index, new Node(car, node));
? ? }
?
? ? @Override
? ? public Car removeCar(String plateNumber) {
? ? ? ? int index = getIndex(plateNumber);
? ? ? ? Node node = garageMap.get(index);
? ? ? ? Node prev = null;
?
? ? ? ? while (node != null) {
? ? ? ? ? ? if (node.car.getPlateNumber().equals(plateNumber)) {
? ? ? ? ? ? ? ? if (prev == null) {
? ? ? ? ? ? ? ? ? ? garageMap.put(index, node.next);
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? prev.next = node.next;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? return node.car;
? ? ? ? ? ? }
? ? ? ? ? ? prev = node;
? ? ? ? ? ? node = node.next;
? ? ? ? }
? ? ? ? return null;
? ? }
?
? ? @Override
? ? public boolean contains(String plateNumber) {
? ? ? ? int index = getIndex(plateNumber);
? ? ? ? Node node = garageMap.get(index);
?
? ? ? ? while (node != null) {
? ? ? ? ? ? if (node.car.getPlateNumber().equals(plateNumber)) {
? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? }
? ? ? ? ? ? node = node.next;
? ? ? ? }
? ? ? ? return false;
? ? }
?
? ? @Override
? ? public int getSize() {
? ? ? ? int size = 0;
? ? ? ? for (Node node : garageMap.values()) {
? ? ? ? ? ? for (; node != null; node = node.next) {
? ? ? ? ? ? ? ? size++;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return size;
? ? }
}
```
?
定義一個接口,包含停車場的基本操作:
?
```java
public interface ParkingLot {
? ? void enter(Car car);? //?車輛進場
? ? void exit(Car car);? //?車輛出場
? ? int getEmptySpotCount();? //?返回停車場空余車位數(shù)量
}
```
?
實現(xiàn)ParkingLot接口,使用隊列存儲車輛排隊的情況,每五個車位使用一個小型車庫進行模擬:
?
```java
public class ParkingLotImpl implements ParkingLot {
? ? private static final int PARKING_LOT_CAPACITY = 100;? //?停車場容量
? ? private static final int GARAGE_COUNT = PARKING_LOT_CAPACITY / 5;? //?小型車庫數(shù)量
? ? private static final double PRICE_PER_15_MINUTE = 0.5;? //?普通車輛停車價格:0.5元/15分鐘
? ? private static final double NIGHT_PRICE_PER_HOUR = 1;? //?夜間車輛停車價格:1元/小時
? ? private static final long NIGHT_START_TIME = 22 * 3600 * 1000;? //?夜間計費開始時間:22點
? ? private static final long NIGHT_END_TIME = 6 * 3600 * 1000;? //?夜間計費結(jié)束時間:6點
?
? ? private int emptySpotCount = PARKING_LOT_CAPACITY;? //?停車場空余車位數(shù)量
? ? private Queue<Car>[]?entranceQueues;? //?入口隊列數(shù)組
? ? private Queue<Car> exitQueue = new LinkedList<>();? //?出口排隊隊列
? ? private CarGarage[]?garages;? //?小型車庫數(shù)組
?
? ? public ParkingLotImpl(int entranceCount) {
? ? ? ? entranceQueues = new Queue[entranceCount];
? ? ? ? for (int i = 0; i < entranceCount; i++) {
? ? ? ? ? ? entranceQueues[i]?= new LinkedList<>();
? ? ? ? }
?
? ? ? ? garages = new CarGarage[GARAGE_COUNT];
? ? ? ? for (int i = 0; i < garages.length; i++) {
? ? ? ? ? ? garages[i]?= new CarGarageImpl();
? ? ? ? }
? ? }
?
? ? @Override
? ? public void enter(Car car) {
? ? ? ? int emptyGarageIndex = -1;
? ? ? ? for (int i = 0; i < garages.length; i++) {
? ? ? ? ? ? if (!garages[i].isFull()) {
? ? ? ? ? ? ? ? emptyGarageIndex = i;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
?
? ? ? ? if (emptyGarageIndex != -1) {
? ? ? ? ? ? garages[emptyGarageIndex].addCar(car);
? ? ? ? ? ? emptySpotCount--;
? ? ? ? } else {
? ? ? ? ? ? entranceQueues[0].offer(car); //?第一個入口就放排隊
? ? ? ? }
? ? }
?
? ? private long getParkingTime(Car car) {
? ? ? ? return car.getExitTime() - car.getEnterTime();
? ? }
?
? ? private double calcPrice(long parkingTime) {
? ? ? ? if (parkingTime <= 15 * 60 * 1000) { //?普通車免費15分鐘
? ? ? ? ? ? return 0;
? ? ? ? } else {
? ? ? ? ? ? double price = Math.ceil((double) parkingTime / (15 * 60 * 1000)) * PRICE_PER_15_MINUTE;
? ? ? ? ? ? //?夜間停車價格
? ? ? ? ? ? if (car.getEnterTime() < NIGHT_END_TIME || car.getEnterTime() >= NIGHT_START_TIME) {
? ? ? ? ? ? ? ? price += (double) Math.ceil((double) parkingTime / (60 * 60 * 1000)) * NIGHT_PRICE_PER_HOUR;
? ? ? ? ? ? }
? ? ? ? ? ? return price;
? ? ? ? }
? ? }
?
? ? @Override
? ? public void exit(Car car) {
? ? ? ? int garageIndex = -1;
? ? ? ? for (int i = 0; i < garages.length; i++) {
? ? ? ? ? ? if (garages[i].contains(car.getPlateNumber())) {
? ? ? ? ? ? ? ? garageIndex = i;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
?
? ? ? ? if (garageIndex != -1) {
? ? ? ? ? ? Car removedCar = garages[garageIndex].removeCar(car.getPlateNumber());
? ? ? ? ? ? long parkingTime = removedCar.getExitTime() - removedCar.getEnterTime();
? ? ? ? ? ? double price = calcPrice(parkingTime);
? ? ? ? ? ? emptySpotCount++;
? ? ? ? ? ? System.out.println("離開了停車庫,車牌號為" + removedCar.getPlateNumber() + ",停車時間為" + parkingTime +
? ? ? ? ? ? ? ? ? ? "毫秒,需支付" + price + "元");
? ? ? ? } else {
? ? ? ? ? ? System.out.println("未找到車牌號為" + car.getPlateNumber() + "的車輛");
? ? ? ? }
? ? }
?
? ? @Override
? ? public int getEmptySpotCount() {
? ? ? ? return emptySpotCount;
? ? }
?
? ? //?模擬車輛進場
? ? public void simulateCarEnter() {
? ? ? ? for (Queue<Car> entranceQueue : entranceQueues) {
? ? ? ? ? ? if (entranceQueue.isEmpty()) {
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? }
?
? ? ? ? ? ? //?先檢查是否有空車位,有的話直接入車庫
? ? ? ? ? ? int emptyGarageIndex = -1;
? ? ? ? ? ? for (int i = 0; i < garages.length; i++) {
? ? ? ? ? ? ? ? if (!garages[i].isFull()) {
? ? ? ? ? ? ? ? ? ? emptyGarageIndex = i;
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
?
? ? ? ? ? ? //?沒有空車位
? ? ? ? ? ? if (emptyGarageIndex == -1) {
? ? ? ? ? ? ? ? Car car = entranceQueue.peek();
? ? ? ? ? ? ? ? if (garages[0].contains(car.getPlateNumber())) {
? ? ? ? ? ? ? ? ? ? entranceQueue.poll();? //?已在排隊列中的車,直接取出
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? entranceQueue.offer(car);? //?添加到排隊隊列的末尾等待下次取出
? ? ? ? ? ? ? ? }
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? Car car = entranceQueue.poll();
? ? ? ? ? ? ? ? garages[emptyGarageIndex].addCar(car);
? ? ? ? ? ? ? ? emptySpotCount--;
? ? ? ? ? ? }
? ? ? ? }
? ? }
?
? ? //?模擬車輛出場
? ? public void simulateCarExit() {
? ? ? ? if (exitQueue.isEmpty()) {
? ? ? ? ? ? return;
? ? ? ? }
?
? ? ? ? Car car = exitQueue.peek();
?
? ? ? ? //?夜間停車不允許從22點較晚離開22點較早
? ? ? ? if (car.getEnterTime() < NIGHT_END_TIME || car.getEnterTime() >= NIGHT_START_TIME) {
? ? ? ? ? ? if (System.currentTimeMillis() < NIGHT_START_TIME && car.getExitTime() >= NIGHT_START_TIME) {
? ? ? ? ? ? ? ? System.out.println("當(dāng)前時間為夜間停車計費時間段內(nèi),不允許車牌號為" + car.getPlateNumber() + "的車輛現(xiàn)在離開停車場。");
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? }
?
? ? ? ? exitQueue.poll();
?
? ? ? ? Car removedCar;
? ? ? ? int garageIndex = -1;
? ? ? ? for (int i = 0; i < garages.length; i++) {
? ? ? ? ? ? if (garages[i].contains(car.getPlateNumber())) {
? ? ? ? ? ? ? ? garageIndex = i;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? if (garageIndex != -1) {
? ? ? ? ? ? removedCar = garages[garageIndex].removeCar(car.getPlateNumber());
? ? ? ? ? ? long parkingTime = removedCar.getExitTime() - removedCar.getEnterTime();
? ? ? ? ? ? double price = calcPrice(parkingTime);
? ? ? ? ? ? emptySpotCount++;
? ? ? ? ? ? System.out.println("離開了停車庫,車牌號為" + removedCar.getPlateNumber() + ",停車時間為" + parkingTime +
? ? ? ? ? ? ? ? ? ? "毫秒,需支付" + price + "元");
? ? ? ? } else {
? ? ? ? ? ? System.out.println("未找到車牌號為" + car.getPlateNumber() + "的車輛");
? ? ? ? }
? ? }
?
? ? //?獲取下一個應(yīng)該出場的車輛
? ? private Car getNextExitCar() {
? ? ? ? Car nextCar = null;
? ? ? ? long nextExitTime = Long.MAX_VALUE;
?
? ? ? ? for (CarGarage garage : garages) {
? ? ? ? ? ? for (Node node : ((CarGarageImpl) garage).garageMap.values()) {
? ? ? ? ? ? ? ? for (; node != null; node = node.next) {
? ? ? ? ? ? ? ? ? ? Car car = node.car;
? ? ? ? ? ? ? ? ? ? if (car.getExitTime() < nextExitTime) {
? ? ? ? ? ? ? ? ? ? ? ? nextExitTime = car.getExitTime();
? ? ? ? ? ? ? ? ? ? ? ? nextCar = car;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
?
? ? ? ? return nextCar;
? ? }
?
? ? //?模擬出場排隊
? ? public void simulateExitQueue() {
? ? ? ? if (getEmptySpotCount() == PARKING_LOT_CAPACITY) {
? ? ? ? ? ? return;
? ? ? ? }
?
? ? ? ? while (exitQueue.size() < 1 && getNextExitCar() != null) {
? ? ? ? ? ? Car nextCar = getNextExitCar();
? ? ? ? ? ? exitQueue.offer(nextCar);
? ? ? ? ? ? long waitTime = nextCar.getExitTime() - System.currentTimeMillis();
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? Thread.sleep(waitTime);
? ? ? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
```
?
最后,可以使用以下方式模擬車輛進出停車場的情況:
?
```java
public static void main(String[]?args) {
? ? ParkingLot parkingLot = new ParkingLotImpl(5);
? ? String[]?plateNumbers = {"京A12345", "京A23456", "京A34567", "京A45678", "京A56789", "京A67890",
? ? ? ? ? ? "京B12345", "京B23456", "京B34567", "京B45678", "京B56789", "京B67890"};
? ? Random random = new Random();
?
? ? //?模擬車輛進入停車場
? ? Timer timer = new Timer();
? ? timer.scheduleAtFixedRate(new TimerTask() {
? ? ? ? @Override
? ? ? ? public void run() {
? ? ? ? ? ? Car car = new Car(plateNumbers[random.nextInt(12)], System.currentTimeMillis());
? ? ? ? ? ? parkingLot.enter(car);
? ? ? ? }
? ? }, 0, 500);
?
? ? //?模擬車輛出場
? ? timer.scheduleAtFixedRate(new TimerTask() {
? ? ? ? @Override
? ? ? ? public void run() {
? ? ? ? ? ? parkingLot.simulateExitQueue();
? ? ? ? ? ? parkingLot.simulateCarExit();
? ? ? ? }
? ? }, 0, 1000);
?
? ? //?模擬車輛進入小型車庫
? ? timer.scheduleAtFixedRate(new TimerTask() {
? ? ? ? @Override
? ? ? ? public void run() {
? ? ? ? ? ? parkingLot.simulateCarEnter();
? ? ? ? }
? ? }, 0, 5000);
}
```
?
?
以下是?Python?代碼示例,實現(xiàn)了停車場管理系統(tǒng)的大致功能:
?
```python
import time
from collections import deque
?
#?停車場
class ParkingLot:
? ? def __init__(self, num_entrances, num_exits, num_spaces):
? ? ? ? self.num_entrances = num_entrances
? ? ? ? self.num_exits = num_exits
? ? ? ? self.num_spaces = num_spaces
? ? ? ? self.entrance_queues =?[deque() for _ in range(num_entrances)]
? ? ? ? self.spaces =?[Space() for _ in range(num_spaces)]
? ? ? ? self.night_rate = 1? #?小型車夜間停車費用:1元/小時
?
? ? #?停車
? ? def park(self, car):
? ? ? ? #?獲取可用車位
? ? ? ? space = self.get_empty_space()
? ? ? ? if space is not None:
? ? ? ? ? ? #?將車停入車位
? ? ? ? ? ? space.park(car)
? ? ? ? elif self.is_full():
? ? ? ? ? ? #?如果停車場已滿,則車輛排隊等候
? ? ? ? ? ? print(f"停車場已滿,請{car}在入口處排隊等候")
? ? ? ? ? ? self.entrance_queues[car.entrance].append(car)
? ? ? ? else:
? ? ? ? ? ? raise Exception("Invalid parking state")
?
? ? #?取車
? ? def unpark(self, car):
? ? ? ? space = self.find_space_by_car(car)
? ? ? ? if space is not None:
? ? ? ? ? ? duration = car.get_duration()
? ? ? ? ? ? fee = 0? #?停車費用
? ? ? ? ? ? if car.is_small:
? ? ? ? ? ? ? ? #?夜間停車費用
? ? ? ? ? ? ? ? hour = time.localtime(car.enter_time).tm_hour
? ? ? ? ? ? ? ? if hour >= 22 or hour < 6:
? ? ? ? ? ? ? ? ? ? fee = self.night_rate * duration / 3600
? ? ? ? ? ? #?計算停車費用
? ? ? ? ? ? fee += duration // 900 * 0.5
? ? ? ? ? ? print(f"{car}停車時長為{duration//60}分鐘,停車費用為{fee:.2f}元")
? ? ? ? ? ? space.unpark(car)
? ? ? ? ? ? #?檢查排隊等候的車輛
? ? ? ? ? ? self.check_waiting_cars()
? ? ? ? else:
? ? ? ? ? ? raise Exception(f"{car}不在停車場內(nèi)")
?
? ? #?獲取可用車位
? ? def get_empty_space(self):
? ? ? ? for space in self.spaces:
? ? ? ? ? ? if not space.is_occupied():
? ? ? ? ? ? ? ? return space
? ? ? ? return None
?
? ? #?根據(jù)車牌號查找車位
? ? def find_space_by_car(self, car):
? ? ? ? for space in self.spaces:
? ? ? ? ? ? if space.car == car:
? ? ? ? ? ? ? ? return space
? ? ? ? return None
?
? ? #?停車場是否已滿
? ? def is_full(self):
? ? ? ? for space in self.spaces:
? ? ? ? ? ? if not space.is_occupied():
? ? ? ? ? ? ? ? return False
? ? ? ? return True
?
? ? #?檢查排隊等候的車輛
? ? def check_waiting_cars(self):
? ? ? ? for i in range(self.num_entrances):
? ? ? ? ? ? if len(self.entrance_queues[i]) > 0:
? ? ? ? ? ? ? ? car = self.entrance_queues[i][0]
? ? ? ? ? ? ? ? space = self.get_empty_space()
? ? ? ? ? ? ? ? if space is not None:
? ? ? ? ? ? ? ? ? ? self.entrance_queues[i].popleft()
? ? ? ? ? ? ? ? ? ? self.park(car)
? ? ? ? ? ? ? ? ? ? print(f"{car}入場成功")
?
?
#?車位
class Space:
? ? def __init__(self):
? ? ? ? self.car = None
?
? ? #?判斷車位是否被占用
? ? def is_occupied(self):
? ? ? ? return self.car is not None
?
? ? #?停車
? ? def park(self, car):
? ? ? ? self.car = car
? ? ? ? car.enter_time = time.time()
?
? ? #?取車
? ? def unpark(self, car):
? ? ? ? self.car = None
?
?
#?車輛
class Car:
? ? def __init__(self, plate, entrance, is_small):
? ? ? ? self.plate = plate? #?車牌號碼
? ? ? ? self.entrance = entrance? #?入口編號
? ? ? ? self.is_small = is_small? #?是否小型車
? ? ? ? self.enter_time = None? #?進場時間
?
? ? #?獲取停車時長,返回秒數(shù)
? ? def get_duration(self):
? ? ? ? if self.enter_time is not None:
? ? ? ? ? ? return time.time() - self.enter_time
? ? ? ? else:
? ? ? ? ? ? return -1
?
? ? def __str__(self):
? ? ? ? return self.plate
?
?
if __name__ == '__main__':
? ? lot = ParkingLot(2, 2, 25)
? ? for i in range(80):
? ? ? ? is_small = i % 5 == 0? #?每5個車位是一個小型車庫
? ? ? ? car = Car(f"京A{i:02d}", i % 2, is_small)
? ? ? ? if i == 40:
? ? ? ? ? ? lot.unpark(car)? #?取走一輛不存在的車
? ? ? ? else:
? ? ? ? ? ? lot.park(car)
? ? ? ? time.sleep(0.1)? #?模擬每隔一段時間有車進入或離開停車場文章來源:http://www.zghlxwxcb.cn/news/detail-484825.html
```
到了這里,關(guān)于停車場管理系統(tǒng)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!