JAVA Complete each class and test driver to test all methods
PROGRAM 3 – Vehicle Rental Agency
// ---------- VEHICLE CLASSES
public abstract class Vehicle {
// basic vehicle information
private String descript;
private int mpg;
private String num_value;
private String VIN;
// Reservation and Cost
information
private Reservation reserv;
private Cost cost;
// constructor
public Vehicle(String descript,
int mpg, String num_value, String VIN) {
this.descript = descript;
this.mpg = mpg;
this.num_value = num_value;
this.VIN = VIN;
}
// abstract toString method (to
be implemented in subclasses)
public abstract String
toString();
// getters for basic vehicle
information
public String getDescript() {
return descript; }
public int getMPG() { return mpg;
}
public String getNumValue() {
return num_value; }
public String getVIN() { return
VIN; }
// boolean method for checking if
reserved
public boolean isReserved() {
return reserve != null; }
// getters and setters for
Reservation and Cost
public Reservation getReserv() {
return reserve; }
public void setReserv(Reservation
resv) { this.reserv = reserv; }
public Cost getCost() { return
cost; }
public void setCost(Cost cost) {
this.cost = cost; }
// calculate cost (for after a
vehicle returned)
public double calcCost(String
time_unit, int num_time_units, int num_miles_driven,
boolean insur_selected)
{
// calls the calcCost method of the Cost object assigned to
vehicle.
// if of type CompanyReservation, then returns amount reduced by
15%
}
}
public class Car Extends Vehicle
{
public Car(String descript, int
mpg, String num_value, String VIN) {
super(descript, mpg, num_value, VIN);
}
public String toString() {
String spacer = " ";
return "make-model: " + getDescript() + spacer + "mpg: " + getMPG()
+ spacer +
"num of seats: " + getNumValue() + spacer + "VIN: " + getVIN();
}
}
public class SUV Extends Vehicle {
etc.
}
public class Truck Extends Vehicle {
etc.
}
// ---------- RESERVATION CLASSES
public abstract class Reservation
{
private String name;
private String time_unit; // day,
week, month
private int num_time_units; //
num of days, weeks or months
private String
num_charged_to;
private boolean
insurance_selected;
public Reservation(String name,
String time_unit, int num_time_units,
String charged_to, boolean insurance_selected) {
// assignments to instance variables
}
// getters
// (getters for each of the
instance variables)
// toString (can be useful)
}
public class PrivateCustReservation extends Reservation
{
// throws exception if the number
in charged_to is not a valid credit card number (i.e., 16
digits)
public Reservation(String name,
String time_unit, int num_time_units,
String charged_to, boolean insurance_selected)
{
super(name, time_unit, num_time_units, charged_to,
insurance_selected);
}
// toString (returns charged_to
number labeled as "credit card num")
public String toString() { …
}
}
public class CompanyReservation extends Reservation
{
// throws exception if the acct
num in charged_to does not exist in the list of accounts
public Reservation(String name,
String time_unit, int num_time_units,
String charged_to, boolean insurance_selected)
{
super(name, time_unit, num_time_units, charged_to,
insurance_selected);
}
// toString (returns charged_to
number labeled as "acct num")
public String toString() { …
}
}
// ---------- COST CLASSES
Note that cost objects are used to store the current
rental costs in the main program, AND are also used to capture and
store the cost of a given rental at the time reserved (e.g., before
the current rates may go up).
public abstract class Cost
{
private double cost_per_day;
private double cost_per_week;
private double
cost_per_month;
private double
per_mile_charge;
private double
daily_insur_cost;
public Cost(double cost_per_day,
double cost_per_week, double cost_per_month,
double per_mile_charge, double daily_insur_cost)
{
// assignments to instance variables
}
// getters for each instance
variable
// toString method could be
useful
// calc cost
public double calcCost(String
time_unit, int num_time_units, int num_miles_driven,
boolean insur_selected)
{ // computes and returns the
cost of rental }
public class CarCost extends Cost
{
public CarCost(double
cost_per_day, double cost_per_week, double cost_per_month,
double per_mile_charge, double daily_insur_cost)
{ // call to constructor of Cost
class }
}
public class SUVCost extends Cost
{
public SUVCost(double
cost_per_day, double cost_per_week, double cost_per_month,
double
per_mile_charge, double daily_insur_cost)
{ // call to constructor of Cost
class }
}
public class TruckCost extends Cost
{
public TruckCost(double
cost_per_day, double cost_per_week, double cost_per_month,
double per_mile_charge, double daily_insur_cost)
{ // call to constructor of Cost
class }
}
Testing Approach
You should develop a simple main program that tests the above
classes, something like the following:
public class VehicleTestDriver
{
public static void main(String[]
args)
{
Vehicle veh;
Reservation res;
Cost cost;
// test Vehicle class constructor
veh = new Car("Some Make-Model", 32, 4, "VIN123");
System.out.println("Testing toString of Car class");
System.out.println(veh.toString());
// test Vehicle class toString
System.out.println("Testing toString of Car class");
System.out.println(veh.toString());
// test Vehicle class isReserved
System.out.println("Vehicle should be found not reserved");
if (veh.isReserved())
System.out.println("Vehicle found RESERVED");
else
System.out.println("Vehicle found NOT RESERVED");
// test Reservation class
res = new Reservation("some name", "days", 4, "charged to me");
System.out.println("Reservation: " + res);
// test setting of reservation for particular vehicle
veh.setReservation(res);
etc.
}
}