Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PROGRAM 3 Vehicle Rental Agency STAGE 1 Help For this stage, we are only creating and testing single vehicle objects. A vehicle stores the basic

PROGRAM 3 Vehicle Rental Agency

STAGE 1 Help

For this stage, we are only creating and testing single vehicle objects.

A vehicle stores the basic vehicle information (descript, mpg, num_value, and VIN). The num_value means something different for each vehicle type. There are also two other variables in a vehicle object, Reservation type and Cost type.The Reservation type object assigned will be either PrivateCustReservation or CompanyReservation type. The difference is that private customer reservations provide a credit card, and company reservations provide a company acct number. (How each of these numbers are checked as valid is different.)

A Cost object is assigned if and only if there is a reservation for the vehicle. The Cost object is constructed to have the appropriate rental rates for the particular type vehicle. These rates are taken from the appropriate Rates object (CarRates, SUVRates, or TruckRates). The rates information include daily, weekly and month rental of vehicle, per mile charge, and daily insurance rate. Thus, if the company raises rates AFTER a reservation has been made, the vehnicle reserved will have the rates applied (stored in its Cost object) that were the rates at the time it was reserved. Note also that while there are three Rates subtypes, the are no subtypes (subclasses) of the Cost class. Thus, all Cost objects are the same type.

Finally, the Account class simply stores a company name, 5-digit account number, and whether the daily insurance is desired for all of the companys rentals. The Accounts class maintains a collection of Account objects.

The method called to calculate the cost of a returned vehicle is in the Vehicle class (method calcCost). It is used to calculate the cost of a returned vehicle once the actual time and number of miles driven is known It uses its Reservation object to determine whether the reservation is a company reservation or of a private customer. Also uses the Reservation object to determine, if a company reservation, whether the insurance is desired. It in turn calls the calcCost method of the Cost object, since its Cost object has the rates appropriate for this rental, which calculates and returns the total cost of the rental. The amount is then reduced if this a company reservation, therefore receiving a 15% discount.

COMPLETE ALL CLASSES BELOW

// ---------- VEHICLE CLASSES

Abstract Vehicle and subclasses Car, SUV and Truck.

public abstract class Vehicle {

// basic vehicle information

private String descript; // make-model for cars and SUVs, length of vehicle for trucks

private int mpg; // miles per gallon

private String num_value; // # seats for cars, # bench seats for SUVs, # room storage for trucks

private String VIN; // vehicle indentification number

// reservation and cost information. When a vehicle is reserved, both reserv and cost are

// assigned. When no reservation exists, both are equal to null.

private Reservation reserv; // stores name, rental period, insur option, credit card # (for private customers)

// stores name, rental period, insur option, acct # (for corporate customers)

// assigned PrivateCustReservation or CorporationReservation type object

private Cost cost; // Stores the rates in effect at the time reservation made, including daily_rate, // weekly_rate, monthly_rate, per_mile_charge, and daily_insur_rate. Contains a // computeCost method using this information for when a vehicle is returned

// and the actual time period used and the number of miles driven is known

// Note that these are the rates for a particular vehicle type, since attached to

// a particular vehicle..

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

reserv = null; // init as no reservation

cost = null;

}

// abstract toString method (to be implemented in subclasses)

public abstract String toString(); // returns appropriate description of vehicle based on its type

// implemented in the Car, SUV and Truck subclasses

// getters for basic vehicle information (no setters needed, this information never changes)

public String getDescript() { return descript; }

public int getMPG() { return mpg; }

public String getNumValue() { return num_value; }

public String getVIN() { return VIN; }

// getters and setters for Reservation and Cost objects

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

// boolean method for checking if reserved

public boolean isReserved() { return reserve != null; }

// Calculates cost of a returned vehicle

public double calcCost(String time_unit, int num_time_units, int num_miles_driven)

{

// Calculates cost of a returned vehicle. Thus passed the ACTUAL time period vehicle used and // ACTUAL num miles driven. Uses its Reservation object to determine whether a company reservation

// or private customer. Also uses the Reservation object to determine, if a company reservation, // whether the insurance is desired.

// It in turn calls the calcCost method of the Cost object, since its Cost object has the rates appropriate

// for this rental, which calculates and returns the total cost of the rental. The amount is then reduced

// if this a company reservation, therefore receiving a 15% discount.

// compute the total charge (before any discounts)

double total_chrg = cost.calcCost(time_unit, num_time_units, num_miles_driven, reserv.insuranceSelected());

// apply any applicable discounts (check if Reservation object of type CompanyReservation)

if(resv instanceof CompanyReservation)

total_chrg = total_chrg * (100 - Rates.company_discount);

return total_chrg;

}

}

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

Abstract Reservation and subclasses PrivateCustReservation and CompanyReservation

public abstract class Reservation

{

private String name; // name of person, or of company

private String time_unit; // day, week, month

private int num_time_units; // num of days, weeks or months

private String num_charged_to; // credit card for private cust, acct num for corporate cust

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

}

public boolean insuranceSelected() // this a kind of getter

{

return insurance_selected;

}

// getters (for each of the instance variables)

public String toString() { } // useful for easily displaying the info of a given reservation

}

public class PrivateCustReservation extends Reservation

{

// throws exception if the string credit_card is not a valid credit card number (i.e., 16 digits)

public Reservation(String name, String time_unit, int num_time_units,

String Utilities.validateCreditCard(creditcard_num), boolean insurance_selected)

{

super(name, time_unit, num_time_units, creditcard_num, insurance_selected);

}

// toString

public String toString() { }

}

public class CompanyReservation extends Reservation

