javase饭店模拟(基础)

javase饭店模拟(基础)

纯加判定,这里还是受苦,为了不让程序因为控制台乱输入,出现异常停止运行,改的乱七八糟的,一个异常抛出多好,懒的改了。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
代码:
Pt2Run类,几乎所有操作都在这


import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Pt2Run {
    public Pt2Run(List<Customer> customersList, List<Restaurant> list) {
        Scanner s = new Scanner(System.in);
        int serviceNo = 0;
        while (true) {
            int count = 0;

            list = LikeList.likeList(list);

            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("7.退出系统");
            System.out.println("***********************************");
            System.out.print("请选择(1-7):");
            s = new Scanner(System.in);
            if (s.hasNextInt()) {
                serviceNo = s.nextInt();
                s.nextLine();
            }
            switch (serviceNo) {
                case 1:
                    System.out.println("****我要订餐****");
                    System.out.print("请输入订餐人姓名:");
                    customersList.add(new Customer());
                    count = customersList.size() - 1;
                    customersList.get(count).setName(s.nextLine());
                    System.out.println("序号   菜名       单价");
                    for (int i = 0; i < 3; i++) {
                        System.out.println(" " + list.get(i).getNum() + "   " + list.get(i).getDishName() + "  " + list.get(i).getPrice() + "元");
                    }

                    System.out.print("请选择您要点的菜品编号:");
                    int in;
                    if (s.hasNextInt()) {
                        in = s.nextInt() - 1;
                        s.nextLine();
                    } else {
                        System.out.println("请确认输入的是数字");
                        customersList.remove(count);
                        break;
                    }
                    if (in >= 0 && in < 3) {
                        customersList.get(count).setDishOrder(list.get(in).getNum());
                    } else {
                        System.out.println("菜品序号错误!");
                        customersList.remove(count);
                        break;
                    }

                    System.out.print("请选择您需要菜品份数:");

                    if (s.hasNextInt()) {
                        in = s.nextInt();
                        s.nextLine();
                    } else {
                        System.out.println("请确认输入的是数字");
                        customersList.remove(count);
                        break;
                    }

                    if (in > 0 && in < 99) {
                        customersList.get(count).setDishNum(in);
                    } else {
                        System.out.println("请输入正确的份数");
                        customersList.remove(count);
                        break;
                    }

                    System.out.print("请输入送餐时间(十点到20点整点):");
                    if (s.hasNextInt()) {
                        in = s.nextInt();
                        s.nextLine();
                    } else {
                        System.out.println("请确认输入的是数字");
                        customersList.remove(count);
                        break;
                    }
                    if (in > 9 && in < 21) {
                        customersList.get(count).setTime(in);
                    } else {
                        System.out.println("请输入正确订餐时间");
                        customersList.remove(count);
                        break;
                    }

                    System.out.print("请输入送餐地址:");
                    customersList.get(count).setAddr(s.nextLine());
                    customersList.get(count).setNo(count + 1);

                    System.out.println("订餐成功!");
                    System.out.println("您订的是:" + list.get(customersList.get(count).getDishOrder() - 1).getDishName() + " " + customersList.get(count).getDishNum() + "份 ");
                    System.out.println("送餐时间: " + customersList.get(count).getTime());
                    customersList.get(count).setPriceAll(customersList.get(count).getDishNum() * list.get(customersList.get(count).getDishOrder() - 1).getPrice());
                    System.out.println("餐费: " + customersList.get(count).getPriceAll() + "元,送餐费:0.0元,总计:" + customersList.get(count).getPriceAll() + "元");
                    System.out.print("输入0返回:");
                    if (s.hasNextInt() && s.nextInt() == 0) {
                        s.nextLine();
                    } else {
                        System.out.println("请不要乱输入");
                    }
                    break;

                case 2:
                    System.out.println("***查看餐袋***");
                    System.out.println("序号  订餐人     餐品信息     送餐日期  送餐地址      总金额  订单状:");
                    for (int i = 0; i < customersList.size(); i++) {
                        if (customersList.get(i) != null) {
                            customersList.get(i).setNo(i + 1);
                            System.out.println(" " + customersList.get(i).getNo() + "  " + customersList.get(i).getName() + "   " + list.get(customersList.get(i).getDishOrder() - 1).getDishName() + " " + customersList.get(i).getDishNum() + "份    " + customersList.get(i).getTime() + "时" + "     " + customersList.get(i).getAddr() + "         " + customersList.get(i).getPriceAll() + "元" + "    " + (!customersList.get(i).isState() ? "已预定" : "已完成"));
                        }
                    }
                    System.out.print("输入0返回:");
                    if (s.hasNextInt() && s.nextInt() == 0) {
                        s.nextLine();
                    } else {
                        System.out.println("请不要乱输入");
                    }
                    break;

                case 3:
                    System.out.println("***签收订单***");
                    System.out.print("请选择要签收的订单序号:");
                    if (s.hasNextInt()) {
                        in = s.nextInt() - 1;
                        s.nextLine();
                    } else {
                        System.out.println("请确认输入的是数字");
                        continue;
                    }
                    if (customersList.size() > in && customersList.get(in) != null) {
                        customersList.get(in).setState(true);
                    } else {
                        System.out.println("没有找到订单");
                        break;
                    }
                    System.out.println("订单签收成功");
                    System.out.print("输入0返回:");
                    if (s.hasNextInt() && s.nextInt() == 0) {
                        s.nextLine();
                    } else {
                        System.out.println("请不要乱输入");
                    }
                    break;


                case 4:
                    System.out.println("***删除订单***");
                    System.out.print("请输入要删除的订单(请先查看订单):");
                    if (s.hasNextInt()) {
                        in = s.nextInt();
                        s.nextLine();
                    } else {
                        System.out.println("请确认输入的是数字");
                        continue;
                    }
                    boolean success = false;
                    for (int i = 0; i < customersList.size(); i++) {
                        customersList.get(i).setNo(i + 1);
                        if (customersList.get(i).getNo() == in && customersList.get(i).isState()) {
                            customersList.remove(i);
                            System.out.println("删除成功");
                            success = true;
                            break;
                        }
                    }
                    if (!success) {
                        System.out.println("删除失败,该订单不存在,或者该订单未完成");
                    }
                    System.out.print("输入0返回:");
                    if (s.hasNextInt() && s.nextInt() == 0) {
                        s.nextLine();
                    } else {
                        System.out.println("请不要乱输入");
                    }
                    break;

                case 5:
                    System.out.println("***我要点赞***");
                    System.out.println("序号   菜名       单价   点赞数");
                    for (int i = 0; i < 3; i++) {
                        System.out.println(" " + list.get(i).getNum() + "   " + list.get(i).getDishName() + "  " + list.get(i).getPrice() + "元   " + list.get(i).getSatisfied());
                    }
                    System.out.print("请选择您要点赞的菜品编号:");
                    if (s.hasNextInt()) {
                        in = s.nextInt() - 1;
                        s.nextLine();
                    } else {
                        System.out.println("请确认输入的是数字");
                        continue;
                    }
                    if (in >= 0 && in < 3) {
                        list.get(in).setSatisfied(list.get(in).getSatisfied() + 1);
                        System.out.println("点赞成功");
                    } else {
                        System.out.println("不存在该菜品");
                    }
                    System.out.print("输入0返回:");
                    if (s.hasNextInt() && s.nextInt() == 0) {
                        s.nextLine();
                    } else {
                        System.out.println("请不要乱输入");
                    }
                    break;


                case 6:
                    System.out.println("***查看点赞***");
                    System.out.println("序号   菜名       单价   点赞数");
                    for (int i = 0; i < 3; i++) {
                        System.out.println(" " + list.get(i).getNum() + "   " + list.get(i).getDishName() + "  " + list.get(i).getPrice() + "元   " + list.get(i).getSatisfied());
                    }
                    System.out.print("输入0返回:");
                    if (s.hasNextInt() && s.nextInt() == 0) {
                        s.nextLine();
                    } else {
                        System.out.println("请不要乱输入");
                    }
                    break;

                case 7:
                    System.out.println("谢谢使用,欢迎下次光临!");
                    System.exit(0);

                default:
                    System.out.println("没有该命令,请重新输入");
                    System.out.print("输入0返回:");
                    if (s.hasNextInt() && s.nextInt() == 0) {
                        s.nextLine();
                    } else {
                        System.out.println("请不要乱输入");
                    }
                    break;
            }
        }
    }
}

LikeList类,点赞后,餐单重新排列

import java.util.List;

public class LikeList {
    public LikeList() {
    }
    public static List<Restaurant> likeList(List<Restaurant> list) {
        Restaurant rs = list.get(0);
        for (int i = 1; i < list.size(); i++) {
            for (int j = 0; j < list.size(); j++) {
                if (list.get(i).getSatisfied() > list.get(j).getSatisfied()) {
                    rs = list.get(i);
                    list.remove(i);
                    list.add(i, list.get(j));
                    list.remove(j);
                    list.add(j, rs);
                }
            }
        }
        return list;
    }
}

main方法,顺便菜单,客户类也扔这了

import java.util.ArrayList;
import java.util.List;
public class PractisesV2 {
    public static void main(String[] args) {
        List<Restaurant> list = new ArrayList<>();
        list.add(new Restaurant(1, "红烧带鱼", 38.0, 0));
        list.add(new Restaurant(2, "鱼香肉丝", 20.0, 0));
        list.add(new Restaurant(3, "时令蔬菜", 10.0, 0));
        List<Customer> customerList = new ArrayList<>();
        new Pt2Run(customerList, list);
    }
}
class Restaurant {
    private int num;
    private String dishName;
    private double price;
    private int satisfied;
    public Restaurant() {
    }
    public Restaurant(int num, String dishName, double price, int satisfied) {
        this.num = num;
        this.dishName = dishName;
        this.price = price;
        this.satisfied = satisfied;
    }
    public int getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }
    public String getDishName() {
        return dishName;
    }
    public void setDishName(String dishName) {
        this.dishName = dishName;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public int getSatisfied() {
        return satisfied;
    }
    public void setSatisfied(int satisfied) {
        this.satisfied = satisfied;
    }
}
class Customer {
    private int no;
    private String name;
    private int dishOrder;
    private int dishNum;
    private int time;
    private double priceAll;
    private String addr;
    boolean state;
    public Customer() {
    }
    public Customer(int no, String name, int dishOrder, int dishNum, int time, double priceAll, String addr, boolean state) {
        this.no = no;
        this.name = name;
        this.dishOrder = dishOrder;
        this.dishNum = dishNum;
        this.time = time;
        this.priceAll = priceAll;
        this.addr = addr;
        this.state = state;
    }
    public int getNo() {
        return no;
    }
    public void setNo(int no) {
        this.no = no;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getDishOrder() {
        return dishOrder;
    }
    public void setDishOrder(int dishOrder) {
        this.dishOrder = dishOrder;
    }
    public int getDishNum() {
        return dishNum;
    }
    public void setDishNum(int dishNum) {
        this.dishNum = dishNum;
    }
    public int getTime() {
        return time;
    }
    public void setTime(int time) {
        this.time = time;
    }
    public double getPriceAll() {
        return priceAll;
    }
    public void setPriceAll(double priceAll) {
        this.priceAll = priceAll;
    }
    public String getAddr() {
        return addr;
    }
    public void setAddr(String addr) {
        this.addr = addr;
    }
    public boolean isState() {
        return state;
    }
    public void setState(boolean state) { this.state = state;
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值