Write a class Store which includes the attributes: store name and sales tax rate
ID: 3754136 • Letter: W
Question
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
class Store
{
private String storeName;
private double taxRate;
public Store(String storeName,double taxRate)// constructor
{
this.storeName = storeName;
this.taxRate = taxRate;
}
public String toString()
{
return "Store Name : "+storeName + " Tax Rate : "+taxRate;
}
public boolean equals(Store s)
{
if(this.getStoreName() == s.getStoreName() && this.taxRate == s.taxRate)
return true;
else
return false;
}
//get and set methods
public String getStoreName()
{
return storeName;
}
public void setStoreName(String storeName)
{
this.storeName = storeName;
}
public double getTaxRate()
{
return taxRate;
}
public void setTaxRate(double taxRate)
{
this.taxRate = taxRate;
}
}
class BookStore extends Store
{
private int booksSold;
private double avgPrice;
public BookStore(String name,double taxRate,int booksSold,double avgPrice)
{
super(name,taxRate); // call to base class constructor
this.booksSold = booksSold;
this.avgPrice = avgPrice;
}
public String toString()
{
return "Book"+super.toString() + " Number of books sold : "+booksSold +" Average price per book : $"+avgPrice;
}
//get and set methods
public int getBooksSold()
{
return booksSold;
}
public void setBooksSold(int booksSold)
{
this.booksSold = booksSold;
}
public double getAvgPrice()
{
return avgPrice;
}
public void setAvgPrice(double avgPrice)
{
this.avgPrice = avgPrice;
}
public boolean equals(BookStore b)
{
if(this.getStoreName() == b.getStoreName() && this.getTaxRate() == b.getTaxRate() && this.booksSold == b.booksSold && this.avgPrice == b.avgPrice)
return true;
else
return false;
}
public double avgTaxPerYear()
{
return booksSold * getTaxRate();
}
}
class Test
{
public static void main (String[] args)
{
Store store = new Store("More",18.5);
System.out.println(store);
BookStore bs1 = new BookStore("A Novel Idea",4.56,2000,123.65);
System.out.println(bs1);
BookStore bs2 = new BookStore("BookBerries",3.28,3005,99.45);
System.out.println(bs2);
System.out.println("Average tax per year : $"+ bs2.avgTaxPerYear());
if(bs1.equals(bs2))
System.out.println("Both book stores are same");
else
System.out.println("Book stores are not same ");
}
}
Output:
Store Name : More Tax Rate : 18.5
BookStore Name : A Novel Idea Tax Rate : 4.56 Number of books sold : 2000 Average price per book : $123.65
BookStore Name : BookBerries Tax Rate : 3.28 Number of books sold : 3005 Average price per book : $99.45
Average tax per year : $9856.4
Book stores are not same
Do ask if any doubt. Please upvote.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.