首页 > 其他分享 >BLOG-2

BLOG-2

时间:2023-11-19 21:56:30浏览次数:24  
标签:空格 String temp int BLOG table out

BLOG-2

  • 前言

本次blog主要是对PTA题目集4、5、6以及期中考试的总结及分析,重点是对菜单计价四、五以及其中考试的总结。

菜单计价四和五都是在菜单计价三的基础上的延伸,菜单计价四增加了很多错误输入情况,菜单计价五则是在三的条件下去延伸了菜的一些特色。

这两次的大作业和期中考试的四道题目主要考察了Java中类的结构构造,继承和多态。

相比于前面的PTA大作业来说,这几次大作业主要都是围绕菜单计价这一个题目,虽然题目量不多,但是难度却是大大增加,对于本就基础薄弱,接受能力较差的我来说是更加的困难。

  • 程序设计分析

菜单计价四

一、题目:

7-1 菜单计价程序-4

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

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

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

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

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

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

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

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

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

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

删除记录格式:序号 delete

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

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

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

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

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

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

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

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

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

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

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

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

Dish {

String name;//菜品名称

int unit_price; //单价

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

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

Menu {

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

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

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

}

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

Record {

int orderNum;//序号

Dish d;//菜品\\

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

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

}

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

Order {

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

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

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

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

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

}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

例如:麻婆豆腐 9 T

菜价的计算方法:

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

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

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

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

输入格式:

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

菜品记录格式:

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

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

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

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

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

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

输出格式:

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

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

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

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

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

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

输入样例:

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

麻婆豆腐 12

油淋生菜 9 T

table 31 2023/2/1 14/20/00

1 麻婆豆腐 1 16

2 油淋生菜 1 2

2 delete

2 delete

end

输出样例:

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

table 31:

1 num out of range 16

2 油淋生菜 18

deduplication 2

table 31: 0 0

输入样例1:

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

麻婆豆腐 12

油淋生菜 9 T

table 31 2023/2/1 14/20/00

1 麻婆豆腐 1 16

2 油淋生菜 4 2

end

输出样例1:

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

table 31:

1 num out of range 16

2 portion out of range 4

table 31: 0 0

输入样例2:

桌号信息错误。例如:

麻婆豆腐 12

油淋生菜 9 T

table a 2023/3/15 12/00/00

1 麻婆豆腐 1 1

2 油淋生菜 2 1

end

输出样例2:

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

wrong format

输入样例3:

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

麻婆豆腐 12

油淋生菜 9 T

table 55 2023/3/31 12/000/00

麻辣香锅 15

1 麻婆豆腐 1 1

2 油淋生菜 2 1

end

输出样例3:

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

wrong format

输入样例4:

错误的菜谱记录。例如:

麻婆豆腐 12.0

油淋生菜 9 T

table 55 2023/3/31 12/00/00

麻辣香锅 15

1 麻婆豆腐 1 1

2 油淋生菜 2 1

end

输出样例4:

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

wrong format

table 55:

invalid dish

麻婆豆腐 does not exist

2 油淋生菜 14

table 55: 14 10

输入样例5:

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

麻婆豆腐 12

油淋生菜 9 T

table a 2023/3/15 12/00/00

1 麻婆 豆腐 1 1

2 油淋生菜 2 1

end

输出样例5:

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

wrong format

输入样例6:

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

麻婆豆腐 12

油淋生菜 9 T

table 1 2023/3/15 12/00/00

1 麻婆豆腐 1 1

2 油淋生菜 2 1

tab le 2 2023/3/15 12/00/00

1 麻婆豆腐 1 1

2 油淋生菜 2 1

end

输出样例6:

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

table 1:

1 麻婆豆腐 12

2 油淋生菜 14

wrong format

record serial number sequence error

record serial number sequence error

table 1: 26 17

二、设计思路:

类设计如下:

Dish类:用于表示菜品的类,包括菜名(name)、单价(unit_price)和是否特色菜(isT)。

Record类:用于表示订单中的记录,包括订单号(orderNum)、菜品(d)、份数(portion)、折扣(discount)等信息。包含了计算价格的方法getPrice()。

Menu类:用于管理菜单,包括查找菜品和添加菜品的功能。

Order类:用于表示订单,包括订单中的记录(records)。包含了添加记录、查找记录和删除记录的功能。使用ArrayList作为记录的存储结构,这样可以动态调整大小。

Table类:用于表示桌子,包括订单(order)、编号(num)、时间(time)、星期几(weekday)、消费总额(sum)等信息。包含了添加订单记录、计算消费总额、判断营业状态等功能。

三、代码展示:

import java.text.ParseException;

import java.time.DateTimeException;

import java.time.Duration;

import java.time.LocalDate;

import java.time.LocalDateTime;

import java.util.ArrayList;

import java.util.Scanner;

public class Main {

public static boolean isNum(String string) {

int key;

try {

key = Integer.parseInt(string);

return true;

} catch (NumberFormatException e) {

return false;

}

}

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

Menu menu = new Menu();

ArrayList<Table> tables = new ArrayList<Table>();

Scanner sc = new Scanner(System.in);

String str = new String();

int i = 0;

int portion = 0, discount = 0;

while (true) {// 输入菜单

Dish temp = new Dish();

int isRepeat = -1;

str = sc.nextLine();

if (str.matches("[\\S]* [1-9][\\d]*")) {

String[] token = str.split(" ");

temp.name = token[0];

temp.unit_price = Integer.parseInt(token[1]);

if (temp.unit_price > 300) {

System.out.println(temp.name + " price out of range " + temp.unit_price);

continue;

}

temp.isT = false;

isRepeat = menu.searchDish(temp.name);

if (isRepeat != -1) {

menu.dishs.remove(isRepeat);

}

menu.dishs.add(temp);

} else if (str.matches("[\\S]* [\\d]* T")) {

String[] token = str.split(" ");

temp.name = token[0];

temp.unit_price = Integer.parseInt(token[1]);

if (temp.unit_price > 300) {

System.out.println(temp.name + " price out of range " + temp.unit_price);

continue;

}

temp.isT = true;

if (isRepeat != -1) {

menu.dishs.remove(isRepeat);

}

menu.dishs.add(temp);

} else if (str.equals("end")) {

break;

} else if (str.matches("tab.*")) {

break;

} else {

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

continue;

}

}

while (!str.equals("end")) {

Table temp = new Table();

boolean isRepeat = false;

int repeatNum = 0;

if (str.matches("table.*")) {

if (str.matches("table [1-9][\\d]* [\\d]*/[\\d][\\d]?/[\\d][\\d]? [\\d][\\d]?/[\\d][\\d]?/[\\d][\\d]?")) {

String[] token = str.split(" ");

String[] Date = token[2].split("/");

String[] Time = token[3].split("/");

int[] intDate = new int[3];

int[] intTime = new int[3];

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

intDate[i] = Integer.parseInt(Date[i]);

intTime[i] = Integer.parseInt(Time[i]);

}

temp.num = Integer.parseInt(token[1]);

if (temp.num > 55) {

System.out.println(temp.num + " table num out of range");

str = sc.nextLine();

continue;

}

try {

temp.time = LocalDateTime.of(intDate[0], intDate[1], intDate[2], intTime[0], intTime[1],

intTime[2]);

temp.getWeekDay();

} catch (DateTimeException e) {

System.out.println(temp.num + " date error");

str = sc.nextLine();

continue;

}

if (!(temp.time.isAfter(LocalDateTime.of(2022, 1, 1, 0, 0, 0))

&& temp.time.isBefore(LocalDateTime.of(2024, 1, 1, 0, 0, 0)))) {

System.out.println("not a valid time period");

str = sc.nextLine();

continue;

}

// 判断桌号是否重复

if (temp.isOpen()) {

for (Table table : tables) {

if (temp.num == table.num && table.isOpen()) {

Duration duration = Duration.between(temp.time, table.time);

if (duration.toDays() == 0) {

if (temp.weekday > 0 && temp.weekday < 6) {

if (temp.time.getHour() < 15 && table.time.getHour() < 15) {

temp = table;

isRepeat = true;

repeatNum = tables.indexOf(table);

break;

}

} else {

if (duration.toHours() < 1) {

temp = table;

repeatNum = tables.indexOf(table);

isRepeat = true;

break;

}

}

}

}

}

}

if(!isRepeat) {

System.out.println("table " + temp.num + ": ");

}

 

}

else {

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

str = sc.nextLine();

continue;

}

// 本桌开始点菜

while (true) {

str = sc.nextLine();

if (str.matches("[1-9][\\d]* [\\S]* [\\d] [1-9][\\d]*")) {

String[] token = str.split(" ");

portion = Integer.parseInt(token[2]);

discount = Integer.parseInt(token[3]);

if (temp.order.records.size() > 0) {

if (Integer.parseInt(

token[0]) <= temp.order.records.get(temp.order.records.size() - 1).orderNum) {

System.out.println("record serial number sequence error");

continue;

}

}

if (menu.searchDish(token[1]) == -1) {

System.out.println(token[1] + " does not exist");

continue;

}

if (portion > 3 || portion < 1) {

System.out.println(Integer.parseInt(token[0]) + " portion out of range " + portion);

continue;

}

if (discount > 15) {

System.out.println(Integer.parseInt(token[0]) + " num out of range " + discount);

continue;

}

temp.od(menu, token[0], token[1], portion, discount);

}

// 判断是否为删除订单

else if (str.matches("[1-9][\\d]* delete")) {

String[] token = str.split(" ");

temp.order.delARecordByOrderNum(Integer.parseInt(token[0]));

}

// 判断是否为夹杂菜单

else if (str.matches("[\\S]* [\\d]*")) {

System.out.println("invalid dish");

continue;

} else if (str.matches("[\\S]* [\\d]* T")) {

System.out.println("invalid dish");

continue;

}

// 判断是否为代点

else if (str.matches("[\\d]* [\\d]* [\\S]* [\\d] [1-9][\\d]*")) {

String[] token = str.split(" ");

// 判断代点桌号是否存在

boolean exist = false;

for (int j = 0; j < tables.size(); j++) {

if (tables.get(j).num == Integer.parseInt(token[0])) {

exist = true;

break;

}

}

if (exist) {

System.out.print(Integer.parseInt(token[1]) + " table " + temp.num + " pay for table "

+ Integer.parseInt(token[0]) + " ");

Record treat = new Record();

treat.d = menu.dishs.get(menu.searchDish(token[2]));

portion = Integer.parseInt(token[3]);

discount = Integer.parseInt(token[4]);

treat.portion = portion;

treat.discount = discount;

System.out.print(treat.getPrice() + "\n");

temp.sum += treat.getPrice();

}

// 若不存在则输出内容

else {

System.out.println("Table number :" + Integer.parseInt(token[0]) + " does not exist");

}

} else if (str.equals("end")) {

break;

} else if(str.matches("ta.*")){

break;

 

}

else {

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

continue;

}

}

} else if (str.matches("t.*")) {

isRepeat = true;

temp = tables.get(tables.size());

while (true) {

str = sc.nextLine();

if (str.matches("[1-9][\\d]* [\\S]* [\\d] [1-9][\\d]*")) {

String[] token = str.split(" ");

portion = Integer.parseInt(token[2]);

discount = Integer.parseInt(token[3]);

// 判断订单号是否由小到大排列

if (temp.order.records.size() > 0) {

if (Integer.parseInt(

token[0]) <= temp.order.records.get(temp.order.records.size() - 1).orderNum) {

System.out.println("record serial number sequence error");

continue;

}

}

if (menu.searchDish(token[1]) == -1) {

System.out.println(token[1] + " does not exist");

continue;

}

if (portion > 3 || portion < 1) {

System.out.println(Integer.parseInt(token[0]) + " portion out of range " + portion);

continue;

}

if (discount > 15) {

System.out.println(Integer.parseInt(token[0]) + " num out of range " + discount);

continue;

}

temp.od(menu, token[0], token[1], portion, discount);

}

// 判断是否为删除订单

else if (str.matches("[1-9][\\d]* delete")) {

String[] token = str.split(" ");

temp.order.delARecordByOrderNum(Integer.parseInt(token[0]));

}

// 判断是否为夹杂菜单

else if (str.matches("[\\S]* [\\d]*")) {

System.out.println("invalid dish");

continue;

} else if (str.matches("[\\S]* [\\d]* T")) {

System.out.println("invalid dish");

continue;

}

// 判断是否为代点

else if (str.matches("[\\d]* [\\d]* [\\S]* [\\d] [1-9][\\d]*")) {

String[] token = str.split(" ");

// 判断代点桌号是否存在

boolean exist = false;

for (int j = 0; j < tables.size(); j++) {

if (tables.get(j).num == Integer.parseInt(token[0])) {

exist = true;

break;

}

}

if (exist) {

System.out.print(Integer.parseInt(token[1]) + " table " + temp.num + " pay for table "

+ Integer.parseInt(token[0]) + " ");

Record treat = new Record();

treat.d = menu.dishs.get(menu.searchDish(token[2]));

portion = Integer.parseInt(token[3]);

discount = Integer.parseInt(token[4]);

treat.portion = portion;

treat.discount = discount;

System.out.print(treat.getPrice() + "\n");

temp.sum += treat.getPrice();

}

// 若不存在则输出内容

else {

System.out.println("Table number :" + Integer.parseInt(token[0]) + " does not exist");

}

} else if (str.equals("end")) {

break;

} else {

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

continue;

}

}

if (tables.size() != 0) {

tables.get(tables.size() - 1).order.records.addAll(temp.order.records);

}

} else {

str = sc.nextLine();

continue;

}

// 本桌点菜结束,进入下一桌

if (isRepeat) {

tables.remove(repeatNum);

}

temp.getSum();

tables.add(temp);

}

// 最终输出桌号订单信息

for (i = 0; i < tables.size(); i++) {

if (tables.get(i).isOpen()) {

System.out

.println("table " + tables.get(i).num + ": " + tables.get(i).origSum + " " + tables.get(i).sum);

} else

System.out.println("table " + tables.get(i).num + " out of opening hours");

}

}

