首页 > 其他分享 >BLOG2

BLOG2

时间:2023-11-19 17:22:05浏览次数:28  
标签:String int BLOG2 words 菜品 table public

一、前言:

这三次大作业难度都很大,且逐渐递增!!!期中考试难度也挺大的,选择题几乎都不太会,编程题倒是好一些给了类图,但是对于继承关系和对于接口的使用还是不太熟练,琢磨了很久。

二、设计与分析:

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
关于这道题,我主要设计了以下四个类。
Dish(菜品)类:表示一个菜品,具有菜名和单价属性。它提供了根据点菜的份额计算价格的方法。Menu(菜谱)类:对应菜谱,包含饭店提供的所有菜的信息。它包含一个菜品数组,用于保存所有菜品信息。它提供了根据菜名在菜谱中查找菜品信息的方法,以及添加菜品信息到菜谱的方法。Record(点菜记录)类:保存订单上的一道菜品记录。它包含序号、菜品、份额和份数属性。它提供了计算本条记录的价格的方法。Order(订单)类:保存用户点的所有菜的信息。它包含一个记录数组,用于保存订单上的每一道记录。它提供了添加菜品信息到订单中的方法,计算订单的总价的方法,根据序号在订单中查找菜品信息的方法,以及根据序号删除一条记录的方法。Main(主类):包含主方法,用于与用户交互。它创建了一个订单对象和一个菜谱对象,并通过用户输入来添加菜品信息到订单或菜谱,删除订单中的记录,计算订单的总价,并输出结果。

这些类之间的关系如下:Main类与Order类和Menu类进行交互,调用它们的方法。Order类与Record类和Menu类进行交互,通过记录数组保存点菜记录,并调用菜谱的方法来查找菜品信息。Menu类与Dish类进行交互,通过菜品数组保存菜品信息,并提供方法来添加菜品和查找菜品。Record类与Dish类进行交互,通过菜品对象保存菜品信息,并计算本条记录的价格。

具体关系如下类图:

 实际设计如下:

  1 package PTA4_4;
  2 
  3 import java.math.BigInteger;
  4 import java.util.Scanner;
  5 //菜品类
  6 class Dish {
  7     String name;//菜名
  8     int unit_price;//单价
  9 
 10     public Dish(String name, int unit_price) {
 11         this.name = name;
 12         this.unit_price = unit_price;
 13     }
 14 
 15     // 根据点菜的份额计算价格
 16     public long getPrice(int portion) {
 17         double price = unit_price;
 18         if (portion == 2) {
 19             price *= 1.5;
 20         } else if (portion == 3) {
 21             price *= 2;
 22         }
 23         return Math.round(price);
 24     }
 25 }
 26 //菜谱类:对应菜谱,包含饭店提供的所有菜的信息
 27 class Menu {
 28     Dish [] dishes ;//菜品数组,保存所有菜品信息
 29 
 30     public Menu(){
 31         this.dishes = new Dish[100];
 32     }
 33 
 34     // 根据菜名在菜谱中查找菜品信息
 35     public Dish searchDish(String dishName) {
 36         for (Dish dish : dishes) {
 37             if (dish != null && dish.name.equals(dishName)) {
 38                 return dish;
 39             }
 40         }
 41         return null;
 42     }
 43 
 44     //添加一道菜品信息
 45     public void addDish(String dishName, int unit_price){
 46         Dish dish = new Dish(dishName, unit_price);
 47         for (int i = 0; i < dishes.length; i++) {
 48             if (dishes[i] != null && dishes[i].name.equals(dishName)) {
 49                 dishes[i] = dish;
 50                 return ;
 51             }
 52         }
 53 
 54         for (int i = 0; i < dishes.length; i++) {
 55             if (dishes[i] == null) {
 56                 dishes[i] = dish;
 57                 break;
 58             }
 59         }
 60     }
 61 }
 62 //点菜记录类:保存订单上的一道菜品记录
 63 class Record {
 64     int orderNum;//序号
 65     Dish dish;//菜品
 66     int portion;//份额(1、2、3代表小、中、大份)
 67     int num;//份数
 68 
 69     //计价
 70     public Record(int oderNum, Dish dish, int portion, int num) {
 71         this.orderNum = oderNum;
 72         this.dish = dish;
 73         this.portion = portion;
 74         this.num = num;
 75     }
 76 
 77     // 计算本条记录的价格
 78     public long getPrice() {
 79         if(dish != null){
 80             return dish.getPrice(portion) * num;
 81         }
 82         return 0;
 83     }
 84 }
 85 //订单类:保存用户点的所有菜的信息。
 86 class Order {
 87     Record [] records;//保存订单上的每一道记录
 88 
 89     public Order() {
 90         this.records =  new Record[100]; //创建对象
 91     }
 92 
 93     // 添加一条菜品信息到订单中
 94     public void addARecord(int orderNum, String dishName, int portion, int num, Menu menu) {
 95         Dish dish = menu.searchDish(dishName);
 96         Record record = new Record(orderNum, dish, portion, num);
 97 
 98         int index = 0;
 99         while (index < records.length && records[index] != null) {
100             index++;
101         }
102 
103         records[index] = record;
104 
105         //如果菜名不存在
106         if (dish == null) {
107             System.out.println(dishName + " does not exist");
108         }
109         else{
110             System.out.println(orderNum + " " + dishName + " " + record.getPrice());
111         }
112     }
113 
114     // 计算订单的总价
115     public BigInteger getTotalPrice() {
116         BigInteger totalPrice = new BigInteger("0");
117         for (Record record : records) {
118             if (record != null) {
119                 totalPrice = totalPrice.add(new BigInteger(String.valueOf(record.getPrice())));
120             }
121         }
122         return totalPrice;
123     }
124 
125     // 根据序号在订单中查找菜品信息
126     public Record searchDishNum(int orderNum) {
127         for (Record record : records) {
128             if (record != null && record.orderNum == orderNum) {
129                 return record;
130             }
131         }
132         return null;
133     }
134 
135     //根据序号删除一条记录
136     public void delARecordByOrderNum(int orderNum){
137         for (int i = 0; i < records.length; i++) {
138             if (records[i] != null && records[i].orderNum == orderNum) {
139                 records[i].num = 0;
140                 break;
141             }
142         }
143     }
144 }
145 
146 public class Main {
147     public static void main(String[] args) {
148         Scanner input = new Scanner(System.in);
149         Order order = new Order();
150         Menu menu = new Menu();
151 
152 
153         while(input.hasNext()){
154             String str = input.nextLine();
155             if(str.equals("end")){
156                 break;
157             }
158             String [] words = str.split(" ");
159             if(words.length == 2){
160                 if(words[1].equals("delete")){
161                     int orderNum = Integer.parseInt(words[0]);
162                     Record record = order.searchDishNum(orderNum);
163                     if (record == null) {
164                         System.out.println("delete error;");
165                     } else {
166                         order.delARecordByOrderNum(orderNum);
167                     }
168                 }
169                 else{
170                     menu.addDish(words[0], Integer.parseInt(words[1]));
171                 }
172             }
173             else{
174                 order.addARecord(Integer.parseInt(words[0]), words[1], Integer.parseInt(words[2]), Integer.parseInt(words[3]),menu);
175             }
176         }
177 
178         System.out.print(order.getTotalPrice().intValue());
179     }
180 }

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
这道题是基于上一道题增加了桌号信息及代点菜功能,本来是想增加一个table类,但是思考以后还是打算将桌号和点菜信息视作订单信息处理,故整体的设计结构没有很大变化,但是最后提交的时候,总是有随机几个测试点显示运行超时,改了很久也没有解决这个问题。 

 实现过程:

package PTA4_1;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Scanner;
//菜品类
class Dish {
    String name;//菜名
    int unit_price;//单价

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

    // 根据点菜的份额计算价格
    public long getPrice(int portion) {
        double price = unit_price;
        if (portion == 2) {
            price *= 1.5;
        } else if (portion == 3) {
            price *= 2;
        }
        return Math.round(price);
    }
}
//菜谱类:对应菜谱,包含饭店提供的所有菜的信息
class Menu {
    Dish [] dishes ;//菜品数组,保存所有菜品信息

    public Menu(){
        this.dishes = new Dish[100];
    }

    // 根据菜名在菜谱中查找菜品信息
    public Dish searchDish(String dishName) {
        for (Dish dish : dishes) {
            if (dish != null && dish.name.equals(dishName)) {
                return dish;
            }
        }
        return null;
    }

    //添加一道菜品信息
    public void addDish(String dishName, int unit_price){
        Dish dish = new Dish(dishName, unit_price);
        for (int i = 0; i < dishes.length; i++) {
            if (dishes[i] != null && dishes[i].name.equals(dishName)) {
                dishes[i] = dish;
                return ;
            }
        }

        for (int i = 0; i < dishes.length; i++) {
            if (dishes[i] == null) {
                dishes[i] = dish;
                break;
            }
        }
    }
}
//点菜记录类:保存订单上的一道菜品记录
class Record {
    int orderNum;//序号
    Dish dish;//菜品
    int portion;//份额(1、2、3代表小、中、大份)
    int num;//份数

    //计价
    public Record(int oderNum, Dish dish, int portion, int num) {
        this.orderNum = oderNum;
        this.dish = dish;
        this.portion = portion;
        this.num = num;
    }

    // 计算本条记录的价格
    public long getPrice() {
        if(dish != null){
            return dish.getPrice(portion) * num;
        }
        return 0;
    }
}
//订单类:保存用户点的所有菜的信息。
class Order {
    int table; // 桌号
    Record [] records;//保存订单上的每一道记录

    double discount = -1.0; //折扣,不同时间段折扣不同

    String tableDtime;//时间
    int year,month,day,week,hh,mm,ss;


    public Order() {
        this.records =  new Record[100]; //创建对象
    }

    // 添加一条菜品信息到订单中
    public Record addARecord(int orderNum, String dishName, int portion, int num, Menu menu) {
        Dish dish = menu.searchDish(dishName);
        Record record = new Record(orderNum, dish, portion, num);

        int index = 0;
        while (index < records.length && records[index] != null) {
            index++;
        }

        records[index] = record;

        //如果菜名不存在
        if (dish == null) {
            System.out.println(dishName + " does not exist");
        }
        else{
            System.out.println(orderNum + " " + dishName + " " + record.getPrice());
        }
        return record;
    }

    // 计算订单的总价
    public BigInteger getTotalPrice() {
        BigInteger totalPrice = new BigInteger("0");
        for (Record record : records) {
            if (record != null) {
                totalPrice = totalPrice.add(new BigInteger(String.valueOf(record.getPrice())));
            }
        }
        return totalPrice;
    }

    public void printAllPrice(){
        if(discount>0){
            System.out.println("table " + table + ": " + Math.round(this.getTotalPrice().intValue() * discount));
        }else {
            System.out.println("table " + table + " out of opening hours");
        }
    }

    // 根据序号在订单中查找菜品信息
    public Record searchDishNum(int orderNum) {
        for (Record record : records) {
            if (record != null && record.orderNum == orderNum) {
                return record;
            }
        }
        return null;
    }

    //根据序号删除一条记录
    public void delARecordByOrderNum(int orderNum){
        for (int i = 0; i < records.length; i++) {
            if (records[i] != null && records[i].orderNum == orderNum) {
                records[i].num = 0;
                break;
            }
        }
    }

    void processTime(){//处理时间
        String[] temp = tableDtime.split(" ");
        String[] temp1 = temp[0].split("/");
        String[] temp2 = temp[1].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 discount(){
        if(week>=1&&week<=5)
        {
            if(hh>=17&&hh<20)
                discount=0.8F;
            else if(hh==20&&mm<30)
                discount=0.8F;
            else if(hh==20&&mm==30&&ss==0)
                discount=0.8F;
            else if(hh>=11&&hh<=13||hh==10&&mm>=30)
                discount=0.6F;
            else if(hh==14&&mm<30)
                discount=0.6F;
            else if(hh==14&&mm==30&&ss==0)
                discount=0.6F;
        }
        else
        {
            if(hh>=10&&hh<=20)
                discount= 1.0F;
            else if(hh==9&&mm>=30)
                discount= 1.0F;
            else if(hh==21&&mm<30||hh==21&&mm==30&&ss==0)
                discount= 1.0F;
        }
    }

    public int getTable() {
        return table;
    }

    public void setTable(int table) {
        this.table = table;
    }

    public double getDiscount() {
        return discount;
    }

    public void setDiscount(double discount) {
        this.discount = discount;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Menu menu = new Menu();
        List<Order> orderList = new ArrayList<>();

        while(input.hasNext()){
            String str = input.nextLine();
            Order order = null;
            if(orderList.size() != 0){
                order = orderList.get(orderList.size() - 1);
            }

            if(str.equals("end")){
                break;
            }
            String [] words = str.split(" ");
            if(words.length == 2){
                if(words[1].equals("delete")){ //删除
                    int orderNum = Integer.parseInt(words[0]);
                    Record record = order.searchDishNum(orderNum);
                    if (record == null) {
                        System.out.println("delete error;");
                    } else {
                        order.delARecordByOrderNum(orderNum);
                    }
                }
                else{ //添加菜品
                    menu.addDish(words[0], Integer.parseInt(words[1]));
                }
            }
            else if(words.length == 4){
                if(words[0].equals("table")){ //桌子信息
                    Order newOrder = new Order();
                    orderList.add(newOrder);
                    int table = Integer.parseInt(words[1]);
                    String tableTime = words[2] +" " + words[3];
                    newOrder.table = table;
                    newOrder.tableDtime = tableTime;
                    newOrder.processTime(); //初始化时间信息
                    newOrder.discount(); //计算折扣
                    System.out.println("table "+table+": ");
                }
                else{ //添加购买菜品记录
                    order.addARecord(Integer.parseInt(words[0]), words[1], Integer.parseInt(words[2]), Integer.parseInt(words[3]),menu);
                }

            }
            else{//代点菜
                //桌号 序号 菜品名称 份额 份数
                int eatTable = Integer.parseInt(words[0]);
                int RecordNum = Integer.parseInt(words[1]);
                String name = words[2];
                int portion = Integer.parseInt(words[3]);
                int num = Integer.parseInt(words[4]);
                int payTable = order.table;

                Record record = order.addARecord(RecordNum,name,portion,num,menu);
                System.out.println(RecordNum + " table " + payTable + " pay for table "+eatTable + " "+record.getPrice());

            }
        }

        for(Order order:orderList){
            order.printAllPrice();
        }
    }
}

7-1 菜单计价程序-4

本次课题比菜单计价系列-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折, 周末全价。

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

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

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

菜单四的迭代我觉得真的是非常非常折磨!!!特别多需要考虑的情况,前期的结构又需要改变一些,又要额外的加很多特殊情况满段。比如光是时间类的判断的特殊情况很多很多,而且很多测试点都不知道是什么,写到后面真的很不想写了。改来改去最后也只有八十多分。

 具体实现:

package PTA5;

import java.math.BigInteger;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//菜品类
class Dish {
    String name;//菜名
    int unit_price;//单价
    boolean T;

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

    public static boolean dishValid(String name,int unit_price){
        if(unit_price <=0 || unit_price >=300){
            System.out.println(name+" price out of range ");
            return false;
        }
        return true;
    }

    // 根据点菜的份额计算价格
    public long getPrice(int portion) {
        double price = unit_price;
        if (portion == 2) {
            price *= 1.5;
        } else if (portion == 3) {
            price *= 2;
        }
        return Math.round(price);
    }
}
//菜谱类:对应菜谱,包含饭店提供的所有菜的信息
class Menu {
    Dish [] dishes ;//菜品数组,保存所有菜品信息

    public Menu(){
        this.dishes = new Dish[100];
    }

    // 根据菜名在菜谱中查找菜品信息
    public Dish searchDish(String dishName) {
        for (Dish dish : dishes) {
            if (dish != null && dish.name.equals(dishName)) {
                return dish;
            }
        }
        return null;
    }

    //添加一道菜品信息
    public void addDish(String dishName, int unit_price,boolean T){
        Dish dish = new Dish(dishName, unit_price,T);
        for (int i = 0; i < dishes.length; i++) {
            if (dishes[i] != null && dishes[i].name.equals(dishName)) {
                dishes[i] = dish;
                return ;
            }
        }

        for (int i = 0; i < dishes.length; i++) {
            if (dishes[i] == null) {
                dishes[i] = dish;
                break;
            }
        }
    }
}
//点菜记录类:保存订单上的一道菜品记录
class Record {
    int orderNum;//序号
    Dish dish;//菜品
    int portion;//份额(1、2、3代表小、中、大份)
    int num;//份数
    boolean T_time; //特色菜打折

    //计价
    public Record(int oderNum, Dish dish, int portion, int num,boolean T_time) {
        this.orderNum = oderNum;
        this.dish = dish;
        this.portion = portion;
        this.num = num;
        this.T_time = T_time;
    }

    // 计算本条记录的价格
    public long getPrice() {
        if(dish != null){
            return dish.getPrice(portion) * num;
        }
        return 0;
    }
}
//订单类:保存用户点的所有菜的信息。
class Order {
    int table; // 桌号
    Record [] records;//保存订单上的每一道记录

    double discount = -1.0; //折扣,不同时间段折扣不同

    String tableDtime;//时间
    int year,month,day,week,hh,mm,ss;
    Set<Integer> deleteSet ;
    int maxNum; //点餐序号


    public Order() {
        this.records =  new Record[100]; //创建对象
        deleteSet = new HashSet<>();
        maxNum = -1;
    }

    // 添加一条菜品信息到订单中
    public Record addARecord(int orderNum, String dishName, int portion, int num, Menu menu) {
        Dish dish = menu.searchDish(dishName);
        Record record;

        Record old = findRecord( dishName,  portion);
        if(old != null){
            old.num += num;
            record = old;
        }
        else{
            boolean T_time = false;
            if(dish!= null && dish.T && week < 6){
                T_time = true;
            }
            record = new Record(orderNum, dish, portion, num,T_time);
        }


        this.maxNum = orderNum;
        int index = 0;
        while (index < records.length && records[index] != null) {
            index++;
        }

        records[index] = record;

        //如果菜名不存在
        if (dish == null) {
            System.out.println(dishName + " does not exist");
        }
        else{
            System.out.println(orderNum + " " + dishName + " " + record.getPrice());
        }
        return record;
    }
    public Record findRecord(String dishName, int portion){
        int index = 0;
        while (index < records.length) {
            if(records[index] != null && records[index].dish != null && records[index].dish.name.equals(dishName) && records[index].portion == portion){
                return records[index];
            }
            index++;
        }
        return null;
    }

    // 计算订单的总价
    public BigInteger getTotalPrice() {
        BigInteger totalPrice = new BigInteger("0");
        for (Record record : records) {
            if (record != null) {
                totalPrice = totalPrice.add(new BigInteger(String.valueOf(record.getPrice())));
            }
        }
        return totalPrice;
    }

    // 计算特色菜 折后价格
    public BigInteger getTDiscountPrice() {
        BigInteger totalPrice = new BigInteger("0");
        for (Record record : records) {
            if (record != null) {
                if(record.T_time){
                    totalPrice = totalPrice.add(new BigInteger(String.valueOf(Math.round(record.getPrice() * 0.7))));
                }
            }
        }
        return totalPrice;
    }

    // 计算非特色菜价格
    public BigInteger getNotTPrice() {
        BigInteger totalPrice = new BigInteger("0");
        for (Record record : records) {
            if (record != null) {
                if(!record.T_time){
                    totalPrice = totalPrice.add(new BigInteger(String.valueOf(Math.round(record.getPrice()))));
                }
            }
        }
        return totalPrice;
    }

    public void printAllPrice(){
        if(discount>0){
            System.out.println("table " + table + ": " + Math.round(this.getTotalPrice().intValue())
                    + " "+ Math.round(this.getNotTPrice().intValue() * discount + this.getTDiscountPrice().intValue()));
        }
    }

    // 根据序号在订单中查找菜品信息
    public Record searchDishNum(int orderNum) {
        for (Record record : records) {
            if (record != null && record.orderNum == orderNum) {
                return record;
            }
        }
        return null;
    }

    //根据序号删除一条记录
    public void delARecordByOrderNum(int orderNum){
        for (int i = 0; i < records.length; i++) {
            if (records[i] != null && records[i].orderNum == orderNum) {
                records[i].num = 0;
                break;
            }
        }
    }

    void processTime(){//处理时间
        String[] temp = tableDtime.split(" ");
        String[] temp1 = temp[0].split("/");
        String[] temp2 = temp[1].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 discount(){
        if(week>=1&&week<=5)
        {
            if(hh>=17&&hh<20)
                discount=0.8F;
            else if(hh==20&&mm<30)
                discount=0.8F;
            else if(hh==20&&mm==30&&ss==0)
                discount=0.8F;
            else if(hh>=11&&hh<=13||hh==10&&mm>=30)
                discount=0.6F;
            else if(hh==14&&mm<30)
                discount=0.6F;
            else if(hh==14&&mm==30&&ss==0)
                discount=0.6F;
        }
        else
        {
            if(hh>=10&&hh<=20)
                discount= 1.0F;
            else if(hh==9&&mm>=30)
                discount= 1.0F;
            else if(hh==21&&mm<30||hh==21&&mm==30&&ss==0)
                discount= 1.0F;
        }
    }

    public boolean dateCheck(){
        boolean isValid = isDateValid(year, month, day);
        if(isValid){
            if(year == 2022 || year == 2023){
                return  true;
            }
            else{ //时间合法但不在范围内
                System.out.println("not a valid time period");
            }
        }
        else{//时间不合法
            System.out.println(this.table+" date error");
        }
        return false;
    }

    public boolean isDateValid(int year, int month, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setLenient(false); // 设置为严格模式,不容忍不合法的日期

        // 设置年、月、日
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1); // 月份从0开始,所以需要减1
        calendar.set(Calendar.DAY_OF_MONTH, day);

        try {
            calendar.getTime(); // 尝试获取日期对象
            return true; // 日期合法
        } catch (Exception e) {
            return false; // 日期不合法
        }
    }

    public int getTable() {
        return table;
    }

    public void setTable(int table) {
        this.table = table;
    }

    public double getDiscount() {
        return discount;
    }

    public void setDiscount(double discount) {
        this.discount = discount;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Menu menu = new Menu();
        List<Order> orderList = new ArrayList<Order>();
        Order order = null;
        boolean outTime = false;

        while(input.hasNext()){
            String str = input.nextLine();

            if(str.equals("end")){
                break;
            }
            String [] words = str.split(" ");
            if(words.length == 2){
                String regex = "^[1-9]\\d*\\s+delete$|^\\S+\\s{1}[1-9]\\d*$";
                if(! isValid(str,regex)){
                    continue;
                }

                //长度为2 合法的输入 只有删除和添加菜品
                if(words[1].equals("delete")){ //删除


                    int orderNum = Integer.parseInt(words[0]);
                    if(order.deleteSet.contains(orderNum)){
                        System.out.println("deduplication "+orderNum);
                        continue;
                    }
                    order.deleteSet.add(orderNum);
                    Record record = order.searchDishNum(orderNum);
                    if (record == null) {
                        System.out.println("delete error;");
                    } else {
                        order.delARecordByOrderNum(orderNum);
                    }
                }
                else{ //添加菜品
                    if(orderList.size() != 0){//已经开始点菜,不能再添加菜品信息
                        System.out.println("invalid dish");
                    }
                    else{
                        if(Dish.dishValid(words[0], Integer.parseInt(words[1]))){
                            menu.addDish(words[0], Integer.parseInt(words[1]),false);
                        }

                    }
                }
            }
            else if(words.length == 3){
                String regex = "^\\S+\\s{1}[1-9]+\\s+T$";
                //\\D+:匹配一个或多个非数字字符。
                //([1-9]\\d*):匹配非0开头的整数数字。
                if(! isValid(str,regex)){
                    continue;
                }
                if(orderList.size() != 0){//已经开始点菜,不能再添加菜品信息
                    System.out.println("invalid dish");
                }else{
                    if(Dish.dishValid(words[0], Integer.parseInt(words[1]))){
                        menu.addDish(words[0], Integer.parseInt(words[1]),true);
                    }
                }
            }
            else if(words.length == 4){
                String regex = "^[1-9]\\d*\\s+[\\u4e00-\\u9fa5]+\\s+[1-9]\\d*\\s+[1-9]\\d*$|^table\\s[1-9]\\d*\\s\\d{4}/\\d{1,2}/\\d{1,2}\\s\\d{1,2}/\\d{1,2}/\\d{1,2}$";

//                \\d+:匹配一个或多个数字。
//                \\s:匹配一个空格。
//                \\D+:匹配一个或多个非数字字符。

//                table:匹配字符串中的 "table"。
//                \\s+:匹配一个或多个空格。
//                \\d+:匹配一个或多个数字。
//                \\d{4}/\\d{1,2}/\\d{1,2}:匹配时间的年月日格式,例如 "2023/3/15"。
//                \\d{4}:匹配四位数字的年份。
//                /:匹配斜杠分隔符。
//                \\d{1,2}:匹配一到两位数字的月份和日期。
//                \\d{2}:匹配两位数字的分钟和秒钟。

                if(! isValid(str,regex)){
                    continue;
                }
                if(words[0].equals("table")){ //桌子信息
                    Order newOrder = new Order();

                    int table = Integer.parseInt(words[1]);
                    if(table <1 ||table >55){
                        System.out.println(table +" table num out of range");
                        continue;
                    }
                    String tableTime = words[2] +" " + words[3];
                    newOrder.table = table;
                    newOrder.tableDtime = tableTime;
                    newOrder.processTime(); //初始化时间信息
                    if( ! newOrder.dateCheck()){
                        continue;
                    }
                    newOrder.discount(); //计算折扣
                    if(newOrder.discount < 0){
                        System.out.println("table " + table + " out of opening hours");
                        outTime = true;
                        continue;
                    }

                    Order old = getSameTable(table,orderList);
                    if(old != null && isSameOrder(old,newOrder)){
                        order = old;
                    }else{
                        orderList.add(newOrder);
                        order = newOrder;
                    }
                    System.out.println("table "+table+": ");
                    outTime = false;
                }
                else{ //添加购买菜品记录
                    if(order == null || outTime) continue;
                    if(order.maxNum >= Integer.parseInt(words[0])){
                        System.out.println("record serial number sequence error");
                        continue;
                    }
                    int orderNum = Integer.parseInt(words[0]);
                    int portion = Integer.parseInt(words[2]);
                    int num = Integer.parseInt(words[3]);
                    if(portion < 1 || portion > 3){
                        System.out.println(orderNum+" portion out of range "+portion);
                        continue;
                    }
                    if(num > 15){
                        System.out.println(orderNum+" num out of range "+num);
                        continue;
                    }
                    order.addARecord(orderNum,words[1],portion ,num ,menu);
                }

            }
            else if(words.length == 5){//代点菜
                if(order == null) continue;
                String regex = "^[1-9]\\d*\\s+[1-9]\\d*\\s+\\S+\\s+[1-3]\\s+[1-9]\\d*$";
                if(! isValid(str,regex)){
                    continue;
                }

                //桌号 序号 菜品名称 份额 份数
                int eatTable = Integer.parseInt(words[0]);
                boolean table = false;
                for(Order x:orderList){
                    if(x.table == eatTable){
                        table = true;
                        break;
                    }
                }
                if(!table){
                    System.out.println("Table number :"+eatTable+" does not exist");
                    continue;
                }

                int RecordNum = Integer.parseInt(words[1]);
                String name = words[2];
                int portion = Integer.parseInt(words[3]);
                int num = Integer.parseInt(words[4]);
                int payTable = order.table;

                Record record = order.addARecord(RecordNum,name,portion,num,menu);
                System.out.println(RecordNum + " table " + payTable + " pay for table "+eatTable + " "+record.getPrice());

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

        for(Order x:orderList){
            x.printAllPrice();
        }
    }

    static boolean isValid(String str,String regex){
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);

        if (!matcher.matches()) { //匹配不成功
            System.out.println("wrong format");
            return false;
        }
        return  true;
    }

    public static Order getSameTable(int tableNum,List<Order> list){
        for(Order order : list){
            if(order.table == tableNum){
                return order;
            }
        }
        return null;
    }
    public static boolean isSameOrder(Order o1,Order o2){
        if(o1.year == o2.year && o1.month == o2.month && o1.day == o2.day && o1.week == o2.week){
            if(o1.week <=5 && o1.discount == o2.discount){
                return true;
            }
            else{
                int sum1 = o1.hh *60*60 + o1.mm * 60 + o1.ss;
                int sum2 = o2.hh *60*60 + o2.mm * 60 + o2.ss;
                int x = Math.abs(sum1 - sum2);
                if(x <= 3600){
                    return true;
                }
            }
        }
        return false;
    }
}

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
这次的菜单迭代也很难复杂,不过我觉得比起菜单4还是好太多!!!主要是添加了菜系和口味等功能,感觉和菜单3难度差不多。

 具体实现:

