Write in JAVA ONLY Write a class encapsulating a restaurantw
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:
* Allows client to set beginning value for name
* This constructor takes one parameter
* 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:
* 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
Solution
public class Store {
public final double SALES_TAX_RATE = 0.06;
private String name;
/**
* Overloaded constructor:
*
* Allows client to set beginning value for name This constructor takes one
* parameter
*
* 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:
*
* 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
/**
* @author xinthe
*
*/
public class Restaurant extends Store {
int peopleServed;
double avgPrice;
/**
* @param newName
* @param peopleServed
* @param avgPrice
*/
public Restaurant(String newName, int peopleServed, double avgPrice) {
super(newName);
this.peopleServed = peopleServed;
this.avgPrice = avgPrice;
}
/**
* @return the peopleServed
*/
public int getPeopleServed() {
return peopleServed;
}
/**
* @param peopleServed
* the peopleServed to set
*/
public void setPeopleServed(int peopleServed) {
this.peopleServed = peopleServed;
}
/**
* @return the avgPrice
*/
public double getAvgPrice() {
return avgPrice;
}
/**
* @param avgPrice
* the avgPrice to set
*/
public void setAvgPrice(double avgPrice) {
this.avgPrice = avgPrice;
}
public boolean equals(Object o) {
if (!(o instanceof Restaurant))
return false;
else if (super.equals(o)) {
Restaurant r = (Restaurant) o;
if (r.getAvgPrice() == getAvgPrice()
&& r.getPeopleServed() == getPeopleServed())
return true;
else
return false;
} else {
return false;
}
}// end equals method
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return super.toString() + \"(peopleServed: \" + peopleServed
+ \", avgPrice: \" + avgPrice + \")\";
}
/**
* @return
*/
public double salesTax() {
return getAvgPrice() * getPeopleServed() * SALES_TAX_RATE;
}
public static void main(String[] args) {
Restaurant restaurant = new Restaurant(\"RestName\", 255, 25.36);
System.out.println(restaurant);
System.out.println(\"Sales Tax per year :\" + restaurant.salesTax());
}
}
OUTPUT:
name: RestName(peopleServed: 255, avgPrice: 25.36)
Sales Tax per year :388.008



