Professional Documents
Culture Documents
In this assignment, we'll get to showcase our skills in utilizing interfaces to manage diverse
vehicle types. This application needs to efficiently handle a variety of vehicles, ranging from
cars and motorcycles to powerful trucks.
To ensure consistency and streamline the process, we leverage the power of interfaces.
Throughout this assignment, we'll be creating these interfaces and implementing them in
dedicated classes for each vehicle type, the interface we will use include:
A core Vehicle interface defining basic information like make, model, and year.
Specialized interfaces for CarVehicle, MotorVehicle, and TruckVehicle to capture
unique attributes associated with each type.
Concrete classes like Car, Motorcycle, and Truck that implement their respective
interfaces.
An interactive main program allowing users to create different vehicle objects,
provide specific details, and finally, display comprehensive summaries of each
vehicle.
/*
* Vehicle Information System
*
* This program demonstrates the implementation of interfaces and classes
* for managing information about different types of vehicles in a car rental agency.
* It allows the user to create custom vehicle objects, provide relevant information,
* and display the details of all vehicles.
*/
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
// Constructor
public Car(String make, String model, int year, int numDoors, String fuelType) {
this.make = make;
this.model = model;
this.year = year;
this.numDoors = numDoors;
this.fuelType = fuelType;
}
// Constructor
public Motorcycle(String make, String model, int year, int numWheels, String motorcycleType) {
this.make = make;
this.model = model;
this.year = year;
this.numWheels = numWheels;
this.motorcycleType = motorcycleType;
}
// Constructor
public Truck(String make, String model, int year, double cargoCapacity, String transmissionType) {
this.make = make;
this.model = model;
this.year = year;
this.cargoCapacity = cargoCapacity;
this.transmissionType = transmissionType;
}
int choice;
do {
displayMenu(); // Display the menu
choice = getIntegerInput(); // Get user's choice
switch (choice) {
case 1:
createCustomVehicle(); // Create a custom vehicle
break;
case 2:
displayAllVehicles(); // Display all vehicles
break;
case 3:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 3);
}
vehicles.add(vehicle);
System.out.println("Vehicle created successfully.");
}
Interfaces:
1. The Vehicle interface defines the common methods for retrieving the vehicle's make,
model, and year of manufacture.
2. The CarVehicle interface defines methods for setting and retrieving the number of
doors and fuel type for cars.
3. The MotorVehicle interface defines methods for setting and retrieving the number of
wheels and motorcycle type for motorcycles.
4. The TruckVehicle interface defines methods for setting and retrieving the cargo
capacity and transmission type for trucks.
Classes:
1. The Car class implements the Vehicle and CarVehicle interfaces, providing methods
to set and retrieve the number of doors and fuel type.
2. The Motorcycle class implements the Vehicle and MotorVehicle interfaces, providing
methods to set and retrieve the number of wheels and motorcycle type.
3. The Truck class implements the Vehicle and TruckVehicle interfaces, providing
methods to set and retrieve the cargo capacity and transmission type.
Main Class:
1. The VehicleInfoSystem class is the main class that contains the main method.
2. It creates a List named vehicles to store all the vehicle objects and a Scanner object
for user input.
3. The main method creates sample vehicle objects and adds them to the vehicles list.
4. It then displays a menu with options to create a custom vehicle, display all vehicles,
or exit the program.
5. The displayMenu method displays the menu options.
6. The createCustomVehicle method prompts the user to enter the vehicle type (Car,
Motorcycle, or Truck) and the corresponding details for that type. It then creates an
object of the specified type and adds it to the vehicles list.
7. The displayAllVehicles method iterates through the vehicles list and calls the
appropriate display methods
(displayVehicleInfo, displayCarInfo, displayMotorcycleInfo, or displayTruckInfo)
based on the type of each vehicle object.
8. The displayVehicleInfo method displays the common vehicle information (make,
model, and year).
9. The displayCarInfo method displays the car-specific information (number of doors
and fuel type).
10. The displayMotorcycleInfo method displays the motorcycle-specific information
(number of wheels and motorcycle type).
11. The displayTruckInfo method displays the truck-specific information (cargo capacity
and transmission type).
12. The getIntegerInput and getDoubleInput methods are helper methods to get integer
and double inputs from the user, respectively. They handle invalid inputs and prompt
the user to enter a valid value.
The code handles invalid user inputs for integer and double values using try-catch
blocks and custom input methods (getIntegerInput and getDoubleInput).
The createCustomVehicle method now converts the vehicle type input to lowercase
before processing it, making it case-insensitive.
The displayAllVehicles method now iterates through the vehicles list instead of
creating separate vehicle objects, making it more dynamic and extensible.
Output: