首页 > 其他分享 >PTA第四第五次总结

PTA第四第五次总结

时间:2023-06-30 23:55:12浏览次数:30  
标签:return int PTA order 第五次 str parseInt table 第四

(1)前言:
PTA四五两次作业均为菜单点菜系统,也都是在PTA3作业上进行功能的增加以及迭代,除了第一次的作业,其他几次的难度都明显上升,第四次与第五次明显难度陡增,类与类之间的联系十分复杂,代码的耦合度也骤然上升,对我来说难度还是很大的。
(2)设计与分析:
第四次题目集因为备考以及各种作业等原因未完成完整代码,但本文仍会对PTA4题目集进行分析。
第五次PTA练习:
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
以下为UML类图:
image

以下为其中一种运行结果。
image
image

完整代码如下:
import java.util.Scanner;
import java.util.regex.Pattern;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Menu menu = new Menu();
Order order = new Order();
Table table = new Table();
for (int i = 0; i < table.order.length; i++) {
table.order[i] = new Order();
for (int i1 = 0; i1 < order.records.length; i1++) {
table.order[i].records[i1] = new Record();
table.order[i].s[i1] = new SpecialDish();
}
}
for (int i = 0; i < menu.dishes.length; i++) {
menu.dishes[i] = new Dish();
menu.dishes[i].name = " ";
}
int invalidFlag = 0, tablei = 0;
int tableFlag = 0;
int orderFlag1 = 0, orderFlag2 = 0;
int j = 0; //j用来看订单的序号
int stringFlag = 0;
int combineFlag = 0; //判断是否要合并桌
while (true) {
String signal = sc.nextLine();
if (signal.equals("end")) {
break;
}
String str[] = signal.split(" ");

       if (str.length == 2 || str.length == 4) {

           //菜单的输入
           if (str.length == 4) {

               //此时是订单的输入
               if (isInteger(str[0])) {
                   if (tableFlag == 1) {
                       j = 0;
                       tableFlag = 0;
                   }
                   if (tablei - 1 > -1) {
                       if(menu.searchDish(str[1]) !=null){
                           table.order[tablei - 1].records[j].d = menu.searchDish(str[1]);
                           table.order[tablei - 1].addARecord(Integer.parseInt(str[0]), str[1], Integer.parseInt(str[2]), Integer.parseInt(str[3]));
                           System.out.println(str[0] + " " + str[1] + " " + table.order[tablei - 1].records[j].getPrice());
                       }
                       else{
                           System.out.println(str[1] + " does not exist");      //记录错误菜品名
                           continue;
                       }
                   }
                   j++;
               }

               //特殊菜
               if (signal.matches("(\\S+)( )(川菜|晋菜|浙菜)( )([^0])(\\d*)( )(T?)")) {
                   menu.addSpecial(true);
                   menu.addType(str[1]);
                   menu.addDish(str[0], Integer.parseInt(str[2]));
               }
           } else if (str[0].length() != 1) {
               menu.addDish(str[0], Integer.parseInt(str[1]));
           }

           //删除
           else {
               if (tablei - 1 < 0) {
                   continue;
               }
              /*if (menu.findSpecial(str[0], table.order[tablei - 1])) {

               } */else {
                   table.order[tablei - 1].delARecordByOrderNum(Integer.parseInt(str[0]));
               }
           }
       }

       //订单的输入(给自己点菜, 并且此时只能是特殊菜)
       else if (str.length == 5) {

           //订单的输入
           if (isInteger(str[0])) {
               if (stringFlag == 1) {
                   continue;
               }

               if (stringFlag == 1) {
                   continue;
               }
               //点的菜在菜单上存在
               if (menu.searchDish(str[1]) != null) {
                   if (tableFlag == 1) {
                       j = 0;
                       tableFlag = 0;
                   }
                   if (tablei - 1 > -1) {
                       if (table.order[tablei - 1].ifAddTaste(menu, str[1], Integer.parseInt(str[2]))) {
                           table.order[tablei - 1].s[j].addDish(menu, str[1], str[3], str[4]);
                           table.order[tablei - 1].s[j].num = Integer.parseInt(str[0]);
                           System.out.println(str[0] + " " + str[1] + " " + table.order[tablei - 1].s[j].searchPrice(str[1]));
                           table.order[tablei - 1].records[j].d = menu.searchDish(str[1]);
                           table.order[tablei - 1].addARecord(Integer.parseInt(str[0]), str[1], Integer.parseInt(str[3]), Integer.parseInt(str[4]));

                           //把口味的count添加到口味当中
                           table.order[tablei - 1].addTaste(menu, str[1], Integer.parseInt(str[2]), table.order[tablei - 1].records[j].count);
                           table.order[tablei - 1].records[j].degree = Integer.parseInt(str[2]);
                           table.order[tablei - 1].pretendToDelete(Integer.parseInt(str[0]));
                           j++;
                       } else {
                           table.order[tablei - 1].printTasteOutOf(menu, str[1], Integer.parseInt(str[2]));
                       }
                   }
               }

               //点的菜在菜单上不存在
               else {
                   System.out.println(str[1] + " does not exist");      //记录错误菜品名
                   continue;
               }
           }
       }

       //代点菜(应该有大问题)
       else if (str.length == 6) {
           if (!isInteger(str[0])) {
               System.out.println("wrong format");
           }
           //代点菜存在
           if (menu.searchDish(str[2]) != null) {
               if (tableFlag == 1) {
                   j = 0;
                   tableFlag = 0;
               }
               if (table.num[tablei - 1] != Integer.parseInt(str[0]) && table.ifExistTableNum(Integer.parseInt(str[0]))) {
                   if (menu.searchSpecial(str[2])) {
                       if (tablei - 1 > -1) {
                           table.order[tablei - 1].s[j].addDish(menu, str[2], str[4], str[5]);
                           table.order[tablei - 1].s[j].num = Integer.parseInt(str[1]);
                           System.out.println(str[1] + " table " + table.num[tablei - 1] + " pay for table " + str[0] + " " + Math.round(Integer.parseInt(str[5]) * menu.searchDish(str[2]).getPrice(Integer.parseInt(str[4]))));
                           table.order[tablei - 1].records[j].d = menu.searchDish(str[2]);
                           table.order[tablei - 1].addARecord(Integer.parseInt(str[1]), str[2], Integer.parseInt(str[4]), Integer.parseInt(str[5]));
                           table.order[tablei - 1].pretendToDelete(Integer.parseInt(str[1]));
                           int number = table.searchSameTableOrder(Integer.parseInt(str[0]));
                           if (number != -1) {
                               Dish d = menu.searchDish(str[2]);
                               if (d.type.equals("川菜")) {
                                   table.order[number].spicyCount += Integer.parseInt(str[5]);
                               } else if (d.type.equals("晋菜")) {
                                   table.order[number].acidCount += Integer.parseInt(str[5]);
                               } else if (d.type.equals("浙菜")) {
                                   table.order[number].sweetCount += Integer.parseInt(str[5]);
                               }
                               table.order[number].addTaste(menu, str[2], Integer.parseInt(str[3]), Integer.parseInt(str[5]));
                           }
                           j++;
                       }
                   } else {
                       System.out.println(str[1] + " table " + table.num[tablei - 1] + " pay for table " + str[0] + " " + Math.round(Integer.parseInt(str[4]) * menu.searchDish(str[2]).getPrice(Integer.parseInt(str[3]))));
                       table.order[tablei - 1].records[j].d = menu.searchDish(str[2]);
                       j++;
                       table.order[tablei - 1].addARecord(Integer.parseInt(str[1]), str[2], Integer.parseInt(str[3]), Integer.parseInt(str[4]));
                   }
               } else {
                   System.out.println("Table number :" + str[0] + " does not exist");
               }
           }

           //代点菜不存在
           else {
               System.out.println(str[2] + " does not exist");
               continue;
           }
       }
       //输入桌号
       else if (str.length == 7) {
           if (str[0].equals("table")) {
               if (!table.ifLegal(signal)) {
                   System.out.println("wrong format");
                   stringFlag = 1;
                   continue;
               }
               table.num[tablei] = Integer.parseInt(str[1]);
               table.date[tablei] = str[5];
               table.time[tablei] = str[6];
               table.order[tablei].workFlag = 0;
               if (!table.isValidDate(tablei)) {
                   System.out.println(str[1] + " date error");
                   stringFlag = 1;
                   continue;
               }
               if (!table.judgeDiscount(tablei)) {
                   System.out.println("wrong format");
                   stringFlag = 1;
                   continue;
               }
               if (table.order[tablei].workFlag != 0) {
                   System.out.println("table " + table.num[tablei] + " out of opening hours");
                   stringFlag = 1;
                   continue;
               }
               table.order[tablei].owner = str[3];
               table.order[tablei].phoneNumber = str[4];
               stringFlag = 0;
               table.specialDiscount(tablei);

               System.out.println("table " + table.num[tablei] + ": ");
               table.order[tablei].tableNum = Integer.parseInt(str[1]);
               tablei++;
               tableFlag = 1;
           } else {
               System.out.println("wrong format");
           }
       } else {
           System.out.println("wrong format");
           stringFlag = 1;
           continue;
       }
   }
   int sum1 = 0, sum2 = 0;
   for (int i = 0; i < tablei; i++) {
       sum1 = 0;
       sum2 = 0;
       System.out.print("table " + table.num[i] + ": ");
       table.order[i].getTotalPrice();
       table.order[i].getTotalSpecialPrice();
       sum1 = table.order[i].totalPrice; // 正常菜的价格
       sum2 = table.order[i].specialTotalPrice; //特殊菜的价格
       System.out.print(sum1 + sum2 + " ");
       table.order[i].getAfterTotalPrice(menu);
       table.order[i].getAfterSpecialTotalPrice();
       int sum = table.order[i].afterDiscountTotalPrice + table.order[i].afterDiscountSpecialTotalPrice;
       System.out.print(sum);
       if (sum2 != 0) {
           table.order[i].getTotalSpecialCount(menu);
           table.order[i].getConcreteDegree();
           int degreeFlag = 0;
           System.out.print(" ");
           if (table.order[i].spicyLength != 0) {
               System.out.print("川菜" + " " + table.order[i].spicyCount + " " + table.order[i].spicyString);
               degreeFlag = 1;
           }
           if (table.order[i].acidLength != 0) {
               if (degreeFlag == 1) {
                   System.out.print(" ");
               }
               degreeFlag = 1;
               System.out.print("晋菜" + " " + table.order[i].acidCount + " " + table.order[i].acidString);
           }
           if (table.order[i].sweetLength != 0) {
               if (degreeFlag == 1) {
                   System.out.print(" ");
               }
               System.out.print("浙菜" + " " + table.order[i].sweetCount + " " + table.order[i].sweetString);
           }
       } else {
           System.out.print(" ");
       }
       System.out.println();
   }

   UserInformation UI = new UserInformation();
   for (int i = 0; i < tablei; i++) {
       table.order[i].userPriceAddTo();
   }
   UI.combineUserInformation(table);
   for (int i = 0; i < UI.orderLength; i++) {
       System.out.println(UI.order[i].owner + " " + UI.order[i].phoneNumber + " " + UI.order[i].userPrice);
   }

}

