0% found this document useful (0 votes)
33 views13 pages

Phase 1 Final Code

The document describes a vehicle rental program with classes for customers, rental contracts, vehicles like cars and motorcycles. The main method runs a menu driven program where a customer can add contracts, add vehicles to contracts, remove vehicles from contracts, search vehicle information and print receipts.

Uploaded by

yarasultan201
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views13 pages

Phase 1 Final Code

The document describes a vehicle rental program with classes for customers, rental contracts, vehicles like cars and motorcycles. The main method runs a menu driven program where a customer can add contracts, add vehicles to contracts, remove vehicles from contracts, search vehicle information and print receipts.

Uploaded by

yarasultan201
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Test Class

import java.util.Scanner;

public class Test {

public static Scanner input = new Scanner(System.in);

public static void main(String[] args) {


System.out.println("Welcome to our Vehicle Renatl
services!");

String choice;

do {//first loop for the customer


System.out.println("would you like to take a look at our
menu of services?");
choice = input.next();

if (choice.equalsIgnoreCase("no")) {//ends loop if


answer is no
System.out.println("thank you, goodbye! ");
break;
} else if (!choice.equalsIgnoreCase("yes")) {//keeps
user until they answer either yes or no
System.out.println("Invalid input!");
System.out.println("Please enter yes or no. ");
continue;
}

System.out.println("Please enter your name.");//to


collect information for the customer object
String name = input.nextLine();
input.nextLine();

System.out.println("Enter the maximum number of rental


contracts you would like to have.");
int num = input.nextInt();

Customer c1 = new Customer(name, num); // customer


created

int chice2;
do {//second loop for the services the customer wants
for their contracts

System.out.println("---------------------------------------------
-----------");
System.out.println("Enter the number for your choice
of service");
System.out.println("1- Add a rental contract.");
System.out.println("2- Add a vehicle to a
contract.");
System.out.println("3- Remove a vehicle from a
contract.");
System.out.println("4- Search for a vehicle's
information.");
System.out.println("5- Print customer's
information.");
System.out.println("6- Print receipt for all
contracts.");
System.out.println("7-Exit");

System.out.println("---------------------------------------------
-----------");
chice2 = input.nextInt();

switch (chice2) {

case 1:
System.out.println("Enter the number of vehicles
you would like to add to your new contract. ");//to collect
information for the RenntalContract object.

int numOfV = input.nextInt();


RentalContract newR = new
RentalContract(c1.getRentalCount() + 1, numOfV);
c1.addRentalContract(newR); //adds the object to
the rentalHistory array of the customer.
break;
case 2:
System.out.println("Enter contract number to which
you would like to add vehicle.");// returns object RentalContracl
from rentalHistory if rental contract number matched.
int contNo = input.nextInt();
RentalContract selectedContract =
c1.getRentalContract(contNo);

if (selectedContract != null) {

System.out.println("would you like to rent a


car or a motorcycle?");
String answer = input.next();

if (answer.equalsIgnoreCase("car")) {

System.out.println("Enter car Brand.");//to


collect information for car object.
String brand = input.next();
System.out.println("Enter number of rental
days ? ");
int numOfrent = input.nextInt();
System.out.println("Enter the color of the
car. ");
String color = input.next();
System.out.println("Enter the number of
seats.");
int numOfSeats = input.nextInt();
Car car = new Car(brand, numOfrent, color,
numOfSeats);//creation of new car object.

if (selectedContract.addVehicle(car)) {
System.out.println("Car added
successfully!");
} else
System.out
.println("sorry cannot add car, you
have reached your limit in this contract.");

} else if
(answer.equalsIgnoreCase("motorcycle")) {//to collect information
for motorcycle object.
System.out.println("Enter motorcycle
Brand.");
String brand = input.next();
System.out.println("Enter the number of
rental days ? ");
int numOfrent = input.nextInt();
System.out.println("Enter the color of the
motorcycle.");
String color = input.next();
System.out.println("Enter the number of
wheels.");
int numOfWheels = input.nextInt();

Motorcycle motorcycle = new


Motorcycle(brand, numOfrent, color, numOfWheels);//creation of
new motorcycle object.

if (selectedContract.addVehicle(motorcycle))
System.out.println("Motorcycle added
successfully!");
else
System.out.println(
"sorry cannot add motorcycle, you
have reached your limit in this contract.");
}
} else
System.out.println("Contract not
found.");//when the getRentalContract method returns null.

break;

case 3:
System.out.println("Enter the contract number from
which you want to remove a vehicle:");//sees which contract in
the rentalHistory array to remove the vehicle to.
int contractNumberToRemove = input.nextInt();
RentalContract contractToRemoveFrom =
c1.getRentalContract(contractNumberToRemove);//returns object
RentalContracl from rentalHistory if rental contract number
matched
if (contractToRemoveFrom != null) {
System.out.println(
"Enter the vehicle's ID you want to
remove from the contract: \n (vehicle's ID format is: first
letter of the vehicle's brand capitalized_number of days the
vehicle is rented for.)");
String vehicleIdToRemove = input.next();
if
(contractToRemoveFrom.removeVehicle(vehicleIdToRemove)) {
System.out.println("Vehicle successfully
removed from the contract!");
} else {
System.out.println("Vehicle not found in the
contract.");
}
} else {
System.out.println("Contract not found!");
}
break;

case 4:

System.out.println("Enter the contract number to


search for a vehicle's information:");
int contractNumberToSearch = input.nextInt();
RentalContract contractToSearchIn =
c1.getRentalContract(contractNumberToSearch);//returns object
RentalContracl from rentalHistory if rental contract number
matched
if (contractToSearchIn != null) {
System.out.println(
"Enter the vehicle's ID you want to
search for: \n (vehicle's ID format is: first letter of the
vehicle's brand capitalized_number of days the vehicle is rented
for.)\"");
String vehicleIdToSearch = input.next();
if
(contractToSearchIn.searchVehicle(vehicleIdToSearch) != -1) {

contractToSearchIn.printVehicle(contractToSearchIn.searchVehicle(
vehicleIdToSearch));// the parameters of printVehicle are
searchVehicle method because it returns the index of the vehicle
we want to search for

} else
System.out.println("Vehicle not found in the
contract.");

} else {
System.out.println("Contract not found!");
}

break;

case 5:
System.out.println(c1.toString());
break;

case 6:

System.out.println("receipt for all contracts = "


+ c1.calculateAllRentalHistory());

break;

case 7:
System.out.println("Thank you for working with us,
Goodbye :D");
System.exit(0);
break;
default:
System.out.println("Invalid choice, please try
again");
}

} while (chice2 != 7);

} while (true);
input.close();
}
}

///////////////////////////////////

Customer Class

public class Customer {

private String name;


RentalContract[] rentalHistory;
private int rentalCount;

public Customer(String name, int size) {


this.name = name;
this.rentalHistory = new RentalContract[size];
this.rentalCount = 0;
}

public void addRentalContract(RentalContract contract) {

if (rentalCount < rentalHistory.length) {


rentalHistory[rentalCount] = contract; // aggregation
relation with RentalContract class
rentalCount++;
System.out.println("Contract" + rentalCount + " added
successfully!");
} else {
System.out.println("Can not add contract.");
}
}

public RentalContract getRentalContract(int cNo) { //returns


the rental contract in rentalHistory through it assigned contract
number
for (int i = 0; i < rentalCount; i++) {
if (rentalHistory[i].getContractNumber() == cNo) {
return rentalHistory[i];
}
}
return null; // Contract not found
}

public double calculateAllRentalHistory() {// calculates the


sum of all contract rent costs
double sum = 0;
for (int i = 0; i < rentalCount; i++)
sum = sum + rentalHistory[i].calculateContractRecipet();

return sum;

public String toString() {


String str = "Rental history for " + name + ":\n";
for (int i = 0; i < rentalCount; i++) {
str += rentalHistory[i].toString() + "\n";
}
return str;
}
public String getName() {
return name;
}

public int getRentalCount() {


return rentalCount;
}

///////////////////////////////////

RentalContract Class

public class RentalContract {

private int contractNumber;


Vehicle[] vehicleList; //an array that holds all vehicles
private int numOfv; // counter

public RentalContract(int contractNum, int size) {


contractNumber = contractNum;
this.vehicleList = new Vehicle[size];
numOfv = 0;

public boolean addVehicle(Vehicle v) { //adds vehicle object


to vehicleList(composition relation with Vehicle)
if (numOfv < vehicleList.length) {
if (v instanceof Car) //checks first if its class car or
motorcycle
vehicleList[numOfv++] = new Car(v.getBrand(),
v.getDays(), v.getColor(), ((Car) (v)).getNumOfSeats());
else if (v instanceof Motorcycle)
vehicleList[numOfv++] = new Motorcycle(v.getBrand(),
v.getDays(), v.getColor(),
((Motorcycle) (v)).getNumOfWheels());
return true;
}

return false;

public boolean removeVehicle(String id) {//removes vehicle


object in vehicleList through it's id
int index = searchVehicle(id);//uses search method to get
the index
if (index != -1) {

vehicleList[index] = vehicleList[numOfv - 1];//replaces


the vehicle we want to delete with the last vehicle in
vehicleList
vehicleList[numOfv - 1] = null;//makes the place of the
last vehicle in vehicleList = null
numOfv--;//reduces the counter by one for the removed
vehicle
return true;
}

return false;
}

public int searchVehicle(String id) {


for (int i = 0; i < numOfv; i++)//loops through vehicleList
and checks their IDs, if found it will return the index of that
vehicle

if (vehicleList[i].getvehicleID().equals(id) &&
vehicleList[i] != null)
return i;

return -1;
}

public void printVehicle(int index) {//only prints one vehicle


System.out.println(vehicleList[index].toString());
}

public double calculateContractRecipet() { // calculates the


sum of all vehicle rent costs
double total = 0;
for (int i = 0; i < numOfv; i++)
total = total + vehicleList[i].getrentalCost();

return total;
}

public String toString() {


String str = "RentalContract " + contractNumber + " :
vehicleList= \n";
if (numOfv == 0) {
str = "There are no vehicles signed to any contract";
} else
for (int i = 0; i < numOfv; i++) {
str += "Vehicle " + (i + 1) + " : " +
vehicleList[i].toString() + "\n";
}
return str;
}

public int getContractNumber() {


return contractNumber;
}

///////////////////////////////////

Vehicle Class

public abstract class Vehicle {

protected String vehicleID;


protected String brand;
protected String color;
protected double rentalCost;
protected int days;

public Vehicle(String brand, int days, String color) {

this.brand = brand;
vehicleID = brand.toUpperCase().charAt(0) + "_" + days; //
so the user can look for the specified vehicle
this.color = color;
rentalCost = calculateRentalCost(days); // calculated
through abstract method in car and motorcycle
this.days = days;
}

public abstract double calculateRentalCost(int days);

public String toString() {


return "vehicleID= " + vehicleID + ", Brand= " + brand +
" , Color: " + color + ", Rental Cost= " + rentalCost
+ ", Number of rent days: " + days + " days";
}

public String getvehicleID() {


return vehicleID;
}

public String getBrand() {


return brand;
}

public double getrentalCost() {


return rentalCost;
}

public int getDays() {


return days;
}

public String getColor() {


return color;
}

///////////////////////////////////

Car Class
public class Car extends Vehicle {

private int numOfSeats;

public Car(String brand, int days, String color, int


numOfSeats) {
super(brand, days, color);
this.numOfSeats = numOfSeats;

public double calculateRentalCost(int days) { // the rent for


one day for a car = 150

rentalCost = 150 * days;

return rentalCost;
}

public String toString() {


return super.toString() + ", Car color: " + color + ",
Number of Seats: " + numOfSeats + "\n";
}

public int getNumOfSeats() {


return numOfSeats;
}

///////////////////////////////////

Motorcycle Class

public class Motorcycle extends Vehicle {

private int numOfWheels;


public Motorcycle(String brand, int days, String color, int
numOfWheels) {
super(brand, days, color);
this.numOfWheels = numOfWheels;
}

public double calculateRentalCost(int days) { // the rent for


one day for a car = 150

rentalCost = 30 * days;

return rentalCost;
}

public String toString() {


return super.toString() + ", Motorcycle Color: " + color +
", Number of Wheels: " + numOfWheels + "\n";
}

public int getNumOfWheels() {


return numOfWheels;
}

You might also like