static class Dish {

String name;

int unit_price;

boolean isT = false;

}

static class Record {

int orderNum;

Dish d;

int portion;

int discount;

boolean isDeleted = false;

int getPrice() {

double price = d.unit_price * discount;

price *= portion == 2 ? 1.5 : portion == 3 ? 2 : 1;

return (int) Math.round(price);

}

}

static class Menu {

ArrayList<Dish> dishs = new ArrayList<Dish>();

int searchDish(String dishName) {

for (int i = 0; i < dishs.size(); i++) {

if (dishName.equals(dishs.get(i).name)) {

return i;

}

}

return -1;

}

Dish addDish(String dishName, int unit_price) {

Dish newDish = new Dish();

newDish.name = dishName;

newDish.unit_price = unit_price;

return newDish;

}

}

static class Order {

// Record[] records = new Record[20];

ArrayList<Record> records = new ArrayList<Record>();

Record addARecord(int orderNum, String dishName, int portion, int quota, Menu menu) {

Record newRecord = new Record();

newRecord.orderNum = orderNum;

newRecord.d = menu.dishs.get(menu.searchDish(dishName));

newRecord.portion = portion;

newRecord.discount = quota;

System.out.println(newRecord.orderNum + " " + newRecord.d.name + " " + newRecord.getPrice());

return newRecord;

}

int searchReocrd(String name) {

for (int i = 0; i < records.size(); i++) {

if (records.get(i).d.name == name) {

return i;

}

}

return -1;

}

boolean delARecordByOrderNum(int orderNum) {

boolean isDeleted = false;

 

for (Record record : records) {

if (record.orderNum == orderNum) {

if (!record.isDeleted) {

record.isDeleted = true;

isDeleted = true;

} else {

System.out.println("deduplication " + orderNum);

}

}

}

 

if (!isDeleted) {

System.out.println("delete error;");

return false;

}

 

return true;

}

}