public static boolean isInteger(String str) {
Pattern pattern = Pattern.compile("[1]?[\d]*$");
return pattern.matcher(str).matches();
}
}

class UserInformation {
Order order[] = new Order[100];
int orderLength = 0;

public void outSetOrder() {
for (int i = 0; i < order.length; i++) {
order[i] = new Order();
order[i].owner = "";
}
}

public void combineUserInformation(Table table) {
outSetOrder();
for (int i = 0; i < table.order.length; i++) {
int flag = 0;
for (int i1 = 0; i1 < order.length; i1++) {
if (order[i1].owner.equals(table.order[i].owner)) {
flag = 1;
order[i1].userPrice += table.order[i].userPrice;
break;
}
}
if (flag == 0 && table.order[i].owner != null) {
order[orderLength++] = table.order[i];
}
}
}
}

class Dish { //对应菜谱上一道菜的信息。
String name; //菜品名称
int unit_price; //单价

boolean special; //是否是特殊菜
String type;//判断是川菜、浙菜、晋菜

public int getPrice(int portion) { //计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
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;
}
int price1 = (int) (run(price));
return price1;
}

double run(double num) {
double a = Math.signum(num); //判断是正数负数还是0,负数返回-1.0,正数返回1.0
if (a < 0.0)
return 0.0 - Math.round(Math.abs(num));
return Math.round(num);
}
}

