Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the restaurant ordering system with java

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "how to achieve the restaurant ordering system in java", so the editor summarizes the following contents, detailed content, clear steps, and has a certain reference value. I hope you can gain something after reading this article. Let's take a look at this "how to achieve restaurant ordering system in java" article.

Implementation of restaurant ordering system based on JAVA

In this summer vacation, I will put some classic java programming exercises on the Internet. Do it and post it to your blog. It is not only the supervision of my own study, but also to facilitate everyone to learn from and communicate. Let's make progress together.

This ordering system is a bit like the math exercises for primary school students in my previous Java curriculum design. However, without file operation, it is relatively simple.

The effect I achieved is as shown in the picture.

The most important thing is the idea of object-oriented, which we should understand and use.

1. In the ordering system, in order to facilitate the restaurant to add recipes, we need to use a list collection, which can change the length.

two。 Each dish is an object, which we want to encapsulate, including three attributes: number, name of dish, and price.

3. For the specific requirements of ordering, we have to write each static method. In the main method, the process is designed according to the customer's ordering process.

I saw the effect picture. We paste the code directly and explain it in the code.

Import java.util.ArrayList;import java.util.List;import java.util.Scanner;/** * Taiyuan University of Technology-School of Software-Li * java realizes the ordering system * Welcome to communicate with us. Comments or private messages. * / public class menu {/ / declares two collections, the first to save the restaurant menu and the second to hold the menu ordered by the customer static List dishList = new ArrayList (); static List personList = new ArrayList (); int id; String name; double price; / / constructor. Each dish is an object public menu (int id, String name, double price) {this.id = id This.name = name; this.price = price;} public static void main (String [] args) {/ / declare that the restaurant only serves five dishes. O (∩ _ ∩) O ~ / / the first declaration method and add it to the hotel menu. Menu dish2 = new menu (1, "Yu-Shiang Shredded Pork", 28.0); dishList.add (dish2); menu dish3 = new menu (2,58.0); dishList.add (dish3); menu dish4 = new menu (3, "home cold dishes", 18.0); dishList.add (dish4) / / the second declaration method: both of these methods need to learn dishList.add (new menu); dishList.add (new menu); Scanner sc = new Scanner (System.in); and then the core code of the ordering system. It's a little complicated, and people are getting to understand it. * I will try my best to annotate and write it all * We need to understand the ordering requirements: 1. What dishes do you have in the restaurant? 2. The customer has finished ordering * re-display the hotel service for customers to choose again. What dishes did the customer order? 4. Customer returns 5. Checkout * is it a while (true) "endless loop" first, each time it is executed, it outputs the hotel service number method * the second customer chooses the service sc.nextInt () The numbers entered by the customer are distinguished by switch * after entering the switch, execute different case according to the customer's requirements * enter the ordering process when executing case 1 * again there is a while (true) to show the five dishes of the restaurant to the customer. Waiting for the customer to order, so sc.nextInt (); * different numbers, corresponding to different dishes, corresponding to the set. Save the dishes selected by the customer in the personList collection * Note: in the showDish method, there is an implementation code of "return to the next level by 0" * it is the if conditional statement in case 1: the customer chooses 0 to finish ordering and return to the restaurant service * the remaining case 2 3 4 should be easier to understand. If you don't understand, you can trust me privately! * another trick is that break is not used in case 3 but return is used because break only works in the Kitchen switch loop, * and after checkout, we need to end the ordering system and use return to directly use this program. * / while (true) {number (); int num = sc.nextInt (); switch (num) {case 1: while (true) {showDish (); int id = sc.nextInt () If (id = = 0) {break;} else {menu dish = dishList.get (id-1); System.out.println ("dear, you ordered:" + dish.name + "dish") PersonList.add (dish);}} case 2: showOrder (); break; case 3: paying (); return Case 4: System.out.println ("enter the number of the item you want to return"); int id = sc.nextInt (); exitDish (id); break;}} / / display the main menu method. Public static void number () {System.out.println ("Welcome to Xiao Shuai Restaurant!") ; System.out.println ("main menu-> 1"); System.out.println ("menu-> 1"); System.out.println ("order-> 2"); System.out.println ("check out-> 3"); System.out.println ("return food-> 4") System.out.println ("- Select service by number--);} / display hotel menu method public static void showDish () {System.out.println ("-Please order-"); / / iterate through the collection, displaying all previously declared objects to the console for (int I = 0; I < dishList.size () Menu dish +) {menu dish = dishList.get (I); System.out.println (dish.id + "" + dish.name + "" + dish.price + "yuan");} System.out.println ("- order according to the serial number, press 0 to return to the previous level--") } / / Show that the customer has ordered menu public static void showOrder () {System.out.println ("- you have ordered -"); / / the method commented out is not suitable to use the return operation. Because the id value in front of each dish is not the real collection order / * for (menu dish: personList) {System.out.println (dish.id + "" + dish.name + "" + dish.price + "yuan") } * / / in order to make it convenient for customers to choose the correct dish number, we output the ordered dishes for (int I = 0; I < personList.size (); iTunes +) {System.out.println ((item1) + "+ personList.get (I). Name +" + personList.get (I). Price + "yuan") }} / / Checkout traverses the price of the personList collection to accumulate public static void paying () {double total = 0f; System.out.println ("- checking out, please wait a moment! -- "); / / another way to traverse the collection. Learn for (menu dish: personList) {total + = dish.price;} System.out.println ("your total consumption: + total +" yuan ");} / / use the remove method of the collection for the return operation. The method with parameters is adopted. First tell the customer the food to be returned, and then perform the remove operation. These two codes cannot reverse public static void exitDish (int I) {System.out.println ("you have returned:" + personList.get (iMel 1). Name + "+ personList.get (iMuk 1). Price +"); personList.remove (iMul 1) }} the above is the content of this article on "how to realize the restaurant ordering system in java". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report