Skip to content

Car-Wash Utility in Python

Requirements Short

  • 10 cars in a row simultaneously - theres no more space for any more cars than that
  • use a class car - to have all the cars in one class
  • Develop a Menu - easy to understand and use
  • show cars in row - to get a quick output to see all the cars

Requirements Long

Vor einer Autowaschanlage gibt es regelmäßig eine Schlange an Autos, 
die gewaschen werden sollen. Es soll nun ein Programm entwickelt werden, 
welches diese Warteschlange verwaltet. Aus Erfahrungsgründen (und aus 
Platzgründen) sind nie mehr als 10 Autos in dieser Schlange. Natürlich 
soll sich ein neu hinzukommendes Auto am Ende der Schlange anstellen 
(solange noch Platz ist), außerdem sollen alle wartende Autos aufrücken, 
wenn das an erster Stelle stehende Auto in die Anlage einfahren darf. 
Autos können natürlich jederzeit die Schlange verlassen und wieder 
"nach Hause" fahren.
  • Modellieren Sie eine geeignete Klasse Auto.
  • Modellieren Sie eine geeignete Klasse Warteschlange.
  • Erstelle eine Konsolen Applikation, welche das gegebene Problem mittels Menü steuern lässt

Source Code

import os

def clear_screen():
    os.system("cls" if os.name == "nt" else "clear")

class Car:
    def __init__(self, license_plate):
        self.license_plate = license_plate

    def __str__(self):
        return f"Car [{self.license_plate}]"

class Queue:
    def __init__(self):
        self.line = []
        self.max_cars = 10

    def add_car(self, car):
        if len(self.line) < self.max_cars:
            self.line.append(car)
            print(f"\n{car} has been added to the queue.\n")
        else:
            print("\nThe queue is full. Car cannot be added.\n")

    def process_next_car(self):
        if self.line:
            car = self.line.pop(0)
            print(f"\n{car} is entering the car wash.\n")
        else:
            print("\nNo cars in the queue.\n")

    def remove_car_by_license(self, license_plate):
        for car in self.line:
            if car.license_plate == license_plate:
                self.line.remove(car)
                print(f"\n{car} has left the queue.\n")
                return
        print(f"\nNo car with license plate '{license_plate}' found in the queue.\n")

    def show_queue(self):
        if self.line:
            print("\nCurrent queue:")
            for index, car in enumerate(self.line, start=1):
                print(f"{index}. {car}")
            print()
        else:
            print("\nThe queue is empty.\n")

def menulist():
    print("--- Car Wash Menu ---")
    print("1: Add car to queue")
    print("2: Process next car (enter car wash)")
    print("3: Remove car from queue (by license plate)")
    print("4: Show current queue")
    print("5: Exit program")

    choice = input("\nYour choice: ")
    return choice

def menu():
    queue = Queue()
    choice = 0

    while choice == 0:
        clear_screen()
        choice = menulist()

        clear_screen()

        if choice == "1":
            license_plate = input("Enter license plate: ")
            car = Car(license_plate)
            queue.add_car(car)
            input("Press Enter to return to the menu...")
            choice = 0

        elif choice == "2":
            queue.process_next_car()
            input("Press Enter to return to the menu...")
            choice = 0

        elif choice == "3":
            license_plate = input("Enter license plate to remove: ")
            queue.remove_car_by_license(license_plate)
            input("Press Enter to return to the menu...")
            choice = 0

        elif choice == "4":
            queue.show_queue()
            input("Press Enter to return to the menu...")
            choice = 0

        elif choice == "5":
            print("\nExiting program.\n")
            break

        else:
            print("\nInvalid input. Please choose a number from 1 to 5.\n")
            input("Press Enter to return to the menu...")

if __name__ == "__main__":
    menu()