class SpecialDish {
Dish d[] = new Dish[100];
int length = 0;
int totalSpecialPrice = 0;
int num = 0;
boolean ifDelete = false;

public void addDish(Menu m, String dishName, String portion, String count) {
d[length] = new Dish();
d[length].name = dishName;
d[length].unit_price = m.searchDish(dishName).getPrice(Integer.parseInt(portion)) * Integer.parseInt(count);
length++;
}

public int searchPrice(String dishName) {
for (int i = 0; i < d.length; i++) {
if (d[i].name.equals(dishName)) {
return d[i].unit_price;
}
}
return 0;
}

public int getSpecialPrice() {
int price = 0;
for (int i = 0; d[i] != null; i++) {
price += d[i].unit_price;
}
return price;
}
}

class Menu { //对应菜谱,包含饭店提供的所有菜的信息。
Dish[] dishes = new Dish[100];//菜品数组,保存所有菜品信息
int length = 0;

Dish searchDish(String dishName) {
for (int i = dishes.length - 1; i >= 0; i--) {
if (dishes[i].name.equals(dishName)) {
return dishes[i];
}
}
return null; //未找到
}

boolean searchSpecial(String dishName) {
if (searchDish(dishName).special) {
return true;
}
return false;
}

void addDish(String dishName, int unit_price) {
dishes[length].name = dishName;
dishes[length].unit_price = unit_price;
length++;
}

void addType(String type){
dishes[length].type = type;
}

void addSpecial(boolean special) {
dishes[length].special = special;
}
}