static class Table {

Order order = new Order();

int num;

LocalDateTime time;

int weekday;

long sum = 0;

long origSum = 0;

void od(Menu menu, String str1, String str2, int portion, int discount) {

{

order.records.add(order.addARecord(Integer.parseInt(str1), str2, portion, discount, menu));

}

}

void getWeekDay() {

weekday = time.getDayOfWeek().getValue();

}

void getSum() {

for (Record record : order.records) {

if (!record.isDeleted) {

origSum += record.getPrice();

 

if (record.d.isT && weekday > 0 && weekday < 6) {

sum += Math.round(record.getPrice() * 0.7);

} else if (weekday > 0 && weekday < 6) {

if ((time.getHour() == 20 && time.getMinute() <= 30) || (time.getHour() >= 17 && time.getHour() < 20)) {

sum += Math.round(record.getPrice() * 0.8);

} else if ((time.getHour() == 14 && time.getMinute() <= 30) || (time.getHour() >= 10 && time.getHour() < 14)) {

sum += Math.round(record.getPrice() * 0.6);

} else {

sum += record.getPrice();

}

} else {

sum += record.getPrice();

}

}

}

}

boolean isOpen() {

boolean isWeekday = weekday > 0 && weekday < 6;

boolean isEvening = (time.getHour() == 20 && time.getMinute() <= 30) || (time.getHour() >= 17 && time.getHour() < 20);

boolean isLunchtime = (time.getHour() == 14 && time.getMinute() <= 30) || (time.getHour() == 10 && time.getMinute() >= 30) || (time.getHour() > 10 && time.getHour() < 14);

boolean isOtherTime = (time.getHour() == 21 && time.getMinute() <= 30) || (time.getHour() == 9 && time.getMinute() >= 30) || (time.getHour() > 9 && time.getHour() < 21);

 

return (isWeekday && (isEvening || isLunchtime)) || (!isWeekday && isOtherTime);

}

}

}

  1. 类图

