CECS 524 Program 3 DP Decorator_030849570

pdf

School

California State University, Long Beach *

*We aren’t endorsed by this school

Course

MISC

Subject

Computer Science

Date

Feb 20, 2024

Type

pdf

Pages

7

Uploaded by MajorUniverseDragonfly48

Report
Assignment CECS 524 Program 3 DP Decorator Divyashree KM 030849570 Question: 1 Create a program that calculates the cost of a custom-built car. The car comes in three basic models: C, E, and CS. The C model costs $32,000. The E model costs $45,000. The CS model costs $60,000. Each model can have any number of additional features at additional cost. The features, description and cost are as Follows: • V-8 Engine, $4400 • V-12 Engine, $6400 • Sun Roof, $1940, $2240, $3400 (C, E and CS respectively) • Oversized Gas Tank, $940 • Blaupunkt New York 800 Radio, $940 • Spare Tire, $440 Solution: The below code utilizes Tkinter for a modest GUI, drop-down boxes for car model selection, checkboxes for feature selection, and adheres to the specified restrictions, including the Decorator design pattern. We can run this code and test it with different car models and features. Code: import tkinter as tk class Car: def __init__(self, model, cost): self.model = model self.cost = cost def get_description(self): return f"{self.model}" def get_cost(self): return self.cost class CarDecorator(Car): def __init__(self, car, feature, cost): super().__init__(car.model, car.cost) self.car = car self.feature = feature self.cost = cost
def get_description(self): return f"{self.car.get_description()} with {self.feature}" def get_cost(self): return self.car.get_cost() + self.cost # Function to calculate the total cost and print order details def calculate_cost(): # Get the selected car model car_model = car_model_entry.get() # Check if the user entered "quit" or "Quit" if car_model.lower() in ["quit"]: root.destroy() # Close the GUI window return # Create a car object based on the selected model if car_model == "C": current_car = Car("C", 32000) elif car_model == "E": current_car = Car("E", 45000) elif car_model == "CS": current_car = Car("CS", 60000) else: print("Invalid car model. Please enter 'C', 'E', or 'CS'.") return # Add selected features to the car selected_features = [] for i, feature_name in enumerate(["V-8 Engine", "V-12 Engine", "Sun Roof", "Oversized Gas Tank", "Blaupunkt New York 800 Radio", "Spare Tire"]): feature_var = feature_labels[feature_name] if feature_var.get(): current_car = add_feature(current_car, feature_name, car_model) selected_features.append(feature_name) # Calculate the total cost total_cost = current_car.get_cost() # Display the result result_label.config(text=f"Total Cost: ${total_cost:,.2f}") # Print order details in the console print_order(car_model, selected_features, total_cost) # Function to add features using the Decorator pattern def add_feature(car, feature_name, car_model):
if feature_name == "V-8 Engine": return CarDecorator(car, "V-8 Engine", 4400) elif feature_name == "V-12 Engine": return CarDecorator(car, "V-12 Engine", 6400) elif feature_name == "Sun Roof": sun_roof_costs = {"C": 1940, "E": 2240, "CS": 3400} return CarDecorator(car, "Sun Roof", sun_roof_costs[car_model]) elif feature_name == "Oversized Gas Tank": return CarDecorator(car, "Oversized Gas Tank", 940) elif feature_name == "Blaupunkt New York 800 Radio": return CarDecorator(car, "Blaupunkt New York 800 Radio", 940) elif feature_name == "Spare Tire": return CarDecorator(car, "Spare Tire", 440) else: return car # Function to print order details in the console def print_order(car_model, selected_features, total_cost): print("\nNew Order:") features_str = ", ".join(selected_features) print(f"Car: {car_model} with {features_str}") print(f"Cost: ${total_cost:,.2f}") print("End Order\n") # Initialize the Tkinter GUI root = tk.Tk() root.title("Custom Car Cost Calculator") # Car model entry label and entry box car_model_label = tk.Label(root, text="Enter car model:") car_model_label.grid(row=0, column=0, pady=(10, 5)) car_model_entry = tk.Entry(root) car_model_entry.grid(row=0, column=1, pady=(10, 5)) # Feature selection labels and checkboxes feature_labels = {} for i, feature_name in enumerate(["V-8 Engine", "V-12 Engine", "Sun Roof", "Oversized Gas Tank", "Blaupunkt New York 800 Radio", "Spare Tire"]): feature_var = tk.IntVar(root) feature_checkbox = tk.Checkbutton(root, text=feature_name, variable=feature_var) feature_checkbox.grid(row=i+1, column=0, padx=10, pady=5, sticky="w") feature_labels[feature_name] = feature_var # Calculate cost button calculate_button = tk.Button(root, text="Calculate Cost", command=calculate_cost) calculate_button.grid(row=len(feature_labels)+1, column=0, columnspan=2, pady=(10, 5))
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
# Quit button quit_button = tk.Button(root, text="Quit", command=root.destroy) quit_button.grid(row=len(feature_labels)+2, column=0, columnspan=2, pady=(5, 10)) # Result label result_label = tk.Label(root, text="") result_label.grid(row=len(feature_labels)+3, column=0, columnspan=2, pady=(0, 10)) # Run the GUI root.mainloop() For car model C: For car model E:
For car model CS: For car model ‘C’ choose all the features.
Checking for error conditions:
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help