class Order { //保存用户点的所有菜的信息
Record[] records = new Record[100]; //保存订单上每一道的记录
SpecialDish[] s = new SpecialDish[100]; //特殊菜
int tableNum = 0;
int length = 0;
double discount;
int workFlag = 0;
int totalPrice = 0;
int userPrice = 0; //输出人名时的价格
int specialTotalPrice = 0;
int afterDiscountTotalPrice = 0;
int afterDiscountSpecialTotalPrice = 0;
int delete[] = new int[100];
int deleteFlag = 0;
double specialDiscount;
int workOrRelaxFlag = 0; //周一到周五的中午为1,晚上为2,周日为3

int spicyDegree[] = new int[100];
int spicyDegreeCount[] = new int[100];
int spicyLength = 0;
int acidityDegree[] = new int[100];
int acidDegreeCount[] = new int[100];
int acidLength = 0;
int sweetnessDegree[] = new int[100];
int sweetDegreeCount[] = new int[100];
int sweetLength = 0;
int spicyAver = 0;
int acidAver = 0;
int sweetAver = 0;
String spicyString;
String acidString;
String sweetString;
String owner;
String phoneNumber;
int spicyCount = 0;
int acidCount = 0;
int sweetCount = 0;

public void printTasteOutOf(Menu menu, String dishName, int degree) {
Dish d = menu.searchDish(dishName);
if (d != null) {
if (d.type.equals("川菜")) {
if (degree > 5) {
System.out.println("spicy num out of range :" + degree);
}
} else if (d.type.equals("晋菜")) {
if (degree > 4) {
System.out.println("acidity num out of range :" + degree);
}
} else if (d.type.equals("浙菜")) {
if (degree > 3) {
System.out.println("sweetness num out of range :" + degree);
}
}
}
}

public boolean ifAddTaste(Menu menu, String dishName, int degree) {
Dish d = menu.searchDish(dishName);
if (d != null) {
if (d.type.equals("川菜")) {
if (degree <= 5) {
return true;
}
return false;
} else if (d.type.equals("晋菜")) {
if (degree <= 4) {
return true;
}
return false;
} else if (d.type.equals("浙菜")) {
if (degree <= 3) {
return true;
}
return false;
}
}
return false;
}

public boolean addTaste(Menu menu, String dishName, int degree, int count) {
Dish d = menu.searchDish(dishName);
if (d != null) {
if (d.type.equals("川菜")) {
if (addSpicy(degree, count)) {
return true;
}
return false;
} else if (d.type.equals("晋菜")) {
if (addAcid(degree, count)) {
return true;
}
return false;
} else if (d.type.equals("浙菜")) {
if (addSweet(degree, count)) {
return true;
}
return false;
}
}
return false;
}

public void getAverage() {
double s1 = 0, a = 0, s2 = 0;
for (int i = 0; i < spicyLength; i++) {
s1 += spicyDegree[i] * spicyDegreeCount[i];
}
for (int i = 0; i < acidLength; i++) {
a += acidityDegree[i] * acidDegreeCount[i];
}
for (int i = 0; i < sweetLength; i++) {
s2 += sweetnessDegree[i] * sweetDegreeCount[i];
}
spicyAver = (int) Math.round(s1 / spicyCount);
acidAver = (int) Math.round(a / acidCount);
sweetAver = (int) Math.round(s2 / sweetCount);
}

public void getConcreteDegree() {
getAverage();
if (spicyAver == 0) {
spicyString = "不辣";
} else if (spicyAver == 1) {
spicyString = "微辣";
} else if (spicyAver == 2) {
spicyString = "稍辣";
} else if (spicyAver == 3) {
spicyString = "辣";
} else if (spicyAver == 4) {
spicyString = "很辣";
} else if (spicyAver == 5) {
spicyString = "爆辣";
}
if (acidAver == 0) {
acidString = "不酸";
} else if (acidAver == 1) {
acidString = "微酸";
} else if (acidAver == 2) {
acidString = "稍酸";
} else if (acidAver == 3) {
acidString = "酸";
} else if (acidAver == 4) {
acidString = "很酸";
}
if (sweetAver == 0) {
sweetString = "不甜";
} else if (sweetAver == 1) {
sweetString = "微甜";
} else if (sweetAver == 2) {
sweetString = "稍甜";
} else if (sweetAver == 3) {
sweetString = "甜";
}
}

public boolean addSpicy(int spicDegree, int count) {
if (spicDegree <= 5) {
spicyDegree[spicyLength] = spicDegree;
spicyDegreeCount[spicyLength++] = count;
return true;
}
return false;
}

public boolean addAcid(int acidDegree, int count) {
if (acidDegree <= 4) {
acidityDegree[acidLength] = acidDegree;
acidDegreeCount[acidLength++] = count;
return true;
}
return false;
}

public boolean addSweet(int sweetDegree, int count) {
if (sweetDegree <= 3) {
sweetnessDegree[sweetLength] = sweetDegree;
sweetDegreeCount[sweetLength++] = count;
return true;
}
return false;
}

void getTotalPrice() {
for (Record record : records) {
totalPrice += record.getPrice();
}
}

public void getTotalSpecialPrice() {
for (SpecialDish specialDish : s) {
specialTotalPrice += specialDish.getSpecialPrice();
}
}

void addARecord(int orderNum, String dishName, int portion, int count) {
records[length].d.name = dishName;
records[length].portion = portion;
records[length].count = count;
records[length].orderNum = orderNum;
length++;
}

void pretendToDelete(int orderNum) {
for (int i = 0; i < length; i++) {
if (records[i].orderNum == orderNum) {
totalPrice -= Math.toIntExact(Math.round(records[i].getPrice()));
}
}
}

SpecialDish findSpecialDish(int Num) {
for (int i = 0; s[i] != null; i++) {
if (s[i].num == Num) {
return s[i];
}
}
return null;
}

void pretendDelRecordByOrderNum(int orderNum, SpecialDish s) {
int flag = 0;
for (int i = 0; i < deleteFlag; i++) {
if (delete[i] == orderNum) {
//System.out.println("deduplication " + orderNum);
return;
}
}
for (int i = 0; i < length; i++) {
if (records[i].orderNum == orderNum) {
flag = 1;
delete[deleteFlag++] = orderNum;
afterDiscountSpecialTotalPrice -= s.searchPrice(records[i].d.name);
specialTotalPrice -= s.searchPrice(records[i].d.name);
}
}
if (flag == 0) {
System.out.println("delete error;");
}
}

void delARecordByOrderNum(int orderNum) {
int flag = 0;
for (int i = 0; i < deleteFlag; i++) {
if (delete[i] == orderNum) {
System.out.println("deduplication " + orderNum);
return;
}
}
for (int i = 0; i < length; i++) {
if (records[i].orderNum == orderNum) {
flag = 1;
delete[deleteFlag++] = orderNum;
totalPrice -= Math.toIntExact(Math.round(records[i].getPrice()));
}
}
if (flag == 0) {
System.out.println("delete error;");
}
}

public void getTotalSpecialCount(Menu m) {
for (int i = 0; i < records.length; i++) {
Dish d = m.searchDish(records[i].d.name);
if (d != null && d.special) {
if (d.type.equals("川菜")) {
spicyCount += records[i].count;
} else if (d.type.equals("晋菜")) {
acidCount += records[i].count;
} else if (d.type.equals("浙菜")) {
sweetCount += records[i].count;
}
}
}
}

public void getAfterTotalPrice(Menu menu) {
for (int i = 0; i < records.length; i++) {
Dish d = menu.searchDish(records[i].d.name);
if (d != null && !d.special) {
afterDiscountTotalPrice += Math.round(records[i].getPrice() * discount);
}
}
if (afterDiscountTotalPrice < 0) {
afterDiscountTotalPrice = 0;
}
}

public void getAfterSpecialTotalPrice() {
for (SpecialDish specialDish : s) {
if (specialDish.ifDelete) {
afterDiscountSpecialTotalPrice += specialDish.getSpecialPrice();
} else {
afterDiscountSpecialTotalPrice += Math.round(specialDish.getSpecialPrice() * specialDiscount);
}

   }

}

public void deleteTaste(Menu menu, String dishName, int degree, int count) {
Dish d = menu.searchDish(dishName);
if (d != null) {
if (d.type.equals("川菜")) {
for (int i = 0; i < spicyDegree.length; i++) {
if (spicyDegree[i] == degree && spicyDegreeCount[i] == count) {
spicyDegree[i] -= degree;
spicyDegreeCount[i] -= count;
spicyCount -= count;
spicyLength--;
break;
}
}
} else if (d.type.equals("晋菜")) {
for (int i = 0; i < acidityDegree.length; i++) {
if (acidityDegree[i] == degree && acidDegreeCount[i] == count) {
acidityDegree[i] -= degree;
acidDegreeCount[i] -= count;
acidCount -= count;
acidLength--;
break;
}
}
} else if (d.type.equals("浙菜")) {
for (int i = 0; i < sweetnessDegree.length; i++) {
if (sweetnessDegree[i] == degree && sweetDegreeCount[i] == count) {
sweetnessDegree[i] -= degree;
sweetDegreeCount[i] -= count;
sweetCount -= count;
sweetLength--;
break;
}
}
}
}
}

public int findRecordByNumber(int number) {
for (int i = 0; i < records.length; i++) {
if (records[i].orderNum == number) {
return i;
}
}
return -1;
}

public int findSpecialRecordByNumber(int number) {
for (int i = 0; i < s.length; i++) {
if (s[i].num == number) {
return i;
}
}
return -1;
}

public void userPriceAddTo() {
userPrice += afterDiscountTotalPrice + afterDiscountSpecialTotalPrice;
}
}

