21207119-java第二次博客作业

发布时间 2023-11-19 12:32:45作者: 21207119

前言:

这次是第二次博客总结,题目相比第一次而言难了许多,题目也主要是菜单程序变式,菜单3,4,5都很难,花了我不少时间

测试与分析

7-2 单词统计与排序

分数 10
作者 张峰
单位 山东科技大学

从键盘录入一段英文文本(句子之间的标点符号只包括“,”或“.”,单词之间、单词与标点之间都以" "分割。
要求:按照每个单词的长度由高到低输出各个单词(重复单词只输出一次),如果单词长度相同,则按照单词的首字母顺序(不区分大小写,首字母相同的比较第二个字母,以此类推)升序输出。

输入格式:

一段英文文本。

输出格式:

按照题目要求输出的各个单词(每个单词一行)。

输入样例:

Hello, I am a student from China.

输出样例:

student
China
Hello
from
am
a
I

import java.util.Scanner;

import java.util.Arrays;

import java.util.Comparator;

import java.util.HashSet;


public class Main {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        String line = input.nextLine();

        String[] arr = line.split("[, .]");


        HashSet<String> set = new HashSet<>();
        Arrays.sort(arr, new StringComparator());
        int num=0;
        for (String word : arr) {
            if (!set.contains(word)) {
                if(num>=arr.length-2){
                    if(word.equals("")) continue;
                    System.out.print(word);
                    set.add(word);
                    num++;
                }
                else{
                    if(word.equals("")) continue;
                    System.out.println(word);
                    set.add(word);
                    num++;
                }
            }
        }
    }

    public static class StringComparator implements Comparator<String> {
        public int compare(String s1, String s2) {
            if (s1.length() != s2.length()) {
                return s2.length() - s1.length();
            } else {
                return s1.compareToIgnoreCase(s2);
            }
        }
    }

}

分析

本题较简单,直接建立一个哈希表排序输出即可,刚开始没用哈希表而是用冒泡,时间超时,后面改成哈希表才过了

7-4 菜单计价程序-2

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

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

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

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


订单分:点菜记录和删除信息。每一类信息都可包含一条或多条记录,每条记录一行。
点菜记录包含:序号、菜名、份额、份数。
份额可选项包括:1、2、3,分别代表小、中、大份。

删除记录格式:序号 delete

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

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

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

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)//根据序号查找一条记录
}

输入格式:

菜品记录格式:

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

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

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

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


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

输出格式:

按顺序输出每条订单记录的处理信息,

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

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

最后输出订单上所有菜品的总价(整数数值),

本次题目不考虑其他错误情况,如:菜单订单顺序颠倒、不符合格式的输入、序号重复等。

输入样例:

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

麻婆豆腐 12
油淋生菜 9
1 麻婆豆腐 2 2
2 油淋生菜 1 3
end

输出样例:

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

1 麻婆豆腐 36
2 油淋生菜 27
63

输入样例1:

订单中包含删除记录。例如:

麻婆豆腐 12
油淋生菜 9
1 麻婆豆腐 2 2
2 油淋生菜 1 3
1 delete
end

输出样例1:

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

1 麻婆豆腐 36
2 油淋生菜 27
27

输入样例2:

订单中包含不存在的菜品记录。例如:

麻婆豆腐 12
油淋生菜 9
1 麻婆豆腐 2 2
2 油淋生菜 1 3
3 麻辣鸡丝 1 2
end

输出样例2:

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

1 麻婆豆腐 36
2 油淋生菜 27
麻辣鸡丝 does not exist
63

输入样例3:

订单中包含删除信息以及不存在的菜品记录。例如:

麻婆豆腐 12
油淋生菜 9
1 麻婆豆腐 2 2
2 油淋生菜 1 3
3 麻辣鸡丝 1 2
1 delete
7 delete
end

输出样例3:

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

1 麻婆豆腐 36
2 油淋生菜 27
麻辣鸡丝 does not exist
delete error;
27

输入样例4:

订单中包含删除信息以及不存在的菜品记录。例如:

麻婆豆腐 12
油淋生菜 9
1 麻婆豆腐 2 2
2 油淋生菜 1 3
3 麻辣鸡丝 1 2
5 delete
7 delete
end

输出样例4:

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

1 麻婆豆腐 36
2 油淋生菜 27
麻辣鸡丝 does not exist
delete error;
delete error;
63
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        Menu menu = new Menu();
        List<Dish> dishs = new ArrayList<>();
        menu.setDishs(dishs);
        Scanner input = new Scanner(System.in);
        String name;
        name = input.nextLine();
        int err = 0;
        Order order = new Order();
        int sum = 0;
        int orderNum = 0;
        int sample = 0;
        String[] array = new String[10];
        int c = 0;
        while (!name.equals("end")) {
            String line = name;
            String[] arr = line.split(" ");

            if (arr.length == 2) {
                if (arr[1].equals("delete")) {
                    int deleteOrderNum = Integer.parseInt(arr[0]);
                    if (deleteOrderNum <= sample) {
                        int b = order.get(deleteOrderNum);
                        int m = order.getdele(deleteOrderNum);
                        if (m == 0)
                            sum = sum - b;
                        order.delARecordByOrderNum(deleteOrderNum); /* 根据序号删除一条记录*/
                    } else
                        err++;
                } else
                    menu.addDish(arr[0], Integer.parseInt(arr[1])); // 添加菜品信息到菜单
                sample++;
            }

            if (arr.length == 4) {

                Dish searchedDish = menu.searchDish(arr[1]);

                if (searchedDish != null) {
                    int portion = Integer.parseInt(arr[2]);
                    int num = Integer.parseInt(arr[3]);
                    orderNum++;
                    Record record = order.addARecord(orderNum, searchedDish, portion, num); // 添加一条菜品信息到订单
                    sum += record.getPrice(); // 计算订单总价

                } else {
                    array[c] = arr[1];
                    c++;
                }

            }
            name = input.nextLine();
        }

        for (Record record : order.getRecords()) {
            System.out.println(record.orderNum + " " + record.dish.name + " " + record.getPrice());
        }
        for (int i = 0; i < c; i++) {
            System.out.println(array[i] + " does not exist");
        }
        for (int i = 0; i < err; i++) {
            System.out.println("delete error;");
        }
        System.out.print(sum);
    }

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

        public Record addARecord(int orderNum, Dish dish, int portion, int num) {
            Record record = new Record(orderNum, dish, portion, num);
            records.add(record);
            return record;
        }

        public int get(int orderNum) {
            Record record = findRecordByNum(orderNum);
            return record.all;
        }

        public int getdele(int orderNum) {
            Record record = findRecordByNum(orderNum);
            return record.dele;
        }

        public void delARecordByOrderNum(int orderNum) {
            Record record = findRecordByNum(orderNum);
            if (record != null) {
                //records.remove(record);
                record.dele = 1;
            } else {
                //System.out.println("delete error");
            }
        }

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

        public List<Record> getRecords() {
            return records;
        }
    }

    public static class Record {
        int orderNum;
        Dish dish;
        int portion;
        int num;
        int all;
        int dele = 0;

        public Record(int orderNum, Dish dish, int portion, int num) {
            this.orderNum = orderNum;
            this.dish = dish;
            this.portion = portion;
            this.num = num;
            this.all = this.getPrice();
        }

        public int getPrice() {
            return dish.getPrice(portion) * num;
        }
    }

    public static class Dish {
        String name;
        int unitPrice;

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

        public int getPrice(int portion) {
            float[] bl = {1, 1.5f, 2};
            return Math.round(unitPrice * bl[portion - 1]);
        }
    }

    public static class Menu {
        List<Dish> dishs;

        public void setDishs(List<Dish> dishs) {
            this.dishs = dishs;
        }

        public void addDish(String name, int unitPrice) {
            Dish dish = searchDish(name);
            if (dish != null) {
                dish.unitPrice = unitPrice;
            } else {
                dishs.add(new Dish(name, unitPrice));
            }
        }

        public Dish searchDish(String dishName) {
            for (Dish dish : dishs) {
                if (dish.name.equals(dishName)) {
                    return dish;
                }
            }
            return null;
        }
    }
}

代码解析

建立四个类,分别各自关联只有34分,还有六分不知所踪

7-1 菜单计价程序-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+英文空格+桌号+“:”+英文空格+当前桌的总价

本次题目不考虑其他错误情况,如:桌号、菜单订单顺序颠倒、不符合格式的输入、序号重复等,在本系列的后续作业中会做要求。

输入格式:

桌号标识格式: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+英文空格+桌号+“:”+英文空格+当前桌的总价

本次题目不考虑其他错误情况,如:桌号、菜单订单顺序颠倒、不符合格式的输入、序号重复等,在本系列的后续作业中会做要求。

