首页 > 其他分享 >BLOG-2

BLOG-2

时间:2023-06-30 19:56:30浏览次数:33  
标签:空格 String int BLOG table new out

一.前言

1.第四次题目集

①知识点:啥都考了。

②题量:少。

③难度:很难。

2.第五次题目集

①知识点:啥都考了。

②题量:少。

③难度:难上加难。

3.期中考试

①知识点:Java基础语法:例如变量定义、常量定义、数据类型、循环、条件语句等。 面向对象编程:例如类和对象的定义,封装、继承和多态的概念,方法的重载和重写等。 

②题量:一般。

③难度:较容易。

二.设计与分析

1.第四次题目集——菜单计价程序-4

①题目如下

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

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

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

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

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

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

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

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

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

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

删除记录格式:序号 delete

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

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

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

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

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

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

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

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

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

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

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

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

Dish {

String name;//菜品名称

int unit_price; //单价

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

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

Menu {

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

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

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

}

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

Record {

int orderNum;//序号

Dish d;//菜品\\

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

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

}

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

Order {

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

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

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

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

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

}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

例如:麻婆豆腐 9 T

菜价的计算方法:

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

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

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

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

输入格式:

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

菜品记录格式:

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

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

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

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

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

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

输出格式:

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

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

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

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

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

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

输入样例:

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

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

输出样例:

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

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

输入样例1:

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

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

输出样例1:

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

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

输入样例2:

桌号信息错误。例如:

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

输出样例2:

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

wrong format
 

输入样例3:

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

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

输出样例3:

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

wrong format
 

输入样例4:

错误的菜谱记录。例如:

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

输出样例4:

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

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

输入样例5:

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

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

输出样例5:

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

wrong format
 

输入样例6:

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

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

输出样例6:

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

table 1: 
1 麻婆豆腐 12
2 油淋生菜 14
wrong format
record serial number sequence error
record serial number sequence error
table 1: 26 17
②代码如下
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 准备菜单
//Map<String,String> map=new HashMap<>();
Scanner in = new Scanner(System.in);
Menu menu = new Menu();
Dish dish;
Table currenttable;
Table[] table = new Table[100];
int tableCount = 0;
String line = in.nextLine();
while(line==null) {
System.out.println("wrong format");
line = in.nextLine();
}
while (!line.equals("end")) {
String[] s = line.split(" ");
int l = s.length;
if (l == 2 || l == 3) {

if (s[1].indexOf('.') != -1||s[1].charAt(0)=='0')//判断菜品是否合法(价格不能是浮点数,并且范围在(0,300))//先不判断菜品名是否正确
{
System.out.println("wrong format");
line = in.nextLine();
continue;
}

try {
int dishPrice = Integer.parseInt(s[1]);
} catch (Exception e) {
System.out.println("wrong format");
line = in.nextLine();
continue;
}

int dishPrice = Integer.parseInt(s[1]);
if(dishPrice < 0 || dishPrice >= 300)
{
System.out.println(s[0]+" price out of range "+dishPrice);
line = in.nextLine();
continue;
}
String dishName = s[0];
if (l == 2) {
dish = new Dish(dishName, dishPrice);
menu.add(dish);
} else {
boolean T = true;
dish = new Dish(dishName, dishPrice, T);
menu.add(dish);
}
line = in.nextLine();
while(line==null) {
System.out.println("wrong format");
line = in.nextLine();
}
} else break;
}
while (!line.equals("end")) {
String[] s = line.split(" ");
if (s[0].charAt(0) == 't')
{
boolean f1=false;
while (!line.equals("end")) {
boolean f2=false;
String[] s1 = line.split(" ");
if (s1[0].equals("table")) {
String useDay = s1[2];
String useTimeOfDay = s1[3];
if(s1[1].length()==1)
{
if (s1[1].charAt(0) <='0' || s1[1].charAt(0) > '9'||!checkTime.checkDateformat1(s1[2])||!checkTime.checkDateformat2(s1[3])) {
currenttable = new Table();
table[++tableCount] = currenttable;
table[tableCount].orderWrong = true;
f1=true;
}
}
else
{
if(s1[1].length()>=2)
{
if (s1[1].charAt(0) <='0' || s1[1].charAt(0) > '9'||!checkTime.checkDateformat1(s1[2])||!checkTime.checkDateformat2(s1[3])) {
currenttable = new Table();
table[++tableCount] = currenttable;
table[tableCount].orderWrong = true;
f1=true;
}
else{
try {
int tableNum = Integer.parseInt(s1[1]);
} catch (Exception e) {
currenttable = new Table();
table[++tableCount] = currenttable;
table[tableCount].orderWrong = true;
f1=true;
}
}
}
}
if(!f1)
{

int tableNum = Integer.parseInt(s1[1]);
if (tableNum < 1 || tableNum > 55) {
currenttable = new Table(false,1,tableNum);
table[++tableCount] = currenttable;
f2=true;
}
if (!checkTime.checkDateError(s1[2])&&!f2) {//检查年月日;时分秒是否合法
currenttable = new Table(false,2,tableNum);
table[++tableCount] = currenttable;
f2=true;
}
if (!f2&&!checkTime.checkTimeValid(s1[2])) {
currenttable = new Table(false,3,tableNum);
table[++tableCount] = currenttable;
f2=true;
}
}
if(!f1&&!f2) {
int tableNum = Integer.parseInt(s1[1]);
currenttable = new Table(tableNum, useDay, useTimeOfDay, menu);
table[++tableCount] = currenttable;
}
line = in.nextLine();
while(line==null) {
System.out.println("wrong format");
line = in.nextLine();
}
String[] s2 = line.split(" ");
if (s2[0].charAt(0) != 't' && !s2[0].equals("end"))//输入该桌子的菜
{
while (true)
{
String[] s3 = line.split(" ");
if(s3[0].equals("end")||s3[0].charAt(0)=='t')
{
break;
}
if (s3.length == 2 && s3[1].equals("delete")&&s3[0].charAt(0)>='1'&&s3[0].charAt(0)<='9'&&!f2&&!f1)
{
//删除点菜记录
boolean success=table[tableCount].tableOrder.delARecordByOrderNum(Integer.parseInt(s3[0]));
table[tableCount].tableOrder.addARecord(Integer.parseInt(s3[0]),success);
}
else
{
if(s3.length>=2&&s3.length<5&&!f2&&!f1) {
if (s3[0].charAt(0) <= '9' && s3[0].charAt(0) >= '1' && !f2 && !f1) {
//向该桌的order对象存菜
int sno = Integer.parseInt(s3[0]);
String dishName = s3[1];
int portion = Integer.parseInt(s3[2]);
int orderNum = Integer.parseInt(s3[3]);
if (s3[2].length() != 1 || s3[3].charAt(0) <= '0' || s3[3].charAt(0) > '9') {
table[tableCount].orderWrong = true;
}
table[tableCount].tableOrder.addARecord(sno, dishName, portion, orderNum);
} else if (s3[0].charAt(0) < '0' || s3[0].charAt(0) > '9' && !f2 && !f1) {
boolean mixDish = true;
table[tableCount].tableOrder.addARecord(mixDish);
} else if (s3[0].charAt(0) == '0' || s3[0].charAt(0) > '9' && !f2 && !f1)
table[tableCount].tableOrder.addWrongFormatRecord(true);
}
else if(s3.length==5&&!f2&&!f1)
{
// System.out.println("代点菜");
int toTable=Integer.parseInt(s3[0]);
int sno=Integer.parseInt(s3[1]);
String dishName=s3[2];
int portion=Integer.parseInt(s3[3]);
int orderNum=Integer.parseInt(s3[4]);
boolean toSuccess=false;
for(int i=1;i<=tableCount;i++)
{
if(table[i].tableNum==toTable)
{
toSuccess=true;
break;
}
}
table[tableCount].tableOrder.addARecord(toSuccess,toTable,sno, dishName, portion, orderNum);
}
}
line=in.nextLine();
while(line==null) {
System.out.println("wrong format");
line = in.nextLine();
}
}
}
}
}
else
{
table[tableCount].tableOrder.addWrongFormatRecord(true);
line = in.nextLine();
if(line==null) {
System.out.println("wrong format");
line = in.nextLine();
}
String[] s2 = line.split(" ");
if (s2[0].charAt(0) != 't' && !s2[0].equals("end"))//输入该桌子的菜
{
while (true)
{
String[] s3 = line.split(" ");
if(s3[0].equals("end")||s3[0].charAt(0)=='t')
{
break;
}
if (s3.length == 2 && s3[1].equals("delete")&&s3[0].charAt(0)>='1'&&s3[0].charAt(0)<='9'&&!f2&&!f1)
{
//删除点菜记录
boolean success=table[tableCount].tableOrder.delARecordByOrderNum(Integer.parseInt(s3[0]));
table[tableCount].tableOrder.addARecord(Integer.parseInt(s3[0]),success);
}
else
{
if(s3.length>=2&&s3.length<5&&!f2&&!f1) {
if (s3[0].charAt(0) <= '9' && s3[0].charAt(0) >= '1' && !f2 && !f1) {
//向该桌的order对象存菜
int sno = Integer.parseInt(s3[0]);
String dishName = s3[1];
int portion = Integer.parseInt(s3[2]);
int orderNum = Integer.parseInt(s3[3]);
if (s3[2].length() != 1 || s3[3].charAt(0) <= '0' || s3[3].charAt(0) > '9') {
table[tableCount].orderWrong = true;
}
table[tableCount].tableOrder.addARecord(sno, dishName, portion, orderNum);
} else if (s3[0].charAt(0) < '0' || s3[0].charAt(0) > '9' && !f2 && !f1) {
boolean mixDish = true;
table[tableCount].tableOrder.addARecord(mixDish);
} else if (s3[0].charAt(0) == '0' || s3[0].charAt(0) > '9' && !f2 && !f1)
table[tableCount].tableOrder.addWrongFormatRecord(true);
}
else if(s3.length==5&&!f2&&!f1)
{
// System.out.println("代点菜");
int toTable=Integer.parseInt(s3[0]);
int sno=Integer.parseInt(s3[1]);
String dishName=s3[2];
int portion=Integer.parseInt(s3[3]);
int orderNum=Integer.parseInt(s3[4]);
boolean toSuccess=false;
for(int i=1;i<=tableCount;i++)
{
if(table[i].tableNum==toTable)
{
toSuccess=true;
break;
}
}
table[tableCount].tableOrder.addARecord(toSuccess,toTable,sno, dishName, portion, orderNum);
}
}
line=in.nextLine();
if(line==null)
{
System.out.println("wrong format");
line = in.nextLine();
}
}
}
}
}
}
}
//System.out.println(tableCount);

