python code on car management system

 car management system 

 Creating a car management system can be a challenging project for a class 12 student

Code:-

class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year

    def display(self):
        print(f"Make: {self.make}, Model: {self.model}, Year: {self.year}")


class CarManagementSystem:
    def __init__(self):
        self.cars = []

    def add_car(self):
        make = input("Enter the make of the car: ")
        model = input("Enter the model of the car: ")
        year = input("Enter the year of the car: ")
        car = Car(make, model, year)
        self.cars.append(car)
        print("Car added successfully!")

    def view_cars(self):
        if not self.cars:
            print("No cars in the system.")
        else:
            print("Cars in the system:")
            for i, car in enumerate(self.cars, 1):
                print(f"{i}.")
                car.display()

    def delete_car(self):
        if not self.cars:
            print("No cars in the system to delete.")
            return

        self.view_cars()
        try:
            index = int(input("Enter the index of the car to delete: ")) - 1
            if 0 <= index < len(self.cars):
                deleted_car = self.cars.pop(index)
                print(f"Deleted the following car:")
                deleted_car.display()
            else:
                print("Invalid car index.")
        except ValueError:
            print("Invalid input. Please enter a valid index.")

    def menu(self):
        while True:
            print("\nCar Management System Menu:")
            print("1. Add Car")
            print("2. View Cars")
            print("3. Delete Car")
            print("4. Exit")

            choice = input("Enter your choice: ")

            if choice == "1":
                self.add_car()
            elif choice == "2":
                self.view_cars()
            elif choice == "3":
                self.delete_car()
            elif choice == "4":
                print("Exiting the Car Management System.")
                break
            else:
                print("Invalid choice. Please select a valid option.")


def main():
    car_system = CarManagementSystem()
    car_system.menu()


if __name__ == "__main__":
    main()

Comments

Popular Posts