preface

After writing a Java SE version of the student score management system last time, we found that the response is good, so today I have brought almost the same order system, hoping to use the Java SE knowledge we have learned, to achieve our order system.

In the comments of the last article, I also saw some suggestions from you, and I may not reply to you in time, but in this article, I will try to meet some good suggestions from you, and move forward in a better direction!

Demand analysis

This time yes, we need to design a ordering system, we need to analyze our system, oriented to who? What are the common operations on objects?

Since it is an order system, our restaurants generally have a fixed menu, and the target is usually customers. Customers can order, delete dishes, check the dishes they have ordered, and finally pay the bill after eating through this menu.

Therefore, for a la carte system, the main required functions are as follows:

  1. Initialization menu
  2. I take your order
  3. Remove dishes that have been ordered
  4. Check your order
  5. The invoicing
  6. Log out

Preview function

The system menu

After entering our order system, there is usually a system menu to remind us of the next operation.

I take your order

After entering the ordering function according to the system menu, enter the menu number to order and return to the upper menu.

Check your order

Let’s say that after we order, we need to make sure that there are no duplicates, so we can check what we have ordered.

Of course, in the picture above, the food will appear only after we have ordered the food. If we have not ordered the food, the system will indicate that we have not ordered the food yet.

Delete items

What if we accidentally order the same thing? Don’t worry, we can directly select the function of deleting dishes, and then delete the corresponding duplicate dishes.

Similarly, if we enter the menu deletion function before we order, the system will also give a prompt.

The invoicing

When we have finished our meal, the next step is to check out. At this point, as long as we enter the checkout option, the system will print out all the money spent this time.

Similarly, if we enter the checkout function before we order, the system will also give a prompt.

Log out

When we enter an option of 0, we exit the system.

Function implementation

The main interface

The main interface of the system is to print out the menu of function selection, and then select different sub-functions according to our input.

package com.cunyu;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 *
 * @author* : the village of the rain@version : 1.0
 * @project: Java Field *@package : com.cunyu
 * @className : DishApp
 * @createTime: 2021/7/31 17:30pm *@email: [email protected] * @public account: Village Yuyao *@website : https://cunyu1943.github.io
 * @description: System main interface */
public class DishApp {

    public static void main(String[] args) {
        Dish dish = new Dish();
        // Initialize the menu
        List<Dish> dishList = dish.initMenu();

        Scanner scanner = new Scanner(System.in);

        List<Dish> orderedList = new ArrayList<>();
        while (true) {
            System.out.println("---------- Welcome to the ordering system --------");
            System.out.println("---------- [1] order --------------");
            System.out.println("---------- [2] see dishes ordered -------");
            System.out.println("---------- [3] Delete dishes -----------");
            System.out.println("---------- [4] checkout --------------");
            System.out.println("---------- [0] Return to the previous level/exit -----");

            System.out.println("Enter your choice");
            String choice = scanner.next();
            switch (choice) {
                case "1":
                    while (true) {
                        dish.showMenu(dishList);
                        System.out.println("Please enter the sequence number to order, enter 0 to return to the previous menu.");
                        int id = scanner.nextInt();
                        if (id == 0) {
                            break;
                        }

                        System.out.println("Enter dish number :" + id);

                        System.out.println("What did you order?" + dishList.get(id - 1).getName());
                        // Add an order to the menu
                        orderedList.add(dishList.get(id - 1));
                    }
                    break;
                case "2":
                    dish.showOrderedMenu(orderedList);
                    break;
                case "3":
                    if (orderedList.isEmpty()) {
                        System.out.println("Not yet ordered, please re-enter your choice.");
                    } else {
                        System.out.println("Enter the sequence number of the dish to be deleted.");
                        int id = scanner.nextInt();
                        dish.deleteDish(id, dishList, orderedList);
                    }
                    break;
                case "4":
                    dish.checkout(orderedList);
                    break;

                case "0":
                    System.exit(0);
                default:
                    break; }}}}Copy the code

The main class design

Mainly involved dishes, so define a dish category, mainly including serial number, dish name, dish unit price three attributes.

package com.cunyu;

/**
 * Created with IntelliJ IDEA.
 *
 * @author* : the village of the rain@version : 1.0
 * @project: Java Field *@package : com.cunyu
 * @className : Dish
 * @createTime : 2021/7/31 17:27
 * @email: [email protected] * @public account: Village Yuyao *@website : https://cunyu1943.github.io
 * @description: Food category */
public class Dish {
    /** ** id */
    private int id;
    /** ** Name of dish */
    private String name;
    /** ** price */
    private double price;