class Record { //保存订单上的一道菜品记录
Dish d = new Dish(); //菜品
int orderNum;
int count;//订单上的份数
int portion; //份额(1/2/3代表小/中/大份)
int degree; //口味的程度
int getPrice(){
return count * d.getPrice(portion);
}
}
class Table {
Order order[] = new Order[100];
int num[] = new int[100];//桌子的序号
String date[] = new String[100];
String time[] = new String[100];

public void specialDiscount(int i) {
String d[] = date[i].split("/");
LocalDate d1 = LocalDate.of(Integer.parseInt(d[0]), Integer.parseInt(d[1]), Integer.parseInt(d[2]));
int day = d1.getDayOfWeek().getValue();
if (day != 6 && day != 7) {
order[i].specialDiscount = 0.7;
} else {
order[i].specialDiscount = 1;
}
}

boolean judgeDiscount(int i) {
String d[] = date[i].split("/");
String t[] = time[i].split("/");
LocalDate d1 = LocalDate.of(Integer.parseInt(d[0]), Integer.parseInt(d[1]), Integer.parseInt(d[2]));
LocalTime d2 = LocalTime.of(Integer.parseInt(t[0]), Integer.parseInt(t[1]), Integer.parseInt(t[2]));
LocalTime dRelaxStart = LocalTime.of(9, 30);
LocalTime dRelaxEnd = LocalTime.of(21, 30);
LocalTime dWorkStart1 = LocalTime.of(10, 30);
LocalTime dWorkStart2 = LocalTime.of(17, 0);
LocalTime dWorkEnd1 = LocalTime.of(14, 30);
LocalTime dWorkEnd2 = LocalTime.of(20, 30);
int day = d1.getDayOfWeek().getValue();
if (day == 6 || day == 7) {
if (d2.isAfter(dRelaxEnd) || d2.isBefore(dRelaxStart)) {
order[i].workFlag = 1;
} else {
order[i].workOrRelaxFlag = 3;
order[i].discount = 1;
}
} else {
if ((d2.isAfter(dWorkStart1) && d2.isBefore(dWorkEnd1)) || (d2.until(dWorkStart1, ChronoUnit.SECONDS) == 0 || d2.until(dWorkEnd1, ChronoUnit.SECONDS) == 0)) {
order[i].discount = 0.6;
order[i].workOrRelaxFlag = 1;
} else if ((d2.isAfter(dWorkStart2) && d2.isBefore(dWorkEnd2)) || (d2.until(dWorkStart2, ChronoUnit.SECONDS) == 0 || d2.until(dWorkEnd2, ChronoUnit.SECONDS) == 0)) {
order[i].workOrRelaxFlag = 2;
order[i].discount = 0.8;
} else {
order[i].workFlag = 1;
}
}
String pattern = "([0-1]?[0-9]|2[0-3])/([0-5][0-9])/([0-5][0-9])";
String timeString = "";
char[] c = time[i].toCharArray();
for (int i1 = 0; i1 < c.length; i1++) {
timeString += c[i1];
}
if (timeString.matches(pattern)) {
return true;
} else {
return false;
}
}

boolean isValidDate(int i) {
String str[] = date[i].split("/");

   try {
       int year = Integer.parseInt(str[0]);
       int month = Integer.parseInt(str[1]);
       int day = Integer.parseInt(str[2]);

       if (year < 1 || month < 1 || month > 12) {
           return false;
       }

       if (day < 1 || day > getDaysInMonth(year, month)) {
           return false;
       }
   } catch (NumberFormatException e) {
       return false;
   }


   return true;

}

public static int getDaysInMonth(int year, int month) {
switch (month) {
case 2:
return isLeapYear(year) ? 29 : 28;
case 4:
case 6:
case 9:
case 11:
return 30;
default:
return 31;
}
}

public static boolean isLeapYear(int year) {
if (year % 4 == 0 && year % 100 != 0) {
return true;
} else if (year % 400 == 0) {
return true;
} else {
return false;
}
}

public boolean ifLegal(String str) {
if (str.matches("(table)( )([^0])(\d*)( )(

标签:return,int,PTA,order,第五次,str,parseInt,table,第四
From: https://www.cnblogs.com/zhuzexi/p/17518073.html

相关文章

  • pta题目集4-5及期中考试
    1.前言:点菜程序,光是听到名字就让人颤栗,我也光荣的基本上没写出来,以下的代码是题目集结束之后写出来的pta题目集4:7-1菜单计价程序-4分数 100 全屏浏览题目切换布局作者 蔡轲单位 南昌航空大学 本体大部分内容与菜单计价程序-3相同,增加的部......
  • PTA题目集4、5以及期中考试
    前言在这篇Blog中,我将总结之前所涉及到的PTA题目集4、5以及期中考试的情况。这些题目涉及了不同的知识点和题量,并具有一定的难度。本次Blog的重点将分析PTA中的菜单计价系列的题目以及期中考试的题目。设计与分析菜单计价系列题目分析菜单计价系列的题目要求设计一个点菜计价......
  • centos系统防火墙iptables禁止访问某个网站域名
    1.禁止访问百度#iptables-AOUTPUT-ptcp-dwww.baidu.com--dport80-jDROP#iptables-L//生效 2.允许访问百度#iptables-AOUTPUT-ptcp-dwww.baidu.com--dport80-jACCEPT#iptables-L————————————————......
  • PTA题目集4、5以及期中考试的总结性Blog
    一、前言第4、5次大作业只有一题菜单的迭代,虽然题目量不多,但是难度却是大大增加,更加考验大家的学习自觉性和Java的功底,这两次的大作业均是在菜单系列-3的基础上迭代,是菜单系列-3的两个不同分支。自然而然,与菜单系列-3的代码结构的完整性和可复用性等关联就很大,菜单系......
  • 第四、五次题目集以及期中考试总结
    第四、五次题目集以及期中考试总结一、题目集四第一题题目内容:菜单计价程序-4**importjava.util.*;publicclassMain{publicstaticvoidmain(String[]args){Restaurantres=newRestaurant();res.start();}}importjava.util.Arr......
  • Java 期中考试及pta4、5总结
    菜单计价1~3(1)前言:期中考试的题目比较简单但是具有典型意义,ptA的题和之前的大差不差,所以会省略一些总体来说这几次作业还是对Java的学习有较大的帮助的,需要认真对待(2)设计与分析:一测验1-圆类设计下面是题目要求:7-1测验1-圆类设计分数 12......
  • STD-study-暑假-大一下-PTA-day1
    L1-001#include<iostream>usingnamespacestd;intmain(){cout<<"HelloWorld!"<<endl;return0;}毫无难度L1-002打印沙漏#include<stdio.h>#include<math.h>intmain(){intn;//符号的个数charc;//符号......
  • pta4,5及期中考试总结
    (1)前言:期中考试的题目比较基础,主要涉及到了基本的面向对象思想和基本语法。pta4与pta5为点菜系列题目难度较大     (2)设计与分析:1.期中考试第一道题目,代码如下importjava.util.Scanner;classCircle{privatedoubleradius;publicCircle(dou......
  • PTA4,5及期中总结
    1,前言PTA4,5还是继续之前的菜单计价程序,只是在1,2,3的基础上加以完善,增加了更多的新的功能,期中考试则是在测试考核面向对象及接口,继承和多态以及抽象类等等知识点、2,设计与分析 菜单计价程序-4 类图   第一次的课程成绩统计程序较为直观,题目要求中直接给了相关的类图,......
  • PTA 4,5题目集及期中考试总结
    PTA4,5题目集及期中考试总结前言第4次题目集知识点:对象和类的创建和应用,字符串的创建和应用。总共有1题,难度偏高。第5次题目集知识点:对象和类的创建和应用,字符串的创建和应用。总共有1题,难度偏高。期中考试知识点:字符的处理,类的封装,接口的创建和使用。总共有4题,难度偏低......