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

JAVA LANGUAGE Write a class encapsulating a restaurant,which inherits from Store

ID: 3907420 • Letter: J

Question

JAVA LANGUAGE

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

Hello, I have answered a similar question once. So I’m referring my own solution here; made proper changes to satisfy your requirements. Drop a comment if you have any doubts. Thanks.

// Store.java (unchanged)

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

// Restaurant.java

public class Restaurant extends Store {

                // additional attributes

                private int peopleServedEveryYear;

                private double averagePricePerPerson;

                // constructor

                public Restaurant(String newName, int peopleServedEveryYear,

                                                double averagePricePerPerson) {

                                // passing name to super class

                                super(newName);

                                this.peopleServedEveryYear = peopleServedEveryYear;

                                this.averagePricePerPerson = averagePricePerPerson;

                }

                // getters and setters

                public int getPeopleServedEveryYear() {

                                return peopleServedEveryYear;

                }

                public void setPeopleServedEveryYear(int peopleServedEveryYear) {

                                this.peopleServedEveryYear = peopleServedEveryYear;

                }

                public double getAveragePricePerPerson() {

                                return averagePricePerPerson;

                }

                public void setAveragePricePerPerson(double averagePricePerPerson) {

                                this.averagePricePerPerson = averagePricePerPerson;

                }

                /**

                * calculates and returns the average amount of tax paid per year

                */

                public double averageTaxPerYear() {

                                double averagePricePerYear = averagePricePerPerson

                                                                * peopleServedEveryYear;

                                double averageTaxPerYear = averagePricePerYear * SALES_TAX_RATE;

                                return averageTaxPerYear;

                }

                @Override

                public String toString() {

                                // appending additional details and returning the super class String

                                return super.toString() + ", People served every year: "

                                                                + peopleServedEveryYear + ", Average price per person: $"

                                                                + averagePricePerPerson;

                }

                @Override

                public boolean equals(Object o) {

                                if (super.equals(o)) {

                                                if (o instanceof Restaurant) {

                                                                Restaurant r = (Restaurant) o;

                                                                if (this.peopleServedEveryYear == r.peopleServedEveryYear

                                                                                                && this.averagePricePerPerson == r.averagePricePerPerson) {

                                                                                // all fields are same

                                                                                return true;

                                                                }

                                                }

                                }

                                return false;

                }

}// end of Restaurant.java

// Test.java (tester class)

public class Test {

                public static void main(String[] args) {

                                //Testing Store and Restaurant classes thoroughly

                                Store store1 = new Store("Some store");

                                System.out.println("store1: " + store1);

                               

                                Restaurant restaurant1 = new Restaurant("Puttum Kattanum", 12880, 75);

                                System.out.println("Restaurant 1: " + restaurant1);

                                System.out.println("Average tax per year: $"

                                                                + restaurant1.averageTaxPerYear());

                               

                                Restaurant restaurant2 = new Restaurant("Puttum Kattanum", 12880, 75);

                                System.out.println("Restaurant 2: " + restaurant2);

                               

                                Restaurant restaurant3 = new Restaurant("Dad's Cafe", 33145, 62);

                                System.out.println("Restaurant 3: " + restaurant3);

                               

                                System.out.println("Store1 == Restaurant1? "

                                                                + store1.equals(restaurant1));

                                System.out.println("Restaurant1 == Restaurant2? "

                                                                + restaurant1.equals(restaurant2));

                                System.out.println("Restaurant1 == Restaurant3? "

                                                                + restaurant1.equals(restaurant3));

                }

}

/*OUTPUT*/

store1: name: Some store

Restaurant 1: name: Puttum Kattanum, People served every year: 12880, Average price per person: $75.0

Average tax per year: $57960.0

Restaurant 2: name: Puttum Kattanum, People served every year: 12880, Average price per person: $75.0

Restaurant 3: name: Dad's Cafe, People served every year: 33145, Average price per person: $62.0

Store1 == Restaurant1? false

Restaurant1 == Restaurant2? true

Restaurant1 == Restaurant3? false