CodeGym /Java Course /Java Multithreading /Big task: Restaurant menu in Java

Big task: Restaurant menu in Java

Java Multithreading
Level 9 , Lesson 15
Available

"Hi, Amigo!"

"Today you'll start working on a super state-of-the-art and useful program! It's an electronic menu. Take a look:"

Big task: Restaurant menu in Java - 1

"Far out! What's it for?"

"You ask so many questions! Look, you do it, and then we'll talk. Consult with the secret agent. He'll give you all the necessary instructions."

"Captain, sir! I can't create such beautiful artwork!"

"Remember, you need to implement the necessary business logic. The pictures will be created by a designer. Go see the secret agent. You'll figure it out along the way."

"I'll figure it out, sir!"

16
Task
Java Multithreading, level 9, lesson 15
Locked
Restaurant (part 1)
Let's write a program to automate what happens in a restaurant. We'll state the task like this: the restaurant manager wants the following: 1) each table has a tablet that can be used to place orders; 2) while an order is being prepared, the tablet shows ads.
32
Task
Java Multithreading, level 9, lesson 15
Locked
Restaurant (part 2)
1. We work with the console a lot. It's time to create a single point of interaction. Create a ConsoleHelper class with a single BufferedReader, through which we will work with the console. Remember, this class does not store any data or state, so all its methods will be static.
32
Task
Java Multithreading, level 9, lesson 15
Locked
Restaurant (part 3)
Now we can create orders. Let's make it so they are automatically sent to the cook. There are many different ways to implement this functionality. Read about the Observer pattern at http://en.wikipedia.org/wiki/Observer_pattern It's already implemented in Java, and we're going to use it.
16
Task
Java Multithreading, level 9, lesson 15
Locked
Restaurant (part 4)
What do we have? The cook has prepared the dishes. It's time to take them away, but the waiter is unaware. We need to notify the waiter that it's time to pick up the order from the kitchen. This situation is similar to the previous task, so we'll use the Observer pattern again.
16
Task
Java Multithreading, level 9, lesson 15
Locked
Restaurant (part 5)
The cook currently prepares dishes instantaneously. In reality, cooking takes some time. We need to calculate the time required to prepare the entire order, so we can then select commercials to fill the time. 1. Suppose we know the cooking time for each dish in minutes.
16
Task
Java Multithreading, level 9, lesson 15
Locked
Restaurant (part 6)
An order is created, prepared by the cook, and then carried out to the guest. We also calculate the time required to fill the order. We'll consider the first part of the task complete. Let's move on to the second: while the order is being prepared, ads should be shown on the tablet.
16
Task
Java Multithreading, level 9, lesson 15
Locked
Restaurant (part 7)
When choosing which class to start from, try to find one that is used by others, and which does not use anything. In our case, this is the repository of advertising videos (AdvertisementStorage). We decided that it would be the only one in the restaurant and made it a singleton.
16
Task
Java Multithreading, level 9, lesson 15
Locked
Restaurant (part 8)
It's time to describe the functionality of the AdvertisementManager class. But first we're going to need some methods in the Advertisement class. 1. Create a long amountPerImpression field in the Advertisement class.
16
Task
Java Multithreading, level 9, lesson 15
Locked
Restaurant (part 9)
We need an exception that will help us handle the situation where we are unable to choose commercials. 1. Create an unchecked NoVideoAvailableException in the ad package. 2. Let's take a closer look at the void processVideos() method in AdvertisementManager.
32
Task
Java Multithreading, level 9, lesson 15
Locked
Restaurant (part 10)
Recursion is used when the algorithm for solving the problem is the same as the algorithm for solving part of the problem. That's just what we have. We need to do a full search of all options and choose the best of them.
16
Task
Java Multithreading, level 9, lesson 15
Locked
Restaurant (part 11)
We've implemented the first two of the three features. You will recall that the first was automated order preparation, the second was commercial selection, and the third is statistics for the manager. And that's our next step.
9
Task
Java Multithreading, level 9, lesson 15
Locked
Restaurant (part 12)
We have several events: *The cook prepared an order; *The set of videos for an order has been chosen; *There are no videos that can be shown while an order is prepared. These are constants, so an enum is suitable for representing them.
16
Task
Java Multithreading, level 9, lesson 15
Locked
Restaurant (part 13)
Let's return to the StatisticsManager class's record method. It should record events in a repository. Let's create the repository :) The repository has a 1-to-1 relationship with the manager, i.e. there is one manager and one repository per application.
32
Task
Java Multithreading, level 9, lesson 15
Locked
Restaurant (part 14)
1. Create a void put(EventDataRow data) method in StatisticsStorage. 2. To allow the put(EventDataRow data) method to add the data object to the map, we need the event type (EventType). It would make sense for the event to store its type.
32
Task
Java Multithreading, level 9, lesson 15
Locked
Restaurant (part 15)
Let's think about what needs to be done so that the manager can view the following: 1) advertising revenues, grouped by day; 2) cook utilization (time spent working), grouped by day; 3) list of active videos and the number of remaining impressions for each.
32
Task
Java Multithreading, level 9, lesson 15
Locked
Restaurant (part 16)
Let's implement the first statistics item: advertising revenues, grouped by day. 1. In the VideosSelectedEventDataRow class, create a getter for the amount field. In the OrderReadyEventDataRow class, create a getter for the cookName field.
32
Task
Java Multithreading, level 9, lesson 15
Locked
Restaurant (part 17)
Let's implement the third and fourth statistics items: the lists of active and inactive videos It will be easier to do this with access to the video repository (AdvertisementStorage class).
32
Task
Java Multithreading, level 9, lesson 15
Locked
Restaurant (part 18)
Currently, we have one tablet and 1 cook. Let's create several tablets that will randomly generate orders, and we'll make two cooks. 1. In the Restaurant class, create a constant PRIVATE static int ORDER_CREATION_INTERVAL = 100.
32
Task
Java Multithreading, level 9, lesson 15
Locked
Restaurant (part 19)
We need to rework the main method. First, let's make the parameters of our RandomOrderGeneratorTask constructors consistent. Please make your method signature the same as mine: public RandomOrderGeneratorTask(List tablets, int interval).
16
Task
Java Multithreading, level 9, lesson 15
Locked
Restaurant (part 20)
Fix a bug: a tablet sends its orders to all known chefs. Expected behavior: a tablet sends its orders to a queue, and an available cook takes orders from the queue. There are two ways to implement this functionality: 1) Each tablet stores a reference to the queue and pushes new orders to it.
32
Task
Java Multithreading, level 9, lesson 15
Locked
Restaurant (part 21)
We continue to fix the bug. Now all the orders arrive in the queue, but they don't reach the cooks. We'll make OrderManager find any available cook and give it an order. To implement this logic, we need a deamon thread.
32
Task
Java Multithreading, level 9, lesson 15
Locked
Restaurant (part 22)
Unfortunately, the orders are still not being prepared in parallel. Here's how our thread from the previous task works. It finds a cook, finds an order, gives the order to the cook using the startCookingOrder method, waits for the order to be prepared, and only then moves on to the next order.
9
Task
Java Multithreading, level 9, lesson 15
Locked
Restaurant (part 23)
That's it! You can make it beautiful on your own. For example: 1. Threads usually use the ThreadLocalRandom class instead of Random. Refactor the code: replace Random with ThreadLocalRandom. 2. Make Waiter a task so that it works like a thread (remove Observer). Make a queue of prepared orders.
Comments (31)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Maksim Krasnov Level 47, Kyiv, Ukraine
2 March 2023
Part 10. When solving part 10, carefully check: a) correct message formatting (see part 9 paragraph 2.4) b) correct sorting of these messages when printed. (see part 9 paragraph 2.4)
Thomas Level 41, Bayreuth, Germany
14 March 2023
Part 10: It is crucial to make sure you coded point a) with absolute correct formatting. Otherwise nothing will go green except the revalidate call. In addition to Maksim's a) and b) hints I'd like to add a c) c) Create an Advertising with zero impressions left and make sure no Exception will be thrown. If you don't do that the requirement 'The set should include only videos with a positive number of remaining impressions.' will never go green. Part 14: From part 10 on I tested a lot of stuff and changed the main method to do so. If you did this as well make sure you have the requirements for the main method from part 5 intact again (no problem for at least parts 11 to 13). If not requirement 'When the food is prepared, the cook must generate an appropriate event.' will fail Part 16: hard as nails again... but they made it a tiny bit easier for you. Sorting by date does not only work with date classes (Date or LocalDate or a date string starting with yyyyMMdd) but instead you can use the format string they require for output (dd-MMM-yyyy). This will give a wrong sorting in the end but validation accepts it nonetheless (they probably just check some different days of the same month in the same year). Part 17: printActiveVideoSet looks sorted ascending and if you just do that, sort the list ascending by Advertising getName you're done. printArchivedVideoSet looks unsorted from the example (Second , Third, Fourth) but you also need to sort ascending. In addition to printActiveVideoSet you must not distinguish between upper and lower case letters (convert the name to lowercase and then sort).
Dawn( #11020889) Level 37, Toronto, Canada
17 May 2023
I was stuck at Part 14 and tried 13 times. After I saw your recommendation, I woke up. Thank you so much!
Vo Level 41, Sofia, Bulgaria
17 March 2022
Condition: 2.5.1. Throw an UnsupportedOperationException if the number of impressions is not positive. Validation: If the impressionsRemaining field is 0, the revalidate() method must throw an UnsupportedOperationException. Me: Really?!?!
Lisa Level 41
9 October 2021
Part 3... in the update method, if you save the passed Object holding the order in a order variable and cast it to the type Order, then this won't pass... like:

    public void update(Observable tablet, Object arg) {
        Order order = (Order) arg;
        ConsoleHelper.writeMessage("Start cooking - " + order);
    }
You need to print the arg directly Ahh... just seen that Seb posted this already. A pity this hasn't been fixed :(
MaGaby2280 Level 41, Guatemala City, Guatemala
12 April 2021
For the part 3 of the task, take under consideration that the Observable Interface has been deprecated since Java 9
allthemore Level 41
26 February 2021
Task#16 To format month in Date use 'MMM' not 'MMMM' (which is also valid for formatter. Both formats provide the same output view for May month because of its short name and you have to guess which format to use to pass task validation).
BlueJavaBanana Level 37
23 November 2020
I'm with everyone else here. Very ambiguous instructions on some tasks. Worse still is I find that sometimes you cannot pass a tasks because of some small error several previous tasks back. I passed everything up to task 16. it was having none of it. I used the model solution for the section I was failing on and it STILL failed with the model answer. I can only assume that some where several tasks back something wasn't quite what they expected and now the requirements is showing up. It's an alright task, but sometimes spending a whole day just trying to figue out how they want a line of text formatted is annoying and stiffles your progress in learning. It's not like these issues are easy to debug without creating huge amounts of test code to simulate different orders on different days (which I did by the way). Hell, it's not even like we've been taught how to debug! I appreciate codegym, but big tasks like this NEED attention. It is good to struggle and work hard at a solution but when the problem lies with your solution not quite working the way they want it to due to shallow explanations it becomes disheartening. I recommend people give things several good attempts and then have a look at the model answers. You won't learn anything spnding days figuring out how th validator wants stuff to work.
John Squirrels Level 41, San Francisco, Poland
24 November 2020
Thanks for you comment. We'll forward that towards our technical team.
John Squirrels Level 41, San Francisco, Poland
24 November 2020
If something does not work out correctly for you, please go to Help section and leave your question over there.
BlueJavaBanana Level 37
24 November 2020
Thanks John. The codegym set up is amazing! But there could definitely be some improvement on some tasks, I feel this is one of them. Regardless you and your team have made a great course overall. So well done for that!
Justin Johnson Level 31, Clearwater, United States
4 October 2020
This is awful...a lot of the conditions are hard to understand without more explanation. Thank you to everyone who helped with people's questions on the tasks...cant wait to finish.
brus5 Level 41, Dąbrowa Górnicza, Poland
5 September 2020
After reading of comment section I was a little bit scared of this task. I knew that, I couldn't give up on this. The help section of tasks was very helpful and I 'borrow' some ideas of others students when validator wasn't friendly to me. This task was hell to me.
brus5 Level 41, Dąbrowa Górnicza, Poland
1 September 2020
Hint for future students, don't restart that task, it will throw you at first part and you'll have to start it once again.
Henrique Level 41, São Paulo, Brazil
21 August 2020
If someone has the solution from part 16 to 23, can you message me? I gave up on this course, but I'd like to see how the complete code of this task is, for curiosity. Thanks!