Prompt
Answer
Pseudo Code for Car Management System
Functions Overview
The car management system includes the following core functionalities:
- Get boolean input from the user for specific prompts.
- Add a car with necessary details.
- View available cars.
- Buy, rent, and return cars.
- Initialize default cars for the system.
1. Get Boolean Input Function
Function to prompt the user for a yes/no input, ensuring valid integer input.
FUNCTION get_boolean_input(prompt):
WHILE TRUE:
GET value FROM USER
IF value == 0 OR value == 1:
RETURN CONVERT value TO BOOLEAN
ELSE:
PRINT "Invalid integer. Please enter 1 for yes or 0 for no."
2. Add Car Function
Function to add a car to the system while checking for restrictions.
FUNCTION add_car():
IF num_cars >= MAX_CARS:
PRINT "Maximum number of cars reached."
RETURN
GET brand FROM USER
GET model FROM USER
FOR SALE = get_boolean_input("Is the car for sale? (1 for yes, 0 for no): ")
IF FOR SALE:
GET sell_price FROM USER
ELSE:
SET sell_price = 0.0
FOR RENT = get_boolean_input("Is the car for rent? (1 for yes, 0 for no): ")
IF FOR RENT:
GET rent_price_rate FROM USER
ELSE:
SET rent_price_rate = 0.0
IF NOT FOR SALE AND NOT FOR RENT:
PRINT "Error: The car must be either for sale or for rent."
RETURN
SET brands[num_cars] = brand
SET models[num_cars] = model
SET sell_prices[num_cars] = sell_price
SET rent_price_rates[num_cars] = rent_price_rate
SET for_sell[num_cars] = FOR SALE
SET for_rent[num_cars] = FOR RENT
SET sold[num_cars] = FALSE
SET rented[num_cars] = FALSE
INCREMENT num_cars
PRINT "Car added successfully."
3. View Cars Function
Function to display the list of cars and allow selection.
FUNCTION view_cars():
PRINT "----- View Cars -----"
IF num_cars == 0:
RETURN -1
PRINT HEADER FOR CAR LIST
FOR i FROM 0 TO num_cars - 1:
DETERMINE status (Sold, Rented, Available)
FORMAT sell_price AND rent_price
PRINT car DETAILS
WHILE TRUE:
GET car_choice FROM USER
IF car_choice == 0:
RETURN 0
ELSE IF 1 <= car_choice <= num_cars:
RETURN car_choice
ELSE:
PRINT "Invalid car selection."
4. Buy Car Function
Function to handle the purchase of a car.
FUNCTION buy_car(car_index):
IF sold[car_index]:
PRINT "Car already sold."
ELSE IF rented[car_index]:
PRINT "Car currently rented."
ELSE IF NOT for_sell[car_index]:
PRINT "Car not for sale."
ELSE:
SET sold[car_index] = TRUE
PRINT "Car bought successfully. Cost: sell_prices[car_index]"
5. Rent Car Function
Function to handle the renting of a car.
FUNCTION rent_car(car_index):
IF sold[car_index]:
PRINT "Car already sold."
ELSE IF rented[car_index]:
PRINT "Car currently rented."
ELSE IF NOT for_rent[car_index]:
PRINT "Car not for rent."
ELSE:
SET rented[car_index] = TRUE
PRINT "Car rented successfully. Cost: rent_price_rates[car_index] per day"
6. Return Car Function
Function to handle the return of a rented car.
FUNCTION return_car(car_index):
IF NOT rented[car_index]:
PRINT "Car is not rented."
ELSE:
SET rented[car_index] = FALSE
PRINT "Car returned successfully."
7. Initialize Default Cars Function
Function to populate the system with initial car data.
FUNCTION initialize_default_cars():
SET default_cars TO A LIST OF DEFAULT CARS
FOR EACH car IN default_cars:
ADD car TO brands, models, sell_prices, rent_price_rates, for_sell, for_rent, sold, rented
INCREMENT num_cars
8. Main Function
Control the flow of operations in the car management system.
FUNCTION main():
CALL initialize_default_cars()
WHILE TRUE:
GET choice FROM display_main_menu()
IF choice == "1":
CALL add_car()
ELSE IF choice == "2":
SET car_choice = CALL view_cars()
IF car_choice == 0:
CALL clear_terminal()
PRINT "Going back to main menu..."
ELSE IF car_choice >= 1:
SET car_action = CALL display_car_actions_menu(car_choice - 1)
IF car_action == 1:
CALL buy_car(car_choice - 1)
ELSE IF car_action == 2:
CALL rent_car(car_choice - 1)
ELSE IF car_action == 3:
CALL return_car(car_choice - 1)
ELSE IF car_action == 0:
CALL clear_terminal()
PRINT "Going back to main menu..."
ELSE:
PRINT "No cars available."
ELSE IF choice == "3":
PRINT "Exiting program."
BREAK
ELSE:
PRINT "Invalid choice. Please enter a number between 1 and 3."
PRINT " "
Main Execution
To start the program, the following structure serves as the entry point:
IF __name__ == "__main__":
CALL main()
This pseudo code provides a clear and structured documentation of a car management system. Each function and its logic is presented in a straightforward manner, allowing for intuitive understanding and potential implementation in a programming language.
Description
This document outlines the pseudo code for a car management system, detailing functionalities such as adding, viewing, buying, renting, and returning cars along with user input handling mechanisms for efficient operations.