Write a class encapsulating a restaurantwhich inherits from
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
Solution
code:
import java.util.Scanner;
class Store
{
public final double SALES_TAX_RATE = 0.06;
private String name;
public Store( String newName )
{
setName( newName );
}
public String getName( )
{
return name;
}
public void setName( String newName )
{
name = newName;
}
public String toString( )
{
return( \"name: \" + name );
}
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
class Restaurant extends Store
{
int no_of_people;
float avg_price;
Restaurant()
{
super(\"\");
}
Restaurant(int n,float p,String name)
{
super(name);
no_of_people = n;
avg_price = p;
}
int get_no_of_people()
{
return no_of_people;
}
float get_avg_price()
{
return avg_price;
}
void set_no_of_people(int n)
{
no_of_people = n;
}
void set_avg_price(float p)
{
avg_price = p;
}
public String toString( )
{
String st = \"no_of_people: \"+ no_of_people+\"\ average price: \"+avg_price;
return st;
}
public double avg_tax_py()
{
return SALES_TAX_RATE*avg_price;
}
}
public class HelloWorld{
public static void main(String []args){
Scanner sc = new Scanner(System.in);
System.out.println(\"Enter number of person per year for restaurant\");
int no=sc.nextInt();
System.out.println(\"Enter average price per person for restaurant\");
float p=sc.nextFloat();
Restaurant r1 = new Restaurant(no,p,\"mystore\");
System.out.println(\"Average tax per year for restaurant is:\"+r1.avg_tax_py());
}
}


