A home service company provides a number of different services, each with specif
ID: 3666016 • Letter: A
Question
A home service company provides a number of different services, each with specific costs associated. Create an inheritance hierarchy to represent various types of services. Use Service as the superclass of the hierarchy, then include classes RegularService, WeekendService and EmergencyService that derive from Service. a. Super class Service.java - It includes private instance variables representing the service invoice number, first and last name of the customer - It includes private instance variables that store the hours required to complete the service and base cost per hour - Constructors should initialize data members. i. The hours and base cost should be validated. - It provides a method calculateCost() that is supposed to return a double indicating the cost associated with the service. This method should return 0.0 here in the superclass. (It is up to the subclasses how to implement it.) - It has getters/accessors, setters/mutators, and toString method. i. The hours and base cost should contain positive values in setters. b. Subclass RegularService.java - It inherits the functionality of super Service, but also includes an additional data member that represents a flat fee that the company uses for calculating the cost of regular service. - Constructors should initialize data members. i. The flat fee should be validated. - It should override calculateCost() method so that it computes the cost by adding the flat fee to the hour-based cost. - It has toString, getters, and setters. i. The flat fee should be validated in the setter. c. Subclass WeekendService.java - It inherits the functionality of super class Service, but also includes a data member that represents a rate that the company charges for weekend service. - Constructors should initialize data members. i. The rate should be validated. - It should override method calculateCost() so that it computes the service cost by multiplying the rate with the hour-based cost. - It has toString, getters, and setters. i. The rate should be validated in the setter. d. Subclass EmergencyService.java - It inherits the functionality of super class Service and contains an additional data member representing an additional fee per hour charged for emergency service. - Constructors should initialize data members. i. The additional fee per hour should be validated. - It should override method calculateCost() so that it adds the additional fee per hour to the standard cost per hour. - It has toString, getters, and setters. i. The additional fee per hour should be validated. e. Write a client program to test these classes. (It doesn’t have to be a menu-driven program) - Create a regular service object based on the user input and output its information. - Create a weekend service object based on the user input and output its information. - Create an emergency service object based on the user input and output its information.
Explanation / Answer
Service.java
public class Service {
private String invoiceNumber;
private String firstName;
private String lastName;
private double hoursRequired;
private double baseCost;
public String getInvoiceNumber() {
return invoiceNumber;
}
public void setInvoiceNumber(String invoiceNumber) {
this.invoiceNumber = invoiceNumber;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public double getHoursRequired() {
return hoursRequired;
}
public void setHoursRequired(double hoursRequired) {
if(hoursRequired>0)
{
this.hoursRequired = hoursRequired;
}
}
public double getBaseCost() {
return baseCost;
}
public void setBaseCost(double baseCost) {
if(baseCost>0)
{
this.baseCost = baseCost;
}
}
public Service()
{
invoiceNumber="";
firstName="";
lastName="";
hoursRequired=0;
baseCost=0;
}
public double calculateCost()
{
return 0.0;
}
public String toString()
{
String output="Hello "+this.getFirstName()+" hours required to complete the service are "+this.getHoursRequired()+
" and base cost per hour for the service is $"+this.getBaseCost()+" ";
output+="Invoice number for the service is "+this.getInvoiceNumber()+" ";
return output;
}
}
RegularService.java
public class RegularService extends Service {
private double flatFee;
public double getFlatFee() {
return flatFee;
}
public void setFlatFee(double flatFee) {
if(flatFee>0)
{
this.flatFee = flatFee;
}
}
public RegularService()
{
super();
flatFee=0;
}
public double calculateCost()
{
double cost=this.getFlatFee()+(this.getHoursRequired()*this.getBaseCost());
return cost;
}
public String toString()
{
String output=super.toString();
output+="This is a regular service so you will be charged a flat fee of $"+this.getFlatFee()+" "+"and the total cost of service is $"+
this.calculateCost();
return output;
}
}
WeekendService.java
public class WeekendService extends Service {
private double weekendServiceCharge;
public double getWeekendServiceCharge() {
return weekendServiceCharge;
}
public void setWeekendServiceCharge(double weekendServiceCharge) {
if(weekendServiceCharge>0)
this.weekendServiceCharge = weekendServiceCharge;
}
public WeekendService()
{
super();
weekendServiceCharge=0;
}
public double calculateCost()
{
double cost=this.getWeekendServiceCharge()*this.getHoursRequired();
return cost;
}
public String toString()
{
String output=super.toString();
output+="This is a weekend service so you will be charged a weekend service charge of $"+this.getWeekendServiceCharge()+
" per hour instead of base cost"+" "+"and the total cost of service is $"+
this.calculateCost();
return output;
}
}
EmergencyService.java
public class EmergencyService extends Service {
private double emergencyServiceCharge;
public double getEmergencyServiceCharge() {
return emergencyServiceCharge;
}
public void setEmergencyServiceCharge(double emergencyServiceCharge) {
if(emergencyServiceCharge>0)
this.emergencyServiceCharge = emergencyServiceCharge;
}
public EmergencyService()
{
super();
emergencyServiceCharge=0;
}
public double calculateCost()
{
double cost=this.getHoursRequired()*(this.getBaseCost()+this.getEmergencyServiceCharge());
return cost;
}
public String toString()
{
String output=super.toString();
output+="This is a emergency service so you will be charged a additonal charge of $"+this.getEmergencyServiceCharge()+
" per hour apart from base cost "+"and the total cost of service is $"+
this.calculateCost();
return output;
}
}
Sample output:
Please enter your details for regular service:
Please input your First Name
Leo
Please input your Last Name
Jones
Please input hours required to complete the service
5
Hello Leo hours required to complete the service are 5.0 and base cost per hour for the service is $10.0
Invoice number for the service is 1
This is a regular service so you will be charged a flat fee of $10.0
and the total cost of service is $60.0
Please enter your details for weekend service:
Please input your First Name
Leo
Please input your Last Name
Jones
Please input hours required to complete the service
5
Hello Leo hours required to complete the service are 5.0 and base cost per hour for the service is $10.0
Invoice number for the service is 2
This is a weekend service so you will be charged a weekend service charge of $15.0 per hour instead of base cost
and the total cost of service is $75.0
Please enter your details for emergency service:
Please input your First Name
Leo
Please input your Last Name
Jones
Please input hours required to complete the service
5
Hello Leo hours required to complete the service are 5.0 and base cost per hour for the service is $10.0
Invoice number for the service is 3
This is a emergency service so you will be charged a additonal charge of $10.0 per hour apart from base cost
and the total cost of service is $100.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.