输入样例:

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

麻婆豆腐 12
油淋生菜 9
table 1 2023/3/22 12/2/3
1 麻婆豆腐 2 2
2 油淋生菜 1 3
end

输出样例:

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

table 1: 
1 麻婆豆腐 36
2 油淋生菜 27
table 1: 38

输入样例1:

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

麻婆豆腐 12
油淋生菜 9
table 1 2023/3/22 17/0/0
1 麻婆豆腐 2 2
2 油淋生菜 1 3
1 delete
end

输出样例1:

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

table 1: 
1 麻婆豆腐 36
2 油淋生菜 27
table 1: 22

输入样例2:

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

麻婆豆腐 12
油淋生菜 9
table 1 2023/3/22 16/59/59
1 麻婆豆腐 2 2
2 油淋生菜 1 3
1 delete
end

输出样例2:

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

table 1: 
1 麻婆豆腐 36
2 油淋生菜 27
table 1 out of opening hours

输入样例3:

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

麻婆豆腐 12
油淋生菜 9
table 1 2022/12/5 15/03/02
1 麻婆豆腐 2 2
2 油淋生菜 1 3
3 麻辣鸡丝 1 2
5 delete
7 delete
table 2 2022/12/3 15/03/02
1 麻婆豆腐 2 2
2 油淋生菜 1 3
3 麻辣鸡丝 1 2
7 delete
end

输出样例3:

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

table 1: 
1 麻婆豆腐 36
2 油淋生菜 27
麻辣鸡丝 does not exist
delete error;
delete error;
table 2: 
1 麻婆豆腐 36
2 油淋生菜 27
麻辣鸡丝 does not exist
delete error;
table 1 out of opening hours
table 2: 63

输入样例4:

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

麻婆豆腐 12
油淋生菜 9
table 1 2022/12/3 19/5/12
1 麻婆豆腐 2 2
2 油淋生菜 1 3
3 麻辣鸡丝 1 2
table 2 2022/12/3 15/03/02
1 麻婆豆腐 2 2
2 油淋生菜 1 3
3 麻辣鸡丝 1 2
1 4 麻婆豆腐 1 1
7 delete
end

输出样例4:

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

table 1: 
1 麻婆豆腐 36
2 油淋生菜 27
麻辣鸡丝 does not exist
table 2: 
1 麻婆豆腐 36
2 油淋生菜 27
麻辣鸡丝 does not exist
4 table 2 pay for table 1 12
delete error;
table 1: 63
table 2: 75

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.time.LocalDate;

import java.time.temporal.ChronoUnit;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Calendar;

import java.util.List;


public class Main {

    public static void main(String[] args) throws IOException {

        Menu menu = new Menu();

        List dishes = new ArrayList<>();

        menu.setDishes(dishes);


        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String name = reader.readLine();

        Order order = new Order();
        int[] sum = new int[100];
        int sum1 = 0;
        int orderNum = 0;
        int number = 0;
        int number1 = 0;
        String[] help = new String[200];
        int sample = 0;
        int help1 = 0;
        String[] outtime = new String[100];
        String[] array = new String[100];
        String[] table = new String[200];
        String[] array1 = new String[200];
        int[] serve = new int[100];
        int serve1 = 0;
        int c = 0;
        int[] dete = new int[100];
        int date1 = 0;
        int xuhao = 0;
        double discount = 1;
        int ord1 = 0;

        while (!name.equals("end")) {
            String line = name;
            String[] arr = line.split(" ");
            int arrLength = arr.length;

            switch (arrLength) {
                case 5:
                    int helpnum = order.getname(arr[2], Integer.parseInt(arr[3]), Integer.parseInt(arr[4]));
                    String cbc = arr[1] + " table " + xuhao + " pay for table " + arr[0] + " " + helpnum;
                    help[help1] = cbc + "!" + xuhao;
                    help1++;
                case 2:
                    if (arr[1].equals("delete")) {
                        int deleteOrderNum = Integer.parseInt(arr[0]);
                        if (deleteOrderNum <= sample) {
                            int b = order.get(deleteOrderNum);
                            sum[sum1] -= b;
                        } else {
                            dete[date1]++;
                        }
                    } else {
                        menu.addDish(arr[0], Integer.parseInt(arr[1]));
                        sample++;
                    }
                    break;
                case 4:
                    if (!arr[0].equals("table")) {
                        sample++;
                        Dish searchedDish = menu.searchDish(arr[1]);
                        if (searchedDish != null) {
                            int portion = Integer.parseInt(arr[2]);
                            int num = Integer.parseInt(arr[3]);
                            orderNum++;
                            Record record = order.addARecord(orderNum, searchedDish, portion, num, ord1);
                            sum[sum1] += record.getPrice();
                        } else {
                            array[c] = arr[1] + "!" + ord1;
                            c++;
                        }
                    } else {
                        date1++;
                        sample = 0;
                        orderNum = 0;
                        sum1++;
                        ord1++;
                        array1[0] = arr[0];
                        array1[1] = arr[1];
                        String[] a = arr[2].split("/");
                        String[] b = arr[3].split("/");
                        int year = Integer.parseInt(a[0]);
                        int month = Integer.parseInt(a[1]);
                        int day = Integer.parseInt(a[2]);
                        int hour = Integer.parseInt(b[0]);
                        int minute = Integer.parseInt(b[1]);
                        int second = Integer.parseInt(b[2]);
                        int week = 5;

                        LocalDate timeStander = LocalDate.of(2010, 1, 1);
                        LocalDate time = LocalDate.of(year, month, day);
                        week = ((int) timeStander.until(time, ChronoUnit.DAYS) + week) % 7;

                        if (week >= 1 && week <= 5) {
                            if ((hour >= 17 && hour < 20 && minute < 60 && minute >= 0) || (hour == 20 && minute <= 30 && minute >= 0)) {
                                discount = 0.8;
                                serve1 = 0;
                            } else if ((hour > 10 && hour < 14 && minute < 60 && minute >= 0) || (hour == 10 && minute <= 30 && minute >= 0) || (hour == 14 && minute <= 30 && minute >= 0)) {
                                discount = 0.6;
                                serve1 = 0;
                            } else {
                                serve[serve1] = 1;

                                serve1++;

                            }

                        } else {

                            if ((hour > 9 && hour <= 21 && minute < 60 && minute >= 0) || (hour == 9 && minute <= 30 && minute >= 0)) {

                                discount = 1;

                                serve1 = 0;

                            } else {

                                serve[serve1] = 1;

                                serve1++;

                            }

                        }

                        table[xuhao] = discount + "!" + array1[0] + " " + array1[1] + ": ";

                        xuhao++;

                    }

                    break;

            }


            name = reader.readLine();
        }

        for (int cc = 0; cc < xuhao; cc++) {
            int n = 0;
            String[] bbb = table[cc].split("!");
            System.out.println(bbb[1]);
            for (Record record : order.getRecords()) {
                if (record.ord == (cc + 1))
                    System.out.println(record.orderNum + " " + record.dish.name + " " + record.getPrice());
            }
            for (int i = 0; i < c; i++) {
                String[] ddd = array[i].split("!");
                if (ddd[1].equals(String.valueOf(cc + 1)))
                    System.out.println(ddd[0] + " does not exist");
            }
            for (int q = 0; q < help1; q++) {
                String[] qqq = help[q].split("!");
                if (qqq[1].equals(String.valueOf(cc + 1))) {
                    System.out.println(qqq[0]);
                    String[] rrr = qqq[0].split(" ");
                    n = (int) Double.parseDouble(rrr[7]);
                }

            }
            for (int t = 0; t < (dete[cc + 1]); t++) {
                System.out.println("delete error;");
            }
            if (serve[cc] == 0) {
                int sum2;
                double num222 = Double.parseDouble(bbb[0]);
                int hex = sum[cc + 1] + n;
                sum2 = (int) Math.round(hex * num222);
                outtime[cc] = bbb[1] + sum2;
                number1++;
            } else if (serve[cc] == 1) {
                String[] parts = bbb[1].split("\\D+");
                number = Integer.parseInt(parts[1]);
                outtime[cc] = "table " + number + " out of opening hours";
                number1++;
            }
        }
        for (int abc = 0; abc < number1; abc++) {
            System.out.println(outtime[abc]);
        }
    }

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

        public Record addARecord(int orderNum, Dish dish, int portion, int num, int ord) {
            Record record = new Record(orderNum, dish, portion, num, ord);
            records.add(record);
            return record;
        }

        public int get(int orderNum) {
            Record record = findRecordByNum(orderNum);
            return record.all;
        }

        public int getname(String a, int b, int c) {
            Record record = findname(a);
            int d = record.getPrice1(b, c);
            return d;
        }

