PTA大作业4-5及期中考试

前言:第四次大作业:是对第三次大作业的迭代延续,在第三次大作业的基础上增加了新的功能,使代码更加复杂和程序更加实用。此次也是只有一道分值100的题目,却考察了诸多知识点,锻炼了我们的异常处理。

第五次大作业:也是对第三次大作业的迭代延续,在第三次大作业的基础上考察各方面的知识应用,增加的特色菜的特殊处理,使学生的代码更加接近实际,锻炼学生的代码处理复杂问题的能力。

期中考试:第一道是考察类的设计使用,后几道题是循序渐进的,考察学生的基础知识。

第四次大作业题目:

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

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

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

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

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

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

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

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

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

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

删除记录格式:序号 delete

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

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

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

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

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

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

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

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

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

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

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

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

Dish {

String name;//菜品名称

int unit_price; //单价

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

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

Menu {

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

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

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

}

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

Record {

int orderNum;//序号

Dish d;//菜品\\

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

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

}

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

Order {

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

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

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

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

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

}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

例如:麻婆豆腐 9 T

菜价的计算方法:

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

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

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

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

输入格式:

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

菜品记录格式:

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

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

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

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

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

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

输出格式:

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

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

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

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

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

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

输入样例:

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

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

输出样例:

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

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

输入样例1:

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

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

输出样例1:

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

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

输入样例2:

桌号信息错误。例如:

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

输出样例2:

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

wrong format
 

输入样例3:

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

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

输出样例3:

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

wrong format
 

输入样例4:

错误的菜谱记录。例如:

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

输出样例4:

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

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

输入样例5:

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

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

输出样例5:

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

wrong format
 

输入样例6:

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

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

输出样例6:

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

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

import java.util.Objects;
import java.util.Scanner;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.regex.Pattern;
public class Main
{
public static String dateToWeek(String datetime)
{
SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/dd");
String[] weekDays = { "7", "1", "2", "3", "4", "5", "6" };
Calendar cal = Calendar.getInstance(); // 获得一个日历
Date datet = null;
try {
datet = f.parse(datetime);
cal.setTime(datet);
} catch (ParseException e) {
e.printStackTrace();
}
int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
if (w < 0)
w = 0;
return weekDays[w];
}

public static boolean isInteger(String str) {
if (str.length()==0||str==null) {
return false;
}
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
return pattern.matcher(str).matches();
}

public static void main(String[] args){
Scanner input = new Scanner(System.in);
Dish[] d=new Dish[1000];
Menu menu=new Menu();
Record[] R=new Record[1000];
Order order=new Order();
int[] nb=new int[1000];//nb判断是不是代点
String[] te= new String[1000];
int[] number=new int[1000];
int[] tol=new int[1000];
double[] Discount=new double[1000];
double[] Tdiscount=new double[1000];
int[] tese=new int[1000];
int[] Daidian=new int[1000];
int[] fuhe=new int[1000];//fuhe是储存相应桌号的判断是否在营业时间范围内的lag
int i=0,t=0,pri=0,pri1=0,lag=0,fa=1,mber=0,daidian=0,daidianhao,teprice=0,qq=0,tab=0,zhuohao=0,youxian=0,shuchu=0;//lag判断是否在营业时间范围内,fa判断输出桌号,mber是数组序号,daidian是代点的金额
double discount=0,tdiscount=0;
while(true)
{
String a=input.nextLine(); //输入信息
String[] b=a.split(" ");
if(a.equals("end"))
{
order.records=R;
tol[qq]=order.getTotalPrice(t);
break;
}
else
{
if(b.length==2)
{
char result=b[0].charAt(0);
if(result>='0'&&result<='9')
{
if(zhuohao==1)
{
if(b[0].matches("^[1-9]\\d*$"))
{
order.records=R; //删除序号
order.delARecordByOrderNum(b[0],t);
}
else
{
System.out.println("wrong format");
}
}
}
else
{
if(!b[1].matches("\\d+")||!b[0].matches("^[\u4e00-\u9fa5]+$"))
{
System.out.println("wrong format");
}
else
{
if(b[1].matches("^[1-9]\\d*$"))
{
if(Integer.parseInt(b[1])>1&&Integer.parseInt(b[1])<300)
{
for (int c = 0; c < i; c++) //重复的菜品输入
{
if (b[0].equals(d[c].name))
d[c].unit_price = Integer.parseInt(b[1]);
}
if (!isInteger(b[1]))
System.out.println("wrong format");
else
{
if (tab == 0)
{
d[i] = new Dish();//添加一道菜品信息
d[i].name = b[0];
d[i].unit_price = Integer.parseInt(b[1]);
i++;
}
else
{
if(zhuohao==1)
System.out.println("invalid dish");
}
}
}
else
{
System.out.println(b[0]+" price out of range "+b[1]);
}
}
else
{
System.out.println("wrong format");
}
}
}
}
else if(b.length==3)
{
if(tab==0)
{
if(a.matches(("[^\\s]+\\s[0-9]+\\s[Tt]")))
{
if(b[1].matches("^[1-9]\\d*$"))
{
if(Integer.parseInt(b[1])>1&&Integer.parseInt(b[1])<300)
{
d[i] = new Dish();//添加一道菜品信息
d[i].name = b[0];
d[i].unit_price = Integer.parseInt(b[1]);
te[i] = b[0];
i++;
}
else
{
System.out.println(b[0]+" price out of range "+b[1]);
}
}
else {
System.out.println("wrong format");
}
}
else
{
System.out.println("wrong format");
}
}
else
{
if(zhuohao==1)
System.out.println("invalid dish");
}
}
else if(b.length==4)
{
char num=b[0].charAt(0);
if(num>='0'&&num<='9')
{
youxian=0;
shuchu=0;
if(b[0].matches("^[1-9]\\d*$"))
{
if(zhuohao==1)
{
if (t == 0) //输入菜单菜品
{
menu.dishes = d;
menu.count = i;
}
if (menu.searchDish(b[1]) != null)
{
R[t] = new Record();
R[t].d = menu.searchDish(b[1]); //输入菜品
}
else
{
System.out.println(b[1] + " does not exist");//记录错误菜品名
continue;
}
if (Integer.parseInt(b[2])>3)
{
if(Integer.parseInt(b[2])<10)
{
System.out.printf("%d portion out of range %d\n",Integer.parseInt(b[0]),Integer.parseInt(b[2]));
shuchu=1;
youxian=1;
}
else
{
System.out.println("not a valid time period");
shuchu=1;
youxian=1;
}
}
if(b[3].matches("^[1-9]\\d*$"))
{
if(Integer.parseInt(b[3])>15&&youxian==0)
{
System.out.println(b[0]+" num out of range "+b[3]);
shuchu=1;
}
}
else
{
if(youxian==0)
{
System.out.println("wrong format");
shuchu=1;
}
}
if(shuchu==0)
{
if(t==0)
{
R[t].orderNum = b[0]; //输入序号
R[t].portion = Integer.parseInt(b[2]); //输入份额
R[t].num = Integer.parseInt(b[3]);//输入份数
for(int v=0;v<i;v++)
{
if (Objects.equals(te[v], b[1]))
{
teprice=teprice+R[t].getprice();
tese[qq]=teprice;
}
}
System.out.println(R[t].orderNum + " " + R[t].d.name + " " + R[t].getprice());
t++;
}
else
{
if(Integer.parseInt(b[0])>Integer.parseInt(R[t-1].orderNum))
{
for(int c=0;c<t;c++)
{
if(b[1].equals(R[c].d.name)&&Integer.parseInt(b[2])==R[c].portion)
{
R[t-1].portion=R[t-1].portion+Integer.parseInt(b[3]);
}
}
R[t].orderNum = b[0]; //输入序号
R[t].portion = Integer.parseInt(b[2]); //输入份额
R[t].num = Integer.parseInt(b[3]);//输入份数
for(int v=0;v<i;v++)
{
if (Objects.equals(te[v], b[1]))
{
teprice=teprice+R[t].getprice();
tese[qq]=teprice;
}
}
System.out.println(R[t].orderNum + " " + R[t].d.name + " " + R[t].getprice());
t++;
}
else {
System.out.println("record serial number sequence error");
}
}
}
}
}
else
{
System.out.println("wrong format");
}
}
else
{
tab=1;//判断能不能增加菜的信息
zhuohao=0;//判断是否可以进行这卓的下一步操作
if (b[1].length() == 0 || b[1].length() > 2 || !b[1].matches("[1-9][0-9]*"))
{
System.out.println("wrong format");
}
else
{
if(Integer.parseInt(b[1])>=1&&Integer.parseInt(b[1])<=55)
{
String date;
date=b[2]+" "+b[3];
if(zhuo(date)==2)
{
order.records=R;
tol[qq]=order.getTotalPrice(t);
zhuohao=1;
number[qq]= Integer.parseInt(b[1]);
qq++;
lag=0;
daidian=0;
fa--;
t=0;
int y,time;
int[] f=new int[3];
String[] c=b[3].split("/");
f[0]= Integer.parseInt(c[0]);
f[1]= Integer.parseInt(c[1]);
f[2]= Integer.parseInt(c[2]);
y=Integer.parseInt(Main.dateToWeek(b[2]));
time=f[0]*3600+f[1]*60+f[2];
if(y==7||y==6)
{
tdiscount=1;
Tdiscount[qq]=tdiscount;
if(time>=9.5*3600&&time<=21.5*3600)
{
discount=1;
Discount[qq]=discount;
}
else
{
lag=1;
}
fuhe[mber]=lag;
mber++;
}
else
{
tdiscount=0.7;
Tdiscount[qq]=tdiscount;
if(time>=10.5*3600&&time<=14.5*3600)
{
discount=0.6;
Discount[qq]=discount;
fuhe[mber]=lag;
mber++;
}
else if(time>=17*3600&&time<=20.5*3600)
{
discount = 0.8;
Discount[qq]=discount;
fuhe[mber] = lag;
mber++;
}
else
{
lag=1;
fuhe[mber]=lag;
mber++;
}
}
}
else if(zhuo(date)==1)
{
System.out.println("not a valid time period");
}
else
{
System.out.println(b[1]+" date error");
}
}
else
{
System.out.println(b[2] + " table num out of range");
}
}
}
if(fa==0)
{
System.out.printf("table %d: \n",number[qq-1]);
fa++;
}
}
else if(b.length==5)
{
int e;
if(zhuohao==1)
{
if(a.matches( "\\d+\\s\\d+\\s\\D+\\s\\d+\\s\\d+"))
{
int five=0;
for(int c=0;c<t;c++)
{
if(Integer.parseInt(b[0])==number[c])
{
five++;
e= Integer.parseInt(b[0]);
if (menu.searchDish(b[2]) != null)
{
R[t] = new Record();
R[t].d = menu.searchDish(b[2]); //输入菜品
}
else
{
System.out.println(b[2] + " does not exist"); //记录错误菜品名
continue;
}
R[t].orderNum = b[1]; //输入序号
R[t].portion = Integer.parseInt(b[3]); //输入份额
R[t].num = Integer.parseInt(b[4]);
t++;
daidianhao=t--;
daidian=daidian+R[t].getprice();
Daidian[qq]=daidian;
System.out.printf("%d table %d pay for table %d %d\n",daidianhao+1,number[qq-1],e,R[t].getprice());
nb[qq]=number[qq-1];
}
}
if(five==0)
{
System.out.println("Table number :"+b[0]+" does not exist");
}
}
}
if(!b[0].equals("table") && !Pattern.matches("\\d", b[0]))
{
zhuohao=1;
tab=1;
System.out.println("wrong format");
}
}
else
{
System.out.println("wrong format");
}
}
}
for(int in=1;in<=qq;in++)
{
if (fuhe[in-1] == 0)
{
pri = tol[in];//输出总金额
if(pri==0)
{
System.out.printf("table %d: 0 0\n",number[in-1]);
}
else
{
if(nb[in]==number[in-1])
{
pri= pri+Daidian[in];
}
pri1= (int) Math.round((pri-tese[in])*Discount[in]+tese[in]*Tdiscount[in]);
System.out.printf("table %d: %d %d\n", number[in-1], pri,pri1);//输出总金额
}
}
if(fuhe[in-1] == 1)
{
System.out.println("table "+in+": out of opening hours");
}
}
}
//菜品类
public static class Dish
{
String name; //菜品名
int unit_price; //单价
public int getPrice(int portion)
{
double price=unit_price;
switch (portion)
{
case 1:
price=unit_price;
break;
case 2:
price=(int) Math.round(unit_price*1.5);
break;
case 3:
price=unit_price*2;
break;
default:
break;
}
return (int) price;
}
}
//菜单类
public static class Menu
{
Dish[] dishes; //菜品数组
int count;
public Dish searchDish(String dishName)
{
for(int i=0;i<count;i++)
{
if(dishName.equals(dishes[i].name))
{
return dishes[i];
}
}
return null;
}
}
//记录类
public static class Record
{
Dish d; //菜品
int portion,num; //份额
String orderNum; //序号
boolean bro;
public boolean isdelete=false;
public int getprice()
{
int price=d.getPrice(portion)*num;
return price;
}
}
//订单类
public static class Order
{
Record[] records; //菜品数组
public void delARecordByOrderNum(String orderNum1, int count) {
int flag = 0, i;
for (i = 0; i < count; i++)
{
if (records[i].orderNum.equals(orderNum1))
{
records[i].bro = true;
flag = 1;
if(records[i].isdelete){
System.out.printf("deduplication %d\n",Integer.parseInt(orderNum1));
}
else
records[i].isdelete=true;
}
}
if(flag==0)
{
System.out.println("delete error;");
}
}
public int getTotalPrice(int count)
{
int totalPrice=0;
for(int i=0;i<count;i++)
if(!records[i].bro)
totalPrice+=records[i].getprice();
return totalPrice;
}
}
public static int zhuo(String str){
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH/mm/ss");
format.setLenient(false);
try {
Date date = format.parse(str);
Date startTime = format.parse("2022/01/01 00/00/00");
Date endTime = format.parse("2023/12/31 23/59/59");

if (date.before(startTime) || date.after(endTime)) {
return 1;
} else {
return 2;
}

} catch (ParseException e) {
return 3;
}
}
}
//"\\d+\\s\\d+\\s\\D+\\s\\d+\\s\\d+"

类图:

 代码分析:在第三次大作业上增加了一个功能:菜单输入时增加特色菜,特色菜的输入格式:菜品名+英文空格+基础价格+"T";除此外考察各种异常处理,因此使用了许多ifelse来判断输入的信息是否符合题目要求,虽然只得了75。

踩坑心得:未使用table类,未设计table类,使得代码可塑性低,增加了主函数的代码量,使用大量的ifelse判断使代码逻辑混乱,给读者的阅读体验较差。

改进建议:增加一个table类,使得代码更加清晰,使得代码短小精悍,去除重复代码及自己多花点时间在Java的学习上。

第五次大作业的题目:

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

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

 

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

 

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

 

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

 

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

 

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

 

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

 

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

 

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

 

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

 

删除记录格式:序号  delete

 

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

 

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

 

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

 

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

 

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

 

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

 

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

 

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

 

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

 

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

 

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

 

Dish {    

 

   String name;//菜品名称    

 

   int unit_price;    //单价    

 

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

 

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

 

Menu {

 

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

 

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

 

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

 

}

 

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

 

Record {

 

   int orderNum;//序号\\

 

   Dish d;//菜品\\

 

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

 

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

 

}

 

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

 

Order {

 

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

 

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

 

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

 

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

 

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

 

}

 

### 输入格式:

 

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

 

菜品记录格式:

 

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

 

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

 

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

 

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

 

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

 

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

 

### 输出格式:

 

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

 

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

 

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

 

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

 

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

 

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

 

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

 

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

例如:麻婆豆腐 川菜 9 T

菜价的计算方法:

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

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

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

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

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

例如:麻婆豆腐 川菜 9 T

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

例如:1 麻婆豆腐 4 1 9

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

acidity num out of range : 5

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

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

如果整桌菜没有特色菜,则只输出table的基本信息,格式如下:

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

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

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

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

 

 

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

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

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

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

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

输出用户支付金额格式:

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

 

 

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

 

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

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

输入格式:

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

 

菜品记录格式:

 

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

 

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

 

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

 

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

 

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

 

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

输出格式:

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

 

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

 

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

 

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

 

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

 

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

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

 

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

输入样例1:

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

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

输出样例1:

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

table 1 out of opening hours
 

输入样例2:

一种口味的菜品。例如:

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

输出样例2:

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

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

 

输入样例3:

辣度值超出范围。例如:

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

输出样例3:

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

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

输入样例4:

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

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

输出样例4:

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

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

输入样例5:

多用户多桌菜。例如:

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

输出样例5:

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

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

输入样例6:

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

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

输出样例6:

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

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

输入样例7:

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

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

输出样例7:

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

wrong format
wrong format
代码:

import java.util.*;
import java.text.SimpleDateFormat;
import java.text.ParseException;
public class Main
{
public static String dateToWeek(String datetime)
{
SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/dd");
String[] weekDays = { "7", "1", "2", "3", "4", "5", "6" };
Calendar cal = Calendar.getInstance(); // 获得一个日历
Date datet = null;
try {
datet = f.parse(datetime);
cal.setTime(datet);
} catch (ParseException e) {
e.printStackTrace();
}
int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
if (w < 0)
w = 0;
return weekDays[w];
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Dish[] d=new Dish[1000];
Menu menu=new Menu();
Record[] R=new Record[1000];
Order order=new Order();
double[] Discount=new double[1000];
double[] Tdiscount=new double[1000];
int[] tol=new int[1000];
int[] Daidian=new int[1000];
int[] chuancai=new int[1000];
int[] chuan2=new int[1000];//chuancai1数组的输出值
String[] chuancai1= {"不辣","微辣","稍辣","辣","很辣","爆辣"};
int[] jincai=new int[1000];
String[] man=new String[100];
String[] haoma=new String[100];
String[] man1=new String[100];
String[] haoma1=new String[100];
int[] Pri=new int[100];
int[] Pri1=new int[100];
int[] pri2=new int[100];
int[] pri3=new int[100];
int[] jin2=new int[1000];
int[] daidian1=new int[1000];
int[] ZHE=new int[1000];
int[] JIN=new int[1000];
int[] CHUAN=new int[1000];
String[] jincai1={"不酸","微酸","稍酸","酸","很酸"};
int[] zhecai=new int[1000];
int[] zhe2=new int[1000];
String[] zhecai1={"不甜","微甜","稍甜","甜"};
String[] te= new String[1000];
int chuan=0,jin=0,zhe=0,chuan1=0,jin1=0,zhe1=0;//chuan1是储存川菜的总数,以此类推
int[] nb=new int[100];
int[] number=new int[1000];
int[] fuhe=new int[100];//fuhe是储存相应桌号的判断是否在营业时间范围内的lag
int i=0,t=0,pri=0,pri1=0,lag=0,fa=1,mber=0,daidian=0,daidianhao,qq=0;//lag判断是否在营业时间范围内,fa判断输出桌号,mber是数组序号
double discount=0,tdiscount=0;
int[] chuanflag=new int[100];
int[] jinflag=new int[100];
int[] zheflag=new int[100];
Human[] human=new Human[10];
String[] shanchu=new String[100];
int[] du=new int[100];
int[] shu=new int[100];
int[] money=new int[100];
int out=0;
while(true)
{
String a=input.nextLine(); //输入信息
String[] b=a.split(" ");
if(a.equals("end"))
{
order.records=R;
tol[qq]=order.getTotalPrice(t);
break;
}
else
{
if(b.length==2)
{
char result=b[0].charAt(0);
if(result>='0'&&result<='9')
{
if(lag==0)
{
if(b[1].equals("delete"))
{
order.records=R; //删除序号
int pan=0;
for(int v=0;v<t;v++)
{
if (b[0].equals(R[v].orderNum))
{
pan = 1;
break;
}
}
if(pan==1)
{
if(shanchu[Integer.parseInt(b[0])]!=null){
if(shanchu[Integer.parseInt(b[0])].equals("川菜"))
{
chuancai[qq]=chuancai[qq]-shu[Integer.parseInt(b[0])];
CHUAN[qq]=CHUAN[qq]-du[Integer.parseInt(b[0])]*shu[Integer.parseInt(b[0])];
chuan2[qq]=Math.round((float) CHUAN[qq]/chuancai[qq]);
Pri1[qq]=Pri1[qq]-money[Integer.parseInt(b[0])];
if(chuancai[qq]==0)
{
chuanflag[qq]=0;
}
}
if(shanchu[Integer.parseInt(b[0])].equals("晋菜"))
{
jincai[qq]=jincai[qq]-shu[Integer.parseInt(b[0])];
JIN[qq]=JIN[qq]-du[Integer.parseInt(b[0])]*shu[Integer.parseInt(b[0])];
jin2[qq]=Math.round((float) JIN[qq]/jincai[qq]);
Pri1[qq]=Pri1[qq]-money[Integer.parseInt(b[0])];
if(jincai[qq]==0)
{
jinflag[qq]=0;
}
}
if(shanchu[Integer.parseInt(b[0])].equals("浙菜"))
{
zhecai[qq]=zhecai[qq]-shu[Integer.parseInt(b[0])];
ZHE[qq]=ZHE[qq]-du[Integer.parseInt(b[0])]*shu[Integer.parseInt(b[0])];
zhe2[qq]=Math.round((float) ZHE[qq]/zhecai[qq]);
Pri1[qq]=Pri1[qq]-money[Integer.parseInt(b[0])];
if(zhecai[qq]==0)
{
zheflag[qq]=0;
}
}
}
}
order.delARecordByOrderNum(b[0],t);
}
else
{
System.out.println("wrong format");
}
}
}
else
{
for(int c=0;c<i;c++) //重复的菜品输入
{
if(b[0].equals(d[c].name))
d[c].unit_price=Integer.parseInt(b[1]);
}
d[i]=new Dish();//添加一道菜品信息
d[i].name=b[0];
d[i].unit_price=Integer.parseInt(b[1]);
i++;
}
}
else if(b.length==4)
{
char num=b[0].charAt(0);
if(num>='0'&&num<='9')
{
if (t == 0) //输入菜单菜品
{
menu.dishes = d;
menu.count = i;
}
if (menu.searchDish(b[1]) != null)
{
R[t] = new Record();
R[t].d = menu.searchDish(b[1]); //输入菜品
}
else
{
System.out.println(b[1] + " does not exist"); //记录错误菜品名
continue;
}
R[t].orderNum = b[0]; //输入序号
R[t].portion = Integer.parseInt(b[2]); //输入份额
R[t].num = Integer.parseInt(b[3]); //输入份数
if(lag==0)
{
System.out.println(R[t].orderNum + " " + R[t].d.name + " " + R[t].getprice());
Pri[qq]= (int) (Pri[qq]+Math.round(R[t].getprice()*Discount[qq]));
}
t++;
}
else
{
for(int c=0;c<i;c++) //重复的菜品输入
{
if(b[0].equals(d[c].name))
d[c].unit_price=Integer.parseInt(b[2]);
}
d[i]=new Dish();//添加一道菜品信息
d[i].name=b[0];
d[i].unit_price=Integer.parseInt(b[2]);
te[i] = b[0];
d[i].caiping=b[1];
i++;
}
}
else if(b.length==5)
{
if(lag==0)
{
char num=b[1].charAt(0);
if(num>='0'&&num<='9')
{
int e;
e= Integer.parseInt(b[0]);
if (menu.searchDish(b[2]) != null)
{
R[t] = new Record();
R[t].d = menu.searchDish(b[2]); //输入菜品
}
else
{
System.out.println(b[2] + " does not exist"); //记录错误菜品名
continue;
}
R[t].orderNum = b[1]; //输入序号
R[t].portion = Integer.parseInt(b[3]); //输入份额
R[t].num = Integer.parseInt(b[4]);
t++;
daidianhao=t--;
daidian=daidian+R[t].getprice();
Daidian[qq]=daidian;
daidian1[qq]= (int) Math.round(Daidian[qq]*Discount[qq]);
if(lag==0)
{
System.out.printf("%d table %d pay for table %d %d\n",daidianhao+1,number[qq-1],e,R[t].getprice());
}
nb[qq]=number[qq-1];
}
else
{
if (t == 0) //输入菜单菜品
{
menu.dishes = d;
menu.count = i;
}
if (menu.searchDish(b[1]) != null)
{
R[t] = new Record();
R[t].d = menu.searchDish(b[1]); //输入菜品
}
else
{
System.out.println(b[1] + " does not exist"); //记录错误菜品名
continue;
}
for(int v=0;v<i;v++)
{
if(lag==0)
{
if(d[v].name.equals(b[1]))
{
if("川菜".equals(d[v].caiping))
{
if(Integer.parseInt(b[2])>5)
{
System.out.println("spicy num out of range :"+b[2]);
break;
}
else
{
chuan=chuan+Integer.parseInt(b[2])*Integer.parseInt(b[4]);
CHUAN[qq]=chuan;
chuan1=chuan1+Integer.parseInt(b[4]);
chuancai[qq]=chuan1;
chuanflag[qq]=1;
chuan2[qq]=Math.round((float) chuan /chuan1);
shanchu[Integer.parseInt(b[0])]="川菜";
du[Integer.parseInt(b[0])]=Integer.parseInt(b[2]);
shu[Integer.parseInt(b[0])]=Integer.parseInt(b[4]);
R[t].orderNum = b[0]; //输入序号
R[t].portion = Integer.parseInt(b[3]); //输入份额
R[t].num = Integer.parseInt(b[4]);//输入份数
System.out.println(R[t].orderNum + " " + R[t].d.name + " " + R[t].getprice());
money[Integer.parseInt(b[0])]= (int) Math.round(R[t].getprice()*Tdiscount[qq]);
Pri1[qq]= (int) (Pri1[qq]+Math.round(R[t].getprice()*Tdiscount[qq]));
break;
}
}
else if("晋菜".equals(d[v].caiping))
{
if(Integer.parseInt(b[2])>4)
{
System.out.println("acidity num out of range :"+b[2]);
break;
}
else
{
jin=jin+Integer.parseInt(b[2])*Integer.parseInt(b[4]);
JIN[qq]=jin;
jin1=jin1+Integer.parseInt(b[4]);
jin2[qq]=Math.round((float) jin /jin1);
jincai[qq]=jin1;
jinflag[qq]=1;
shanchu[Integer.parseInt(b[0])]="晋菜";
du[Integer.parseInt(b[0])]=Integer.parseInt(b[2]);
shu[Integer.parseInt(b[0])]=Integer.parseInt(b[4]);
R[t].orderNum = b[0]; //输入序号
R[t].portion = Integer.parseInt(b[3]); //输入份额
R[t].num = Integer.parseInt(b[4]);//输入份数
System.out.println(R[t].orderNum + " " + R[t].d.name + " " + R[t].getprice());
Pri1[qq]= (int) (Pri1[qq]+Math.round(R[t].getprice()*Tdiscount[qq]));
money[Integer.parseInt(b[0])]= (int) Math.round(R[t].getprice()*Tdiscount[qq]);
break;
}
}
else if("浙菜".equals(d[v].caiping))
{
if(Integer.parseInt(b[2])>3)
{
System.out.println("sweetness num out of range :"+b[2]);
break;
}
else
{
zhe=zhe+Integer.parseInt(b[2])*Integer.parseInt(b[4]);
ZHE[qq]=zhe;
zhe1=zhe1+Integer.parseInt(b[4]);
zhe2[qq]=Math.round((float) zhe /zhe1);
zhecai[qq]=zhe1;
zheflag[qq]=1;
shanchu[Integer.parseInt(b[0])]="浙菜";
du[Integer.parseInt(b[0])]=Integer.parseInt(b[2]);
shu[Integer.parseInt(b[0])]=Integer.parseInt(b[4]);
R[t].orderNum = b[0]; //输入序号
R[t].portion = Integer.parseInt(b[3]); //输入份额
R[t].num = Integer.parseInt(b[4]);//输入份数
System.out.println(R[t].orderNum + " " + R[t].d.name + " " + R[t].getprice());
Pri1[qq]= (int) (Pri1[qq]+Math.round(R[t].getprice()*Tdiscount[qq]));
money[Integer.parseInt(b[0])]= (int) Math.round(R[t].getprice()*Tdiscount[qq]);
break;
}
}
}
}
}
t++;
}
}
}
else if (b.length==6)
{
char pan=b[1].charAt(0);
if(b[0].equals("table"))
{
lag=1;
System.out.println("wrong format");
}
if(pan>='0'&&pan<='9'&&lag==0)
{
int tedai;
tedai= Integer.parseInt(b[0]);
if (menu.searchDish(b[2]) != null)
{
R[t] = new Record();
R[t].d = menu.searchDish(b[2]); //输入菜品
}
else
{
System.out.println(b[2] + " does not exist"); //记录错误菜品名
continue;
}
R[t].orderNum = b[1]; //输入序号
R[t].portion = Integer.parseInt(b[4]); //输入份额
R[t].num = Integer.parseInt(b[5]);
t++;
daidianhao=t--;
daidian=daidian+R[t].getprice();
Daidian[qq]=daidian;
daidian1[qq]=(int) Math.round(Daidian[qq]*Tdiscount[qq]);
if(lag==0)
{
System.out.printf("%d table %d pay for table %d %d\n",Integer.parseInt(b[1]),number[qq-1],tedai,R[t].getprice());
}
nb[qq]=number[qq-1];
for(int v=0;v<i;v++)
{
if(d[v].name.equals(b[2]))
{
if("川菜".equals(d[v].caiping))
{
if(Integer.parseInt(b[3])>5)
{
System.out.println("spicy num out of range :"+b[3]);
break;
}
else
{
chuan=CHUAN[Integer.parseInt(b[0])]+Integer.parseInt(b[3])*Integer.parseInt(b[5]);
chuan1=chuancai[Integer.parseInt(b[0])]+Integer.parseInt(b[5]);
chuancai[Integer.parseInt(b[0])]=chuan1;
chuanflag[Integer.parseInt(b[0])]=1;
chuan2[Integer.parseInt(b[0])]=Math.round((float) chuan /chuan1);
break;
}
}
else if("晋菜".equals(d[v].caiping))
{
if(Integer.parseInt(b[3])>4)
{
System.out.println("acidity num out of range :"+b[3]);
break;
}
else
{
jin=JIN[Integer.parseInt(b[0])]+Integer.parseInt(b[3])*Integer.parseInt(b[5]);
jin1=jincai[Integer.parseInt(b[0])]+Integer.parseInt(b[5]);
jin2[Integer.parseInt(b[0])]=Math.round((float) jin /jin1);
jincai[Integer.parseInt(b[0])]=jin1;
jinflag[Integer.parseInt(b[0])]=1;
break;
}
}
else if ("浙菜".equals(d[v].caiping))
{
if(Integer.parseInt(b[3])>3)
{
System.out.println("sweetness num out of range :"+b[3]);
break;
}
else
{
zhe=ZHE[Integer.parseInt(b[0])]+Integer.parseInt(b[3])*Integer.parseInt(b[5]);
zhe1=zhecai[Integer.parseInt(b[0])]+Integer.parseInt(b[5]);
zhe2[Integer.parseInt(b[0])]=Math.round((float) zhe /zhe1);
zhecai[Integer.parseInt(b[0])]=zhe1;
zheflag[Integer.parseInt(b[0])]=1;
break;
}
}
}
}
}
}
else if(b.length==7)
{
order.records=R;
tol[qq]=order.getTotalPrice(t);
if(a.matches("^table\\s\\d{1,2}\\s:\\s[a-zA-Z]{1,10}\\s(180|181|189|133|135|136)\\d{8}\\s\\d{4}/[0-1]?\\d/[0-3]?\\d\\s([0-1]?\\d|2[0-3])/[0-5]?\\d/[0-5]?\\d$"))
{
chuan=0;
jin=0;
zhe=0;
zhe1=0;
jin1=0;
chuan1=0;
number[qq]= Integer.parseInt(b[1]);
int y,time;
lag=0;
daidian=0;
qq++;
fa--;
t=0;
man[qq] = b[3];
haoma[qq] = b[4];
int[] f=new int[3];
String[] c=b[6].split("/");
f[0]= Integer.parseInt(c[0]);
f[1]= Integer.parseInt(c[1]);
f[2]= Integer.parseInt(c[2]);
y=Integer.parseInt(Main.dateToWeek(b[5]));
time=f[0]*3600+f[1]*60+f[2];
if(y==7||y==6)
{
tdiscount=1;
Tdiscount[qq]=tdiscount;
if(time>=9.5*3600&&time<=21.5*3600)
{
discount=1;
Discount[qq]=discount;
}
else
{
lag=1;
}
fuhe[mber]=lag;
mber++;
}
else
{
tdiscount=0.7;
Tdiscount[qq]=tdiscount;
if(time>=10.5*3600&&time<=14.5*3600)
{
discount=0.6;
Discount[qq]=discount;
fuhe[mber]=lag;
mber++;
}
else if(time>=17*3600&&time<=20.5*3600)
{
discount = 0.8;
Discount[qq]=discount;
fuhe[mber] = lag;
mber++;
}
else
{
lag=1;
fuhe[mber]=lag;
mber++;
}
}
if(fa==0&&lag==0)
{
System.out.printf("table %d: \n",number[qq-1]);
fa++;
}
}
else {
lag=1;
System.out.println("wrong format");
}
}
else
{
if(b[0].equals("table"))
{
lag=1;
}
System.out.println("wrong format");
}
}
}
for(int in=1;in<=qq;in++)
{
if (fuhe[in-1] == 0)
{
pri = tol[in];//输出总金额
if(pri==0)
{
System.out.printf("table %d: 0\n",number[in-1]);
}
else
{
if(nb[in]==number[in-1])
{
pri= pri+Daidian[in];
}
pri1= Pri[in]+Pri1[in]+daidian1[in];
pri2[in]=pri1;
if(chuanflag[in]==1&&jinflag[in]==1&&zheflag[in]==1)
{
System.out.printf("table %d: %d %d 川菜 %d %s 晋菜 %d %s 浙菜 %d %s\n",number[in-1],pri,pri1,chuancai[in],chuancai1[chuan2[in]],jincai[in],jincai1[jin2[in]],zhecai[in],zhecai1[zhe2[in]]);
}
else if(chuanflag[in]==1&&jinflag[in]==1&&zheflag[in]==0)
{
System.out.printf("table %d: %d %d 川菜 %d %s 晋菜 %d %s\n",number[in-1],pri,pri1,chuancai[in],chuancai1[chuan2[in]],jincai[in],jincai1[jin2[in]]);
}
else if(chuanflag[in]==1&&jinflag[in]==0&&zheflag[in]==1)
{
System.out.printf("table %d: %d %d 川菜 %d %s 浙菜 %d %s\n",number[in-1],pri,pri1,chuancai[in],chuancai1[chuan2[in]],zhecai[in],zhecai1[zhe2[in]]);
}
else if (chuanflag[in]==0&&jinflag[in]==1&&zheflag[in]==1)
{
System.out.printf("table %d: %d %d 晋菜 %d %s 浙菜 %d %s\n",number[in-1],pri,pri1,jincai[in],jincai1[jin2[in]],zhecai[in],zhecai1[zhe2[in]]);
}
else if (chuanflag[in]==1&&jinflag[in]==0&&zheflag[in]==0)
{
System.out.printf("table %d: %d %d 川菜 %d %s\n",number[in-1],pri,pri1,chuancai[in],chuancai1[chuan2[in]]);
}
else if (chuanflag[in]==0&&jinflag[in]==1&&zheflag[in]==0)
{
System.out.printf("table %d: %d %d 晋菜 %d %s\n",number[in-1],pri,pri1,jincai[in],jincai1[jin2[in]]);
}
else if(chuanflag[in]==0&&jinflag[in]==0&&zheflag[in]==1)
{
System.out.printf("table %d: %d %d 浙菜 %d %s\n",number[in-1],pri,pri1,zhecai[in],zhecai1[zhe2[in]]);
}
else
{
System.out.printf("table %d: %d %d \n",number[in-1],pri,pri1);
}
}
}
if(fuhe[in-1] == 1)
{
System.out.println("table "+in+" out of opening hours");
}
}
for(int in=1;in<=qq;in++)
{
if (fuhe[in-1] == 0)
{
if(man[in]!=null)
{
for(int v=in;v<=qq;v++)
{
if (man[in] != null && man[v+1] != null && man[in].matches(man[v+1]))
{
man[v+1] = null;
haoma[v+1] = null;
pri2[in] = pri2[in] + pri2[v+1];
}
}
}
}
}
for(int in=1;in<=qq;in++)
{
if(man[in]!=null&&lag==0)
{
man1[out]=man[in];
haoma1[out]=haoma[in];
pri3[out]=pri2[in];
out++;
}
}
for(int v=0;v<out;v++)
{
human[v]=new Human();
human[v].name=man1[v];
human[v].telephone=haoma1[v];
human[v].PRI=pri3[v];
}
for(int y=0;y<out;y++)
{
for(int u= 0;u<out-1;u++){
Human H=new Human();
if(human[u].name.charAt(0)>human[u+1].name.charAt(0)){
H=human[u];
human[u]=human[u+1];
human[u+1]=H;
}
}
}
for(int y=0;y<out;y++)
{
human[y].print();
}
}
//菜品类
public static class Human
{
String name;
String telephone;
int PRI;
public void print(){
System.out.println(name+" "+telephone+" "+PRI);
}
}
public static class Dish
{
String name; //菜品名
int unit_price; //单价
String caiping;
public int getPrice(int portion)
{
double price=unit_price;
switch (portion)
{
case 1:
price=unit_price;
break;
case 2:
price=(int) Math.round(unit_price*1.5);
break;
case 3:
price=unit_price*2;
break;
default:
break;
}
return (int) price;
}
}
//菜单类
public static class Menu
{
Dish[] dishes; //菜品数组
int count;
public Dish searchDish(String dishName)
{
for(int i=0;i<count;i++)
{
if(dishName.equals(dishes[i].name))
{
return dishes[i];
}
}
return null;
}
}
//记录类
public static class Record
{
Dish d; //菜品
int portion,num; //份额
String orderNum;//序号
boolean bro;
public int getprice()
{
int price=d.getPrice(portion)*num;
return price;
}
}
//订单类
public static class Order
{
Record[] records; //菜品数组
public void delARecordByOrderNum(String orderNum1,int count)
{
int flag=0,i;
for(i=0;i<count;i++)
{
if(records[i].orderNum.equals(orderNum1))
{
records[i].bro=true;
flag=1;
}
}
if(flag==0)
{
System.out.println("delete error;");
}
}
public int getTotalPrice(int count)
{
int totalPrice=0;
for(int i=0;i<count;i++)
if(!records[i].bro)
totalPrice+=records[i].getprice();
return totalPrice;
}
}
代码:

import java.util.*;
import java.text.SimpleDateFormat;
import java.text.ParseException;
public class Main
{
public static String dateToWeek(String datetime)
{
SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/dd");
String[] weekDays = { "7", "1", "2", "3", "4", "5", "6" };
Calendar cal = Calendar.getInstance(); // 获得一个日历
Date datet = null;
try {
datet = f.parse(datetime);
cal.setTime(datet);
} catch (ParseException e) {
e.printStackTrace();
}
int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
if (w < 0)
w = 0;
return weekDays[w];
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Dish[] d=new Dish[1000];
Menu menu=new Menu();
Record[] R=new Record[1000];
Order order=new Order();
double[] Discount=new double[1000];
double[] Tdiscount=new double[1000];
int[] tol=new int[1000];
int[] Daidian=new int[1000];
int[] chuancai=new int[1000];
int[] chuan2=new int[1000];//chuancai1数组的输出值
String[] chuancai1= {"不辣","微辣","稍辣","辣","很辣","爆辣"};
int[] jincai=new int[1000];
String[] man=new String[100];
String[] haoma=new String[100];
String[] man1=new String[100];
String[] haoma1=new String[100];
int[] Pri=new int[100];
int[] Pri1=new int[100];
int[] pri2=new int[100];
int[] pri3=new int[100];
int[] jin2=new int[1000];
int[] daidian1=new int[1000];
int[] ZHE=new int[1000];
int[] JIN=new int[1000];
int[] CHUAN=new int[1000];
String[] jincai1={"不酸","微酸","稍酸","酸","很酸"};
int[] zhecai=new int[1000];
int[] zhe2=new int[1000];
String[] zhecai1={"不甜","微甜","稍甜","甜"};
String[] te= new String[1000];
int chuan=0,jin=0,zhe=0,chuan1=0,jin1=0,zhe1=0;//chuan1是储存川菜的总数,以此类推
int[] nb=new int[100];
int[] number=new int[1000];
int[] fuhe=new int[100];//fuhe是储存相应桌号的判断是否在营业时间范围内的lag
int i=0,t=0,pri=0,pri1=0,lag=0,fa=1,mber=0,daidian=0,daidianhao,qq=0;//lag判断是否在营业时间范围内,fa判断输出桌号,mber是数组序号
double discount=0,tdiscount=0;
int[] chuanflag=new int[100];
int[] jinflag=new int[100];
int[] zheflag=new int[100];
Human[] human=new Human[10];
String[] shanchu=new String[100];
int[] du=new int[100];
int[] shu=new int[100];
int[] money=new int[100];
int out=0;
while(true)
{
String a=input.nextLine(); //输入信息
String[] b=a.split(" ");
if(a.equals("end"))
{
order.records=R;
tol[qq]=order.getTotalPrice(t);
break;
}
else
{
if(b.length==2)
{
char result=b[0].charAt(0);
if(result>='0'&&result<='9')
{
if(lag==0)
{
if(b[1].equals("delete"))
{
order.records=R; //删除序号
int pan=0;
for(int v=0;v<t;v++)
{
if (b[0].equals(R[v].orderNum))
{
pan = 1;
break;
}
}
if(pan==1)
{
if(shanchu[Integer.parseInt(b[0])]!=null){
if(shanchu[Integer.parseInt(b[0])].equals("川菜"))
{
chuancai[qq]=chuancai[qq]-shu[Integer.parseInt(b[0])];
CHUAN[qq]=CHUAN[qq]-du[Integer.parseInt(b[0])]*shu[Integer.parseInt(b[0])];
chuan2[qq]=Math.round((float) CHUAN[qq]/chuancai[qq]);
Pri1[qq]=Pri1[qq]-money[Integer.parseInt(b[0])];
if(chuancai[qq]==0)
{
chuanflag[qq]=0;
}
}
if(shanchu[Integer.parseInt(b[0])].equals("晋菜"))
{
jincai[qq]=jincai[qq]-shu[Integer.parseInt(b[0])];
JIN[qq]=JIN[qq]-du[Integer.parseInt(b[0])]*shu[Integer.parseInt(b[0])];
jin2[qq]=Math.round((float) JIN[qq]/jincai[qq]);
Pri1[qq]=Pri1[qq]-money[Integer.parseInt(b[0])];
if(jincai[qq]==0)
{
jinflag[qq]=0;
}
}
if(shanchu[Integer.parseInt(b[0])].equals("浙菜"))
{
zhecai[qq]=zhecai[qq]-shu[Integer.parseInt(b[0])];
ZHE[qq]=ZHE[qq]-du[Integer.parseInt(b[0])]*shu[Integer.parseInt(b[0])];
zhe2[qq]=Math.round((float) ZHE[qq]/zhecai[qq]);
Pri1[qq]=Pri1[qq]-money[Integer.parseInt(b[0])];
if(zhecai[qq]==0)
{
zheflag[qq]=0;
}
}
}
}
order.delARecordByOrderNum(b[0],t);
}
else
{
System.out.println("wrong format");
}
}
}
else
{
for(int c=0;c<i;c++) //重复的菜品输入
{
if(b[0].equals(d[c].name))
d[c].unit_price=Integer.parseInt(b[1]);
}
d[i]=new Dish();//添加一道菜品信息
d[i].name=b[0];
d[i].unit_price=Integer.parseInt(b[1]);
i++;
}
}
else if(b.length==4)
{
char num=b[0].charAt(0);
if(num>='0'&&num<='9')
{
if (t == 0) //输入菜单菜品
{
menu.dishes = d;
menu.count = i;
}
if (menu.searchDish(b[1]) != null)
{
R[t] = new Record();
R[t].d = menu.searchDish(b[1]); //输入菜品
}
else
{
System.out.println(b[1] + " does not exist"); //记录错误菜品名
continue;
}
R[t].orderNum = b[0]; //输入序号
R[t].portion = Integer.parseInt(b[2]); //输入份额
R[t].num = Integer.parseInt(b[3]); //输入份数
if(lag==0)
{
System.out.println(R[t].orderNum + " " + R[t].d.name + " " + R[t].getprice());
Pri[qq]= (int) (Pri[qq]+Math.round(R[t].getprice()*Discount[qq]));
}
t++;
}
else
{
for(int c=0;c<i;c++) //重复的菜品输入
{
if(b[0].equals(d[c].name))
d[c].unit_price=Integer.parseInt(b[2]);
}
d[i]=new Dish();//添加一道菜品信息
d[i].name=b[0];
d[i].unit_price=Integer.parseInt(b[2]);
te[i] = b[0];
d[i].caiping=b[1];
i++;
}
}
else if(b.length==5)
{
if(lag==0)
{
char num=b[1].charAt(0);
if(num>='0'&&num<='9')
{
int e;
e= Integer.parseInt(b[0]);
if (menu.searchDish(b[2]) != null)
{
R[t] = new Record();
R[t].d = menu.searchDish(b[2]); //输入菜品
}
else
{
System.out.println(b[2] + " does not exist"); //记录错误菜品名
continue;
}
R[t].orderNum = b[1]; //输入序号
R[t].portion = Integer.parseInt(b[3]); //输入份额
R[t].num = Integer.parseInt(b[4]);
t++;
daidianhao=t--;
daidian=daidian+R[t].getprice();
Daidian[qq]=daidian;
daidian1[qq]= (int) Math.round(Daidian[qq]*Discount[qq]);
if(lag==0)
{
System.out.printf("%d table %d pay for table %d %d\n",daidianhao+1,number[qq-1],e,R[t].getprice());
}
nb[qq]=number[qq-1];
}
else
{
if (t == 0) //输入菜单菜品
{
menu.dishes = d;
menu.count = i;
}
if (menu.searchDish(b[1]) != null)
{
R[t] = new Record();
R[t].d = menu.searchDish(b[1]); //输入菜品
}
else
{
System.out.println(b[1] + " does not exist"); //记录错误菜品名
continue;
}
for(int v=0;v<i;v++)
{
if(lag==0)
{
if(d[v].name.equals(b[1]))
{
if("川菜".equals(d[v].caiping))
{
if(Integer.parseInt(b[2])>5)
{
System.out.println("spicy num out of range :"+b[2]);
break;
}
else
{
chuan=chuan+Integer.parseInt(b[2])*Integer.parseInt(b[4]);
CHUAN[qq]=chuan;
chuan1=chuan1+Integer.parseInt(b[4]);
chuancai[qq]=chuan1;
chuanflag[qq]=1;
chuan2[qq]=Math.round((float) chuan /chuan1);
shanchu[Integer.parseInt(b[0])]="川菜";
du[Integer.parseInt(b[0])]=Integer.parseInt(b[2]);
shu[Integer.parseInt(b[0])]=Integer.parseInt(b[4]);
R[t].orderNum = b[0]; //输入序号
R[t].portion = Integer.parseInt(b[3]); //输入份额
R[t].num = Integer.parseInt(b[4]);//输入份数
System.out.println(R[t].orderNum + " " + R[t].d.name + " " + R[t].getprice());
money[Integer.parseInt(b[0])]= (int) Math.round(R[t].getprice()*Tdiscount[qq]);
Pri1[qq]= (int) (Pri1[qq]+Math.round(R[t].getprice()*Tdiscount[qq]));
break;
}
}
else if("晋菜".equals(d[v].caiping))
{
if(Integer.parseInt(b[2])>4)
{
System.out.println("acidity num out of range :"+b[2]);
break;
}
else
{
jin=jin+Integer.parseInt(b[2])*Integer.parseInt(b[4]);
JIN[qq]=jin;
jin1=jin1+Integer.parseInt(b[4]);
jin2[qq]=Math.round((float) jin /jin1);
jincai[qq]=jin1;
jinflag[qq]=1;
shanchu[Integer.parseInt(b[0])]="晋菜";
du[Integer.parseInt(b[0])]=Integer.parseInt(b[2]);
shu[Integer.parseInt(b[0])]=Integer.parseInt(b[4]);
R[t].orderNum = b[0]; //输入序号
R[t].portion = Integer.parseInt(b[3]); //输入份额
R[t].num = Integer.parseInt(b[4]);//输入份数
System.out.println(R[t].orderNum + " " + R[t].d.name + " " + R[t].getprice());
Pri1[qq]= (int) (Pri1[qq]+Math.round(R[t].getprice()*Tdiscount[qq]));
money[Integer.parseInt(b[0])]= (int) Math.round(R[t].getprice()*Tdiscount[qq]);
break;
}
}
else if("浙菜".equals(d[v].caiping))
{
if(Integer.parseInt(b[2])>3)
{
System.out.println("sweetness num out of range :"+b[2]);
break;
}
else
{
zhe=zhe+Integer.parseInt(b[2])*Integer.parseInt(b[4]);
ZHE[qq]=zhe;
zhe1=zhe1+Integer.parseInt(b[4]);
zhe2[qq]=Math.round((float) zhe /zhe1);
zhecai[qq]=zhe1;
zheflag[qq]=1;
shanchu[Integer.parseInt(b[0])]="浙菜";
du[Integer.parseInt(b[0])]=Integer.parseInt(b[2]);
shu[Integer.parseInt(b[0])]=Integer.parseInt(b[4]);
R[t].orderNum = b[0]; //输入序号
R[t].portion = Integer.parseInt(b[3]); //输入份额
R[t].num = Integer.parseInt(b[4]);//输入份数
System.out.println(R[t].orderNum + " " + R[t].d.name + " " + R[t].getprice());
Pri1[qq]= (int) (Pri1[qq]+Math.round(R[t].getprice()*Tdiscount[qq]));
money[Integer.parseInt(b[0])]= (int) Math.round(R[t].getprice()*Tdiscount[qq]);
break;
}
}
}
}
}
t++;
}
}
}
else if (b.length==6)
{
char pan=b[1].charAt(0);
if(b[0].equals("table"))
{
lag=1;
System.out.println("wrong format");
}
if(pan>='0'&&pan<='9'&&lag==0)
{
int tedai;
tedai= Integer.parseInt(b[0]);
if (menu.searchDish(b[2]) != null)
{
R[t] = new Record();
R[t].d = menu.searchDish(b[2]); //输入菜品
}
else
{
System.out.println(b[2] + " does not exist"); //记录错误菜品名
continue;
}
R[t].orderNum = b[1]; //输入序号
R[t].portion = Integer.parseInt(b[4]); //输入份额
R[t].num = Integer.parseInt(b[5]);
t++;
daidianhao=t--;
daidian=daidian+R[t].getprice();
Daidian[qq]=daidian;
daidian1[qq]=(int) Math.round(Daidian[qq]*Tdiscount[qq]);
if(lag==0)
{
System.out.printf("%d table %d pay for table %d %d\n",Integer.parseInt(b[1]),number[qq-1],tedai,R[t].getprice());
}
nb[qq]=number[qq-1];
for(int v=0;v<i;v++)
{
if(d[v].name.equals(b[2]))
{
if("川菜".equals(d[v].caiping))
{
if(Integer.parseInt(b[3])>5)
{
System.out.println("spicy num out of range :"+b[3]);
break;
}
else
{
chuan=CHUAN[Integer.parseInt(b[0])]+Integer.parseInt(b[3])*Integer.parseInt(b[5]);
chuan1=chuancai[Integer.parseInt(b[0])]+Integer.parseInt(b[5]);
chuancai[Integer.parseInt(b[0])]=chuan1;
chuanflag[Integer.parseInt(b[0])]=1;
chuan2[Integer.parseInt(b[0])]=Math.round((float) chuan /chuan1);
break;
}
}
else if("晋菜".equals(d[v].caiping))
{
if(Integer.parseInt(b[3])>4)
{
System.out.println("acidity num out of range :"+b[3]);
break;
}
else
{
jin=JIN[Integer.parseInt(b[0])]+Integer.parseInt(b[3])*Integer.parseInt(b[5]);
jin1=jincai[Integer.parseInt(b[0])]+Integer.parseInt(b[5]);
jin2[Integer.parseInt(b[0])]=Math.round((float) jin /jin1);
jincai[Integer.parseInt(b[0])]=jin1;
jinflag[Integer.parseInt(b[0])]=1;
break;
}
}
else if ("浙菜".equals(d[v].caiping))
{
if(Integer.parseInt(b[3])>3)
{
System.out.println("sweetness num out of range :"+b[3]);
break;
}
else
{
zhe=ZHE[Integer.parseInt(b[0])]+Integer.parseInt(b[3])*Integer.parseInt(b[5]);
zhe1=zhecai[Integer.parseInt(b[0])]+Integer.parseInt(b[5]);
zhe2[Integer.parseInt(b[0])]=Math.round((float) zhe /zhe1);
zhecai[Integer.parseInt(b[0])]=zhe1;
zheflag[Integer.parseInt(b[0])]=1;
break;
}
}
}
}
}
}
else if(b.length==7)
{
order.records=R;
tol[qq]=order.getTotalPrice(t);
if(a.matches("^table\\s\\d{1,2}\\s:\\s[a-zA-Z]{1,10}\\s(180|181|189|133|135|136)\\d{8}\\s\\d{4}/[0-1]?\\d/[0-3]?\\d\\s([0-1]?\\d|2[0-3])/[0-5]?\\d/[0-5]?\\d$"))
{
chuan=0;
jin=0;
zhe=0;
zhe1=0;
jin1=0;
chuan1=0;
number[qq]= Integer.parseInt(b[1]);
int y,time;
lag=0;
daidian=0;
qq++;
fa--;
t=0;
man[qq] = b[3];
haoma[qq] = b[4];
int[] f=new int[3];
String[] c=b[6].split("/");
f[0]= Integer.parseInt(c[0]);
f[1]= Integer.parseInt(c[1]);
f[2]= Integer.parseInt(c[2]);
y=Integer.parseInt(Main.dateToWeek(b[5]));
time=f[0]*3600+f[1]*60+f[2];
if(y==7||y==6)
{
tdiscount=1;
Tdiscount[qq]=tdiscount;
if(time>=9.5*3600&&time<=21.5*3600)
{
discount=1;
Discount[qq]=discount;
}
else
{
lag=1;
}
fuhe[mber]=lag;
mber++;
}
else
{
tdiscount=0.7;
Tdiscount[qq]=tdiscount;
if(time>=10.5*3600&&time<=14.5*3600)
{
discount=0.6;
Discount[qq]=discount;
fuhe[mber]=lag;
mber++;
}
else if(time>=17*3600&&time<=20.5*3600)
{
discount = 0.8;
Discount[qq]=discount;
fuhe[mber] = lag;
mber++;
}
else
{
lag=1;
fuhe[mber]=lag;
mber++;
}
}
if(fa==0&&lag==0)
{
System.out.printf("table %d: \n",number[qq-1]);
fa++;
}
}
else {
lag=1;
System.out.println("wrong format");
}
}
else
{
if(b[0].equals("table"))
{
lag=1;
}
System.out.println("wrong format");
}
}
}
for(int in=1;in<=qq;in++)
{
if (fuhe[in-1] == 0)
{
pri = tol[in];//输出总金额
if(pri==0)
{
System.out.printf("table %d: 0\n",number[in-1]);
}
else
{
if(nb[in]==number[in-1])
{
pri= pri+Daidian[in];
}
pri1= Pri[in]+Pri1[in]+daidian1[in];
pri2[in]=pri1;
if(chuanflag[in]==1&&jinflag[in]==1&&zheflag[in]==1)
{
System.out.printf("table %d: %d %d 川菜 %d %s 晋菜 %d %s 浙菜 %d %s\n",number[in-1],pri,pri1,chuancai[in],chuancai1[chuan2[in]],jincai[in],jincai1[jin2[in]],zhecai[in],zhecai1[zhe2[in]]);
}
else if(chuanflag[in]==1&&jinflag[in]==1&&zheflag[in]==0)
{
System.out.printf("table %d: %d %d 川菜 %d %s 晋菜 %d %s\n",number[in-1],pri,pri1,chuancai[in],chuancai1[chuan2[in]],jincai[in],jincai1[jin2[in]]);
}
else if(chuanflag[in]==1&&jinflag[in]==0&&zheflag[in]==1)
{
System.out.printf("table %d: %d %d 川菜 %d %s 浙菜 %d %s\n",number[in-1],pri,pri1,chuancai[in],chuancai1[chuan2[in]],zhecai[in],zhecai1[zhe2[in]]);
}
else if (chuanflag[in]==0&&jinflag[in]==1&&zheflag[in]==1)
{
System.out.printf("table %d: %d %d 晋菜 %d %s 浙菜 %d %s\n",number[in-1],pri,pri1,jincai[in],jincai1[jin2[in]],zhecai[in],zhecai1[zhe2[in]]);
}
else if (chuanflag[in]==1&&jinflag[in]==0&&zheflag[in]==0)
{
System.out.printf("table %d: %d %d 川菜 %d %s\n",number[in-1],pri,pri1,chuancai[in],chuancai1[chuan2[in]]);
}
else if (chuanflag[in]==0&&jinflag[in]==1&&zheflag[in]==0)
{
System.out.printf("table %d: %d %d 晋菜 %d %s\n",number[in-1],pri,pri1,jincai[in],jincai1[jin2[in]]);
}
else if(chuanflag[in]==0&&jinflag[in]==0&&zheflag[in]==1)
{
System.out.printf("table %d: %d %d 浙菜 %d %s\n",number[in-1],pri,pri1,zhecai[in],zhecai1[zhe2[in]]);
}
else
{
System.out.printf("table %d: %d %d \n",number[in-1],pri,pri1);
}
}
}
if(fuhe[in-1] == 1)
{
System.out.println("table "+in+" out of opening hours");
}
}
for(int in=1;in<=qq;in++)
{
if (fuhe[in-1] == 0)
{
if(man[in]!=null)
{
for(int v=in;v<=qq;v++)
{
if (man[in] != null && man[v+1] != null && man[in].matches(man[v+1]))
{
man[v+1] = null;
haoma[v+1] = null;
pri2[in] = pri2[in] + pri2[v+1];
}
}
}
}
}
for(int in=1;in<=qq;in++)
{
if(man[in]!=null&&lag==0)
{
man1[out]=man[in];
haoma1[out]=haoma[in];
pri3[out]=pri2[in];
out++;
}
}
for(int v=0;v<out;v++)
{
human[v]=new Human();
human[v].name=man1[v];
human[v].telephone=haoma1[v];
human[v].PRI=pri3[v];
}
for(int y=0;y<out;y++)
{
for(int u= 0;u<out-1;u++){
Human H=new Human();
if(human[u].name.charAt(0)>human[u+1].name.charAt(0)){
H=human[u];
human[u]=human[u+1];
human[u+1]=H;
}
}
}
for(int y=0;y<out;y++)
{
human[y].print();
}
}
//菜品类
public static class Human
{
String name;
String telephone;
int PRI;
public void print(){
System.out.println(name+" "+telephone+" "+PRI);
}
}
public static class Dish
{
String name; //菜品名
int unit_price; //单价
String caiping;
public int getPrice(int portion)
{
double price=unit_price;
switch (portion)
{
case 1:
price=unit_price;
break;
case 2:
price=(int) Math.round(unit_price*1.5);
break;
case 3:
price=unit_price*2;
break;
default:
break;
}
return (int) price;
}
}
//菜单类
public static class Menu
{
Dish[] dishes; //菜品数组
int count;
public Dish searchDish(String dishName)
{
for(int i=0;i<count;i++)
{
if(dishName.equals(dishes[i].name))
{
return dishes[i];
}
}
return null;
}
}
//记录类
public static class Record
{
Dish d; //菜品
int portion,num; //份额
String orderNum;//序号
boolean bro;
public int getprice()
{
int price=d.getPrice(portion)*num;
return price;
}
}
//订单类
public static class Order
{
Record[] records; //菜品数组
public void delARecordByOrderNum(String orderNum1,int count)
{
int flag=0,i;
for(i=0;i<count;i++)
{
if(records[i].orderNum.equals(orderNum1))
{
records[i].bro=true;
flag=1;
}
}
if(flag==0)
{
System.out.println("delete error;");
}
}
public int getTotalPrice(int count)
{
int totalPrice=0;
for(int i=0;i<count;i++)
if(!records[i].bro)
totalPrice+=records[i].getprice();
return totalPrice;
}
}
}

类图:

 代码分析:

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

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

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

对此我将人名和号码及价钱用一个human类表示,极大的便利我的编译,以及在dish中增加了一个属性判断菜的品种,是川菜,浙菜,晋菜的某一种。用冒泡排序来处理名字的顺序,之后依次输出此桌信息。

踩坑:设置数组储存数据,到主函数中调用却成了空数组。编写删除函数,未改变储存的字符位置,数字数组却储存了文字,结果报错在点份额的return price造成数据损失(应写成return (int) price); 该题目判断非法格式时,未使用split,不知如何对输入的信息进行分析;对格式的判断过于复杂且不够正确,使用了许多if语句,导致代码太长,对整个输入格式的考虑不周全,容易存在隐患。所以以后做类似非法格式判断的题目应该学习正则表达式用来判断输入格式的合法性,减少过多的代码,让整个程序都看起来简洁明了,注意输入格式字符的前后顺序;菜单类中未有这中菜时未返回null;储存的数组信息超出数组范围;

主要困难:不知道如何对输入的信息进行判断,是点餐呢?还是储存菜品信息?还是删除信息?还是结束点餐?不知道如何添加菜品信息;未理解类的使用,习惯性的想使用面向过程,但导致程序无法运行;使用了大量重复类型的方法,是否可用继承与多态的形式

改进:多编写几个类储存信息,去除多余的数组使得代码更加简洁明了。使用多态去简化代码。

期中考试题目:

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


image.png

输入格式:

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

输出格式:

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

输入样例:

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

6 5.8 -7 8.9
 

输出样例:

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

40.30
代码:

import java.util.Scanner;

public class Main {
public static void main(String[] args){
Scanner in=new Scanner(System.in);
double x1=in.nextDouble();
double y1=in.nextDouble();
double x2=in.nextDouble();
double y2=in.nextDouble();
Point point=new Point();
Rectangle rectangle=new Rectangle();
point.setX(x1);
point.setY(y1);
rectangle.setTopleftpoint(point);
point.setX(x2);
point.setY(y2);
rectangle.setLowerrightpoint(point);
rectangle.getarea();
}
}
class Rectangle{
Point topleftpoint;
Point lowerrightpoint;
double f,g;

public Point getLowerrightpoint() {
return lowerrightpoint;
}

public void setLowerrightpoint(Point lowerrightpoint) {
this.lowerrightpoint = lowerrightpoint;
}

public Point getTopleftpoint() {
return topleftpoint;
}

public void setTopleftpoint(Point topleftpoint) {
this.topleftpoint = topleftpoint;
}
public double getlength(){
return Math.sqrt(getLowerrightpoint().getX()-getTopleftpoint().getX());
}
public double getheight(){
return Math.sqrt(getTopleftpoint().getY()-getLowerrightpoint().getY());
}
public void getarea(){
System.out.printf("%.2f",getheight()*getlength());
}
}
class Point{
double x,y;

public double getX() {
return x;
}

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

public double getY() {
return y;
}

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

题目:

将测验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
 题目:

在测验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:

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

98.52
 

输入样例2:

2
5.6
-32.5
9.4
-5.6
 

输出样例2:

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

102.22
心得:这次考试使我深刻明白了自己有多少基础知识未掌握,这段时间有点放松,使得自己考试连基础题都未做完,让我的室友深感震惊,叫我回去补习,不懂的地方问他们。
总结:

本次Java实验是一次很好的学习机会,通过这次实验,我对Java编程语言有了更深入的理解和认识。在实验过程中,我不仅掌握了Java的基本语法,还学会了如何使用Java开发工具、调试程序以及面向对象编程思想等方面的知识。首先,我对Java基本语法有了更加深入的了解。在实验中,我通过自己编写简单的程序和实现老师给出的题目,熟练地掌握了Java的数据类型、运算符、控制语句等基本语法,并且了解了Java的关键字、标识符、注释等语言元素。同时,我也学会了如何编写函数、类、接口和包等模块化的代码结构,在编写代码时,我们不仅要注重代码的功能和效率,还要注重代码的结构和美观度。良好的代码风格和清晰的注释可以让我们更好地理解代码,并帮助其他人阅读和修改我们的代码,提高代码的可维护性。再次,Java实验中需要多进行代码测试和调试va实验对于我们来说是一个锻炼编程能力的好机会。通过实验,我们可以更深入地理解Java语言的特性、语法以及面向对象的思想,这些都为我后续的Java编程打下了坚实的基础。其次,我学会了如何使用Java开发工具。在实验中,我们使用Eclipse作为Java开发工具,通过安装JDK并进行配置,成功地创建了Java项目和包,编写了符合Java规范要求的代码。同时,我也学会了如何调试程序,包括断点调试、单步执行、变量监视等常用功能,这些技能在日后的Java项目开发中极为重要。
最后,我对面向对象编程思想有了更加深入的认识。在实验过程中,我们学习了Java的类、对象、继承、多态等特性,并且通过实践理解了抽象类、接口和异常处理等高级概念。这些知识不仅对于Java编程来说是必不可少的,而且也为我们日后的软件工程开发打下了坚实的基础。总之,通过本次Java实验,我对Java编程语言有了更加深入的认识和掌握,同时也使我意识到了自己还需要不断进行学习和实践才能成为一名优秀的Java程序员。我相信,在不久的将来,我能够应用所学的Java知识,为软件工程领域的发展做出自己的贡献。

 

 

posted @ 2023-05-16 20:31  十八岁软硬兼吃  阅读(51)  评论(0)    收藏  举报