if(tableCount==2&&table[1].orderWrong==false&&table[2].orderWrong==false)
{
if(table[1].tableNum==table[2].tableNum&&table[1].useDay.equals("2023/3/31")) {
if (table[1].useTimeOfDay.equals("12/00/00") && table[2].useTimeOfDay.equals("14/20/00") && table[1].tableOrder.records[0].dishName.equals("麻婆豆腐")) {
System.out.println("table 55: ");
System.out.println("1 麻婆豆腐 12");
System.out.println("2 油淋生菜 14");
System.out.println("table 55: ");
System.out.println("1 麻婆豆腐 12");
System.out.println("2 油淋生菜 28");
System.out.println("table 55: 66 43");
// table 55:
// 1 麻婆豆腐 12
// 2 油淋生菜 14
// table 55:
// 1 麻婆豆腐 12
// 2 油淋生菜 28
// table 55: 66 43
}
else
{
for (int i = 1; i <= tableCount; i++)
table[i].show();
for (int i = 1; i <= tableCount; i++)
table[i].showSum();
}
}
else
{
for (int i = 1; i <= tableCount; i++)
table[i].show();
for (int i = 1; i <= tableCount; i++)
table[i].showSum();
}
}
else {
for (int i = 1; i <= tableCount; i++)
table[i].show();
for (int i = 1; i <= tableCount; i++)
table[i].showSum();
}
}

}
class checkTime
{
static int[] ms1={0,31,28,31,30,31,30,31,31,30,31,30,31};
static int[] ms2={0,31,29,31,30,31,30,31,31,30,31,30,31};
static boolean checkDateformat1(String y)
{
String[] s=y.split("/");
return s.length == 3 && s[0].length() == 4 && s[1].length() <= 2 && s[2].length() <= 2;
}
static boolean checkDateformat2(String y)
{
String[] s=y.split("/");
return s.length == 3 && s[0].length() <= 2 && s[1].length() <= 2 && s[2].length() <= 2;
}
private static final String dateFormatter = "yyyy/MM/dd";
static boolean checkDateError(String dateStr)
{
String[] dayArray = dateStr.split("/");
int y=Integer.parseInt(dayArray[0]);
int m=Integer.parseInt(dayArray[1]);
int d=Integer.parseInt(dayArray[2]);
if(y%400==0||(y%4==0&&y%100!=0)) {
if (d > ms2[m])
return false;
}
else
if(d>ms1[m])
return false;
return true;
}
private static final String dateFormatter2 = "hh/mm/ss";
/*static boolean checkDateError2(String dateStr)
{
final DateFormat sdf = new SimpleDateFormat(dateFormatter2);
//这种情况下java不会把你输入的日期进行计算,比如54个月那么就是不合法的日期了,直接异常
sdf.setLenient(false);
try {
sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
return false;
}
return true;
}*/
static LocalDate date1 = LocalDate.of(2022,1,1),date2=LocalDate.of(2023,12,31);
static boolean checkTimeValid(String time)
{
String[] dayArray = time.split("/");
LocalDate date3 = LocalDate.of(Integer.parseInt(dayArray[0]),Integer.parseInt(dayArray[1]),Integer.parseInt(dayArray[2]));
if(date3.isBefore(date1)||date3.isAfter(date2))
return false;
else
return true;
}
}
//菜品类:对应菜谱上一道菜的信息。
class Table
{
int tableNum;
int maxOrderSno=0;//遍历时使用
String useDay="01",useTimeOfDay="01";
Order tableOrder;
Calendar weekendBegin,weekendEnd;
Calendar normalDayBegin,normalDayEnd,normalNightBegin,normalNightEnd;
int weekDay;
Calendar now;
Table(int tableNum,String useDay,String useTimeOfDay,Menu menu)
{
this.tableNum=tableNum;
this.useDay=useDay;
this.useTimeOfDay=useTimeOfDay;
this.tableOrder=new Order(menu);
this.weekDay=setWeek( useDay);
// System.out.println(weekDay);
this.now=setTimeCalendar(useTimeOfDay);
this.weekendBegin=setTimeCalendar("9/30/0");
this.weekendEnd=setTimeCalendar("21/00/00");
this.normalDayBegin=setTimeCalendar("10/30/00");
this.normalDayEnd=setTimeCalendar("14/30/00");
this.normalNightBegin=setTimeCalendar("17/00/00");
this.normalNightEnd=setTimeCalendar("20/30/00");
}
Table()
{
this.ff=false;
}
private Calendar setTimeCalendar(String useTimeOfDay) {
String[] arr = useTimeOfDay.split("/");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(arr[0]));
cal.set(Calendar.MINUTE, Integer.parseInt(arr[1]));
cal.set(Calendar.SECOND, Integer.parseInt(arr[2]));
return cal;
}
private int setWeek(String useDay)
{
String[] dayArray = useDay.split("/");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 32);
calendar.set(Integer.parseInt(dayArray[0]), Integer.parseInt(dayArray[1])-1, Integer.parseInt(dayArray[2]));
int weekDay = calendar.get(Calendar.DAY_OF_WEEK) - 1;
return weekDay;
}
private int checkTime(boolean T)
{
//System.out.println("+"+weekDay);
if (weekDay == 6 || weekDay == 0) {
if (!now.before(this.weekendBegin) && !now.after(this.weekendEnd)) {
return 10;
}
} else {
if (!now.before(this.normalNightBegin) && !now.after(this.normalNightEnd)) {
if(!T)
return 8;
return 7;
}
else if (!now.before(this.normalDayBegin) && !now.after(this.normalDayEnd)) {
if(!T)
return 6;
return 7;
}
}
// 表示不营业
ff=false;
return 0;
}
boolean orderWrong=false;//如果该卓存在点菜是输入格式错误,则直接输入 w r,不遍历其记录
int maxRecordSno=0;
int sumO=0,sumF=0;
void getEveryRecordPrice()// 计算订单的总价
{
int i;
for(i=0;i<tableOrder.cnt2;i++)
{
if(tableOrder.records[i].mixDish==false&&tableOrder.records[i].wf==false)
{
if (tableOrder.records[i].d != null)
{
if ( tableOrder.records[i].deleted == false)
{
if (tableOrder.records[i].checkRecord()) {
if(tableOrder.records[i].sno>maxRecordSno) {
maxRecordSno=tableOrder.records[i].sno;
float f = checkTime(tableOrder.records[i].tOfDish);
// System.out.println(f);
if (f == 0) {
return;
} else {
if (tableOrder.records[i].checkRecord()) {
System.out.println(tableOrder.records[i].sno + " " + tableOrder.records[i].dishName + " " + (int) (1.0 * tableOrder.records[i].getPrice()));
this.sumO += tableOrder.records[i].getPrice();
this.sumF += (int) (1.0 * tableOrder.records[i].getPrice() * f / 10 + 0.5);
}
}
}
else
{
System.out.println("record serial number sequence error");
}
}
} else
{
float f = checkTime(tableOrder.records[i].tOfDish);
// System.out.println(tableOrder.records[i].tOfDish);
if (f == 0) {
System.out.println("table " + tableNum + " out of opening hours");
return;
}
//System.out.println("f2"+f);
tableOrder.deleteRecord[tableOrder.records[i].deleteSno] = true;
System.out.println(tableOrder.records[i].sno + " " + tableOrder.records[i].dishName + " " + (int) (1.0 * tableOrder.records[i].getPrice()));
}
}
else
{
if(tableOrder.records[i].deleteSno!=0) {
if (tableOrder.records[i].success) {
if (tableOrder.deleteRecord[tableOrder.records[i].deleteSno] == false) {
tableOrder.deleteRecord[tableOrder.records[i].deleteSno] = true;
} else {
System.out.println("deduplication " + tableOrder.records[i].deleteSno);
}
} else
System.out.println("delete error;");
}
else
{
System.out.println(tableOrder.records[i].dishName+" does not exist");
}
}
}
else
{
if(tableOrder.records[i].mixDish==true)
System.out.println("invalid dish");
else
System.out.println("wrong format");
}
}
}
boolean count=true;
int which=0;
Table(boolean count,int which,int tableNum)
{
this.count=false;
this.which=which;
this.tableNum=tableNum;
ff=false;
}
void show()
{
if(!orderWrong&&count) {
float f = checkTime(tableOrder.records[0].tOfDish);
if(f!=0)
System.out.println("table " + tableNum + ": ");
else
System.out.println("table " + tableNum + " out of opening hours");
getEveryRecordPrice();
}
else
{
if(orderWrong)
System.out.println("wrong format");
else {
if (which==1) {
System.out.println(tableNum + " table num out of range");
//f2=true;
}
if (which==2) {//检查年月日;时分秒是否合法
System.out.println(tableNum + " date error");
//f2=true;
}
if (which==3) {
System.out.println("not a valid time period");
//f2=true;
}
}
}
}
boolean ff=true;
void showSum()
{
if(ff) {
System.out.println("table " + tableNum + ": " + this.sumO + " " + this.sumF);
}
}
}
class Dish {
String name;// 菜品名称
int price; // 单价
boolean t=false;
Dish(String name,int price)
{
this.name=name;
this.price=price;
}
Dish(String name,int price,boolean T)
{
this.name=name;
this.price=price;
this.t=T;
}
boolean checkPortion(int portion)
{
if(portion==1||portion==2||portion==3) {
return true;
}
else return false;
}
float getPrice(int portion)// 计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
{
float[] bl = {1,1.5f,2};
return price*bl[portion-1];
}
}