    public int getId(a) {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName(a) {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice(a) {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public Dish(a) {}public Dish(int id, String name, double price) {
        this.id = id;
        this.name = name;
        this.price = price; }}Copy the code

The function interface

In the last article, the reader suggested me to separate each function from the interface and then implement it, so here I give the interface of each function first.

package com.cunyu;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author* : the village of the rain@version : 1.0
 * @project: Java Field *@package : com.cunyu
 * @className : DishInterface
 * @createTime : 2021/7/31 20:26
 * @email: [email protected] * @public account: Village Yuyao *@website : https://cunyu1943.github.io
 * @description: * /
public interface DishInterface {
    /** * Initialize menu **@returnReturns a list of dishes currently served */
    public List<Dish> initMenu(a);

    /** * display menu **@paramDishList */
    public void showMenu(List<Dish> dishList);


    /** * Displays the order **@paramOrderedList Current order */
    public void showOrderedMenu(List<Dish> orderedList);

    /** * Remove menu **@paramId Indicates the dish serial number *@paramDishList *@paramOrderedList List of ordered dishes */
    public void deleteDish(int id, List<Dish> dishList, List<Dish> orderedList);

    /** * check out **@param orderedList
     */
    public void checkout(List<Dish> orderedList);
}

Copy the code

Initialization menu

General restaurants provide a fixed menu, so there is no menu management function, directly after initialization to give each dish, put it in the list.

/** * Initialize menu **@returnInitialized menu */
public List<Dish> initMenu(a) {
    List<Dish> dishList = new ArrayList<>();
    dishList.add(new Dish(1."Golden hand grasping bone".38));
    dishList.add(new Dish(2."Country Fire Meat.".58));
    dishList.add(new Dish(3."Healthy Turtle Soup.".68));
    dishList.add(new Dish(4."Soup of Three Delicacies".28));
    dishList.add(new Dish(5."Nori egg Drop soup.".18));
    dishList.add(new Dish(6."Iron plate golden egg".38));
    dishList.add(new Dish(7."Sauteed Beef with Pickled Peppers.".48));
    dishList.add(new Dish(8."Mapo tofu".18));
    dishList.add(new Dish(9."Dry fried beans".28));
    dishList.add(new Dish(10."Dry pot baby cabbage".29));
    dishList.add(new Dish(11."Shredded potato in dry pot".28));
    dishList.add(new Dish(12."Stir-fried vegetables.".25));
    dishList.add(new Dish(13."Cucumber salad.".10));
    dishList.add(new Dish(14.Preserved eggs with pepper.15));
    dishList.add(new Dish(15."Braised eggplant".20));
    return dishList;
}
Copy the code

Show menu

After initializing the menu, show the entire menu to the customer.

/** * display menu **@paramDishList */
public void showMenu(List<Dish> dishList) {
    System.out.println("------------ Menu ------------");
    System.out.println("Serial number \t\t name \t\t\t\t price");
    for (int i = 0; i < dishList.size(); i++) {
        System.out.format("%d\t\t%s\t\t\t%.2f\n", dishList.get(i).getId(), dishList.get(i).getName(), dishList.get(i).getPrice());
    }
    System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
}
Copy the code

Show your order

/** * Check the order */
public void showOrderedMenu(List<Dish> orderedList) {
    if (orderedList.isEmpty()) {
        System.out.println("You have not ordered at present, please re-enter select.");
    } else {
        System.out.println("Your current order is as follows.");
        for (int i = 0; i < orderedList.size(); i++) { System.out.println(orderedList.get(i).getName()); }}}Copy the code

Remove the dishes

Find the item with the corresponding serial number and remove it.

/** * delete the corresponding menu **@paramId Indicates the dish serial number *@paramDishList *@paramOrderedList List of ordered dishes */
public void deleteDish(int id, List<Dish> dishList, List<Dish> orderedList) {
    if(! orderedList.isEmpty()) { orderedList.remove(dishList.get(id -1)); }}Copy the code

The invoicing

Judge whether to order first, if not, prompt, if already ordered, then directly check out.

/** * check out */
public void checkout(List<Dish> orderedList) {
    double money = 0.0 d;
    if (orderedList.isEmpty()) {
        System.out.println("You have not ordered at present, please re-enter select.");
    } else {
        System.out.println("Just a moment, please. Settlement is in progress...");
        for (int i = 0; i < orderedList.size(); i++) {
            money += orderedList.get(i).getPrice();
        }
        System.out.format("Your total consumption this time: ¥%.2f\n", money); }}Copy the code

The total program

Well, after realizing the functions of the above modules, integrate them together and get our final overall program.

Each part of the code has been posted, but for your convenience, I give the overall program structure here.

All codes are in the com.cunyu package, followed by the entity class Dish, the interface class DishInterface, and the main program DishApp.

conclusion

Ok, so that’s the realization of our ordering system. If you have a good grasp of Java SE part of the knowledge, this is not difficult, after all, is a very simple console application, the main syntax familiar, a little logic can be.

Well, this is the end of today’s content, if you have any good suggestions, welcome to leave a message.

Finally, post the Github repository address of this design: github.com/cunyu1943/j…

If you need children’s shoes, you can take them by yourself. Of course, I also hope you can give me an ⭐ star to satisfy my vanity.