java---ordering system

Java-ordering system

Requirements:
Insert picture description here
Analysis:
Insert picture description here

Code:

package Demo20210113;

import java.util.Scanner;

/**
 * @Author shall潇
 * @Date 2021/1/13
 * @Description     点餐系统
 */
public class EatLeagueSystem {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int choice;

        String[] dishes = {"假红烧肉", "炭烤生蚝", "开水白菜", "真佛跳墙"};        //菜类
        double[] dish_price = {18, 23, 12, 30};    //菜的单价
        int[] honer = {12, 45, 203, 11};           //点赞数
        String[][] order = new String[1024][];

        do {
            System.out.println("******欢迎使用点餐系统*********");
            System.out.println("------------------------------");
            System.out.println("--------1.我要订餐-------------");
            System.out.println("--------2.查看餐袋-------------");
            System.out.println("--------3.签收订单-------------");
            System.out.println("--------4.删除订单-------------");
            System.out.println("--------5.我要点赞-------------");
            System.out.println("--------6.退出系统-------------");
            System.out.println("------------------------------");
            System.out.println("********请输入选择:***********");
            choice = sc.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("***我要订餐***");
                    System.out.println("请输入订餐人姓名:");
                    String user_name = sc.next();
                    System.out.println("序号\t菜品\t\t单价");
                    for (int i = 0; i < dishes.length; i++) {
                        System.out.println((i + 1) + "\t" + dishes[i] + "\t" + dish_price[i]);
                    }
                    int disNum = 0;
                    do {
                        System.out.println("请输入您要的菜品编号:");
                        disNum = sc.nextInt();
                        if (disNum <= 0 || disNum >= dishes.length) {
                            System.out.println("输入有误");
                        }
                    } while (disNum <= 0 || disNum >=dishes.length);
                    int pcs = 0;
                    do {
                        System.out.println("请输入您需要的份数");
                        pcs = sc.nextInt();
                        if (pcs < 0) {
                            System.out.println("输入有误");
                        }
                    }while (pcs < 0) ;
                    int time = 0;
                    do {
                            System.out.println("请输入送餐时间10~20点");
                            time = sc.nextInt();
                            if (time < 10 || time > 20) {
                                System.out.println("输入有误");
                            }
                    } while (time < 10 && time > 20);
                        System.out.println("请输入您的收货地址");
                        String address = sc.next();

                        System.out.println("订餐成功!");
                        System.out.println("您订的是:" + dishes[disNum - 1] + "\t份数:" + pcs);
                        System.out.println("送餐时间:" + time + "点");

                        double cost = dish_price[disNum - 1] * pcs;
                        double sent = cost > 30 ? 6 : 3;
                        double total = cost + sent;
                        System.out.println("订餐费:" + cost + "\t送餐费:" + sent + "\t总计:" + total);

                        for (int i = 0; i < order.length; i++) {
                            if (order[i] == null){                  //如果订单为空
                                order[i] = new String[2];           //定义两列
                                order[i][0] = user_name+"\t"+dishes[disNum-1]+" "+pcs+"份\t"+time+"点\t"+ address+"\t"+total+"元"; //第一列:订单基本信息
                                order[i][1] = "已预订";                                                                            //第二列:订单状态
                                break;
                            }
                        }
                        break;
                    case 2:
                        System.out.println("***查看餐袋***");
                        System.out.println("序号\t订餐人\t餐品信息\t送餐时间\t送餐地址\t总金额\t订单状态");
                        for (int i = 0; i < order.length; i++) {
                            if (order[i]!=null){
                                System.out.println(i+1+"\t"+order[i][0]+"\t"+order[i][1]);
                            }else {
                                break;
                            }
                        }
                        break;
                    case 3:
                        System.out.println("***签收订单***");
                        System.out.println("序号\t订餐人\t餐品信息\t送餐时间\t送餐地址\t总金额\t订单状态");
                        for (int i = 0; i < order.length; i++) {
                           if (order[i]!=null){         //如果有订单就输出
                               System.out.println(i+1+"\t"+order[i][0]+"\t"+order[i][1]);
                               int ordernum = 0;        //定义要签收的订单号
                               int end = 0;             //定义最后一位
                               do {
                                   System.out.println("请选择要签收的订单号");
                                   for (int j = 0; j < order.length; j++) {
                                       if (order[j] == null) {
                                           end = j;
                                           break;
                                       }
                                   }
                                   ordernum = sc.nextInt();
                                   if (ordernum < 1 || ordernum > end) {
                                       System.out.println("输入有误");
                                   } else if (!"已预订".equals(order[ordernum - 1][1])) {
                                       System.out.println("订单已签收,不能重复签收");
                                   } else {
                                       order[ordernum - 1][1] = "已完成";
                                       System.out.println("订单签收成功");
                                       break;
                                   }
                               } while (ordernum < 1 || ordernum > end || !"已预订".equals(order[ordernum - 1][1]));
                               break;											//只是导致bug的一个原因,不加的话会卡死,哈哈哈
                           }else {                     //没有订单就退出
                               break;
                           }
                        }
                        break;
                    case 4:
                        System.out.println("***删除订单***");
                        System.out.println("序号\t订餐人\t餐品信息\t送餐时间\t送餐地址\t总金额\t订单状态");
                        for (int i = 0; i < order.length; i++) {
                            if (order[i]!=null){
                                System.out.println(i+1+"\t"+order[i][0]+"\t"+order[i][1]);
                            }else {
                                //System.out.println("\n暂无订单!\n");
                                break;
                            }
                        }
                        int n = 0;              //定义要删除的订单号
                        int e = 0;              //定义最后一位
                        do{
                            for (int k = 0; k < order.length; k++) {
                                if(order[k]==null){
                                    e =k;
                                    break;
                                }
                            }
                            System.out.println("请输入要删除的订单号");
                            n = sc.nextInt();
                            if(n<1||n>e){
                                System.out.println("请重新输入");
                            }else if(!"已完成".equals(order[n-1][1])){
                                System.out.println("未签收不能删除");
                            }else {
                                boolean isDelete = false;			//定义删除操作标志
                                for (int j = n-1; j < order.length; j++) {
                                    if(j==order.length-1){        	//如果要删除的是最后一位
                                        order[j]=null;				//先置空
                                        isDelete = true;
                                    }else {
                                        order[j] = order[j+1];      //前移
                                        if(order[j]==null){
                                            isDelete = true;
                                            break;
                                        }
                                    }
                                }
                                if(isDelete){
                                    System.out.println("订单删除成功");
                                    break;
                                }
                            }

                        }while (n<1||n>e||!"已完成".equals(order[n-1][1]));
                        break;
                    case 5:
                        System.out.println("***我要点赞***");
                        for (int i = 0; i < dishes.length; i++) {
                            System.out.println((i+1)+"\t"+dishes[i]+"点赞数:"+honer[i]);       //先输出序号,菜品,点赞数
                        }
                        int hornor_dish_pos = 0;                //记录被点赞的菜品序号
                        do{
                            System.out.println("请选择要点赞的菜品序号:(每次点赞只能点赞一次)");
                            hornor_dish_pos = sc.nextInt();
                            if(hornor_dish_pos<=0 || hornor_dish_pos>=dishes.length){
                                System.out.println("输入序号有误");
                            }else {
                                honer[hornor_dish_pos - 1]++;     //被点赞的菜品的点赞数++
                                System.out.println("点赞完成!");
                                break;
                            }
                        }while (hornor_dish_pos<=0 || hornor_dish_pos>=dishes.length);
                        break;
                    default:
                        System.exit(0);
                    }
                    if (choice >= 1 && choice <= 6) {
                        System.out.println("输入0返回");
                        choice = sc.nextInt();
                    } else {
                        break;
                    }
            } while (choice == 0) ;
            System.out.println("欢迎使用本系统");
        }
}

In fact, the content is not very good, because this is just using the content mentioned before, and does not use functions, try-catch, collections, etc., so it is very messy and not concise enough, but the basic code logic and business logic are not problem. But this program has two bugs:

The first one is that only the first order information can be output in the sign-for-order function, but other information is not displayed (it is indeed saved, you can go to the 2 function to check) the
second is the actual delete order function, if all are not signed Orders will be stuck.

In fact, these two are the same bug at the beginning, but I changed the bug of signing the order (which can solve the problem of cycle stuck) and a new bug occurred, hahahaha.The reason is that a break is added at the end of the if statement at the beginning, Resulting in the output can only output the first order information. Therefore, a new bug is generated, and the deleted function is too lazy to change.

Guess you like

Origin blog.csdn.net/qq_43288259/article/details/112573356