In: Computer Science
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..
Since you have not mentioned the language of your preference, I am providing the code in Java.
CODE
Store.java
public class Store {
protected String storeName;
protected double salesTaxRate;
public Store(String storeName, double salesTaxRate) {
this.storeName = storeName;
this.salesTaxRate = salesTaxRate;
}
public String getStoreName() {
return storeName;
}
public void setStoreName(String storeName) {
this.storeName = storeName;
}
public double getSalesTaxRate() {
return salesTaxRate;
}
public void setSalesTaxRate(double salesTaxRate) {
this.salesTaxRate = salesTaxRate;
}
@Override
public String toString() {
return "Store [storeName=" + storeName + ", salesTaxRate=" + salesTaxRate + "]";
}
}
BookStore.java
public class BookStore extends Store {
private int sold;
private double avgPricePerBook;
public BookStore(String storeName, double salesTaxRate, int sold, double avgPricePerBook) {
super(storeName, salesTaxRate);
this.sold = sold;
this.avgPricePerBook = avgPricePerBook;
}
public int getSold() {
return sold;
}
public void setSold(int sold) {
this.sold = sold;
}
public double getAvgPricePerBook() {
return avgPricePerBook;
}
public void setAvgPricePerBook(double avgPricePerBook) {
this.avgPricePerBook = avgPricePerBook;
}
public double getAverageTaxPerYear() {
return avgPricePerBook * sold * salesTaxRate;
}
@Override
public String toString() {
return "BookStore [sold=" + sold + ", avgPricePerBook=" + avgPricePerBook + "]";
}
}
Main.java
public class Main {
public static void main(String[] args) {
Store s1 = new Store("Store1", 0.15);
System.out.println(s1);
BookStore bs1 = new BookStore("Store1", 0.15, 15, 67.99);
System.out.println(bs1);
System.out.println("Average tax per year for Book store 1: $" + bs1.getAverageTaxPerYear());
BookStore bs2 = new BookStore("Store1", 0.15, 20, 70.99);
bs2.setStoreName("Store2");
bs2.setSalesTaxRate(19);
bs2.setSold(20);
bs2.setAvgPricePerBook(99.99);
System.out.println(bs2);
System.out.println("Average tax per year for Book store 2: $" + bs2.getAverageTaxPerYear());
}
}