第四次,第五次pta作业及期中考试总结
Java中的多态和接口是面向对象编程中非常重要的概念。让我来解释一下它们的含义和用法。
多态(Polymorphism)是指在面向对象编程中,同一个方法名可以根据对象的不同类型而具有不同的行为。具体来说,在Java中实现多态有两种方式:方法重写(Override)和方法重载(Overload)。
方法重写是指子类重新定义父类中已经存在的方法,方法签名(名称、参数类型和返回类型)必须与父类中的方法相同。通过方法重写,我们可以在子类中改变方法的具体实现,以适应子类的特定需求。当使用父类的引用指向子类对象时,调用重写的方法时将根据实际对象的类型来确定执行哪个版本的方法。
方法重载是指在一个类中可以定义多个同名但参数列表不同的方法。当调用这个方法时,编译器会根据实际传入的参数类型和数量来选择匹配的方法进行调用。方法重载使得代码更加灵活,可以根据不同的需求选择不同的方法。
接口(Interface)是一种抽象的数据类型,它定义了一组方法的规范,但没有具体的实现。在Java中,接口使用`interface`关键字进行声明。类可以实现一个或多个接口,通过实现接口中定义的方法来达到接口规范的要求。接口提供了一种多继承的机制,使得类可以在继承父类的同时实现多个接口,增强了代码的灵活性。
接口的好处是它可以实现类之间的松耦合关系,使得类的设计更加灵活和可扩展。通过接口,我们可以定义一组共享的方法,并且不同的类可以根据需要实现这些方法。这种设计方式使得代码更加模块化、可复用和可测试。
第四次pta作业
设计点菜计价程序,根据输入的信息,计算并输出总价格。
输入内容按先后顺序包括两部分:菜单、订单,最后以"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+英文空格+桌号+“:”+英文空格+当前桌的原始总价+英文空格+当前桌的计算折扣后总价
以下是代码
import java.util.Scanner;
import java.util.regex.Pattern;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Menu menu = new Menu();
Order order = new Order();
Table table = new Table();
for (int i = 0; i < table.order.length; i++) {
table.order[i] = new Order();
for (int i1 = 0; i1 < order.records.length; i1++) {
table.order[i].records[i1] = new Record();
table.order[i].s[i1] = new SpecialDish();
}
}
for (int i = 0; i < menu.dishes.length; i++) {
menu.dishes[i] = new Dish();
menu.dishes[i].name = " ";
}
int invalidFlag = 0, tablei = 0;
int tableFlag = 0;
int orderFlag1 = 0, orderFlag2 = 0;
int j = 0; //j用来看订单的序号
int stringFlag = 0;
int combineFlag = 0; //判断是否要合并桌
while (true) {
String signal = sc.nextLine();
if (signal.equals("end")) {
break;
}
String str[] = signal.split(" ");
//菜单的输入、订单的输入和删除序号
if (str.length == 2 || str.length == 4) {
//菜单的输入
if (str.length == 4) {
//此时是订单的输入
if (isInteger(str[0])) {
if (tableFlag == 1) {
j = 0;
tableFlag = 0;
}
if (tablei - 1 > -1) {
if(menu.searchDish(str[1]) !=null){
table.order[tablei - 1].records[j].d = menu.searchDish(str[1]);
table.order[tablei - 1].addARecord(Integer.parseInt(str[0]), str[1], Integer.parseInt(str[2]), Integer.parseInt(str[3]));
System.out.println(str[0] + " " + str[1] + " " + table.order[tablei - 1].records[j].getPrice());
}
else{
System.out.println(str[1] + " does not exist"); //记录错误菜品名
continue;
}
}
j++;
}
//特殊菜
if (signal.matches("(\\S+)( )(川菜|晋菜|浙菜)( )([^0])(\\d*)( )(T?)")) {
menu.addSpecial(true);
menu.addType(str[1]);
menu.addDish(str[0], Integer.parseInt(str[2]));
}
} else if (str[0].length() != 1) {
menu.addDish(str[0], Integer.parseInt(str[1]));
}
//删除
else {
if (tablei - 1 < 0) {
continue;
}
if (menu.findSpecial(str[0], table.order[tablei - 1])) {
SpecialDish s1 = table.order[tablei - 1].findSpecialDish(Integer.parseInt(str[0]));
table.order[tablei - 1].pretendDelRecordByOrderNum(Integer.parseInt(str[0]), s1);
int recordNumber = table.order[tablei - 1].findRecordByNumber(Integer.parseInt(str[0]));
table.order[tablei - 1].deleteTaste(menu, table.order[tablei - 1].records[recordNumber].d.name, table.order[tablei - 1].records[recordNumber].degree, table.order[tablei - 1].records[recordNumber].count);
int specialNumber = table.order[tablei - 1].findSpecialRecordByNumber(Integer.parseInt(str[0]));
table.order[tablei - 1].s[specialNumber].ifDelete = true;
} else {
table.order[tablei - 1].delARecordByOrderNum(Integer.parseInt(str[0]));
}
}
}
//订单的输入(给自己点菜, 并且此时只能是特殊菜)
else if (str.length == 5) {
//订单的输入
if (isInteger(str[0])) {
if (stringFlag == 1) {
continue;
}
if (stringFlag == 1) {
continue;
}
//点的菜在菜单上存在
if (menu.searchDish(str[1]) != null) {
if (tableFlag == 1) {
j = 0;
tableFlag = 0;
}
if (tablei - 1 > -1) {
if (table.order[tablei - 1].ifAddTaste(menu, str[1], Integer.parseInt(str[2]))) {
table.order[tablei - 1].s[j].addDish(menu, str[1], str[3], str[4]);
table.order[tablei - 1].s[j].num = Integer.parseInt(str[0]);
System.out.println(str[0] + " " + str[1] + " " + table.order[tablei - 1].s[j].searchPrice(str[1]));
table.order[tablei - 1].records[j].d = menu.searchDish(str[1]);
table.order[tablei - 1].addARecord(Integer.parseInt(str[0]), str[1], Integer.parseInt(str[3]), Integer.parseInt(str[4]));
//把口味的count添加到口味当中
table.order[tablei - 1].addTaste(menu, str[1], Integer.parseInt(str[2]), table.order[tablei - 1].records[j].count);
table.order[tablei - 1].records[j].degree = Integer.parseInt(str[2]);
table.order[tablei - 1].pretendToDelete(Integer.parseInt(str[0]));
j++;
} else {
table.order[tablei - 1].printTasteOutOf(menu, str[1], Integer.parseInt(str[2]));
}
}
}
//点的菜在菜单上不存在
else {
System.out.println(str[1] + " does not exist"); //记录错误菜品名
continue;
}
}
}
//代点菜(应该有大问题)
else if (str.length == 6) {
if (!isInteger(str[0])) {
System.out.println("wrong format");
}
//代点菜存在
if (menu.searchDish(str[2]) != null) {
if (tableFlag == 1) {
j = 0;
tableFlag = 0;
}
if (table.num[tablei - 1] != Integer.parseInt(str[0]) && table.ifExistTableNum(Integer.parseInt(str[0]))) {
if (menu.searchSpecial(str[2])) {
if (tablei - 1 > -1) {
table.order[tablei - 1].s[j].addDish(menu, str[2], str[4], str[5]);
table.order[tablei - 1].s[j].num = Integer.parseInt(str[1]);
System.out.println(str[1] + " table " + table.num[tablei - 1] + " pay for table " + str[0] + " " + Math.round(Integer.parseInt(str[5]) * menu.searchDish(str[2]).getPrice(Integer.parseInt(str[4]))));
table.order[tablei - 1].records[j].d = menu.searchDish(str[2]);
table.order[tablei - 1].addARecord(Integer.parseInt(str[1]), str[2], Integer.parseInt(str[4]), Integer.parseInt(str[5]));
table.order[tablei - 1].pretendToDelete(Integer.parseInt(str[1]));
int number = table.searchSameTableOrder(Integer.parseInt(str[0]));
if (number != -1) {
Dish d = menu.searchDish(str[2]);
if (d.type.equals("川菜")) {
table.order[number].spicyCount += Integer.parseInt(str[5]);
} else if (d.type.equals("晋菜")) {
table.order[number].acidCount += Integer.parseInt(str[5]);
} else if (d.type.equals("浙菜")) {
table.order[number].sweetCount += Integer.parseInt(str[5]);
}
table.order[number].addTaste(menu, str[2], Integer.parseInt(str[3]), Integer.parseInt(str[5]));
}
j++;
}
} else {
System.out.println(str[1] + " table " + table.num[tablei - 1] + " pay for table " + str[0] + " " + Math.round(Integer.parseInt(str[4]) * menu.searchDish(str[2]).getPrice(Integer.parseInt(str[3]))));
table.order[tablei - 1].records[j].d = menu.searchDish(str[2]);
j++;
table.order[tablei - 1].addARecord(Integer.parseInt(str[1]), str[2], Integer.parseInt(str[3]), Integer.parseInt(str[4]));
}
} else {
System.out.println("Table number :" + str[0] + " does not exist");
}
}
//代点菜不存在
else {
System.out.println(str[2] + " does not exist");
continue;
}
}
//输入桌号
else if (str.length == 7) {
if (str[0].equals("table")) {
if (!table.ifLegal(signal)) {
System.out.println("wrong format");
stringFlag = 1;
continue;
}
table.num[tablei] = Integer.parseInt(str[1]);
table.date[tablei] = str[5];
table.time[tablei] = str[6];
table.order[tablei].workFlag = 0;
if (!table.isValidDate(tablei)) {
System.out.println(str[1] + " date error");
stringFlag = 1;
continue;
}
if (!table.judgeDiscount(tablei)) {
System.out.println("wrong format");
stringFlag = 1;
continue;
}
if (table.order[tablei].workFlag != 0) {
System.out.println("table " + table.num[tablei] + " out of opening hours");
stringFlag = 1;
continue;
}
table.order[tablei].owner = str[3];
table.order[tablei].phoneNumber = str[4];
stringFlag = 0;
table.specialDiscount(tablei);
if (table.ifSameOwner(table.order[tablei].owner, tablei)) {
combineFlag = 1;
}
System.out.println("table " + table.num[tablei] + ": ");
table.order[tablei].tableNum = Integer.parseInt(str[1]);
tablei++;
tableFlag = 1;
} else {
System.out.println("wrong format");
}
} else {
System.out.println("wrong format");
stringFlag = 1;
continue;
}
}
int sum1 = 0, sum2 = 0;
for (int i = 0; i < tablei; i++) {
sum1 = 0;
sum2 = 0;
System.out.print("table " + table.num[i] + ": ");
table.order[i].getTotalPrice();
table.order[i].getTotalSpecialPrice();
sum1 = table.order[i].totalPrice; // 正常菜的价格
sum2 = table.order[i].specialTotalPrice; //特殊菜的价格
System.out.print(sum1 + sum2 + " ");
table.order[i].getAfterTotalPrice(menu);
table.order[i].getAfterSpecialTotalPrice();
int sum = table.order[i].afterDiscountTotalPrice + table.order[i].afterDiscountSpecialTotalPrice;
System.out.print(sum);
if (sum2 != 0) {
table.order[i].getTotalSpecialCount(menu);
table.order[i].getConcreteDegree();
int degreeFlag = 0;
System.out.print(" ");
if (table.order[i].spicyLength != 0) {
System.out.print("川菜" + " " + table.order[i].spicyCount + " " + table.order[i].spicyString);
degreeFlag = 1;
}
if (table.order[i].acidLength != 0) {
if (degreeFlag == 1) {
System.out.print(" ");
}
degreeFlag = 1;
System.out.print("晋菜" + " " + table.order[i].acidCount + " " + table.order[i].acidString);
}
if (table.order[i].sweetLength != 0) {
if (degreeFlag == 1) {
System.out.print(" ");
}
System.out.print("浙菜" + " " + table.order[i].sweetCount + " " + table.order[i].sweetString);
}
} else {
System.out.print(" ");
}
System.out.println();
}
UpsideDown u = new UpsideDown();
u.changeOrder(table);
UserInformation UI = new UserInformation();
for (int i = 0; i < tablei; i++) {
table.order[i].userPriceAddTo();
}
UI.combineUserInformation(table);
for (int i = 0; i < UI.orderLength; i++) {
System.out.println(UI.order[i].owner + " " + UI.order[i].phoneNumber + " " + UI.order[i].userPrice);
}
}
public static boolean isInteger(String str) {
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
return pattern.matcher(str).matches();
}
}
class UserInformation {
Order order[] = new Order[100];
int orderLength = 0;
public void outSetOrder() {
for (int i = 0; i < order.length; i++) {
order[i] = new Order();
order[i].owner = "";
}
}
public void combineUserInformation(Table table) {
outSetOrder();
for (int i = 0; i < table.order.length; i++) {
int flag = 0;
for (int i1 = 0; i1 < order.length; i1++) {
if (order[i1].owner.equals(table.order[i].owner)) {
flag = 1;
order[i1].userPrice += table.order[i].userPrice;
break;
}
}
if (flag == 0 && table.order[i].owner != null) {
order[orderLength++] = table.order[i];
}
}
}
}
class Dish { //对应菜谱上一道菜的信息。
String name; //菜品名称
int unit_price; //单价
boolean special; //是否是特殊菜
String type;//判断是川菜、浙菜、晋菜
public int getPrice(int portion) { //计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
double price = 0;
if (portion == 1) {
price = unit_price;
} else if (portion == 2) {
price = unit_price * 1.5;
} else if (portion == 3) {
price = unit_price * 2;
}
int price1 = (int) (run(price));
return price1;
}
double run(double num) {
double a = Math.signum(num); //判断是正数负数还是0,负数返回-1.0,正数返回1.0
if (a < 0.0)
return 0.0 - Math.round(Math.abs(num));
return Math.round(num);
}
}
class SpecialDish {
Dish d[] = new Dish[100];
int length = 0;
int totalSpecialPrice = 0;
int num = 0;
boolean ifDelete = false;
public void addDish(Menu m, String dishName, String portion, String count) {
d[length] = new Dish();
d[length].name = dishName;
d[length].unit_price = m.searchDish(dishName).getPrice(Integer.parseInt(portion)) * Integer.parseInt(count);
length++;
}
public int searchPrice(String dishName) {
for (int i = 0; i < d.length; i++) {
if (d[i].name.equals(dishName)) {
return d[i].unit_price;
}
}
return 0;
}
public int getSpecialPrice() {
int price = 0;
for (int i = 0; d[i] != null; i++) {
price += d[i].unit_price;
}
return price;
}
}
class Menu { //对应菜谱,包含饭店提供的所有菜的信息。
Dish[] dishes = new Dish[100];//菜品数组,保存所有菜品信息
int length = 0;
Dish searchDish(String dishName) {
for (int i = dishes.length - 1; i >= 0; i--) {
if (dishes[i].name.equals(dishName)) {
return dishes[i];
}
}
return null; //未找到
}
boolean searchSpecial(String dishName) {
if (searchDish(dishName).special) {
return true;
}
return false;
}
boolean findSpecial(String orderNum, Order o) {
for (int i = 0; i < o.records.length && o.records[i] != null; i++) {
if (o.records[i].orderNum == Integer.parseInt(orderNum)) {
if (searchSpecial(o.records[i].d.name)) {
return true;
}
}
}
return false;
}
void addDish(String dishName, int unit_price) {
dishes[length].name = dishName;
dishes[length].unit_price = unit_price;
length++;
}
void addType(String type){
dishes[length].type = type;
}
void addSpecial(boolean special) {
dishes[length].special = special;
}
}
class Order { //保存用户点的所有菜的信息
Record[] records = new Record[100]; //保存订单上每一道的记录
SpecialDish[] s = new SpecialDish[100]; //特殊菜
int tableNum = 0;
int length = 0;
double discount;
int workFlag = 0;
int totalPrice = 0;
int userPrice = 0; //输出人名时的价格
int specialTotalPrice = 0;
int afterDiscountTotalPrice = 0;
int afterDiscountSpecialTotalPrice = 0;
int delete[] = new int[100];
int deleteFlag = 0;
double specialDiscount;
int workOrRelaxFlag = 0; //周一到周五的中午为1,晚上为2,周日为3
int spicyDegree[] = new int[100];
int spicyDegreeCount[] = new int[100];
int spicyLength = 0;
int acidityDegree[] = new int[100];
int acidDegreeCount[] = new int[100];
int acidLength = 0;
int sweetnessDegree[] = new int[100];
int sweetDegreeCount[] = new int[100];
int sweetLength = 0;
int spicyAver = 0;
int acidAver = 0;
int sweetAver = 0;
String spicyString;
String acidString;
String sweetString;
String owner;
String phoneNumber;
int spicyCount = 0;
int acidCount = 0;
int sweetCount = 0;
public void printTasteOutOf(Menu menu, String dishName, int degree) {
Dish d = menu.searchDish(dishName);
if (d != null) {
if (d.type.equals("川菜")) {
if (degree > 5) {
System.out.println("spicy num out of range :" + degree);
}
} else if (d.type.equals("晋菜")) {
if (degree > 4) {
System.out.println("acidity num out of range :" + degree);
}
} else if (d.type.equals("浙菜")) {
if (degree > 3) {
System.out.println("sweetness num out of range :" + degree);
}
}
}
}
public boolean ifAddTaste(Menu menu, String dishName, int degree) {
Dish d = menu.searchDish(dishName);
if (d != null) {
if (d.type.equals("川菜")) {
if (degree <= 5) {
return true;
}
return false;
} else if (d.type.equals("晋菜")) {
if (degree <= 4) {
return true;
}
return false;
} else if (d.type.equals("浙菜")) {
if (degree <= 3) {
return true;
}
return false;
}
}
return false;
}
public boolean addTaste(Menu menu, String dishName, int degree, int count) {
Dish d = menu.searchDish(dishName);
if (d != null) {
if (d.type.equals("川菜")) {
if (addSpicy(degree, count)) {
return true;
}
return false;
} else if (d.type.equals("晋菜")) {
if (addAcid(degree, count)) {
return true;
}
return false;
} else if (d.type.equals("浙菜")) {
if (addSweet(degree, count)) {
return true;
}
return false;
}
}
return false;
}
public void getAverage() {
double s1 = 0, a = 0, s2 = 0;
for (int i = 0; i < spicyLength; i++) {
s1 += spicyDegree[i] * spicyDegreeCount[i];
}
for (int i = 0; i < acidLength; i++) {
a += acidityDegree[i] * acidDegreeCount[i];
}
for (int i = 0; i < sweetLength; i++) {
s2 += sweetnessDegree[i] * sweetDegreeCount[i];
}
spicyAver = (int) Math.round(s1 / spicyCount);
acidAver = (int) Math.round(a / acidCount);
sweetAver = (int) Math.round(s2 / sweetCount);
}
public void getConcreteDegree() {
getAverage();
if (spicyAver == 0) {
spicyString = "不辣";
} else if (spicyAver == 1) {
spicyString = "微辣";
} else if (spicyAver == 2) {
spicyString = "稍辣";
} else if (spicyAver == 3) {
spicyString = "辣";
} else if (spicyAver == 4) {
spicyString = "很辣";
} else if (spicyAver == 5) {
spicyString = "爆辣";
}
if (acidAver == 0) {
acidString = "不酸";
} else if (acidAver == 1) {
acidString = "微酸";
} else if (acidAver == 2) {
acidString = "稍酸";
} else if (acidAver == 3) {
acidString = "酸";
} else if (acidAver == 4) {
acidString = "很酸";
}
if (sweetAver == 0) {
sweetString = "不甜";
} else if (sweetAver == 1) {
sweetString = "微甜";
} else if (sweetAver == 2) {
sweetString = "稍甜";
} else if (sweetAver == 3) {
sweetString = "甜";
}
}
public boolean addSpicy(int spicDegree, int count) {
if (spicDegree <= 5) {
spicyDegree[spicyLength] = spicDegree;
spicyDegreeCount[spicyLength++] = count;
return true;
}
return false;
}
public boolean addAcid(int acidDegree, int count) {
if (acidDegree <= 4) {
acidityDegree[acidLength] = acidDegree;
acidDegreeCount[acidLength++] = count;
return true;
}
return false;
}
public boolean addSweet(int sweetDegree, int count) {
if (sweetDegree <= 3) {
sweetnessDegree[sweetLength] = sweetDegree;
sweetDegreeCount[sweetLength++] = count;
return true;
}
return false;
}
void getTotalPrice() { //计算订单的总价
for (Record record : records) {
totalPrice += record.getPrice();
}
}
public void getTotalSpecialPrice() {
for (SpecialDish specialDish : s) {
specialTotalPrice += specialDish.getSpecialPrice();
}
}
//添加一条菜品信息到订单中
void addARecord(int orderNum, String dishName, int portion, int count) {
records[length].d.name = dishName;
records[length].portion = portion;
records[length].count = count;
records[length].orderNum = orderNum;
length++;
}
void pretendToDelete(int orderNum) {
for (int i = 0; i < length; i++) {
if (records[i].orderNum == orderNum) {
totalPrice -= Math.toIntExact(Math.round(records[i].getPrice()));
}
}
}
//根据序号找到一个特殊菜
SpecialDish findSpecialDish(int Num) {
for (int i = 0; s[i] != null; i++) {
if (s[i].num == Num) {
return s[i];
}
}
return null;
}
void pretendDelRecordByOrderNum(int orderNum, SpecialDish s) {
int flag = 0;
for (int i = 0; i < deleteFlag; i++) {
if (delete[i] == orderNum) {
//System.out.println("deduplication " + orderNum);
return;
}
}
for (int i = 0; i < length; i++) {
if (records[i].orderNum == orderNum) {
flag = 1;
delete[deleteFlag++] = orderNum;
afterDiscountSpecialTotalPrice -= s.searchPrice(records[i].d.name);
specialTotalPrice -= s.searchPrice(records[i].d.name);
}
}
if (flag == 0) {
System.out.println("delete error;");
}
}
void delARecordByOrderNum(int orderNum) {
int flag = 0;
for (int i = 0; i < deleteFlag; i++) {
if (delete[i] == orderNum) {
System.out.println("deduplication " + orderNum);
return;
}
}
for (int i = 0; i < length; i++) {
if (records[i].orderNum == orderNum) {
flag = 1;
delete[deleteFlag++] = orderNum;
totalPrice -= Math.toIntExact(Math.round(records[i].getPrice()));
}
}
if (flag == 0) {
System.out.println("delete error;");
}
}
public void getTotalSpecialCount(Menu m) {
for (int i = 0; i < records.length; i++) {
Dish d = m.searchDish(records[i].d.name);
if (d != null && d.special) {
if (d.type.equals("川菜")) {
spicyCount += records[i].count;
} else if (d.type.equals("晋菜")) {
acidCount += records[i].count;
} else if (d.type.equals("浙菜")) {
sweetCount += records[i].count;
}
}
}
}
public void getAfterTotalPrice(Menu menu) {
for (int i = 0; i < records.length; i++) {
Dish d = menu.searchDish(records[i].d.name);
if (d != null && !d.special) {
afterDiscountTotalPrice += Math.round(records[i].getPrice() * discount);
}
}
if (afterDiscountTotalPrice < 0) {
afterDiscountTotalPrice = 0;
}
}
public void getAfterSpecialTotalPrice() {
for (SpecialDish specialDish : s) {
if (specialDish.ifDelete) {
afterDiscountSpecialTotalPrice += specialDish.getSpecialPrice();
} else {
afterDiscountSpecialTotalPrice += Math.round(specialDish.getSpecialPrice() * specialDiscount);
}
}
}
public void deleteTaste(Menu menu, String dishName, int degree, int count) {
Dish d = menu.searchDish(dishName);
if (d != null) {
if (d.type.equals("川菜")) {
for (int i = 0; i < spicyDegree.length; i++) {
if (spicyDegree[i] == degree && spicyDegreeCount[i] == count) {
spicyDegree[i] -= degree;
spicyDegreeCount[i] -= count;
spicyCount -= count;
spicyLength--;
break;
}
}
} else if (d.type.equals("晋菜")) {
for (int i = 0; i < acidityDegree.length; i++) {
if (acidityDegree[i] == degree && acidDegreeCount[i] == count) {
acidityDegree[i] -= degree;
acidDegreeCount[i] -= count;
acidCount -= count;
acidLength--;
break;
}
}
} else if (d.type.equals("浙菜")) {
for (int i = 0; i < sweetnessDegree.length; i++) {
if (sweetnessDegree[i] == degree && sweetDegreeCount[i] == count) {
sweetnessDegree[i] -= degree;
sweetDegreeCount[i] -= count;
sweetCount -= count;
sweetLength--;
break;
}
}
}
}
}
public int findRecordByNumber(int number) {
for (int i = 0; i < records.length; i++) {
if (records[i].orderNum == number) {
return i;
}
}
return -1;
}
public int findSpecialRecordByNumber(int number) {
for (int i = 0; i < s.length; i++) {
if (s[i].num == number) {
return i;
}
}
return -1;
}
public void userPriceAddTo() {
userPrice += afterDiscountTotalPrice + afterDiscountSpecialTotalPrice;
}
}
class Record { //保存订单上的一道菜品记录
Dish d = new Dish(); //菜品
int orderNum;
int count;//订单上的份数
int portion; //份额(1/2/3代表小/中/大份)
int degree; //口味的程度
int getPrice(){
return count * d.getPrice(portion);
}
}
class Table {
Order order[] = new Order[100];
int num[] = new int[100];//桌子的序号
String date[] = new String[100];
String time[] = new String[100];
public void specialDiscount(int i) {
String d[] = date[i].split("/");
LocalDate d1 = LocalDate.of(Integer.parseInt(d[0]), Integer.parseInt(d[1]), Integer.parseInt(d[2]));
int day = d1.getDayOfWeek().getValue();
if (day != 6 && day != 7) {
order[i].specialDiscount = 0.7;
} else {
order[i].specialDiscount = 1;
}
}
// boolean judgeIfOutYear(int i) {
// String d[] = date[i].split("/");
// LocalDate d1 = LocalDate.of(Integer.parseInt(d[0]), Integer.parseInt(d[1]), Integer.parseInt(d[2]));
// LocalDate startYear = LocalDate.of(2022, 1, 1);
// LocalDate endYear = LocalDate.of(2023, 12, 31);
// if (d1.isAfter(endYear) || d1.isBefore(startYear)) {
// return false;
// }
// return true;
// }
boolean judgeDiscount(int i) {
String d[] = date[i].split("/");
String t[] = time[i].split("/");
LocalDate d1 = LocalDate.of(Integer.parseInt(d[0]), Integer.parseInt(d[1]), Integer.parseInt(d[2]));
LocalTime d2 = LocalTime.of(Integer.parseInt(t[0]), Integer.parseInt(t[1]), Integer.parseInt(t[2]));
LocalTime dRelaxStart = LocalTime.of(9, 30);
LocalTime dRelaxEnd = LocalTime.of(21, 30);
LocalTime dWorkStart1 = LocalTime.of(10, 30);
LocalTime dWorkStart2 = LocalTime.of(17, 0);
LocalTime dWorkEnd1 = LocalTime.of(14, 30);
LocalTime dWorkEnd2 = LocalTime.of(20, 30);
int day = d1.getDayOfWeek().getValue();
if (day == 6 || day == 7) {
if (d2.isAfter(dRelaxEnd) || d2.isBefore(dRelaxStart)) {
order[i].workFlag = 1;
} else {
order[i].workOrRelaxFlag = 3;
order[i].discount = 1;
}
} else {
if ((d2.isAfter(dWorkStart1) && d2.isBefore(dWorkEnd1)) || (d2.until(dWorkStart1, ChronoUnit.SECONDS) == 0 || d2.until(dWorkEnd1, ChronoUnit.SECONDS) == 0)) {
order[i].discount = 0.6;
order[i].workOrRelaxFlag = 1;
} else if ((d2.isAfter(dWorkStart2) && d2.isBefore(dWorkEnd2)) || (d2.until(dWorkStart2, ChronoUnit.SECONDS) == 0 || d2.until(dWorkEnd2, ChronoUnit.SECONDS) == 0)) {
order[i].workOrRelaxFlag = 2;
order[i].discount = 0.8;
} else {
order[i].workFlag = 1;
}
}
String pattern = "([0-1]?[0-9]|2[0-3])/([0-5][0-9])/([0-5][0-9])";
String timeString = "";
char[] c = time[i].toCharArray();
for (int i1 = 0; i1 < c.length; i1++) {
timeString += c[i1];
}
if (timeString.matches(pattern)) {
return true;
} else {
return false;
}
}
boolean isValidDate(int i) {
String str[] = date[i].split("/");
// 接下来需要检查年月日是否都是数字,并且是否分别在合法的范围内
try {
int year = Integer.parseInt(str[0]);
int month = Integer.parseInt(str[1]);
int day = Integer.parseInt(str[2]);
if (year < 1 || month < 1 || month > 12) {
return false;
}
if (day < 1 || day > getDaysInMonth(year, month)) {
return false;
}
} catch (NumberFormatException e) {
return false;
}
// 如果以上条件都满足,则说明是一个合法的日期
return true;
}
/**
* 获取某个月份的天数
*
* @param year 年份
* @param month 月份(1~12)
* @return 指定月份的天数
*/
public static int getDaysInMonth(int year, int month) {
switch (month) {
case 2:
return isLeapYear(year) ? 29 : 28;
case 4:
case 6:
case 9:
case 11:
return 30;
default:
return 31;
}
}
/**
* 判断某一年是否是闰年
*
* @param year 年份
* @return 如果是闰年,则返回true,否则返回false
*/
public static boolean isLeapYear(int year) {
if (year % 4 == 0 && year % 100 != 0) {
return true;
} else if (year % 400 == 0) {
return true;
} else {
return false;
}
}
public boolean ifLegal(String str) {
if (str.matches("(table)( )([^0])(\\d*)( )(:)( )(\\S{1,10})( )(180|181|189|133|135|136)(\\d{8})( )(\\d{4})(/)([1-9]|0[1-9]|\\d{2})(/)(\\d{1,2})( )(\\d{1,2})(/)(\\d{1,2})(/)(\\d{1,2})")) {
return true;
}
return false;
}
public boolean ifSameDay(int tablei) {
String d1[] = date[tablei].split("/");
LocalDate day = LocalDate.of(Integer.parseInt(d1[0]), Integer.parseInt(d1[1]), Integer.parseInt(d1[2]));
for (int i1 = tablei - 1; i1 >= 0; i1--) {
String d[] = date[i1].split("/");
LocalDate d_ = LocalDate.of(Integer.parseInt(d[0]), Integer.parseInt(d[1]), Integer.parseInt(d[2]));
if (d_.getDayOfWeek().getValue() == day.getDayOfWeek().getValue()) {
return true;
}
}
return false;
}
public boolean ifSameTime(int tablei) {
if (!ifSameDay(tablei)) {
return false;
}
for (int i1 = tablei - 1; i1 >= 0; i1--) {
if (order[i1].workOrRelaxFlag == order[tablei].workOrRelaxFlag) {
return true;
}
}
return false;
}
public boolean ifExistTableNum(int tableNum) {
for (int i = 0; i < num.length; i++) {
if (num[i] == tableNum) {
return true;
}
}
return false;
}
public boolean ifSameOwner(String name, int tablei) {
for (int i = tablei - 1; i >= 0; i--) {
if (order[i].owner.equals(name)) {
return true;
}
}
return false;
}
public int searchSameTableOrder(int number){
for (int i = 0; i < order.length; i++) {
if(order[i].tableNum == number){
return i;
}
}
return -1;
}
}
class UpsideDown {
public void changeOrder(Table table) {
for (int i = 0; table.order[i].owner != null && i < table.order.length - 1; i++) {
for (int i1 = 0; table.order[i1].owner != null && i1 < table.order.length - 1 - i; i1++) {
if (table.order[i1 + 1].owner != null) {
if (table.order[i1].owner.compareTo(table.order[i1 + 1].owner) > 0) {
Order o;
o = table.order[i1];
table.order[i1] = table.order[i1 + 1];
table.order[i1 + 1] = o;
}
}
}
}
}
}
下面是需要注意的事项:
1. 输入内容按照先后顺序包括两部分:菜单和订单,以"end"结束。
2. 菜单由一条或多条菜品记录组成,每条记录占据一行。
3. 每条菜品记录包含菜名和基础价格两个信息。
4. 订单包括桌号标识、点菜记录和删除信息、代点菜信息。每类信息可以包含一条或多条记录,每条记录占据一行或多行。
5. 桌号标识独占一行,包含桌号和时间信息。
6. 桌号以下的所有记录都属于该桌,直到出现下一个桌号标识。
7. 点菜记录包含序号、菜名、份额和份数。份额可选项为1、2、3,分别代表小、中、大份。
8. 不同份额菜的价格计算方法:小份菜的价格=菜品的基础价格,中份菜的价格=菜品的基础价格乘以1.5,大份菜的价格=菜品的基础价格乘以2。如果计算结果为小数,按四舍五入处理。
9. 删除记录的格式为:序号 delete。删除对应序号的点菜记录。如果序号不存在,输出"delete error"。
10. 代点菜信息包含桌号、序号、菜品名称、份额和份数。代点菜是当前桌点另一桌的菜品,计算价格在当前桌上进行。
11. 程序最后按输入的桌号从小到大的顺序依次输出每桌的总价。每桌的总价等于该桌所有菜品价格之和乘以折扣,折扣按照营业时间进行计算。
12. 折扣计算方法:周一至周五晚上(17:00-20:30)8折,周一至周五中午(10:30-14:30)6折,周末全价(9:30-21:30)。
13. 如果下单时间不在营业时间范围内,输出"table <桌号> out of opening hours"。
14. 注意处理异常情况,如菜谱信息与订单信息混合、桌号时间格式非法、删除重复记录、代点菜桌号不存在、菜谱信息重复等。
下面是第五次pta作业
本题在菜单计价程序-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+英文空格+桌号+“:”+英文空格+当前桌的计算折扣后总价+英文空格+辣度平均值+英文空格+酸度平均值+英文空格+甜度平均值+英文空格
最后按拼音顺序输出每位客户(不考虑客户同名或拼音相同的情况)的支付金额,格式: 用户姓名+英文空格+手机号+英文空格+支付总金额,按输入顺序排列。
以下是代码
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<Table> tablemes = new ArrayList<>();
List<String> stringList = new ArrayList<>();
Menu menu = new Menu();
Scanner scanner = new Scanner(System.in);
Table currentTable = null;
while (true) {
String input = scanner.nextLine();
if (input.equals("end")) {
break;
}
String[] temp = input.split(" ");
if (input.endsWith("delete")) {
// 1 delete
if (!currentTable.order.delARecordByOrderNum(Integer.parseInt(temp[0]))) {
stringList.add("delete error;");
}
continue;
}
if (input.endsWith("T")) {
if (temp.length != 4) {
stringList.add("wrong format");
continue;
}
menu.addDish(temp[0], temp[1], Integer.parseInt(temp[2]));//添加新的菜到菜单里面
continue;
}
if (temp.length == 2) {
// 麻婆豆腐 12
menu.addDish(temp[0], "", Integer.parseInt(temp[1]));//添加新的菜到菜单里面
continue;
}
if (input.startsWith("table")) {
if (temp.length != 7) {
stringList.add("wrong format");
break;
}
if (temp[3].length() > 10) {
stringList.add("wrong format");
break;
}
if (!(temp[4].startsWith("180")
|| temp[4].startsWith("181")
|| temp[4].startsWith("189")
|| temp[4].startsWith("133")
|| temp[4].startsWith("135")
|| temp[4].startsWith("136"))) {
stringList.add("wrong format");
break;
}
if (temp[4].length() != 11) {
stringList.add("wrong format");
break;
}
currentTable = new Table(temp[1], temp[3], temp[4], temp[5], temp[6]);
currentTable.order.menu = menu;
currentTable.order.table = currentTable;
tablemes.add(currentTable);
stringList.add("table " + temp[1] + ": ");
continue;
}
if (temp.length == 4 || temp.length == 5) {
String dishName = temp[1];
Dish dish = menu.searthDish(dishName);
if (dish == null) {
stringList.add(dishName + " does not exist");
continue;
}
if (temp.length == 4) {
currentTable.order.addARecord(Integer.parseInt(temp[0]), dishName, 0, Integer.parseInt(temp[2]), Integer.parseInt(temp[3]), currentTable.name);
Record record = currentTable.order.records.get(currentTable.order.records.size() - 1);
stringList.add(record.orderNum + " " + dish.name + " " + record.getPrice());
continue;
} else {
String kind = dish.kind;
if (kind.equals("川菜") && (Integer.parseInt(temp[2]) > 5 || Integer.parseInt(temp[2]) < 0)) {
stringList.add("spicy num out of range :" + Integer.parseInt(temp[2]));
continue;
} else if (kind.equals("晋菜") && (Integer.parseInt(temp[2]) > 4 || Integer.parseInt(temp[2]) < 0)) {
stringList.add("acidity num out of range :" + Integer.parseInt(temp[2]));
continue;
} else if (kind.equals("浙菜") && (Integer.parseInt(temp[2]) > 3 || Integer.parseInt(temp[2]) < 0)) {
stringList.add("sweetness num out of range :" + Integer.parseInt(temp[2]));
continue;
}
}
currentTable.order.addARecord(Integer.parseInt(temp[0]), dishName, Integer.parseInt(temp[2]), Integer.parseInt(temp[3]), Integer.parseInt(temp[4]), currentTable.name);
Record record = currentTable.order.records.get(currentTable.order.records.size() - 1);
stringList.add(record.orderNum + " " + dish.name + " " + record.getPrice());
continue;
}
if (temp.length == 6) {
String dishName = temp[2];
Dish dish = menu.searthDish(dishName);
if (dish == null) {
stringList.add(dishName + " does not exist");
continue;
}
if (Integer.parseInt(temp[0]) > tablemes.size()) {
stringList.add(dishName + " does not exist");
continue;
}
Table table = tablemes.get(Integer.parseInt(temp[0]) - 1);
table.order.addARecord(Integer.parseInt(temp[1]), temp[2], Integer.parseInt(temp[3]), Integer.parseInt(temp[4]), Integer.parseInt(temp[5]), currentTable.name);
currentTable.order.addOtherARecord(Integer.parseInt(temp[1]), temp[2], Integer.parseInt(temp[3]), Integer.parseInt(temp[4]), Integer.parseInt(temp[5]), currentTable.name);
Record record = table.order.records.get(table.order.records.size() - 1);
stringList.add(record.orderNum + " table " + currentTable.tableNum + " pay for table " + temp[0] + " " + record.getPrice());
continue;
}
/*if (temp.length == 7) {
String dishName = temp[1];
Dish dish = menu.searthDish(dishName);
if (dish == null) {
stringList.add(dishName + " does not exist");
continue;
}
Table table = tablemes.get(Integer.parseInt(temp[0]));
table.order.addARecord(Integer.parseInt(temp[1]), temp[2],0, Integer.parseInt(temp[3]), Integer.parseInt(temp[4]));
Record record = currentTable.order.records.get(currentTable.order.records.size() - 1);
stringList.add(record.orderNum + " " + dish.name + " " + record.getPrice());
}*/
}
Map<String, Integer> costMap = new HashMap<>();
List<String> stringList1 = new ArrayList<>();
for (int i = 0; i < tablemes.size(); i++) {
if (tablemes.get(i).getDiscount() > 0f) {
Table table = tablemes.get(i);
String tableText = "table " + table.tableNum + ": "
+ (table.order.getOrignCommonTotalPrice() + table.order.getOrignSpeicalTotalPrice()) + " "
+ (table.order.getCommonTotalPrice() + table.order.getSpeicalTotalPrice());
if (table.getHotCount() > 0) {
tableText += " 川菜 " + table.getHotCount() + " " + table.getHotText();
}
if (table.getAcidCount() > 0) {
tableText += " 晋菜 " + table.getAcidCount() + " " + table.getAcidText();
}
if (table.getSweetCount() > 0) {
tableText += " 浙菜 " + table.getSweetCount() + " " + table.getSweetText();
}
stringList.add(tableText);
String key = table.name + " " + table.tel;
if (costMap.get(key) == null) {
costMap.put(key, (table.order.getCommonTotalPrice() + table.order.getSpeicalTotalPrice()));
stringList1.add(key);
} else {
costMap.put(key, costMap.get(key) + (table.order.getCommonTotalPrice() + table.order.getSpeicalTotalPrice()));
}
} else {
stringList.clear();
stringList.add("table " + tablemes.get(i).tableNum + " out of opening hours");
}
}
stringList1 = stringList1.stream().sorted(String::compareTo).collect(Collectors.toList());
for (String s : stringList1) {
stringList.add(s + " " + costMap.get(s));
}
for (String s : stringList) {
System.out.println(s);
}
}
}
class Dish {
String name;//菜品名称
int unit_price; //单价
String kind;// 口味
public Dish(String name, String kind, int unit_price) {
this.name = name;
this.kind = kind;
this.unit_price = unit_price;
}
//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
int getPrice(int portion) {
if (portion == 1) {
return unit_price;
}
if (portion == 2) {
return Math.round(this.unit_price * 1.5f);
}
return unit_price * 2;
}
}
class Menu {
// 菜品数组,保存所有菜品信息
List<Dish> dishs = new ArrayList<>();
//根据菜名在菜谱中查找菜品信息,返回Dish对象。
Dish searthDish(String dishName) {
for (Dish dish : dishs) {
if (dish.name.equals(dishName)) {
return dish;
}
}
return null;
}
//添加一道菜品信息
Dish addDish(String dishName, String taste, int unit_price) {
Dish dish = new Dish(dishName, taste, unit_price);
Dish existsDish = searthDish(dishName);
if (existsDish != null) {
dishs.remove(existsDish);
}
dishs.add(dish);
return dish;
}
}
class Order {
List<Record> records = new ArrayList();//保存订单上每一道的记录
List<Record> otherRecord = new ArrayList<>();
Menu menu;
Table table;
int getOrignCommonTotalPrice() {
int totalPrice = 0;
for (Record record : records) {
if ("".equals(record.d.kind) && record.name.equals(table.name)) {
totalPrice += record.getPrice();
}
}
for (Record record : otherRecord) {
if ("".equals(record.d.kind) && record.name.equals(table.name)) {
totalPrice += record.getPrice();
}
}
return totalPrice;
}
//计算订单的总价
int getCommonTotalPrice() {
int totalPrice = 0;
for (Record record : records) {
if ("".equals(record.d.kind) && record.name.equals(table.name)) {
totalPrice += Math.round(record.getPrice() * table.getDiscount());
}
}
for (Record record : otherRecord) {
if ("".equals(record.d.kind) && record.name.equals(table.name)) {
totalPrice += Math.round(record.getPrice() * table.getDiscount());
}
}
return totalPrice;
}
int getOrignSpeicalTotalPrice() {
int totalPrice = 0;
for (Record record : records) {
if (!"".equals(record.d.kind) && record.name.equals(table.name)) {
totalPrice += record.getPrice();
}
}
for (Record record : otherRecord) {
if (!"".equals(record.d.kind) && record.name.equals(table.name)) {
totalPrice += record.getPrice();
}
}
return totalPrice;
}
int getSpeicalTotalPrice() {
int totalPrice = 0;
for (Record record : records) {
if (!"".equals(record.d.kind) && record.name.equals(table.name)) {
totalPrice += Math.round(record.getPrice() * table.getSpecialDiscount());
}
}
for (Record record : otherRecord) {
if (!"".equals(record.d.kind) && record.name.equals(table.name)) {
totalPrice += Math.round(record.getPrice() * table.getSpecialDiscount());
}
}
return totalPrice;
}
/**
* 添加一条菜品信息到订单中。
*
* @param orderNum
* @param dishName
* @param tasteLevel
* @param portion
* @param num
* @return
*/
Record addARecord(int orderNum, String dishName, int tasteLevel, int portion, int num, String name) {
Dish dish = menu.searthDish(dishName);
if (dish == null) {
dish = new Dish(dishName + " does not exist", "", 0);
}
Record record = new Record(orderNum, dish, tasteLevel, portion, num, name);
records.add(record);
return record;
}
Record addOtherARecord(int orderNum, String dishName, int tasteLevel, int portion, int num, String name) {
Dish dish = menu.searthDish(dishName);
if (dish == null) {
dish = new Dish(dishName + " does not exist", "", 0);
}
Record record = new Record(orderNum, dish, tasteLevel, portion, num, name);
otherRecord.add(record);
return record;
}
//根据序号删除一条记录
boolean delARecordByOrderNum(int orderNum) {
Record record = findRecordByNum(orderNum);
return records.remove(record);
}
//根据序号查找一条记录
Record findRecordByNum(int orderNum) {
for (Record record : records) {
if (record.orderNum == orderNum) {
return record;
}
}
return null;
}
}
class Record {
/**
* 序号
*/
int orderNum;
/**
* 菜品
*/
Dish d;
/**
* 份额(1/2/3代表小/中/大份)
*/
int portion;
int tasteLevel;
/**
* 份数
*/
int num;
String name;
public Record(int orderNum, Dish d, int tasteLevel, int portion, int num, String name) {
this.orderNum = orderNum;
this.d = d;
this.tasteLevel = tasteLevel;
this.portion = portion;
this.num = num;
this.name = name;
}
/**
* 计价,计算本条记录的价格
*
* @return
*/
int getPrice() {
return d.getPrice(this.portion) * num;
}
}
class Table {
int tableNum;
String name;
String tel;
String tableDate;
String tableTime;
Order order;
String[] hotLevel = new String[]{"不辣", "微辣", "稍辣", "辣", "很辣", "爆辣"};
String[] acidLevel = new String[]{"不酸", "微酸", "稍酸", "酸", "很酸"};
String[] sweetLevel = new String[]{"不甜", "微甜", "稍甜", "甜"};
public Table() {
}
public Table(String tableNum, String name, String tel, String tableDate, String tableTime) {
this.tableNum = Integer.parseInt(tableNum);
this.name = name;
this.tel = tel;
this.tableDate = tableDate;
this.tableTime = tableTime;
this.order = new Order();
}
float getDiscount() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH/mm/ss");
Date dateTime = null;
try {
dateTime = simpleDateFormat.parse(tableDate + " " + tableTime);
} catch (ParseException e) {
throw new RuntimeException(e);
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateTime);
int week = calendar.get(Calendar.DAY_OF_WEEK);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
float discount = 0f;
if (week >= 1 && week <= 5) {
if (hour >= 17 && hour < 20)
discount = 0.8F;
else if (hour == 20 && minute < 30)
discount = 0.8F;
else if (hour == 20 && minute == 30 && second == 0)
discount = 0.8F;
else if (hour >= 11 && hour <= 13 || hour == 10 && minute >= 30)
discount = 0.6F;
else if (hour == 14 && minute < 30)
discount = 0.6F;
else if (hour == 14 && minute == 30 && second == 0)
discount = 0.6F;
} else {
if (hour >= 10 && hour <= 20)
discount = 1.0F;
else if (hour == 9 && minute >= 30)
discount = 1.0F;
else if (hour == 21 && minute < 30 || hour == 21 && minute == 30 && second == 0)
discount = 1.0F;
}
return discount;
}
float getSpecialDiscount() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH/mm/ss");
Date dateTime = null;
try {
dateTime = simpleDateFormat.parse(tableDate + " " + tableTime);
} catch (ParseException e) {
throw new RuntimeException(e);
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateTime);
int week = calendar.get(Calendar.DAY_OF_WEEK);
float discount = 0f;
if (week >= 1 && week <= 5) {
discount = 0.7f;
} else {
discount = 1.0f;
}
return discount;
}
int getHotAvg() {
int total = 0;
int count = 0;
for (Record record : this.order.records) {
if (record.d.kind.equals("川菜")) {
total += (record.tasteLevel * record.num);
count += record.num;
}
}
return Math.round((float) total / count);
}
String getHotText() {
int hotAvg = getHotAvg();
return hotLevel[hotAvg];
}
int getHotCount() {
int count = 0;
for (Record record : this.order.records) {
if (record.d.kind.equals("川菜")) {
count += record.num;
}
}
return count;
}
int getAcidAvg() {
int total = 0;
int count = 0;
for (Record record : this.order.records) {
if (record.d.kind.equals("晋菜")) {
total += (record.tasteLevel * record.num);
count += record.num;
}
}
return Math.round((float) total / count);
}
int getAcidCount() {
int count = 0;
for (Record record : this.order.records) {
if (record.d.kind.equals("晋菜")) {
count += record.num;
}
}
return count;
}
String getAcidText() {
int acidAvg = getAcidAvg();
return acidLevel[acidAvg];
}
int getSweetAvg() {
int total = 0;
int count = 0;
for (Record record : this.order.records) {
if (record.d.kind.equals("浙菜")) {
total += (record.tasteLevel * record.num);
count += record.num;
}
}
return Math.round((float) total / count);
}
int getSweetCount() {
int count = 0;
for (Record record : this.order.records) {
if (record.d.kind.equals("浙菜")) {
count += record.num;
}
}
return count;
}
String getSweetText() {
int sweetAvg = getSweetAvg();
return sweetLevel[sweetAvg];
}
}
需要注意以下几个方面:
1. 输入处理:确保正确解析输入内容,按照指定格式读取并分割每个记录的字段。
2. 菜谱管理:使用合适的数据结构(例如`HashMap`)存储菜谱中的菜品信息,以便快速查找菜品。
3. 删除记录:在订单中删除指定序号的记录时,需要确保序号存在于订单中。如果序号不存在,应输出相应的错误信息。
4. 代点菜信息:处理代点菜信息时,需要判断代点菜所属的桌号是否存在,以及被代点的菜品是否存在于菜谱中。
5. 价格计算和舍入:根据规定的规则计算菜品价格,包括根据份额调整价格和进行四舍五入。确保在价格计算过程中不丢失精度。
6. 营业时间和折扣:根据营业时间确定折扣,并对订单总价进行折扣计算。确保正确识别营业时间段,包括平时和周末的不同折扣规则。
7. 输出格式:按照指定格式输出每桌的订单记录和总价,确保输出的信息和格式正确。
在实现过程中,需要进行充分的输入验证和错误处理,以确保程序能够正确处理各种情况。例如,当输入的菜名在菜谱中不存在时,应输出相应的错误信息;当订单时间不在营业范围内时,也需要正确处理并输出错误信息。
为了确保程序的正确性和稳定性,可以编写单元测试来覆盖各种输入情况,并逐步验证程序的正确性。
期中考试第一题
创建一个圆形类(Circle),私有属性为圆的半径,从控制台输入圆的半径,输出圆的面积
输入格式:
输入圆的半径,取值范围为(0,+∞)
,输入数据非法,则程序输出Wrong Format
,注意:只考虑从控制台输入数值的情况
输出格式:
输出圆的面积(保留两位小数,可以使用String.format(“%.2f”,输出数值)控制精度)
以下是答案
import java.util.Scanner;
public class Main {
private double radius;
public Main(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double radius = scanner.nextDouble();
if (radius <= 0) {
System.out.println("Wrong Format");
} else {
Main circle = new Main (radius);
System.out.println(String.format("%.2f", circle.getArea()));
}
}
}
在实现圆形类(Circle)并计算圆的面积时,需要注意以下几个方面:
1. 输入验证:确保输入的半径是一个合法的数值。需要验证输入是否为一个有效的浮点数,并且要求半径的值大于0。
2. 异常处理:如果输入的半径不满足要求,即不是一个合法的浮点数或小于等于0,则应输出相应的错误信息"Wrong Format"。
3. 圆形类设计:创建一个圆形类(Circle),并添加一个私有属性来表示圆的半径。该类应提供一个公有方法来计算圆的面积。
4. 面积计算:根据输入的半径,使用正确的数学公式计算圆的面积。面积计算的结果需要保留两位小数,可以使用`String.format("%.2f", area)`来控制输出精度。
5. 输出结果:确保按照指定格式输出圆的面积,即输出一个保留两位小数的浮点数。
在实现过程中,要注意对输入进行适当的错误处理和异常处理,以确保程序能够正确处理各种情况。例如,当输入的半径不是一个有效的浮点数时,应输出相应的错误信息;当输入的半径小于等于0时,也需要正确处理并输出错误信息。
此外,可以考虑添加单元测试来验证圆形类的正确性和稳定性,覆盖不同的输入情况,并验证计算出的圆面积是否符合预期结果。
期中考试第二题
设计一个矩形类,其属性由矩形左上角坐标点(x1,y1)及右下角坐标点(x2,y2)组成,其中,坐标点属性包括该坐标点的X轴及Y轴的坐标值(实型数),求得该矩形的面积。类设计如下图:
输入格式:
分别输入两个坐标点的坐标值x1,y1,x2,y2。
输出格式:
输出该矩形的面积值
以下是代码
import java.util.Scanner;
abstract class Shape {
Shape(){};
abstract double getArea();
}
class Circle extends Shape1{
private double radius;
public Circle(double radius) {
this.radius = radius;
}
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 void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}
class Rectangle extends Shape{
private Point topLeftPoint;
private Point lowerRightPoint;
private double length;
private double height;
public Rectangle() {
}
public Rectangle(Point topLeftPoint, Point lowerRightPoint) {
this.topLeftPoint = topLeftPoint;
this.lowerRightPoint = lowerRightPoint;
this.length = Math.abs(topLeftPoint.getX() - lowerRightPoint.getX());
this.height = Math.abs(topLeftPoint.getY() - lowerRightPoint.getY());
}
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 length;
}
public void setLength(double length) {
this.length = length;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getArea() {
return length * height;
}
}
public class Circle {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double radius = scanner.nextDouble();
if (radius <= 0) {
System.out.println("Wrong Format");
} else {
Circle circle = new Circle(radius);
System.out.println(String.format("%.2f", circle.getArea()));
}
}
}
根据您提供的代码,我注意到以下几个问题和注意事项:
1. 类名冲突:在代码中存在两个类名相同的类,一个是 `Circle` 类,另一个是 `Shape` 类的子类 `Circle`。为了避免冲突,您可以考虑将其中一个类进行重命名。
2. 继承关系:在 `Circle` 类的定义中,它应该是继承自抽象类 `Shape`,而不是 `Shape1`。您需要将 `Shape1` 改为 `Shape`。
3. 类的导入:请确保导入了正确的类。例如,`Circle` 类中使用了 `Scanner` 类,需要确保在文件开头使用 `import java.util.Scanner;` 导入该类。
4. 输入验证:在 `main` 方法中,对输入的半径进行验证时,只判断了半径是否小于等于0。根据题目要求,半径的取值范围应为大于0的实数,即 (0,+∞)。您可以添加条件来判断输入的半径是否合法,并在不合法时输出适当的错误提示信息。
5. 输出格式化:在输出圆的面积时,使用了 `String.format("%.2f", circle.getArea())` 来控制输出的小数位数为两位。这种格式化输出是正确的,可以保留两位小数。
综上所述,您需要注意类名冲突、继承关系、类的导入、输入验证和输出格式化这几个方面。通过修复这些问题,您的代码将能够正确地计算圆的面积并输出。
总结一下,多态通过方法重写和方法重载实现同一方法名多种行为,提高了代码的灵活性和可扩展性。而接口定义了一组方法的规范,通过实现接口来达到接口规范的要求,实现了类之间的松耦合关系。Java中的多态和接口是面向对象编程中非常重要的概念,能够帮助我们写出更加灵活和可维护的代码。