菜单计价五

  1. 题目:

本题在菜单计价程序-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

  1. 设计思路

类设计如下:

Dish类:

属性包括菜品名称(name)、单价(unit_price)和类型(type)。

有两个构造函数,一个包含菜品类型,一个不包含菜品类型。

重写了toString方法,返回菜品名称。

Menu类:

包含了一个菜品列表(dishs)。

包含了添加菜品的方法,可以根据菜品名称和单价添加菜品,也可以根据菜品名称、单价和类型添加菜品。

包含了搜索菜品的方法,可以根据菜品名称查找菜品。

Record类:

记录了订单号(orderNum)、菜品(dish)、份数(portion)、数量(quantity)、口味等级(level)、标记(flag)、桌号(givenId)等信息。

包含了检查口味等级的方法(check_level)和计算价格的方法(getPrice)。

包含了打印订单信息的方法(print)。

Table类:

包含了订单记录列表(records)、桌号(tableId)、顾客姓名(name)、电话号码(phone)、日期(date)、时间(time)、标记(flag)等信息。

包含了获取营业时间系数的方法(getTemp)、计算总价和结账价格的方法(getTotalPrice和getCheckedPrice)、获取特定菜品口味平均值的方法(getAveLevel)、添加订单记录的方法(addRecord)、删除指定订单记录的方法(delRecordByOrderNum)以及其他打印信息的方法(printId和printInfo)。

  1. 代码展示

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.*;