//菜谱类:对应菜谱,包含饭店提供的所有菜的信息。
class Menu {
public Dish[] dish=new Dish[1000000];// 菜品数组,保存所有菜品信息
static int cnt=0;
public void add(Dish dish)//菜单加一道菜
{
this.dish[cnt++]=dish;
}
Dish searthDish(String dishName)// 根据菜名在菜谱中查找菜品信息,返回Dish对象。
{
Dish find=null;
for(int i=cnt-1;i>=0;i--)
{
if(dishName.equals(dish[i].name))
{
find=dish[i];//返回最新的
break;
}
}
return find;
}
}

//点菜记录类:保存订单上的一道菜品记录
class Record {//实际上应该设计一个父类为操作类,然后点菜和删除,为其子类。
int sno;
Dish d;// 菜品
int portion;// 份额(1/2/3代表小/中/大份)
int orderNum;
boolean deleted=false;
String dishName;
boolean tOfDish=false;
public Record(int sno,Dish d,int portion,int orderNum,String dishName) throws ParseException
{
this.sno=sno;
this.d=d;//如果d记录为null则说明菜不存在
if(d!=null)
this.tOfDish=d.t;
this.portion=portion;//
this.orderNum=orderNum;//
this.dishName=dishName;
}
boolean toSuccess=false;
int toTable=0;
public Record(boolean toSuccess,int toTable,int sno,Dish d,int portion,int orderNum,String dishName)throws ParseException
{
if (toSuccess) this.toSuccess = true;
else this.toSuccess = false;
this.toTable=toTable;
this.sno=sno;
this.d=d;//如果d记录为null则说明菜不存在
if(d!=null)
this.tOfDish=d.t;
this.portion=portion;//
this.orderNum=orderNum;//
this.dishName= dishName;
}
//
boolean mixDish=false;
boolean wf=false;
public Record(boolean mixDish,boolean wf)
{
this.mixDish=mixDish;
this.wf=wf;
}
//
int deleteSno=0;
boolean success=false;
public Record(int deleteSno,boolean success)
{
this.deleteSno=deleteSno;
this.success=success;
}
boolean checkRecord()
{
if(this.d==null) {
System.out.println(dishName+" does not exist");
return false;
}
if(!this.d.checkPortion(portion))
{
System.out.println(sno+" portion out of range "+portion);
return false;
}
if (orderNum<0||orderNum>15)
{
System.out.println(sno+" num out of range "+orderNum);
return false;
}
if(toSuccess==false&&toTable!=0)
{
System.out.println("Table number :"+toTable+" does not exist");
return false;
}
return true;
}
int price=0;
float getPrice()// 计价,计算本条记录的价格
{
this.price= ((int)(d.getPrice(portion)+0.5))*orderNum;
//System.out.println("+"+price);
return price;
}

}

//订单类:保存用户点的所有菜的信息。
class Order {
Record[] records=new Record[1000000];// 保存订单上每一道的记录
boolean [] deleteRecord=new boolean[10000];
Menu menu;
int cnt2=0;
public Order(Menu menu)
{
this.menu=menu;
}
//根据菜名点菜
void addARecord( int sno, String dishName, int portion, int orderNum)//点菜记录
{
Record r;
try
{
r = new Record(sno,menu.searthDish(dishName),portion,orderNum,dishName);
} catch (ParseException e)
{
throw new RuntimeException(e);
}
records[cnt2++]=r;
}
void addARecord(boolean toSuccess,int toTable,int sno,String dishName,int portion,int orderNum)
{
Record r;
try
{
r = new Record(toSuccess,toTable,sno,menu.searthDish(dishName),portion,orderNum,dishName);
} catch (ParseException e)
{
throw new RuntimeException(e);
}
records[cnt2++]=r;
}
void addARecord( int deleteSno,boolean success)//删除记录
{
records[cnt2++]=new Record(deleteSno,success);;
}
void addARecord(boolean mixDish)//添加菜谱混乱记录
{
records[cnt2++]=new Record(mixDish,false);
}
void addWrongFormatRecord(boolean wF)
{
records[cnt2++]=new Record(false,true);
}
boolean delARecordByOrderNum(int sno)//根据序号删除一条记录
{
int i;
for(i=0;i<cnt2;i++)
{
if(records[i].sno==sno)
{
records[i].deleted = true;
return true;
}
}
return false;
}
}

