Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a Java NetBeans project named RestaurantClient with the main class named

ID: 3851501 • Letter: C

Question

Create a Java NetBeans project named RestaurantClient with the main class named RestaurantClient, Add two service classes to the project, a superclass named Store and a subclass named Restaurant which will encapsulate a restaurant and inherit from class Store. Data encapsulated in class Store will be the restaurant name and a constant tax rate, Data encapsulated in class Restaurant will be the number of people served yearly and the average price per person. Code the appropriate constructor, mutator, accessor, toString and equals methods for both service classes. Code a method in the Restaurant class to return the average taxes per year. In the client application class you will instantiate Restaurant class objects and write statements (method calls) to test the methods in your student service classes. Download and follow the instructions in the attached file above to complete Programming Exercise 4. After successfully compiling and executing your exercise upload the zipped project folder to Programming Exercise 4.

Explanation / Answer

RestaurantClient.java

package restaurantclient;

public class RestaurantClient {
public static void main(String[] args) {
Restaurant r1 = new Restaurant("YYY", 812, 20.0);
Restaurant r2 = new Restaurant("ZZZ", 238, 25.50);
System.out.println(r1.toString());
System.out.println(r2.toString());
r2.setAveragePrice(r1.getAveragePrice());
r2.setPeopleServed(r1.getPeopleServed());
if(r1.equals(r2))
System.out.println("r1 and r2 are equsl");
else
System.out.println("r1 and r2 are not equsl");
r2.setName(r1.getName());
if(r1.equals(r2))
System.out.println("r1 and r2 are equsl");
else
System.out.println("r1 and r2 are not equsl");
System.out.println(r1.getAveragePrice());
}   
}

Store.java


package restaurantclient;
public class Store {
protected final double taxRate=0.1;
private String name;
public Store(String n)
{
setName(n);
}
public String getName()
{
return name;
}
public void setName(String n)
{
name=n;
}
public String toString()
{
String s="";
s=s+name+" "+taxRate;
return s;
}
public boolean equals(Store obj)
{
if (this.name == obj.name && this.taxRate == obj.taxRate)
return true;
else
return false;
}
}

Restaurant.java

package restaurantclient;

public class Restaurant extends Store{
private int numPeople;
private double avg;
public Restaurant(String n,int nu,double ae)
{
super(n); //pass parameters to base class constructor
this.numPeople = nu;
this.avg = ae;
}
public int getPeopleServed()
{
return numPeople;
}
public double getAveragePrice()
{
return avg;
}
public void setPeopleServed(int nu)
{
if(nu<0)
  
System.out.println("Number of people served cannot be less than 0");
else
this.numPeople = nu;
}
public void setAveragePrice(double ae)
{
if(ae<0)
  
System.out.println("Average price cannot be less than 0");
else
this.avg = ae;
}
public String toString()
{
  
return super.toString()+" People Served :"+numPeople+" Average price : "+avg;
}
  
public double avgTaxes()
{
return avg * numPeople * this.taxRate;
}
  
public boolean equals(Restaurant obj)
{
if (super.getName() == obj.getName() && this.taxRate == obj.taxRate && this.numPeople==obj.numPeople && this.avg==obj.avg)
return true;
else
return false;
}
}

Output:

run:
YYY 0.1
People Served :812 Average price : 20.0
ZZZ 0.1
People Served :238 Average price : 25.5
r1 and r2 are not equsl
r1 and r2 are equsl
20.0
BUILD SUCCESSFUL (total time: 0 seconds)