You are on page 1of 9

Unit 6: Unit 6: OOPS Paradigms

Computer Science Department: University of the People


CS 1102-01 - AY2024-T3 Programming 1
Ursa Sayeed
March 12, 2024
INTRODUCTION
Vehicle Information System.

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;

// Interface for common vehicle properties


interface Vehicle {
String getMake(); // Get the make of the vehicle
String getModel(); // Get the model of the vehicle
int getYear(); // Get the year of manufacture
}

// Interface for car-specific properties


interface CarVehicle {
int getNumDoors(); // Get the number of doors
String getFuelType(); // Get the fuel type (petrol, diesel, electric)
void setNumDoors(int numDoors); // Set the number of doors
void setFuelType(String fuelType); // Set the fuel type
}

// Interface for motorcycle-specific properties


interface MotorVehicle {
int getNumWheels(); // Get the number of wheels
String getMotorcycleType(); // Get the motorcycle type (sport, cruiser, off-road)
void setNumWheels(int numWheels); // Set the number of wheels
void setMotorcycleType(String motorcycleType); // Set the motorcycle type
}

// Interface for truck-specific properties


interface TruckVehicle {
double getCargoCapacity(); // Get the cargo capacity in tons
String getTransmissionType(); // Get the transmission type (manual or automatic)
void setCargoCapacity(double cargoCapacity); // Set the cargo capacity
void setTransmissionType(String transmissionType); // Set the transmission type
}

// Car class implementing Vehicle and CarVehicle interfaces


class Car implements Vehicle, CarVehicle {
private String make;
private String model;
private int year;
private int numDoors;
private String fuelType;

// 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;
}

// Implement Vehicle interface methods


public String getMake() { return make; }
public String getModel() { return model; }
public int getYear() { return year; }

// Implement CarVehicle interface methods


public int getNumDoors() { return numDoors; }
public String getFuelType() { return fuelType; }
public void setNumDoors(int numDoors) { this.numDoors = numDoors; }
public void setFuelType(String fuelType) { this.fuelType = fuelType; }
}

// Motorcycle class implementing Vehicle and MotorVehicle interfaces


class Motorcycle implements Vehicle, MotorVehicle {
private String make;
private String model;
private int year;
private int numWheels;
private String motorcycleType;

// 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;
}

// Implement Vehicle interface methods


public String getMake() { return make; }
public String getModel() { return model; }
public int getYear() { return year; }

// Implement MotorVehicle interface methods


public int getNumWheels() { return numWheels; }
public String getMotorcycleType() { return motorcycleType; }
public void setNumWheels(int numWheels) { this.numWheels = numWheels; }
public void setMotorcycleType(String motorcycleType) { this.motorcycleType = motorcycleType; }
}

// Truck class implementing Vehicle and TruckVehicle interfaces


class Truck implements Vehicle, TruckVehicle {
private String make;
private String model;
private int year;
private double cargoCapacity;
private String transmissionType;

// 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;
}

// Implement Vehicle interface methods


public String getMake() { return make; }
public String getModel() { return model; }
public int getYear() { return year; }

// Implement TruckVehicle interface methods


public double getCargoCapacity() { return cargoCapacity; }
public String getTransmissionType() { return transmissionType; }
public void setCargoCapacity(double cargoCapacity) { this.cargoCapacity = cargoCapacity; }
public void setTransmissionType(String transmissionType) { this.transmissionType = transmissionType; }
}

// Main class to create and display vehicle objects


public class VehicleInfoSystem {
private static List<Vehicle> vehicles = new ArrayList<>(); // List to store all vehicle objects
private static Scanner scanner = new Scanner(System.in); // Scanner object for user input
public static void main(String[] args) {
// Create sample vehicle objects
Car car = new Car("Toyota", "Camry", 2020, 4, "Petrol");
Motorcycle motorcycle = new Motorcycle("Honda", "CBR600RR", 2018, 2, "Sport");
Truck truck = new Truck("Ford", "F-150", 2022, 1.5, "Automatic");
vehicles.add(car);
vehicles.add(motorcycle);
vehicles.add(truck);

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);
}

// Method to display the menu


private static void displayMenu() {
System.out.println("\nVehicle Information System");
System.out.println("1. Create Custom Vehicle");
System.out.println("2. Display All Vehicles");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
}

// Method to create a custom vehicle


private static void createCustomVehicle() {
System.out.println("\nCreate Custom Vehicle");
System.out.print("Enter vehicle type (Car/Motorcycle/Truck): ");
String type = scanner.nextLine().toLowerCase();

System.out.print("Enter make: ");


String make = scanner.nextLine();

System.out.print("Enter model: ");


String model = scanner.nextLine();

System.out.print("Enter year: ");


int year = getIntegerInput(); // Get user input for year
Vehicle vehicle;
switch (type) {
case "car":
System.out.print("Enter number of doors: ");
int numDoors = getIntegerInput(); // Get user input for number of doors

System.out.print("Enter fuel type: ");


String fuelType = scanner.nextLine();

vehicle = new Car(make, model, year, numDoors, fuelType);


break;
case "motorcycle":
System.out.print("Enter number of wheels: ");
int numWheels = getIntegerInput(); // Get user input for number of wheels

System.out.print("Enter motorcycle type: ");


String motorcycleType = scanner.nextLine();

vehicle = new Motorcycle(make, model, year, numWheels, motorcycleType);


break;
case "truck":
System.out.print("Enter cargo capacity (in tons): ");
double cargoCapacity = getDoubleInput(); // Get user input for cargo capacity

System.out.print("Enter transmission type: ");


String transmissionType = scanner.nextLine();

vehicle = new Truck(make, model, year, cargoCapacity, transmissionType);


break;
default:
System.out.println("Invalid vehicle type.");
return;
}

vehicles.add(vehicle);
System.out.println("Vehicle created successfully.");
}

// Method to display all vehicles


private static void displayAllVehicles() {
System.out.println("\nAll Vehicles:");
for (Vehicle vehicle : vehicles) {
System.out.println("\nVehicle Details:");
System.out.println("Make: " + vehicle.getMake());
System.out.println("Model: " + vehicle.getModel());
System.out.println("Year: " + vehicle.getYear());

if (vehicle instanceof Car) {


Car car = (Car) vehicle;
System.out.println("Number of Doors: " + car.getNumDoors());
System.out.println("Fuel Type: " + car.getFuelType());
} else if (vehicle instanceof Motorcycle) {
Motorcycle motorcycle = (Motorcycle) vehicle;
System.out.println("Number of Wheels: " + motorcycle.getNumWheels());
System.out.println("Motorcycle Type: " + motorcycle.getMotorcycleType());
} else if (vehicle instanceof Truck) {
Truck truck = (Truck) vehicle;
System.out.println("Cargo Capacity: " + truck.getCargoCapacity() + " tons");
System.out.println("Transmission Type: " + truck.getTransmissionType());
}
System.out.println();
}
}

// Helper method to get integer input from the user


private static int getIntegerInput() {
while (true) {
try {
return scanner.nextInt();
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input. Please enter an integer value.");
scanner.nextLine(); // Consume the invalid input
}
}
}

// Helper method to get double input from the user


private static double getDoubleInput() {
while (true) {
try {
return scanner.nextDouble();
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input. Please enter a decimal value.");
scanner.nextLine(); // Consume the invalid input
}
}
}
}

Here's a detailed explanation of each code block:

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.

Error Handling and Improvements:

 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:

You might also like