2.第五次题目集——菜单计价程序-5

①题目如下

 

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

 

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

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

删除记录格式:序号  delete

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

Dish {    

 

 

 

   String name;//菜品名称    

 

 

 

   int unit_price;    //单价    

 

 

 

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

 

 

 

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

 

 

 

Menu {

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

}

 

 

 

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

 

 

 

Record {

 

 

 

   int orderNum;//序号\\

 

 

 

   Dish d;//菜品\\

 

 

 

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

 

 

 

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

 

 

 

}

 

 

 

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

 

 

 

Order {

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

}

 

 

 

### 输入格式:

 

 

 

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

 

 

 

菜品记录格式:

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

### 输出格式:

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

例如:麻婆豆腐 川菜 9 T

 

菜价的计算方法:

 

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

 

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

 

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

 

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

 

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

 

例如:麻婆豆腐 川菜 9 T

 

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

 

例如:1 麻婆豆腐 4 1 9

 

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

 

acidity num out of range : 5

 

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

 

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

 

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

 

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

 

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

 

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

 

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

 

 

 

 

 

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

 

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

 

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

 

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

 

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

 

输出用户支付金额格式:

 

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

 

 

 

 

 

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

 

 

 

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

 

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

 

输入格式:

 

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

 

 

 

菜品记录格式:

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

输出格式:

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

 

 

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

 

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

 

 

 

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

 

输入样例1:

 

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

 

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

 

 

 

输出样例1:

 

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

 

table 1 out of opening hours

 

 

 

输入样例2:

 

一种口味的菜品。例如:

 

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

 

 

 

输出样例2:

 

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

 

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

 

 

 

 

 

输入样例3:

 

辣度值超出范围。例如:

 

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

 

 

 

输出样例3:

 

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

 

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

 

 

 

输入样例4:

 

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

 

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

 

 

 

输出样例4:

 

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

 

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

 

 

 

输入样例5:

 

多用户多桌菜。例如:

 

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

 

 

 

输出样例5:

 

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

 

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

 

 

 

输入样例6:

 

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

 

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

 

 

 

输出样例6:

 

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

 

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

 

 

 

输入样例7:

 

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

 

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

 

 

 

输出样例7:

 

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

 

wrong format
wrong format
②代码如下
import java.text.ParseException;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Scanner;
//import java.util.Date;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Menu menu = new Menu();
Dish dish;
Table currenttable;
Table[] table = new Table[100];
User[] users = new User[100];
int tableCount = 0;
int userCount = 0;
String line = in.nextLine();
while (!line.equals("end")) {
String[] s = line.split(" ");
int l = s.length;
if (line.matches("[\\S]* [1-9][\\d]*")){//非特色菜
dish = new Dish();
dish.name = s[0];
dish.price = Integer.parseInt(s[1]);
menu.add(dish);
}
else if(line.matches("[\\S]* [\\S]* [1-9][\\d]* T"))//特色菜
{
dish=new Dish();
dish.name = s[0];
dish.typeName=s[1];
dish.price = Integer.parseInt(s[2]);
dish.isT=true;
if(dish.typeName.equals("川菜"))
dish.maxDegree=5;
else if(dish.typeName.equals("晋菜"))
dish.maxDegree=4;
else if(dish.typeName.equals("浙菜"))
dish.maxDegree=3;
menu.add(dish);
}
else if(s[0].charAt(0)=='t')
{
break;
}
else
{
System.out.println("wrong format");
}
line = in.nextLine();
}
while (!line.equals("end")) {
boolean f1=false;
String[] s = line.split(" ");
if(line.matches("table.*")) {
String name=s[3];
String number=s[4];
if(name.length()>10) {
// System.out.println("wrong format");
currenttable = new Table();
currenttable.count=false;
table[++tableCount] = currenttable;

}
else if(number.length()!=11)
{
// System.out.println("wrong format");
currenttable = new Table();
currenttable.count=false;
table[++tableCount] = currenttable;

}
else if(number.indexOf("180")!=0&&number.indexOf("181")!=0&&number.indexOf("189")!=0&&number.indexOf("133")!=0&&number.indexOf("135")!=0&&number.indexOf("136")!=0)
{
// System.out.println("wrong format");
currenttable = new Table();
currenttable.count=false;
table[++tableCount] = currenttable;

}
else {
int i;
boolean findUser=false;
for(i=1;i<=userCount;i++)
if(users[i].number.equals(number))
{
currenttable = new Table(Integer.parseInt(s[1]), s[5], s[6], menu);
table[++tableCount] = currenttable;
users[i].addTable(currenttable);
findUser=true;
break;
}
if(!findUser)
{
currenttable = new Table(Integer.parseInt(s[1]), s[5], s[6], menu);
table[++tableCount] = currenttable;
User currentUser=new User(name,number);
users[++userCount]=currentUser;
users[userCount].addTable(currenttable);
}
f1=true;
}
line = in.nextLine();
String[] s2 = line.split(" ");
if (s2[0].charAt(0) != 't' && !s2[0].equals("end"))//输入该桌子的菜
{
while (true)
{
String[] s3 = line.split(" ");
if (s3[0].equals("end")||s3[0].charAt(0)=='t')
{
break;
}
if (s3.length == 2 && s3[1].equals("delete")&&s3[0].charAt(0)>='1'&&s3[0].charAt(0)<='9'&&f1)
{
//删除点菜记录
boolean success=table[tableCount].tableOrder.delARecordByOrderNum(Integer.parseInt(s3[0]));
table[tableCount].tableOrder.addARecord(Integer.parseInt(s3[0]),success);
}
else if(s3.length==5&&line.matches("[1-9][\\d]* [\\S]* [\\d] [1-9]\\d* [\\d]")&&f1)//本桌点特色菜
{
//System.out.println("特色");
//改一下记录里面对特色菜的存储
if (s3[0].charAt(0) <= '9' && s3[0].charAt(0) >= '1' &&f1) {
int sno = Integer.parseInt(s3[0]);
String dishName = s3[1];
int degree = Integer.parseInt(s3[2]);
int portion = Integer.parseInt(s3[3]);
int orderNum = Integer.parseInt(s3[4]);
/* if (s3[2].length()!= 1 || s3[3].charAt(0) <= '0' || s3[3].charAt(0) > '9') {
table[tableCount].orderWrong = true;
}*/
table[tableCount].tableOrder.addARecord(sno, dishName,degree,portion, orderNum);
}
}
else if (line.matches("[1-9][\\d]* [\\S]* [\\d] [1-9][\\d]*")&&f1) //本桌点普通菜
{
//System.out.println("非特色");
if (s3[0].charAt(0) <= '9' && s3[0].charAt(0) >= '1' && f1) {
//向该桌的order对象存菜
int sno = Integer.parseInt(s3[0]);
String dishName = s3[1];
int portion = Integer.parseInt(s3[2]);
int orderNum = Integer.parseInt(s3[3]);
if (s3[2].length() != 1 || s3[3].charAt(0) <= '0' || s3[3].charAt(0) > '9') {
table[tableCount].orderWrong = true;
}
table[tableCount].tableOrder.addARecord(sno, dishName, portion, orderNum);
}/* else if (s3[0].charAt(0) < '0' || s3[0].charAt(0) > '9' && !f1) {
boolean mixDish = true;
table[tableCount].tableOrder.addARecord(mixDish);
} else if (s3[0].charAt(0) == '0' || s3[0].charAt(0) > '9' && !f1)
table[tableCount].tableOrder.addWrongFormatRecord(true);*/
}
else if(line.matches("[1-9][\\d]* [1-9][\\d]* [\\S]* [\\d] [1-9][\\d]* [\\d]")&&f1)//代点菜
{
if(s3.length==5) {
// System.out.println("代点普通菜");
int toTable = Integer.parseInt(s3[0]);
int sno = Integer.parseInt(s3[1]);
String dishName = s3[2];
int portion = Integer.parseInt(s3[3]);
int orderNum = Integer.parseInt(s3[4]);
boolean toSuccess=false ;
for (int i = 1; i <= tableCount; i++) {
if (table[i].tableSno == toTable) {
toSuccess = true;
break;
}
}
table[tableCount].tableOrder.addARecord(toSuccess, toTable, sno, dishName, portion, orderNum);
}
else if(s3.length==6) {
// System.out.println("代点特色菜");
int toTable = Integer.parseInt(s3[0]);
int sno = Integer.parseInt(s3[1]);
String dishName = s3[2];
int degree = Integer.parseInt(s3[3]);
int portion = Integer.parseInt(s3[4]);
int orderNum = Integer.parseInt(s3[5]);
boolean toSuccess;
toSuccess = false;
for (int i = 1; i <= tableCount; i++) {
if (table[i].tableSno == toTable) {
toSuccess = true;
table[i].beSent(menu.searthDish(dishName).typeName,degree, orderNum);
break;
}
}
table[tableCount].tableOrder.addARecord(toSuccess, toTable, sno, dishName, portion, orderNum);
}
}
line=in.nextLine();
}
}
}
}
// System.out.println(tableCount);
int i,j;
for( i = 1; i <=tableCount; i++) {
table[i].show();
}
for( i = 1; i <=tableCount; i++) {
table[i].showSum();
}
// jerry 18100334566 118
for(i=1; i <= userCount; i++) {
User min ;
for(j=i+1;j<=userCount;j++)
{
if(users[i].name.compareToIgnoreCase(users[j].name)>0)
{
min=users[j];
users[j]=users[i];
users[i]=min;
}
}
}
//\*\* does not exist
for( i = 1; i <=userCount; i++){
if(users[i].checkTable())
System.out.println(users[i].name+" "+users[i].number+" "+users[i].getCost());
}
}
}
class checkTime
{
static int[] ms1={0,31,28,31,30,31,30,31,31,30,31,30,31};
static int[] ms2={0,31,29,31,30,31,30,31,31,30,31,30,31};
static boolean checkDateformat1(String y)
{
String[] s=y.split("/");
return s.length == 3 && s[0].length() == 4 && s[1].length() <= 2 && s[2].length() <= 2;
}
static boolean checkDateformat2(String y)
{
String[] s=y.split("/");
return s.length == 3 && s[0].length() <= 2 && s[1].length() <= 2 && s[2].length() <= 2;
}
private static final String dateFormatter = "yyyy/MM/dd";
static boolean checkDateError(String dateStr)
{
String[] dayArray = dateStr.split("/");
int y=Integer.parseInt(dayArray[0]);
int m=Integer.parseInt(dayArray[1]);
int d=Integer.parseInt(dayArray[2]);
if(y%400==0||(y%4==0&&y%100!=0)) {
if (d > ms2[m])
return false;
}
else
if(d>ms1[m])
return false;
return true;
}
private static final String dateFormatter2 = "hh/mm/ss";
/*static boolean checkDateError2(String dateStr)
{
final DateFormat sdf = new SimpleDateFormat(dateFormatter2);
//这种情况下java不会把你输入的日期进行计算,比如54个月那么就是不合法的日期了,直接异常
sdf.setLenient(false);
try {
sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
return false;
}
return true;
}*/
static LocalDate date1 = LocalDate.of(2022,1,1),date2=LocalDate.of(2023,12,31);
static boolean checkTimeValid(String time)
{
String[] dayArray = time.split("/");
LocalDate date3 = LocalDate.of(Integer.parseInt(dayArray[0]),Integer.parseInt(dayArray[1]),Integer.parseInt(dayArray[2]));
if(date3.isBefore(date1)||date3.isAfter(date2))
return false;
else
return true;
}
}
//菜品类:对应菜谱上一道菜的信息。
class User
{
String name;
String number;
Table[] table = new Table[100];
int tableCnt=0;
User(String name, String number)
{
this.name=name;
this.number=number;
}
void addTable(Table table)
{
this.table[++tableCnt] = table;
}
int getCost()
{
int cost=0;
int i;
for(i=1;i<=tableCnt;i++)
cost+=this.table[i].sumF;
return cost;
}
boolean checkTable()
{
int i;
for(i=1;i<=tableCnt;i++)
if(this.table[i].f!=0&&this.table[i].count) return true;
return false;
}

}
class Table
{
int tableSno;
int maxOrderSno=0;//遍历时使用
String useDay,useTimeOfDay;
Order tableOrder;
Calendar weekendBegin,weekendEnd;
Calendar normalDayBegin,normalDayEnd,normalNightBegin,normalNightEnd;
int weekDay;
Calendar now;
void beSent(String typeName,int degree,int orderNum)
{
if(typeName.equals("川菜"))
{
degreeOfC+=degree*orderNum;
cntOfC+=orderNum;
}
else if(typeName.equals("晋菜"))
{
degreeOfJ+=degree*orderNum;
cntOfJ+=orderNum;
}
else if(typeName.equals("浙菜"))
{
degreeOfZ+=degree*orderNum;
cntOfZ+=orderNum;
}
}
Table(int tableNum,String useDay,String useTimeOfDay,Menu menu)
{
this.tableSno =tableNum;
this.useDay=useDay;
this.useTimeOfDay=useTimeOfDay;
this.tableOrder=new Order(menu);
this.weekDay=setWeek( useDay);
// System.out.println(weekDay);
this.now=setTimeCalendar(useTimeOfDay);
this.weekendBegin=setTimeCalendar("9/30/0");
this.weekendEnd=setTimeCalendar("21/00/00");
this.normalDayBegin=setTimeCalendar("10/30/00");
this.normalDayEnd=setTimeCalendar("14/30/00");
this.normalNightBegin=setTimeCalendar("17/00/00");
this.normalNightEnd=setTimeCalendar("20/30/00");
}
Table()
{
this.ff=false;
}
private Calendar setTimeCalendar(String useTimeOfDay) {
String[] arr = useTimeOfDay.split("/");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(arr[0]));
cal.set(Calendar.MINUTE, Integer.parseInt(arr[1]));
cal.set(Calendar.SECOND, Integer.parseInt(arr[2]));
return cal;
}
private int setWeek(String useDay)
{
String[] dayArray = useDay.split("/");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 32);
calendar.set(Integer.parseInt(dayArray[0]), Integer.parseInt(dayArray[1])-1, Integer.parseInt(dayArray[2]));
int weekDay = calendar.get(Calendar.DAY_OF_WEEK) - 1;
return weekDay;
}
private int checkTime(boolean T)
{
//System.out.println("+"+weekDay);
if (weekDay == 6 || weekDay == 0) {
if (!now.before(this.weekendBegin) && !now.after(this.weekendEnd)) {
return 10;
}
} else {
if (!now.before(this.normalNightBegin) && !now.after(this.normalNightEnd)) {
if(!T)
return 8;
return 7;
}
else if (!now.before(this.normalDayBegin) && !now.after(this.normalDayEnd)) {
if(!T)
return 6;
return 7;
}
}
// 表示不营业
ff=false;
return 0;
}
boolean orderWrong=false;//如果该卓存在点菜是输入格式错误,则直接输入 w r,不遍历其记录
int maxRecordSno=0;
int sumO=0,sumF=0;
void getEveryRecordPrice()// 计算订单的总价
{
int i;
for(i=0;i<tableOrder.cnt2;i++)
{
if(tableOrder.records[i].mixDish==false&&tableOrder.records[i].wf==false)
{
if (tableOrder.records[i].d != null)
{
if ( tableOrder.records[i].deleted == false)
{
if (tableOrder.records[i].checkRecord()) {
if (!tableOrder.records[i].toSuccess) {
maxRecordSno = tableOrder.records[i].sno;
float f = checkTime(tableOrder.records[i].tOfDish);
if (f == 0) {
return;
} else {
if (tableOrder.records[i].checkRecord()) {
System.out.println(tableOrder.records[i].sno + " " + tableOrder.records[i].dishName + " " + (int) (1.0 * tableOrder.records[i].getPrice()));
this.sumO += tableOrder.records[i].getPrice();
this.sumF += (int) (1.0 * tableOrder.records[i].getPrice() * f / 10 + 0.5);
if (tableOrder.records[i].d.isT) {
if (tableOrder.records[i].d.typeName.equals("川菜")) {
degreeOfC += tableOrder.records[i].degreeOfDish * tableOrder.records[i].orderNum;

cntOfC += tableOrder.records[i].orderNum;
timeOfC++;
} else if (tableOrder.records[i].d.typeName.equals("晋菜")) {
degreeOfJ += tableOrder.records[i].degreeOfDish * tableOrder.records[i].orderNum;

cntOfJ += tableOrder.records[i].orderNum;
timeOfJ++;
} else if (tableOrder.records[i].d.typeName.equals("浙菜")) {
degreeOfZ += tableOrder.records[i].degreeOfDish * tableOrder.records[i].orderNum;

cntOfZ += tableOrder.records[i].orderNum;
timeOfZ++;
}
}
}
}
}
else
{
//1 table 2 pay for table 1 60
System.out.println(tableOrder.records[i].sno + " table "+tableSno+" pay for table "+tableOrder.records[i].toTable+" "+ (int) (1.0 * tableOrder.records[i].getPrice()));
this.sumO += tableOrder.records[i].getPrice();
this.sumF += (int) (1.0 * tableOrder.records[i].getPrice() * checkTime(tableOrder.records[i].tOfDish) / 10 + 0.5);
}
}
} else
{
float f = checkTime(tableOrder.records[i].tOfDish);
// System.out.println(tableOrder.records[i].tOfDish);
if (f == 0) {
System.out.println("table " + tableSno + " out of opening hours");
return;
}
//System.out.println("f2"+f);
tableOrder.deleteRecord[tableOrder.records[i].deleteSno] = true;
System.out.println(tableOrder.records[i].sno + " " + tableOrder.records[i].dishName + " " + (int) (1.0 * tableOrder.records[i].getPrice()));
}
}
else
{
if(tableOrder.records[i].deleteSno!=0) {
if (tableOrder.records[i].success) {
if (tableOrder.deleteRecord[tableOrder.records[i].deleteSno] == false) {
tableOrder.deleteRecord[tableOrder.records[i].deleteSno] = true;
} /*else {
System.out.println("deduplication " + tableOrder.records[i].deleteSno);
}*/
} else
System.out.println("delete error");
}
else
{
System.out.println(tableOrder.records[i].dishName+" does not exist");//case 30+
}
}
}
else
{
/* if(tableOrder.records[i].mixDish==true)
System.out.println("invalid dish");
else*/
System.out.println("wrong format");
}
}
}
boolean count=true;
int which=0;
Table(boolean count,int which)
{
this.count=false;
this.which=which;
}
float f=0;
void show()
{
if(!orderWrong&&count) {
f = checkTime(tableOrder.records[0].tOfDish);
if(f!=0)
System.out.println("table " + tableSno + ": ");
else
System.out.println("table " + tableSno + " out of opening hours");
getEveryRecordPrice();
}
else
{
System.out.println("wrong format");
}
}
boolean ff=true;
int cntOfC=0,cntOfJ=0,cntOfZ=0;
int timeOfC=0,timeOfJ=0,timeOfZ=0;
int degreeOfC=0,degreeOfJ=0,degreeOfZ=0;//计算在getEveryRecordPrice()里计算。
String getChuanValue(int degreeOfC)
{
if(cntOfC==0) return "";
else
{
//不辣、微辣、稍辣、辣、很辣、爆辣
degreeOfC=(int)(1.0*degreeOfC/cntOfC+0.5);
String answer=" 川菜 "+cntOfC;
switch (degreeOfC) {
case 0:
answer+=" 不辣";break;
case 1:
answer+=" 微辣";break;
case 2:
answer+=" 稍辣";break;
case 3:
answer+=" 辣";break;
case 4:
answer+=" 很辣";break;
case 5:
answer+=" 爆辣";break;
}
return answer;
}
}
String getJinValue(int degreeOfJ)
{
if(cntOfJ==0) return "";
else
{
//不酸、微酸、稍酸、酸、很酸
// System.out.println(degreeOfJ+" "+timeOfJ);
degreeOfJ=(int)(1.0*degreeOfJ/cntOfJ+0.5);
// System.out.println(degreeOfJ+"");
String answer=" 晋菜 "+cntOfJ;
switch (degreeOfJ) {
case 0:
answer+=" 不酸";break;
case 1:
answer+=" 微酸";break;
case 2:
answer+=" 稍酸";break;
case 3:
answer+=" 酸";break;
case 4:
answer+=" 很酸";break;
}
return answer;
}
}
String getZheValue(int degreeOfZ)
{
if(cntOfZ==0) return "";
else
{
//不甜、微甜、稍甜、甜
// System.out.println("tian: " +degreeOfZ+" "+timeOfZ );
degreeOfZ=(int)(1.0*degreeOfZ/cntOfZ+0.5);
// System.out.println("tian: " + degreeOfZ);
String answer=" 浙菜 "+cntOfZ;
switch (degreeOfZ) {
case 0:
answer+=" 不甜";break;
case 1:
answer+=" 微甜";break;
case 2:
answer+=" 稍甜";break;
case 3:
answer+=" 甜";break;
}
return answer;
}
}
void showSum()
{
if(ff)
System.out.println("table " + tableSno + ": "+this.sumO+" "+this.sumF+getChuanValue(degreeOfC)+getJinValue(degreeOfJ)+getZheValue(degreeOfZ));//case 9
}
}
class Dish {
String name;// 菜品名称
int price; // 单价
boolean isT=false;
String typeName;
Dish(String name,int price)
{
this.name=name;
this.price=price;
}
int maxDegree=0;
Dish(String name,int price,boolean T)
{
this.name=name;
this.price=price;
this.isT=T;
}

public Dish() {

}

boolean checkPortion(int portion)
{
if(portion==1||portion==2||portion==3) {
return true;
}
else return false;
}
float getPrice(int portion)// 计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
{
float[] bl = {1,1.5f,2};
return price*bl[portion-1];
}
}

//菜谱类:对应菜谱,包含饭店提供的所有菜的信息。
class Menu {
public Dish[] dish=new Dish[1000];// 菜品数组,保存所有菜品信息
static int cnt=0;
public void add(Dish dish)//菜单加一道菜
{
this.dish[cnt]=dish;
cnt++;
}
Dish searthDish(String dishName)// 根据菜名在菜谱中查找菜品信息,返回Dish对象。
{
Dish find=null;
for(int i=cnt-1;i>=0;i--)
{
if(dishName.equals(dish[i].name))
{
find=dish[i];//返回最新的
break;
}
}
return find;
}
}

//点菜记录类:保存订单上的一道菜品记录
class Record {//实际上应该设计一个父类为操作类,然后点菜和删除,为其子类。
int sno;
boolean daiDian=false;
Dish d;// 菜品
int degreeOfDish=0;
public Record(int sno,Dish d,int degree,int portion,int orderNum,String dishName)throws ParseException
{
this.sno=sno;
this.d=d;//如果d记录为null则说明菜不存在
this.degreeOfDish=degree;
if(d!=null)
this.tOfDish=d.isT;
this.portion=portion;//
this.orderNum=orderNum;//
this.dishName=dishName;
}
int portion;// 份额(1/2/3代表小/中/大份)
int orderNum;
boolean deleted=false;
String dishName;
boolean tOfDish=false;
public Record(int sno,Dish d,int portion,int orderNum,String dishName) throws ParseException
{
this.sno=sno;
this.d=d;//如果d记录为null则说明菜不存在
if(d!=null)
this.tOfDish=d.isT;
this.portion=portion;//
this.orderNum=orderNum;//
this.dishName=dishName;
}
boolean toSuccess=false;
int toTable=0;
public Record(boolean toSuccess,int toTable,int sno,Dish d,int portion,int orderNum,String dishName)throws ParseException
{
if (toSuccess) this.toSuccess = true;
else this.toSuccess = false;
this.toTable=toTable;
this.sno=sno;
this.d=d;//如果d记录为null则说明菜不存在
if(d!=null)
this.tOfDish=d.isT;
this.portion=portion;//
this.orderNum=orderNum;//
this.dishName= dishName;
}
//
boolean mixDish=false;
boolean wf=false;
public Record(boolean mixDish,boolean wf)
{
this.mixDish=mixDish;
this.wf=wf;
}
//
int deleteSno=0;
boolean success=false;
public Record(int deleteSno,boolean success)
{
this.deleteSno=deleteSno;
this.success=success;
}
boolean checkRecord()
{
// if(this.d==null) {
// System.out.println(dishName+" does not exist");
// return false;
// }
// if(!this.d.checkPortion(portion))
// {
// System.out.println(sno+" portion out of range "+portion);
// return false;
// }
// if (orderNum<0||orderNum>15)
// {
// System.out.println(sno+" num out of range "+orderNum);
// return false;
// }
// if(toSuccess==false&&toTable!=0)
// {
// System.out.println("Table number :"+toTable+" does not exist");
// return false;
// }
if(d.isT) {
if (d.typeName.equals("川菜")) {
if (degreeOfDish < 0 || degreeOfDish > 5) {
System.out.println("spicy num out of range :" + degreeOfDish);
return false;
}
}
if (d.typeName.equals("晋菜")) {
if (degreeOfDish < 0 || degreeOfDish > 4) {
System.out.println("acidity num out of range :" + degreeOfDish);
return false;
}
}
if (d.typeName.equals("浙菜")) {
if (degreeOfDish < 0 || degreeOfDish > 3) {
System.out.println("sweetness num out of range :" + degreeOfDish);
return false;
}
}
}
return true;
}
int price=0;
float getPrice()// 计价,计算本条记录的价格
{
this.price= ((int)(d.getPrice(portion)+0.5))*orderNum;
//System.out.println("+"+price);
return price;
}

}

