In Java, Write a class Store which includes the attributes: store name and sales
ID: 3756313 • Letter: I
Question
In Java, Write a class Store which includes the attributes: store name and sales tax rate. Write another class encapsulating a Book Store, which inherits from Store. A Book Store has the following additional attributes: how many books are sold every year and the average price per book. Code the constructor, accessors, mutators, toString and equals method of the super class Store and the subclass Book Store; In the Book Store class, also code a method returning the average taxes per year. You should create a test class which creates 1 Store object and 2 Book Store objects, then calls your set methods, get methods, toString and equals methods and average taxes per year for the Book Store objects..
Explanation / Answer
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
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.