物品租赁程序python代码
时间: 2023-11-13 22:04:47 浏览: 53
以下是一个简单的物品租赁程序的Python代码示例:
```python
rented_items = {}
class Item:
def __init__(self, name, price_per_day):
self.name = name
self.price_per_day = price_per_day
self.is_rented = False
def rent(self):
self.is_rented = True
def return_item(self):
self.is_rented = False
def get_rental_price(self, days):
return self.price_per_day * days
class Rental:
def __init__(self, item, renter, days):
self.item = item
self.renter = renter
self.days = days
self.price = item.get_rental_price(days)
def print_rental_info(self):
print(f"Rental information:\nItem: {self.item.name}\nRenter: {self.renter}\nDays rented: {self.days}\nTotal price: {self.price}")
def return_item(self):
self.item.return_item()
rented_items.pop(self.item.name, None)
def rent_item(item_name, renter, days):
if item_name in rented_items:
print("This item is already rented.")
return
item = items[item_name]
item.rent()
rental = Rental(item, renter, days)
rental.print_rental_info()
rented_items[item_name] = rental
items = {
"bike": Item("Bike", 10),
"car": Item("Car", 50),
"boat": Item("Boat", 100)
}
while True:
print("Welcome to the rental program!")
print("Select an option:")
print("1. Rent an item")
print("2. Exit")
choice = input()
if choice == "1":
print("Select an item to rent:")
for item_name in items:
item = items[item_name]
if not item.is_rented:
print(f"{item_name}: {item.price_per_day}/day")
item_name = input()
if item_name not in items:
print("Invalid item selection.")
continue
print("Enter your name:")
renter = input()
print("Enter the number of days you want to rent the item:")
days = int(input())
rent_item(item_name, renter, days)
elif choice == "2":
break
else:
print("Invalid choice.")
```
这个程序使用了一个`Item`类来表示租赁的物品,以及一个`Rental`类来表示租赁信息。程序在一个循环中接受用户输入来租赁物品,并记录已经租赁的物品。当用户要归还物品时,程序会从已租赁的物品列表中删除该物品。
阅读全文