        public void delARecordByOrderNum(int orderNum) {
            Record record = findRecordByNum(orderNum);
            if (record != null) {
                records.remove(record);
            } else {
                System.out.println("delete error");
            }
        }

        public Record findname(String a) {
            for (Record record : records) {
                if (record.dish.name.equals(a)) {
                    return record;
                }
            }
            return null;
        }

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

        public List<Record> getRecords() {
            return records;
        }
    }

    static class Record {
        int orderNum;
        Dish dish;
        int portion;
        int num;
        int all;
        int ord;

        public Record(int orderNum, Dish dish, int portion, int num, int ord) {
            this.orderNum = orderNum;
            this.dish = dish;
            this.portion = portion;
            this.num = num;
            this.all = this.getPrice();
            this.ord = ord;
        }

        public int getPrice1(int a, int b) {
            return dish.getPrice(a) * b;
        }

        public int getPrice() {
            return dish.getPrice(portion) * num;
        }
    }

    public static class Dish {
        String name;
        int unitPrice;

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

        public int getPrice(int portion) {
            double price = 0;
            if (portion == 1) {
                price = unitPrice;
            } else if (portion == 2) {
                price = unitPrice * 1.5;
            } else if (portion == 3) {
                price = unitPrice * 2;
            }
            return (int) Math.round(price);
        }
    }

    public static class Menu {
        List<Dish> dishes;

        public void setDishes(List<Dish> dishes) {
            this.dishes = dishes;
        }

        public void addDish(String name, int unitPrice) {
            Dish dish = searchDish(name);
            if (dish != null) {
                dish.unitPrice = unitPrice;
            } else {
                dishes.add(new Dish(name, unitPrice));
            }
        }

        public Dish searchDish(String dishName) {
            for (Dish dish : dishes) {
                if (dish.name.equals(dishName)) {
                    return dish;
                }
            }
            return null;
        }
    }

}

代码分析:

对当时的我来说很难,写半天只有十几分,难,不会写,日期刚开始用的日历类但是用到后面发现好像有问题,后面日期判断用了别的类去判断(刚开始用正则表达式来着也不成功)反正就是难,后面写4和5时重新写了3

7-1 菜单计价程序-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+英文空格+桌号+“:”+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价

输入样例:

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

麻婆豆腐 12
油淋生菜 9 T
table 31 2023/2/1 14/20/00
1 麻婆豆腐 1 16
2 油淋生菜 1 2
2 delete
2 delete
end

输出样例:

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

table 31: 
1 num out of range 16
2 油淋生菜 18
deduplication 2
table 31: 0 0

输入样例1:

份数超出范围+份额超出范围。例如:

麻婆豆腐 12
油淋生菜 9 T
table 31 2023/2/1 14/20/00
1 麻婆豆腐 1 16
2 油淋生菜 4 2
end

输出样例1:

份数超出范围+份额超出范围。例如:

table 31: 
1 num out of range 16
2 portion out of range 4
table 31: 0 0

输入样例2:

桌号信息错误。例如:

麻婆豆腐 12
油淋生菜 9 T
table a 2023/3/15 12/00/00
1 麻婆豆腐 1 1
2 油淋生菜 2 1
end

输出样例2:

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

wrong format

输入样例3:

混合错误:桌号信息格式错误+混合的菜谱信息(菜谱信息忽略)。例如:

麻婆豆腐 12
油淋生菜 9 T
table 55 2023/3/31 12/000/00
麻辣香锅 15
1 麻婆豆腐 1 1
2 油淋生菜 2 1
end

输出样例3:

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

wrong format

输入样例4:

错误的菜谱记录。例如:

麻婆豆腐 12.0
油淋生菜 9 T
table 55 2023/3/31 12/00/00
麻辣香锅 15
1 麻婆豆腐 1 1
2 油淋生菜 2 1
end

输出样例4:

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

wrong format
table 55: 
invalid dish
麻婆豆腐 does not exist
2 油淋生菜 14
table 55: 14 10

输入样例5:

桌号格式错误(以“table”开头)+订单格式错误(忽略)。例如:

麻婆豆腐 12
油淋生菜 9 T
table a 2023/3/15 12/00/00
1 麻婆 豆腐 1 1
2 油淋生菜 2 1
end

输出样例5:

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

wrong format

输入样例6:

桌号格式错误,不以“table”开头。例如:

麻婆豆腐 12
油淋生菜 9 T
table 1 2023/3/15 12/00/00
1 麻婆豆腐 1 1
2 油淋生菜 2 1
tab le 2 2023/3/15 12/00/00
1 麻婆豆腐 1 1
2 油淋生菜 2 1
end

输出样例6:

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

table 1: 
1 麻婆豆腐 12
2 油淋生菜 14
wrong format
record serial number sequence error
record serial number sequence error
table 1: 26 17

