For java Write a class encapsulating a Restaurant,which inherits from Store. Sto
ID: 3805429 • Letter: F
Question
For java
Write a class encapsulating a Restaurant,which inherits from Store. Store has the following attributes: name and sales tax rate; A Restaurant has the following additional attributes: how many people are served every year and the average price per person. code the constructor, accessors, mutators, toString and equals method of the super class Store and the subclass Restaurant; also in Restaurant - code a method returning the average taxes per year. You also need to include a client class to test your code for both the parent class and the subclass.
You should test creating Store objects and Restaurant objects, your set methods and get methods, toString and equals methods.
Explanation / Answer
import java.util.*;
class Store
{
private String name;
private double saleTaxRate;
public Store(String name,double saleTaxRate) //constructor
{
this.name = name;
this.saleTaxRate = saleTaxRate;
}
//set and get methods
public void setName(String Name)
{
this.name = name;
}
public void setSaleTaxRate(double saleTaxRate)
{
this.saleTaxRate = saleTaxRate;
}
public String getName()
{
return name;
}
public double getSaleTaxRate()
{
return saleTaxRate;
}
public String toString()
{
return " Store name : "+name + " Sales Tax Rate : "+saleTaxRate;
}
public boolean equals(Store obj)
{
if (this.name == obj.name && this.saleTaxRate == obj.saleTaxRate)
return true;
else
return false;
}
}
class Restaurant extends Store
{
private int peopleServed;
private double averagePrice;
public Restaurant(String name,double saleTaxRate,int peopleServed,double averagePrice)
{
super(name,saleTaxRate); //pass parameters to base class constructor
this.peopleServed = peopleServed;
this.averagePrice = averagePrice;
}
//set and get methods
public void setPeopleServed(int peopleServed)
{
this.peopleServed = peopleServed;
}
public void setAveragePrice(double averagePrice)
{
this.averagePrice = averagePrice;
}
public int getPeopleServed()
{
return peopleServed;
}
public double getAveragePrice()
{
return averagePrice;
}
public String toString()
{
return super.toString()+" People Served in a year :"+peopleServed+" Average price per person : "+averagePrice;
}
public double avgTaxPerYear()
{
return averagePrice * peopleServed * getSaleTaxRate();
}
}
class Test
{
public static void main (String[] args)
{
Store store1 = new Store("Meers'Store ",0.45);
Store store2 = new Store("Meers'Store ",0.55);
System.out.println(store1.toString());
System.out.println("Store 1 is same as store 2 :"+store1.equals(store2));
Restaurant restaurant1 = new Restaurant("Kings Store",0.08,4000,178.00);
System.out.println(restaurant1.toString());
System.out.println(" Average taxes per year : "+restaurant1.avgTaxPerYear());
}
}
Output:
Store name : Meer 's Store Sales Tax Rate : 0.45
Store 1 is same as store 2 : false
Store name : Kings Store Sales Tax Rate : 0.08
People Served in a year:4000 Average price per year : 178.0
Average taxes per year : 56960.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.