a. Write methods of calChargesMPV () and calcChargesLimo()
to calculate the rental charges for both subclasses.
The formulas are given below:
public double calChargesMPV()
{
double totcharge = 0.0;
totcharge = this.day*400.0;
return totcharge;
}
public double calChargesLimo()
{
double totcharge = 0.0;
if(this.getChauffer() == true)
totcharge = this.hour*HOURLY + 100*this.hour;
else
totcharge = this.hour*HOURLY;
return totcharge;
}
b)
import java.util.*;
public class RentalAPP
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
//declare local variables
String name, plateno, model, choice;
int day, hour;
boolean chauffer = true || false;
// i) declare an array of objects to store data on various types of Rental;
Rental rent [] = new Rental[10]
// ii)input and store data for both Limo and MPV objects
for(int i=0; i<rent.length; i++)
{
System.out.println("Enter Customer name:");
name = input.next();
System.out.println("Enter Plate Number:");
plateno = input.next();
System.out.println("Enter mpv or limo:");
choice = input.next();
if(choice.equalsIgnoreCase("mpv"))
{
System.out.println("Enter the number of days:");
day = input.nextInt();
System.out.println("Enter mpv's model:");
model = input.next();
rent[i] = new MPV(name, plateno, day, model);
}
else if(choice.equalsIgnoreCase("limo"))
{
System.out.println("Do you want to take chauffer: true or false");
chauffer = input.nextBoolean();
System.out.println("Enter the number of hour:");
hour = input.nextInt();
rent[i] = new Limo(name, plateno, chauffer, hour);
}
}
// iii)display the details of customers (both Limo and MPV)
for(int j=0; rent.length; j++)
{
if(rent[j] instanceof MPV)
{
MPV temp = (MPV) rent[j];
temp.toString();
}
else if(rent[j] instanceof Limo)
{
Limo temp = (Limo) rent[j];
temp.toString();
}
}
// iv)calculate and display the total rental charges of both services (Limo and MPV)
double totMPV=0.0, totLimo=0.0;
for(int k=0; rent.length; k++)
{
if(rent[k] instanceof MPV)
{
MPV temp += (MPV) rent[j];
totMPV = temp.calChargesMPV();
}
else if(rent[k] instanceof Limo)
{
Limo temp += (Limo) rent[j];
totLimo = temp.calcChargesLimo();
}
}
System.out.println("Total Charges for MPV :" + totMPV);
System.out.println("Total Charges for Limo :" + totLimo);
}
}
No comments:
Post a Comment