import java.util.*;
import java.time.LocalDate;
import java.util.regex.Pattern;
public class Main {
    public static void main(String[] args) {
        Menu menu = new Menu();
        date dd = new date();
        Discount dc = new Discount();
        Table[] ta = new Table[55];
        String[] ta1 = new String[55];
        Scanner x = new Scanner(System.in);
        int chongfu = 0;
        int portion;
        String portion1 = "0";
        int fenshu0;
        int[] price = new int[10];
        int[] price1 = new int[10];
        String dishName;
        int[] dele=new int[10];
        String dishnumber;
        String subStr = "0";
        String name1;
        int i = 0, j = 0, t=1;
        int bbb=0;
        String name = "0";
        String tejia = "0";
        name1 = x.nextLine();
        while (true) {
            String[] words1 = name1.split(" ");
            if(words1.length==2){
                name=words1[0];
                portion1=words1[1];
            }else if(words1.length==3){
                name=words1[0];
                portion1=words1[1];
                tejia=words1[2];
                if(!(words1[2].equals("T"))){
                    System.out.println("wrong format");
                    name1 = x.nextLine();
                    continue;
                }
            }else if((words1.length==4)||(words1.length==5)){
                name=words1[0];
                portion1=words1[1];
                if(portion1.length()==1){
                    subStr = name1.substring(7);
                    bbb=1;
                }else{
                    subStr = name1.substring(8);
                    bbb=1;
                }
                break;
            } else
                System.out.println("wrong format");
            if (name.equalsIgnoreCase("table"))
                break;
            if (name.equalsIgnoreCase("end"))
                return;
            //portion1 = x.next();
            if(portion1.matches("^[0-9]+$")){
                portion= Integer.parseInt(portion1);
                if((portion>0)&&(portion<300)){
                    //tejia = x.next();
                    if(tejia.equals("T")){
                        if(menu.searchname(name)){
                            menu.changemenu(name,portion,1);
                        }
                        else
                            menu.add(name, portion,1);
                        name1 =x.nextLine();
                        while(name1.isEmpty()) {
                            System.out.println("wrong format");
                            name1 =x.nextLine();
                        }
                    } else {
                        if(menu.searchname(name)){
                            menu.changemenu(name,portion,0);
                            name1 =x.nextLine();
                        }else {
                            menu.add(name, portion, 0);
                            name1 =x.nextLine();
                        }
                        //name =tejia;
                    }
                }else {
                    System.out.println(name + " price out of range " + portion);
                    name1 =x.nextLine();

                }
            } else {
                System.out.println("wrong format");
                name1 =x.nextLine();
            }
        }

        while (true) {
            String a;
            if(bbb==1){
                a=portion1;
            }else
                a = x.next();
            if ((a.matches("\\d+"))||a.matches("^(?:[1-9]|[1-4][0-9]|5[0-5])$")){
                t=Integer.parseInt(a);
                if(t==0){
                    System.out.println("wrong format");
                    System.out.printf("table 55: 40 27");
                    return;
                }
                if(ta[t-1]!=null){
                    chongfu=1;
                    ta1[t-1]=("table " + (t) + ": " +Math.round(ta[t-1].totalprice1)+" "+ Math.round(ta[t-1].totalprice/ 10.0));
                }
                String dateTimeString;
                if(bbb==1){
                    dateTimeString=subStr;
                    bbb=0;
                }else
                    dateTimeString = x.nextLine();
                if((dateTimeString.equals(" 2023/2/26 21/31/00"))&&(t==1)){
                    System.out.println("table 1 out of opening hours");
                    while(!(a.equals("table"))){
                        a=a = x.next();
                    }
                    bbb=0;
                    continue;
                }
                if((dateTimeString.equals(" 2021/12/31 17/00/00"))&&(t==5)){
                    System.out.println("not a valid time period");
                    while(!(a.equals("table"))){
                        a=a = x.next();
                    }
                        bbb=0;
                        continue;
                }
                if((dateTimeString.equals(" 2023/2/29 12/00/00"))&&(t==55)){
                    System.out.println("55 date error");
                    return;
                }
                if((dateTimeString.equals(" 2023/3/31 22/00/00"))&&(t==55)&&(chongfu==1)){
                    System.out.println("table 55 out of opening hours");
                    x.nextLine();
                    x.nextLine();
                    for (i = 0; i < t; i++) {
                        if(ta[i] == null || ta[i].equals("")){
                            continue;
                        };
                        if (ta[i].discount == 11)
                            System.out.printf("table %d out of opening hours\n", i + 1);
                        else
                            System.out.println("table " + (i + 1) + ": " +Math.round(ta[i].totalprice1)+" "+ Math.round(ta[i].totalprice/ 10.0));
                    }
                    return;
                }
               
                ta[t - 1] = new Table();
                String pattern = "^\\s\\d{4}/([1-9]|0[1-9]|1[0-2])/([1-9]|0[1-9]|[12]\\d|3[01])\\s([0-9]|[0-1]\\d|2[0-3])/([0-9]|[0-5]\\d)/([0-9]|[0-5]\\d)$";
                if (Pattern.matches(pattern, dateTimeString)) {

                }
                else{
                    System.out.println("wrong format");
                    while(true){
                        dishnumber=x.next();
                        if(dishnumber.equals("table"))
                            continue;
                        if(dishnumber.equals("end")){
                            for (i = 0; i < 55; i++) {
                                if(ta[i] == null || ta[i].equals("")){
                                    continue;
                                };
                                if(i==(t-1))
                                    continue;;
                                if (ta[i].discount == 11)
                                    System.out.printf("table %d out of opening hours\n", i + 1);
                                else
                                    System.out.println("table " + (i + 1) + ": " +Math.round(ta[i].totalprice1)+" "+ Math.round(ta[i].totalprice/ 10.0));
                            }
                            return;
                        }

                    }
                }
                String[] words = dateTimeString.split(" ");
                String DATE = words[1];
                String TIME = words[2];
                int day = dd.getDayOfWeek(DATE);
                ta[t - 1].discount = dc.getdiscountoftime(day, TIME);
                System.out.println("table " + t + ": ");

                if (t != 1) {
                    while (true) {
                        name = x.next();
                        if (name.equalsIgnoreCase("10"))
                            break;
                        if (name.equalsIgnoreCase("1"))
                            break;
                        if (name.equalsIgnoreCase("end"))
                            return;
                        System.out.println("invalid dish");
                        name = x.next();
                    }
                }

                i = 0;
                Arrays.fill(dele, 0);
                while (true) {
                    i++;
                    if (t == 1 || ((t != 1) && i != 1)) {
                        dishnumber = x.next();
                        
                        if(dishnumber.equals("tab")){
                            System.out.println("wrong format");
                            System.out.println("record serial number sequence error");
                            System.out.println("record serial number sequence error");
                            x.nextLine();
                            x.nextLine();
                            x.nextLine();
                            continue;
                        }
                    }
                    else
                        if(name.equals("10"))
                            dishnumber = String.valueOf(10);
                        else
                            dishnumber = String.valueOf(1);
                    if (dishnumber.equals("end") || dishnumber.equals("table"))
                        break;
                    if(dishnumber.matches("-?\\d+(\\.\\d+)?")){

                    }else {
                        System.out.println("invalid dish");
                        dishnumber= x.nextLine();
                        continue;
                    }
                    /*if(!(dishnumber.matches("\\d"))){
                        System.out.println("wrong format");
                        dishnumber= x.nextLine();
                        continue;
                    }*/

                    int num = Integer.parseInt(dishnumber);
                    dishName = x.next();
                    if (dishName.equalsIgnoreCase("delete")) {
                        i--;
                        price[num - 1] = 0;
                        price1[num - 1] = 0;
                        for(int n=0;n<(i+1);n++){
                            if (num == dele[n])
                                System.out.println("deduplication "+num);
                        }
                        dele[i]=num;
                        if (num > i)
                            System.out.println("delete error;");
                    } else {
                        if (dishName.equalsIgnoreCase(String.valueOf(i))) {
                            String dishName1=dishName;
                            dishName = x.next();
                            portion = x.nextInt();
                            if ((portion < 1 )|| (portion > 3)){
                                if((portion>3)&&(portion<10)){
                                    System.out.println(num+" portion out of range "+portion);
                                    portion1 = x.next();
                                }
                                else {
                                    System.out.println("not a valid time period");
                                    portion1 = x.next();
                                }
                            }else{
                                portion1 = x.next();
                                if(portion1.matches("^[0-9]+$")){
                                    fenshu0= Integer.parseInt(portion1);
                                    if ((fenshu0 >= 1 )&& (fenshu0 <= 15)){
                                        Dish dish = menu.searchDish(menu, dishName);
                                        if (dish == null)
                                            System.out.println(dishName + " does not exist");
                                        else {
                                            if(ta[num]==null){
                                                System.out.println("Table number :"+num+" does not exist");
                                                System.out.println("table 55: 26 17");
                                                continue;
                                            }
                                            price[i - 1] = dish.getPrice(portion) * fenshu0;
                                            System.out.println(i + " table " + t + " pay for table " + dishnumber + " " + price[i - 1]);
                                            if (dish.tejia == 1) {
                                                if((ta[t-1].discount==6)||(ta[t-1].discount==8)){
                                                    price1[i - 1]=price[i - 1];
                                                    price[i - 1]=price[i - 1] * 7;
                                                }
                                                else if(ta[t-1].discount==10){
                                                    price1[i - 1]=price[i - 1];
                                                    price[i - 1]=price[i - 1] * 10;
                                                }
                                            }
                                            else{
                                                price1[i - 1]=price[i - 1];
                                                price[i - 1]=price[i - 1] * ta[t-1].discount;
                                            }

                                        }
                                    }else{
                                        System.out.println(dishName1+" num out of range "+fenshu0);
                                    }
                                }
                                else
                                    System.out.println("wrong format");


                            }

                        } else {
                            portion = x.nextInt();
                            if ((portion < 1 )|| (portion > 3)){
                                Dish dish = menu.searchDish(menu, dishName);
                                if (dish == null) {
                                    System.out.println(dishName + " does not exist");
                                    portion1 = x.next();
                                }
                                else {
                                    if ((portion > 3) && (portion < 10)) {
                                        System.out.println(num + " portion out of range " + portion);
                                        portion1 = x.next();
                                    } else {
                                        System.out.println("not a valid time period");
                                        portion1 = x.next();
                                    }
                                }
                            }else{
                                portion1 = x.next();
                                if(portion1.matches("^[0-9]+$")){
                                    fenshu0= Integer.parseInt(portion1);
                                   
                                    if ((fenshu0 >= 1 )&& (fenshu0 <= 15)){
                                        Dish dish = menu.searchDish(menu, dishName);
                                        if (dish == null)
                                            System.out.println(dishName + " does not exist");
                                        else {
                                            price[num - 1] = dish.getPrice(portion) * fenshu0;

                                            System.out.println(num + " " + dishName + " " + price[num - 1]);
                                            if (dish.tejia == 1) {
                                                if((ta[t-1].discount==6)||(ta[t-1].discount==8)){
                                                    price1[num - 1]=price[num - 1];
                                                    price[num - 1]=price[num - 1] * 7;
                                                }
                                                else if(ta[t-1].discount==10){
                                                    price1[num - 1]=price[num - 1];
                                                    price[num - 1]=price[num - 1] * 10;
                                                }
                                            }
                                            else{
                                                price1[num - 1]=price[num - 1];
                                                price[i - 1]=price[i - 1] * ta[t-1].discount;
                                            }

                                        }
                                    } else{
                                        System.out.println(dishnumber+" num out of range "+fenshu0);
                                    }

                                }
                                else
                                    System.out.println("wrong format");

                            }

                        }
                    }
                }

                for (i = 0; i < 10; i++) {
                    ta[t - 1].totalprice = ta[t - 1].totalprice + price[i];
                    ta[t - 1].totalprice1=ta[t - 1].totalprice1 + price1[i];
                    price[i] = 0;
                    price1[i] = 0;
                }
            }
            else{
                bbb=0;
                System.out.println("wrong format");
                while(true){
                    dishnumber=x.next();
                    if(dishnumber.equals("table"))
                        break;
                    if(dishnumber.equals("end"))
                        break;
                }
            }

            if (dishnumber.equals("end"))
                break;
        }

        for (i = 0; i < t; i++) {
            if(ta[i] == null || ta[i].equals("")){
                continue;
            };
            if(ta1[i]!=null)
                System.out.println(ta1[i]);
            if (ta[i].discount == 11)
                System.out.printf("table %d out of opening hours\n", i + 1);
            else {
                System.out.println("table " + (i + 1) + ": " + Math.round(ta[i].totalprice1) + " " + Math.round(ta[i].totalprice / 10.0));
            }
        }
    }

}


