[go: up one dir, main page]

0% found this document useful (0 votes)
28 views8 pages

Source Code

Uploaded by

Devang Vasava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views8 pages

Source Code

Uploaded by

Devang Vasava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

SOURCE CODE

car_rental.py

from datetime import datetime, timedelta

class CarRental:
def __init__(self, stock=0):
self.stock = stock

def display_stock(self):
return f"Available cars: {self.stock}"

def rent_hourly(self, n):


if n <= 0:
return "Number of cars should be positive."
elif n > self.stock:
return f"Sorry, only {self.stock} cars available."
else:
self.stock -= n
now = datetime.now()
print(f"Hourly rental start time: {now}")
return now

def rent_daily(self, n):


if n <= 0:
return "Number of cars should be positive."
elif n > self.stock:
return f"Sorry, only {self.stock} cars available."
else:
self.stock -= n
now = datetime.now()
print(f"Daily rental start time: {now}")
return now

def rent_weekly(self, n):


if n <= 0:
return "Number of cars should be positive."
elif n > self.stock:
return f"Sorry, only {self.stock} cars available."
else:
self.stock -= n
now = datetime.now()
print(f"Weekly rental start time: {now}")
return now

def return_car(self, request):


rental_time, rental_basis, num_of_cars = request
bill = 0
if rental_time and rental_basis and num_of_cars:
self.stock += num_of_cars
now = datetime.now()
rental_period = now - rental_time

# Debugging logs
print(f"Rental start time: {rental_time}")
print(f"Current time: {now}")
print(f"Rental period: {rental_period}")
print(f"Number of cars: {num_of_cars}")

if rental_basis == 1: # hourly
hours = rental_period.total_seconds() / 3600
bill = round(hours) * 5 * num_of_cars
print(f"Hourly rental: {round(hours)} hours at $5/hour")

elif rental_basis == 2: # daily


days = rental_period.days
bill = days * 20 * num_of_cars
print(f"Daily rental: {days} days at $20/day")

elif rental_basis == 3: # weekly


weeks = rental_period.days // 7
bill = weeks * 60 * num_of_cars
print(f"Weekly rental: {weeks} weeks at $60/week")

# Apply discounts
if 2 <= num_of_cars <= 5:
bill *= 0.9 # 10% discount
print("Applied 10% discount")
elif num_of_cars >= 6:
bill *= 0.8 # 20% discount
print("Applied 20% discount")

return f"Bill: ${bill:.2f}"


else:
return "Invalid return request."

class Customer:
def __init__(self):
self.cars = 0
self.rental_basis = 0
self.rental_time = 0

def request_car(self):
cars = input("How many cars would you like to rent? ")
try:
cars = int(cars)
except ValueError:
print("Number of cars should be a positive integer.")
return -1
if cars < 1:
print("Invalid number of cars.")
return -1
else:
self.cars = cars
return self.cars

def return_car(self):
if self.rental_time and self.rental_basis and self.cars:
return self.rental_time, self.rental_basis, self.cars
else:
return 0, 0, 0
car_rental_system.ipynb

import car_rental

def main():
car_rental_company = car_rental.CarRental(10) # Initial stock of 10 cars
customer = car_rental.Customer()

while True:
print("""
====== Car Rental System =======
1. Display available cars
2. Request a car on hourly basis $5/hour
3. Request a car on daily basis $20/day
4. Request a car on weekly basis $60/week
5. Return a car
6. Exit
""")
choice = input("Enter choice: ")

try:
choice = int(choice)
except ValueError:
print("Invalid input. Please enter a number between 1 and 6.")
continue
if choice == 1:
print(car_rental_company.display_stock())
elif choice == 2:
customer.cars = customer.request_car()
if customer.cars != -1:
customer.rental_basis = 1
customer.rental_time =
car_rental_company.rent_hourly(customer.cars)
elif choice == 3:
customer.cars = customer.request_car()
if customer.cars != -1:
customer.rental_basis = 2
customer.rental_time = car_rental_company.rent_daily(customer.cars)
elif choice == 4:
customer.cars = customer.request_car()
if customer.cars != -1:
customer.rental_basis = 3
customer.rental_time =
car_rental_company.rent_weekly(customer.cars)
elif choice == 5:
request = customer.return_car()
bill = car_rental_company.return_car(request)
print(bill)
customer.rental_basis, customer.rental_time, customer.cars = 0, 0, 0
elif choice == 6:
break
else:
print("Invalid input. Please enter a number between 1 and 6.")

if __name__ == "__main__":
main()

You might also like