你能用Python写一个停车场管理系统的代码吗
时间: 2023-03-01 09:41:57 浏览: 97
2022基于python的停车场管理系统源码+数据库(课程设计)
5星 · 资源好评率100%
当然,这是一个很好的Python项目。这里是一个简单的实现,涵盖了停车场的基本功能:入场、出场和收费。
```
class Car:
def __init__(self, license_number):
self.license_number = license_number
class ParkingLot:
def __init__(self, capacity):
self.capacity = capacity
self.cars = {}
def is_full(self):
return len(self.cars) >= self.capacity
def park(self, car):
if self.is_full():
print(f"Parking lot is full, {car.license_number} can't park.")
else:
self.cars[car.license_number] = car
print(f"{car.license_number} parked successfully.")
def leave(self, license_number):
if license_number in self.cars:
self.cars.pop(license_number)
print(f"{license_number} left the parking lot.")
else:
print(f"{license_number} not found in the parking lot.")
def charge(self, license_number):
hours = 2
fee = 10
print(f"{license_number} needs to pay {hours * fee} for {hours} hours.")
parking_lot = ParkingLot(5)
car1 = Car("123456")
parking_lot.park(car1)
parking_lot.leave("123457")
parking_lot.charge("123456")
```
这只是一个简单的例子,您可以根据需求进一步完善它,例如添加时间戳,记录停车记录等。
阅读全文