{

// throws exception if the acct_num does not exist in the list of accounts

public CompanyReservation(String name, String time_unit, int num_time_units,

String Utilities.vaidateAcctNum(acct_num), boolean insurance_selected)

{

super(name, time_unit, num_time_units, acct_num, insurance_selected);

}

// toString

public String toString() { }

}

// ---------- RATES CLASSES

Note that Rate objects are used to store the current rental rates of the rental agency, for either Cars, SUVs or Trucks. They are hard-coded to construct themselves with the rates for daily, weekly, and monthly rental, as well as the per-mile charge and the daily charge for insurance.

public abstract class Rates {

private double rate_per_day;

private double rate_per_week;

private double rate_per_month;

private double per_mile_charge;

private double daily_insur_cost;

// 15% discount to companies for all vehicle types

private static final double company_discount = 0.15;

public Rates(double rate_per_day, double rate_per_week, double rate_per_month,

double per_mile_charge, double daily_insur_cost) {

// assignments to instance variables

this.rate_per_day = rate_per_day;

etc.

}

// getters for each instance variable

public double getDailyRate() {

return rate_per_day;

etc.

}

// toString method (could be useful for testing purposes)

}

public class CarRates extends Rate {

public CarRates() {

// hard coded rates for cars

final double daily_rate = 24.95;

final double weekly_rate = 149.95;

final double monthly_rate = 797.95;

final per_mile_charge = 0.15;

final daily_insur = 14.95;

super(daily_rate, weekly_rate, monthly_rate, per_mile_charge, daily_insur);

}

}

public class SUVRates extends Rates {

// similar to CarRates class

}

public class TruckRates extends Rates {

// similar to CarRates class

}

// ---------- COST CLASS

The Cost class contains the rates, for the particular type vehicle attached to (Car, SUV or Truck) when reserved, constructed with the current rates in the corresponding Rates class.

public 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 here

}

// getters for each instance variable (put here)

// toString method could be useful (put here)

// calc cost

public double calcCost(String time_unit, int num_time_units, int num_miles_driven,

boolean insur_selected) {

// get the rate for the time unit given (daily, weekly, or monthly)

double unit_rate;

switch(time_unit) // Note: since Java 7, can use Strings in a switch statement {

case daily: unit_rate = cost.getDailyRate(); break;

case weekly: unit_rate = cost.getWeeklyRate() break;

case monthly: unit_rate = cost.getMonthlyRate(): break;

}

// get the total insurance cost (if selected by customer)

double insur_chrg = 0;

if(insur_selected) {

switch(time_unit) {

case daily: insur_chrg = num_time_units * daily_insur_cost; break;

case weekly: insur_chrg = num_time_units * daily_insur_cost * 7; break;

case monthly: insur_chrg = num_time_units * daily_insur_cost * 30; break;

}

}

// determine total vehicle use charge

double vehicle_use_chrg = num_time_units * unit_rate;

// determine total mileage charge

double mileage_chrg = num_miles_driven * per_mile_charge;

// compute the total charge (before any discounts)

double total_rental_charge = vehicle_use_chrg + mileage_chrg + insur_chrg;

// apply any applicable discounts (check if Reservation object of type CompanyReservation)

if(reserv instanceof CompanyReservation)

return total_rental_chrg * (100 - Rates.company_discount);

else

return total_rental_chrg; }

}

// ---------- ACCOUNT CLASSES

Account class simply stores the company name, 5-digit account number, and whether or not all rentals for the company should include the daily insurance. The Accounts class stores the complete set of Account objects.

public class Account {

private String name; // company name

private String acct_num; // 5-digit acct num

private boolean insurance_option; // default insurance option for all rentals by this company

public Account(String name, String acct_num, boolean insur_option)

{ // assign instance variables }

public String getName() { }

public String getAcctNum() { }

public boolean insuranceDesired() { }

}

public class Accounts {

private Account[] accounts;

private static int latest_acct_num = 1; // init as first account number (i.e., 00001)

public Accounts() {

// allocate for max of 50 accounts

accounts = new Account[50];

// init as empty set of accounts

for(int i = 0, i < 50; i++)

accounts[i] = null;

}

public void add(String name, boolean insur_desired) {

// automatically generate new account number

accounts[nextFreeLoc() ] = new Account(name, genAcctNum(), insur_desired);

}

private String genAcctNum() {

String padded_zeros = 00000;

String acct_num_str;

// increment to next acct number

lastest_acct_num = lastest_acct_num + 1;

// convert int acct_num to String type

acct_num_str = Integer.toString(lastest_account_num);

// pad the acct_num with leading zeros so that of length 5

acct_num_str = padded_zeros.substring(acct_num_str.length()) + acct_num_str;

return acct_num_str;

}

private int nextFreeLoc() {

// returns index of next free location in array accounts (assumes array will never get full)

for (int i = 0; i < 50; i++)

if (accounts[i] == null)

return i;

}

}

// ---------- UTILITIES CLASS

public class Utilities {

public static String validateCreditCard(String credit_card) throws InvalidCreditCardException

// simply returns back credit card passed to it if found valid, otherwise, throws exception.

// valid if credit_card contains 16 characters, and each character is a digit.

public static String validateAcctNum(String acct_num) throws InvalidAcctNumException

// simply returns back acct num passed to it if found valid, otherwise, throws exception.

// valid if acct_num exists in the collection of accounts.

}

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.

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Solaris 11.2 System Administration (oracle Press)

Authors: Harry Foxwell

1st Edition

007184421X, 9780071844215

More Books

Students also viewed these Databases questions

Question

Define the term Working Capital Gap.

Answered: 1 week ago