public class Main {

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

Menu menu = new Menu();

// 读入菜单信息

Scanner input = new Scanner(System.in);

String menuLine = input.nextLine();

while (!menuLine.startsWith("table")) {

String[] menuInfo = menuLine.split(" ");

if (menuInfo.length == 2) {

String name = menuInfo[0];

int unit_price = Integer.parseInt(menuInfo[1]);

if (menu.searchDish(name) == null) {

menu.addDish(name, unit_price);

}

} else if (menuInfo.length == 4 && menuLine.endsWith("T")) {

String name = menuInfo[0];

String type = menuInfo[1];

int unit_price = Integer.parseInt(menuInfo[2]);

Map<String, String> map = new HashMap<String, String>() {

{

put("川菜", "ChuanCai");

put("晋菜", "JinCai");

put("浙菜", "ZheCai");

}

};

DishType dishType = DishType.valueOf(map.get(type));

if (menu.searchDish(name) == null) {

menu.addDish(name, unit_price, dishType);

}

} else {

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

}

menuLine = input.nextLine();

}

ArrayList<Table> tables = new ArrayList<>();

ArrayList<String> names = new ArrayList<>();

// 读入订单信息

int tableId = 0;

String name = null;

String phone = null;

Date date = null;

Date time = null;

boolean legaltime = true;

boolean legalformat = true;

String orderLine = menuLine;

while (!orderLine.equals("end")) {

String[] orderInfo = orderLine.split(" ");

// 解析桌号标识

if (orderLine.startsWith("table")) {

legalformat = true;

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");

SimpleDateFormat timeFormat = new SimpleDateFormat("HH/mm/ss");

tableId = Integer.parseInt(orderInfo[1]);

name = orderInfo[3];

phone = orderInfo[4];

try {

date = dateFormat.parse(orderInfo[5]);

time = timeFormat.parse(orderInfo[6]);

} catch (Exception e) {

legalformat = false;

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

orderLine = input.nextLine();

continue;

}

String regex = "^1(80|81|89|33|35|36)\\d{8}$";

Table table = new Table(tableId, name, phone, date, time);

tables.add(table);

if (name.length() > 10 || !phone.matches(regex)) {

legalformat = false;

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

orderLine = input.nextLine();

continue;

}

if (!names.contains(name)) {

names.add(name);

}

if (table.getTemp(true) == 0) {

legaltime = false;

System.out.println("table " + table.tableId + " out of opening hours");

} else {

System.out.println(table.printId());

}

} else {

if (legalformat) {

int orderNum;

try {

orderNum = Integer.parseInt(orderInfo[0]);

} catch (Exception e) {

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

orderLine = input.nextLine();

continue;

}

if (orderLine.endsWith("delete")) {

if (!tables.get(tableId - 1).delRecordByOrderNum(orderNum)) {

System.out.println("delete error");

}

} else {

if (orderInfo.length == 4) {

String dishName = orderInfo[1];

int portion = Integer.parseInt(orderInfo[2]);

int quantity = Integer.parseInt(orderInfo[3]);

Dish dish = menu.searchDish(dishName);

if (dish == null) {

System.out.println(dishName + " does not exist");

orderLine = input.nextLine();

continue;

}

Record record = new Record(tableId, orderNum, dish, portion, quantity);

tables.get(tableId - 1).addRecord(record);

if (legaltime) {

System.out.println(record.print(tableId));

}

} else if (orderInfo.length == 5) {

String dishName = orderInfo[1];

int level = Integer.parseInt(orderInfo[2]);

int portion = Integer.parseInt(orderInfo[3]);

int quantity = Integer.parseInt(orderInfo[4]);

Dish dish = menu.searchDish(dishName);

if (dish == null) {

System.out.println(dishName + " does not exist");

orderLine = input.nextLine();

continue;

}

Record record = new Record(tableId, orderNum, dish, level, portion, quantity);

tables.get(tableId - 1).addRecord(record);

if (legaltime) {

System.out.println(record.print(tableId));

}

} else if (orderInfo.length == 6) {

int givenId = Integer.parseInt(orderInfo[1]);

String dishName = orderInfo[2];

int level = Integer.parseInt(orderInfo[3]);

int portion = Integer.parseInt(orderInfo[4]);

int quantity = Integer.parseInt(orderInfo[5]);

Dish dish = menu.searchDish(dishName);

if (dish == null) {

System.out.println(dishName + " does not exist");

orderLine = input.nextLine();

continue;

}

Record record1 = new Record(givenId, orderNum, dish, level, portion, quantity);

Record record2 = new Record(givenId, orderNum, dish, level, 0, quantity);

tables.get(tableId - 1).addRecord(record1);

tables.get(givenId - 1).addRecord(record2);

if (legaltime) {

System.out.println(record1.print(tableId));

}

} else {

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

}

}

}

}

// 读入下一个桌号标识

orderLine = input.nextLine();

}

input.close();

for (Table table : tables) {

if (table.flag && table.getTotalPrice() != 0) {

System.out.println(table.printInfo());

}

}

names.sort(new Comparator<String>() {

@Override

public int compare(String s1, String s2) {

return s1.compareTo(s2);

}

});

for (String costumName : names) {

int sum = 0;

String costumPhone = null;

for (Table table : tables) {

if (table.name.equals(costumName)) {

sum += table.getCheckedPrice();

costumPhone = table.phone;

}

}

if (sum != 0) {

System.out.println(costumName + " " + costumPhone + " " + sum);

}

}

}

}

enum DishType {

ChuanCai,

JinCai,

ZheCai,

}

class Dish {

public String name;

public int unit_price;

public DishType type;

public Dish(String name, int unit_price, DishType type) {

this.name = name;

this.unit_price = unit_price;

this.type = type;

}

public Dish(String name, int unit_price) {

this.name = name;

this.unit_price = unit_price;

}

@Override

public String toString() {

return name;

}

}

class Menu {

public ArrayList<Dish> dishs = new ArrayList<>();

public Dish searchDish(String dishName) {

for (Dish dish : dishs) {

if (dish.name.equals(dishName)) {

return dish;

}

}

return null;

}

void addDish(String dishName, int unit_price) {

dishs.add(new Dish(dishName, unit_price));

}

void addDish(String dishName, int unit_price, DishType type) {

dishs.add(new Dish(dishName, unit_price, type));

}

}

class Record {

int orderNum;

Dish dish;

int portion;

int quantity;

int level;

boolean flag;

int givenId;

boolean check_level() {

boolean isValidLevel = true;

switch (dish.type) {

case ChuanCai:

isValidLevel = (level >= 0 && level <= 5);

break;

case JinCai:

isValidLevel = (level >= 0 && level <= 4);

break;

case ZheCai:

isValidLevel = (level >= 0 && level <= 3);

break;

default:

isValidLevel = true;

}

return isValidLevel;

}

public Record(int givenID, int orderNum, Dish dish, int portion, int quantity) {

this.orderNum = orderNum;

this.dish = dish;

this.portion = portion;

this.quantity = quantity;

this.level = -1;

this.flag = true;

this.givenId = givenID;

}

public Record(int givenId, int orderNum, Dish dish, int level, int portion, int quantity) {

this.orderNum = orderNum;

this.dish = dish;

this.portion = portion;

this.quantity = quantity;

this.level = level;

this.flag = check_level();

this.givenId = givenId;

}

int getPrice() {

if (!flag)

return 0;

double temp = 0;

switch (portion) {

case 1:

temp = 1;

break;

case 2:

temp = 1.5;

break;

case 3:

temp = 2;

break;

}

int price = (int) Math.round(dish.unit_price * temp) * quantity;

return price;

}

int getCheckedPrice(Double temp) {

return (int) Math.round(getPrice() * temp);

}

public String print(int tableId) {

if (flag == false) {

switch (dish.type) {

case ChuanCai:

return "spicy num out of range :" + level;

case JinCai:

return "acidity num out of range :" + level;

case ZheCai:

return "sweetness num out of range :" + level;

default:

return null;

}

} else {

if (givenId == tableId) {

return orderNum + " " + dish.toString() + " " + getPrice();

}

return orderNum + " table " + tableId + " pay for table " + givenId + " " + getPrice();

}

}

@Override

public String toString() {

return "Record [orderNum=" + orderNum + ", dish=" + dish + ", portion=" + portion + ", quantity=" + quantity

+ ", level=" + level + ", flag=" + flag + ", givenId=" + givenId + "]";

}

}

class Table {

ArrayList<Record> records = new ArrayList<>();

int tableId;

String name;

String phone;

Date date;

Date time;

boolean flag;

public Table(int tableId, String name, String phone, Date date, Date time) {

this.name = name;

this.phone = phone;

this.date = date;

this.time = time;

this.tableId = tableId;

this.flag = true;

}

double getTemp(boolean Special) throws ParseException {

double temp = 0;

SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm");

Calendar cal = Calendar.getInstance();

cal.setTime(date);

int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

// 营业时间

if (dayOfWeek == 1 || dayOfWeek == 7) {

if (time.after(sdfTime.parse("9:29")) && time.before(sdfTime.parse("21:31"))) {

temp = 1;

}

} else {

if (time.after(sdfTime.parse("16:59")) && time.before(sdfTime.parse("20:31"))) {

if (Special) {

temp = 0.7;

} else {

temp = 0.8;

}

} else if (time.after(sdfTime.parse("10:29")) && time.before(sdfTime.parse("14:31"))) {

if (Special) {

temp = 0.7;

} else {

temp = 0.6;

}

}

}

if (temp == 0) {

flag = false;

}

return temp;

}

int getTotalPrice() {

int sum = 0;

for (Record record : records) {

sum += record.getPrice();

}

return sum;

}

int getCheckedPrice() throws ParseException {

int sum = 0;

for (Record record : records) {

if (record.level != -1) {

sum += record.getCheckedPrice(getTemp(true));

} else {

sum += record.getCheckedPrice(getTemp(false));

}

}

return sum;

}

String getAveLevel(DishType type) {

String[] spicy = { "不辣", "微辣", "稍辣", "辣", "很辣", "爆辣" };

String[] acidity = { "不酸", "微酸", "稍酸", "酸", "很酸" };

String[] sweetness = { "不甜", "微甜", "稍甜", "甜" };

double sum = 0;

double num = 0;

for (Record record : records) {

if (record.dish.type == type) {

if (record.flag && tableId == record.givenId) {

num += record.quantity;

sum += record.level * record.quantity;

}

}

}

if (num == 0) {

return "";

}

int ave = (int) Math.round(sum / num);

switch (type) {

case ChuanCai:

return " 川菜 " + (int) num + " " + spicy[ave];

case JinCai:

return " 晋菜 " + (int) num + " " + acidity[ave];

case ZheCai:

return " 浙菜 " + (int) num + " " + sweetness[ave];

default:

return null;

}

}

void addRecord(Record record) {

records.add(record);

}

boolean delRecordByOrderNum(int orderNum) {

return records.removeIf(record -> record.orderNum == orderNum);

}

Record findRecordByOrderNum(int orderNum) {

for (Record record : records) {

if (record.orderNum == orderNum) {

return record;

}

}

return null;

}

public String printId() {

return "table " + tableId + ": ";

}

public String printInfo() throws ParseException {

String chuan = getAveLevel(DishType.ChuanCai);

String jin = getAveLevel(DishType.JinCai);

String zhe = getAveLevel(DishType.ZheCai);

if (chuan == "" && jin == "" && zhe == "") {

return "table " + tableId + ": " + getTotalPrice() + " " + getCheckedPrice() + " ";

} else {

return "table " + tableId + ": " + getTotalPrice() + " " + getCheckedPrice() + chuan + jin + zhe;

}

}

}

  1. 类图

期中考试题目4:

一、题目

7-4 测验4-抽象类与接口

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

提示:题目中Shape类要实现Comparable接口。

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

public class Main {

public static void main(String\[\] args) {

// TODO Auto-generated method stub

Scanner input = new Scanner(System.in);

ArrayList<Shape> list = new ArrayList<>();

int choice = input.nextInt();

while(choice != 0) {

switch(choice) {

case 1://Circle

double radiums = input.nextDouble();

Shape circle = new Circle(radiums);

list.add(circle);

break;

case 2://Rectangle

double x1 = input.nextDouble();

double y1 = input.nextDouble();

double x2 = input.nextDouble();

double y2 = input.nextDouble();

Point leftTopPoint = new Point(x1,y1);

Point lowerRightPoint = new Point(x2,y2);

Rectangle rectangle = new Rectangle(leftTopPoint,lowerRightPoint);

list.add(rectangle);

break;

}

choice = input.nextInt();

}

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

for(int i = 0; i < list.size(); i++) {

System.out.print(String.format("%.2f", list.get(i).getArea()) + " ");

}

}

}

输入格式:

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

输入图形所需参数

输出格式:

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

输入样例:

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

1

2.3

2

3.2

3

6

5

1

2.3

0

输出样例:

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

5.60 16.62 16.62

  1. 设计思路

类设计如下:

Circle 类 (圆形类):

包含一个私有属性 radius 表示圆的半径,以及计算圆面积的方法。在控制台输入圆的半径后,调用计算面积的方法,将结果输出到控制台。如果输入数据非法,输出 "Wrong Format"。

Rectangle 类 (矩形类):

包含四个私有属性表示左上角坐标点和右下角坐标点的 x 和 y 坐标值。在控制台输入这四个坐标值后,调用计算面积的方法,将结果输出到控制台。

Shape 类 (图形类):

Shape 类是 Circle 和 Rectangle 类的父类,它包含一个抽象方法 area(),代表计算图形面积的方法,并实现了 Comparable 接口,用于按照图形面积进行排序。由于 area() 方法是一个抽象方法,所以 Shape 类也是一个抽象类。

Main 类 (主类):

Main 类的职责是从控制台读取数据,根据输入的形状类型分别创建 Circle 和 Rectangle 对象,并计算输出它们的面积。此外,它还需要将所有图形放入一个列表中,调用 Collections.sort() 方法按照面积大小进行排序,最后输出排序过后的图形信息。

三、代码展示

import java.util.ArrayList;

import java.util.Comparator;

import java.util.Scanner;

abstract class Shape implements Comparable<Shape> {

public Shape() {}

public abstract double getArea();

@Override

public int compareTo(Shape other) {

if (this.getArea() < other.getArea()) {

return -1;

} else if (this.getArea() > other.getArea()) {

return 1;

} else {

return 0;

}

}

}

class Circle extends Shape {

private double radius = 0;

public Circle() {}

public Circle(double radius) {

this.radius = radius;

}

public double getRadius() {

return radius;

}

public void setRadius(double radius) {

this.radius = radius;

}

@Override

public double getArea() {

return Math.PI * radius * radius;

}

}

class Rectangle extends Shape {

private Point topLeftPoint;

private Point lowerRightPoint;

public Rectangle() {

topLeftPoint = new Point();

lowerRightPoint = new Point();

}

public Rectangle(Point topLeftPoint, Point lowerRightPoint) {

this.topLeftPoint = topLeftPoint;

this.lowerRightPoint = lowerRightPoint;

}

public Point getTopLeftPoint() {

return topLeftPoint;

}

public void setTopLeftPoint(Point topLeftPoint) {

this.topLeftPoint = topLeftPoint;

}

public Point getLowerRightPoint() {

return lowerRightPoint;

}

public void setLowerRightPoint(Point lowerRightPoint) {

this.lowerRightPoint = lowerRightPoint;

}

public double getLength() {

return Math.abs(lowerRightPoint.getX() - topLeftPoint.getX());

}

public double getHeight() {

return Math.abs(lowerRightPoint.getY() - topLeftPoint.getY());

}

@Override

public double getArea() {

return getLength() * getHeight();

}

}

class Point {

private double x;

private double y;

public Point() {}

public Point(double x, double y) {

this.x = x;

this.y = 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;

}

}

public class Main {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

ArrayList<Shape> list = new ArrayList<>();

int choice = input.nextInt();

while(choice != 0) {

switch(choice) {

case 1://Circle

double radius = input.nextDouble();

if(radius>0)

{

Shape circle = new Circle(radius);

list.add(circle);

}

else System.out.println("Wrong Format");

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()) + " ");

}

}

}

四、类图

IMG_256

  • 踩坑心得

1.异常处理:

在代码中应该考虑各种异常情况,并进行适当的错误处理。例如,处理不存在的桌号、重复的菜谱信息、重复的删除记录等情况,给出合适的错误提示。

2.ArrayList的并发访问:

在Table类中的records属性是一个ArrayList,在多线程环境下可能存在并发访问的问题。如果涉及到多线程操作,需要考虑使用线程安全的集合类或者对ArrayList进行适当的同步处理。

3.空指针异常:

在Record类的print方法中,对dish的type属性进行了访问,如果dish为null,就会抛出空指针异常。在使用对象属性之前,需要进行合理的空指针判断。

4.输入格式验证:

确保输入的格式符合要求,例如正确的桌号格式、时间格式以及菜单、订单的格式。对于不合法的输入,需要进行适当的错误处理。

5.访问修饰符错误:

忘记添加正确的访问修饰符(如public、private)可能导致无法访问或访问权限错误。

6.参数错误:

使用构造方法时需要注意参数的类型和顺序是否正确,否则会出现编译错误或运行时错误。

  • 代码改进建议

1.菜单系类:

  1. 在使用try-catch块捕获异常时,尽量避免捕获所有异常,可以只捕获需要处理的特定异常类型,以便更好地处理异常情况。
  2. 在比较字符串时,推荐使用equals()方法而不是"=="运算符,以确保比较的是字符串的内容而不是引用。
  3. 重写toString()方法:在Shape、Circle和Rectangle类中,可以重写toString()方法以提供更有意义的字符串表示形式。这将有助于调试和输出对象时的可读性。
  4.  考虑将 Record 类的一些逻辑(如价格计算和级别检查)封装为方法,以提高代码的可读性和可维护性。

2.期中:

  1. 类的封装:考虑将每个类放在单独的文件中,并使用适当的访问修饰符(如private、protected、public)来限制成员变量和方法的访问权。
  2. 操作符的连续使用:在使用Scanner读取输入时,可以考虑使用操作符的连续使用来简化代码。例如,可以将以下代码段:

double x1 = input.nextDouble();

double y1 = input.nextDouble();

double x2 = input.nextDouble();

double y2 = input.nextDouble();

简化为:

double x1, y1, x2, y2;

x1 = input.nextDouble();

y1 = input.nextDouble();

x2 = input.nextDouble();

y2 = input.nextDouble();

  • 总结

在编写上述代码的实验过程中,我学到了许多关于类设计和面向对象编程的知识。下面是我的实验总结:

1.良好的类设计原则:

在开始编写代码之前,先进行合理的类设计是非常重要的。良好的类设计应该遵循单一职责原则、开放封闭原则、依赖倒置原则等基本原则。

2.合理的类和方法命名:

类和方法的命名应该清晰、准确地表达其功能和用途,以提高代码的可读性和可维护性。

3.合理使用数据结构和数据类型:

在选择数据结构和数据类型时,要根据实际需求进行合理选择。例如,在菜单类中使用ArrayList来管理菜品列表,在订单记录类中使用HashMap来存储订单信息。

4.异常处理:

在编写代码时,要考虑可能出现的异常情况,并进行合理的异常处理。这样可以增加程序的健壮性和可靠性。

5.代码重用:

在设计类和方法时,要尽量避免重复代码的出现。可以利用继承、接口、组合等技术来实现代码的重用,提高代码的可维护性和可扩展性。

6.代码风格和格式:

在编写代码时,要注意良好的代码风格和格式。使用适当的缩进、注释和命名规范,可以提高代码的可读性和可理解性。

7.测试和调试:

在编写代码后,进行测试和调试是非常重要的。通过单元测试和集成测试,可以验证代码的正确性,并发现潜在的问题和bug。

总的来说,这次实验对我的面向对象编程能力和代码设计能力有了很大的提升。我学会了如何合理地设计和组织类,如何处理异常情况,以及如何进行代码的测试和调试。通过这次实验,我对面向对象编程的思想和原则有了更深入的理解,这对我的编程能力将产生长远的影响。

标签:空格,String,temp,int,BLOG,table,out
From: https://www.cnblogs.com/xiaozhao1109/p/17842749.html

相关文章

  • BLOG-2
    importjava.time.DayOfWeek;importjava.time.LocalDate;importjava.time.LocalDateTime;importjava.time.LocalTime;importjava.time.format.DateTimeFormatter;importjava.util.*;classUserInformationimplementsComparable<UserInformation>{publicSt......
  • java-blog2
    一、前言1、知识点:主要是java中类与类之间的关系和类图的构建以及对数据的处理2、题量:题量不大,简单的作业题量多点,难点的作业题量少点总体来说还行3、难度:难度在不断的变大,每一题所花的时间也在不断的增加二、设计与分析:第四次大作业:设计点菜计价程序,根据输入的信息,计算并输......
  • BLOG-2
    一、前言这几次的习题集都是围绕菜单计价程序进行的,对菜单计价程序进行了2,3,4,5的迭代,对一个程序进行的不同方向的深化。在深化的过程中我发现了自己在代码上很多方向上的不足,对面向对象编程的了解还不够,没有做出符合设计原则的设计。这导致我在对程序进行迭代的时候经常对代码......
  • BLOG-2
    1.前言1.知识点总结:第四次PTA作业集7-1菜单计价程序-3分数40全屏浏览题目切换布局作者 蔡轲单位 南昌航空大学设计点菜计价程序,根据输入的信息,计算并输出总价格。输入内容按先后顺序包括两部分:菜单、订单,最后以"end"结束。菜单由一条或多条......
  • BLOG-2
    作业总结1.1前言这几次作业主要是对菜单计价程序的完善,第四次作业中的菜单计价程序2是在菜单计价程序1的基础上进行完善,添加了输入菜单和份额的要求,难度还在可以接受的范围。第四次作业中的菜单计价程序3则是在菜单计价程序2的基础上添加了一系列的要求,包括添加桌号、代点菜......
  • 南昌航空大学JAVA Blog-2题目4-6期中考试
    一.前言 在进行题目集4-6的练习时,老师课堂上讲的内容一般都会围绕在这次题目集需要用到的新内容上。对于题目集代码的完成有很大的帮助,如课堂讲的封装、继承、多态等内容,简化了代码修改的难度,正则表达式则在一定程度上减少了代码量。但是就我个人认为,这几次的题目集除了期中考试......
  • BLOG-2
    一.前言  这次是对之前发布的PTA题目集4、5、6以及期中考试的总结。题目集4有两个题目,是菜单计价程序-2和菜单计价程序-3,难度较后面的题目来说相对简单,但是我当时还是觉得不简单。题目集5、6都只有一个题目,分别是菜单计价程序-4和菜单计价程序-5,是菜单计价程序的最后两个题目......
  • BLOG-2
    前言:这是本学期第二次博客作业,是对pta题目集4,5,6以及期中考试的总结。第四次作业难度正常,熟练掌握知识点应该就能完成。之后的第五次和第六次难度都非常高,需要花费很多时间。第四次作业主要考察了知识点:类的应用,正则表达式,面向对象思想;第五次作业主要考察了知识点:类的应用,面向对象......
  • BLOG-2
    (1)前言:题目集4:知识点:类与对象、字符串方法调用、边界值判断题量:中难度:难题集5:知识点:类与对象、正则表达式题量:大难度:难题目集6:知识点:类与对象、、边界值判断、正则表达式题量:大难度:难期中考试:知识点:类设计、继承与多态题量:中难度:中(2)设计与分析:题集4:主要题目为菜......
  • BLOG-2
    7-1菜单计价程序-3分数40全屏浏览题目切换布局作者 蔡轲单位 南昌航空大学设计点菜计价程序,根据输入的信息,计算并输出总价格。输入内容按先后顺序包括两部分:菜单、订单,最后以"end"结束。菜单由一条或多条菜品记录组成,每条记录一行每条菜品记录......