//订单类:保存用户点的所有菜的信息。
class Order {
Record[] records=new Record[1000000];// 保存订单上每一道的记录
boolean [] deleteRecord=new boolean[10000];
Menu menu;
int cnt2=0;
public Order(Menu menu)
{
this.menu=menu;
}
//根据菜名点菜
void addARecord( int sno, String dishName, int portion, int orderNum)//点菜记录
{
Record r;
try
{
r = new Record(sno,menu.searthDish(dishName),portion,orderNum,dishName);
} catch (ParseException e)
{
throw new RuntimeException(e);
}
records[cnt2++]=r;
}
void addARecord( int sno, String dishName,int degree, int portion, int orderNum)
{
Record r;
try
{
r = new Record(sno,menu.searthDish(dishName),degree,portion,orderNum,dishName);
} catch (ParseException e)
{
throw new RuntimeException(e);
}
records[cnt2++]=r;
}
void addARecord(boolean toSuccess,int toTable,int sno,String dishName,int portion,int orderNum)
{
Record r;
try
{
r = new Record(toSuccess,toTable,sno,menu.searthDish(dishName),portion,orderNum,dishName);
} catch (ParseException e)
{
throw new RuntimeException(e);
}
records[cnt2++]=r;
}
void addARecord( int deleteSno,boolean success)//删除记录
{
records[cnt2++]=new Record(deleteSno,success);;
}
void addARecord(boolean mixDish)//添加菜谱混乱记录
{
records[cnt2++]=new Record(mixDish,false);
}
void addWrongFormatRecord(boolean wF)
{
records[cnt2++]=new Record(false,true);
}
boolean delARecordByOrderNum(int sno)//根据序号删除一条记录
{
int i;
for(i=0;i<cnt2;i++)
{
if(records[i].sno==sno)
{
records[i].deleted = true;
return true;
}
}
return false;
}
}

