Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class encapsulating a Grocery store,which inherits from Store. Store has

ID: 3724052 • Letter: W

Question

Write a class encapsulating a Grocery store,which inherits from Store. Store has the following attributes: name and sales tax rate; A grocery store has the following additional attributes: how many customers are served every week and the average sale amount per customer. code the constructor, accessors, mutators, toString and equals method of the super class Store and the subclass Grocery store; also in Grocery store - 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 Grocery store objects, your set methods and get methods, toString and equals methods.

Explanation / Answer

class Store   //base class
{
private String name;
private double salesTaxRate;
public Store(String name,double salesTaxRate)//constructor
{
  this.name = name;
  this.salesTaxRate = salesTaxRate;
}
//set and get methods
public void setName(String name)
{
  this.name= name;
}
public String getName()
{
  return name;
}
public void setSalestaxRate(double salesTaxRate)
{
  this.salesTaxRate = salesTaxRate;
}
public double getSalesTaxRate()
{
  return salesTaxRate;
}
public String toString()
{
  return " Store : Name "+name + "   Sales Tax Rate : "+salesTaxRate;
}
public boolean equals(Store st) // compare two stores
{
  if(this.name == st.name && this.salesTaxRate == st.salesTaxRate)
  return true;
  else
  return false;
  
}
}
class GroceryStore extends Store //derived class
{
private int customersServed;
private double avgSale;
public GroceryStore(String name,double salesTaxRate,int customersServed,double avgSale)
{
  super(name,salesTaxRate);// call to base class constructor
  this.customersServed = customersServed;
  this.avgSale = avgSale;
  
}
//set and get methods
public void setCustomersServed(int customersServed)
{
  this.customersServed = customersServed;
}
public int getCustomersServed()
{
  return customersServed;
}
public void setAvgSale(double avgSale)
{
  this.avgSale = avgSale;
}
public double getAvgSale()
{
  return avgSale;
}
public String toString()
{
  return " "+super.toString()+" Number of Customers Served : "+customersServed +" Average Sale per Customer : "+ avgSale;
}
public boolean equals(GroceryStore gs)//compare two grocery stores
{
  if(this.customersServed == gs.customersServed && this.avgSale == gs.avgSale)
  return true;
  else
  return false;
}
public double avgTax()
{
  return getSalesTaxRate()*12;
}

}
class TestStore
{
public static void main (String[] args)
{
Store s1 = new Store("Amway",0.14);
System.out.println(s1);

Store s2 = new Store("Reliance",0.8);
System.out.println(s2);

GroceryStore gs1 = new GroceryStore("More",0.56,300,1400);
GroceryStore gs2 = new GroceryStore("More",0.56,300,1400);

if(gs1.equals(gs2))
System.out.println(" Grocery Stores gs1 and gs2 are same ");
}
}

Output:

Store : Name AmwaySales Tax Rate : 0.14

Store : Name RelianceSales Tax Rate : 0.8

Grocery Stores gs1 and gs2 are same

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote