Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Order class is missing in the above code so kindly write code for order class with reference to this Question and written code and do

Order class is missing in the above code so kindly write code for order class with reference to this Question and written code and do complete missing parts.

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Code For the question is:

package diner;

import sheffield.*;

public class Diner {

// :::::::::::::Required for Task 1 public static void main(String[] args) { Diner.createPartyFor(args); Diner.takeEveryonesOrder(); Diner.listEveryonesOrder(); Diner.getAnotherRound(); Diner.listEveryonesOrder(); }

static Diner[] allTheDiners;

// Takes in names of the Diners and adds them to "allTheDiners" array static void createPartyFor(String[] peoplesNames) {

Diner[] allTheDiners = new Diner[peoplesNames.length]; int i = 0; // index counter for (String peoplesName : peoplesNames) { // iterates through each arg (each Diner) allTheDiners[i] = new Diner(peoplesName); // Adds the Diner to the array i++; // increments the index counter }

Diner.allTheDiners = allTheDiners;

}

static EasyWriter screen = new EasyWriter();

static void listAllTheDiners() { // This needs a body to do task 1. Using screen declared above screen.println("The diners are"); for (Diner diner : Diner.allTheDiners) { // iterate through the Diner objects stored in all the diners screen.println(diner.theDinersName); } }

private String theDinersName; Order[] thingsOrderedByDiner;

// constructor private Diner(String called) { this.theDinersName = called; this.thingsOrderedByDiner = new Order[MAX_NUMBER_OF_THINGS_A_DINER_CAN_ORDER]; // This needs a body to do task 1 and an addition line for task 2 }

// :::::::::::::Additionally required for Task 2 static void takeEveryonesOrder() { for (Diner diner : Diner.allTheDiners) { // iterate through the Diner objects stored in all the diners diner.askForTheirOrder(); // propagates the thingsOrderedByDiner for the diner } // This needs a body to do Task 2 and should make use of askForOrder }

static final int MAX_NUMBER_OF_THINGS_A_DINER_CAN_ORDER = 10;

private void askForTheirOrder() { // This is also required for task 2 screen.println("Time for " + theDinersName + " to order"); // Fill in the rest of the method. // You should use thingsOrderedByDiner (declared above) and // Order.askForNewThingOrdered() // Upto here int i = 0; // index counter for while loop Order order; do { order = Order.askForANewThingOrdered(); thingsOrderedByDiner[i] = order; i++; } while (order != null);

}

static void listEveryonesOrder() { // This is the second method for task 2 screen.println("The diners' order is"); for (Diner diner : Diner.allTheDiners) { // iterate through the Diner objects stored in all the diners screen.println("\t" + diner.theDinersName); for (Order order : diner.thingsOrderedByDiner) { if (order != null) { screen.println("\t \t" + order.toString()); } } // Fill in the rest of the method. // It should use printOrder() below // Upto here screen.println(); } }

private void printOrder() { // This is also part of task 2. You can use the constant spaces // declared below to get a nice tidy layout // You should use the toString method of the class order // without changing it to print out the details of the item ordered // If the diner ordered nothing print out "Nothing" }

// :::::::::::::Additionally required for Task 3 static void dealWithTheBill() { // Fill this in for task 3 using screen as above for output // and the method getShareOfBill() below which also needs to be // filled in double total_owed = 0; int rounded_total; double[] amounts_owed = new double[Diner.allTheDiners.length];

int i = 0; // index counter for amounts owed array for (Diner diner : Diner.allTheDiners) { // iterate through the Diner objects stored in all the diners // iterates through the orders adding up each order for each diner double unrounded_total = diner.getShareOfBill(); amounts_owed[i] = unrounded_total; total_owed += unrounded_total; i++; }

screen.print("The diners \t owe "); screen.println(total_owed, 2); screen.println("Everyone's debt rounded up is"); double total_owed_rounded = 0;

for (int j = 0; j

rounded_total = (int) Math.ceil(amounts_owed[j]); total_owed_rounded += rounded_total; screen.print(Diner.allTheDiners[j].theDinersName); screen.println(" " + rounded_total); }

screen.print("which is too much by "); screen.println((total_owed_rounded - total_owed), 2); }

private double getShareOfBill() { // Fill this in for task 3 too replacing the line below double diner_total = 0; // iterates through the orders adding up each order for each diner rounded up () for (Order order : this.thingsOrderedByDiner) { if (order != null) { diner_total += (double) order.getPriceInPence() / 100.0; } } return diner_total; }

// :::::::::::::Additionally required for Task 4 static void getAnotherRound() { // Fill this in for task 4

for (Diner diner : Diner.allTheDiners) {

Order final_order = Order.askForANewThingOrdered(diner.theDinersName); // check for room boolean full = true; for (int i = 0; i

if (full && final_order != null) { screen.println("sorry maximum order reached unable to take order"); }

screen.println(); }

}

}

Task 1 The package resturant contains the class Task1 with the main method public static void main (String [] args) { Diner.createParty For (args); Diner.listAllTheDiners(); } but the two methods it calls currently have empty bodies and you must change them to make it work. You will notice Task1 expects to take values from the args parameter (see Exercise1G). These values are the names of the diners. The method createPartyFor() should use them to create and populate a static ar- ray in Diner called allTheDiners by creating a new Diner object for each name supplied with the correct value for its instance variable theDinersName. The second method call in Task, listAllTheDiners(), should print out "The din- ers are on the first line followed by the names of the diners one to a line ending with a blank line. A static EasyWriter object called screen has already been declared in the class Diner which you must use to do display this information. To do this successfully you must fill in the bodies of three methods in Diner; the constructor, createPartyFor() and listAllTheDiners (). You may not change anything outside the bodies of these three methods. So a successful run of Taskl could look like this ... >java resturant/Taski Harry Hermione Ron The diners are Harry Hermione Ron If you do this correctly with a perfectly written program you will get 40% Task 2 Task2's main method public static void main (String [] args) { Diner.createParty For (args); Diner. take Everyones Order(); Diner.list EveryonesOrder (); } starts like Taskl but now you need to find what everyone is ordering and then display it. The first method call createPartyFor() is already written. You now need to fill in the bodies of two more methods in Diner and some other methods as well. The method takeEveryonesOrder() should go through all the diners in turn to ask for their order by asking them to choose an item and go on asking them to choose items until they don't want anything else. It should then move on the the next diner. Diner already has an instance variable Order [] thingsOrderedByDiner; to store the things the diner orders. An incomplete version of the class Order has been provided as part of the package. An ArrayList, rather than an array, would have been better for thingsrderedByDiner but we will be using an array for practice so you should not change this line. This variable needs a value assigned to it in the constructor for Diner and then each item ordered can be added to it up to a maximum defined by the constant static final int MAX_NUMBER_OF_THINGS_A_DINER_CAN_ORDER=10; which has also already been declared. So takeEveryonesrder() should fill in the thingsOrderedByDiner ar- ray by calling the instance method askForTheirOrder() for each diner and askFor TheirOrder() in turn should call a method called askFor ANewThingOrdered() in the class Order. askForANewThingOrdered() asks if the item ordered is a Starter, Main, Side, Pudding or Drink and how much the item costs using an EasyReader object called keyboard already declared for you. There is an enum for Starter, Main, Side, Pudding and Drink called MenuItem which has also al- ready been declared and it has a method called Called which takes a String as a parameter and returns either the appropriate MenuItem or null if it can't guess what the parameter is meant to be. It will accept any identifiable shorthand for the mem item and is not case sensitive. You have no need to change it. If the item ordered is not identifiable askForANewThingOrdered() must return null otherwise it asks the price and returns an Order object. Finally you need to write the last method in Task2 listEveryonesOrder(). This does what the name says but there is a useful toString method in Order which you can use to get a tidy output. You don't need to do anything with it except use it. So for Task 2 you need to fill in the bodies of five methods; takeEveryonesOrder() askForTheirOrder () and listEveryonesOrder() in Diner and askFor ANewThingOrdered and the constructor in Order. You will need to make a small change to the con- structor for Diner too but should change nothing else. A successful run of Task2 could look like this ... >java resturant/Task 2 Harry Ron Hermione Time for Harry to order What do you want? (starter, main, pudding, side, drink or nothing) starter What is the starter's price? 7.50 What do you want? (starter, main, pudding, side, drink or nothing) main What is the main's price? 15 What do you want? (starter, main, pudding, side, drink or nothing) nothing else Time for Ron to order What do you want? (starter, main, pudding, side, drink or nothing) side What is the side's price? 3.50 What do you want? (starter, main, pudding, side, drink or nothing) drink What is the drink's price? 5.50 What do you want? (starter, main, pud 8, side, drink or nothing) pudding What is the pudding's price? 6.75 What do you want? (starter, main, pudding, side, drink or nothing) nothing Time for Hermione to order What do you want? (starter, main, pudding, side, drink or nothing) drink What is the drink's price? 7.50 What do you want? (starter, main, pudding, side, drink or nothing) nothing The diners' order is Harry Starter 7.50 Main 15.00 Ron Side 3.50 Drink 5.50 Pudding 6.75 Hermione Drink 7.50 although the items ordered could have been abbreviated to just their initial letters except for starter and side which need the first two letters and nothing could equally well have been "That's all" because anything not identifiable as a starter, main, pudding, side or drink is treated as the end of the list. If you do this correctly with a perfectly written program you will get another 30% so 70% in total. Task 3 Task3's main method public static void main (String[] args) { Diner.createParty For (args); Diner. takeEveryonesOrder (); Diner.listEveryonesOrder(); Diner.deal With TheBill(); } is the same as Task 2 except it has one extra line, the call to the method dealWithTheBill in Diner. dealWithTheBill should work out how much the whole party owes and then list their individual contributions on the basis that they will pay whatever they owe rounded up to the nearest whole pound (see Math.ceil(). Finally it should say how much they would overpay by that method. All output should use the Easy Writer variable screen already declared. So the first line output should be "The diners owe" followed by the amount to two decimal places displayed using the methods of screen variable. On the next line it should say "Everyone's debt rounded up is" and then, on subsequent lines, the individual rounded up debts one to a line name first with no decimal places and finally the last line should be "which is too much by" followed, on the same line by the amount to two decimal places using screen again. A successful run of Task3 could look like this ... >java resturant/Task3 Harry Ron Hermione Time for Harry to order What do you want? (starter, main, pudding, side, drink or nothing) st What is the starter's price? 7.5 What do you want? (starter, main, pudding, side, drink or nothing) m What is the main's price? 15 What do you want? (starter, main, pudding, side, drink or nothing) x Time for Ron to order What do you want? (starter, main, pudding, side, drink or nothing) si What is the side's price? 3.5 What do you want? (starter, main, pudding, side, drink or nothing) d What is the drink's price? 5.5 What do you want? (starter, main, pudding, side, drink or nothing) P What is the pudding's price? 6.75 What do you want? (starter, main, pudding, side, drink or nothing) n Time for Hermione to order What do you want? (starter, main, pudding, side, drink or nothing) d What is the drink's price? 7.5 What do you want? (starter, main, pudding, side, drink or nothing) nothing thank you The diners' order is Harry Starter 7.50 Main 15.00 Ron Side 3.50 Drink 5.50 Pudding 6.75 Hermione Drink 7.50 The diners owe 45.75 Everyone's debt rounded up is Harry 23 Ron 16 Hermione 8 which is too much by 1.25 If you do this correctly with a perfectly written program you will get another 15% so 85% in total. Task 4 Task 4 considers the possibility that everyone may not order everything in one go. Its main method is public static void main (String[] args) { Diner.create Party For (args); Diner.take EveryonesOrder(); Diner.listEveryonesOrder(); Diner.getAnotherRound(); Diner.list EveryonesOrder(); } It includes a new method call get Another Round which allows the diners to order one new item each. It does this by a call to another new method in Orders also called askForANew ThingOrdered but this version takes, as a parameter, a String, the name of the diner being asked about extras. So each diner is asked "What else do you want .....?" where ... is replaced by the diner's name. And second time around there is no need to list the items available. So a successful run of Task3 could look like this ..>java resturant/Task4 Harry Ron Hermione Time for Harry to order What do you want? (starter, main, pudding, side, drink or nothing) st What is the starter's price? 8.56 What do you want? (starter, sain, pudding, side, drink or nothing) m What is the main's price? 15 What do you want? (starter, sain, pudding. side, drink or nothing) a What is the drink's price? 4 What do you want? (starter, sain, pudding, side, drink or nothing) x Time for Ron to order What do you want? (starter, main, pudding, side, drink or nothing) si What is the side's price? 4 What do you want? (starter, main, pudding, side, drink or nothing) a What is the drink's price? 5.9 What do you want? (starter, main, pudding, side, drink or nothing) x Time for Hermione to order What do you want? (starter, main, pudding, side, drink or nothing) d What is the drink's price? 9.50 What do you want? (starter, main, pudding, side, drink or nothing) nothing The diners' order is Harry Starter 8.56 Main 15.00 Drink 4.00 Ron Side 4.00 Drink 5.90 Hermione Drink 9.50 What else do you want Harry? d What is the drink's price? 12 What else do you want Ron? What is the main'a price? 13.45 What else do you want Hermione? at What is the starter's price? 8.23 The diners' order is Harry Starter 8.56 Main 15.00 Drink 4.00 Drink 12.00 Ron 4.00 5.90 13.45 Side Drink Main Hermione Drink Starter 9.50 8.23

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And Implementation

Authors: Edward Sciore

2nd Edition

3030338355, 978-3030338350

More Books

Students also viewed these Databases questions

Question

Provide examples of Dimensional Tables.

Answered: 1 week ago