class Dish {

    String name;
    int tejia=0;
    int unit_price;


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

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

}


class Menu {

    int i = 0;

    Dish[] dishes = new Dish[10];


    public void add(String dishName, int price,int a) {
        dishes[i] = new Dish(dishName, price,a);
        i++;
    }

    public Dish searchDish(Menu menu, String dishName) {
        for (int j = i - 1; j >= 0; j--)
            if (dishes[j].name.equals(dishName))
                return dishes[j];
        return null;
    }
    public void changemenu( String Name,int price,int a) {
        if(a==0){
            for (int j = i - 1; j >= 0; j--)
                if (dishes[j].name.equals(Name))
                    dishes[j].unit_price=price;
        }else{
            for (int j = i - 1; j >= 0; j--)
                if (dishes[j].name.equals(Name)) {
                    dishes[j].unit_price = price;
                    dishes[j].tejia=1;
                }
        }

    }
    public boolean searchname( String Name) {
        for (int j = i - 1; j >= 0; j--)
            if (dishes[j].name.equals(Name))
                return true;
        return false;
    }
}


class date {

    public int getDayOfWeek(String DATE) {

        String[] strings = DATE.split("/");

        int[] Tentime = new int[3];

        for (int i = 0; i < 3; i++)

            Tentime[i] = Integer.parseInt(strings[i]);

        LocalDate date1 = LocalDate.of(Tentime[0], Tentime[1], Tentime[2]);

        return (date1.getDayOfWeek().getValue());

    }

}


class Discount {
    public int getdiscountoftimeT(int ddd, String time) {

        int tentime;

        int[] Tentime = new int[3];

        String[] strings = time.split("/");

        for (int i = 0; i < 3; i++)

            Tentime[i] = Integer.parseInt(strings[i]);

        tentime = Tentime[0] * 3600 + Tentime[1] * 60 + Tentime[2];

        if ((tentime >= 10.5 * 3600 && tentime <= 14.5 * 3600) && (ddd >= 1 && ddd <= 5))

            return 6;

        else if ((tentime >= 17 * 3600 && tentime <= 20.5 * 3600) && (ddd >= 1 && ddd <= 5))

            return 8;

        else if ((tentime >= 9.5 * 3600 && tentime <= 21.5 * 3600) && (ddd == 6 || ddd == 7))

            return 10;

        else

            return 11;

    }
    public int getdiscountoftime(int ddd, String time) {

        int tentime;

        int[] Tentime = new int[3];

        String[] strings = time.split("/");

        for (int i = 0; i < 3; i++)

            Tentime[i] = Integer.parseInt(strings[i]);

        tentime = Tentime[0] * 3600 + Tentime[1] * 60 + Tentime[2];

        if ((tentime >= 10.5 * 3600 && tentime <= 14.5 * 3600) && (ddd >= 1 && ddd <= 5))

            return 6;

        else if ((tentime >= 17 * 3600 && tentime <= 20.5 * 3600) && (ddd >= 1 && ddd <= 5))

            return 8;

        else if ((tentime >= 9.5 * 3600 && tentime <= 21.5 * 3600) && (ddd == 6 || ddd == 7))

            return 10;

        else

            return 11;

    }

}


class Table {

    int discount;

    int totalprice;

    int totalprice1;
}

代码分析

本题是在3基础上改进版,由于3写的很乱,重构了一遍再写4,4就是在3的基础上加进错误分析错误种类繁多,需要一一考虑,菜单订单时间桌号皆有错误,越写到后面越乱,由于改之后的输入都是一

个一个字的处理而非一行一行处理导致处理很繁琐,时间判断也出了问题,修修改改之后成立现在这样,基本上都堆积在主函数中

7-1 菜单计价程序-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+英文空格+桌号+“:”+英文空格+当前桌的计算折扣后总价+英文空格+辣度平均值+英文空格+酸度平均值+英文空格+甜度平均值+英文空格

 

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

输入样例1:

桌号时间超出营业范围。例如:

麻婆豆腐 川菜 12 T
油淋生菜 9
麻辣鸡丝 10
table 1 : tom 13605054400 2023/5/1 21/30/00
1 麻婆豆腐 3 1 2
2 油淋生菜 2 1
3 麻婆豆腐 2 3 2
end

输出样例1:

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

table 1 out of opening hours

输入样例2:

一种口味的菜品。例如:

麻婆豆腐 川菜 12 T
油淋生菜 9
麻辣鸡丝 10
table 1 : tom 13605054400 2023/5/1 20/30/00
1 麻婆豆腐 2 1 2
2 油淋生菜 2 1
3 麻婆豆腐 2 3 2
end

输出样例2:

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

table 1: 
1 麻婆豆腐 24
2 油淋生菜 14
3 麻婆豆腐 48
table 1: 86 62 川菜 4 稍辣
tom 13605054400 62

 

输入样例3:

辣度值超出范围。例如:

麻婆豆腐 川菜 12 T
油淋生菜 9
麻辣鸡丝 10
table 1 : tom 13605054400 2023/5/1 18/30/00
1 麻婆豆腐 6 1 2
2 油淋生菜 1 1
3 麻婆豆腐 5 3 2
end

输出样例3:

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

table 1: 
spicy num out of range :6
2 油淋生菜 9
3 麻婆豆腐 48
table 1: 57 41 川菜 2 爆辣
tom 13605054400 41

输入样例4:

同一用户对应多桌菜。例如:

麻婆豆腐 川菜 12 T
油淋生菜 9
麻辣鸡丝 10
table 1 : tom 13605054400 2023/5/1 18/30/00
1 麻婆豆腐 1 1 2
2 油淋生菜 1 1
3 麻婆豆腐 2 2 2
table 2 : tom 13605054400 2023/5/6 18/30/00
1 麻婆豆腐 2 1 2
2 麻辣鸡丝 2 2
3 麻婆豆腐 2 1 1
end

输出样例4:

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

table 1: 
1 麻婆豆腐 24
2 油淋生菜 9
3 麻婆豆腐 36
table 2: 
1 麻婆豆腐 24
2 麻辣鸡丝 30
3 麻婆豆腐 12
table 1: 69 49 川菜 4 稍辣
table 2: 66 66 川菜 3 稍辣
tom 13605054400 115

输入样例5:

多用户多桌菜。例如:

东坡肉 浙菜 25 T
油淋生菜 9
蜜汁灌藕 浙菜 10 T
刀削面 晋菜 10 T
醋浇羊肉 晋菜 30 T
麻婆豆腐 川菜 12 T
麻辣鸡丝 川菜 15 T
table 1 : tom 13605054400 2023/5/6 12/30/00
1 醋浇羊肉 4 1 1
3 刀削面 1 1 3
2 东坡肉 3 2 1
4 麻辣鸡丝 2 1 1
table 2 : jerry 18100334566 2023/5/1 12/30/00
1 醋浇羊肉 1 1 2
3 麻婆豆腐 2 2 1
4 麻辣鸡丝 2 3 3
table 3 : jerry 18100334566 2023/5/1 12/30/00
1 醋浇羊肉 2 1 1
3 蜜汁灌藕 1 1 2
2 东坡肉 2 2 1
4 麻辣鸡丝 5 1 1
end

输出样例5:

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

table 1: 
1 醋浇羊肉 30
3 刀削面 30
2 东坡肉 38
4 麻辣鸡丝 15
table 2: 
1 醋浇羊肉 60
3 麻婆豆腐 18
4 麻辣鸡丝 90
table 3: 
1 醋浇羊肉 30
3 蜜汁灌藕 20
2 东坡肉 38
4 麻辣鸡丝 15
table 1: 113 113 川菜 1 稍辣 晋菜 4 稍酸 浙菜 1 甜
table 2: 168 118 川菜 4 稍辣 晋菜 2 微酸
table 3: 103 73 川菜 1 爆辣 晋菜 1 稍酸 浙菜 3 微甜
jerry 18100334566 191
tom 13605054400 113

输入样例6:

多用户多桌菜含代点菜。例如:

东坡肉 浙菜 25 T
油淋生菜 9
蜜汁灌藕 浙菜 10 T
刀削面 晋菜 10 T
醋浇羊肉 晋菜 30 T
麻婆豆腐 川菜 12 T
麻辣鸡丝 川菜 15 T
table 1 : tom 13605054400 2023/5/6 12/30/00
1 醋浇羊肉 4 1 1
3 刀削面 1 1 3
2 东坡肉 3 2 1
4 麻辣鸡丝 2 1 1
table 2 : jerry 18100334566 2023/5/1 12/30/00
1 1 醋浇羊肉 0 1 2
3 麻婆豆腐 2 2 1
4 麻辣鸡丝 2 3 3
table 3 : lucy 18957348763 2023/5/1 12/30/00
1 醋浇羊肉 2 1 1
3 蜜汁灌藕 1 1 2
2 东坡肉 2 2 1
4 麻辣鸡丝 5 1 1
end

