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
// Write a class Store
class Store {
// which includes the attributes: store name and sales tax rate
private String storeName;
private double saleTaxRate;
// constructor
public Store(String storeName, double saleTaxRate) {
this.storeName = storeName;
this.saleTaxRate = saleTaxRate;
}
// accessors, mutators
public String getStoreName() {
return storeName;
}
public void setStoreName(String storeName) {
this.storeName = storeName;
}
public double getSaleTaxRate() {
return saleTaxRate;
}
public void setSaleTaxRate(double saleTaxRate) {
this.saleTaxRate = saleTaxRate;
}
// equals method
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Store store = (Store) o;
if (Double.compare(store.saleTaxRate, saleTaxRate) != 0) return false;
return storeName != null ? storeName.equals(store.storeName) : store.storeName == null;
}
// toString method
@Override
public String toString() {
return "Store{" +
"storeName='" + storeName + '\'' +
", saleTaxRate=" + saleTaxRate +
'}';
}
}
// Write another class encapsulating a Book Store, which inherits from Store.
class BookStore extends Store {
// A Book Store has the following additional attributes: how many books are sold every year and the average price per book
private int numberOfBooksSoldPerYear;
private double averagePrice;
// constructor
public BookStore(String storeName, double saleTaxRate, int numberOfBooksSoldPerYear, double averagePrice) {
super(storeName, saleTaxRate);
this.numberOfBooksSoldPerYear = numberOfBooksSoldPerYear;
this.averagePrice = averagePrice;
}
// accessors, mutators
public int getNumberOfBooksSoldPerYear() {
return numberOfBooksSoldPerYear;
}
public void setNumberOfBooksSoldPerYear(int numberOfBooksSoldPerYear) {
this.numberOfBooksSoldPerYear = numberOfBooksSoldPerYear;
}
public double getAveragePrice() {
return averagePrice;
}
public void setAveragePrice(double averagePrice) {
this.averagePrice = averagePrice;
}
// equals method
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
BookStore bookStore = (BookStore) o;
if (numberOfBooksSoldPerYear != bookStore.numberOfBooksSoldPerYear) return false;
return Double.compare(bookStore.averagePrice, averagePrice) == 0;
}
// toString method
@Override
public String toString() {
return "Book" + super.toString() +
"numberOfBooksSoldPerYear=" + numberOfBooksSoldPerYear +
", averagePrice=" + averagePrice +
'}';
}
// In the Book Store class, also code a method returning the average taxes per year.
public double averageTaxesPerYear() {
return this.averagePrice * this.numberOfBooksSoldPerYear / this.getSaleTaxRate();
}
}
public class StoreTest {
public static void main(String[] args) {
// You should create a test class which creates 1 Store object
Store store = new Store("Bajaj", 2.5);
System.out.println(store);
// 2 Book Store objects, then calls your set methods, get methods, toString and equals methods
BookStore bookStore1 = new BookStore("MDG", 3.5, 100, 250);
BookStore bookStore2 = new BookStore("MSB", 3.8, 300, 500);
// testing toString method and average taxes per year
System.out.println(bookStore1);
System.out.println(bookStore1.averageTaxesPerYear());
System.out.println(bookStore2);
System.out.println(bookStore2.averageTaxesPerYear());
// test equals method
System.out.println(bookStore1.equals(bookStore2));
}
}
sample output screenshot:
Get Answers For Free
Most questions answered within 1 hours.