Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Programming Assignment Unit 6

This Java program models a car rental agency system using interfaces and classes to represent different vehicle types, including cars, motorcycles, and trucks. Each vehicle type implements specific interfaces that define unique attributes and methods, ensuring a consistent structure across the system. The main program allows users to select a vehicle type, input relevant details, and displays the created vehicle's information based on user input.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Programming Assignment Unit 6

This Java program models a car rental agency system using interfaces and classes to represent different vehicle types, including cars, motorcycles, and trucks. Each vehicle type implements specific interfaces that define unique attributes and methods, ensuring a consistent structure across the system. The main program allows users to select a vehicle type, input relevant details, and displays the created vehicle's information based on user input.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Input:

import java.util.Scanner;

interface Vehicle {

String getMake();

String getModel();

int getYearOfManufacture();

interface CarVehicle extends Vehicle {

void setNumberOfDoors(int numberOfDoors);

int getNumberOfDoors();

void setFuelType(String fuelType);

String getFuelType();

interface MotorVehicle extends Vehicle {


void setNumberOfWheels(int numberOfWheels);

int getNumberOfWheels();

void setMotorcycleType(String motorcycleType);

String getMotorcycleType();

interface TruckVehicle extends Vehicle {

void setCargoCapacity(double cargoCapacity);

double getCargoCapacity();

void setTransmissionType(String transmissionType);

String getTransmissionType();

class Car implements CarVehicle {

private String make;

private String model;

private int yearOfManufacture;


private int numberOfDoors;

private String fuelType;

public Car(String make, String model, int yearOfManufacture) {

this.make = make;

this.model = model;

this.yearOfManufacture = yearOfManufacture;

@Override

public String getMake() {

return make;

@Override

public String getModel() {

return model;
}

@Override

public int getYearOfManufacture() {

return yearOfManufacture;

@Override

public void setNumberOfDoors(int numberOfDoors) {

this.numberOfDoors = numberOfDoors;

@Override

public int getNumberOfDoors() {

return numberOfDoors;

}
@Override

public void setFuelType(String fuelType) {

this.fuelType = fuelType;

@Override

public String getFuelType() {

return fuelType;

@Override

public String toString() {

return "Car{" +

"make='" + make + '\'' +

", model='" + model + '\'' +

", yearOfManufacture=" + yearOfManufacture +

", numberOfDoors=" + numberOfDoors +


", fuelType='" + fuelType + '\'' +

'}';

class Motorcycle implements MotorVehicle {

private String make;

private String model;

private int yearOfManufacture;

private int numberOfWheels;

private String motorcycleType;

public Motorcycle(String make, String model, int yearOfManufacture) {

this.make = make;

this.model = model;

this.yearOfManufacture = yearOfManufacture;

}
@Override

public String getMake() {

return make;

@Override

public String getModel() {

return model;

@Override

public int getYearOfManufacture() {

return yearOfManufacture;

@Override
public void setNumberOfWheels(int numberOfWheels) {

this.numberOfWheels = numberOfWheels;

@Override

public int getNumberOfWheels() {

return numberOfWheels;

@Override

public void setMotorcycleType(String motorcycleType) {

this.motorcycleType = motorcycleType;

@Override

public String getMotorcycleType() {

return motorcycleType;
}

@Override

public String toString() {

return "Motorcycle{" +

"make='" + make + '\'' +

", model='" + model + '\'' +

", yearOfManufacture=" + yearOfManufacture +

", numberOfWheels=" + numberOfWheels +

", motorcycleType='" + motorcycleType + '\'' +

'}';

class Truck implements TruckVehicle {

private String make;

private String model;


private int yearOfManufacture;

private double cargoCapacity;

private String transmissionType;

public Truck(String make, String model, int yearOfManufacture) {

this.make = make;

this.model = model;

this.yearOfManufacture = yearOfManufacture;

@Override

public String getMake() {

return make;

@Override

public String getModel() {


return model;

@Override

public int getYearOfManufacture() {

return yearOfManufacture;

@Override

public void setCargoCapacity(double cargoCapacity) {

this.cargoCapacity = cargoCapacity;

@Override

public double getCargoCapacity() {

return cargoCapacity;

}
@Override

public void setTransmissionType(String transmissionType) {

this.transmissionType = transmissionType;

@Override

public String getTransmissionType() {

return transmissionType;

@Override

public String toString() {

return "Truck{" +

"make='" + make + '\'' +

", model='" + model + '\'' +

", yearOfManufacture=" + yearOfManufacture +


", cargoCapacity=" + cargoCapacity +

", transmissionType='" + transmissionType + '\'' +

'}';

public class CarRental {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Welcome to the Car Rental Agency!");

while (true) {

System.out.println("\nChoose a vehicle type:");

System.out.println("1. Car");

System.out.println("2. Motorcycle");

System.out.println("3. Truck");
System.out.println("4. Exit");

int choice = scanner.nextInt();

scanner.nextLine(); // Consume the newline character

if (choice == 1) {

System.out.println("Enter the make of the car:");

String make = scanner.nextLine();

System.out.println("Enter the model of the car:");

String model = scanner.nextLine();

System.out.println("Enter the year of manufacture of the car:");

int yearOfManufacture = scanner.nextInt();

scanner.nextLine(); // Consume the newline character

Car car = new Car(make, model, yearOfManufacture);

System.out.println("Enter the number of doors:");

car.setNumberOfDoors(scanner.nextInt());
scanner.nextLine(); // Consume the newline character

System.out.println("Enter the fuel type (petrol, diesel, electric):");

car.setFuelType(scanner.nextLine());

System.out.println("Car details:");

System.out.println(car);

} else if (choice == 2) {

System.out.println("Enter the make of the motorcycle:");

String make = scanner.nextLine();

System.out.println("Enter the model of the motorcycle:");

String model = scanner.nextLine();

System.out.println("Enter the year of manufacture of the motorcycle:");

int yearOfManufacture = scanner.nextInt();

scanner.nextLine(); // Consume the newline character

Motorcycle motorcycle = new Motorcycle(make, model, yearOfManufacture);


System.out.println("Enter the number of wheels:");

motorcycle.setNumberOfWheels(scanner.nextInt());

scanner.nextLine(); // Consume the newline character

System.out.println("Enter the type of motorcycle (sport, cruiser, off-road):");

motorcycle.setMotorcycleType(scanner.nextLine());

System.out.println("Motorcycle details:");

System.out.println(motorcycle);

} else if (choice == 3) {

System.out.println("Enter the make of the truck:");

String make = scanner.nextLine();

System.out.println("Enter the model of the truck:");

String model = scanner.nextLine();

System.out.println("Enter the year of manufacture of the truck:");

int yearOfManufacture = scanner.nextInt();

scanner.nextLine(); // Consume the newline character


Truck truck = new Truck(make, model, yearOfManufacture);

System.out.println("Enter the cargo capacity (in tons):");

truck.setCargoCapacity(scanner.nextDouble());

scanner.nextLine(); // Consume the newline character

System.out.println("Enter the transmission type (manual, automatic):");

truck.setTransmissionType(scanner.nextLine());

System.out.println("Truck details:");

System.out.println(truck);

} else if (choice == 4) {

System.out.println("Exiting the application. Goodbye!");

break;

} else {

System.out.println("Invalid choice. Please enter a number between 1 and 4.");

}
}

Output:

Choose a vehicle type:

1. Car

2. Motorcycle

3. Truck

4. Exit

Enter the make of the car:

Toyoto

Enter the model of the car:

Corolla

Enter the year of manufacture of the car:

2022

Enter the number of doors:


4

Enter the fuel type (petrol, diesel, electric):

petrol

Car details:

Car{make='Toyoto', model='Corolla', yearOfManufacture=2022, numberOfDoors=4,

fuelType='petrol'}

Choose a vehicle type:

1. Car

2. Motorcycle

3. Truck

4. Exit

Enter the make of the motorcycle:

Harley-Davidson

Enter the model of the motorcycle:

Street Bob

Enter the year of manufacture of the motorcycle:


2020

Enter the number of wheels:

Enter the type of motorcycle (sport, cruiser, off-road):

sport

Motorcycle details:

Motorcycle{make='Harley-Davidson', model='Street Bob', yearOfManufacture=2020,

numberOfWheels=2, motorcycleType='sport'}

Choose a vehicle type:

1. Car

2. Motorcycle

3. Truck

4. Exit

Enter the make of the truck:

Ford

Enter the model of the truck:


F-150

Enter the year of manufacture of the truck:

2010

Enter the cargo capacity (in tons):

1.5

Enter the transmission type (manual, automatic):

automatic

Truck details:

Truck{make='Ford', model='F-150', yearOfManufacture=2010, cargoCapacity=1.5,

transmissionType='automatic'}

Choose a vehicle type:

1. Car

2. Motorcycle

3. Truck

4. Exit

Exiting the application. Goodbye!


PS C:\Users\Eliya13h\Desktop\Vehicle informationsystem>

Explanation:

This Java program utilizes interfaces and classes to model a car rental agency system. The core

of the program is the `Vehicle` interface, which establishes a contract for all vehicle types. This

interface defines fundamental methods such as `getMake`, `getModel`, and

`getYearOfManufacture`, ensuring common functionality across different vehicles.

Specific interfaces for cars (`CarVehicle`), motorcycles (`MotorVehicle`), and trucks

(`TruckVehicle`) extend `Vehicle` and add methods relevant to their unique characteristics.

`CarVehicle` handles attributes like number of doors and fuel type, while `MotorVehicle`

manages wheels and motorcycle type, and `TruckVehicle` accommodates cargo capacity and

transmission type.

The program then implements these interfaces through classes: `Car`, `Motorcycle`, and `Truck`.

Each class stores data specific to its respective vehicle type and implements the appropriate

interface methods.

Finally, the `CarRental` class acts as the main program. It presents a menu to the user, allowing

them to select a vehicle type. The user is then prompted to provide relevant information such as

make, model, year, and any additional details related to the chosen vehicle type. Based on the

user's input, an object of the corresponding vehicle class is created. The program then displays

the details of the newly created vehicle object, showcasing the values entered by the user. This

structure ensures a flexible and well-organized system for managing different vehicle types

within the car rental agency.


Screenshots:

You might also like