Question
You are to develop and test the Java classes needed to maintain a collection of vehicles of various type. Vehicles should be able to be
You are to develop and test the Java classes needed to maintain a collection of vehicles of various type. Vehicles should be able to be added to the collection; reservations should be able to be made, displayed, and cancelled; and the vehicles should be able to be listed (either all vehicles, or only those that are not reserved).
The following classes have already been provided below:
- Vehicle
- Reservation
- TimeSpan
- Car, SUV, Truck
Needed Classes:
- Vehicles
- Main
Existing Code:
Vehicle.java
public abstract class Vehicle { private String description; private int mpg; private String vin; private Reservation resv; public Vehicle(String description, int mpg, String vin) { super(); this.description = description; this.mpg = mpg; this.vin = vin; this.resv = null; } public String getDescription() { return description; } public int getMpg() { return mpg; } public String getVin() { return vin; } public Reservation getReservation() { return resv; } public abstract String toString(); public void reserve(Reservation r) { this.resv = r; } public boolean isReserved() { return !(resv == null); } public void cancelReservation() throws UnreservedVehicleException { if (resv == null) throw new UnreservedVehicleException(); resv = null; } }
Reservation.java
public class Reservation { private String creditCardNum; private TimeSpan rentalPeriod; private boolean insuranceSelected; public Reservation(String creditCardNum, TimeSpan rentalPeriod, boolean insuranceSelected) { super(); this.creditCardNum = creditCardNum; this.rentalPeriod = rentalPeriod; this.insuranceSelected = insuranceSelected; } public String getCreditCardNum() { return creditCardNum; } public TimeSpan getRentalPeriod() { return rentalPeriod; } public boolean isInsuranceSelected() { return insuranceSelected; } @Override public String toString() { return "Reservation [creditCardNum=" + creditCardNum + ", rentalPeriod=" + rentalPeriod + ", insuranceSelected=" + insuranceSelected + "]"; } }
UnreservedVehicleException.java
public class UnreservedVehicleException extends Exception { public UnreservedVehicleException() { System.out.println("Venhicle is reserved"); } }
TimeSpan.java
public class TimeSpan { private char timeUnit; private int numUnits; public char getTimeUnit() { return timeUnit; } public int getNumUnits() { return numUnits; } @Override public String toString() { return numUnits+" "+timeUnit; } }
Car.java
public class Car extends Vehicle { private int noOfSeats; public Car(String description, int mpg, String vin, int noOfSeats) { super(description, mpg, vin); this.noOfSeats = noOfSeats; } public int getNoOfSeats() { return noOfSeats; } @Override public String toString() { return "Car[" + getVin() + " " + getDescription() + " " + getMpg() + " " + noOfSeats+"]"; } }
Truck.java
public class Truck extends Vehicle{ private double load; public Truck(String description, int mpg, String vin,double load) { super(description, mpg, vin); this.load=load; } public double getLoad() { return load; } @Override public String toString() { return "Car[" + getVin() + " " + getDescription() + " " + getMpg() + " " + load+"]"; } }
SUV.java
public class SUV extends Vehicle { private int noOfTyres; public SUV(String description, int mpg, String vin, int noOfTyres) { super(description, mpg, vin); this.noOfTyres = noOfTyres; } public int getNoOfTyres() { return noOfTyres; } @Override public String toString() { return "SUV[" + getVin() + " " + getDescription() + " " + getMpg() + " " + noOfTyres+" tyres"+"]"; } }
Abstract Vehicle Class instance variables private String description // stores make-model-year for cars and SUVs, stores length for trucks private int mpg // miles per gallon rating private String vin // unique vehicle identification number private Reservation resv Il reservation information (a null value means not reserved) constructor Inits with provided description (cars/SUVs: make-model-year, Trucks: length), mpg and VIN. Sets resv to null (i.e., newly created vehicles are not yet reserved). methods public int getMpg() { ... } public String getVIN() { ... } public Reservation getReservation() { ... } public abstract String toString(); || ABSTRACT METHOD - must be implemented in each subclass public boolean is Reserved() {...} public void reserve (Reservation r) { ... } public cancelReservation() { ... } - throws UnreservedVehicleException if reservation doesn't exist Car, SUV and Truck Classes Subclasses of the abstract Vehicle class. Each contains an appropriate constructor, added instance variables, and appropriate implementation of the toString method. Reservation Class instance variables private String creditCardNum; // credit card number vehicle reserved under private Time Span rentalPeriod; Il e.g., four days, two weeks, one month private boolean insurance Selected; // set to true if optional daily insurance wanted methods appropriate constructor, getter methods and to String method. Note that reservations cannot be altered once created (i.e., Reservation objects are immutable). TimeSpan Class instance variables private char timeUnit; // 'D' (day), 'W' (week), 'M' (month) private int numUnits; Il num of days, weeks or months constructor / getters Il appropriate constructor and getters Vehicles Class (collection of Vehicle objects) instance variable private Vehicle[ ] agency_vehicles; Il array of Vehicle objects (ArrayList type may NOT be used) private int current // used by iterator methods only methods (appropriate constructor) public void add(Vehicle v) Il adds a new vehicle to the collection public Vehicle getVehicle(String VIN) // throws VINNotFoundException if no vehicle found for provided VIN iterator methods public void reset() // resets to first vehicle in list public boolean hasNext() Il returns true if more vehicles in list to retrieve pubic Vehicle getNext() // returns next vehicle in list 1. Initially populate the collection of vehicles with the information on page 1. (USEFUL HINT: Use your own simple VINs for your testing, and add the actual VINs before submitting) 2. Can add new vehicles to the collection. 3. Can reserve a vehicle, display a reservation, and cancel a reservation. 4. Can display the list of all vehicles, e.g., ALL AGENCY VEHICLES Ford Fusion - 2018 (Car) MPG: 34 Seating: 4 VIN: AB4EG5689GM Dodge Caravan - 2019 (SUV) MPG: 25 Seating: 5 Storage: 4 cu. ft. VIN: QK3FT4273ME Eighteen-Foot (Truck) MPG: 10 Load Capacity: 5930 lbs. VIN: KG4DM5472RK etc. 5. Can display a list of unreserved vehicles only. AVAILABLE VEHICLES Dodge Caravan - 2019 (SUV) MPG: 25 Seating: 5 Storage: 4 cu.ft. VIN: QK3FT4273ME Eighteen-Foot (Truck) MPG: 10 Load Capacity: 5930 lbs. VIN: KG4DM5472RK etc. Main Class The main class of the program (with main method) will behave as a test driver for the classes developed. The program must provide the following menu of options EXACTLY as given: 1 - Display all vehicles 2 - Display available vehicles 3 - Reserve a vehicle 4 - Display a Reservation 5 - Cancel a Reservation 6 - Add a vehicle 7 - Quit
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started