第二次博客作业——4-6次实验及期中考试总结

  1. 前言
  2. 知识点

本次实验最关键的就是菜单计价程序,三次实验根据菜单计价程序-1一点一点添加功能,难度逐级递增,主要知识点还是与java类有关,其中包括了:

方法:类中定义的行为,用于描述对象能够做什么。方法包括普通方法、构造方法和静态方法等。

封装:通过访问修饰符(public、private、protected)来控制类的成员变量和方法的访问权限,实现数据隐藏和保护。

继承:子类可以继承父类的属性和方法,可以扩展父类的功能,提高代码的复用性。

多态:允许不同的子类对象对同一消息作出不同的响应,提高了灵活性和可扩展性。

抽象类和接口:抽象类用于定义具有某些未实现方法的类,而接口定义了一组方法的规范,实现类必须实现这些方法。

类的实例化:使用关键字"new"可以创建类的实例(对象),并使用构造方法对其进行初始化。

类的访问控制:Java中使用访问修饰符来限定类、属性和方法的访问权限,包括public、private、protected和默认(package-private)等。

静态成员:使用关键字"static"可以定义静态成员变量和静态方法,它们属于类而不是对象,可以直接通过类名访问。

  1. 题量及难度

题量都不多,实验4有四道题,实验5和6都是只有一道题——菜单计价程序,但是单独较大,虽然是根据菜单计价程序-1的基础上一步步增加内容,比直接写要清楚明了的多,但是看到那么长的题目还是会有畏惧之心,而且代码要想实现起码要个三五百行,对于一个编程能力不够强的人来说还是有一定难度。老师给了足够长时间,但是测试点比较多,而且很细,所以本人没有一次是拿了满分的。

  1. 设计与分析

菜单计价程序-2:

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

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

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

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

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

点菜记录包含:序号、菜名、份额、份数。

份额可选项包括:1、2、3,分别代表小、中、大份。

删除记录格式:序号 delete

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

不同份额菜价的计算方法:

小份菜的价格=菜品的基础价格。

中份菜的价格=菜品的基础价格1.5。

小份菜的价格=菜品的基础价格2。

如果计算出现小数,按四舍五入的规则进行处理。

参考以下类的模板进行设计:

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

Dish {

String name;//菜品名称

int unit_price; //单价

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

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

Menu {

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

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

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

}

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

Record {

int orderNum;//序号\

Dish d;//菜品\

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

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

}

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

Order {

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

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

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

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

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

}

按题目要求,代码如下:

Dish类:

class Dish {

String name;//菜品名称

int unit_price; //单价

public String getname() {

return name;

}

public int getUnit_price() {

return unit_price;

}

public void setname(String name) {

this.name = name;

}

public void setUnit_price(int unit_price) {

this.unit_price = unit_price;

}

public Dish(String name, int unit_price) {

this.name = name;

this.unit_price = unit_price;

}

public Dish() {

}

int getPrice(int portion) {

int price;

if (portion == 1) {

price = Math.round(unit_price * 1);

} else if (portion == 2) {

price = Math.round(unit_price * 1.5f);

} else {

price = Math.round(unit_price * 2);

}

return price;

}

}

Menu类:

class Menu {

private List<Dish> dishs = new ArrayList<>();//菜品数组,保存所有菜品信息

Dish searthDish(String dishName) {

for (Dish dish : dishs) {

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

return dish;

}

}

return null;

}

Order类:

class Order {

private Menu menu;

private static List<Record> records = new ArrayList<>();//保存订单上每一道的记录

public Order(Menu menu) {

this.menu = menu;

}

//计算订单的总价

int getTotalPrice() {

int sum = 0;

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

Record record = records.get(i);

int price = record.getPrice();

if (!record.isDelete()) {

sum += price;

}

}

return sum;

}

可以通过循环接收用户输入,根据用户的操作来更新菜单和订单列表,并最终计算总价格并输出。

类图如下:

菜单计价程序-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"

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

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

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

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

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

Menu类:

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 Dish1 = new Dish();

Dish1.name = dishName;

Dish1.unit_price = unit_price;

return Dish1;

}

}

Order类:

static class Order {

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

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

Record Record1 = new Record();

Record1.orderNum = orderNum;

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

Record1.portion = portion;

Record1.quota = quota;

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

return Record1;

}

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) {

for (Record record : records) {

if (record.orderNum == orderNum) {

if (!record.isDeleted) {

record.isDeleted = true;

return true;

} else {

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

return false;

}

}

}

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

return false;

}

}

Table类:

static class Table {

Order order = new Order();

int num;

int weekday;

LocalDateTime time;

long sum = 0;

long origSum = 0;

void oder(Menu menu, String str1, String str2, int portion, int quota) {

{

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

}

}

void getWeekDay() {

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

}

void getSum() {

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

if (!order.records.get(i).isDeleted) {

origSum += order.records.get(i).getPrice();

if (order.records.get(i).d.isT) {

switch (weekday) {

case 1:

sum += Math.round(order.records.get(i).getPrice() * 0.7);

break;

case 2:

sum += Math.round(order.records.get(i).getPrice() * 0.7);

break;

case 3:

sum += Math.round(order.records.get(i).getPrice() * 0.7);

break;

case 4:

sum += Math.round(order.records.get(i).getPrice() * 0.7);

break;

case 5:

sum += Math.round(order.records.get(i).getPrice() * 0.7);

break;

default:

sum += order.records.get(i).getPrice();

break;

}

} else {

switch (weekday) {

case 1:

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

sum += Math.round(order.records.get(i).getPrice() * 0.8);

} else if (time.getHour() == 20) {

if (time.getMinute() <= 30) {

sum += Math.round(order.records.get(i).getPrice() * 0.8);

}

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

sum += Math.round(order.records.get(i).getPrice() * 0.6);

} else if (time.getHour() == 14) {

if (time.getMinute() <= 30) {

sum += Math.round(order.records.get(i).getPrice() * 0.6);

}

} else {

sum += order.records.get(i).getPrice();

}

break;

case 2:

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

sum += Math.round(order.records.get(i).getPrice() * 0.8);

} else if (time.getHour() == 20) {

if (time.getMinute() <= 30) {

sum += Math.round(order.records.get(i).getPrice() * 0.8);

}

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

sum += Math.round(order.records.get(i).getPrice() * 0.6);

} else if (time.getHour() == 14) {

if (time.getMinute() <= 30) {

sum += Math.round(order.records.get(i).getPrice() * 0.6);

}

} else {

sum += order.records.get(i).getPrice();

}

break;

case 3:

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

sum += Math.round(order.records.get(i).getPrice() * 0.8);

} else if (time.getHour() == 20) {

if (time.getMinute() <= 30) {

sum += Math.round(order.records.get(i).getPrice() * 0.8);

}

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

sum += Math.round(order.records.get(i).getPrice() * 0.6);

} else if (time.getHour() == 14) {

if (time.getMinute() <= 30) {

sum += Math.round(order.records.get(i).getPrice() * 0.6);

}

} else {

sum += order.records.get(i).getPrice();

}

break;

case 4:

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

sum += Math.round(order.records.get(i).getPrice() * 0.8);

} else if (time.getHour() == 20) {

if (time.getMinute() <= 30) {

sum += Math.round(order.records.get(i).getPrice() * 0.8);

}

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

sum += Math.round(order.records.get(i).getPrice() * 0.6);

} else if (time.getHour() == 14) {

if (time.getMinute() <= 30) {

sum += Math.round(order.records.get(i).getPrice() * 0.6);

}

} else {

sum += order.records.get(i).getPrice();

}

break;

case 5:

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

sum += Math.round(order.records.get(i).getPrice() * 0.8);

} else if (time.getHour() == 20) {

if (time.getMinute() <= 30) {

sum += Math.round(order.records.get(i).getPrice() * 0.8);

}

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

sum += Math.round(order.records.get(i).getPrice() * 0.6);

} else if (time.getHour() == 14) {

if (time.getMinute() <= 30) {

sum += Math.round(order.records.get(i).getPrice() * 0.6);

}

} else {

sum += order.records.get(i).getPrice();

}

break;

default:

sum += order.records.get(i).getPrice();

break;

}

}

}

}

}

类图如下:

该代码没有实现全部的功能,比如输入:

麻婆豆腐 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

3 油淋生菜 1 1

End

时,输出的结果是:

只能单独输出wrong format,而不能在中间判断是否输入错误而在中间输出wrong format以及错误的原因。

菜单计价程序-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"。

以上为菜单计价系列-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金额都要累加。

输出用户支付金额格式:

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

主函数:

public class Main {

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

Menu menu = new Menu();

// 读入菜单信息

Scanner sc = new Scanner(System.in);

String menuLine = sc.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("川菜", "Chuan");

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

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

}

};

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 = sc.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 = sc.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 = sc.nextLine();

continue;

}

if (!names.contains(name)) {

names.add(name);

}

if (table.getcaidan(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 = sc.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 = sc.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 = sc.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 = sc.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 = sc.nextLine();

}

sc.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);

}

}

}

}

类图如下:

这个题目我的得分并不高,只有66分,刚及格,都是输出方面有问题,比如输入:

麻婆豆腐 川菜 12 T

