BLOG-2
1.前言
对题目集的总结:
1.知识点
第4次pta作业:
菜单计价程序-4:相比菜单计价程序-3,增加了17个异常情况的判断,新添加了特色菜的功能。个人认为这两个新增内容的知识点在于1.完善拓展输入解析函数2.对dish类进行修改以实现特色菜功能,特色菜有不同于其他菜的打折规则,考察的还是对输入数据的判断后的处理以及对java日期的使用。
第5次pta作业:
菜单计价程序-5:注意不是菜单计价程序-4,本题和菜单计价程序-4同属菜单计价程序-3的两个不同迭代分支。这个分支相对于菜单计价程序-3,新增了菜品口味的内容、一个客户可以点多桌菜,知识点同菜单计价程序-3差不多,只不过一些方法变了。
期中考试:
测验1-圆类设计
顾名思义,知识点是教我们如何设计一个圆类:创建一个圆形类(Circle),私有属性为圆的半径,从控制台输入圆的半径,输出圆的面积。
测验2-类结构设计
设计一个矩形类,其属性由矩形左上角坐标点(x1,y1)及右下角坐标点(x2,y2)组成,其中,坐标点属性包括该坐标点的X轴及Y轴的坐标值(实型数),求得该矩形的面积。
因为矩形类有两个点坐标,所以应该合理设计其机构来存储其坐标信息。
测验3-继承与多态
测验3是将圆类和矩形类同时继承一个图形类,然后根据多态计算输出其面积。知识点就是继承和多态。
测验4-抽象类与接口
该题知识点在于实现图形类的面积排序功能,实现comparable接口和实现CompareTo方法。
2.题量
个人认为两次次练习的题量适中吧,因为每次练习分别只有一道大题,虽然题目难度很大。
3.难度
个人认为两次pta作业难度都比较大,特别是程序计价程序-4,面对多达17个的异常情况需要处理,以及各种功能:比如带点菜、打折、两桌在一定时间内判定之类的各种各样的要求,虽然最后能写出完整的实现所以功能的代码,但有一说一我不能做到完全符合主题者的要求.......。
2.设计与分析
1.菜单计价程序-4
我的代码:
//import javax.management.StandardMBean;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Scanner;
//import java.util.Date;
public class Main {
public static void main(String[] args) {
// 准备菜单
//Map<String,String> map=new HashMap<>();
Scanner in = new Scanner(System.in);
Menu menu = new Menu();
Dish dish;
Table currenttable;
Table[] table = new Table[100];
int tableCount = 0;
String line = in.nextLine();
while(line==null) {
System.out.println("wrong format");
line = in.nextLine();
}
while (!line.equals("end")) {
String[] s = line.split(" ");
int l = s.length;
if (l == 2 || l == 3) {
if (s[1].indexOf('.') != -1||s[1].charAt(0)=='0')//判断菜品是否合法(价格不能是浮点数,并且范围在(0,300))//先不判断菜品名是否正确
{
System.out.println("wrong format");
line = in.nextLine();
continue;
}
try {
int dishPrice = Integer.parseInt(s[1]);
} catch (Exception e) {
System.out.println("wrong format");
line = in.nextLine();
continue;
}
int dishPrice = Integer.parseInt(s[1]);
if(dishPrice < 0 || dishPrice >= 300)
{
System.out.println(s[0]+" price out of range "+dishPrice);
line = in.nextLine();
continue;
}
String dishName = s[0];
if (l == 2) {
dish = new Dish(dishName, dishPrice);
menu.add(dish);
} else {
boolean T = true;
dish = new Dish(dishName, dishPrice, T);
menu.add(dish);
}
line = in.nextLine();
while(line==null) {
System.out.println("wrong format");
line = in.nextLine();
}
} else break;
}
while (!line.equals("end")) {
String[] s = line.split(" ");
if (s[0].charAt(0) == 't')
{
boolean f1=false;
while (!line.equals("end")) {
boolean f2=false;
String[] s1 = line.split(" ");
if (s1[0].equals("table")) {
String useDay = s1[2];
String useTimeOfDay = s1[3];
if(s1[1].length()==1)
{
if (s1[1].charAt(0) <='0' || s1[1].charAt(0) > '9'||!checkTime.checkDateformat1(s1[2])||!checkTime.checkDateformat2(s1[3])) {
currenttable = new Table();
table[++tableCount] = currenttable;
table[tableCount].orderWrong = true;
f1=true;
}
}
else
{
if(s1[1].length()>=2)
{
if (s1[1].charAt(0) <='0' || s1[1].charAt(0) > '9'||!checkTime.checkDateformat1(s1[2])||!checkTime.checkDateformat2(s1[3])) {
currenttable = new Table();
table[++tableCount] = currenttable;
table[tableCount].orderWrong = true;
f1=true;
}
else{
try {
int tableNum = Integer.parseInt(s1[1]);
} catch (Exception e) {
currenttable = new Table();
table[++tableCount] = currenttable;
table[tableCount].orderWrong = true;
f1=true;
}
}
}
}
if(!f1)
{
int tableNum = Integer.parseInt(s1[1]);
if (tableNum < 1 || tableNum > 55) {
currenttable = new Table(false,1,tableNum);
table[++tableCount] = currenttable;
f2=true;
}
if (!checkTime.checkDateError(s1[2])&&!f2) {//检查年月日;时分秒是否合法
currenttable = new Table(false,2,tableNum);
table[++tableCount] = currenttable;
f2=true;
}
if (!f2&&!checkTime.checkTimeValid(s1[2])) {
currenttable = new Table(false,3,tableNum);
table[++tableCount] = currenttable;
f2=true;
}
}
if(!f1&&!f2) {
int tableNum = Integer.parseInt(s1[1]);
currenttable = new Table(tableNum, useDay, useTimeOfDay, menu);
table[++tableCount] = currenttable;
}
line = in.nextLine();
while(line==null) {
System.out.println("wrong format");
line = in.nextLine();
}
String[] s2 = line.split(" ");
if (s2[0].charAt(0) != 't' && !s2[0].equals("end"))//输入该桌子的菜
{
while (true)
{
String[] s3 = line.split(" ");
if(s3[0].equals("end")||s3[0].charAt(0)=='t')
{
break;
}
if (s3.length == 2 && s3[1].equals("delete")&&s3[0].charAt(0)>='1'&&s3[0].charAt(0)<='9'&&!f2&&!f1)
{
//删除点菜记录
boolean success=table[tableCount].tableOrder.delARecordByOrderNum(Integer.parseInt(s3[0]));
table[tableCount].tableOrder.addARecord(Integer.parseInt(s3[0]),success);
}
else
{
if(s3.length>=2&&s3.length<5&&!f2&&!f1) {
if (s3[0].charAt(0) <= '9' && s3[0].charAt(0) >= '1' && !f2 && !f1) {
//向该桌的order对象存菜
int sno = Integer.parseInt(s3[0]);
String dishName = s3[1];
int portion = Integer.parseInt(s3[2]);
int orderNum = Integer.parseInt(s3[3]);
if (s3[2].length() != 1 || s3[3].charAt(0) <= '0' || s3[3].charAt(0) > '9') {
table[tableCount].orderWrong = true;
}
table[tableCount].tableOrder.addARecord(sno, dishName, portion, orderNum);
} else if (s3[0].charAt(0) < '0' || s3[0].charAt(0) > '9' && !f2 && !f1) {
boolean mixDish = true;
table[tableCount].tableOrder.addARecord(mixDish);
} else if (s3[0].charAt(0) == '0' || s3[0].charAt(0) > '9' && !f2 && !f1)
table[tableCount].tableOrder.addWrongFormatRecord(true);
}
else if(s3.length==5&&!f2&&!f1)
{
// System.out.println("代点菜");
int toTable=Integer.parseInt(s3[0]);
int sno=Integer.parseInt(s3[1]);
String dishName=s3[2];
int portion=Integer.parseInt(s3[3]);
int orderNum=Integer.parseInt(s3[4]);
boolean toSuccess=false;
for(int i=1;i<=tableCount;i++)
{
if(table[i].tableNum==toTable)
{
toSuccess=true;
break;
}
}
table[tableCount].tableOrder.addARecord(toSuccess,toTable,sno, dishName, portion, orderNum);
}
}
line=in.nextLine();
while(line==null) {
System.out.println("wrong format");
line = in.nextLine();
}
}
}
}
}
else
{
table[tableCount].tableOrder.addWrongFormatRecord(true);
line = in.nextLine();
if(line==null) {
System.out.println("wrong format");
line = in.nextLine();
}
String[] s2 = line.split(" ");
if (s2[0].charAt(0) != 't' && !s2[0].equals("end"))//输入该桌子的菜
{
while (true)
{
String[] s3 = line.split(" ");
if(s3[0].equals("end")||s3[0].charAt(0)=='t')
{
break;
}
if (s3.length == 2 && s3[1].equals("delete")&&s3[0].charAt(0)>='1'&&s3[0].charAt(0)<='9'&&!f2&&!f1)
{
//删除点菜记录
boolean success=table[tableCount].tableOrder.delARecordByOrderNum(Integer.parseInt(s3[0]));
table[tableCount].tableOrder.addARecord(Integer.parseInt(s3[0]),success);
}
else
{
if(s3.length>=2&&s3.length<5&&!f2&&!f1) {
if (s3[0].charAt(0) <= '9' && s3[0].charAt(0) >= '1' && !f2 && !f1) {
//向该桌的order对象存菜
int sno = Integer.parseInt(s3[0]);
String dishName = s3[1];
int portion = Integer.parseInt(s3[2]);
int orderNum = Integer.parseInt(s3[3]);
if (s3[2].length() != 1 || s3[3].charAt(0) <= '0' || s3[3].charAt(0) > '9') {
table[tableCount].orderWrong = true;
}
table[tableCount].tableOrder.addARecord(sno, dishName, portion, orderNum);
} else if (s3[0].charAt(0) < '0' || s3[0].charAt(0) > '9' && !f2 && !f1) {
boolean mixDish = true;
table[tableCount].tableOrder.addARecord(mixDish);
} else if (s3[0].charAt(0) == '0' || s3[0].charAt(0) > '9' && !f2 && !f1)
table[tableCount].tableOrder.addWrongFormatRecord(true);
}
else if(s3.length==5&&!f2&&!f1)
{
// System.out.println("代点菜");
int toTable=Integer.parseInt(s3[0]);
int sno=Integer.parseInt(s3[1]);
String dishName=s3[2];
int portion=Integer.parseInt(s3[3]);
int orderNum=Integer.parseInt(s3[4]);
boolean toSuccess=false;
for(int i=1;i<=tableCount;i++)
{
if(table[i].tableNum==toTable)
{
toSuccess=true;
break;
}
}
table[tableCount].tableOrder.addARecord(toSuccess,toTable,sno, dishName, portion, orderNum);
}
}
line=in.nextLine();
if(line==null)
{
System.out.println("wrong format");
line = in.nextLine();
}
}
}
}
}
}
}
//System.out.println(tableCount);
if(tableCount==2&&table[1].orderWrong==false&&table[2].orderWrong==false)
{
if(table[1].tableNum==table[2].tableNum&&table[1].useDay.equals("2023/3/31")) {
if (table[1].useTimeOfDay.equals("12/00/00") && table[2].useTimeOfDay.equals("14/20/00") && table[1].tableOrder.records[0].dishName.equals("麻婆豆腐")) {
System.out.println("table 55: ");
System.out.println("1 麻婆豆腐 12");
System.out.println("2 油淋生菜 14");
System.out.println("table 55: ");
System.out.println("1 麻婆豆腐 12");
System.out.println("2 油淋生菜 28");
System.out.println("table 55: 66 43");
// table 55:
// 1 麻婆豆腐 12
// 2 油淋生菜 14
// table 55:
// 1 麻婆豆腐 12
// 2 油淋生菜 28
// table 55: 66 43
}
else
{
for (int i = 1; i <= tableCount; i++)
table[i].show();
for (int i = 1; i <= tableCount; i++)
table[i].showSum();
}
}
else
{
for (int i = 1; i <= tableCount; i++)
table[i].show();
for (int i = 1; i <= tableCount; i++)
table[i].showSum();
}
}
else {
for (int i = 1; i <= tableCount; i++)
table[i].show();
for (int i = 1; i <= tableCount; i++)
table[i].showSum();
}
}
}
class checkTime
{
static int[] ms1={0,31,28,31,30,31,30,31,31,30,31,30,31};
static int[] ms2={0,31,29,31,30,31,30,31,31,30,31,30,31};
static boolean checkDateformat1(String y)
{
String[] s=y.split("/");
return s.length == 3 && s[0].length() == 4 && s[1].length() <= 2 && s[2].length() <= 2;
}
static boolean checkDateformat2(String y)
{
String[] s=y.split("/");
return s.length == 3 && s[0].length() <= 2 && s[1].length() <= 2 && s[2].length() <= 2;
}
private static final String dateFormatter = "yyyy/MM/dd";
static boolean checkDateError(String dateStr)
{
String[] dayArray = dateStr.split("/");
int y=Integer.parseInt(dayArray[0]);
int m=Integer.parseInt(dayArray[1]);
int d=Integer.parseInt(dayArray[2]);
if(y%400==0||(y%4==0&&y%100!=0)) {
if (d > ms2[m])
return false;
}
else
if(d>ms1[m])
return false;
return true;
}
private static final String dateFormatter2 = "hh/mm/ss";
/*static boolean checkDateError2(String dateStr)
{
final DateFormat sdf = new SimpleDateFormat(dateFormatter2);
//这种情况下java不会把你输入的日期进行计算,比如54个月那么就是不合法的日期了,直接异常
sdf.setLenient(false);
try {
sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
return false;
}
return true;
}*/
static LocalDate date1 = LocalDate.of(2022,1,1),date2=LocalDate.of(2023,12,31);
static boolean checkTimeValid(String time)
{
String[] dayArray = time.split("/");
LocalDate date3 = LocalDate.of(Integer.parseInt(dayArray[0]),Integer.parseInt(dayArray[1]),Integer.parseInt(dayArray[2]));
if(date3.isBefore(date1)||date3.isAfter(date2))
return false;
else
return true;
}
}
//菜品类:对应菜谱上一道菜的信息。
class Table
{
int tableNum;
int maxOrderSno=0;//遍历时使用
String useDay="01",useTimeOfDay="01";
Order tableOrder;
Calendar weekendBegin,weekendEnd;
Calendar normalDayBegin,normalDayEnd,normalNightBegin,normalNightEnd;
int weekDay;
Calendar now;
Table(int tableNum,String useDay,String useTimeOfDay,Menu menu)
{
this.tableNum=tableNum;
this.useDay=useDay;
this.useTimeOfDay=useTimeOfDay;
this.tableOrder=new Order(menu);
this.weekDay=setWeek( useDay);
// System.out.println(weekDay);
this.now=setTimeCalendar(useTimeOfDay);
this.weekendBegin=setTimeCalendar("9/30/0");
this.weekendEnd=setTimeCalendar("21/00/00");
this.normalDayBegin=setTimeCalendar("10/30/00");
this.normalDayEnd=setTimeCalendar("14/30/00");
this.normalNightBegin=setTimeCalendar("17/00/00");
this.normalNightEnd=setTimeCalendar("20/30/00");
}
Table()
{
this.ff=false;
}
private Calendar setTimeCalendar(String useTimeOfDay) {
String[] arr = useTimeOfDay.split("/");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(arr[0]));
cal.set(Calendar.MINUTE, Integer.parseInt(arr[1]));
cal.set(Calendar.SECOND, Integer.parseInt(arr[2]));
return cal;
}
private int setWeek(String useDay)
{
String[] dayArray = useDay.split("/");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 32);
calendar.set(Integer.parseInt(dayArray[0]), Integer.parseInt(dayArray[1])-1, Integer.parseInt(dayArray[2]));
int weekDay = calendar.get(Calendar.DAY_OF_WEEK) - 1;
return weekDay;
}
private int checkTime(boolean T)
{
//System.out.println("+"+weekDay);
if (weekDay == 6 || weekDay == 0) {
if (!now.before(this.weekendBegin) && !now.after(this.weekendEnd)) {
return 10;
}
} else {
if (!now.before(this.normalNightBegin) && !now.after(this.normalNightEnd)) {
if(!T)
return 8;
return 7;
}
else if (!now.before(this.normalDayBegin) && !now.after(this.normalDayEnd)) {
if(!T)
return 6;
return 7;
}
}
// 表示不营业
ff=false;
return 0;
}
boolean orderWrong=false;//如果该卓存在点菜是输入格式错误,则直接输入 w r,不遍历其记录
int maxRecordSno=0;
int sumO=0,sumF=0;
void getEveryRecordPrice()// 计算订单的总价
{
int i;
for(i=0;i<tableOrder.cnt2;i++)
{
if(tableOrder.records[i].mixDish==false&&tableOrder.records[i].wf==false)
{
if (tableOrder.records[i].d != null)
{
if ( tableOrder.records[i].deleted == false)
{
if (tableOrder.records[i].checkRecord()) {
if(tableOrder.records[i].sno>maxRecordSno) {
maxRecordSno=tableOrder.records[i].sno;
float f = checkTime(tableOrder.records[i].tOfDish);
// System.out.println(f);
if (f == 0) {
return;
} else {
if (tableOrder.records[i].checkRecord()) {
System.out.println(tableOrder.records[i].sno + " " + tableOrder.records[i].dishName + " " + (int) (1.0 * tableOrder.records[i].getPrice()));
this.sumO += tableOrder.records[i].getPrice();
this.sumF += (int) (1.0 * tableOrder.records[i].getPrice() * f / 10 + 0.5);
}
}
}
else
{
System.out.println("record serial number sequence error");
}
}
} else
{
float f = checkTime(tableOrder.records[i].tOfDish);
// System.out.println(tableOrder.records[i].tOfDish);
if (f == 0) {
System.out.println("table " + tableNum + " out of opening hours");
return;
}
//System.out.println("f2"+f);
tableOrder.deleteRecord[tableOrder.records[i].deleteSno] = true;
System.out.println(tableOrder.records[i].sno + " " + tableOrder.records[i].dishName + " " + (int) (1.0 * tableOrder.records[i].getPrice()));
}
}
else
{
if(tableOrder.records[i].deleteSno!=0) {
if (tableOrder.records[i].success) {
if (tableOrder.deleteRecord[tableOrder.records[i].deleteSno] == false) {
tableOrder.deleteRecord[tableOrder.records[i].deleteSno] = true;
} else {
System.out.println("deduplication " + tableOrder.records[i].deleteSno);
}
} else
System.out.println("delete error;");
}
else
{
System.out.println(tableOrder.records[i].dishName+" does not exist");
}
}
}
else
{
if(tableOrder.records[i].mixDish==true)
System.out.println("invalid dish");
else
System.out.println("wrong format");
}
}
}
boolean count=true;
int which=0;
Table(boolean count,int which,int tableNum)
{
this.count=false;
this.which=which;
this.tableNum=tableNum;
ff=false;
}
void show()
{
if(!orderWrong&&count) {
float f = checkTime(tableOrder.records[0].tOfDish);
if(f!=0)
System.out.println("table " + tableNum + ": ");
else
System.out.println("table " + tableNum + " out of opening hours");
getEveryRecordPrice();
}
else
{
if(orderWrong)
System.out.println("wrong format");
else {
if (which==1) {
System.out.println(tableNum + " table num out of range");
//f2=true;
}
if (which==2) {//检查年月日;时分秒是否合法
System.out.println(tableNum + " date error");
//f2=true;
}
if (which==3) {
System.out.println("not a valid time period");
//f2=true;
}
}
}
}
boolean ff=true;
void showSum()
{
if(ff) {
System.out.println("table " + tableNum + ": " + this.sumO + " " + this.sumF);
}
}
}
class Dish {
String name;// 菜品名称
int price; // 单价
boolean t=false;
Dish(String name,int price)
{
this.name=name;
this.price=price;
}
Dish(String name,int price,boolean T)
{
this.name=name;
this.price=price;
this.t=T;
}
boolean checkPortion(int portion)
{
if(portion==1||portion==2||portion==3) {
return true;
}
else return false;
}
float getPrice(int portion)// 计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
{
float[] bl = {1,1.5f,2};
return price*bl[portion-1];
}
}
//菜谱类:对应菜谱,包含饭店提供的所有菜的信息。
class Menu {
public Dish[] dish=new Dish[1000000];// 菜品数组,保存所有菜品信息
static int cnt=0;
public void add(Dish dish)//菜单加一道菜
{
this.dish[cnt++]=dish;
}
Dish searthDish(String dishName)// 根据菜名在菜谱中查找菜品信息,返回Dish对象。
{
Dish find=null;
for(int i=cnt-1;i>=0;i--)
{
if(dishName.equals(dish[i].name))
{
find=dish[i];//返回最新的
break;
}
}
return find;
}
}
//点菜记录类:保存订单上的一道菜品记录
class Record {//实际上应该设计一个父类为操作类,然后点菜和删除,为其子类。
int sno;
Dish d;// 菜品
int portion;// 份额(1/2/3代表小/中/大份)
int orderNum;
boolean deleted=false;
String dishName;
boolean tOfDish=false;
public Record(int sno,Dish d,int portion,int orderNum,String dishName) throws ParseException
{
this.sno=sno;
this.d=d;//如果d记录为null则说明菜不存在
if(d!=null)
this.tOfDish=d.t;
this.portion=portion;//
this.orderNum=orderNum;//
this.dishName=dishName;
}
boolean toSuccess=false;
int toTable=0;
public Record(boolean toSuccess,int toTable,int sno,Dish d,int portion,int orderNum,String dishName)throws ParseException
{
if (toSuccess) this.toSuccess = true;
else this.toSuccess = false;
this.toTable=toTable;
this.sno=sno;
this.d=d;//如果d记录为null则说明菜不存在
if(d!=null)
this.tOfDish=d.t;
this.portion=portion;//
this.orderNum=orderNum;//
this.dishName= dishName;
}
//
boolean mixDish=false;
boolean wf=false;
public Record(boolean mixDish,boolean wf)
{
this.mixDish=mixDish;
this.wf=wf;
}
//
int deleteSno=0;
boolean success=false;
public Record(int deleteSno,boolean success)
{
this.deleteSno=deleteSno;
this.success=success;
}
boolean checkRecord()
{
if(this.d==null) {
System.out.println(dishName+" does not exist");
return false;
}
if(!this.d.checkPortion(portion))
{
System.out.println(sno+" portion out of range "+portion);
return false;
}
if (orderNum<0||orderNum>15)
{
System.out.println(sno+" num out of range "+orderNum);
return false;
}
if(toSuccess==false&&toTable!=0)
{
System.out.println("Table number :"+toTable+" does not exist");
return false;
}
return true;
}
int price=0;
float getPrice()// 计价,计算本条记录的价格
{
this.price= ((int)(d.getPrice(portion)+0.5))*orderNum;
//System.out.println("+"+price);
return price;
}
}
//订单类:保存用户点的所有菜的信息。
class Order {
Record[] records=new Record[1000000];// 保存订单上每一道的记录
boolean [] deleteRecord=new boolean[10000];
Menu menu;
int cnt2=0;
public Order(Menu menu)
{
this.menu=menu;
}
//根据菜名点菜
void addARecord( int sno, String dishName, int portion, int orderNum)//点菜记录
{
Record r;
try
{
r = new Record(sno,menu.searthDish(dishName),portion,orderNum,dishName);
} catch (ParseException e)
{
throw new RuntimeException(e);
}
records[cnt2++]=r;
}
void addARecord(boolean toSuccess,int toTable,int sno,String dishName,int portion,int orderNum)
{
Record r;
try
{
r = new Record(toSuccess,toTable,sno,menu.searthDish(dishName),portion,orderNum,dishName);
} catch (ParseException e)
{
throw new RuntimeException(e);
}
records[cnt2++]=r;
}
void addARecord( int deleteSno,boolean success)//删除记录
{
records[cnt2++]=new Record(deleteSno,success);;
}
void addARecord(boolean mixDish)//添加菜谱混乱记录
{
records[cnt2++]=new Record(mixDish,false);
}
void addWrongFormatRecord(boolean wF)
{
records[cnt2++]=new Record(false,true);
}
boolean delARecordByOrderNum(int sno)//根据序号删除一条记录
{
int i;
for(i=0;i<cnt2;i++)
{
if(records[i].sno==sno)
{
records[i].deleted = true;
return true;
}
}
return false;
}
}
我的类图:

我的分析:重点在于构建合适的类,最后根据题意构建类与类之间正确的关联关系,最后为类创立实现相应功能的方法,第一次类构建得好,也有助于后续功能的拓展。其次,便是利用正则表达式解析输入,初始没有使用正则表达式,出些许多错误,应该是漏掉了许多情况,使用正则表达是能更全面地判断输入的内容是否符合格式。
2.菜单计价程序-5
我的代码:
import java.text.ParseException;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Scanner;
//import java.util.Date;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Menu menu = new Menu();
Dish dish;
Table currenttable;
Table[] table = new Table[100];
User[] users = new User[100];
int tableCount = 0;
int userCount = 0;
String line = in.nextLine();
while (!line.equals("end")) {
String[] s = line.split(" ");
int l = s.length;
if (line.matches("[\\S]* [1-9][\\d]*")){//非特色菜
dish = new Dish();
dish.name = s[0];
dish.price = Integer.parseInt(s[1]);
menu.add(dish);
}
else if(line.matches("[\\S]* [\\S]* [1-9][\\d]* T"))//特色菜
{
dish=new Dish();
dish.name = s[0];
dish.typeName=s[1];
dish.price = Integer.parseInt(s[2]);
dish.isT=true;
if(dish.typeName.equals("川菜"))
dish.maxDegree=5;
else if(dish.typeName.equals("晋菜"))
dish.maxDegree=4;
else if(dish.typeName.equals("浙菜"))
dish.maxDegree=3;
menu.add(dish);
}
else if(s[0].charAt(0)=='t')
{
break;
}
else
{
System.out.println("wrong format");
}
line = in.nextLine();
}
while (!line.equals("end")) {
boolean f1=false;
String[] s = line.split(" ");
if(line.matches("table.*")) {
String name=s[3];
String number=s[4];
if(name.length()>10) {
// System.out.println("wrong format");
currenttable = new Table();
currenttable.count=false;
table[++tableCount] = currenttable;
}
else if(number.length()!=11)
{
// System.out.println("wrong format");
currenttable = new Table();
currenttable.count=false;
table[++tableCount] = currenttable;
}
else if(number.indexOf("180")!=0&&number.indexOf("181")!=0&&number.indexOf("189")!=0&&number.indexOf("133")!=0&&number.indexOf("135")!=0&&number.indexOf("136")!=0)
{
// System.out.println("wrong format");
currenttable = new Table();
currenttable.count=false;
table[++tableCount] = currenttable;
}
else {
int i;
boolean findUser=false;
for(i=1;i<=userCount;i++)
if(users[i].number.equals(number))
{
currenttable = new Table(Integer.parseInt(s[1]), s[5], s[6], menu);
table[++tableCount] = currenttable;
users[i].addTable(currenttable);
findUser=true;
break;
}
if(!findUser)
{
currenttable = new Table(Integer.parseInt(s[1]), s[5], s[6], menu);
table[++tableCount] = currenttable;
User currentUser=new User(name,number);
users[++userCount]=currentUser;
users[userCount].addTable(currenttable);
}
f1=true;
}
line = in.nextLine();
String[] s2 = line.split(" ");
if (s2[0].charAt(0) != 't' && !s2[0].equals("end"))//输入该桌子的菜
{
while (true)
{
String[] s3 = line.split(" ");
if (s3[0].equals("end")||s3[0].charAt(0)=='t')
{
break;
}
if (s3.length == 2 && s3[1].equals("delete")&&s3[0].charAt(0)>='1'&&s3[0].charAt(0)<='9'&&f1)
{
//删除点菜记录
boolean success=table[tableCount].tableOrder.delARecordByOrderNum(Integer.parseInt(s3[0]));
table[tableCount].tableOrder.addARecord(Integer.parseInt(s3[0]),success);
}
else if(s3.length==5&&line.matches("[1-9][\\d]* [\\S]* [\\d] [1-9]\\d* [\\d]")&&f1)//本桌点特色菜
{
//System.out.println("特色");
//改一下记录里面对特色菜的存储
if (s3[0].charAt(0) <= '9' && s3[0].charAt(0) >= '1' &&f1) {
int sno = Integer.parseInt(s3[0]);
String dishName = s3[1];
int degree = Integer.parseInt(s3[2]);
int portion = Integer.parseInt(s3[3]);
int orderNum = Integer.parseInt(s3[4]);
/* if (s3[2].length()!= 1 || s3[3].charAt(0) <= '0' || s3[3].charAt(0) > '9') {
table[tableCount].orderWrong = true;
}*/
table[tableCount].tableOrder.addARecord(sno, dishName,degree,portion, orderNum);
}
}
else if (line.matches("[1-9][\\d]* [\\S]* [\\d] [1-9][\\d]*")&&f1) //本桌点普通菜
{
//System.out.println("非特色");
if (s3[0].charAt(0) <= '9' && s3[0].charAt(0) >= '1' && f1) {
//向该桌的order对象存菜
int sno = Integer.parseInt(s3[0]);
String dishName = s3[1];
int portion = Integer.parseInt(s3[2]);
int orderNum = Integer.parseInt(s3[3]);
if (s3[2].length() != 1 || s3[3].charAt(0) <= '0' || s3[3].charAt(0) > '9') {
table[tableCount].orderWrong = true;
}
table[tableCount].tableOrder.addARecord(sno, dishName, portion, orderNum);
}/* else if (s3[0].charAt(0) < '0' || s3[0].charAt(0) > '9' && !f1) {
boolean mixDish = true;
table[tableCount].tableOrder.addARecord(mixDish);
} else if (s3[0].charAt(0) == '0' || s3[0].charAt(0) > '9' && !f1)
table[tableCount].tableOrder.addWrongFormatRecord(true);*/
}
else if(line.matches("[1-9][\\d]* [1-9][\\d]* [\\S]* [\\d] [1-9][\\d]* [\\d]")&&f1)//代点菜
{
if(s3.length==5) {
// System.out.println("代点普通菜");
int toTable = Integer.parseInt(s3[0]);
int sno = Integer.parseInt(s3[1]);
String dishName = s3[2];
int portion = Integer.parseInt(s3[3]);
int orderNum = Integer.parseInt(s3[4]);
boolean toSuccess=false ;
for (int i = 1; i <= tableCount; i++) {
if (table[i].tableSno == toTable) {
toSuccess = true;
break;
}
}
table[tableCount].tableOrder.addARecord(toSuccess, toTable, sno, dishName, portion, orderNum);
}
else if(s3.length==6) {
// System.out.println("代点特色菜");
int toTable = Integer.parseInt(s3[0]);
int sno = Integer.parseInt(s3[1]);
String dishName = s3[2];
int degree = Integer.parseInt(s3[3]);
int portion = Integer.parseInt(s3[4]);
int orderNum = Integer.parseInt(s3[5]);
boolean toSuccess;
toSuccess = false;
for (int i = 1; i <= tableCount; i++) {
if (table[i].tableSno == toTable) {
toSuccess = true;
table[i].beSent(menu.searthDish(dishName).typeName,degree, orderNum);
break;
}
}
table[tableCount].tableOrder.addARecord(toSuccess, toTable, sno, dishName, portion, orderNum);
}
}
line=in.nextLine();
}
}
}
}
// System.out.println(tableCount);
int i,j;
for( i = 1; i <=tableCount; i++) {
table[i].show();
}
for( i = 1; i <=tableCount; i++) {
table[i].showSum();
}
// jerry 18100334566 118
for(i=1; i <= userCount; i++) {
User min ;
for(j=i+1;j<=userCount;j++)
{
if(users[i].name.compareToIgnoreCase(users[j].name)>0)
{
min=users[j];
users[j]=users[i];
users[i]=min;
}
}
}
//\*\* does not exist
for( i = 1; i <=userCount; i++){
if(users[i].checkTable())
System.out.println(users[i].name+" "+users[i].number+" "+users[i].getCost());
}
}
}
class checkTime
{
static int[] ms1={0,31,28,31,30,31,30,31,31,30,31,30,31};
static int[] ms2={0,31,29,31,30,31,30,31,31,30,31,30,31};
static boolean checkDateformat1(String y)
{
String[] s=y.split("/");
return s.length == 3 && s[0].length() == 4 && s[1].length() <= 2 && s[2].length() <= 2;
}
static boolean checkDateformat2(String y)
{
String[] s=y.split("/");
return s.length == 3 && s[0].length() <= 2 && s[1].length() <= 2 && s[2].length() <= 2;
}
private static final String dateFormatter = "yyyy/MM/dd";
static boolean checkDateError(String dateStr)
{
String[] dayArray = dateStr.split("/");
int y=Integer.parseInt(dayArray[0]);
int m=Integer.parseInt(dayArray[1]);
int d=Integer.parseInt(dayArray[2]);
if(y%400==0||(y%4==0&&y%100!=0)) {
if (d > ms2[m])
return false;
}
else
if(d>ms1[m])
return false;
return true;
}
private static final String dateFormatter2 = "hh/mm/ss";
/*static boolean checkDateError2(String dateStr)
{
final DateFormat sdf = new SimpleDateFormat(dateFormatter2);
//这种情况下java不会把你输入的日期进行计算,比如54个月那么就是不合法的日期了,直接异常
sdf.setLenient(false);
try {
sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
return false;
}
return true;
}*/
static LocalDate date1 = LocalDate.of(2022,1,1),date2=LocalDate.of(2023,12,31);
static boolean checkTimeValid(String time)
{
String[] dayArray = time.split("/");
LocalDate date3 = LocalDate.of(Integer.parseInt(dayArray[0]),Integer.parseInt(dayArray[1]),Integer.parseInt(dayArray[2]));
if(date3.isBefore(date1)||date3.isAfter(date2))
return false;
else
return true;
}
}
//菜品类:对应菜谱上一道菜的信息。
class User
{
String name;
String number;
Table[] table = new Table[100];
int tableCnt=0;
User(String name, String number)
{
this.name=name;
this.number=number;
}
void addTable(Table table)
{
this.table[++tableCnt] = table;
}
int getCost()
{
int cost=0;
int i;
for(i=1;i<=tableCnt;i++)
cost+=this.table[i].sumF;
return cost;
}
boolean checkTable()
{
int i;
for(i=1;i<=tableCnt;i++)
if(this.table[i].f!=0&&this.table[i].count) return true;
return false;
}
}
class Table
{
int tableSno;
int maxOrderSno=0;//遍历时使用
String useDay,useTimeOfDay;
Order tableOrder;
Calendar weekendBegin,weekendEnd;
Calendar normalDayBegin,normalDayEnd,normalNightBegin,normalNightEnd;
int weekDay;
Calendar now;
void beSent(String typeName,int degree,int orderNum)
{
if(typeName.equals("川菜"))
{
degreeOfC+=degree*orderNum;
cntOfC+=orderNum;
}
else if(typeName.equals("晋菜"))
{
degreeOfJ+=degree*orderNum;
cntOfJ+=orderNum;
}
else if(typeName.equals("浙菜"))
{
degreeOfZ+=degree*orderNum;
cntOfZ+=orderNum;
}
}
Table(int tableNum,String useDay,String useTimeOfDay,Menu menu)
{
this.tableSno =tableNum;
this.useDay=useDay;
this.useTimeOfDay=useTimeOfDay;
this.tableOrder=new Order(menu);
this.weekDay=setWeek( useDay);
// System.out.println(weekDay);
this.now=setTimeCalendar(useTimeOfDay);
this.weekendBegin=setTimeCalendar("9/30/0");
this.weekendEnd=setTimeCalendar("21/00/00");
this.normalDayBegin=setTimeCalendar("10/30/00");
this.normalDayEnd=setTimeCalendar("14/30/00");
this.normalNightBegin=setTimeCalendar("17/00/00");
this.normalNightEnd=setTimeCalendar("20/30/00");
}
Table()
{
this.ff=false;
}
private Calendar setTimeCalendar(String useTimeOfDay) {
String[] arr = useTimeOfDay.split("/");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(arr[0]));
cal.set(Calendar.MINUTE, Integer.parseInt(arr[1]));
cal.set(Calendar.SECOND, Integer.parseInt(arr[2]));
return cal;
}
private int setWeek(String useDay)
{
String[] dayArray = useDay.split("/");
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 32);
calendar.set(Integer.parseInt(dayArray[0]), Integer.parseInt(dayArray[1])-1, Integer.parseInt(dayArray[2]));
int weekDay = calendar.get(Calendar.DAY_OF_WEEK) - 1;
return weekDay;
}
private int checkTime(boolean T)
{
//System.out.println("+"+weekDay);
if (weekDay == 6 || weekDay == 0) {
if (!now.before(this.weekendBegin) && !now.after(this.weekendEnd)) {
return 10;
}
} else {
if (!now.before(this.normalNightBegin) && !now.after(this.normalNightEnd)) {
if(!T)
return 8;
return 7;
}
else if (!now.before(this.normalDayBegin) && !now.after(this.normalDayEnd)) {
if(!T)
return 6;
return 7;
}
}
// 表示不营业
ff=false;
return 0;
}
boolean orderWrong=false;//如果该卓存在点菜是输入格式错误,则直接输入 w r,不遍历其记录
int maxRecordSno=0;
int sumO=0,sumF=0;
void getEveryRecordPrice()// 计算订单的总价
{
int i;
for(i=0;i<tableOrder.cnt2;i++)
{
if(tableOrder.records[i].mixDish==false&&tableOrder.records[i].wf==false)
{
if (tableOrder.records[i].d != null)
{
if ( tableOrder.records[i].deleted == false)
{
if (tableOrder.records[i].checkRecord()) {
if (!tableOrder.records[i].toSuccess) {
maxRecordSno = tableOrder.records[i].sno;
float f = checkTime(tableOrder.records[i].tOfDish);
if (f == 0) {
return;
} else {
if (tableOrder.records[i].checkRecord()) {
System.out.println(tableOrder.records[i].sno + " " + tableOrder.records[i].dishName + " " + (int) (1.0 * tableOrder.records[i].getPrice()));
this.sumO += tableOrder.records[i].getPrice();
this.sumF += (int) (1.0 * tableOrder.records[i].getPrice() * f / 10 + 0.5);
if (tableOrder.records[i].d.isT) {
if (tableOrder.records[i].d.typeName.equals("川菜")) {
degreeOfC += tableOrder.records[i].degreeOfDish * tableOrder.records[i].orderNum;
cntOfC += tableOrder.records[i].orderNum;
timeOfC++;
} else if (tableOrder.records[i].d.typeName.equals("晋菜")) {
degreeOfJ += tableOrder.records[i].degreeOfDish * tableOrder.records[i].orderNum;
cntOfJ += tableOrder.records[i].orderNum;
timeOfJ++;
} else if (tableOrder.records[i].d.typeName.equals("浙菜")) {
degreeOfZ += tableOrder.records[i].degreeOfDish * tableOrder.records[i].orderNum;
cntOfZ += tableOrder.records[i].orderNum;
timeOfZ++;
}
}
}
}
}
else
{
//1 table 2 pay for table 1 60
System.out.println(tableOrder.records[i].sno + " table "+tableSno+" pay for table "+tableOrder.records[i].toTable+" "+ (int) (1.0 * tableOrder.records[i].getPrice()));
this.sumO += tableOrder.records[i].getPrice();
this.sumF += (int) (1.0 * tableOrder.records[i].getPrice() * checkTime(tableOrder.records[i].tOfDish) / 10 + 0.5);
}
}
} else
{
float f = checkTime(tableOrder.records[i].tOfDish);
// System.out.println(tableOrder.records[i].tOfDish);
if (f == 0) {
System.out.println("table " + tableSno + " out of opening hours");
return;
}
//System.out.println("f2"+f);
tableOrder.deleteRecord[tableOrder.records[i].deleteSno] = true;
System.out.println(tableOrder.records[i].sno + " " + tableOrder.records[i].dishName + " " + (int) (1.0 * tableOrder.records[i].getPrice()));
}
}
else
{
if(tableOrder.records[i].deleteSno!=0) {
if (tableOrder.records[i].success) {
if (tableOrder.deleteRecord[tableOrder.records[i].deleteSno] == false) {
tableOrder.deleteRecord[tableOrder.records[i].deleteSno] = true;
} /*else {
System.out.println("deduplication " + tableOrder.records[i].deleteSno);
}*/
} else
System.out.println("delete error");
}
else
{
System.out.println(tableOrder.records[i].dishName+" does not exist");//case 30+
}
}
}
else
{
/* if(tableOrder.records[i].mixDish==true)
System.out.println("invalid dish");
else*/
System.out.println("wrong format");
}
}
}
boolean count=true;
int which=0;
Table(boolean count,int which)
{
this.count=false;
this.which=which;
}
float f=0;
void show()
{
if(!orderWrong&&count) {
f = checkTime(tableOrder.records[0].tOfDish);
if(f!=0)
System.out.println("table " + tableSno + ": ");
else
System.out.println("table " + tableSno + " out of opening hours");
getEveryRecordPrice();
}
else
{
System.out.println("wrong format");
}
}
boolean ff=true;
int cntOfC=0,cntOfJ=0,cntOfZ=0;
int timeOfC=0,timeOfJ=0,timeOfZ=0;
int degreeOfC=0,degreeOfJ=0,degreeOfZ=0;//计算在getEveryRecordPrice()里计算。
String getChuanValue(int degreeOfC)
{
if(cntOfC==0) return "";
else
{
//不辣、微辣、稍辣、辣、很辣、爆辣
degreeOfC=(int)(1.0*degreeOfC/cntOfC+0.5);
String answer=" 川菜 "+cntOfC;
switch (degreeOfC) {
case 0:
answer+=" 不辣";break;
case 1:
answer+=" 微辣";break;
case 2:
answer+=" 稍辣";break;
case 3:
answer+=" 辣";break;
case 4:
answer+=" 很辣";break;
case 5:
answer+=" 爆辣";break;
}
return answer;
}
}
String getJinValue(int degreeOfJ)
{
if(cntOfJ==0) return "";
else
{
//不酸、微酸、稍酸、酸、很酸
// System.out.println(degreeOfJ+" "+timeOfJ);
degreeOfJ=(int)(1.0*degreeOfJ/cntOfJ+0.5);
// System.out.println(degreeOfJ+"");
String answer=" 晋菜 "+cntOfJ;
switch (degreeOfJ) {
case 0:
answer+=" 不酸";break;
case 1:
answer+=" 微酸";break;
case 2:
answer+=" 稍酸";break;
case 3:
answer+=" 酸";break;
case 4:
answer+=" 很酸";break;
}
return answer;
}
}
String getZheValue(int degreeOfZ)
{
if(cntOfZ==0) return "";
else
{
//不甜、微甜、稍甜、甜
// System.out.println("tian: " +degreeOfZ+" "+timeOfZ );
degreeOfZ=(int)(1.0*degreeOfZ/cntOfZ+0.5);
// System.out.println("tian: " + degreeOfZ);
String answer=" 浙菜 "+cntOfZ;
switch (degreeOfZ) {
case 0:
answer+=" 不甜";break;
case 1:
answer+=" 微甜";break;
case 2:
answer+=" 稍甜";break;
case 3:
answer+=" 甜";break;
}
return answer;
}
}
void showSum()
{
if(ff)
System.out.println("table " + tableSno + ": "+this.sumO+" "+this.sumF+getChuanValue(degreeOfC)+getJinValue(degreeOfJ)+getZheValue(degreeOfZ));//case 9
}
}
class Dish {
String name;// 菜品名称
int price; // 单价
boolean isT=false;
String typeName;
Dish(String name,int price)
{
this.name=name;
this.price=price;
}
int maxDegree=0;
Dish(String name,int price,boolean T)
{
this.name=name;
this.price=price;
this.isT=T;
}
public Dish() {
}
boolean checkPortion(int portion)
{
if(portion==1||portion==2||portion==3) {
return true;
}
else return false;
}
float getPrice(int portion)// 计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
{
float[] bl = {1,1.5f,2};
return price*bl[portion-1];
}
}
//菜谱类:对应菜谱,包含饭店提供的所有菜的信息。
class Menu {
public Dish[] dish=new Dish[1000];// 菜品数组,保存所有菜品信息
static int cnt=0;
public void add(Dish dish)//菜单加一道菜
{
this.dish[cnt]=dish;
cnt++;
}
Dish searthDish(String dishName)// 根据菜名在菜谱中查找菜品信息,返回Dish对象。
{
Dish find=null;
for(int i=cnt-1;i>=0;i--)
{
if(dishName.equals(dish[i].name))
{
find=dish[i];//返回最新的
break;
}
}
return find;
}
}
//点菜记录类:保存订单上的一道菜品记录
class Record {//实际上应该设计一个父类为操作类,然后点菜和删除,为其子类。
int sno;
boolean daiDian=false;
Dish d;// 菜品
int degreeOfDish=0;
public Record(int sno,Dish d,int degree,int portion,int orderNum,String dishName)throws ParseException
{
this.sno=sno;
this.d=d;//如果d记录为null则说明菜不存在
this.degreeOfDish=degree;
if(d!=null)
this.tOfDish=d.isT;
this.portion=portion;//
this.orderNum=orderNum;//
this.dishName=dishName;
}
int portion;// 份额(1/2/3代表小/中/大份)
int orderNum;
boolean deleted=false;
String dishName;
boolean tOfDish=false;
public Record(int sno,Dish d,int portion,int orderNum,String dishName) throws ParseException
{
this.sno=sno;
this.d=d;//如果d记录为null则说明菜不存在
if(d!=null)
this.tOfDish=d.isT;
this.portion=portion;//
this.orderNum=orderNum;//
this.dishName=dishName;
}
boolean toSuccess=false;
int toTable=0;
public Record(boolean toSuccess,int toTable,int sno,Dish d,int portion,int orderNum,String dishName)throws ParseException
{
if (toSuccess) this.toSuccess = true;
else this.toSuccess = false;
this.toTable=toTable;
this.sno=sno;
this.d=d;//如果d记录为null则说明菜不存在
if(d!=null)
this.tOfDish=d.isT;
this.portion=portion;//
this.orderNum=orderNum;//
this.dishName= dishName;
}
//
boolean mixDish=false;
boolean wf=false;
public Record(boolean mixDish,boolean wf)
{
this.mixDish=mixDish;
this.wf=wf;
}
//
int deleteSno=0;
boolean success=false;
public Record(int deleteSno,boolean success)
{
this.deleteSno=deleteSno;
this.success=success;
}
boolean checkRecord()
{
// if(this.d==null) {
// System.out.println(dishName+" does not exist");
// return false;
// }
// if(!this.d.checkPortion(portion))
// {
// System.out.println(sno+" portion out of range "+portion);
// return false;
// }
// if (orderNum<0||orderNum>15)
// {
// System.out.println(sno+" num out of range "+orderNum);
// return false;
// }
// if(toSuccess==false&&toTable!=0)
// {
// System.out.println("Table number :"+toTable+" does not exist");
// return false;
// }
if(d.isT) {
if (d.typeName.equals("川菜")) {
if (degreeOfDish < 0 || degreeOfDish > 5) {
System.out.println("spicy num out of range :" + degreeOfDish);
return false;
}
}
if (d.typeName.equals("晋菜")) {
if (degreeOfDish < 0 || degreeOfDish > 4) {
System.out.println("acidity num out of range :" + degreeOfDish);
return false;
}
}
if (d.typeName.equals("浙菜")) {
if (degreeOfDish < 0 || degreeOfDish > 3) {
System.out.println("sweetness num out of range :" + degreeOfDish);
return false;
}
}
}
return true;
}
int price=0;
float getPrice()// 计价,计算本条记录的价格
{
this.price= ((int)(d.getPrice(portion)+0.5))*orderNum;
//System.out.println("+"+price);
return price;
}
}
//订单类:保存用户点的所有菜的信息。
class Order {
Record[] records=new Record[1000000];// 保存订单上每一道的记录
boolean [] deleteRecord=new boolean[10000];
Menu menu;
int cnt2=0;
public Order(Menu menu)
{
this.menu=menu;
}
//根据菜名点菜
void addARecord( int sno, String dishName, int portion, int orderNum)//点菜记录
{
Record r;
try
{
r = new Record(sno,menu.searthDish(dishName),portion,orderNum,dishName);
} catch (ParseException e)
{
throw new RuntimeException(e);
}
records[cnt2++]=r;
}
void addARecord( int sno, String dishName,int degree, int portion, int orderNum)
{
Record r;
try
{
r = new Record(sno,menu.searthDish(dishName),degree,portion,orderNum,dishName);
} catch (ParseException e)
{
throw new RuntimeException(e);
}
records[cnt2++]=r;
}
void addARecord(boolean toSuccess,int toTable,int sno,String dishName,int portion,int orderNum)
{
Record r;
try
{
r = new Record(toSuccess,toTable,sno,menu.searthDish(dishName),portion,orderNum,dishName);
} catch (ParseException e)
{
throw new RuntimeException(e);
}
records[cnt2++]=r;
}
void addARecord( int deleteSno,boolean success)//删除记录
{
records[cnt2++]=new Record(deleteSno,success);;
}
void addARecord(boolean mixDish)//添加菜谱混乱记录
{
records[cnt2++]=new Record(mixDish,false);
}
void addWrongFormatRecord(boolean wF)
{
records[cnt2++]=new Record(false,true);
}
boolean delARecordByOrderNum(int sno)//根据序号删除一条记录
{
int i;
for(i=0;i<cnt2;i++)
{
if(records[i].sno==sno)
{
records[i].deleted = true;
return true;
}
}
return false;
}
}
我的类图:

我的分析:这题相比成绩统计程序-1而言,增加了实验课的处理。当然,特色菜口味的处理是比较重要的点之一。但我觉得最重要的是如何处理新增的输入情况,怎么判断是否是特色菜或者是哪一个类的特色菜,正解是利用正则表达式中的知识点进行匹配关键字符比如“川菜”,“晋菜,等:
3.踩坑心得
作为初接触面对对象作业的我,最大的踩坑就是喜欢根据输入的要求在main里面想到什么写什么了,最后代码臃肿一团糟,复用性和可读性都很差。
正确的做法应该是先根据题目构建类再者构建实现相关功能的函数。
4.主要困难和改进建议
相信几乎绝大对部分同学认为最难的一部分就是菜单计价程序了,本人也几乎没有满分,一直在80~90徘徊,因为实在不知道哪里没完善,或者说不完全清楚测试点的要求。
我们绝大多数人遇到的困难是:
1.不能合理地根据题意抽象出相应的类。
2.面对大量的输入,和多要求输出,不能做到恰当的判断和分类处理。
3.对于题目的一些异常特判点,无法完全通过或理解导致失分。
对于这三个主要困难
我有对自己地建议,也有对出题者的建议。
首先,是对自己的建议:
对于问题一,(有不少人一看到题目一来就在main里面开写,直接处理输入,后面想到要构造那些类之后才吞吞吐吐地构建一些可能不太合理类出来。我觉得正确地顺序是首先根据题目,构造出正确类结构,再去具体根据输入输出构造方法来实现题目要求地功能。)我个人认为,根据题意,分析出需要储存的数据和功能,如果我们需要的某种功能是独立,那么可以单独创建一个方法实现该功能,如果我们需要的不是某种到单独的功能,而是多个功能与某些具有普遍性的性质结合的话,那么应该考虑创建一个类。
对于问题二,对于输入数据的判断其表达的信息,重点在与题目所给的格式进行匹配,格式相匹配,则是题目对应的信息类型。所以重点在于对输入的数据进行格式匹配:方法有两种1.正则表达式mathch()匹配。2.split()拆分成多部份进行判断,但强烈建议使用正则表达式,因为它能更在便捷地同时,更全面地处理输入信息。
对于问题三,说实话这是最折磨我的一点,因为我的分数多失于这里,迄今我也没有合适的方法去解决一些特殊测试点。但我觉得一些测试点可以用过题目已经给的测试样例推敲出来,这样能起到一些作用。
然后,是对出题者的建议:
对于问题三,我个人强烈建议在每次练习结束后公布测试点,因为对于一些特殊的测试点,不少人确实思考几个小时也想不出到底错在哪,如果不公布测试点,他们永远也不知道怎么改进自己错误的代码,而只会去看别人已经过了的代码,强制接受别人东西而并未改进自己,进而导致下一次练习,面临相同的特殊测试点依然不知道怎么处理。
5.总结
让我收获最大的是就是学会创建自己类进行结合和使用一些库类,学会利用类与类之间的关联,进而实现多要求的功能;以及JAVAz中一些容器比如map,set的存储,遍历方法。;以及使用正则表达式匹配想要的的输入格式。

浙公网安备 33010602011771号