Lunches are either sandwiches or hot lunches. All sandwiches come with a meat and a number of salads. All hot lunches come with either chips or side salad. The cost of the lunch is displayed to the user and any change required is calculated. It is always assumed that the customer pays the right amount or more than the right amount (cashier ensures the customer never gives too little, not the program). Sometimes, certain items are not available. The system must print a message telling the customer that the lunch they ordered is not available when they order it.
(a) Implement an abstract class Lunch:
1) Lunch has the following private members: title, cost, availability
2) Lunch must have a constructor which takes in the title as a parameter
3) Lunch must have a setter and getter for availability and cost
4) Lunch must have a printDetails() method which prints the title and cost of the lunch
5) Lunch must have a takePayment() method which takes in the amount of money tendered (given to the cashier) and returns the correct change
(b) Lunch as two subclasses, Sandwich and HotLunch:
1) Sandwich has private member variables: meat type, number of salads
2) HotLunch as a private member variable: chips (true) or side salad (false)
3) The cost of a sandwich is 3 euro for the meat + .55 cent for each salad. There is a limit of 5 salads, and there must be at least 3 salads. Should the customer order above to below those values, set the number of salads to the closest acceptable value (e.g. if they order 1 salad, give them 3, if they order 20, give them 5).
4) HotLunches are 4.50 euro with chips and 3.95 with side salad
5) Write the constructors for both of the subclasses; ensure that the correct parameter is passed up to the super class and that constraints are enforced on the number of salads. The constructor should calculate the cost of the lunch and call the setter method on the super class so that the cost is set.
6) Sandwich should have a getter method for meat.
Gav Doc wrote:...on the getters and setters I'm not sure what exactly it wants to be returned and stuff...
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber wrote:
Gav Doc wrote:...on the getters and setters I'm not sure what exactly it wants to be returned and stuff...
Welcome to the ranch!
For help on the getters and setters, see this tutorial on encapsulation.
For the other items, show us what you have so far, and where you're stuck.
public void printDetails ()
{
System.out.println(title);
System.out.println(cost);
}
public void takePayment()
{
}
Gav Doc wrote:...should the methods and getters and setters be public or private?
Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
David Newton wrote:This purpose of this code eludes me:
-Nev
"It's about choosing your battles, not fighting somebody else's war"
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Campbell Ritchie wrote: . . . and nobody told them what iffy practice writing == true and == false is?
-Nev
"It's about choosing your battles, not fighting somebody else's war"
Jesper Young wrote:It's much simpler to just write:
No need for an if-statement or a ternary expression.
-Nev
"It's about choosing your battles, not fighting somebody else's war"
Consider Paul's rocket mass heater. |