This Java project demonstrates the concepts of inheritance and the decorator design pattern by implementing a car customization system. Users can select a car model and add various optional features, with the final cost and description dynamically calculated.
-
Car Models:
- Choose from three base car models:
- Model C: $32,000
- Model E: $45,000
- Model CS: $60,000
- Choose from three base car models:
-
Customizable Options:
- Add optional features to your car:
- V-8 Engine (+$4,400)
- V-12 Engine (+$6,400)
- Sunroof (cost varies by model: +$1,940 for Model C, +$2,240 for Model E, +$3,400 for Model CS)
- Blaupunkt New York 800 Radio (+$940)
- Spare Tire (+$440)
- Oversized Gas Tank (+$940)
- Add optional features to your car:
-
Dynamic Cost Calculation:
- The total cost updates as features are added.
-
Interactive Console Application:
- Users interact through a command-line interface to select car models and customize them.
- Inheritance:
- The base
Car
interface is implemented by different car models (Model_C
,Model_E
,Model_CS
).
- The base
- Decorator Pattern:
- Optional features like engines, sunroofs, and radios are implemented as decorators that wrap around a base car object to dynamically modify its behavior (e.g., cost and description).
- The user selects a base car model (
C
,E
, orCS
). - The user adds optional features (e.g., "V-8", "Sun Roof", "Radio").
- The program calculates and displays the updated description and total cost.
- The process repeats until the user types "done" to finalize their order or "quit" to exit.
Enter car model(C, E, CS) or quit to exit: C
Option (V-8, V-12, Sun Roof, Baupunkt New York 800 Radio (Radio), Spare Tire, Oversized Gas Tank(OGT), done): V-8
Option (V-8, V-12, Sun Roof, Baupunkt New York 800 Radio (Radio), Spare Tire, Oversized Gas Tank(OGT), done): Sun Roof
Option (V-8, V-12, Sun Roof, Baupunkt New York 800 Radio (Radio), Spare Tire, Oversized Gas Tank(OGT), done): done
New Order:
Car: Model C with V-8 Engine, Sun Roof
Cost: $38,340
End Order
The project consists of the following classes:
-
Base Interface (
Car
):- Defines methods for
getCost()
,getInfo()
, andgetType()
.
- Defines methods for
-
Car Models:
Model_C
,Model_E
, andModel_CS
: Implement the base interface with unique costs and descriptions.
-
Decorator Class:
- Abstract class
Decorator
: Wraps aCar
object to add new functionality.
- Abstract class
-
Optional Features:
V8Engine
,V12Engine
,Radio
,SpareTire
,SunRoof
, andOversizedGasTank
: Extend the decorator class to modify cost and description dynamically.
-
Application Entry Point (
CarApp
):- Handles user input via a console-based menu system.
- Java Development Kit (JDK) installed on your system.
- Clone this repository.
- Compile all
.java
files:javac *.java
- Run the application:
java CarApp