in JAVA
Write a class Store which includes the attributes: store name and sales tax rate. Write another class encapsulating a Yarn Store, which inherits from Store. A Yarn Store has the following additional attributes: how many skeins of yarn are sold every year and the average price per skein.
Code the constructor, accessors, mutators, toString and equals method of the super class Store and the subclass Yarn Store; In the Yarn 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 Yarn Store objects, then calls your set methods, get methods, toString and equals methods and average taxes per year for the Yarn Store objects.
Store.java
public class Store {
private String name;
private double salesTaxRate;
public Store()
{
this.name = "";
this.salesTaxRate = 0;
}
public Store(String name, double salesTaxRate) {
this.name = name;
this.salesTaxRate = salesTaxRate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalesTaxRate() {
return salesTaxRate;
}
public void setSalesTaxRate(double salesTaxRate) {
this.salesTaxRate = salesTaxRate;
}
@Override
public boolean equals(Object other)
{
if(other instanceof Store)
{
if(this.name.equals(((Store) other).name) &&
this.salesTaxRate == ((Store) other).salesTaxRate)
return true;
}
return false;
}
@Override
public String toString()
{
return("Store Name: " + this.name + "\nSales Tax Rate: " +
this.salesTaxRate + "%");
}
}
YarnStore.java
public class YarnStore extends Store{
private int nSkeinsOfYarnSoldPerYear;
private double avgPricePerSkein;
public YarnStore()
{
super();
this.nSkeinsOfYarnSoldPerYear = 0;
this.avgPricePerSkein = 0;
}
public YarnStore(String name, double salesTaxRate, int
nSkeinsOfYarnSoldPerYear, double avgPricePerSkein)
{
super(name, salesTaxRate);
this.nSkeinsOfYarnSoldPerYear = nSkeinsOfYarnSoldPerYear;
this.avgPricePerSkein = avgPricePerSkein;
}
public int getnSkeinsOfYarnSoldPerYear() {
return nSkeinsOfYarnSoldPerYear;
}
public void setnSkeinsOfYarnSoldPerYear(int
nSkeinsOfYarnSoldPerYear) {
this.nSkeinsOfYarnSoldPerYear = nSkeinsOfYarnSoldPerYear;
}
public double getAvgPricePerSkein() {
return avgPricePerSkein;
}
public void setAvgPricePerSkein(double avgPricePerSkein) {
this.avgPricePerSkein = avgPricePerSkein;
}
@Override
public boolean equals(Object other)
{
if(super.equals(other))
{
if(other instanceof YarnStore)
{
if(this.nSkeinsOfYarnSoldPerYear == ((YarnStore)
other).nSkeinsOfYarnSoldPerYear
&& this.avgPricePerSkein == ((YarnStore)
other).avgPricePerSkein)
return true;
}
}
return false;
}
public double averageTaxPerYear()
{
return ((super.getSalesTaxRate() / 100) *
this.nSkeinsOfYarnSoldPerYear * this.avgPricePerSkein);
}
@Override
public String toString()
{
return(super.toString() + "\nAverage number of skeins of yarn sold
per year: " + this.nSkeinsOfYarnSoldPerYear
+ "\nAverage price per skein of yarn: $" + String.format("%.2f",
this.avgPricePerSkein)
+ "\nAverage tax per year: $" + String.format("%.2f",
averageTaxPerYear()));
}
}
YarnStoreTest.java (Driver class)
public class YarnStoreTest {
public static void main(String[] args)
{
Store store = new Store("XYZ Stores", 12.5);
YarnStore ys1 = new YarnStore();
ys1.setName("ABC Stores");
ys1.setSalesTaxRate(store.getSalesTaxRate());
ys1.setnSkeinsOfYarnSoldPerYear(340);
ys1.setAvgPricePerSkein(56.5);
YarnStore ys2 = new YarnStore();
ys2.setName("DEF Stores");
ys2.setSalesTaxRate(store.getSalesTaxRate());
ys2.setnSkeinsOfYarnSoldPerYear(500);
ys2.setAvgPricePerSkein(100);
System.out.println("Displaying the Store object details...\n" +
store.toString());
System.out.println("\nDisplaying the First YarnStore object
details...\n" + ys1.toString());
System.out.println("\nDisplaying the Second YarnStore object
details...\n" + ys2.toString());
System.out.println("\nYarnStore 1 equals YarnStore 2: " +
ys1.equals(ys2) + "\n");
}
}
***************************************************************** SCREENSHOT **********************************************************
Get Answers For Free
Most questions answered within 1 hours.