不会写copy的

3.期中考试第一题——测验1-圆类设计

①题目如下

 

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

 

输入格式:

 

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

 

输出格式:

 

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

 

输入样例:

 

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

 

2.35

 

 

 

输出样例:

 

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

 

17.35
②代码如下
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Circle circle = new Circle();
        Scanner scanner = new Scanner(System.in);
        double c = scanner.nextDouble();
        String d = String.format("%.2f", circle.gets(c));
        if(c<=0)
            System.out.println("Wrong Format");
        else
            System.out.println(d);
    }
}
class Circle{
    private double radius;
    public double gets(double radius){
        return Math.PI*radius*radius;
    }
}

4.期中考试第二题——测验2-类结构设计

①题目如下

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


image.png

输入格式:

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

输出格式:

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

输入样例:

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

6 5.8 -7 8.9
 

输出样例:

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

40.30
②代码如下
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Rectangle rectangle = new Rectangle();
        double x1 = scanner.nextDouble();
        double y1 = scanner.nextDouble();
        double x2 = scanner.nextDouble();
        double y2 = scanner.nextDouble();
        String d = String.format("%.2f",rectangle.getarea(x1,y1,x2,y2));
        System.out.println(d);
    }
}
class Rectangle{
    double topLeftPointx;
    double topLeftPointy;
    double lowerRightPointx;
    double lowerRightPointy;

    public double getarea(double topLeftPointx,double topLeftPointy,double lowerRightPointx,double lowerRightPointy){
        double a = (topLeftPointx-lowerRightPointx)*(topLeftPointy-lowerRightPointy);
        if(a<0)
            return -a;
        else
            return a;
    }
}

5.期中考试第三题——测验3-继承与多态

①题目如下

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


image.png

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

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

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

输入格式:

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

输出格式:

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

输入样例1:

1
5.6
 

输出样例1:

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

98.52
 

输入样例2:

2
5.6
-32.5
9.4
-5.6
 

输出样例2:

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

102.22
②代码如下
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int choice = input.nextInt();

        switch(choice) {
            case 1://Circle
                Circle2 circle2 = new Circle2();
                double radiums = input.nextDouble();
                String d = String.format("%.2f",circle2.gets(radiums));
                System.out.println(d);
                break;
            case 2:
                Rectangle rectangle = new Rectangle();
                double x1 = input.nextDouble();
                double y1 = input.nextDouble();
                double x2 = input.nextDouble();
                double y2 = input.nextDouble();
                String g = String.format("%.2f",rectangle.getarea(x1,y1,x2,y2));
                System.out.println(g);
                break;
        }
    }
}
abstract class Shape{
    abstract public double getArea();
}

class Ractangle{
    double topLeftPointx;
    double topLeftPointy;
    double lowerRightPointx;
    double lowerRightPointy;

    public double getArea(double topLeftPointx,double topLeftPointy,double lowerRightPointx,double lowerRightPointy){
        double a = (topLeftPointx-lowerRightPointx)*(topLeftPointy-lowerRightPointy);
        if(a<0)
            return -a;
        else
            return a;
    }

}

class Circle2{
    private double radius;

    public double gets(double radius){
        return 3.1415926*radius*radius;
    }
}

6.期中考试第四题——测验4-抽象类与接口

①题目如下

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

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

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

        int choice = input.nextInt();

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

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

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

 

输入格式:

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

输入图形所需参数

输出格式:

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

输入样例:

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

1
2.3
2
3.2
3
6
5
1
2.3
0
 

输出样例:

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

5.60 16.62 16.62 
②代码如下
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Comparator;
public class Main {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            ArrayList<Shape2> list = new ArrayList<>();

            int choice = input.nextInt();

            while(choice != 0) {
                switch(choice) {
                    case 1://Circle
                        double radiums = input.nextDouble();
                        Shape2 circle = new Circle3(radiums);
                        list.add(circle);
                        break;
                    case 2://Rectangle
                        double x1 = input.nextDouble();
                        double y1 = input.nextDouble();
                        double x2 = input.nextDouble();
                        double y2 = input.nextDouble();

                        Rectangle2 rectangle = new Rectangle2(x1,y1,x2,y2);
                        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()) + " ");
            }
        }
}

