Created
May 12, 2023 09:20
-
-
Save seunoyeniyi/cb3827539f88b9a39824918702a28982 to your computer and use it in GitHub Desktop.
Scooter Rental Management
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#OYENIYI SEUN TAIWO | |
#CSC/2021/235 | |
class Scooter: | |
def __init__(self, rental_company, starting_fee, price_per_100m, available_distance): | |
self.rental_company = rental_company | |
self.starting_fee = starting_fee | |
self.price_per_100m = price_per_100m | |
self.available_distance = available_distance | |
def price(self, ride_length): | |
if ride_length <= self.available_distance: | |
ride_price = self.starting_fee + (ride_length * self.price_per_100m * 10) | |
return ride_price | |
else: | |
return 1000 | |
def ride(self, ride_length): | |
if ride_length <= self.available_distance: | |
self.available_distance -= ride_length | |
if self.available_distance < 0: | |
self.available_distance = 0 | |
def charge(self, kilometers): | |
self.available_distance += kilometers | |
class Rental: | |
def __init__(self, scooters): | |
self.scooters = scooters | |
def display_choices(self, ride_length): | |
choices = [] | |
for scooter in self.scooters: | |
ride_price = scooter.price(ride_length) | |
choices.append((scooter.rental_company, ride_price)) | |
choices.sort(key=lambda x: x[1]) # Sort by ride price ascending | |
for choice in choices: | |
print(f"{choice[0]}: {choice[1]}") | |
def rent(self, scooter_name, ride_length): | |
for scooter in self.scooters: | |
if scooter.rental_company == scooter_name: | |
if ride_length <= scooter.available_distance: | |
ride_price = scooter.price(ride_length) | |
print(f"Price of the ride: {ride_price}") | |
scooter.ride(ride_length) | |
return | |
else: | |
print("Ride length exceeds available riding distance.") | |
return | |
print("Scooter not found.") | |
def charge_scooter(self, scooter_name, kilometers): | |
for scooter in self.scooters: | |
if scooter.rental_company == scooter_name: | |
scooter.charge(kilometers) | |
return | |
print("Scooter not found.") | |
# Creating instances of the Scooter class | |
scooter1 = Scooter("Bolt", 1.5, 0.1, 20) | |
scooter2 = Scooter("Tuul", 1, 0.15, 18) | |
scooter3 = Scooter("Bird", 0, 0.3, 34) | |
# Creating an instance of the Rental class | |
rental = Rental([scooter1, scooter2, scooter3]) | |
# get all prices from a distance | |
distance = float(input("Enter your distance: ")) | |
print("Your prices are:") | |
rental.display_choices(distance) | |
# get price for a particular company | |
print("___________________________________") | |
rental.rent("Bolt", 3) | |
rental.rent("Tuul", 18) | |
rental.rent("Bird", 5) | |
# Calling the methods on the rental instance | |
print("___________________________________") | |
rental.charge_scooter("Tuul", 5) | |
rental.rent("Tuul", 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment