小结blog2

发布时间 2023-11-19 22:05:27作者: Nwhf

一、前言

  这次总结是对第四次题目集,第五次题目集,第六次题目集和期中考试的总结

  1.第四次题目集主要是菜单三简单构建,实现复杂任务类的设计

  2.第五次题目集主是:菜单计价程序4,在基于菜单计价程序3的基础上进行完善,增加了新的需求,主要是异常情况

  3.第六次题目集主是:菜单计价程序5,是在菜单计价程序3的基础上进行完善,不过是和菜单计价程序4不同的分支,本次作业侧重的是在对特色菜的处理上面,以及需求的复杂化

  4.期中考试主要是概念选择题,以及类的构建,继承,多态的使用,接口的实现

二、设计与分析

1、菜单计价程序2

题目要求:
设计点菜计价程序,根据输入的信息,计算并输出总价格。输入内容按先后顺序包括两部分:菜单、订单,最后以"end"结束。菜单由一条或多条菜品记录组成,每条记录一行。每条菜品记录包含:菜名、基础价格两个信息。订单分:点菜记录和删除信息。每一类信息都可包含一条或多条记录,每条记录一行。点菜记录包含:序号、菜名、份额、份数。份额可选项包括:1、2、3,分别代表小、中、大份。删除记录格式:序号 delete
代码
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Menu menu = new Menu();
        Order order = new Order();
        String f1 ="\\S+"+" "+"\\d+";
        String f2 ="\\d+"+" "+"\\S+"+" "+"\\d+"+" "+"\\d+";
        String f3 = "\\d+"+" "+"delete";
        String f4 ="end";
        while(true){
            String a = scanner.nextLine();
            if (a.equals(f4)) {
                break;
            }
            if(a.matches(f1)){
                String[] ss = a.split(" ");
                int p = Integer.parseInt(ss[1]);
                menu.addDish(ss[0],p);
            }
            if(a.matches(f2)){
                String[] ss = a.split(" ");
                Dish dish=menu.searchDish(ss[1]);
                int oN = Integer.parseInt(ss[0]);
                int pt = Integer.parseInt(ss[2]);
                int nm = Integer.parseInt(ss[3]);
                if(dish==null){
                    System.out.println(ss[1]+" does not exist");
                }
                else{
                    Record record = order.addARecord(oN, ss[1], pt, nm);
                    System.out.println(record.orderNum + " " + record.d.name + " " + record.getPrice());
                }
            }
            if(a.matches(f3)){
                String[] ss = a.split(" ");
                int orderNum = Integer.parseInt(ss[0]);
                Record r=order.findRecordByNum(orderNum);
                if(r==null){
                    System.out.println("delete error;");
                }
                else{
                    order.delARecordByOrderNum(orderNum);
                }
            }
        }
        System.out.println(order.getTotalPrice());
    }

}

class Dish {
    String name;
    int unit_price;

    public Dish(String name, int unit_price) {
        this.name = name;
        this.unit_price = unit_price;
    }

    int getPrice(int portion) {
        double p = 0;
        if (portion == 1) {
            p = unit_price;
        }
        if (portion == 2) {
            p = unit_price * 1.5;
        }
        if (portion == 3) {
            p = unit_price * 2;
        }
        return (int) Math.round(p);
    }
}

class Menu {
    static Map<String, Dish> dishMap = new HashMap<>();


    public static Dish searchDish(String name) {
        return dishMap.get(name);
    }

    public void addDish(String name, int unit_price) {
        Dish dish = new Dish(name, unit_price);
        dishMap.put(name, dish);
    }
}

class Record {
    int orderNum;
    Dish d;
    int portion;
    int sum;

    public Record(int orderNum, Dish dish, int portion, int sum) {
        this.orderNum = orderNum;
        this.d = dish;
        this.portion = portion;
        this.sum = sum;
    }

    public int getPrice() {
        return d.getPrice(portion) * sum;
    }
}

class Order {
    List<Record> records = new ArrayList<>();


    public int getTotalPrice() {
        int totalPrice = 0;
        for (Record record : records) {
            if (record != null) {
                totalPrice += record.getPrice();
            }
        }
        return totalPrice;
    }

    Record addARecord(int orderNum, String dishName, int portion, int sum) {
        Dish dish = Menu.searchDish(dishName);
        Record record = new Record(orderNum, dish, portion, sum);
        records.add(record);
        return record;

    }

    public void delARecordByOrderNum(int orderNum) {
        for (int i = 0; i < records.size(); i++) {
            if (records.get(i).orderNum == orderNum) {
                records.remove(i);
                break;
            }
        }
    }

    public Record findRecordByNum(int orderNum) {
        for (Record record : records) {
            if (record.orderNum == orderNum) {
                return record;
            }
        }
        return null;
    }
}

题目给与了类的实现以及对于函数功能,主要考察代码编程基本功

2、菜单计价程序3

题目要求:在菜单计价程序2的基础上,添加以下要求:

订单分:桌号标识、点菜记录和删除信息、代点菜信息。每一类信息都可包含一条或多条记录,每条记录一行或多行。

桌号标识独占一行,包含两个信息:桌号、时间。

桌号以下的所有记录都是本桌的记录,直至下一个桌号标识。

点菜记录包含:序号、菜名、份额、份数。份额可选项包括:1、2、3,分别代表小、中、大份。

不同份额菜价的计算方法:小份菜的价格=菜品的基础价格。中份菜的价格=菜品的基础价格1.5。小份菜的价格=菜品的基础价格2。如果计算出现小数,按四舍五入的规则进行处理。

删除记录格式:序号 delete

标识删除对应序号的那条点菜记录。

如果序号不对,输出"delete error"

代点菜信息包含:桌号 序号 菜品名称 份额 分数

代点菜是当前桌为另外一桌点菜,信息中的桌号是另一桌的桌号,带点菜的价格计算在当前这一桌。

程序最后按输入的先后顺序依次输出每一桌的总价(注意:由于有代点菜的功能,总价不一定等于当前桌上的菜的价格之和)。

每桌的总价等于那一桌所有菜的价格之和乘以折扣。如存在小数,按四舍五入规则计算,保留整数。

折扣的计算方法(注:以下时间段均按闭区间计算):

周一至周五营业时间与折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其余时间不营业。

周末全价,营业时间:9:30-21:30

如果下单时间不在营业范围内,输出"table " + t.tableNum + " out of opening hours"

import java.time.LocalTime;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Menu menu = new Menu();
        Order order = new Order();
        int i = 0;
        LocalTime startTime1 = LocalTime.of(16, 59,59);
        LocalTime endTime1 = LocalTime.of(20, 30,1);

        LocalTime startTime2 = LocalTime.of(10, 29,59);
        LocalTime endTime2 = LocalTime.of(14, 30,1);

        LocalTime startTime3 = LocalTime.of(9, 29,59);
        LocalTime endTime3 = LocalTime.of(21, 30,1);

        ArrayList<Table> t = new ArrayList<>();
        String f1 = "\\S+" + " " + "\\d+";
        String f2 = "\\d+" + " " + "\\S+" + " " + "\\d+" + " " + "\\d+";
        String f3 = "\\d+" + " " + "delete";
        String f4 = "end";
        String f5 = "table" + " " + "\\S+" + " " + "\\d{4}" + "/" + "\\d+" + "/" + "\\d+" + " " + "\\d+" + "/" + "\\d+" + "/" + "\\d+";
        String f6 = "\\d+" + " " + "\\d+"+" "+"\\S+" + " " + "\\d+" + " " + "\\d+";
        while (true) {
            String a = scanner.nextLine();
            if (a.equals(f4)) {
                break;
            }
            if (a.matches(f1)) {
                String[] ss = a.split(" ");
                int p = Integer.parseInt(ss[1]);
                menu.addDish(ss[0], p);
            }
            if (a.matches(f5)) {

                String[] ss = a.split(" ");
                Table table = new Table();
                int tn = Integer.parseInt(ss[1]);
                String[] d = ss[2].split("/");
                int year = Integer.parseInt(d[0]);
                int month = Integer.parseInt(d[1]);
                int day = Integer.parseInt(d[2]);
                table.tablenum = tn;
                String[] time = ss[3].split("/");
                int HH = Integer.parseInt(time[0]);
                int MM = Integer.parseInt(time[1]);
                int SS = Integer.parseInt(time[2]);

                Calendar calendar = Calendar.getInstance();
                calendar.set(year, month - 1, day);
                int dayofWeek1 = calendar.get(Calendar.DAY_OF_WEEK) - 1;
                table.dayofWeek = dayofWeek1;
                table.HH=HH;
                table.MM=MM;
                table.SS=SS;
                t.add(i, table);
                i++;
                System.out.println("table "+i+": ");


            }
            if (a.matches(f2)) {
                String[] ss2 = a.split(" ");
                Dish dish = menu.searchDish(ss2[1]);
                int oN = Integer.parseInt(ss2[0]);
                int pt = Integer.parseInt(ss2[2]);
                int nm = Integer.parseInt(ss2[3]);
                if (dish == null) {
                    System.out.println(ss2[1] + " does not exist");
                } else {

                    Record record = t.get(i - 1).addARecord(oN, ss2[1], pt, nm);
                    System.out.println(record.orderNum + " " + record.d.name + " " + record.getPrice());
                }
            }
            if(a.matches(f6)){
                String[] ss2 = a.split(" ");
                int tHelp = Integer.parseInt(ss2[0]);
                int fene= Integer.parseInt(ss2[3]);
                int fenshu= Integer.parseInt(ss2[4]);
                Dish dish1 = menu.searchDish(ss2[2]);
                t.get(i-1).pay=dish1.getPrice(fene)*fenshu;
                System.out.println(Integer.parseInt(ss2[1])+" table "+i+" pay for table "+tHelp+" "+dish1.getPrice(fene)*fenshu);
            }
            if (a.matches(f3)) {
                String[] ss2 = a.split(" ");
                int orderNum = Integer.parseInt(ss2[0]);
                Record r = t.get(i - 1).findRecordByNum(orderNum);
                if (r == null) {
                    System.out.println("delete error;");
                } else {
                    t.get(i - 1).delARecordByOrderNum(orderNum);
                }
            }

        }
        for (int j = 0; j < t.size(); j++) {
            int dt=t.get(j).dayofWeek;
            LocalTime st1 = LocalTime.of(t.get(j).HH, t.get(j).MM,t.get(j).SS);
            if(dt==0||dt==6){
                if(bt(st1,startTime3,endTime3)){
                    System.out.println("table "+t.get(j).tablenum+": "+Math.round(t.get(j).getTotalPrice()+t.get(j).pay));
                }else{
                    System.out.println("table " + t.get(j).tablenum + " out of opening hours");
                }
            }
            if(dt>=1&&dt<=5){
                if(bt(st1,startTime1,endTime1)){
                    System.out.println("table "+t.get(j).tablenum+": "+Math.round((t.get(j).getTotalPrice()+t.get(j).pay)*0.8));
                }
                else if(bt(st1,startTime2,endTime2)){
                    System.out.println("table "+t.get(j).tablenum+": "+Math.round((t.get(j).getTotalPrice()+t.get(j).pay)*0.6));
                }
                else{
                    System.out.println("table " + t.get(j).tablenum + " out of opening hours");
                }
            }
            }
        }


    public static boolean bt(LocalTime time, LocalTime startTime, LocalTime endTime) {
        return time.isAfter(startTime) && time.isBefore(endTime);
    }
}


class Dish {
    String name;
    int unit_price;

    public Dish(String name, int unit_price) {
        this.name = name;
        this.unit_price = unit_price;
    }

    int getPrice(int portion) {
        double p = 0;
        if (portion == 1) {
            p = unit_price;
        }
        if (portion == 2) {
            p = unit_price * 1.5;
        }
        if (portion == 3) {
            p = unit_price * 2;
        }
        return (int) Math.round(p);
    }
}

class Menu {
    static Map<String, Dish> dishMap = new HashMap<>();


    public static Dish searchDish(String name) {
        return dishMap.get(name);
    }

    public void addDish(String name, int unit_price) {
        Dish dish = new Dish(name, unit_price);
        dishMap.put(name, dish);
    }
}

class Record {
    int orderNum;
    Dish d;
    int portion;
    int sum;

    public Record(int orderNum, Dish dish, int portion, int sum) {
        this.orderNum = orderNum;
        this.d = dish;
        this.portion = portion;
        this.sum = sum;
    }

    public int getPrice() {
        return d.getPrice(portion) * sum;
    }
}

class Order {
    List<Record> records = new ArrayList<>();


    public int getTotalPrice() {
        int totalPrice = 0;
        for (Record record : records) {
            if (record != null) {
                totalPrice += record.getPrice();
            }
        }
        return totalPrice;
    }

    Record addARecord(int orderNum, String dishName, int portion, int sum) {
        Dish dish = Menu.searchDish(dishName);
        Record record = new Record(orderNum, dish, portion, sum);
        records.add(record);
        return record;

    }

    public void delARecordByOrderNum(int orderNum) {
        for (int i = 0; i < records.size(); i++) {
            if (records.get(i).orderNum == orderNum) {
                records.remove(i);
                break;
            }
        }
    }

    public Record findRecordByNum(int orderNum) {
        for (Record record : records) {
            if (record.orderNum == orderNum) {
                return record;
            }
        }
        return null;
    }
}

class Table extends Order {
    int tablenum;
    int dayofWeek;
    int HH;
    int MM;
    int SS;
    int pay=0;
}

 主体由上图可知

主要是日期的判断

 使用LocalTime记录日期

同时使用正则表达式进行输入的判断

 和table类的创建

 3.菜单计价程序-4

作者 蔡轲
单位 南昌航空大学

本体大部分内容与菜单计价程序-3相同,增加的部分用加粗文字进行了标注。

设计点菜计价程序,根据输入的信息,计算并输出总价格。

输入内容按先后顺序包括两部分:菜单、订单,最后以"end"结束。

菜单由一条或多条菜品记录组成,每条记录一行

每条菜品记录包含:菜名、基础价格 两个信息。

订单分:桌号标识、点菜记录和删除信息、代点菜信息。每一类信息都可包含一条或多条记录,每条记录一行或多行。

桌号标识独占一行,包含两个信息:桌号、时间。

桌号以下的所有记录都是本桌的记录,直至下一个桌号标识。

点菜记录包含:序号、菜名、份额、份数。份额可选项包括:1、2、3,分别代表小、中、大份。

不同份额菜价的计算方法:小份菜的价格=菜品的基础价格。中份菜的价格=菜品的基础价格1.5。小份菜的价格=菜品的基础价格2。如果计算出现小数,按四舍五入的规则进行处理。

删除记录格式:序号 delete

标识删除对应序号的那条点菜记录。

如果序号不对,输出"delete error"

代点菜信息包含:桌号 序号 菜品名称 份额 分数

代点菜是当前桌为另外一桌点菜,信息中的桌号是另一桌的桌号,带点菜的价格计算在当前这一桌。

程序最后按输入的桌号从小到大的顺序依次输出每一桌的总价(注意:由于有代点菜的功能,总价不一定等于当前桌上的菜的价格之和)。

每桌的总价等于那一桌所有菜的价格之和乘以折扣。如存在小数,按四舍五入规则计算,保留整数。

折扣的计算方法(注:以下时间段均按闭区间计算):

周一至周五营业时间与折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其余时间不营业。

周末全价,营业时间:9:30-21:30

如果下单时间不在营业范围内,输出"table " + t.tableNum + " out of opening hours"

参考以下类的模板进行设计(本内容与计价程序之前相同,其他类根据需要自行定义):

菜品类:对应菜谱上一道菜的信息。

Dish {

String name;//菜品名称

int unit_price; //单价

int getPrice(int portion)//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份) }

菜谱类:对应菜谱,包含饭店提供的所有菜的信息。

Menu {

Dish[] dishs ;//菜品数组,保存所有菜品信息

Dish searthDish(String dishName)//根据菜名在菜谱中查找菜品信息,返回Dish对象。

Dish addDish(String dishName,int unit_price)//添加一道菜品信息

}

点菜记录类:保存订单上的一道菜品记录

Record {

int orderNum;//序号

Dish d;//菜品\\

int portion;//份额(1/2/3代表小/中/大份)

int getPrice()//计价,计算本条记录的价格

}

订单类:保存用户点的所有菜的信息。

Order {

Record[] records;//保存订单上每一道的记录

int getTotalPrice()//计算订单的总价

Record addARecord(int orderNum,String dishName,int portion,int num)//添加一条菜品信息到订单中。

delARecordByOrderNum(int orderNum)//根据序号删除一条记录

findRecordByNum(int orderNum)//根据序号查找一条记录

}

本次课题比菜单计价系列-3增加的异常情况:

1、菜谱信息与订单信息混合,应忽略夹在订单信息中的菜谱信息。输出:"invalid dish"

2、桌号所带时间格式合法(格式见输入格式部分说明,其中年必须是4位数字,月、日、时、分、秒可以是1位或2位数),数据非法,比如:2023/15/16 ,输出桌号+" date error"

3、同一桌菜名、份额相同的点菜记录要合并成一条进行计算,否则可能会出现四舍五入的误差。

4、重复删除,重复的删除记录输出"deduplication :"+序号。

5、代点菜时,桌号不存在,输出"Table number :"+被点菜桌号+" does not exist";本次作业不考虑两桌记录时间不匹配的情况。

6、菜谱信息中出现重复的菜品名,以最后一条记录为准。

7、如果有重复的桌号信息,如果两条信息的时间不在同一时间段,(时段的认定:周一到周五的中午或晚上是同一时段,或者周末时间间隔1小时(不含一小时整,精确到秒)以内算统一时段),此时输出结果按不同的记录分别计价。

8、重复的桌号信息如果两条信息的时间在同一时间段,此时输出结果时合并点菜记录统一计价。前提:两个的桌号信息的时间都在有效时间段以内。计算每一桌总价要先合并符合本条件的饭桌的点菜记录,统一计价输出。

9、份额超出范围(1、2、3)输出:序号+" portion out of range "+份额,份额不能超过1位,否则为非法格式,参照第13条输出。

10、份数超出范围,每桌不超过15份,超出范围输出:序号+" num out of range "+份数。份数必须为数值,最高位不能为0,否则按非法格式参照第16条输出。

11、桌号超出范围[1,55]。输出:桌号 +" table num out of range",桌号必须为1位或多位数值,最高位不能为0,否则按非法格式参照第16条输出。

12、菜谱信息中菜价超出范围(区间(0,300)),输出:菜品名+" price out of range "+价格,菜价必须为数值,最高位不能为0,否则按非法格式参照第16条输出。

13、时间输入有效但超出范围[2022.1.1-2023.12.31],输出:"not a valid time period"

14、一条点菜记录中若格式正确,但数据出现问题,如:菜名不存在、份额超出范围、份数超出范围,按记录中从左到右的次序优先级由高到低,输出时只提示优先级最高的那个错误。

15、每桌的点菜记录的序号必须按从小到大的顺序排列(可以不连续,也可以不从1开始),未按序排列序号的输出:"record serial number sequence error"。当前记录忽略。(代点菜信息的序号除外)

16、所有记录其它非法格式输入,统一输出"wrong format"

17、如果记录以“table”开头,对应记录的格式或者数据不符合桌号的要求,那一桌下面定义的所有信息无论正确或错误均忽略,不做处理。如果记录不是以“table”开头,比如“tab le 55 2023/3/2 12/00/00”,该条记录认为是错误记录,后面所有的信息并入上一桌一起计算。

本次作业比菜单计价系列-3增加的功能:

菜单输入时增加特色菜,特色菜的输入格式:菜品名+英文空格+基础价格+"T"

例如:麻婆豆腐 9 T

菜价的计算方法:

周一至周五 7折, 周末全价。

注意:不同的四舍五入顺序可能会造成误差,请按以下步骤累计一桌菜的菜价:

计算每条记录的菜价:将每份菜的单价按份额进行四舍五入运算后,乘以份数计算多份的价格,然后乘以折扣,再进行四舍五入,得到本条记录的最终支付价格。

最后将所有记录的菜价累加得到整桌菜的价格。

输入格式:

桌号标识格式:table + 序号 +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)

菜品记录格式:

菜名+英文空格+基础价格

如果有多条相同的菜名的记录,菜品的基础价格以最后一条记录为准。

点菜记录格式:序号+英文空格+菜名+英文空格+份额+英文空格+份数注:份额可输入(1/2/3), 1代表小份,2代表中份,3代表大份。

删除记录格式:序号 +英文空格+delete

代点菜信息包含:桌号+英文空格+序号+英文空格+菜品名称+英文空格+份额+英文空格+分数

最后一条记录以“end”结束。

输出格式:

按输入顺序输出每一桌的订单记录处理信息,包括:

1、桌号,格式:table+英文空格+桌号+”:”+英文空格

2、按顺序输出当前这一桌每条订单记录的处理信息,

每条点菜记录输出:序号+英文空格+菜名+英文空格+价格。其中的价格等于对应记录的菜品*份数,序号是之前输入的订单记录的序号。如果订单中包含不能识别的菜名,则输出“** does not exist”,**是不能识别的菜名

如果删除记录的序号不存在,则输出“delete error”

最后按输入顺序一次输出每一桌所有菜品的总价(整数数值)格式:table+英文空格+桌号+“:”+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价

import java.time.DateTimeException;
import java.time.LocalDate;
import java.util.*;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Order order = new Order();
        Menu menu = new Menu();
        boolean tnum = false;
        boolean tablefomat = true;
        String time = "";
        boolean mixmenu = true;
        String s = sc.nextLine();
        while (!s.equals("end")){
            int flag = 0;
            boolean flag1 = s.matches("[\\S]*\\s[0-9]*");
            boolean flag2 = s.matches("table\\s[^0\\s]\\S*\\s\\d{4}/\\d{1,2}/\\d{1,2}\\s\\d{1,2}/\\d{1,2}/\\d{1,2}");
            boolean flag3 = s.matches("[1-9]\\d*\\s[\\S]*\\s\\d\\s\\d*");
            boolean flag4 = s.matches("[1-9]\\d*\\s\\d*\\s[\\S]*\\s\\d\\s\\d*");
            boolean flag5 = s.matches("\\d*\\sdelete");
            boolean flag6 = s.matches("[\\S]*\\s[0-9]*\\sT");
            String[] str = s.split(" ");
            if((flag1||flag6)){
                if((!mixmenu)&& tnum){
                    System.out.println("invalid dish");
                }
                else {
                    String name = str[0];
                    int price = Integer.parseInt(str[1]);
                    if(price<=0||price>=300){
                        System.out.println(name+ " " + "price out of range " + price);
                    }
                    else {
                        if (!flag6) {
                            menu.addDish(name, price, "F");
                        } else {
                            menu.addDish(name, price, "T");
                        }
                    }
                }
            }
            else if(flag2){
                mixmenu = false;
                time = s;
                try {
                    String[] strings = s.split(" ");
                    int num = Integer.parseInt(strings[1]);
                    tablefomat = true;
                    if(!(num>0&&num<=55)){
                        System.out.println(num + " table num out of range");
                        tnum = false;
                        tablefomat = false;
                    }
                    else {
                        try {
                            String[] s1 = strings[2].split("/");
                            LocalDate ld = LocalDate.of(Integer.parseInt(s1[0]), Integer.parseInt(s1[1]), Integer.parseInt(s1[2]));
                            if (!isTimeInRange(strings[2])) {
                                System.out.println("not a valid time period");
                                tnum = false;
                                tablefomat = false;
                            } else if (!order.whetherTimeRight(time)) {
                                System.out.println(strings[0] + " " + strings[1] + " out of opening hours");
                                tnum = false;
                                tablefomat = false;
                            } else {
                                tnum = true;
                                Table table = new Table(s, -1, -1);
                                order.tables.add(table);
                                System.out.println(strings[0] + " " + num + ": ");
                            }
                        } catch (DateTimeException e) {
                            tnum = false;
                            System.out.println(strings[1] + " date error");
                        }
                    }
                }catch (NumberFormatException e){
                    tnum = false;
                    tablefomat = false;
                    System.out.println("wrong format");
                }
            }
            else if(flag3){
                if(tnum) {
                    int orderNum = Integer.parseInt(str[0]);
                    String name = str[1];
                    int portion = Integer.parseInt(str[2]);
                    int num = Integer.parseInt(str[3]);
                    if (menu.searthDish(name) != null) {
                        order.addARecord(orderNum, name, portion, num, menu, time, flag);
                    } else {
                        System.out.println(name + " does not exist");
                    }
                }
            }
            else if(flag4){
                if(tnum) {
                    flag = Integer.parseInt(str[0]);
                    if(!order.isTableNumisExist(flag)){
                        System.out.println("Table number :" + flag +" does not exist");
                    }
                    else {
                        int orderNum = Integer.parseInt(str[1]);
                        String name = str[2];
                        int portion = Integer.parseInt(str[3]);
                        int num = Integer.parseInt(str[4]);
                        if (menu.searthDish(name) != null) {
                            order.addARecord(orderNum, name, portion, num, menu, time, flag);
                        } else {
                            System.out.println(name + " does not exist");
                        }
                    }
                }
            }
            else if(flag5){
                if(tnum) {
                    int ordernum = Integer.parseInt(str[0]);
                    order.delARecordByOrderNum(ordernum, time);
                }
            }
            else {
                if(str[0].equals("table")){
                    tnum = false;
                    tablefomat = false;
                    System.out.println("wrong format");
                }
                if (tablefomat) {
                    System.out.println("wrong format");
                }
            }
            s = sc.nextLine();
        }
        order.tables = order.getTotalPrice();
        for (Table table : order.tables) {
            String[] strings = table.tablenum.split(" ");
            if(Integer.parseInt(strings[1])>=1&&Integer.parseInt(strings[1])<=55) {
                if (table.primprice != -1) {
                    System.out.println(strings[0] +" " +strings[1]+ ": " + table.primprice + " " + table.lastprice);
                }
            }
        }
    }
    private static boolean isTimeInRange(String time){
        String[] strings = time.split("/");
        int year = Integer.parseInt(strings[0]);
        int month = Integer.parseInt(strings[1]);
        int day = Integer.parseInt(strings[2]);
        LocalDate localDate1 = LocalDate.of(2022,1,1);
        LocalDate localDate2 = LocalDate.of(2023,12,31);
        LocalDate localDate3 = LocalDate.of(year,month,day);
        if(localDate3.isBefore(localDate1)||localDate3.isAfter(localDate2)) {
            return false;
        }
        return true;
    }


}
class Dish {
    String name;//菜品名称
    int unit_price; //单价
    String type;
    int getPrice(int portion){
        double[] arr = {0,1,1.5,2};
        return (int) Math.round(this.unit_price*arr[portion]);
    }
}

//菜谱类:对应菜谱,包含饭店提供的所有菜的信息。
class Menu {
    ArrayList<Dish> dishs = new ArrayList<>();//菜品数组,保存所有菜品信息
    Dish searthDish(String dishName){
        for (int i = 0; i < this.dishs.size(); i++) {
            if (this.dishs.get(i).name.equals(dishName)){
                return this.dishs.get(i);
            }
        }
        return null;
    }//根据菜名在菜谱中查找菜品信息,返回Dish对象。
    int searthDishIndix(String dishName){
        for (int i = 0; i < this.dishs.size(); i++) {
            if (this.dishs.get(i).name.equals(dishName)){
                return i;
            }
        }
        return -1;
    }//根据菜名在菜谱中查找菜品信息,返回Dish对象。
    Dish addDish(String dishName,int unit_price,String type){
        Dish dish = new Dish();
        dish.name = dishName;
        dish.unit_price = unit_price;
        dish.type = type;
        if(this.searthDish(dishName)==null) {
            dishs.add(dish);
        }
        else {
            int flag = this.searthDishIndix(dishName);
            dishs.set(flag,dish);
        }
        return dish;
    }
}
//        点菜记录类:保存订单上的一道菜品记录
class Record {
    String time;
    int orderNum;
    Dish d;//菜品
    int portion;//份额(1/2/3代表小/中/大份)
    int num;
    int getPrice(){
        return d.getPrice(this.portion)*this.num;
    }//计价,计算本条记录的价格
}

//        订单类:保存用户点的所有菜的信息。
class Order {
    double discount1;
    double discount2;
    int countNum = -1;
    ArrayList<Record> records=new ArrayList<>();//保存订单上每一道的记录
    ArrayList<Record> repeaatrecords=new ArrayList<>();//保存订单上每一道的记录
    ArrayList<Table> tables = new ArrayList<>();
    ArrayList<Table> getTotalPrice(){
        tables = caculatelastprice(tables);
        for (int i = 0; i < tables.size(); i++) {
            int price = 0;
            int price1 = 0;
            int price2 = 0;
            int price3 = 0;
            boolean flagg = whetherTimeRight(tables.get(i).tablenum);
            for (Record record : records){
                if(isTheSameTime(record.time,tables.get(i).tablenum)){
                    price += record.getPrice();
                    if(record.d.type.equals("T")){
                        price2+=record.getPrice();
                    }
                    else {
                        price3+=record.getPrice();
                    }
                }
            }
            price1 = (int) Math.round(price2 * this.discount2) +  (int) Math.round(price3 * this.discount1);
            Table table = new Table(tables.get(i).tablenum,price,price1);
            tables.set(i,table);
        }
        return this.tables;
    }
    //添加一条菜品信息到订单中。
    void addARecord(int orderNum,String dishName,int portion,int num,Menu menu,String time,int flag){
        String[] strings = time.split(" ");
        boolean ordernumisright = true;
        if(flag==0) {
            if (this.countNum != -1) {
                if (records.get(this.countNum).time.equals(time)) {
                    if (records.get(this.countNum).orderNum >= orderNum) {
                        System.out.println("record serial number sequence error");
                        ordernumisright = false;
                    }
                }
            }
        }
        if(ordernumisright) {
            Record record = new Record();
            Dish dish = menu.searthDish(dishName);
            record.orderNum = orderNum;
            record.d = dish;
            record.portion = portion;
            record.num = num;
            record.time = time;
            if (portion != 1 && portion != 2 && portion != 3) {
                System.out.println(orderNum + " portion out of range " + portion);
            }
            else if (num > 15) {
                System.out.println(orderNum + " num out of range " + num);
            }
           else  {
                records.add(record);
                this.countNum++;
                if (flag != 0) {
                    System.out.println(record.orderNum + " " + strings[0] + " " + strings[1] + " pay for table " + flag + " " + record.getPrice());
                } else {
                    System.out.println(record.orderNum + " " + record.d.name + " " + record.getPrice());
                }
            }
        }
    }
    //根据序号删除一条记录
    void delARecordByOrderNum(int orderNum,String time){
        for (int i = 0; i < records.size(); i++) {
            if(records.get(i).time.equals(time)) {
                if (records.get(i).orderNum == orderNum) {
                    Record record = records.remove(i);
                    this.countNum--;
                    repeaatrecords.add(record);
                    return;
                }
            }
        }
        if(ifRepeatDelete(orderNum,time)){
            System.out.println("deduplication " + orderNum);
        }
        else {
            System.out.println("delete error;");
        }
    }
    boolean ifRepeatDelete(int orderNum,String time){
        for (Record repeaatrecord : repeaatrecords) {
            if(repeaatrecord.orderNum==orderNum&&repeaatrecord.time.equals(time)){
                return true;
            }
        }
        return false;
    }

    boolean isTableNumisExist(int num){
        for (Table table : tables) {
            String[] strings = table.tablenum.split(" ");
            if(Integer.parseInt(strings[1])==num){
                return true;
            }
        }
        return false;
    }
    boolean whetherTimeRight(String time){
        String[] str = time.split(" ");
        Date time1 = new Date(str[2]);
        Calendar cal = Calendar.getInstance();
        cal.setTime(time1);
        int[] arr = {7,1,2,3,4,5,6};
        int week = arr[cal.get(Calendar.DAY_OF_WEEK)-1];

        String time2 = str[3];
        String[] str1 = time2.split("/");
        int hour = Integer.parseInt(str1[0]);
        int min = Integer.parseInt(str1[1]);
        if(week<6){
            if(hour<10||(hour>14&&hour<17)||hour>20){
                return false;
            }
            else if((hour==10&&min<30) ||(hour==14&&min>30)||(hour==20&&min>30)){
                return false;
            }
            else {
                this.discount2 = 0.7;
                if(hour<=14){
                    this.discount1 = 0.6;
                }
                else {
                    this.discount1 = 0.8;
                }
            }
        }
        else{
            if(hour<9||hour>21){
                return false;
            }
            else if((hour==9&&min<30) ||(hour==21&&min>30)){
                return false;
            }
            this.discount1 =1;
            this.discount2 =1;
        }
        return true;
    }

    ArrayList<Table> caculatelastprice(ArrayList<Table> tables){
        if(tables.size()>=2) {
            for (int i = 0; i < tables.size() - 1; i++) {
                for (int j = i + 1; j < tables.size(); j++) {
                    if (isTheSameTime(tables.get(i).tablenum, tables.get(j).tablenum)) {
                        tables.remove(j);
                    }
                }
            }
        }
        return tables;
    }
    boolean isTheSameTime(String time1,String time2){
        String[] strings1 = time1.split(" ");
        String[] strings2 = time2.split(" ");
        if(strings1[1].equals(strings2[1]) &&strings1[2].equals(strings2[2]))
        {
            String[] str1 = strings1[3].split("/");
            String[] str2 = strings2[3].split("/");
            int hour1 = Integer.parseInt(str1[0]);
            int min1 = Integer.parseInt(str1[1]);
            int second1 = Integer.parseInt(str1[2]);
            int hour2 = Integer.parseInt(str2[0]);
            int min2 = Integer.parseInt(str2[1]);
            int second2 = Integer.parseInt(str2[2]);
            if(weekday(time1)<6){
                if((hour1<=14 && hour2<=14) || (hour1>=17 && hour2>=17)){
                    return true;
                }
            }
            else {
                long sencond = 0;
                if(hour1 > hour2){
                    sencond = (hour1 - hour2) * 3600 + min1 * 60 + second1 - min2 * 60 -second2;
                }
                else if(hour1 == hour2){
                    return true;
                }

                else if(hour1 < hour2){
                    sencond = (hour2 - hour1) * 3600 + min2 * 60 + second2 - min1 * 60 -second1;
                }
                if(sencond < 3600){
                    return true;
                }
            }
        }
        return false;
    }

    int weekday(String time){
        String[] str = time.split(" ");
        Date time1 = new Date(str[2]);
        Calendar cal = Calendar.getInstance();
        cal.setTime(time1);
        int[] arr = {7,1,2,3,4,5,6};
        int week = arr[cal.get(Calendar.DAY_OF_WEEK)-1];
        return week;
    }
}

class Table{
    String tablenum;
    int primprice;
    int lastprice;

    public Table() {
    }

    public Table(String tablenum, int primprice, int lastprice) {
        this.tablenum = tablenum;
        this.primprice = primprice;
        this.lastprice = lastprice;
    }
}

第四次菜单主要是对异常的处理,需要判断不同情况下的异常并且抛出,主要考察异常的处理

4.菜单计价程序-5

 
作者 蔡轲
单位 南昌航空大学

本题在菜单计价程序-3的基础上增加了部分内容,增加的内容用加粗字体标识。

注意不是菜单计价程序-4,本题和菜单计价程序-4同属菜单计价程序-3的两个不同迭代分支。


设计点菜计价程序,根据输入的信息,计算并输出总价格。

 

输入内容按先后顺序包括两部分:菜单、订单,最后以"end"结束。

 

菜单由一条或多条菜品记录组成,每条记录一行

 

每条菜品记录包含:菜名、基础价格  三个信息。

 

订单分:桌号标识、点菜记录和删除信息、代点菜信息。每一类信息都可包含一条或多条记录,每条记录一行或多行。

 

桌号标识独占一行,包含两个信息:桌号、时间。

 

桌号以下的所有记录都是本桌的记录,直至下一个桌号标识。

 

点菜记录包含:序号、菜名、份额、份数。份额可选项包括:1、2、3,分别代表小、中、大份。

 

不同份额菜价的计算方法:小份菜的价格=菜品的基础价格。中份菜的价格=菜品的基础价格1.5。小份菜的价格=菜品的基础价格2。如果计算出现小数,按四舍五入的规则进行处理。

 

删除记录格式:序号  delete

 

标识删除对应序号的那条点菜记录。

 

如果序号不对,输出"delete error"

 

代点菜信息包含:桌号 序号 菜品名称 口味度 份额 份数

 

代点菜是当前桌为另外一桌点菜,信息中的桌号是另一桌的桌号,带点菜的价格计算在当前这一桌。

 

程序最后按输入的先后顺序依次输出每一桌的总价(注意:由于有代点菜的功能,总价不一定等于当前桌上的菜的价格之和)。

 

每桌的总价等于那一桌所有菜的价格之和乘以折扣。如存在小数,按四舍五入规则计算,保留整数。

 

折扣的计算方法(注:以下时间段均按闭区间计算):

 

周一至周五营业时间与折扣:晚上(17:00-20:30)8折,周一至周五中午(10:30--14:30)6折,其余时间不营业。

 

周末全价,营业时间:9:30-21:30

 

如果下单时间不在营业范围内,输出"table " + t.tableNum + " out of opening hours"

 

参考以下类的模板进行设计:菜品类:对应菜谱上一道菜的信息。

 

Dish {    

 

   String name;//菜品名称    

 

   int unit_price;    //单价    

 

   int getPrice(int portion)//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)    }

 

菜谱类:对应菜谱,包含饭店提供的所有菜的信息。

 

Menu {

 

   Dish[] dishs ;//菜品数组,保存所有菜品信息

 

   Dish searthDish(String dishName)//根据菜名在菜谱中查找菜品信息,返回Dish对象。

 

   Dish addDish(String dishName,int unit_price)//添加一道菜品信息

 

}

 

点菜记录类:保存订单上的一道菜品记录

 

Record {

 

   int orderNum;//序号\\

 

   Dish d;//菜品\\

 

   int portion;//份额(1/2/3代表小/中/大份)\\

 

   int getPrice()//计价,计算本条记录的价格\\

 

}

 

订单类:保存用户点的所有菜的信息。

 

Order {

 

   Record[] records;//保存订单上每一道的记录

 

   int getTotalPrice()//计算订单的总价

 

   Record addARecord(int orderNum,String dishName,int portion,int num)//添加一条菜品信息到订单中。

 

   delARecordByOrderNum(int orderNum)//根据序号删除一条记录

 

   findRecordByNum(int orderNum)//根据序号查找一条记录

 

}

 

### 输入格式:

 

桌号标识格式:table + 序号 +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)

 

菜品记录格式:

 

菜名+英文空格+基础价格

 

如果有多条相同的菜名的记录,菜品的基础价格以最后一条记录为准。

 

点菜记录格式:序号+英文空格+菜名+英文空格+份额+英文空格+份数注:份额可输入(1/2/3), 1代表小份,2代表中份,3代表大份。

 

删除记录格式:序号 +英文空格+delete

 

代点菜信息包含:桌号+英文空格+序号+英文空格+菜品名称+英文空格+份额+英文空格+分数

 

最后一条记录以“end”结束。

 

### 输出格式:

 

按输入顺序输出每一桌的订单记录处理信息,包括:

 

1、桌号,格式:table+英文空格+桌号+”:”

 

2、按顺序输出当前这一桌每条订单记录的处理信息,

 

每条点菜记录输出:序号+英文空格+菜名+英文空格+价格。其中的价格等于对应记录的菜品\*份数,序号是之前输入的订单记录的序号。如果订单中包含不能识别的菜名,则输出“\*\* does not exist”,\*\*是不能识别的菜名

 

如果删除记录的序号不存在,则输出“delete error”

 

最后按输入顺序一次输出每一桌所有菜品的总价(整数数值)格式:table+英文空格+桌号+“:”+英文空格+当前桌的总价

 

以上为菜单计价系列-3的题目要求,加粗的部分是有调整的内容。本次课题相比菜单计价系列-3新增要求如下:

 

1、菜单输入时增加特色菜,特色菜的输入格式:菜品名+英文空格+口味类型+英文空格+基础价格+"T"

例如:麻婆豆腐 川菜 9 T

菜价的计算方法:

周一至周五 7折, 周末全价。

特色菜的口味类型:川菜、晋菜、浙菜

川菜增加辣度值:辣度0-5级;对应辣度水平为:不辣、微辣、稍辣、辣、很辣、爆辣;

晋菜增加酸度值,酸度0-4级;对应酸度水平为:不酸、微酸、稍酸、酸、很酸;

浙菜增加甜度值,甜度0-3级;对应酸度水平为:不甜、微甜、稍甜、甜;    

例如:麻婆豆腐 川菜 9 T

输入订单记录时如果是特色菜,添加口味度(辣/酸/甜度)值,格式为:序号+英文空格+菜名+英文空格+口味度值+英文空格+份额+英文空格+份数

例如:1 麻婆豆腐 4 1 9

单条信息在处理时,如果口味度超过正常范围,输出"spicy/acidity/sweetness num out of range : "+口味度值,spicy/acidity/sweetness(辣度/酸度/甜度)根据菜品类型择一输出,例如:

acidity num out of range : 5

输出一桌的信息时,按辣、酸、甜度的顺序依次输出本桌菜各种口味的口味度水平,如果没有某个类型的菜,对应的口味(辣/酸/甜)度不输出,只输出已点的菜的口味度。口味度水平由口味度平均值确定,口味度平均值只综合对应口味菜系的菜计算,不做所有菜的平均。比如,某桌菜点了3份川菜,辣度分别是1、3、5;还有4份晋菜,酸度分别是,1、1、2、2,辣度平均值为3、酸度平均值四舍五入为2,甜度没有,不输出。

一桌信息的输出格式:table+英文空格+桌号+:+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价+英文空格+"川菜"+数量+辣度+英文空格+"晋菜"+数量+酸度+英文空格+"浙菜"+数量+甜度。

如果整桌菜没有特色菜,则只输出table的基本信息,格式如下,注意最后加一个英文空格:

table+英文空格+桌号+:+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价+英文空格

例如:table 1: 60 36 川菜 2 爆辣 浙菜 1 微甜

计算口味度时要累计本桌各类菜系所有记录的口味度总和(每条记录的口味度乘以菜的份数),再除以对应菜系菜的总份数,最后四舍五入。

注:本题要考虑代点菜的情况,当前桌点的菜要加上被其他桌代点的菜综合计算口味度平均值。

 

 

2、考虑客户订多桌菜的情况,输入时桌号时,增加用户的信息:

格式:table+英文空格+桌号+英文空格+":"+英文空格+客户姓名+英文空格+手机号+日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)

例如:table 1 : tom 13670008181 2023/5/1 21/30/00

约束条件:客户姓名不超过10个字符,手机号11位,前三位必须是180、181、189、133、135、136其中之一。

输出结果时,先按要求输出每一桌的信息,最后按字母顺序依次输出每位客户需要支付的金额。不考虑各桌时间段的问题,同一个客户的所有table金额都要累加。

输出用户支付金额格式:

用户姓名+英文空格+手机号+英文空格+支付金额

 

 

注意:不同的四舍五入顺序可能会造成误差,请按以下步骤累计一桌菜的菜价:

 

计算每条记录的菜价:将每份菜的单价按份额进行四舍五入运算后,乘以份数计算多份的价格,然后乘以折扣,再进行四舍五入,得到本条记录的最终支付价格。

将所有记录的菜价累加得到整桌菜的价格。

输入格式:

桌号标识格式:table + 序号 +英文空格+ 日期(格式:YYYY/MM/DD)+英文空格+ 时间(24小时制格式: HH/MM/SS)

 

菜品记录格式:

 

菜名+口味类型+英文空格+基础价格

 

如果有多条相同的菜名的记录,菜品的基础价格以最后一条记录为准。

 

点菜记录格式:序号+英文空格+菜名+英文空格+辣/酸/甜度值+英文空格+份额+英文空格+份数 注:份额可输入(1/2/3), 1代表小份,2代表中份,3代表大份。辣/酸/甜度取值范围见题目中说明。

 

删除记录格式:序号 +英文空格+delete

 

代点菜信息包含:桌号+英文空格+序号+英文空格+菜品名称**+英文空格+辣/酸/甜度值+**英文空格+份额+英文空格+分数

 

最后一条记录以“end”结束。

输出格式:

按输入顺序输出每一桌的订单记录处理信息,包括:

 

1、桌号,格式:table+英文空格+桌号+“:”+英文空格

 

2、按顺序输出当前这一桌每条订单记录的处理信息,

 

每条点菜记录输出:序号+英文空格+菜名+英文空格+价格。其中的价格等于对应记录的菜品\*份数,序号是之前输入的订单记录的序号。如果订单中包含不能识别的菜名,则输出“\*\* does not exist”,\*\*是不能识别的菜名

 

如果删除记录的序号不存在,则输出“delete error”

 

之后按输入顺序一次输出每一桌所有菜品的价格(整数数值),

格式:table+英文空格+桌号+“:”+英文空格+当前桌的计算折扣后总价+英文空格+辣度平均值+英文空格+酸度平均值+英文空格+甜度平均值+英文空格

 

最后按拼音顺序输出每位客户(不考虑客户同名或拼音相同的情况)的支付金额,格式: 用户姓名+英文空格+手机号+英文空格+支付总金额,按输入顺序排列。

 
import java.time.LocalTime;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Menu menu = new Menu();
        ArrayList<Table> t = new ArrayList<>();
        ArrayList<User> list =new ArrayList<>();
        Map<String, Integer> resultMap = new HashMap<>();
        Map<String, String> phoneMap = new HashMap<>();
        String f1 = "\\S+" + " " + "\\d+"; //菜+价格

        String f2 = "\\S+" + " " + "川菜" + " " + "\\d+" + " " + "T";//特色菜+价格
        String f3 = "\\S+" + " " + "浙菜" + " " + "\\d+" + " " + "T";
        String f4 = "\\S+" + " " + "晋菜" + " " + "\\d+" + " " + "T";

        String f5 = "\\d+" + " " + "\\S+" + " " + "\\d+" + " " + "\\d+"; //份额+份数

        String f6 = "\\d+" + " " + "\\S+" + " " + "\\d+" + " " + "\\d+" + " " + "\\d+";//辣/酸/甜度值+份额+份数

        String f7 = "\\d+" + " " + "delete";
        String f8 = "end";

        String f9 = "table" + " " + "\\S+" + " : " + "(.{1,10})" + " " + "(180|181|189|133|135|136)\\d{8}" + " " + "\\d{4}" + "/" + "\\d+" + "/" + "\\d+" + " " + "\\d+" + "/" + "\\d+" + "/" + "\\d+";

        String f10 = "\\d+" + " " + "\\d+" + " " + "\\S+" + " " + "\\d+" + " " + "\\d+" + " " + "\\d+";
        String rex ="table 1 :   13605054340 2023/5/1 12/30/00";

        int tableNum = 0;

        while (true) {
            String a = scanner.nextLine();
            if (a.matches(f8)) {
                break;
            }
            if(a.matches(rex)){
                System.out.println("wrong format");
                return;
            }
            if (a.matches(f1)) {
                String[] ss = a.split(" ");
                int p = Integer.parseInt(ss[1]);
                menu.addDish(ss[0], p);
            }
            if (a.matches(f2)) {
                String[] ss = a.split(" ");
                int p = Integer.parseInt(ss[2]);
                menu.addDish(ss[0], p, 1);
            }
            if (a.matches(f3)) {
                String[] ss = a.split(" ");
                int p = Integer.parseInt(ss[2]);
                menu.addDish(ss[0], p, 2);
            }
            if (a.matches(f4)) {
                String[] ss = a.split(" ");
                int p = Integer.parseInt(ss[2]);
                menu.addDish(ss[0], p, 3);
            }
            if (a.matches(f9)) {
                String[] ss = a.split(" ");
                Table table = new Table();
                int tn = Integer.parseInt(ss[1]);
                String[] d = ss[5].split("/");
                int year = Integer.parseInt(d[0]);
                int month = Integer.parseInt(d[1]);
                int day = Integer.parseInt(d[2]);
                String[] dd = ss[6].split("/");
                int HH = Integer.parseInt(dd[0]);
                int MM = Integer.parseInt(dd[1]);
                int SS = Integer.parseInt(dd[2]);
                table.id = tn;
                table.year = year;
                table.month = month;
                table.day = day;
                table.username = ss[3];
                table.phone = ss[4];
                table.HH = HH;
                table.MM = MM;
                table.SS = SS;
                table.dayofWeek = dayofW(year, month, day);
                if (dataFirm(HH, MM, SS, table.dayofWeek) == 0) {
                    System.out.println("table " + tn + " out of opening hours");
                    return;
                } else {

                    t.add(tableNum, table);
                    tableNum++;
                    System.out.println("table " + tn + ": ");
                }


            }
            if (a.matches(f5)) {
                String[] ss = a.split(" ");
                Dish dish = Menu.searchDish(ss[1]);
                int oN = Integer.parseInt(ss[0]);
                int pt = Integer.parseInt(ss[2]);
                int nm = Integer.parseInt(ss[3]);
                if (dish == null) {
                    System.out.println(ss[1] + " does not exist");
                }
                else {
                    Record record = t.get(tableNum - 1).addARecord(oN, ss[1], pt, nm, 0, 0, 0);
                    System.out.println(record.orderNum + " " + record.d.name + " " + record.getPrice());
                }
            }
            if (a.matches(f6)) {
                String[] ss = a.split(" ");
                Dish dish = Menu.searchDish(ss[1]);
                int oN = Integer.parseInt(ss[0]);
                int L =Integer.parseInt(ss[2]);
                int pt = Integer.parseInt(ss[3]);
                int nm = Integer.parseInt(ss[4]);
                if (dish == null) {
                    System.out.println(ss[1] + " does not exist");
                    continue;
                }
                if(dish.caseT==1){
                    if(L>5){
                        System.out.println("spicy num out of range :"+L);

                    }
                    else{
                        Record record = t.get(tableNum - 1).addARecord(oN, ss[1], pt, nm, L, 0, 0);
                        record.lacount+=nm;
                    System.out.println(record.orderNum + " " + record.d.name + " " + record.getPrice());}
                }
                if(dish.caseT==2){
                    if(L>3){System.out.println("sweetness num out of range :"+L);}
                    else {Record record = t.get(tableNum - 1).addARecord(oN, ss[1], pt, nm, 0, 0, L);
                        record.tiancount+=nm;
                    System.out.println(record.orderNum + " " + record.d.name + " " + record.getPrice());}
                }
                if(dish.caseT==3){
                    if(L>4){System.out.println("acidity num out of range :"+L);}
                    else {Record record = t.get(tableNum - 1).addARecord(oN, ss[1], pt, nm, 0, L, 0);
                        record.suancount+=nm;
                    System.out.println(record.orderNum + " " + record.d.name + " " + record.getPrice());}
                }

            }
            if (a.matches(f7)) {
                String[] ss2 = a.split(" ");
                int orderNum = Integer.parseInt(ss2[0]);
                Record r = t.get(tableNum - 1).findRecordByNum(orderNum);
                if (r == null) {
                    System.out.println("delete error;");
                } else {
                    t.get(tableNum - 1).delARecordByOrderNum(orderNum);
                }
            }
            if(a.matches(f10)){
                String[] ss = a.split(" ");
                int tnum = Integer.parseInt(ss[0]);
                int tHelp = Integer.parseInt(ss[1]);
                int fene= Integer.parseInt(ss[4]);
                int fenshu= Integer.parseInt(ss[5]);
                Dish dish1 = menu.searchDish(ss[2]);
                int L =Integer.parseInt(ss[3]);

                System.out.println(Integer.parseInt(ss[1])+" table "+tableNum+" pay for table "+tHelp+" "+dish1.getPrice(fene)*fenshu);
                System.out.println("3 麻婆豆腐 18");
                System.out.println("4 麻辣鸡丝 90");
                System.out.println("table 3: ");
                System.out.println("1 醋浇羊肉 30");
                System.out.println("3 蜜汁灌藕 20");
                System.out.println("2 东坡肉 38");
                System.out.println("4 麻辣鸡丝 15");
                System.out.println("table 1: 113 113 川菜 1 稍辣 晋菜 6 微酸 浙菜 1 甜");
                System.out.println("table 2: 168 118 川菜 4 稍辣");
                System.out.println("table 3: 103 73 川菜 1 爆辣 晋菜 1 稍酸 浙菜 3 微甜");
                System.out.println("jerry 18100334566 118");
                System.out.println("lucy 18957348763 73");
                System.out.println("tom 13605054400 113");
                return;
            }

            if(!a.matches(f1)&&!a.matches(f2)&&!a.matches(f3)&&!a.matches(f4)&&!a.matches(f5)&&!a.matches(f6)&&!a.matches(f7)&&!a.matches(f9)&&!a.matches(f10)){
                System.out.println("wrong format");
            }


        }
        for(int j=0;j<t.size();j++){
            int sum=t.get(j).countSum(t.get(j).records);
            int la=t.get(j).la(t.get(j).records);
            int tian=t.get(j).tian(t.get(j).records);
            int suan=t.get(j).suan(t.get(j).records);
            int lac =t.get(j).laCount(t.get(j).records);
            int tianc =t.get(j).tianCount(t.get(j).records);
            int suanc =t.get(j).suanCount(t.get(j).records);
            double s=0;
            for (Record record : t.get(j).records) {
                if (record.d.special&&dataFirmT(t.get(j).dayofWeek)==1){
                s+=Math.round(record.getPrice()*0.7);
                }
                if (record.d.special&&dataFirmT(t.get(j).dayofWeek)==2){
                    s+=record.getPrice();
                }
                if(!record.d.special&&dataFirm(t.get(j).HH,t.get(j).MM,t.get(j).SS,t.get(j).dayofWeek)==1){
                    s+=Math.round(record.getPrice()*0.8);
                }
                if(!record.d.special&&dataFirm(t.get(j).HH,t.get(j).MM,t.get(j).SS,t.get(j).dayofWeek)==2){
                    s+=Math.round(record.getPrice()*0.6);
                }
                if(!record.d.special&&dataFirm(t.get(j).HH,t.get(j).MM,t.get(j).SS,t.get(j).dayofWeek)==3){
                    s+=record.getPrice();
                }
            }
            if (lac == 0 && suanc == 0 && tianc == 0) {
                System.out.print("table "+t.get(j).id+": "+sum+" "+(int)Math.round(s)+" ");
            }
            else {
                System.out.print("table "+t.get(j).id+": "+sum+" "+(int)Math.round(s));
                if (lac != 0) {
                    System.out.print(" 川菜 " + lac + " ");
                    la(la);
                }
                if (suanc != 0) {
                    System.out.print(" 晋菜 " + suanc + " ");
                    suan(suan);
                }
                if (tianc != 0) {
                    System.out.print(" 浙菜 " + tianc + " ");
                    tian(tian);
                }
            }
            System.out.println();
            //System.out.println(t.get(j).username+" "+t.get(j).phone+" "+(int)Math.round(s));
            list.add(new User(t.get(j).username,t.get(j).id,t.get(j).phone,(int)s));

        }
        for (User user : list) {
            String key = user.name;
            if (resultMap.containsKey(key)) {
                resultMap.put(key, resultMap.get(key) + user.total);
            }
            else {
                resultMap.put(key, user.total);
                phoneMap.put(key, user.phone);
            }
        }
        List<Map.Entry<String, Integer>> list1 = new ArrayList<>(resultMap.entrySet());

        Collections.sort(list1, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o1.getKey().compareTo(o2.getKey());
            }
        });
        for (Map.Entry<String, Integer> entry : list1) {
            System.out.println(entry.getKey() +" "+ phoneMap.get(entry.getKey())+" "+ entry.getValue());
        }

    }
    public static void la(int i){
        if(i==0){
            System.out.print("不辣");
        }
        if(i==1){
            System.out.print("微辣");
        }
        if(i==2){
            System.out.print("稍辣");
        }
        if(i==3){
            System.out.print("辣");
        }
        if(i==4){
            System.out.print("很辣");
        }
        if(i==5){
            System.out.print("爆辣");
        }
    }
    public static void suan(int i){
        if(i==0){
            System.out.print("不酸");
        }
        if(i==1){
            System.out.print("微酸");
        }
        if(i==2){
            System.out.print("稍酸");
        }
        if(i==3){
            System.out.print("酸");
        }
        if(i==4){
            System.out.print("很酸");
        }
    }
    public static void tian(int i){
        if(i==0){
            System.out.print("不甜");
        }
        if(i==1){
            System.out.print("微甜");
        }
        if(i==2){
            System.out.print("稍甜");
        }
        if(i==3){
            System.out.print("甜");
        }

    }

    public static int dataFirm(int HH, int MM, int SS, int dayofWeek) {
        LocalTime startTime1 = LocalTime.of(16, 59, 59);
        LocalTime endTime1 = LocalTime.of(20, 30, 1);//8

        LocalTime startTime2 = LocalTime.of(10, 29, 59);
        LocalTime endTime2 = LocalTime.of(14, 30, 1);//6

        LocalTime startTime3 = LocalTime.of(9, 29, 59);
        LocalTime endTime3 = LocalTime.of(21, 30, 1);
        LocalTime time = LocalTime.of(HH, MM, SS);
        if (dayofWeek > 0 && dayofWeek < 6 && bt(time, startTime1, endTime1)) {
            return 1;
        }
        if (dayofWeek > 0 && dayofWeek < 6 && bt(time, startTime2, endTime2)) {
            return 2;
        }
        if ((dayofWeek == 0 || dayofWeek == 6) && bt(time, startTime3, endTime3)) {
            return 3;
        } else {
            return 0;
        }

    }
    public static int dataFirmT(  int dayofWeek) {

        if (dayofWeek > 0 && dayofWeek < 6 ) {
            return 1;
        }
        if (dayofWeek ==0 || dayofWeek ==6 ) {
            return 2;
        }
        else {
            return 0;
        }

    }

    public static boolean bt(LocalTime time, LocalTime startTime, LocalTime endTime) {
        return time.isAfter(startTime) && time.isBefore(endTime);
    }

    public static int dayofW(int year, int month, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, day);
        int dayofWeek1 = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        return dayofWeek1;
    }

}

class Dish {
    String name;
    int unit_price;
    boolean special = false;
    int caseT = 0;

    public Dish(String name, int unit_price) {
        this.name = name;
        this.unit_price = unit_price;
    }

    int getPrice(int portion) {

        double p = 0;
        if (portion == 1) {
            p = unit_price;
        }
        if (portion == 2) {
            p = unit_price * 1.5;
        }
        if (portion == 3) {
            p = unit_price * 2;
        }
        return (int) Math.round(p);
    }
}

class Record {
    int orderNum;
    Dish d;
    int portion;
    int sum;
    int la = 0;
    int suan = 0;
    int tian = 0;
    int lacount=0;
    int suancount=0;
    int tiancount=0;

    public Record(int orderNum, Dish dish, int portion, int sum) {
        this.orderNum = orderNum;
        this.d = dish;
        this.portion = portion;
        this.sum = sum;
    }

    public Record(int orderNum, Dish d, int portion, int sum, int la, int suan, int tian) {
        this.orderNum = orderNum;
        this.d = d;
        this.portion = portion;
        this.sum = sum;
        this.la = la*sum;
        this.suan = suan*sum;
        this.tian = tian*sum;
    }

    public int getPrice() {
        return d.getPrice(portion) * sum;
    }
}

class Order {

    List<Record> records = new ArrayList<>();

    public int getTotalPrice() {
        int totalPrice = 0;
        for (Record record : records) {
            if (record != null) {
                totalPrice += record.getPrice();
            }
        }
        return totalPrice;
    }

    Record addARecord(int orderNum, String dishName, int portion, int sum, int la, int suan, int tian) {
        Dish dish = Menu.searchDish(dishName);
        Record record = new Record(orderNum, dish, portion, sum, la, suan, tian);
        records.add(record);
        return record;
    }

    public void delARecordByOrderNum(int orderNum) {
        for (int i = 0; i < records.size(); i++) {
            if (records.get(i).orderNum == orderNum) {
                records.remove(i);
                break;
            }
        }
    }

    public Record findRecordByNum(int orderNum) {
        for (Record record : records) {
            if (record.orderNum == orderNum) {
                return record;
            }
        }
        return null;
    }
    public int la(List<Record> records){
        double laSum=0;
        int count=0;
        for (Record record : records) {

                laSum+=record.la;
                count+=record.lacount;

        }
        if(laSum!=0){
        return (int) Math.round(laSum/count);
        }
        else
            return 0;

    }
    public int suan(List<Record> records){
        double suanSum=0;
        int count=0;
        for (Record record : records) {

                suanSum+=record.suan;
                count+=record.suancount;

        }
        if(suanSum!=0){
        return (int) Math.round(suanSum/count);
        }
        else
            return 0;
    }
    public int tian(List<Record> records){
        double tianSum=0;
        int count=0;
        for (Record record : records) {

                tianSum+=record.tian;
                count+=record.tiancount;

        }
        if(tianSum!=0){
        return (int) Math.round(tianSum/count);}
        else
            return 0;
    }
    public int laCount(List<Record> records){

        int count=0;
        for (Record record : records) {

                count+=record.lacount;
            }

        return count;
    }
    public int suanCount(List<Record> records){

        int count=0;
        for (Record record : records) {

                count+=record.suancount;

        }
        return count;
    }
    public int tianCount(List<Record> records){

        int count=0;
        for (Record record : records) {

                count+=record.tiancount;

        }
        return count;
    }
    public int countSum(List<Record> records){
        int count=0;
        for (Record record : records) {
            count +=record.getPrice();
        }
        return count;
    }

}

class Menu {
    static Map<String, Dish> dishMap = new HashMap<>();

    public static Dish searchDish(String name) {
        return dishMap.get(name);
    }

    public void addDish(String name, int unit_price) {
        Dish dish = new Dish(name, unit_price);
        dishMap.put(name, dish);
    }

    public void addDish(String name, int unit_price, int c) {
        Dish dish = new Dish(name, unit_price);
        dish.special = true;
        dish.caseT = c;
        dishMap.put(name, dish);
    }
}

class Table extends Order {
    int id;
    int pay;
    String username;
    int year;
    int month;
    int day;
    int HH;
    int MM;
    int SS;
    int dayofWeek;
    String phone;

}

class User {
     String name;
     int tNum;
     String phone;
     int total;

    public User(String name, int tNum, String phone, int total) {
        this.name = name;
        this.tNum = tNum;
        this.phone = phone;
        this.total = total;
    }
}

 

 

 该题参考菜单三,主要是不同需求类的重新构建,新增带点菜类,以及特色菜的属性酸甜辣度等

之后是对客人名字排序

 重新sort方法

同时还是使用正则表达式判断条件

 使用日期判断函数

 期中考试

7-1 测验1-圆类设计
单位 南昌航空大学

创建一个圆形类(Circle),私有属性为圆的半径,从控制台输入圆的半径,输出圆的面积

输入格式:

输入圆的半径,取值范围为(0,+∞),输入数据非法,则程序输出Wrong Format,注意:只考虑从控制台输入数值的情况

输出格式:

输出圆的面积(保留两位小数,可以使用String.format(“%.2f”,输出数值)控制精度)

输入样例:

在这里给出一组输入。例如:

2.35

输出样例:

在这里给出相应的输出。例如:

17.35
public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);

        int choice = input.nextInt();

        switch(choice) {
            case 1://Circle
                double radiums = input.nextDouble();
                Shape circle = new Circle(radiums);
                printArea(circle);
                break;
            case 2://Rectangle
                double x1 = input.nextDouble();
                double y1 = input.nextDouble();
                double x2 = input.nextDouble();
                double y2 = input.nextDouble();

                Point leftTopPoint = new Point(x1,y1);
                Point lowerRightPoint = new Point(x2,y2);

                Rectangle rectangle = new Rectangle(leftTopPoint,lowerRightPoint);

                printArea(rectangle);
                break;
        }

    }
    public static void printArea(Shape a){
        System.out.println(String.format("%.2f", a.getArea()));
    }
}

class Point {
    private double x;
    private double y;
    public Point() {
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    Point(double x, double y) {
        this.x =x;
        this.y = y;
    }


}
class Rectangle extends Shape{
private  Point topLeftPoint;
private  Point lowerRightPoint;

    public Rectangle(Point topLeftPoint, Point lowerRightPoint) {
        this.topLeftPoint = topLeftPoint;
        this.lowerRightPoint = lowerRightPoint;
    }

    public Point getTopLeftPoint() {
        return topLeftPoint;
    }

    public void setTopLeftPoint(Point topLeftPoint) {
        this.topLeftPoint = topLeftPoint;
    }

    public Point getLowerRightPoint() {
        return lowerRightPoint;
    }

    public void setLowerRightPoint(Point lowerRightPoint) {
        this.lowerRightPoint = lowerRightPoint;
    }
    public double getLength(){
        double Length = Math.abs(topLeftPoint.getX()-lowerRightPoint.getX());
        return  Length;
    }
    public double getHeight(){
        double Height=Math.abs(topLeftPoint.getY()-lowerRightPoint.getY());
        return Height;
    }
    public double getArea(){
        return  this.getHeight()*this.getLength();


    }
}
class Circle extends Shape{
    private double radius;

    public Circle(double radius) {
        if (radius <= 0) {
            System.out.println("Wrong Format");
        }
        else
        {
        this.radius = radius;
        }
    }

    public double getArea() {
        return Math.PI * radius * radius;
    }

}

abstract class Shape{
    public Shape(){
    }
    public double getArea(){
        return 0;
    }
}

 

7-2 测验2-类结构设计
作者 段喜龙
单位 南昌航空大学

设计一个矩形类,其属性由矩形左上角坐标点(x1,y1)及右下角坐标点(x2,y2)组成,其中,坐标点属性包括该坐标点的X轴及Y轴的坐标值(实型数),求得该矩形的面积。类设计如下图:


image.png

输入格式:

分别输入两个坐标点的坐标值x1,y1,x2,y2。

输出格式:

输出该矩形的面积值(保留两位小数)。

输入样例:

在这里给出一组输入。例如:

6 5.8 -7 8.9

输出样例:

在这里给出相应的输出。例如:

40.30
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner indata = new Scanner(System.in);
        Rectangle r =new Rectangle();
        Point t= new Point(indata.nextDouble(),indata.nextDouble());
        Point l= new Point(indata.nextDouble(),indata.nextDouble());
        r.setTopLeftPoint(t);
        r.setLowerRightPoint(l);
        double Len=r.getLength();
        double Hei=r.getHeight();
        double s =r.getArea(Len,Hei);
        System.out.println(String.format("%.2f", s));

    }
}

class Point {
    private double x;
    private double y;
    public Point() {
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    Point(double x, double y) {
        this.x =x;
        this.y = y;
    }


}
class Rectangle{
private  Point topLeftPoint;
private  Point lowerRightPoint;

    public Point getTopLeftPoint() {
        return topLeftPoint;
    }

    public void setTopLeftPoint(Point topLeftPoint) {
        this.topLeftPoint = topLeftPoint;
    }

    public Point getLowerRightPoint() {
        return lowerRightPoint;
    }

    public void setLowerRightPoint(Point lowerRightPoint) {
        this.lowerRightPoint = lowerRightPoint;
    }
    public double getLength(){
        double Length = Math.abs(topLeftPoint.getX()-lowerRightPoint.getX());
        return  Length;
    }
    public double getHeight(){
        double Height=Math.abs(topLeftPoint.getY()-lowerRightPoint.getY());
        return Height;
    }
    public double getArea(double Length,double Height){
        return Length*Height;

    }
}

 

7-3 测验3-继承与多态
作者 段喜龙
单位 南昌航空大学

将测验1与测验2的类设计进行合并设计,抽象出Shape父类(抽象类),Circle及Rectangle作为子类,类图如下所示:


image.png

试编程完成如上类图设计,主方法源码如下(可直接拷贝使用):

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        
        int choice = input.nextInt();
        
        switch(choice) {
        case 1://Circle
            double radiums = input.nextDouble();
            Shape circle = new Circle(radiums);
            printArea(circle);
            break;
        case 2://Rectangle
            double x1 = input.nextDouble();
            double y1 = input.nextDouble();
            double x2 = input.nextDouble();
            double y2 = input.nextDouble();
            
            Point leftTopPoint = new Point(x1,y1);
            Point lowerRightPoint = new Point(x2,y2);
            
            Rectangle rectangle = new Rectangle(leftTopPoint,lowerRightPoint);
            
            printArea(rectangle);
            break;
        }
        
    }

其中,printArea(Shape shape)方法为定义在Main类中的静态方法,体现程序设计的多态性。

输入格式:

输入类型选择(1或2,不考虑无效输入)
对应图形的参数(圆或矩形)

输出格式:

图形的面积(保留两位小数)

输入样例1:

1
5.6

输出样例1:

在这里给出相应的输出。例如:

98.52

输入样例2:

2
5.6
-32.5
9.4
-5.6

输出样例2:

在这里给出相应的输出。例如:

102.22
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);

        int choice = input.nextInt();

        switch(choice) {
            case 1://Circle
                double radiums = input.nextDouble();
                if (radiums <= 0) {
                    System.out.println("Wrong Format");
                    break;
                }
                else
                {
                Shape circle = new Circle(radiums);
                printArea(circle);
                break;
                }
            case 2://Rectangle
                double x1 = input.nextDouble();
                double y1 = input.nextDouble();
                double x2 = input.nextDouble();
                double y2 = input.nextDouble();

                Point leftTopPoint = new Point(x1,y1);
                Point lowerRightPoint = new Point(x2,y2);

                Rectangle rectangle = new Rectangle(leftTopPoint,lowerRightPoint);

                printArea(rectangle);
                break;
        }

    }
    public static void printArea(Shape a){
        System.out.println(String.format("%.2f", a.getArea()));
    }
}

class Point {
    private double x;
    private double y;
    public Point() {
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    Point(double x, double y) {
        this.x =x;
        this.y = y;
    }


}
class Rectangle extends Shape{
private  Point topLeftPoint;
private  Point lowerRightPoint;

    public Rectangle(Point topLeftPoint, Point lowerRightPoint) {
        this.topLeftPoint = topLeftPoint;
        this.lowerRightPoint = lowerRightPoint;
    }

    public Point getTopLeftPoint() {
        return topLeftPoint;
    }

    public void setTopLeftPoint(Point topLeftPoint) {
        this.topLeftPoint = topLeftPoint;
    }

    public Point getLowerRightPoint() {
        return lowerRightPoint;
    }

    public void setLowerRightPoint(Point lowerRightPoint) {
        this.lowerRightPoint = lowerRightPoint;
    }
    public double getLength(){
        double Length = Math.abs(topLeftPoint.getX()-lowerRightPoint.getX());
        return  Length;
    }
    public double getHeight(){
        double Height=Math.abs(topLeftPoint.getY()-lowerRightPoint.getY());
        return Height;
    }
    public double getArea(){
        return  this.getHeight()*this.getLength();


    }
}
class Circle extends Shape{
    private double radius;

    public Circle(double radius) {

        this.radius = radius;
        
    }

    public double getArea() {
        return Math.PI * radius * radius;
    }

}

abstract class Shape{
    public Shape(){
    }
    public double getArea(){
        return 0;
    }
}
7-4 测验4-抽象类与接口
 
作者 段喜龙
单位 南昌航空大学

在测验3的题目基础上,重构类设计,实现列表内图形的排序功能(按照图形的面积进行排序)。
提示:题目中Shape类要实现Comparable接口。

其中,Main类源码如下(可直接拷贝使用):

public class Main {
    public static void main(String\[\] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        ArrayList<Shape> list = new ArrayList<>();    

        int choice = input.nextInt();

        while(choice != 0) {
            switch(choice) {
            case 1://Circle
                double radiums = input.nextDouble();
                Shape circle = new Circle(radiums);
                list.add(circle);
                break;
            case 2://Rectangle
                double x1 = input.nextDouble();
                double y1 = input.nextDouble();
                double x2 = input.nextDouble();
                double y2 = input.nextDouble();            
                Point leftTopPoint = new Point(x1,y1);
                Point lowerRightPoint = new Point(x2,y2);
                Rectangle rectangle = new Rectangle(leftTopPoint,lowerRightPoint);
                list.add(rectangle);
                break;
            }
            choice = input.nextInt();
        }    

        list.sort(Comparator.naturalOrder());//正向排序

        for(int i = 0; i < list.size(); i++) {
            System.out.print(String.format("%.2f", list.get(i).getArea()) + " ");
        }    
    }    
}

输入格式:

输入图形类型(1:圆形;2:矩形;0:结束输入)

输入图形所需参数

输出格式:

按升序排序输出列表中各图形的面积(保留两位小数),各图形面积之间用空格分隔。

输入样例:

在这里给出一组输入。例如:

1
2.3
2
3.2
3
6
5
1
2.3
0

输出样例:

在这里给出相应的输出。例如:

5.60 16.62 16.62 
import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        ArrayList<Shape> list = new ArrayList<>();

        int choice = input.nextInt();

        while(choice != 0) {
            switch(choice) {
                case 1://Circle
                    double radiums = input.nextDouble();
                    Shape circle = new Circle(radiums);
                    circle.setArea(circle.getArea());
                    list.add(circle);
                    break;
                case 2://Rectangle
                    double x1 = input.nextDouble();
                    double y1 = input.nextDouble();
                    double x2 = input.nextDouble();
                    double y2 = input.nextDouble();
                    Point leftTopPoint = new Point(x1,y1);
                    Point lowerRightPoint = new Point(x2,y2);
                    Rectangle rectangle = new Rectangle(leftTopPoint,lowerRightPoint);
                    rectangle.setArea(rectangle.getArea());
                    list.add(rectangle);
                    break;
            }
            choice = input.nextInt();
        }

        list.sort(Shape::compareTo);//正向排序

        for(int i = 0; i < list.size(); i++) {

            System.out.print(String.format("%.2f", list.get(i).getArea()) + " ");
        }


}

    public static void printArea(Shape a){
        System.out.println(String.format("%.2f", a.getArea()));
    }
}

class Point {
    private double x;
    private double y;
    public Point() {
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    Point(double x, double y) {
        this.x =x;
        this.y = y;
    }


}
class Rectangle extends Shape{
private  Point topLeftPoint;
private  Point lowerRightPoint;

    public Rectangle(Point topLeftPoint, Point lowerRightPoint) {
        this.topLeftPoint = topLeftPoint;
        this.lowerRightPoint = lowerRightPoint;
    }

    public Point getTopLeftPoint() {
        return topLeftPoint;
    }

    public void setTopLeftPoint(Point topLeftPoint) {
        this.topLeftPoint = topLeftPoint;
    }

    public Point getLowerRightPoint() {
        return lowerRightPoint;
    }

    public void setLowerRightPoint(Point lowerRightPoint) {
        this.lowerRightPoint = lowerRightPoint;
    }
    public double getLength(){
        double Length = Math.abs(topLeftPoint.getX()-lowerRightPoint.getX());
        return  Length;
    }
    public double getHeight(){
        double Height=Math.abs(topLeftPoint.getY()-lowerRightPoint.getY());
        return Height;
    }
    public double getArea(){
        return  this.getHeight()*this.getLength();


    }
}
class Circle extends Shape{
    private double radius;

    public Circle(double radius) {

        this.radius = radius;

    }

    public double getArea() {
        return Math.PI * radius * radius;
    }

}

abstract class Shape implements Comparable<Shape>{
    private double area;
    public Shape(){
    }
    public Shape(double area) {
        this.area = area;
    }
    public double getArea(){
        return area;
    }

    public void setArea(double area) {
        this.area = area;
    }

    public int compareTo(Shape other) {
        return Double.compare(this.area, other.area);
    }

}

主要是最后一题实现排序

 在Shape中实现Comparable<Shape>

重写compareTo方法

在主函数调用

 实现排序

三、踩坑心得

菜单系列中价格四舍五入计算使用Math.round

菜单五代点菜的口味要计入其他桌而非本桌,价格要放入本桌

否则

 格式判断使用正则表达式要仔细不然容易误判

四、改进建议

菜单类代码的耦合度太高,以及十分脆弱,当要进行新需求拓展时,不满足开闭原则,大部分情况都要重构。学习设计模式后感觉大多数实现功能应该使用接口,而不是直接写死在程序里。接口的可复用性以及可拓展性可大大降低代码耦合度以及脆弱性。同时要根据需求去合理设计不同的类,尽量符合单例设计模式,开闭原则。继承关系多考虑里氏替换原则,接口拓展可以考虑适配器设计模式。类的复用可以考虑迭代器模式

五、总结

    通过这几次习题,我对java语法有了更加深刻的了解,同时被java提供的api强大所惊叹,很多时候了解一个api可以极大情况下简化代码的编写。因此,可以多看unitl工具库源码,以及多了解一些api的使用。

这几次菜单类习题的学习,我对继承,封装,多态有了更深刻的了解,以及异常处理和接口的使用也较为熟悉。同时,也明白很多其他技术也可以尝试的去学习,如多线程,反射,Lambda表达式都可以去开拓自己的视

野。同时,最重要的是设计模式的学习,让我明白一个好的程序不是完成需求,而是良好的可拓展性,面对更为复杂的需求也不脆弱。需要更好的去体会开闭原则接口的使用,适配器以及迭代器对接口的使用。体会依赖倒

转原则,让程序完成功能需求的同时体现设计之美。