Please write in JAVA ONLY! Write a class encapsulating a restaurant,which inheri
ID: 3924626 • Letter: P
Question
Please write in JAVA ONLY!
Write a class encapsulating a restaurant,which inherits from Store.
A restaurant has the following additional attributes: how many people are served every year and the average price per person.
code the constructor, accessors, mutators, toString and equals method of the new subclass; also 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.
Code for Store below(Super class aka Parent class)
public class Store
{
public final double SALES_TAX_RATE = 0.06;
private String name;
/**
* Overloaded constructor:<BR>
* Allows client to set beginning value for name
* This constructor takes one parameter<BR>
* Calls mutator method
* @param newName the name of the store
*/
public Store( String newName )
{
setName( newName );
}
/** getName method
* @return a String, the name of the store
*/
public String getName( )
{
return name;
}
/**
* Mutator method:<BR>
* Allows client to set value of name
* @param newName the new name for the store
*/
public void setName( String newName )
{
name = newName;
}
/**
* @return a string representation of the store
*/
public String toString( )
{
return( "name: " + name );
}
/**
* equals method
* Compares two Store objects for the same field value
* @param o another Store object
* @return a boolean, true if this object
* has the same field value as the parameter s
*/
public boolean equals( Object o )
{
if ( ! ( o instanceof Store ) )
return false;
else
{
Store s = (Store) o;
return ( name.equalsIgnoreCase( s.name ) );
}//end else
}//end equals method
}//end class
Explanation / Answer
public class RestaurantClient {
public static void main(String[] args) {
Restaurant Restaurant("One", 100, 200);
double taxableAmount = one.getTaxableAmount();
double expected = (100 * 0.1 * 200);
// enable assertion first -ea
assert (expected == taxableAmount) : String.format("Expected <%f> but was <%f>", expected + 1, taxableAmount);
System.out.println(taxableAmount);
}
}
class Store {
protected final double TAX_RATE = 0.1;
private String name;
public Store(String name) {
setName(name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Restaurant extends Store {
private int numberOfCustomer;
private double averagePricePerCustomer;
public Restaurant(String name, int numberOfCustomer,
double averagePricePerCustomer) {
super(name);
setNumberOfCustomer(numberOfCustomer);
setAveragePricePerCustomer(averagePricePerCustomer);
}
public int getNumberOfCustomer() {
return numberOfCustomer;
}
public void setNumberOfCustomer(int numberOfCustomer) {
this.numberOfCustomer = numberOfCustomer;
}
public double getAveragePricePerCustomer() {
return averagePricePerCustomer;
}
public void setAveragePricePerCustomer(double averagePricePerCustomer) {
this.averagePricePerCustomer = averagePricePerCustomer;
}
public double getTaxableAmount() {
double result = 0;
result = this.getNumberOfCustomer() * this.getAveragePricePerCustomer()
* super.TAX_RATE;
return result;
}
}