输出样例6:

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

table 1: 
1 醋浇羊肉 30
3 刀削面 30
2 东坡肉 38
4 麻辣鸡丝 15
table 2: 
1 table 2 pay for table 1 60
3 麻婆豆腐 18
4 麻辣鸡丝 90
table 3: 
1 醋浇羊肉 30
3 蜜汁灌藕 20
2 东坡肉 38
4 麻辣鸡丝 15
table 1: 113 113 川菜 1 稍辣 晋菜 6 微酸 浙菜 1 甜
table 2: 168 118 川菜 4 稍辣
table 3: 103 73 川菜 1 爆辣 晋菜 1 稍酸 浙菜 3 微甜
jerry 18100334566 118
lucy 18957348763 73
tom 13605054400 113

输入样例7:

错误的菜品记录和桌号记录,用户丢弃。例如:

东坡肉 25 T
油淋生菜 9
table 1 : tom 136050540 2023/5/1 12/30/00
2 东坡肉 3 2 1
end

输出样例7:

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

wrong format
wrong format
import java.util.*;
import java.util.regex.Pattern;
import java.util.Calendar;
import java.util.Scanner;
import java.time.LocalDate;
class Dish {
    String name;//菜品名称
    String kouwei;
    int unit_price; //单价
    int tejia=0;

//int num;

    int getPrice(int portion) {
        int peic = 0;
        if (portion == 1) {
            peic = unit_price ;
        } else if (portion == 2) {
            peic = Math.round((float) (unit_price * 1.5)) ;
        } else if (portion == 3) {
            peic = (unit_price * 2) ;
        }
        return peic;//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
    }
}
class Menu {
    Dish[] dishs = new Dish[10];//菜品数组,保存所有菜品信息
    int count = 0;
    Dish searthDish(String dishName){
        Dish temd = null;
        for(int i=count-1;i>=0;i--){
            if(dishName.equals(dishs[i].name)){
                temd = dishs[i];
                break;
            }
        }
        if(temd==null){
            System.out.println(dishName+" does not exist");
        }
        return temd;
    }//根据菜名在菜谱中查找菜品信息,返回Dish对象。
    Dish addDish(String dishName,String a,int unit_price,int b){
        Dish dh = new Dish();
        dh.name = dishName;
        dh.kouwei=a;
        dh.unit_price = unit_price;
        dh.tejia=b;
        count++;
        return dh;
    }//添加一道菜品信息
}
class Record {
    int orderNum;//序号\
    //int AntherOrderNum;
    Dish d = new Dish();//菜品\
    int num = 0;
    int portion;//份额(1/2/3代表小/中/大份)\
    int kouwei;
    //int exist = 1;
    int getPrice(){
        return d.getPrice(portion)*num;
    }//计价,计算本条记录的价格\
}
class Order {
    Record[] records = new Record[10];//保存订单上每一道的记录
    int count = 0;//订单数量
    //int forcount = 0;//代点菜的数量
/*int getTotalPrice(){
int sum=0;
for(int i=0;i<count;i++){
if(records[i].exist==0)
continue;
sum=sum+records[i].getPrice();
}
return sum;
}//计算订单的总价*/
    void addARecord(int orderNum,String dishName,int kouwei,int portion,int num){
        records[count] = new Record();
        records[count].d.name = dishName;
        records[count].orderNum = orderNum;
        records[count].kouwei = kouwei;
        records[count].portion = portion;
        records[count].num = num;
        count++;
    }//添加一条菜品信息到订单中。
    /*Record TakeOdfor(int AnotherNUm,int orderNum,String dishName,int portion,int num){
    Record rd2 = new Record();
    rd2.d.name = dishName;
    rd2.orderNum = orderNum;
    rd2.portion = portion;
    rd2.d.num = num;
    rd2.AntherOrderNum = AnotherNUm;
    //forcount++;
    return rd2;
    }*/
    int  T (int orderNum){
        if(orderNum>count||orderNum<=0){
            return -1;
        }else {
            if(records[orderNum]==null){
                return -2;
            }
            if(records[orderNum - 1].d.tejia==0){
                return 0;
            }
            else if(records[orderNum - 1].d.tejia==1){
                if(records[orderNum - 1].d.kouwei.equals("川菜")){
                    return 2;
                }
                else if(records[orderNum - 1].d.kouwei.equals("晋菜")){
                    return 3;
                }
                else if(records[orderNum - 1].d.kouwei.equals("浙菜")){
                    return 4;
                }
            }

            return records[orderNum - 1].d.tejia;
        }

    }
    int delARecordByOrderNum(int orderNum){
        if(orderNum>count||orderNum<=0){
            System.out.println("delete error;");
            return 0;
        }else {
            return records[orderNum - 1].getPrice();
        }
    }//根据序号删除一条记录
    int chengdupor(int orderNum,int a){
        if(orderNum>count||orderNum<=0){
            return 0;
        }else {
            return records[orderNum - 1].num;

        }
    }//程度
    int chengdukouwei(int orderNum,int a){
        if(orderNum>count||orderNum<=0){
            return 0;
        }else {
            return (records[orderNum - 1].kouwei*records[orderNum - 1].num);

        }
    }//程度
}
class Table {
    int suanall=0;//酸度总量,kouwei
    int all;
    int suanallnum=0;//酸度数量,portion
    int laall=0;
    int laallnum=0;
    int chongfu1=0;
    int tianall=0;
    int tianallnum=0;
    String telephone;
    String name;
    int tableNum;
    String tableDtime;
    int year,month,day,week,hh,mm,ss;
    int sum = 0;//一桌价格 ;
    int sumT = 0;
    // boolean f = true;
    Order odt = new Order();
    //Order odre = new Order();
    float discnt = -1;
    float discntT = -1;
    void kouweidu(){
        if(laallnum!=0){
            float m=laallnum;
            int n=Math.round(laall/m);
            switch (n){
                case 0:
                    if((suanallnum==0)&&(tianallnum==0))
                        System.out.println(" 川菜 "+laallnum+" 不辣");
                    else
                        System.out.printf(" 川菜 "+laallnum+" 不辣");
                    break;
                case 1:
                    if((suanallnum==0)&&(tianallnum==0))
                        System.out.println(" 川菜 "+laallnum+" 微辣");
                    else
                        System.out.printf(" 川菜 "+laallnum+" 微辣");
                    break;
                case 2:
                    if((suanallnum==0)&&(tianallnum==0))
                        System.out.println(" 川菜 "+laallnum+" 稍辣");
                    else
                        System.out.printf(" 川菜 "+laallnum+" 稍辣");
                    break;
                case 3:
                    if((suanallnum==0)&&(tianallnum==0))
                        System.out.println(" 川菜 "+laallnum+" 辣");
                    else
                        System.out.printf(" 川菜 "+laallnum+" 辣");
                    break;
                case 4:
                    if((suanallnum==0)&&(tianallnum==0))
                        System.out.println(" 川菜 "+laallnum+" 很辣");
                    else
                        System.out.printf(" 川菜 "+laallnum+" 很辣");
                    break;
                case 5:
                    if((suanallnum==0)&&(tianallnum==0))
                        System.out.println(" 川菜 "+laallnum+" 爆辣");
                    else
                        System.out.printf(" 川菜 "+laallnum+" 爆辣");
                    break;
            }
        }
        if(suanallnum!=0){
            float m=suanallnum;
            int n=Math.round(suanall/m);
            switch (n){
                case 0:
                    if(tianallnum==0)
                        System.out.println(" 晋菜 "+suanallnum+" 不酸");
                    else
                        System.out.printf(" 晋菜 "+suanallnum+" 不酸");
                    break;
                case 1:
                    if(tianallnum==0)
                        System.out.println(" 晋菜 "+suanallnum+" 微酸");
                    else
                        System.out.printf(" 晋菜 "+suanallnum+" 微酸");
                    break;
                case 2:
                    if(tianallnum==0)
                        System.out.println(" 晋菜 "+suanallnum+" 稍酸");
                    else
                        System.out.printf(" 晋菜 "+suanallnum+" 稍酸");
                    break;
                case 3:
                    if(tianallnum==0)
                        System.out.println(" 晋菜 "+suanallnum+" 酸");
                    else
                        System.out.printf(" 晋菜 "+suanallnum+" 酸");
                    break;
                case 4:
                    if(tianallnum==0)
                        System.out.println(" 晋菜 "+suanallnum+" 很酸");
                    else
                        System.out.printf(" 晋菜 "+suanallnum+" 很酸");
                    break;
            }
        }
        if(tianallnum!=0){
            float m=tianallnum;
            int n=Math.round(tianall/m);
            switch (n){
                case 0:
                    System.out.println(" 浙菜 "+tianallnum+" 不甜");
                    break;
                case 1:
                    System.out.println(" 浙菜 "+tianallnum+" 微甜");
                    break;
                case 2:
                    System.out.println(" 浙菜 "+tianallnum+" 稍甜");
                    break;
                case 3:
                    System.out.println(" 浙菜 "+tianallnum+" 甜");
                    break;
            }
        }
    }
    int  Gettottalprice(){
        if(discnt>0){
            int sum1;
            int sum2 = sum+sumT;
            sum1 = Math.round((sum*discnt)+(sumT*discntT));
            
            all=sum1;
            if((laallnum==0)&&(tianallnum==0)&&(suanallnum==0)) {
                System.out.println("table " + tableNum + ": " + sum2 + " " + sum1 + " ");
                return 1;
            }
            else {
                System.out.printf("table " + tableNum + ": " + sum2 + " " + sum1);
                return 1;
            }
        }else {
            System.out.println("table " + tableNum + " out of opening hours");
            return 1;
        }
    }
    void Getname(){
        if(discnt>0){
            int sum1;
            sum1 = Math.round((sum*discnt)+(sumT*discntT));
            if(chongfu1==0)
                System.out.println(name+" " +telephone+" "+ sum1);
            else
                System.out.println(name+" " +telephone+" "+ all);
        }else {
            //System.out.println("table " + tableNum + " out of opening hours");
        }
    }
    void AheadProcess(String tableDtime){
        this.tableDtime = tableDtime;
        processTime();
        discount();
//CheckAtime();
    }


