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