  1 package PTA6;
  2 
  3 import java.math.BigInteger;
  4 import java.util.*;
  5 import java.util.regex.Matcher;
  6 import java.util.regex.Pattern;
  7 
  8 //菜品类
  9 class Dish {
 10     String name;//菜名
 11     int unit_price;//单价
 12     String taste;//口味
 13     boolean T;//是否特色菜
 14 
 15     public Dish(String name, int unit_price, String taste,boolean T) {
 16         this.name = name;
 17         this.unit_price = unit_price;
 18         this.taste = taste;
 19         this.T = T;
 20     }
 21 
 22     // 根据点菜的份额计算价格
 23     public long getPrice(int portion) {
 24         double price = unit_price;
 25         if (portion == 2) {
 26             price *= 1.5;
 27         } else if (portion == 3) {
 28             price *= 2;
 29         }
 30         return Math.round(price);
 31     }
 32     //判断数据是否合法
 33     public static boolean isValid(String taste,int taste_value){
 34         if(taste.equals("川菜")){
 35             if(taste_value>=0 && taste_value <= 5){
 36                 return true;
 37             }
 38             System.out.println("spicy num out of range :"+taste_value);
 39         }
 40         else if(taste.equals("晋菜") ){
 41             if( taste_value>=0 && taste_value <= 4){
 42                 return true;
 43             }
 44             System.out.println("acidity num out of range :"+taste_value);
 45         }
 46         else if(taste.equals("浙菜")){
 47             if( taste_value>=0 && taste_value <= 3){
 48                 return true;
 49             }
 50             System.out.println("sweetness num out of range :"+taste_value);
 51         }
 52         return false;
 53     }
 54 }
 55 //菜谱类:对应菜谱,包含饭店提供的所有菜的信息
 56 class Menu {
 57     Dish [] dishes ;//菜品数组,保存所有菜品信息
 58 
 59     public Menu(){
 60         this.dishes = new Dish[100];
 61     }
 62 
 63     // 根据菜名在菜谱中查找菜品信息
 64     public Dish searchDish(String dishName) {
 65         for (Dish dish : dishes) {
 66             if (dish != null && dish.name.equals(dishName)) {
 67                 return dish;
 68             }
 69         }
 70         return null;
 71     }
 72 
 73     //添加一道菜品信息
 74     public void addDish(String dishName, int unit_price,String taste,boolean T){
 75         Dish dish = new Dish(dishName, unit_price,taste,T);
 76         for (int i = 0; i < dishes.length; i++) {
 77             if (dishes[i] != null && dishes[i].name.equals(dishName)) {
 78                 dishes[i] = dish;
 79                 return ;
 80             }
 81         }
 82 
 83         for (int i = 0; i < dishes.length; i++) {
 84             if (dishes[i] == null) {
 85                 dishes[i] = dish;
 86                 break;
 87             }
 88         }
 89     }
 90 }
 91 //点菜记录类:保存订单上的一道菜品记录
 92 class Record {
 93     int orderNum;//序号
 94     Dish dish;//菜品
 95     int taste_value;
 96     int portion;//份额(1、2、3代表小、中、大份)
 97     int num;//份数
 98     boolean T_time;
 99 
100     boolean orderingFood;
101     boolean freeFood;
102 
103     //计价
104     public Record(int oderNum, Dish dish,int taste_value,int portion, int num) {
105         this.orderNum = oderNum;
106         this.dish = dish;
107         this.portion = portion;
108         this.num = num;
109         this.taste_value = taste_value;
110         this.T_time = false;
111         this.orderingFood = false;
112         this.freeFood = false;
113     }
114 
115     // 计算本条记录的价格
116     public long getPrice() {
117         if(dish != null && !freeFood){
118             return dish.getPrice(portion) * num;
119         }
120         return 0;
121     }
122 }
123 //订单类:保存用户点的所有菜的信息。
124 class Order {
125     int table; // 桌号
126     Record [] records;//保存订单上的每一道记录
127 
128     double discount = -1.0; //折扣,不同时间段折扣不同
129 
130     String tableDtime;//时间
131     int year,month,day,week,hh,mm,ss;
132 
133     String userName = "";
134     String phone = "";
135     long sum = 0;
136 
137 
138     public Order() {
139         this.records =  new Record[100]; //创建对象
140     }
141 
142     // 添加一条菜品信息到订单中
143 //    序号+英文空格+菜名+英文空格+辣/酸/甜度值+英文空格+份额+英文空格+份数
144     public Record addARecord(int orderNum, String dishName, int taste_value,int portion, int num, Menu menu,boolean orderingFood) {
145         Dish dish = menu.searchDish(dishName);
146         //如果菜名不存在
147         if (dish == null) {
148             System.out.println(dishName + " does not exist");
149             return null;
150         }
151         if(dish.T  && !Dish.isValid(dish.taste,taste_value)){
152             return null;
153         }
154 
155         Record record = new Record(orderNum, dish,taste_value, portion, num);
156         if(dish.T){
157             if(week !=6 && week != 7){
158                 record.T_time = true;
159             }
160         }
161 
162         int index = 0;
163         while (index < records.length && records[index] != null) {
164             index++;
165         }
166 
167         records[index] = record;
168         if(! orderingFood){
169             System.out.println(orderNum + " " + dishName + " " + record.getPrice());
170         }
171         return record;
172     }
173 
174     //计算口味值
175     public void printTaste(){
176         boolean flag = false;
177         HashMap<Long,String> spicyMap = new HashMap<>();
178         spicyMap.put(0L,"不辣");
179         spicyMap.put(1L,"微辣");
180         spicyMap.put(2L,"稍辣");
181         spicyMap.put(3L,"辣");
182         spicyMap.put(4L,"很辣");
183         spicyMap.put(5L,"爆辣");
184 
185         HashMap<Long,String> acidityMap = new HashMap<>();
186         acidityMap.put(0L,"不酸");
187         acidityMap.put(1L,"微酸");
188         acidityMap.put(2L,"稍酸");
189         acidityMap.put(3L,"酸");
190         acidityMap.put(4L,"很酸");
191 
192         HashMap<Long,String> sweetnessMap = new HashMap<>();
193         sweetnessMap.put(0L,"不甜");
194         sweetnessMap.put(1L,"微甜");
195         sweetnessMap.put(2L,"稍甜");
196         sweetnessMap.put(3L,"甜");
197 
198         int spicy = 0,spicySum = 0;
199         int acidity = 0,aciditySum = 0;
200         int sweetness = 0,sweetnessSum = 0;
201         long spicyAvg = 0, acidityAvg = 0, sweetnessAvg = 0;
202 
203 //        川菜 1 稍辣 晋菜 4 稍酸 浙菜 1 甜
204         for(Record x : records){
205             if(x != null && x.dish.T && !x.orderingFood){
206                 if(x.dish.taste.equals("川菜")){
207                     spicy += x.taste_value * x.num;
208                     spicySum += x.num;
209                 }
210                 else if(x.dish.taste.equals("晋菜")){
211                     acidity += x.taste_value * x.num;
212                     aciditySum += x.num;
213                 }else if(x.dish.taste.equals("浙菜")){
214                     sweetness += x.taste_value * x.num;
215                     sweetnessSum += x.num;
216                 }
217             }
218         }
219 
220         if(spicySum != 0){
221             flag = true;
222             double avg = (double) spicy/spicySum;
223             spicyAvg = Math.round(avg);
224             System.out.print("川菜 " + spicySum +" " +spicyMap.get(spicyAvg) );
225         }
226         if(aciditySum != 0){
227             double avg = (double) acidity/aciditySum;
228             acidityAvg = Math.round(avg);
229             if(flag){
230                 System.out.print(" ");
231             }
232             flag = true;
233             System.out.print("晋菜 " + aciditySum +" " + acidityMap.get(acidityAvg) );
234         }
235         if(sweetnessSum != 0){
236             if(flag){
237                 System.out.print(" ");
238             }
239             double avg = (double) sweetness/sweetnessSum;
240             sweetnessAvg = Math.round(avg);
241             System.out.print("浙菜 " + sweetnessSum +" " + sweetnessMap.get(sweetnessAvg) );
242         }
243         System.out.println();
244     }
245 
246     // 计算订单的总价
247     public BigInteger getTotalPrice() {
248         BigInteger totalPrice = new BigInteger("0");
249         for (Record record : records) {
250             if (record != null) {
251                 totalPrice = totalPrice.add(new BigInteger(String.valueOf(record.getPrice())));
252             }
253         }
254         return totalPrice;
255     }
256 
257     // 计算特色菜 折后价格
258     public BigInteger getTDiscountPrice() {
259         BigInteger totalPrice = new BigInteger("0");
260         for (Record record : records) {
261             if (record != null) {
262                 if(record.T_time){
263                     totalPrice = totalPrice.add(new BigInteger(String.valueOf(Math.round(record.getPrice() * 0.7))));
264                 }
265             }
266         }
267         return totalPrice;
268     }
269 
270     // 计算非特色菜价格
271     public BigInteger getNotTPrice() {
272         BigInteger totalPrice = new BigInteger("0");
273         for (Record record : records) {
274             if (record != null) {
275                 if(!record.T_time){
276                     totalPrice = totalPrice.add(new BigInteger(String.valueOf(Math.round(record.getPrice()))));
277                 }
278             }
279         }
280         return totalPrice;
281     }
282 
283     public void printAllPrice(){
284         if(discount>0){
285             sum = Math.round(this.getNotTPrice().intValue() * discount + this.getTDiscountPrice().intValue());
286             System.out.print("table " + table + ": " + Math.round(this.getTotalPrice().intValue())
287                     + " "+ sum+" ");
288         }
289     }
290 
291     // 根据序号在订单中查找菜品信息
292     public Record searchDishNum(int orderNum) {
293         for (Record record : records) {
294             if (record != null && record.orderNum == orderNum) {
295                 return record;
296             }
297         }
298         return null;
299     }
300 
301     //根据序号删除一条记录
302     public void delARecordByOrderNum(int orderNum){
303         for (int i = 0; i < records.length; i++) {
304             if (records[i] != null && records[i].orderNum == orderNum) {
305                 records[i].num = 0;
306                 break;
307             }
308         }
309     }
310 
311     void processTime(){//处理时间
312         String[] temp = tableDtime.split(" ");
313         String[] temp1 = temp[0].split("/");
314         String[] temp2 = temp[1].split("/");
315 
316         year = Integer.parseInt(temp1[0]);
317         month = Integer.parseInt(temp1[1]);
318         day = Integer.parseInt(temp1[2]);
319 
320         Calendar c = Calendar.getInstance();
321         c.set(year, (month-1), day);
322         week = c.get(Calendar.DAY_OF_WEEK);
323         if(week==1) {
324             week = 7;
325         } else {
326             week--;
327         }
328         hh = Integer.parseInt(temp2[0]);
329         mm = Integer.parseInt(temp2[1]);
330         ss = Integer.parseInt(temp2[2]);
331     }
332 
333     void discount(){
334         if(week>=1&&week<=5)
335         {
336             if(hh>=17&&hh<20)
337                 discount=0.8F;
338             else if(hh==20&&mm<30)
339                 discount=0.8F;
340             else if(hh==20&&mm==30&&ss==0)
341                 discount=0.8F;
342             else if(hh>=11&&hh<=13||hh==10&&mm>=30)
343                 discount=0.6F;
344             else if(hh==14&&mm<30)
345                 discount=0.6F;
346             else if(hh==14&&mm==30&&ss==0)
347                 discount=0.6F;
348         }
349         else
350         {
351             if(hh>=10&&hh<=20)
352                 discount= 1.0F;
353             else if(hh==9&&mm>=30)
354                 discount= 1.0F;
355             else if(hh==21&&mm<30||hh==21&&mm==30&&ss==0)
356                 discount= 1.0F;
357         }
358     }
359 
360     public int getTable() {
361         return table;
362     }
363 
364     public void setTable(int table) {
365         this.table = table;
366     }
367 
368     public double getDiscount() {
369         return discount;
370     }
371 
372     public void setDiscount(double discount) {
373         this.discount = discount;
374     }
375 }
376 
377 public class Main {
378     public static void main(String[] args) {
379         Scanner input = new Scanner(System.in);
380         Menu menu = new Menu();
381         List<Order> orderList = new ArrayList<Order>();
382         Order order = null;
383 
384         while(input.hasNext()){
385             String str = input.nextLine();
386             if(str.equals("end")){
387                 break;
388             }
389             String [] words = str.split(" ");
390             if(words.length == 2){
391                 String regex = "\\d+ delete|^(\\p{L}+)\\s+(\\d+)$";
392                 if(!Match(str,regex)){
393                     continue;
394                 }
395 
396                 if(words[1].equals("delete")){ //删除
397                     int orderNum = Integer.parseInt(words[0]);
398                     Record record = order.searchDishNum(orderNum);
399                     if (record == null) {
400                         System.out.println("delete error;");
401                     } else {
402                         order.delARecordByOrderNum(orderNum);
403                     }
404                 }
405                 else{ //添加菜品
406                     menu.addDish(words[0], Integer.parseInt(words[1]),"",false);
407                 }
408             }
409             else if(words.length == 4){
410                 String regex = "^(\\p{L}+\\s?){2}\\d+\\sT$|^\\d+\\s(\\p{L}+\\s?){2}\\d+\\s\\d+$";
411                 if(!Match(str,regex)){
412                     continue;
413                 }
414                 if(words[3].equals("T")){
415                     menu.addDish(words[0], Integer.parseInt(words[2]),words[1],true);
416                 }
417                 else{
418                     if(order == null) continue;
419                     order.addARecord(Integer.parseInt(words[0]), words[1],0, Integer.parseInt(words[2]), Integer.parseInt(words[3]),menu,false);
420                 }
421             }
422             else if(words.length == 5){//代点普通菜|点特色菜
423                 String regex = "^\\d+\\s\\d+\\s\\p{L}+\\s\\d+\\s\\d+$|^\\d+\\s\\p{L}+\\s\\d+\\s\\d+\\s\\d+$";
424                 if(!Match(str,regex)){
425                     continue;
426                 }
427                 if(isNumeric(words[1])){
428                     if(order == null) continue;
429 
430                     int eatTable = Integer.parseInt(words[0]);
431                     int RecordNum = Integer.parseInt(words[1]);
432                     String name = words[2];
433                     int portion = Integer.parseInt(words[3]);
434                     int num = Integer.parseInt(words[4]);
435                     int payTable = order.table;
436 
437                     Record record = order.addARecord(RecordNum,name,0,portion,num,menu,true);
438                     if(record != null){
439                         System.out.println(RecordNum + " table " + payTable + " pay for table "+eatTable + " "+record.getPrice());
440                     }
441                 }
442                 else{
443                     if(order == null) continue;
444                     order.addARecord(Integer.parseInt(words[0]), words[1],Integer.parseInt(words[2]), Integer.parseInt(words[3]), Integer.parseInt(words[4]),menu,false);
445                 }
446             }
447             else if(words.length == 6){
448                 String regex = "^\\d+\\s\\d+\\s\\p{L}+\\s\\d+\\s\\d+\\s\\d+$";
449                 if(!Match(str,regex)){
450                     continue;
451                 }
452                 if(order == null) continue;
453 
454                 int eatTable = Integer.parseInt(words[0]);
455                 int RecordNum = Integer.parseInt(words[1]);
456                 String name = words[2];
457                 int taste_value = Integer.parseInt(words[3]);
458                 int portion = Integer.parseInt(words[4]);
459                 int num = Integer.parseInt(words[5]);
460                 int payTable = order.table;
461 
462                 Order eatTableOrder = findOrder(orderList,eatTable);
463                 Record freeRecord = eatTableOrder.addARecord(RecordNum,name,taste_value,portion,num,menu,true);
464                 freeRecord.freeFood = true;
465 
466                 Record record = order.addARecord(RecordNum,name,taste_value,portion,num,menu,true);
467                 record.orderingFood = true;
468 
469                 if(record != null){
470                     System.out.println(RecordNum + " table " + payTable + " pay for table "+eatTable + " "+record.getPrice());
471                 }
472             }
473             else if(words.length == 7){
474 //                table 1 : tom 13605054400 2023/5/6 12/30/00
475                 String regex = "table\\s(\\d+)\\s:\\s(\\w{1,10})\\s((180|181|189|133|135|136)\\d{8})\\s(\\d{4}/\\d{1,2}/\\d{1,2})\\s(\\d{2}/\\d{2}/\\d{2})";
476                 if(!Match(str,regex)){
477                     continue;
478                 }
479                 Order newOrder = new Order();
480                 int table = Integer.parseInt(words[1]);
481                 String tableTime = words[5] +" " + words[6];
482                 newOrder.table = table;
483                 newOrder.tableDtime = tableTime;
484                 newOrder.userName = words[3];
485                 newOrder.phone = words[4];
486                 newOrder.processTime(); //初始化时间信息
487                 newOrder.discount(); //计算折扣
488                 if(newOrder.discount < 0){
489                     System.out.println("table "+newOrder.table+" out of opening hours");
490                     continue;
491                 }
492 
493                 orderList.add(newOrder);
494                 order = newOrder;
495                 System.out.println("table "+table+": ");
496 
497             }
498             else{
499                 System.out.println("wrong format");
500             }
501         }
502         HashMap<String,Long> userMap = new HashMap<>();
503 
504         for(Order x:orderList){
505             x.printAllPrice();
506             x.printTaste();
507             String key = x.userName +" "+ x.phone;
508             if(userMap.containsKey(key)){
509                 userMap.put(key,userMap.get(key) + x.sum);
510             }
511             else{
512                 userMap.put(key,x.sum);
513             }
514         }
515         PriorityQueue<String> priorityQueue = new PriorityQueue<>(String::compareTo);
516         for(String key:userMap.keySet()){
517             priorityQueue.add(key);
518         }
519         while (!priorityQueue.isEmpty()) {
520             String str = priorityQueue.poll();
521             Long sum = userMap.get(str);
522             System.out.println(str+ " " + sum);
523         }
524     }
525 
526 
527     public static boolean Match(String str,String regex){
528         Pattern pattern = Pattern.compile(regex);
529         Matcher matcher = pattern.matcher(str);
530         if (!matcher.matches()) {
531             System.out.println("wrong format");
532             return false;
533         }
534         return true;
535     }
536 
537     public static boolean isNumeric(String str) {
538         String pattern = "^\\d+$";
539         return str.matches(pattern);
540     }
541 
542     public static Order findOrder(List<Order> orderList,int table){
543         for (Order order : orderList){
544             if(order.table == table){
545                 return order;
546             }
547         }
548         return null;
549     }
550 }

期中考试:

7-1 测验1-圆类设计

 

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

输入格式:

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

输出格式:

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

输入样例:

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

2.35

输出样例:

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

17.35
这题还是蛮简单的,一遍就过了。
 1 import java.util.Scanner;
 2 
 3 public class Main{
 4         public static void main(String[] args) {
 5                 Scanner input = new Scanner(System.in);
 6                 Circle c = new Circle();
 7                 double r = c.setR(input.nextDouble());
 8                 if(r < 0){
 9                         return;
10                 }
11                 double s = c.getR() * c.getR() * Math.PI;
12                 System.out.printf("%.2f", s);
13         }
14 }
15 class Circle {
16         private double r;
17 
18         public Circle() {
19         }
20 
21         public double getR() {
22                 return r;
23         }
24 
25 
26         public double setR(double r) {
27                 if(r <= 0){
28                         System.out.println("Wrong Format");
29                         return - 1;
30                 }
31                 this.r = r;
32                 return r;
33         }
34 
35 }

7-2 测验2-类结构设计

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


image.png

输入格式:

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

输出格式:

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

输入样例:

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

6 5.8 -7 8.9

输出样例:

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

40.30
这道题也很简单,而且有类图几乎就是只要照着实现就好了。唯一需要注意的是要在结果取绝对值,不然会在一些特殊情况如原点的测试点报错
 1 import java.util.Scanner;
 2 public class Main {
 3     public static void main(String[] args) {
 4         Scanner input = new Scanner(System.in);
 5         Rectangle rectangle = new Rectangle();
 6         Point point1 = new Point();
 7         Point point2 = new Point();
 8         point1.setX(input.nextDouble());
 9         point1.setY(input.nextDouble());
10         point2.setX(input.nextDouble());
11         point2.setY(input.nextDouble());
12         rectangle.setLowerRightPoint(point1);
13         rectangle.setTopLeftPoint(point2);
14         System.out.printf("%.2f",rectangle.getArea());
15     }
16 }
17 
18 class Point{
19     private double x;
20     private double y;
21     public Point(){
22 
23     }
24     public Point(double x, double y){
25         this.x = x;
26         this.y = y;
27     }
28     public double getX(){
29         return x;
30     }
31     public void setX(double x){
32         this.x = x;
33     }
34     public double getY(){
35         return y;
36     }
37     public void setY(double y){
38         this.y = y;
39     }
40 }
41 
42 class Rectangle{
43     private Point topLeftPoint;
44     private Point lowerRightPoint;
45     public Rectangle(){
46 
47     }
48     public Rectangle(Point topLeftPoint, Point lowerRightPoint){
49         this.topLeftPoint = topLeftPoint;
50         this.lowerRightPoint = lowerRightPoint;
51     }
52     public Point getTopLeftPoint(){
53         return topLeftPoint;
54     }
55     public void setTopLeftPoint(Point topLeftPoint){
56         this.topLeftPoint = topLeftPoint;
57     }
58     public Point getLowerRightPoint(){
59         return lowerRightPoint;
60     }
61     public void setLowerRightPoint(Point lowerRightPoint){
62         this.lowerRightPoint = lowerRightPoint;
63     }
64     public double getLength(){
65         return Math.abs(getLowerRightPoint().getX() - getTopLeftPoint().getX());//取绝对值
66     }
67     public double getHeight(){
68         return Math.abs(getLowerRightPoint().getY() - getTopLeftPoint().getY());//取绝对值
69     }
70 
71     public double getArea(){
72         return getHeight() * getLength();
73     }
74 
75 }

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
这道题需要结合前两题来写,好在给了类图,只需要做码农实现一下就好了。
  1 import java.util.Scanner;
  2 public class Main {
  3     public static void main(String[] args) {
  4         // TODO Auto-generated method stub
  5         Scanner input = new Scanner(System.in);
  6 
  7         int choice = input.nextInt();
  8 
  9         switch(choice) {
 10             case 1://Circle
 11                 double radiums = input.nextDouble();
 12                 if(radiums <= 0){
 13                     System.out.println("Wrong Format");
 14                     return;
 15                 }
 16                 Shape circle = new Circle(radiums);
 17                 printArea(circle);
 18                 break;
 19             case 2://Rectangle
 20                 double x1 = input.nextDouble();
 21                 double y1 = input.nextDouble();
 22                 double x2 = input.nextDouble();
 23                 double y2 = input.nextDouble();
 24 
 25                 Point leftTopPoint = new Point(x1,y1);
 26                 Point lowerRightPoint = new Point(x2,y2);
 27 
 28                 Rectangle rectangle = new Rectangle(leftTopPoint,lowerRightPoint);
 29 
 30                 printArea(rectangle);
 31                 break;
 32         }
 33 
 34     }
 35     public static void printArea(Shape shape){
 36         double area = 0;
 37         if(shape instanceof Rectangle){
 38             shape = (Rectangle)shape;
 39             area = shape.getArea();
 40         }
 41         else{
 42             shape = (Circle)shape;
 43             area = shape.getArea();
 44         }
 45         System.out.printf("%.2f",area);
 46     }
 47 }
 48 class Point{
 49     private double x;
 50     private double y;
 51     public Point(){
 52 
 53     }
 54     public Point(double x, double y){
 55         this.x = x;
 56         this.y = y;
 57     }
 58     public double getX(){
 59         return x;
 60     }
 61     public void setX(double x){
 62         this.x = x;
 63     }
 64     public double getY(){
 65         return y;
 66     }
 67     public void setY(double y){
 68         this.y = y;
 69     }
 70 }
 71 class Circle extends Shape{
 72     private double radiums;
 73 
 74     public Circle() {
 75     }
 76     public Circle(double radiums) {
 77         this.radiums = radiums;
 78     }
 79     public double getRadiums() {
 80         return radiums;
 81     }
 82 
 83     public void setRadiums(double radiums) {
 84         this.radiums = radiums;
 85     }
 86 
 87     public double getArea(){
 88         return radiums * radiums * Math.PI;
 89     }
 90 }
 91 class Rectangle extends Shape{
 92     private Point topLeftPoint;
 93     private Point lowerRightPoint;
 94     public Rectangle(){
 95 
 96     }
 97     public Rectangle(Point topLeftPoint, Point lowerRightPoint){
 98         this.topLeftPoint = topLeftPoint;
 99         this.lowerRightPoint = lowerRightPoint;
100     }
101     public Point getTopLeftPoint(){
102         return topLeftPoint;
103     }
104     public void setTopLeftPoint(Point topLeftPoint){
105         this.topLeftPoint = topLeftPoint;
106     }
107     public Point getLowerRightPoint(){
108         return lowerRightPoint;
109     }
110     public void setLowerRightPoint(Point lowerRightPoint){
111         this.lowerRightPoint = lowerRightPoint;
112     }
113     public double getLength(){
114         return Math.abs(getLowerRightPoint().getX() - getTopLeftPoint().getX());//取绝对值
115     }
116     public double getHeight(){
117         return Math.abs(getLowerRightPoint().getY() - getTopLeftPoint().getY());//取绝对值
118     }
119 
120     public double getArea(){
121         return getHeight() * getLength();
122     }
123 
124 }
125 abstract class Shape{
126     public Shape(){
127 
128     }
129     public abstract double getArea();
130 }

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 

最后一题要用到抽象类和接口,说实话我对接口有点不太熟练,好在题目本身实现的功能不难,一下就想起来完成了。
  1 import java.util.Scanner;
  2 import java.util.ArrayList;
  3 import java.util.Comparator;
  4 public class Main {
  5     public static void main(String[] args) {
  6         // TODO Auto-generated method stub
  7         Scanner input = new Scanner(System.in);
  8         ArrayList<Shape> list = new ArrayList<>();
  9 
 10         int choice = input.nextInt();
 11 
 12         while(choice != 0) {
 13             switch(choice) {
 14                 case 1://Circle
 15                     double radiums = input.nextDouble();
 16                     Shape circle = new Circle(radiums);
 17                     list.add(circle);
 18                     break;
 19                 case 2://Rectangle
 20                     double x1 = input.nextDouble();
 21                     double y1 = input.nextDouble();
 22                     double x2 = input.nextDouble();
 23                     double y2 = input.nextDouble();
 24                     Point leftTopPoint = new Point(x1,y1);
 25                     Point lowerRightPoint = new Point(x2,y2);
 26                     Rectangle rectangle = new Rectangle(leftTopPoint,lowerRightPoint);
 27                     list.add(rectangle);
 28                     break;
 29             }
 30             choice = input.nextInt();
 31         }
 32 
 33         list.sort(Comparator.naturalOrder());//正向排序
 34 
 35         for(int i = 0; i < list.size(); i++) {
 36             System.out.print(String.format("%.2f", list.get(i).getArea()) + " ");
 37         }
 38     }
 39 }
 40 class Point{
 41     private double x;
 42     private double y;
 43     public Point(){
 44 
 45     }
 46     public Point(double x, double y){
 47         this.x = x;
 48         this.y = y;
 49     }
 50     public double getX(){
 51         return x;
 52     }
 53     public void setX(double x){
 54         this.x = x;
 55     }
 56     public double getY(){
 57         return y;
 58     }
 59     public void setY(double y){
 60         this.y = y;
 61     }
 62 }
 63 class Circle extends Shape{
 64     private double radiums;
 65 
 66     public Circle() {
 67     }
 68     public Circle(double radiums) {
 69         this.radiums = radiums;
 70     }
 71     public double getRadiums() {
 72         return radiums;
 73     }
 74 
 75     public void setRadiums(double radiums) {
 76         if (radiums > 0) {
 77             this.radiums = radiums;
 78         } else {
 79             System.out.println("Wrong Format");
 80         }
 81     }
 82 
 83     public double getArea(){
 84         return radiums * radiums * Math.PI;
 85     }
 86     @Override
 87     public int compareTo(Object object) {
 88         if(object instanceof Circle){
 89             if (getArea() > ((Circle) object).getArea()) {
 90                 return 1;
 91             } else {
 92                 return -1;
 93             }
 94         }
 95         else{
 96             if (getArea() > ((Rectangle) object).getArea()) {
 97                 return 1;
 98             } else {
 99                 return -1;
100             }
101         }
102     }
103 }
104 class Rectangle extends Shape{
105     private Point topLeftPoint;
106     private Point lowerRightPoint;
107     public Rectangle(){
108 
109     }
110     public Rectangle(Point topLeftPoint, Point lowerRightPoint){
111         this.topLeftPoint = topLeftPoint;
112         this.lowerRightPoint = lowerRightPoint;
113     }
114     public Point getTopLeftPoint(){
115         return topLeftPoint;
116     }
117     public void setTopLeftPoint(Point topLeftPoint){
118         this.topLeftPoint = topLeftPoint;
119     }
120     public Point getLowerRightPoint(){
121         return lowerRightPoint;
122     }
123     public void setLowerRightPoint(Point lowerRightPoint){
124         this.lowerRightPoint = lowerRightPoint;
125     }
126     public double getLength(){
127         return Math.abs(getLowerRightPoint().getX() - getTopLeftPoint().getX());//取绝对值
128     }
129     public double getHeight(){
130         return Math.abs(getLowerRightPoint().getY() - getTopLeftPoint().getY());//取绝对值
131     }
132 
133     public double getArea(){
134         return getHeight() * getLength();
135     }
136     @Override
137     public int compareTo(Object object) {
138         if(object instanceof Circle){
139             if (getArea() > ((Circle) object).getArea()) {
140                 return 1;
141             } else {
142                 return -1;
143             }
144         }
145         else{
146             if (getArea() > ((Rectangle) object).getArea()) {
147                 return 1;
148             } else {
149                 return -1;
150             }
151         }
152     }
153 }
154 abstract class Shape implements Comparable{
155     public Shape(){
156 
157     }
158     public abstract double getArea();
159 
160 }

三、踩坑心得:

在对源码进行提交的过程中,我遇到了一些问题和采坑的经验,下面是我总结的一些心得:

代码规范和命名:在进行代码提交之前,要确保代码符合规范并使用清晰的命名。这有助于他人理解和阅读代码。参数输入处理:对于输入的参数,要进行适当的处理和校验,以确保输入的数据符合要求并能正常运行。可以添加一些输入合法性的检查。 测试用例的覆盖:在进行代码测试时,要确保测试用例覆盖了各种情况。通过多组测试用例,可以验证代码的正确性和鲁棒性。 注释和文档说明:良好的注释和文档说明对于他人理解和维护代码非常重要。在提交代码之前,要仔细检查并完善注释和文档说明,解释关键逻辑、参数含义和函数作用等。这有助于他人更快地理解代码的功能和设计意图。 代码的可扩展性:在进行类设计时,要考虑代码的可扩展性,使得代码能够方便地添加新的功能或适应未来的需求变化。这样的设计可以减少重复代码,提高代码的可维护性。

通过以上的采坑心得,我意识到在代码提交过程中,不仅要关注代码的实现逻辑和正确性,还要注重代码的可读性、可扩展性和测试覆盖度。这样可以提高代码的质量,减少后续维护和调试的工作量。同时,与他人的交流和讨论也是非常重要的,可以从他人的反馈中获得不同的视角和改进的建议,进一步提高代码的质量和自身的编程能力。


四、改进建议:

多使用面向对象的设计原则:在代码中,可以采用更符合面向对象设计原则的方式来组织和管理类。 引入新学习的工厂模式:对于创建对象的过程,可以考虑使用工厂模式来进行封装和管理。通过一个工厂类,根据输入的类型参数,动态创建相应的对象,并将其添加到列表中。这样可以降低代码的耦合度,同时也方便后续添加新的类型。单元测试和模块化设计:在改进代码时,可以采用单元测试来验证每个模块的功能和正确性。通过编写针对各个函数和类的单元测试用例,可以及时发现和修复潜在的问题,确保代码的质量。同时,可以将代码进行模块化设计,将不同功能的代码块进行独立封装,提高代码的可维护性和复用性。可以使得代码更加清晰、可扩展和易于维护。同时,持续改进和反思自己的编码实践也是非常重要的,通过学习和实践不断提升自己的编程技能和代码品质。

 


五、总结:


在本阶段的学习中,我学到了很多关于编程和软件开发的知识和技能。然而,在学习过程中,我也发现了一些需要进一步学习和研究的地方:深入理解算法和数据结构:虽然我学习了一些基本的算法和数据结构,但我认识到还有更多高级的算法和数据结构需要深入学习,以解决更复杂的问题和优化代码性能。实践项目经验:虽然我在课程中有一些编程作业和实验,但我希望能够参与更多实际的项目,积累实践经验,并了解实际开发中的挑战和技巧。深入理解软件工程原理:除了编程技术,我也希望进一步学习软件工程的原理和方法论,包括需求工程、软件测试、质量保证等方面的知识,以提高软件开发的质量和效率。本阶段的学习让我掌握了一些基本的编程技能和软件开发的概念。我认识到还有很多需要学习和提高的地方,希望在接下来的学习中能够不断进步。

 

 
 

标签:String,int,BLOG2,words,菜品,table,public
From: https://www.cnblogs.com/YiKe-QUAN/p/17842288.html

相关文章

