A Car-Dealer Case Study


UND Motors sells and rents vehicles. It rents two types of vehicles: bus and car. This case study shows how UND Motors handles its fleet. It includes five classes: TestVehicle, Vehicle, Rental, Bus, Sale, shown on the right:

The Driver Class TestVehicle
It performs the following tasks:
  1. Write a driver class with a main method which creates an array with the data from the following table and displays the array contents:

    License # Vehicle Name Category Price Additional Data
    FG10000 Mercedes B-Class Sale $100,000.00 Depreciation is $25,000
    GH1000 Lexus LS460 Rental $125,000.00 No. of days is 5.
    Rate @ $300/day
    AY3000 Ford F-650 Bus $300,000.00 Driver Cost @ $10000/bus
    HI10000 GMC Sierra Sale $75,000.00 Depreciation is $6000

  2. Modify your code to use the setDepreciation method to set the depreciation value for the element at position 0 using the rate 20%.
  3. Modify your code to use the setDriverCost method to set the cost for driver for the element at position 2 to $15,000.
  4. Display the contents of the array. When print an instance of the class, that does not override the toString method, then the toString method of the object is used.
Override the price method in each subclass so that price is calculated correctly for each type of vehicle.

The Class Vehicle
Every vehicle has a license number, a make, a model, and a price. Write the class Vehicle to represent a motor vehicle. The code should include:
  • Declarations for all attributes that belong in this class. These will be those attributes that all vehicles have.
  • A constructor that gets (as parameters) a vehicle’s number, a make, a model, and a price (in that order).
  • An accessor method named getVehicleName that returns the vehicle’s name in the form make, model (e.g., “Suzuki Swift”).
  • A method named price that returns the price of the vehicle.
  • A toString method which returns a string in the following format:

      License #: DL9087
      Vehicle name: Suzuki Swift
      Price: $200,000.00
The Class Rental
Write the class Rental to represent a vehicle to be rented. This class is also a subclass of the Vehicle class including:
  • Declarations of all attributes that a rental vehicle has, but are not yet declared in the parent class.
  • A constructor that accepts a vehicle’s number, a make, a model, price, number of days for rental, and the rate per day.
  • The super keyword is a reference variable that is used to refer parent class objects.
  • The rental price of the vehicle is calculated as number of days times the rate.
  • A toString method that formats its returned value in the following manner:

      License #: DL9087
      Vehicle name: Suzuki Swift
      Price: $200,000.00
      Rental info:
          Number of days: 5
          Rate per day: $2,000.00
          Total: $10,000.00
The Class Bus
Write the class Bus to represent a bus on rental including:
  • Declarations of all attributes that a rental bus has, but are not yet declared in the parent class.
  • A constructor that a vehicle’s number, a make, a model, price, number of days for rental, the rate per day and driver cost.
  • A method named setDriverCost which accepts the driver’s cost and sets this value on the object.
  • A toString method that formats its returned value in the following manner:

      License #: DL9087
      Vehicle name: Suzuki Swift
      Price: $200,000.00
      Rental info:
          Number of days: 5
          Rate per day: $ 2,000.00
          Total: $10,000.00
          Driver cost: $12,000.00
          Final cost: $22,000.00
The Class Sale
Write the class Sale as a subclass of Vehicle including:
  • Declarations of all attributes that vehicle on sale has, but are not yet declared in the parent class.
  • A constructor that accepts (as parameters) a vehicle’s number, a make, a model, price, and depreciation value (in that order).
  • A method called setDepreciation that accepts the percentage rate, calculates the depreciation value (which is rate*price of vehicle), and sets this attribute on the object.
  • A toString method that formats its returned value in the following manner:

      License #: DL9087
      Vehicle name: Suzuki Swift
      Price: $200,000.00
          Depreciation: $50,000.00
          Selling price: $150,000.00
shell> java TestVehicle        

     
TestVehicle.java (a driver class)
public class TestVehicle {
  public static void main( String[ ] args ) {
    Vehicle[ ] motors = new Vehicle[4];
    Sale veh1   = new Sale  ( "FG1000", args[0], "B-Class",  100000.00, 25000 );
    Rental veh2 = new Rental( "GH7000", args[1], "LS460", 125000.00, 5, 300 );
    Bus veh3    = new Bus   ( "AY3000", args[2], "F-650", 300000.00, 5, 300, 10000 );
    Sale veh4   = new Sale  ( "HI2000", args[3], "Sierra",   75000.00, 6000.00 );
    Vehicle me  = new Bus   ( "BW1099", args[1], "LS460",   120000,    10, 400, 1000 );
    motors[0]   = veh1;
    motors[1]   = veh2;
    motors[2]   = veh3;
    motors[3]   = veh4;
    ((Sale) motors[0]).setDepreciation( 20.0 );
    ((Bus)  motors[2]).setDriverCost( 15000.0 );
    for ( Vehicle veh : motors )  System.out.println( "\n" + veh );
  }
}
Vehicle.java (a superclass)
public class Vehicle {
  protected String license;
  protected String make;
  protected String model;
  protected double price;

  public Vehicle( String l_n, String v_make, String v_model, double v_price ) {
    license = l_n;
    make    = v_make;
    model   = v_model;
    price   = v_price;
  }
  public String getVehicleName( ) {
    return( make + " " + model );
  }
  public double price( ) {
    return price;
  }
  public String toString( ) {
    String result;
    result  = "License #: " + license + "\nVehicle name: ";
    result += getVehicleName( ) + "\nPrice: " + price;
    return result;
  }
}
Rental.java (a vehicle to be rented)
public class Rental extends Vehicle {
  protected int    numberOfDays;
  protected double ratePerDay;

  public Rental( String l_n, String v_make, String v_model, double v_price,
                 int no_of_day, double r_p_d ) {
    super( l_n, v_make, v_model, v_price );
    numberOfDays = no_of_day;
    ratePerDay   = r_p_d;
  }
  public double price( ) {
    return numberOfDays * ratePerDay;
  }
  public String toString( ) {
    String result;
    result  = super.toString( );
    result += "\nRetal info: " + "\n  Number of days: " + numberOfDays;
    result += "\n  Rate per day: " + ratePerDay + "\n  Total: " + price( );
    return result;
  }
}
Bus.java (a bus on rental)
public class Bus extends Rental {
  private double driverCost;

  public Bus( String l_n, String v_make, String v_model, double v_price,
              int no_of_day, double r_p_d, double dri_cost ) {
    super( l_n, v_make, v_model, v_price, no_of_day, r_p_d );
    driverCost = dri_cost;
  }
  public void setDriverCost( double s_driver ) {
    driverCost = s_driver;
  }
  public String toString( ) {
    String result;
    double finalCost = driverCost + super.price( );
    result  = super.toString( );
    result += "\n  Driver cost: " + driverCost + "\n  Final cost: " + finalCost;
    return result;
  }
  public double price( ) {
    return super.price( ) + driverCost;
  }
}
Sale.java (a vehicle on sale)
public class Sale extends Vehicle {
  private double depreciation, sellPrice;
  private double rate;

  public Sale( String vehnum, String make, String model, double p, double depval ) {
    super( vehnum, make, model, p );
    depreciation = depval;
    sellPrice    = p;
  }
  public void setDepreciation( double rate ) {
    depreciation = ( rate/100 ) * super.price( );
  }
  public double price( ) {
    sellPrice = super.price( ) - depreciation;
    return sellPrice;
  }
  public String toString( ) {
    return( super.toString( ) + "\n  Depreciation: " + depreciation + "\n  Selling price: " + price( ) );
  }
}