油淋生菜 9

麻辣鸡丝 10

table 1 : tom 13605054400 2023/5/1 12/30/00

1 麻婆豆腐 0 1 2

2 油淋生菜 1 1

3 麻婆豆腐 0 3 2

End

时,我的输出和要求输出:

不太明白问题所在,所以也不确定怎么改。

有一个很容易采坑的地方,就是错误的菜品记录和桌号记录,用户丢弃时输出的是:

wrong format

wrong format

而输入错误桌号记录时,输出的是:

wrong format

两个不同的输出不好同时实现。

期中考试:

继承与多态:

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

IMG_256

代码如下:

import java.util.Scanner;

abstract class Shape {

public abstract double getArea();

}

class Circle extends Shape {

private double radius;

public Circle(double radius) {

this.radius = radius;

}

@Override

public double getArea() {

return Math.PI * radius * radius;

}

}

class Point {

private double x;

private double y;

public Point(double x, double y) {

this.x = x;

this.y = y;

}

public double getX() {

return x;

}

public double getY() {

return y;

}

}

class Rectangle extends Shape {

private Point leftTopPoint;

private Point lowerRightPoint;

public Rectangle(Point leftTopPoint, Point lowerRightPoint) {

this.leftTopPoint = leftTopPoint;

this.lowerRightPoint = lowerRightPoint;

}

@Override

public double getArea() {

double width = Math.abs(lowerRightPoint.getX() - leftTopPoint.getX());

double height = Math.abs(leftTopPoint.getY() - lowerRightPoint.getY());

return width * height;

}

}

public class Main {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int choice = input.nextInt();

switch (choice) {

case 1: // Circle

double radius = input.nextDouble();

Shape circle = new Circle(radius);

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;

}

}

public static void printArea(Shape shape) {

System.out.printf("The area of the shape is: %.2f\n", shape.getArea());

}

}

抽象类与接口:

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

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

代码如下:

import java.util.ArrayList;

import java.util.Comparator;

import java.util.Scanner;

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

}

}

}

class Circle extends Shape {

private double a;

public Circle(double a) {

this.a = a;

}

public double getA() {

return a;

}

public void setA(double a) {

this.a = a;

}

@Override

public double getArea() {

return Math.PI*this.a*this.a;

}

}

class Rectangle extends Shape{

Point topLeftPoint = new Point();

Point lowerRightPoint = new Point();

public Rectangle(Point topLeftPoint, Point lowerRightPoint) {

this.topLeftPoint = topLeftPoint;

this.lowerRightPoint = lowerRightPoint;

}

public Rectangle() {

}

@Override

public double getArea() {

return Math.abs(getHeight()*getLength());

}

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;

}

double getLength(){

return getLowerRightPoint().getX()-getTopLeftPoint().getX();

}

double getHeight(){

return getTopLeftPoint().getY()-getLowerRightPoint().getY();

}

}

class Point{

double x;

double y;

public Point(double x, double y) {

this.x = x;

this.y = y;

}

public Point() {

}

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;

}

}

class Shape implements Comparable<Shape>{

public Shape() {

}

public double getArea(){

return 0;

};

@Override

public int compareTo(Shape shape) {

double s1,s2;

s1 = getArea();

s2 = shape.getArea();

if(s1>s2)

return 1;

else if(s1<s2)

return -1;

return 0;

}

}

这个代码也不能够全队,n很大或者很小的时候会运行超时。

  1. 主要困难以及改进建议

我认为我遇到的主要困难是对于代码的理解与设计不清,有时候完成了代码也没有报错,可以输入数据,但是输出要不然就报错,要不然就是输出的结果不正确,但是我又不清楚该怎么去修改,导致题目答案一直是部分正确或者全部错误,我认为这跟我没有掌握java代码的编写有很大关系。

对于改进,我认为我应该再多多练习编写代码,学会循序渐进的编写,多学习新的知识,不只是从课堂学习,更要从网上、书上去学习。对于老师的建议,我希望老师能够讲解一下pta中难度比较大的题目。

  1. 总结

通过这次的pta作业以及期中考试,我对Java语言的严谨性有了更深刻的认识,Java不像C语言,它的语法会更加严谨。这三次作业是循序渐进的,从简单一点的菜单计价程序-1开始,一点点的增加要求,增加类,增加属性等,将菜单计价程序一步步补充完善,但是难度对于我来说还是有点大,常常会没有思绪,不知道怎么下手。经过这几次的练习,让我看到自己代码方面的不足,日后要多加练习,不能松懈,争取下次能够更好的完成作业。

posted @ 2023-11-19 21:47  小鱼等雨停  阅读(13)  评论(0编辑  收藏  举报