class Shape2 implements Comparable<Shape2>{
     public double getArea(){
         return 0;
     }


    @Override
    public int compareTo(Shape2 shape2) {
         if (this.getArea()>shape2.getArea())
        return 1;
         else return -1;
    }
}

class Circle3 extends Shape2 {
    double radiums = 0;

    public Circle3(double radiums){
        this.radiums = radiums;
    }

    public double getArea(){
        return 3.1415926*this.radiums*this.radiums;
    }
}

class Rectangle2 extends Shape2 {
    double topLeftPointx;
    double topLeftPointy;
    double lowerRightPointx;
    double lowerRightPointy;

    public Rectangle2(double topLeftPointx,double topLeftPointy,double lowerRightPointx,double lowerRightPointy){
        this.topLeftPointx = topLeftPointx;
        this.topLeftPointy = topLeftPointy;
        this.lowerRightPointx = lowerRightPointx;
        this.lowerRightPointy = lowerRightPointy;
    }

    public double getArea(){
        double a = (this.topLeftPointx-this.lowerRightPointx)*(this.topLeftPointy-this.lowerRightPointy);
        if(a<0)
            return -a;
        else
            return a;
    }
}

 

三.遇到的困难及见解

1.遇到的问题

①不能合理地根据题意抽象出相应的类。

②面对大量的输入,和多要求输出,不能做到恰当的判断和分类处理。

2.我的见解

①对于问题一我个人认为,根据题意,分析出需要储存的数据和功能,如果我们需要的某种功能是独立,那么可以单独创建一个方法实现该功能,如果我们需要的不是某种到单独的功能,而是多个功能与某些具有普遍性的性质结合的话,那么应该考虑创建一个类。

②对于问题二,对于输入数据的判断其表达的信息,重点在与题目所给的格式进行匹配,格式相匹配,则是题目对应的信息类型。所以重点在于对输入的数据进行格式匹配:方法有两种1.正则表达式mathch()匹配。2.split()拆分成多部份进行判断,但强烈建议使用正则表达式,因为它能更在便捷地同时,更全面地处理输入信息。

四.总结

1.Java学习的总结:

在学习Java的过程中,我发现Java是一门功能强大而又灵活的编程语言。通过掌握Java的核心概念,如面向对象编程、异常处理、集合框架等,我能够使用Java开发各种应用程序,并且具备了解决实际问题的能力。

关于Java学习的总结,我还可以总结如下几点:

  1. 基础知识的重要性:Java是一门庞大而复杂的语言,深厚的基础知识是学习的关键。理解并熟练掌握基本语法、数据类型、控制结构等基础概念对于进一步学习和应用Java至关重要。

  2. 实践是提高的关键:通过编写实际的代码来加深对Java的理解和掌握,这是学习过程中不可或缺的一部分。通过解决问题和实现项目,我积累了宝贵的经验,提升了编程能力和效率。

  3. 学习资源的选择:选择合适的学习资源对于学习Java至关重要。无论是教材、在线教程还是参与编程社区,都能提供不同程度的帮助和指导。合理利用这些资源,能够更好地加深对Java的理解。

1.Java课程改进的总结:

为了进一步改进Java课程,让学习者能够更加高效和深入地学习Java,我提出以下几点建议:

  1. 更注重实践:在课程中增加更多的实践项目和案例,让学生能够将所学知识应用到实际问题中,提升他们的实际编程能力。

  2. 强化基础知识学习:在课程最初阶段,要注重教授Java的核心概念和基础知识,包括面向对象编程、异常处理、集合框架等。只有打好基础,学生才能更好地理解和应用更高级的特性和框架。

  3. 引入案例和项目驱动学习:通过引入真实世界中的案例和项目,将学生置于实际开发环境中,让他们能够体验到Java在实际应用中的价值和用途。这样能够增加学生的兴趣和动力,同时也更加贴近实际工作需求。

  4. 增加互动和实时反馈机制:提供互动式学习,通过讨论、编程挑战和即时反馈,帮助学生更好地理解和消化所学知识。并且,提供一对一的指导和辅导,让学生可以针对自己的问题得到即时解答。

标签:空格,String,int,BLOG,table,new,out
From: https://www.cnblogs.com/oopxxz/p/17517696.html

相关文章

  • Blog2
    一、前言1.题量及难度这几次作业的题量适中,题目数量适中,不过也不过少,能够提供足够的练习机会。在PTA上的大作业中,一共有三道题。其中第一题和第三题难度不大,但第二题属于点线形系列的题目,是之前得分不高的PTA作业中的重点题型,占据了70分,而我只得到了39分。在期中考试中虽然只有......
  • 4-5次PTA题目总结blog
    前言:题目集1~3的知识点、题量、难度等情况如下:知识点:JAVA基础,基础算法,面向对象程序设计题量:共计2道题目难度:题目从易到难,逐层递进,分别为考察Java各个类的理解、掌握与运用。设计与分析:1importjava.text.ParseException;2importjava.time.DateTimeExce......
  • BLOG-2
    BLOG-2前言这次blog是关于pta题目集4-6和期中考试的总结,菜单计价系统还是挺难的,而期中考试就较为简单,题量也不大,但就是很耗时间。7-1菜单计价程序-4分数 100全屏浏览题目切换布局作者 蔡轲单位 南昌航空大学本体大部分内容与菜单计价程序-3相同,增加的部分用加粗文字......
  • BLOG-2
    一、前言(要写3000个字还是很累的,希望互评能给高点分,我给所有人都是95以上的!)对于pta4和pta5不想多说,点菜系列巅峰之作两题难度都非常大,而且很复杂,写很多次也没啥分,题量也大关于期中考试,难度到不大,主要考察继承多态接口什么的,题量还可以,就是要在规定时间内写完有点紧张二......
  • BLOG-2
    考试分析:个人感觉本次考试难度偏简单,也无需注意些什么,只要仔细读题问题应该都不大; 1.创建一个圆形类(Circle),私有属性为圆的半径,从控制台输入圆的半径,输出圆的面积输入格式:输入圆的半径,取值范围为(0,+∞),输入数据非法,则程序输出WrongFormat,注意:只考虑从控制台输入数值的情况......
  • BLOG2-PTA题目集4、5以及期中考试
    (1)前言本次博客主要涵盖了Java题目的几个主要知识点,包括:1.面向对象的基础知识:这部分主要包括了类和对象的基本概念,构造方法,访问权限和成员变量的相关内容。在面向对象编程中,对这些基础知识的理解至关重要。2.面向对象的设计原则:这个题目强调了两个重要的设计原则,即继承和组......
  • BLOG-2
    前言:这两次PTA4与5都是菜单计价系统的续集,并且都是PTA3的不同迭代的代码,所以需要对PTA3进行不同的修改来完成题目要求。PTA4:由于时间等诸多原因,我并没有完成PTA4上的任务,并且没有在PTA4上及时的提交代码,所以我无法附上我PTA4的源码,但是我将对菜单计价系统4的题目进行分析设计,......
  • Blog - 2
    前言题目量和难度我觉得觉得作业题量适中,不会太多也不会太少,能让我练习。这次大作业,一共三道题。第一题和第三题难度不大,但是第二题是点菜系列的题目,这道题占了70分,而我只拿到了60分。在期中考中,虽然有三道题,但难度不算太大,每一道题都是在前一道题的基础上稍加修改即可。实验一......
  • oo第二次BLOG
    一:总结  pta4,5和期中的作业其实主要用于对所学知识的练习,第六次作业只有一到成绩题目有一定的难度,后两次作业的题目这相对简单一些。作业尽力去完成,虽然有些不完美,但已经尽了全力。七,八次作业折相对简单一些,完成情况也相对好一点。二:作业实现7-1菜单计价程序-4......
  • 菜单4、5以及期中考试总结-BLOG-PTA-4、5
    22201612-刘健涛目录(1)前言(2)设计与分析(3)踩坑心得(4)改进建议(5)总结正文(1)前言  (2)设计与分析(3)踩坑心得(4)改进建议(5)总结......