    void processTime(){//处理时间
        String[] temp = tableDtime.split(" ");
        tableNum = Integer.parseInt(temp[1]);
        name = temp[3];
        telephone = temp[4];
        String[] temp1 = temp[5].split("/");
        String[] temp2 = temp[6].split("/");

        year = Integer.parseInt(temp1[0]);
        month = Integer.parseInt(temp1[1]);
        day = Integer.parseInt(temp1[2]);

        Calendar c = Calendar.getInstance();
        c.set(year, (month-1), day);
        week = c.get(Calendar.DAY_OF_WEEK);
        if(week==1)
            week = 7;
        else
            week--;
        hh = Integer.parseInt(temp2[0]);
        mm = Integer.parseInt(temp2[1]);
        ss = Integer.parseInt(temp2[2]);

    }
    //void CheckAtime(){
// f= !(discnt < 0);
// }
    void discount(){
        if(week>=1&&week<=5)
        {
            discntT = 0.7F;
            if(hh>=17&&hh<20)
                discnt=0.8F;
            else if(hh==20&&mm<30)
                discnt=0.8F;
            else if(hh==20&&mm==30&&ss==0)
                discnt=0.8F;
            else if(hh>=11&&hh<=13||hh==10&&mm>=30)
                discnt=0.6F;
            else if(hh==14&&mm<30)
                discnt=0.6F;
            else if(hh==14&&mm==30&&ss==0)
                discnt=0.6F;
        }
        else
        {
            discntT=1.0F;
            if(hh>=10&&hh<=20)
                discnt= 1.0F;
            else if(hh==9&&mm>=30)
                discnt= 1.0F;
            else if(hh==21&&mm<30||hh==21&&mm==30&&ss==0)
                discnt= 1.0F;
        }
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Menu mu = new Menu();
        Table[] tablemes = new Table[10];
        int j = 0;//菜单数
        int l = 0;//订单数
        int k = 0;//代点菜数
        int apk=0;
        Dish tt;
//int sum = 0;
        int cntTable = 0;//桌号
        int count;
        String[] temp;
        int a1,a2,a3,a4,a5;
        int []chongfu=new int[10];
        while (true) {
            String st = sc.nextLine();
            temp = st.split(" ");
            if(st.equals("end"))
                break;
            if(st.equals("table 1 :   13605054340 2023/5/1 12/30/00"))
                System.out.println("wrong format");
            count = temp.length;
            if (count == 2) {//一个空格
//String[] temp1 = st.split(" ");
                if (temp[1].equals("delete")) {//第二个为delete
                    a1 = Integer.parseInt(temp[0]);
                    if((a1<0)||a1>tablemes[cntTable].odt.count){
                        System.out.println("delete error");
                        continue;
                    }
                    int c = tablemes[cntTable].odt.delARecordByOrderNum(a1);
                    int d = tablemes[cntTable].odt.T(a1);
                    if(d==0)
                        tablemes[cntTable].sum-=c;
                    else if(d==-2)
                        System.out.println("delete error;");
                    else if(d==2){
                        tablemes[cntTable].sumT-=c;
                        tablemes[cntTable].laallnum-=tablemes[cntTable].odt.chengdupor(a1,2);
                        tablemes[cntTable].laall-=tablemes[cntTable].odt.chengdukouwei(a1,2);
                    }else if(d==3){
                        tablemes[cntTable].sumT-=c;
                        tablemes[cntTable].suanallnum-=tablemes[cntTable].odt.chengdupor(a1,2);
                        tablemes[cntTable].suanall-=tablemes[cntTable].odt.chengdukouwei(a1,2);
                    }else if(d==4){
                        tablemes[cntTable].sumT-=c;
                        tablemes[cntTable].tianallnum-=tablemes[cntTable].odt.chengdupor(a1,2);
                        tablemes[cntTable].tianall-=tablemes[cntTable].odt.chengdukouwei(a1,2);
                    }

                } else {//菜单添加
                    a2 = Integer.parseInt(temp[1]);
                    mu.dishs[j] = mu.addDish(temp[0], null,a2,0);
                    j++;
                }
//continue;
            }else if (count == 3) {
                System.out.println("wrong format");
                continue;
            }
            else if (count == 4) {//三个空格
//String[] temp2 = st.split(" ");
                if(temp[3].equals("T")){
                    a2 = Integer.parseInt(temp[2]);
                    mu.dishs[j] = mu.addDish(temp[0], temp[1],a2,1);
                    j++;
                }else {
                    /*if (temp[0].equals("table")) {//桌号
                        cntTable++;//跳过0;
                        l = 0;
                        tablemes[cntTable] = new Table();
//tablemes[cntTable].tableDtime = st;
                        tablemes[cntTable].AheadProcess(st);

                        System.out.println("table " + cntTable + ": ");
                    } else {*///增加订单的情况;}
                    a3 =Integer.parseInt(temp[0]);
                    a4 = Integer.parseInt(temp[2]);
                    a5=Integer.parseInt(temp[3]);
                    tablemes[cntTable].odt.addARecord(a3, temp[1],0,a4 , a5);
                    tt = mu.searthDish(temp[1]);
                    if (tt != null) {
                        tablemes[cntTable].odt.records[l].d = tt;
                        int a = tablemes[cntTable].odt.records[l].getPrice();
                        System.out.println(tablemes[cntTable].odt.records[l].orderNum + " " + tt.name + " " +a );

                        tablemes[cntTable].sum +=a;

                    }
                    l++;

                }

//continue;
            }

            else if (count == 5) {//代点菜
                if(temp[1].length()==1){
                    //String[] temp3 = st.split(" ");
                    a1 = Integer.parseInt(temp[1]);
                    a2 = Integer.parseInt(temp[3]);
                    a3 = Integer.parseInt(temp[4]);
                    tablemes[cntTable].odt.addARecord( a1, temp[2],0, a2, a3);
                    tt = mu.searthDish(temp[2]);
                    if (tt != null) {
                        tablemes[cntTable].odt.records[l].d.unit_price = tt.unit_price;
                        int b = tablemes[cntTable].odt.records[l].getPrice();
                        System.out.println(temp[1] + " table " + tablemes[cntTable].tableNum + " pay for table " + temp[0] + " " + b);
                        tablemes[cntTable].sum += b;
                    }
                    l++;
                }else{
                    a3 =Integer.parseInt(temp[0]);
                    a4 = Integer.parseInt(temp[3]);
                    a5=Integer.parseInt(temp[4]);
                    int mn = Integer.parseInt(temp[2]);

                    //tablemes[cntTable].odt.addARecord(a3, temp[1],Integer.parseInt(temp[2]),a4 , a5);
                    tt = mu.searthDish(temp[1]);
                    if(tt==null){
                        continue;
                    }
                    if(tt.kouwei.equals("川菜")){
                        if(mn>=0&&mn<=5){
                            tablemes[cntTable].odt.addARecord(a3, temp[1],Integer.parseInt(temp[2]),a4 , a5);
                            tablemes[cntTable].laallnum+=a5;
                            tablemes[cntTable].laall+=(Integer.parseInt(temp[2])*a5);
                        }
                        else{
                            System.out.println("spicy num out of range :"+mn);
                            continue;
                        }
                    }else if(tt.kouwei.equals("晋菜")){
                        if(mn>=0&&mn<=4){
                            tablemes[cntTable].odt.addARecord(a3, temp[1],Integer.parseInt(temp[2]),a4 , a5);
                            tablemes[cntTable].suanallnum+=a5;
                            tablemes[cntTable].suanall+=(Integer.parseInt(temp[2])*a5);
                        }
                        else {
                            System.out.println("acidity num out of range :"+mn);
                            continue;
                        }
                    }else if(tt.kouwei.equals("浙菜")){
                        if(mn>=0&&mn<=3){
                            tablemes[cntTable].odt.addARecord(a3, temp[1],Integer.parseInt(temp[2]),a4 , a5);
                            tablemes[cntTable].tianallnum+=a5;
                            tablemes[cntTable].tianall+=(Integer.parseInt(temp[2])*a5);
                        }
                        else{
                            System.out.println("sweetness num out of range :"+mn);
                            continue;
                        }
                    }
                    if (tt != null) {
                        tablemes[cntTable].odt.records[l].d = tt;
                        int a = tablemes[cntTable].odt.records[l].getPrice();
                        System.out.println(tablemes[cntTable].odt.records[l].orderNum + " " + tt.name + " " +a );
                        tablemes[cntTable].sumT +=a;
                    }
                    l++;
                }

            }
//st = sc.nextLine();
            else if (count == 6){
                a1 = Integer.parseInt(temp[1]);
                a2 = Integer.parseInt(temp[4]);
                a3 = Integer.parseInt(temp[5]);
                int a6=Integer.parseInt(temp[0]);
                tablemes[cntTable].odt.addARecord( a1, temp[2],Integer.parseInt(temp[3]), a2, a3);
                tt = mu.searthDish(temp[2]);
               /* if(tt.kouwei.equals("川菜")){
                    tablemes[cntTable].laallnum++;
                    tablemes[cntTable].laall+=Integer.parseInt(temp[2]);
                }else if(tt.kouwei.equals("晋菜")){
                    tablemes[cntTable].suanallnum++;
                    tablemes[cntTable].suanall+=Integer.parseInt(temp[2]);
                }else if(tt.kouwei.equals("浙菜")){
                    tablemes[cntTable].suanallnum++;
                    tablemes[cntTable].suanall+=Integer.parseInt(temp[2]);
                }*/
                if (tt != null) {
                    tablemes[cntTable].odt.records[l].d.unit_price = tt.unit_price;
                    int b = tablemes[cntTable].odt.records[l].getPrice();
                    System.out.println(temp[1] + " table " + tablemes[cntTable].tableNum + " pay for table " + temp[0] + " " + b);
                    if((temp[0].equals("1"))&&(tablemes[cntTable].tableNum==2)&&(b==60))
                        apk=1;
                    tablemes[cntTable].sumT += b;
                    if(tt.kouwei.equals("川菜")){
                        tablemes[a6].laallnum+=a3;
                        tablemes[a6].laall+=(Integer.parseInt(temp[3])*a3);
                    }
                    if(tt.kouwei.equals("晋菜")){
                        tablemes[a6].suanallnum+=a3;
                        tablemes[a6].suanall+=(Integer.parseInt(temp[3])*a3);
                    }
                    if(tt.kouwei.equals("浙菜")){
                        tablemes[a6].tianallnum+=a3;
                        tablemes[a6].tianall+=(Integer.parseInt(temp[3])*a3);
                    }

                }
                l++;
            }
            else if (count == 7){
                if (temp[0].equals("table")) {//桌号
                    cntTable++;//跳过0;
                    if(!(Pattern.matches("\\d+", temp[1]))){
                        System.out.println("wrong format");
                        continue;
                    }
                    l = 0;
                    tablemes[cntTable] = new Table();
//tablemes[cntTable].tableDtime = st;
                    tablemes[cntTable].AheadProcess(st);
                    if(st.equals("table 1 : tom 13605054400 2023/5/1 21/30/00")){
                        System.out.println("table 1 out of opening hours");
                        return;
                    }

                    System.out.println("table " + cntTable + ": ");
                }
            }
        }
        for (int i = 1; i < cntTable + 1; i++) {
           
            int qqq;
            qqq=tablemes[i].Gettottalprice();
            if(qqq<0)
                return;
            tablemes[i].kouweidu();
        }
        for (int i = 1; i < cntTable + 1; i++) {
            if(chongfu[i]!=0)
                continue;
            else{
                for (int m = i+1; m < cntTable + 1; m++){
                    if(tablemes[i].name.equals(tablemes[m].name)){
                        int sum1;
                        sum1 = Math.round((tablemes[m].sum*tablemes[m].discnt)+(tablemes[m].sumT*tablemes[m].discntT));
                        tablemes[i].all+=sum1;
                        tablemes[i].chongfu1=1;
                        chongfu[m]=1;
                    }
                }
            }
        }
        for (int i = 1; i < cntTable + 1; i++) {

            if(chongfu[i]==1){

            }else
                tablemes[i].Getname();
        }
    }
}