  • BLOG2
    一、作业总结该次博客作业是对第4次PTA、菜单计价程序-4、菜单计价程序-5、期中测试的一次总结。这次的菜单计价程序在之前的基础上进行了更完善的更改升级,菜单计价程序-4在菜单计价程序-3的基础上增加了异常情况的处理,菜单计价程序-5在菜单计价程序-3的基础上增加了特色菜的深入......
  • blog2
     一、前言随着对java学习的越来越深入,需要学习的东西也越来越多,第四五次pta题目集主要还是以菜单计价系统为主,相较于以前的菜单计价系统,增加了异常情况的处理,以及特色菜,口味度等功能,使这个菜单计价系统越来越与现实生活相关联,当然与之同时题目的难度当然也是大幅度提高了。虽......
  • blog2
    一、前言第四次题目集,主要是菜单计价程序,难度逐渐提高,难度不是很高。第五次题目集,只有一道菜单计价程序4,这道题是在菜单计价程序3的基础上添加了时间处理,使得程序整体难度提升很大第六次题目集,也只有一道菜单计价程序5,这道题也是以菜单计价程序3为基础,添加了特色菜的处理,难度相......
  • blog2
    前言菜单计价程序-3作为计价4和计价5的基础,做不了3就不用谈作为延伸拓展的4和5,期中考试难度一般。主要是菜单4涉及到了异常的处理机制,难度方面还是菜单3比较难,菜单4,5,都是在3的基础上增加内容,难度逐渐上升7-4菜单计价程序-2分数40全屏浏览题目切换布局作者......
  • Blog2
    一、前言1.题量及难度这几次作业的题量适中,题目数量适中,不过也不过少,能够提供足够的练习机会。在PTA上的大作业中,一共有三道题。其中第一题和第三题难度不大,但第二题属于点线形系列的题目,是之前得分不高的PTA作业中的重点题型,占据了70分,而我只得到了39分。在期中考试中虽然只有......
  • BLOG2-PTA题目集4、5以及期中考试
    (1)前言本次博客主要涵盖了Java题目的几个主要知识点,包括:1.面向对象的基础知识:这部分主要包括了类和对象的基本概念,构造方法,访问权限和成员变量的相关内容。在面向对象编程中,对这些基础知识的理解至关重要。2.面向对象的设计原则:这个题目强调了两个重要的设计原则,即继承和组......
  • blog2
    第四次题目集7-1菜单计价程序-4分数100作者蔡轲单位南昌航空大学本体大部分内容与菜单计价程序-3相同,增加的部分用加粗文字进行了标注。设计点菜计价程序,根据输入的信息,计算并输出总价格。输入内容按先后顺序包括两部分:菜单、订单,最后以"end"结束。菜单由一条或多条菜品记......
  • blog2
    本体大部分内容与菜单计价程序-3相同,增加的部分用加粗文字进行了标注。设计点菜计价程序,根据输入的信息,计算并输出总价格。输入内容按先后顺序包括两部分:菜单、订单,最后以"end"结束。菜单由一条或多条菜品记录组成,每条记录一行每条菜品记录包含:菜名、基础价格两个信息。订单......
  • blog2
    PTA题目集4,5及期中考试总结Blog 一.前言;      大一下学期开始,我们开始接触java这门语言,Java具有大部分编程语言所共有的一些特征,被特意设计用于互联网的分布式环境。Java具有类似于C++语言的形式和感觉,但它要比C++语言更易于使用,而且在编程时彻底采用了一种以对象为......
  • 面向对象程序设计题目集总结blog2-22206110-胡瑞杰
    一、前言第二次在博客园上发布面向对象程序设计题目集的总结博客。经过几周的学习,面向对象的理念更加深入。虽然已经学了些面向对象程序设计,学好这部分内容还是有较大难度。关于知识点本次的题目集所体现的知识点已经不仅限于Java的语法知识,还需要考虑设计问题,不......