根据老师要求,让我们写一个基于python的停车管理系统,一下是我的代码:
import os
import json
from datetime import datetime, timedelta
class ParkingSystem:
def __init__(self):
self.cars_file = 'cars.json'
self.monthly_pass_file = 'monthly_pass.json'
self.parking_spaces = 10
self.parked_cars = []
self.monthly_pass_holders = []
self.load_cars()
self.load_monthly_pass()
def load_cars(self):
if os.path.exists(self.cars_file):
with open(self.cars_file, 'r') as file:
self.parked_cars = json.load(file)
def load_monthly_pass(self):
if os.path.exists(self.monthly_pass_file):
with open(self.monthly_pass_file, 'r') as file:
self.monthly_pass_holders = json.load(file)
def save_cars(self):
with open(self.cars_file, 'w') as file:
json.dump(self.parked_cars, file)
def save_monthly_pass(self):
with open(self.monthly_pass_file, 'w') as file:
json.dump(self.monthly_pass_holders, file)
def display_menu(self):
print("1. 停车")
print("2. 离开")
print("3. 显示停车场状态")
print("4. 查询停车信息")
print("5. 购买月卡")
print("6. 查询月卡信息")
print("7. 退出")
def park_car(self, license_plate):
if len(self.parked_cars) < self.parking_spaces:
entry_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
car_info = {'license_plate': license_plate, 'entry_time': entry_time}
self.parked_cars.append(car_info)
self.save_cars()
print(f"车牌号为 {license_plate} 的车辆已成功停车。")
else:
print("停车场已满,无法停车。")
def leave_parking(self, license_plate):
for car in self.parked_cars:
if car['license_plate'] == license_plate:
entry_time = datetime.strptime(car['entry_time'], "%Y-%m-%d %H:%M:%S")
exit_time = datetime.now()
duration = exit_time - entry_time
fee = self.calculate_fee(duration, license_plate)
print(f"车牌号为 {license_plate} 的车辆已成功离开。")
print(f"停车时长: {duration}")
print(f"停车费用: {fee}")
self.parked_cars.remove(car)
self.save_cars()
return
print(f"未找到车牌号为 {license_plate} 的车辆。")
def calculate_fee(self, duration, license_plate):
# 这里简单计算停车费用,如果有月卡,打八折
hours = duration.total_seconds() / 3600
regular_fee = hours * 5
if license_plate in self.monthly_pass_holders:
return round(regular_fee * 0.8, 2)
else:
return round(regular_fee, 2)
def display_parking_status(self):
print(f"停车场总共有 {self.parking_spaces} 个停车位,当前有 {len(self.parked_cars)} 辆车停放。")
def query_parking_info(self, license_plate):
for car in self.parked_cars:
if car['license_plate'] == license_plate:
entry_time = datetime.strptime(car['entry_time'], "%Y-%m-%d %H:%M:%S")
print(f"车牌号为 {license_plate} 的车辆当前停车信息:")
print(f"入场时间: {entry_time}")
return
print(f"未找到车牌号为 {license_plate} 的车辆。")
def purchase_monthly_pass(self, license_plate):
if license_plate not in self.monthly_pass_holders:
self.monthly_pass_holders.append(license_plate)
self.save_monthly_pass()
print(f"成功购买月卡,车牌号为 {license_plate}。")
else:
print("该车辆已经拥有月卡。")
def display_monthly_pass_holders(self):
print("已购买月卡的车辆信息:")
for license_plate in self.monthly_pass_holders:
print(f"车牌号: {license_plate}")
def run(self):
while True:
self.display_menu()
choice = input("请选择操作 (1-7): ")
if choice == '1':
license_plate = input("请输入车牌号: ")
self.park_car(license_plate)
elif choice == '2':
license_plate = input("请输入车牌号: ")
self.leave_parking(license_plate)
elif choice == '3':
self.display_parking_status()
elif choice == '4':
license_plate = input("请输入要查询的车牌号: ")
self.query_parking_info(license_plate)
elif choice == '5':
license_plate = input("请输入要购买月卡的车牌号: ")
self.purchase_monthly_pass(license_plate)
elif choice == '6':
self.display_monthly_pass_holders()
elif choice == '7':
print("感谢使用停车管理系统,再见!")
break
else:
print("无效的选择,请重新输入。")
if __name__ == "__main__":
parking_system = ParkingSystem()
parking_system.run()