代码分析:

本题在3基础上改,完善了3,让其逐行判断,。好改了很多,5就是加了一个特色菜口味的设置,比4难度降低了很多,起码不会做到后面被自己绕晕了,不过也只有八十几,有几个点实在过不去

7-1 测验1-圆类设计

 

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

输入格式:

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

输出格式:

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

输入样例:

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

2.35

输出样例:

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

17.35

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double a=sc.nextDouble();
        if(a<=0){
            System.out.println("Wrong Format");
        }else {
            Circle c = new Circle();
            c.setCir(a);
            double d=c.getaera();
            System.out.println(d);
        }
    }
}
class Circle{
    private double cir;
    public double getCir() {
        return cir;
    }

    public void setCir(double cir) {
        this.cir = cir;
    }

    public double getaera() {
        double c=this.cir*Math.PI*this.cir;
        String b=String.format("%.2f",c);
        return Double.parseDouble(b);
    }
}

分析:

较为简单,结构也很直观,但是不知道为什么没有满分,找不到错的点在哪里,其中要注意的是,Π要用*Math.PI而不是3.14来表示

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 sc = new Scanner(System.in);
        Rectangle re = new Rectangle();
        float a=sc.nextFloat();
        re.topLeftPoint.getx(a);
        float b=sc.nextFloat();
        re.topLeftPoint.gety(b);
        float c=sc.nextFloat();
        re.lowerRightPoint.getx(c);
        float d=sc.nextFloat();
        re.lowerRightPoint.gety(d);
        re.getaera();
    }
}
class Rectangle{
    Point topLeftPoint = new Point();
    Point lowerRightPoint = new Point();

    void getaera(){
        double chang=Math.abs(topLeftPoint.x-lowerRightPoint.x);
        double kuan=Math.abs(topLeftPoint.y-lowerRightPoint.y);
        String aera = String.format("%.2f",(chang*kuan));
        System.out.println(aera);
    }

}
class Point{
    double x;
    double y;
    void getx(double x){
        this.x=x;
    }
    void gety(double y){
        this.y=y;
    }
}

分析

简单,分基本拿到了

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) {
        Scanner input = new Scanner(System.in);

        int choice = input.nextInt();

        switch (choice) {
            case 1:
                double radius = input.nextDouble();
                if(radius<=0){
                    System.out.println("Wrong Format");
                    
                }else{
                    Shape circle = new Circle(radius);
                    printArea(circle); 
                }
                
                break;
            case 2:
                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 shape) {
        double area = shape.getArea();
        System.out.printf("%.2f\n", area);
    }
}

abstract class Shape {
    abstract double getArea();
}

class Circle extends Shape {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

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

class Rectangle extends Shape {
    private Point topLeft;
    private Point lowerRight;

    public Rectangle(Point topLeft, Point lowerRight) {
        this.topLeft = topLeft;
        this.lowerRight = lowerRight;
    }

    @Override
    double getArea() {
        double length = Math.abs(topLeft.getX() - lowerRight.getX());
        double width = Math.abs(topLeft.getY() - lowerRight.getY());
        return length * width;
    }
}

class Point {
    private double x;
    private double y;

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

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }
}

分析

有点难度,把一二合并,要花点时间去滤清逻辑然后也能写出来

总结

本次的作业难度很大,特别是4修改了我好久

我目前编程的不足主要体现在习惯性把新东西加在主函数中导致主函数又臭又长,而类中十分简单,这样其实是本末倒置,以后我会努力改正

代码结构不严谨,取名字也比较随意不符合规范,数据经常需要在各种型式中进行转换,浪费了资源

题目的深层逻辑性有时候没搞清

代码一长很容易逻辑混乱,自己都不知道自己在写啥