第二次博客作业
题目集4~6总结性blog
(1)前言:
题目集4难度一般,但部分题目题量较大,主要考察类的设计,类与类之间的关系,封装性以及逻辑思维。同时考察了Scanner类中nextLine()等方法的使用、String类中split()等方法的使用、Integer类中parseInt()等方法的用法与LocalDate类中of()、isAfter()、isBefore()、until()等方法的使用规则,以及ChronoUnit类中DAYS、WEEKS、MONTHS等单位的用法。
题目集5难度一般,题量偏大,考察了正则表达式的合理使用,以及两种聚合方式,但重点还是有关类的设计,针对类的设计原则进行考察。
题目集6难度较高,虽然只有一道题,但任务量较大,主要考察类的设计,类的设计原则,但更多的是考察学生的逻辑思维以及对题目需求的考量。
(2)设计与分析
pta题目集四7-1
题目名:菜单计价程序-3
输入格式:
桌号标识格式: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+英文空格+桌号+“:”+英文空格+当前桌的总价
本次题目不考虑其他错误情况,如:桌号、菜单订单顺序颠倒、不符合格式的输入、序号重复等,在本系列的后续作业中会做要求。
分析:
类图如图所示:
既然是点菜系统,那首先得对菜品进行设计,设计一个Dish类,他有两个属性分别是菜名name,单价unit-price,然后针对菜单的设计,菜单是所有菜品的汇总,所以我们在菜单的属性中加个泛型为Dish的ArrayList用来存放所有菜品,然后加个名为addDish的方法用来向菜单中添加菜品,再有一个方法名为searchDish用来根据菜名返回菜品,这在后续记录价格肯定需要用到,再就是Record类用来针对点菜记录,日常生活中的点菜记录一般包括序号,所点菜名,所点菜的份额(大份或者小份),份数。因此Record包含五个属性分别是orderNum(序号),dishName(菜名),portion(份额),number(份数)。另外,根据题目要求,涉及到对点菜记录的删除操作,因此我们不妨再加个名为isOrder的属性(默认为true,如果该记录删除,记为false,后续计价的过程将会忽略该条记录)。再就是Order类,Order中包含各个点菜记录,因此我们将一个泛型为RecordArrayList作为他的属性,然后他有个ofTime,atTime属性,表示点单的时间,这在后续的得到折扣有所用到。再就是设计几个比较重要的方法,addRecod用来添加记录,delARecodByNum用来删除记录,findRecodBynum通过对应序号找到对应的记录。但这道题的关键我觉得更多的是如何设计类以及对应的方法以能够达到题目的需求。
不过,这道题我当时并没有写对,我写这道java编程题花了不少的时间,但是并没有达到理想的结果。但在我看来,方法总比困难多,这道题我未能够解决是我花的时间不够多的问题,在这个过程中,我并没有尝试用多种方法,就是一种方法走到黑,后面再不断的增删改以达到题目的要求,但很显然我尝试的是错误的,这道题给我的启发就是要尝试多种思路,有的时候方向对了效率会高得多。
pta题目集五7-5
题目名:日期问题面向对象设计(聚合一)
输入格式:
有三种输入方式(以输入的第一个数字划分[1,3]):
- 1 year month day n //测试输入日期的下n天
- 2 year month day n //测试输入日期的前n天
- 3 year1 month1 day1 year2 month2 day2 //测试两个日期之间相差的天数
输出格式:
- 当输入有误时,输出格式如下:
Wrong Format
- 当第一个数字为1且输入均有效,输出格式如下:
year-month-day
- 当第一个数字为2且输入均有效,输出格式如下:
year-month-day
- 当第一个数字为3且输入均有效,输出格式如下:
天数值
分析:
类图如图所示:
其实考察的就是类与类之间的关系,就由题目给的类图不难看出他是DateUtil-——Day——Month——Year这样一种层层的关联,因为他有递进的成分,所以不妨看出一种聚合的关系。具体算法的实现其实之前题目集中以及实现了,这道题主要就是让我们以类的形式来表示出来,然后考察了一种聚合的方式。
源码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n;
int choice=input.nextInt();//输入判断类型
int year1=input.nextInt();//输入年
int month1= input.nextInt();//输入月
int day1=input.nextInt();//输入日
DateUtil date1=new DateUtil(day1,month1,year1);
if(choice==1){//求下n天
n=input.nextInt();
if(!date1.checkInputValidity()||n<0){//非法数据
System.out.print("Wrong Format");
return;
}
else{
System.out.print(date1.getNextNDays(n).showDate());
}
}
else if(choice==2){//求前n天
n=input.nextInt();
if(!date1.checkInputValidity()||n<0){//非法数据
System.out.print("Wrong Format");
return;
}
else{
System.out.print(date1.getPreviousNDays(n).showDate());
}
}
else if(choice==3){//求两日期相差天数
//输入第二个日期
int y1=input.nextInt();
int m1= input.nextInt();
int d1=input.nextInt();
DateUtil d=new DateUtil(d1,m1,y1);
if(!date1.checkInputValidity()||!d.checkInputValidity()){//非法数据
System.out.print("Wrong Format");
return;
}
else
System.out.print(date1.getDaysofDates(d));
}
else{
System.out.println("Wrong Format");
}
}
}
class Day{
private int value;
private Month month;
private int[] mon_maxnum = new int[]{31,28,31,30,31,30,31,31,30,31,30,31};
public Day(){
}
public Day(int dayValue,int monthValue,int yearValue){
this.value = dayValue;
this.month = new Month(yearValue,monthValue);
}
public int getValue(){
return this.value;
}
public void setValue(int value){
this.value = value;
}
public Month getMonth(){
return this.month;
}
public void setMonth(Month month){
this.month = month;
}
public void resetMin(){
value = 1;
}
public void resetMax(){
value = mon_maxnum[month.getValue() - 1];
}
public boolean validate(){
if(month.getYear().isLeapYear()){
mon_maxnum[1] = 29;
}
if(value > 0 && value <= mon_maxnum[month.getValue() - 1]){
return true;
}
else{
return false;
}
}
public void dayIncrement(){
this.value = value + 1;
}
public void dayReduction(){
this.value = value - 1;
}
}
class Month{
private int value;
private Year year;
public Month(){
}
public Month(int yearValue,int monthValue){
this.value = monthValue;
this.year = new Year(yearValue) ;
}
public int getValue(){
return this.value;
}
public void setValue(int value){
this.value = value;
}
public Year getYear(){
return this.year;
}
public void setYear(Year year){
this.year = year;
}
public void resetMin(){
value = 1;
}
public void resetMax(){
value = 12;
}
public boolean validate(){
if(this.getValue() > 0 && this.getValue() < 13){
return true;
}
else{
return false;
}
}
public void monthIncrement(){
this.value = value + 1;
}
public void monthReduction(){
this.value = value - 1;
}
}
class Year{
private int value;
public Year(){
}
public Year(int yearValue){
this.value = yearValue;
}
public int getValue(){
return this.value;
}
public void setValue(int value){
this.value = value;
}
public boolean isLeapYear(){
if(value % 4 == 0 && value % 100 != 0 || value % 400 == 0){
return true;
}
else{
return false;
}
}
public boolean validate(){
if(this.getValue() >= 1900 && this.getValue() <= 2050){
return true;
}
else{
return false;
}
}
public void yearIncrement(){
this.value = value + 1;
}
public void yearReduction(){
this.value = value - 1;
}
}
class DateUtil{
private Day day;
public DateUtil(){
}
public DateUtil(int d,int m,int y){
this.day=new Day(d,m,y);
}
public Day getDay(){
return this.day;
}
public void setDay(Day day){
this.day = day;
}
public boolean checkInputValidity(){
if(this.day.getMonth().getYear().validate() && this.day.getMonth().validate() && this.day.validate()){
return true;
}
else{
return false;
}
}
public boolean compareDates(DateUtil date){
if(this.day.getMonth().getYear().getValue() > date.day.getMonth().getYear().getValue()){
return true;
}
else if(this.day.getMonth().getYear().getValue() == date.day.getMonth().getYear().getValue()){
if(this.day.getMonth().getValue() > date.day.getMonth().getValue()){
return true;
}
else if(this.day.getMonth().getValue() == date.day.getMonth().getValue()){
if(this.day.getValue() > date.day.getValue()){
return true;
}
else{
return false;
}
}
else{
return false;
}
}
else{
return false;
}
}
//比较两日期是否相等
public boolean equalTwoDates(DateUtil date){
if(this.day.getValue() == date.day.getValue() && this.day.getMonth().getValue() == date.day.getMonth().getValue() && this.day.getMonth().getYear().getValue() == date.day.getMonth().getYear().getValue()){
return true;
}
else{
return false;
}
}
public String showDate(){
return (this.getDay().getMonth().getYear().getValue() + "-" + this.getDay().getMonth().getValue() + "-" + this.getDay().getValue());
}
//求下n天
public DateUtil getNextNDays(int n){
int a[] = new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(this.getDay().getMonth().getYear().isLeapYear()){
a[2] = 29;
}
for(int i = 0;i < n;i++){
if(this.day.getValue() == a[this.day.getMonth().getValue()]){
if(this.day.getMonth().getValue() == 12){
this.day.getMonth().getYear().yearIncrement();
this.day.getMonth().setValue(1);
this.day.setValue(1);
if(this.getDay().getMonth().getYear().isLeapYear()){
a[2] = 29;
}
else{
a[2] = 28;
}
}
else{
this.day.getMonth().monthIncrement();
this.day.setValue(1);
}
}
else{
this.day.dayIncrement();
}
}
int year1 = this.day.getMonth().getYear().getValue();
int month1 = this.day.getMonth().getValue();
int day1 = this.day.getValue();
return new DateUtil(day1,month1,year1);
}
//求前n天
public DateUtil getPreviousNDays(int n){
int a[] = new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(this.getDay().getMonth().getYear().isLeapYear()){
a[2] = 29;
}
for(int i = 0;i < n;i++){
if(this.day.getValue() == 1){
if(this.day.getMonth().getValue() == 1){
this.day.getMonth().getYear().yearReduction();
this.day.getMonth().setValue(12);
this.day.setValue(31);
//当年变化后,判断该年是否为闰年
if(this.getDay().getMonth().getYear().isLeapYear()){
a[2] = 29;
}
else{
a[2] = 28;
}
}
else{
this.day.getMonth().monthReduction();
this.day.setValue(a[this.day.getMonth().getValue()]);
}
}
else{
this.day.dayReduction();
}
}
int year1 = this.day.getMonth().getYear().getValue();
int month1 = this.day.getMonth().getValue();
int day1 = this.day.getValue();
return new DateUtil(day1,month1,year1);
}
//求两日期相差天数
public int getDaysofDates(DateUtil date){
int result = 0;
int a[] = new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(equalTwoDates(date)){
result = 0;
}
else{
if(compareDates(date)){
while (!equalTwoDates(date)){
if(this.day.getValue() == 1){
if(this.day.getMonth().getValue() == 1){
this.day.getMonth().getYear().yearReduction();
this.day.getMonth().resetMax();
this.day.resetMax();
//每当年变化后,都要判断该年是否为闰年
if(this.getDay().getMonth().getYear().isLeapYear()){
a[2] = 29;
}
else{
a[2] = 28;
}
}
else{
this.day.getMonth().monthReduction();
this.day.setValue(a[this.day.getMonth().getValue()]);
}
}
else{
this.day.dayReduction();
}
result++;
}
}
else{
while (!equalTwoDates(date)){
if(this.day.getValue() == a[this.day.getMonth().getValue()]){
if(this.day.getMonth().getValue() == 12){
this.day.getMonth().getYear().yearIncrement();
this.day.getMonth().resetMin();
this.day.resetMin();
if(this.getDay().getMonth().getYear().isLeapYear()){
a[2] = 29;
}
else{
a[2] = 28;
}
}
else{
this.day.getMonth().monthIncrement();
this.day.resetMin();
}
}
else{
this.day.dayIncrement();
}
result++;
}
}
}
return result;//返回相差的天数
}
}
pta题目集五7-6
题目名:日期问题面向对象设计(聚合二)
输入格式:
有三种输入方式(以输入的第一个数字划分[1,3]):
- 1 year month day n //测试输入日期的下n天
- 2 year month day n //测试输入日期的前n天
- 3 year1 month1 day1 year2 month2 day2 //测试两个日期之间相差的天数
输出格式:
- 当输入有误时,输出格式如下:
Wrong Format
- 当第一个数字为1且输入均有效,输出格式如下:
year1-month1-day1 next n days is:year2-month2-day2
- 当第一个数字为2且输入均有效,输出格式如下:
year1-month1-day1 previous n days is:year2-month2-day2
- 当第一个数字为3且输入均有效,输出格式如下:
The days between year1-month1-day1 and year2-month2-day2 are:值
分析:
类图:
这道题与上个题目差不多,只是改变了类之间的关系,可以看出这也是一种聚合关系,Year,Month,Day聚合于DateUtil,但这种聚合关系明显比上个聚合关系的耦合度降低了,也更加的符合我们类的设计原则。
源码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n;
int choice=input.nextInt();//输入判断类型
int year1=input.nextInt();//输入年
int month1= input.nextInt();//输入月
int day1=input.nextInt();//输入日
DateUtil date1=new DateUtil(day1,month1,year1);
if(choice==1){//求下n天
n=input.nextInt();
if(!date1.checkInputValidity()||n<0){//非法数据
System.out.print("Wrong Format");
return;
}
else{
System.out.print(date1.showDate() + " next " + n + " days is:" + date1.getNextNDays(n).showDate());
}
}
else if(choice==2){//求前n天
n=input.nextInt();
if(!date1.checkInputValidity()||n<0){//非法数据
System.out.print("Wrong Format");
return;
}
else{
System.out.print(date1.showDate() + " previous " + n + " days is:" + date1.getPreviousNDays(n).showDate());
}
}
else if(choice==3){//求两日期相差天数
//输入第二个日期
int y1=input.nextInt();
int m1= input.nextInt();
int d1=input.nextInt();
DateUtil date2=new DateUtil(d1,m1,y1);
if(!date1.checkInputValidity()||!date2.checkInputValidity()){//非法数据
System.out.print("Wrong Format");
return;
}
else
System.out.print("The days between " + date1.showDate() + " and " + date2.showDate() + " are:" + date1.getDaysofDates(date2));
}
else{
System.out.print("Wrong Format");
}
}
}
class Day{
private int value;
public Day(){
}
public Day(int dayValue){
this.value = dayValue;
}
public int getValue(){
return this.value;
}
public void setValue(int value){
this.value = value;
}
public void dayIncrement(){
this.value = value + 1;
}
public void dayReduction(){
this.value = value - 1;
}
}
class Month{
private int value;
public Month(){
}
public Month(int monthValue){
this.value = monthValue;
}
public int getValue(){
return this.value;
}
public void setValue(int value){
this.value = value;
}
public void resetMin(){
value = 1;
}
public void resetMax(){
value = 12;
}
public boolean validate(){
if(this.getValue() > 0 && this.getValue() < 13){
return true;
}
else{
return false;
}
}
public void monthIncrement(){
this.value = value + 1;
}
public void monthReduction(){
this.value = value - 1;
}
}
class Year{
private int value;
public Year(){
}
public Year(int yearValue){
this.value = yearValue;
}
public int getValue(){
return this.value;
}
public void setValue(int value){
this.value = value;
}
public boolean isLeapYear(){
if(value % 4 == 0 && value % 100 != 0 || value % 400 == 0){
return true;
}
else{
return false;
}
}
public boolean validate(){
if(this.getValue() >= 1820 && this.getValue() <= 2020){
return true;
}
else{
return false;
}
}
public void yearIncrement(){
this.value = value + 1;
}
public void yearReduction(){
this.value = value - 1;
}
}
class DateUtil{
private Day day;
private Year year;
private Month month;
private int[] mon_maxnum = new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
public DateUtil(){
}
public DateUtil(int d,int m,int y){
this.day = new Day(d);
this.month = new Month(m);
this.year = new Year(y);
}
public Day getDay(){
return this.day;
}
public void setDay(Day day){
this.day = day;
}
public boolean checkInputValidity(){
if(this.year.isLeapYear()){
mon_maxnum[2] = 29;
}
else{
mon_maxnum[2] = 28;
}
if(this.year.validate() && this.month.validate() && this.day.getValue() > 0 && this.day.getValue() <= mon_maxnum[this.month.getValue()]){
return true;
}
else{
return false;
}
}
public boolean compareDates(DateUtil date){
if(this.year.getValue() > date.year.getValue()){
return true;
}
else if(this.year.getValue() == date.year.getValue()){
if(this.month.getValue() > date.month.getValue()){
return true;
}
else if(this.month.getValue() == date.month.getValue()){
if(this.day.getValue() > date.day.getValue()){
return true;
}
else{
return false;
}
}
else{
return false;
}
}
else{
return false;
}
}
//比较两日期是否相等
public boolean equalTwoDates(DateUtil date){
if(this.day.getValue() == date.day.getValue() && this.month.getValue() == date.month.getValue() && this.year.getValue() == date.year.getValue()){
return true;
}
else{
return false;
}
}
public String showDate(){
return (this.year.getValue() + "-" + this.month.getValue() + "-" + this.getDay().getValue());
}
//求下n天
public DateUtil getNextNDays(int n){
int a[] = new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(this.year.isLeapYear()){
a[2] = 29;
}
for(int i = 0;i < n;i++){
if(this.day.getValue() == a[this.month.getValue()]){
if(this.month.getValue() == 12){
this.year.yearIncrement();
this.month.setValue(1);
this.day.setValue(1);
if(this.year.isLeapYear()){
a[2] = 29;
}
else{
a[2] = 28;
}
}
else{
this.month.monthIncrement();
this.day.setValue(1);
}
}
else{
this.day.dayIncrement();
}
}
int year1 = this.year.getValue();
int month1 = this.month.getValue();
int day1 = this.day.getValue();
return new DateUtil(day1,month1,year1);
}
//求前n天
public DateUtil getPreviousNDays(int n){
int a[] = new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(this.year.isLeapYear()){
a[2] = 29;
}
for(int i = 0;i < n;i++){
if(this.day.getValue() == 1){
if(this.month.getValue() == 1){
this.year.yearReduction();
this.month.setValue(12);
this.day.setValue(31);
//当年变化后,判断该年是否为闰年
if(this.year.isLeapYear()){
a[2] = 29;
}
else{
a[2] = 28;
}
}
else{
this.month.monthReduction();
this.day.setValue(a[this.month.getValue()]);
}
}
else{
this.day.dayReduction();
}
}
int year1 = this.year.getValue();
int month1 = this.month.getValue();
int day1 = this.day.getValue();
return new DateUtil(day1,month1,year1);
}
//求两日期相差天数
public int getDaysofDates(DateUtil date){
int result = 0;
int a[] = new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(this.year.isLeapYear()){
a[2] = 29;
}
if(equalTwoDates(date)){
result = 0;
}
else{
if(compareDates(date)){
while (!equalTwoDates(date)){
if(this.day.getValue() == 1){
if(this.month.getValue() == 1){
this.year.yearReduction();
this.month.resetMax();
this.day.setValue(31);
//每当年变化后,都要判断该年是否为闰年
if(this.year.isLeapYear()){
a[2] = 29;
}
else{
a[2] = 28;
}
}
else{
this.month.monthReduction();
this.day.setValue(a[this.month.getValue()]);
}
}
else{
this.day.dayReduction();
}
result++;
}
}
else{
while (!equalTwoDates(date)){
if(this.day.getValue() == a[this.month.getValue()]){
if(this.month.getValue() == 12){
this.year.yearIncrement();
this.month.resetMin();
this.day.setValue(1);
if(this.year.isLeapYear()){
a[2] = 29;
}
else{
a[2] = 28;
}
}
else{
this.month.monthIncrement();
this.day.setValue(1);
}
}
else{
this.day.dayIncrement();
}
result++;
}
}
}
return result;//返回相差的天数
}
}
pta题目集六7-1
题目名:菜单计价程序-4
类图:
分析:这道题目我花了较长的一段时间去写,但最后只是得了32分,这道题目比上次的菜单题目多了许多的异常情况处理,我做这道题比较迷惑的就是题目的测试点我有点搞不太清楚,再就是很明显的我类设计出现了错误,导致很难满足题目的需求,我当时想的是在输入记录后立马就进行判断,但这样效率显然并不高,并且满足不了题目的一些需求,后面我换了个思路,就是将每条记录先储存起来,后面再拿出来一个个进行判断。但我不清楚的是题目的测试点以及不同异常情况的输出格式是怎样的,但归根结底是我花的时间太少了以及我设计的类不符合要求才导致出现低分的情况,并且·有时候会出现改了之后有的测试点过了,但有些本来已经过了的测试点反而没过,后面我想了一下,应该是我设计的类之间的耦合性太高导致的。
源码:
import java.util.Scanner;
import java.util.ArrayList;
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.LocalDate;
public class Main {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String str = null;
Menu menu = new Menu();
ArrayList<Order> orders = new ArrayList<Order>();
Order order1 = new Order();
Order order[] = new Order[10];
int i = 0;int s = 0 ;
int q = 0;
int num = 0;
int n = 0;
int z = -10;
order[i] = new Order();
ArrayList<String> strs = new ArrayList<String>();
while(!"end".equals(str)){
str = input.nextLine();
strs.add(str);
}
for(String j:strs){
String s1[] = j.split(" |\\/");
if(s1.length == 2){
if("delete".equals(s1[1])&&s == 0){
int a = Integer.parseInt(s1[0]);
order[i].delARecordByOrderNum(a);
if(order[i].findRecordByNum(a).getIsOrder() > 1){
System.out.println("deduplication " + a);
}}
else{
if(q == 0){
if(s1[1].matches("[1-9][0-9]{0,}")) {
if(s1[1].matches("[1-9][0-9]{0,2}||[1-2][0-9]{0,3}")) {
int a = Integer.parseInt(s1[1]);
menu.addDish(s1[0], a, 0);
}
else{
System.out.println(s1[0] + " price out of range ");}
}
if(!s1[1].matches("[1-9][0-9]{0,}")){
System.out.println("wrong format");
}}
if(q ==1&&s == 0){
System.out.println("invalid dish");
}
}
}
if(s1.length == 4&&s == 0 ){
a:if(Menu.searthDish(s1[1])!=null){
if(s1[2].length() > 1||!s1[3].matches("[1-9][\\d]{0,}")){
System.out.println("Wrong Format");
//System.exit(0);
}
else{
int a = Integer.parseInt(s1[0]);
int b = Integer.parseInt(s1[2]);
int c = Integer.parseInt(s1[3]);
if(b < 4&&b > 0&&c < 16 && c > 0){
order[i].addARecord(a,s1[1],b,c);
order[i].addARecord(a,s1[1],b,c).printOrderNumAndDishName();
}
else{
if(b > 3){
System.out.println(a + " portion out of range " +b);break a;
}
if(c >15){
System.out.println(a + " num out of range " + c);
}
}
}
}
else{
System.out.println(s1[1] + " does not exist");break a;
}
}
else if(s1.length == 3 ){
if(q == 0){
if(s1[1].matches("[1-9][0-9]{0,}")) {
if(s1[1].matches("[1-9][0-9]{0,2}||[1-2][0-9]{0,3}")) {
int a = Integer.parseInt(s1[1]);
menu.addDish(s1[0], a, 1);
}
else{
System.out.println(s1[0] + " price out of range ");}
}
if(!s1[1].matches("[1-9][0-9]{0,}")){
System.out.println("wrong format");
}
}
if(q == 1){
System.out.println("invalid dish");
}
}
else if(s1.length == 8){
if(num == 1 && n == 1){
order[i].getTotalPrice();
}
s=0;
q=1;
num=1;
if(s1[0].equals("table")){
//if(s1[1].matches("[1-9][\\d]{0,}")&&s1[2].length() == 4&&s1[3].length() < 3&&
//s1[4].length() < 3&&s1[5].length() < 3&&s1[6].length() < 3&&s1[7].length() < 3){
if(s1[1].matches("[1-9][\\d]{0,}")) {
i++;
int a = Integer.parseInt(s1[1]);
int y = Integer.parseInt(s1[2]);
int m = Integer.parseInt(s1[3]);
int d = Integer.parseInt(s1[4]);
int hour = Integer.parseInt(s1[5]);
int minute = Integer.parseInt(s1[6]);
int second = Integer.parseInt(s1[7]);
if(a > 0&&a < 56){
if(s1[2].length() == 4&&s1[3].length() < 3&&
s1[4].length() < 3&&s1[5].length() < 3&&s1[6].length() < 3&&s1[7].length() < 3){
if( m > 0&&m < 13){
LocalDate time = LocalDate.of(y,m,d);
LocalDate time1 = LocalDate.of(2022,1,1);
LocalDate time2 = LocalDate.of(2023,12,31);
if(time.isAfter(time1) && time.isBefore(time2)){
System.out.println("table " + s1[1] + ": ");
order[i] = new Order(a, y, m, d, hour, minute, second);
n=1;
orders.add(order[i]);
}}
else{
s=1;
System.out.println("not a valid time period");
}
}
else{
s=1;
System.out.println("wrong format");
}
}
else{
System.out.println(a + " table num out of range");
}
//}
}
else{
System.out.println("wrong format");
s=1;
}
}
else{
System.out.println("wrong format");
s=1;
}
}
}
if(n == 1 && s==0){
order[i].getTotalPrice();}
}}
class Dish {
String name;//菜品名称
int unit_price; //单价
private int feature;
public int getFeature() {
return feature;
}
public void setFeature(int feature) {
this.feature = feature;
}
//计算菜品价格的方法
int getPrice(int portion){
if(portion == 1)
return unit_price;
else if(portion == 2)
return (int)(0.5 + 1.5 * unit_price);
else
return 3 * unit_price;
}
Dish(String name,int price,int feature){
this.name = name;
this.unit_price = price;
this.feature = feature;
}
}
class Menu {
static ArrayList<Dish> dishs = new ArrayList<Dish>();//菜品数组,保存所有菜品信息
//根据菜名在菜谱中查找菜品信息,返回Dish对象。
public static Dish searthDish(String dishName){
for(int i = 0;i < dishs.size();i++){
if(dishs.get(i).name.equals(dishName)){
return dishs.get(i);
}
}
return null;
}
//添加一道菜品信息
Dish addDish(String dishName,int unit_price,int feature){
Dish newDish = new Dish(dishName,unit_price,feature);
dishs.add(newDish);
return newDish;
}
}
class Record {
private int orderNum;//序号
private String dishName;//菜品
private int portion;//份额(1/2/3代表小/中/大份)
private int number;//份数
private int isOrder = 0;
public Record(int orderNum, String dishName, int portion,int num) {
this.orderNum = orderNum;
this.dishName = dishName;
this.portion = portion;
this.number=num;
}
public int getOrderNum() {
return orderNum;
}
public int getPortion() {
return portion;
}
public void setPortion(int portion) {
this.portion = portion;
}
public void setOrderNum(int orderNum) {
this.orderNum = orderNum;
}
public String getDishName() {
return dishName;
}
public void setDishName(String dishName) {
this.dishName = dishName;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public int getIsOrder() {
return isOrder;
}
public void setIsOrder(int isOrder) {
this.isOrder = isOrder;
}
//计价,计算本条记录的价格
int getPrice(){
if(this.getNumber() <= 15 && this.getPortion() <= 3){
return Menu.searthDish(dishName).getPrice(portion) * number;
}
else{
return 0;
}
}
void printOrderNumAndDishName(){
if(Menu.searthDish(this.getDishName()) != null){
//if(Menu.searthDish(this.getDishName()).getFeature() == 1){
if(this.getPortion() > 3){
System.out.println(this.getOrderNum() + " portion out of range " + this.getPortion());
if(this.getNumber() > 15)
System.out.println(this.getOrderNum() + " num out of range " + this.getNumber());
}
else{
if(this.getNumber() <= 15)
System.out.println(this.getOrderNum() + " " + this.getDishName() + " " + this.getPrice());
else
System.out.println(this.getOrderNum() + " num out of range " + this.getNumber());
}
}
else{
System.out.println(this.dishName + " does not exist");
}
if(this.getIsOrder() > 1){
System.out.println("deduplication " + this.getOrderNum());
}
}
}
class Order {
private int year;
private int month;
private int day;
private int tableNum;
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
private LocalDate ofDate;
private LocalTime atTime;
public int getTableNum() {
return tableNum;
}
public void setTableNum(int tableNum) {
this.tableNum = tableNum;
}
public LocalDate getDateTime() {
return ofDate;
}
public void setDateTime(LocalDate dateTime) {
this.ofDate = dateTime;
}
public LocalTime getAtTime() {
return atTime;
}
public void setAtTime(LocalTime atTime) {
this.atTime = atTime;
}
ArrayList<Record> records = new ArrayList<Record>();//保存订单上每一道的记录
public Order(){
}
public Order(int y,int m,int d){
this.year = y;
this.month = m;
this.day = d;
}
public Order(int a,int y,int m,int d,int hour,int minute,int second){
this.tableNum = a;
this.ofDate = LocalDate.of(y,m,d);
this.atTime = LocalTime.of(hour,minute,second);
this.year = y;
this.month = m;
this.day = d;
}
public double getDiscount(){
DayOfWeek dayOfWeek = ofDate.getDayOfWeek();
if(dayOfWeek==DayOfWeek.MONDAY||dayOfWeek==DayOfWeek.TUESDAY||dayOfWeek==DayOfWeek.WEDNESDAY
||dayOfWeek==DayOfWeek.THURSDAY||dayOfWeek==DayOfWeek.FRIDAY){
if(this.getAtTime().isAfter(LocalTime.of(10,30,00))&&
this.getAtTime().isBefore(LocalTime.of(14,30,00))){
return 0.6;
}
else if(this.getAtTime().isAfter(LocalTime.of(17,00,00))&&
this.getAtTime().isBefore(LocalTime.of(20,30,00))){
return 0.8;
}
else{
return 100;
}
}
else{
if(this.getAtTime().isAfter(LocalTime.of(9,30,00))&&
this.getAtTime().isBefore(LocalTime.of(21,30,00))){
return 1;
}
else{
return 100;
}
}
}
//计算订单的总价
void getTotalPrice(){
DayOfWeek dayOfWeek = ofDate.getDayOfWeek();
double a = 1;
if(dayOfWeek==DayOfWeek.MONDAY||dayOfWeek==DayOfWeek.TUESDAY||dayOfWeek==DayOfWeek.WEDNESDAY
||dayOfWeek==DayOfWeek.THURSDAY||dayOfWeek==DayOfWeek.FRIDAY){
a = 0.7;
}
if(getDiscount() == 100){
System.out.println("table " + this.getTableNum() + " out of opening hours");
}
else{
int sum1 = 0;
int sum2 = 0;
int sum3 = 0;
for(int i = 0;i < records.size()-1;i++){
if(records.get(i).getIsOrder() == 0 && Menu.searthDish(records.get(i).getDishName())!=null){
if(Menu.searthDish(records.get(i).getDishName()).getFeature() == 1){
sum1 = sum1 + (int)(records.get(i).getPrice() * a + 0.5);
}
else {
sum2 = sum2 + records.get(i).getPrice();
}
sum3 = sum3 + records.get(i).getPrice();
}
}
System.out.println("table " + this.getTableNum() + ": " + sum3 + " " + (sum1 + (int)(sum2 * this.getDiscount() + 0.5)));
}
}
//添加一条菜品信息到订单中
Record addARecord(int orderNum,String dishName,int portion,int num){
Record newRecord = new Record(orderNum,dishName,portion,num);
records.add(newRecord);
return newRecord;
}
void delARecordByOrderNum(int orderNum){
Record i=findRecordByNum(orderNum);
if (i==null){
System.out.println("delete error;");return;
}
i.setIsOrder(i.getIsOrder() + 1);
}
Record findRecordByNum(int orderNum){
for (Record i:records) {
if(i.getOrderNum() == orderNum)
return i;
}
return null;
}
public void showOrder(){
/*if(this.getTableNum() > 0 && this.getTableNum() <56){
//System.out.println("table " + this.getTableNum() + ": ");
}
else{
System.out.println(this.getTableNum() + " table num out of range");
}
if(this.getMonth() < 1||this.getMonth() > 12){
System.out.println(this.getTableNum() + " date error");
}*/
for (Record i:records) {
i.printOrderNumAndDishName();
}
this.getTotalPrice();
}
}
(3)踩坑心得
1.正则表达式的使用:字符串用【】,而字符出现的次数用{},二者不可搞混。
2.数组越界问题:在使用数组时需要注意数组越界问题,避免出现 Array Index Out Of BoundsException 的情况。
3.类的设计以及算法的设计:在写代码之前一定要根据题目需求对类的设计以及算法的设计进行充分的考量,避免出现因为设计低效而出现费时费力还达不到要求的情况,这是我目前踩的最大的坑。
(4)改进建议
其实我觉得pta第六次作业的题目需求输出格式可以在详细一点,具体一点。
(5)总结:
1.关于正则表达式:正则表达式在出来字符串等问题的时候真的是非常的方便,能非常提高代码的可读性以及编码的难度。
2.关于String类中的Split()方法:这个方法在需要提取字符串内容时需要用到,要注意的就是分隔的条件,以什么符号分割。
3.关于LocalDate类中of()、isAfter()、isBefore()、until()等方法:事实上,在处理日期的时候,LocalDate类中的许多方法都非常有用,比如判断时间的先后,得到某时间所对应的星期几等等。
4.类的设计原则:类的七大设计原则,包括像单一职责,迪米特法则等等。
5.根据需求进行设计:在写代码之前,要对需求进行分析并进行设计,我该如何设计才能高效准确的满足需求。