Managing a car rental business involves tracking vehicles, customers, ongoing rentals, and transactions. This guide walks you through building a menu-based car rental management system in Python, leveraging text files for data storage and operations. The system includes features for renting cars, returning rentals, and calculating revenue.
vehicles.txt:
vehicles.txt
AMG-111,Toyota Yaris,65,Air Conditioning,Hybrid,Automatic Transmission
customers.txt:
customers.txt
09/09/1999,Tom,Shark,Tom.Shark@gmail.com
rentedVehicles.txt:
rentedVehicles.txt
CHF-337,22/02/1961,25/10/2024 12:33
transActions.txt:
transActions.txt
BMC-69,17/04/1997,15/10/2024 14:01,19/10/2024 22:20,5,210.00
The program begins with a user-friendly menu offering the following options:
def main_menu(): print("Welcome to the Car Rental Management System") print("1. List Available Cars") print("2. Rent a Car") print("3. Return a Car") print("4. Count Total Revenue") print("5. Exit")
Display cars from vehicles.txt that are not currently rented (not listed in rentedVehicles.txt).
Logic:
def list_available_cars(): with open('vehicles.txt', 'r') as vehicles: cars = vehicles.readlines() with open('rentedVehicles.txt', 'r') as rented: rented_cars = [line.split(',')[0] for line in rented.readlines()] available_cars = [car for car in cars if car.split(',')[0] not in rented_cars] print("Available Cars:") for car in available_cars: print(car.strip())
Allow customers to rent a car, adding their details if they are new.
def rent_car(): car_plate = input("Enter the car's registration plate: ") customer_birthdate = input("Enter your birthdate (DD/MM/YYYY): ") with open('rentedVehicles.txt', 'r') as rented: rented_cars = [line.split(',')[0] for line in rented.readlines()] if car_plate in rented_cars: print("Car is already rented.") return with open('customers.txt', 'r') as customers: existing_customers = [line.split(',')[0] for line in customers.readlines()] if customer_birthdate not in existing_customers: first_name = input("Enter your first name: ") last_name = input("Enter your last name: ") email = input("Enter your email: ") with open('customers.txt', 'a') as customers: customers.write(f"{customer_birthdate},{first_name},{last_name},{email}\n") rental_time = input("Enter rental start time (DD/MM/YYYY HH:MM): ") with open('rentedVehicles.txt', 'a') as rented: rented.write(f"{car_plate},{customer_birthdate},{rental_time}\n") print("Car successfully rented!")
Calculate rental duration and price, update transActions.txt, and remove from rentedVehicles.txt.
Read transActions.txt and sum the total revenue.
def count_total_revenue(): total_revenue = 0 with open('transActions.txt', 'r') as transactions: for line in transactions: total_revenue += float(line.split(',')[-1]) print(f"Total Revenue: ${total_revenue:.2f}")
This project combines file handling with Python programming to create a robust car rental management system. From tracking vehicles to managing customers and rentals, this system is a practical tool for small businesses.
If you're working on a similar project, feel free to adapt this guide to your needs. Need help with implementation? Reach out for expert support!