首页 > 其他分享 >oo第二次博客总结

oo第二次博客总结

时间:2023-04-29 15:13:39浏览次数:45  
标签:oo 总结 int value getValue 博客 month public day

 

 

目录

1.前言

2.设计与分析

3.踩坑心得

4.改进建议

5.总结

一:前言

题目集四:

1,菜单计价程序-3

2,有重复的数据

3,去掉重复的数据

4.单词的统计与排序

5.面向对象编程(封装性)

6.GPS测绘中度分秒转换

7.判断两个日期的先后,计算间隔天数、周数.

题目集五:

1.正则表达式训练-QQ号校验

2.字符串训练-字符排序

3.正则表达式训练-验证码校验

4. 正则表达式训练-学号校验

5.日期问题面向对象设计(聚合一)

6.日期问题面向对象设计(聚合二)

题目集六:

1.菜单计价程序-4

 在题目集四中,刚写的时候就被第一题给吓到了,毕竟题目太长了,就放在最后写,但是最后可能是自己学的不精,导致最后时间不够,所以这题就没有完成,第二题和之前的题目类似·,没什么说的,到第三题,一开始想用ArrayList和里面的方法add和contain解决的,但是n过大时程序会超时,所以有几个点一直过不去,后面在同学的推荐下,使用了Hashset最好解决了这个问题。第四题先使用冒泡排序比较,再通过compareToIgnoreCase这个忽略大小写的比较就出来了。第五题就是简单封装性,第六题也就是简单的转换,在第七题,我学习使用了很多很好用的方法,如·String类中split()等方法、Integer类中parseInt()等方法,LocalDate类中of()、isAfter()、isBefore()等方法,这些方法拓展了我的视野,在以后的问题中提供了更多的思路。   到了题目集五,前面四题都是与正则表达式有关的题目,稍微了解到正则表达式的基础知识就能够解决。而到第五题和第六题就涉及到了聚合,其实这两道题目是一样的,只不过方法不同,所以写出来了一道题,第二道也很轻松就能够被写出来,其实这两道题目都不是很难,毕竟类之间的关系图都已经给出来了,只要写里面具体的方法就行,难度不高,因为里面的方法在之前都写过,只要把之前的方法拿进去就行,总体上花点时间就能够完成的那种。   到了题目集六,这次就一题,难度也是最大的一道题,这道题是在这次题目集一的基础上进行改善,我花了两天的时间来研究这道题,然后才着手敲代码,在敲了三百行的代码时,我进行了第一次提交,答案错误,一个测试点都没有过,在后面的多次调试下陆续的通过了几个测试点,但是对于多桌菜和带点菜这两种问题还是没有想到解决的方法,这道题的难度对我来说还是有点大。   知识点:正则表达式,冒泡排序,Linkhashset和里面的方法,String类中split()等方法,Integer类中parseInt(),LocalDate类中of()、isAfter()、isBefore()、until(),类与类之间的聚合。 题量和难度:第一次的题量适中,难度对我来说还能够接受,但第一次的第一题那时候正则表达式还没学过,所以那道题对我来说很难,第二次题量有点少,感觉没什么难度,到第三次就一道题目,题量不多,但是难度非常高,只能过部分测试点。   二:设计与分析 题目集四7-1题菜单计价程序-3,这道题在规定的时间里没有写出来,在后面也提交不了了,所以我也不知道这道题我到底过了多少。 题目集五7-5,这道题有三个任务
  1. 求下n天
  2. 求前n天
  3. 求两个日期相差的天数   下面是有关的类图

源码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int year = 0;
int month = 0;
int day = 0;

int choice = input.nextInt();

if (choice == 1) { // test getNextNDays method
int m = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());
// System.out.println(day);
DateUtil date = new DateUtil(day, month, year);

if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}

m = input.nextInt();

if (m < 0) {
System.out.println("Wrong Format");
System.exit(0);
}

//System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:");
System.out.println(date.getNextNDays(m).showDate());
} else if (choice == 2) { // test getPreviousNDays method
int n = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

DateUtil date = new DateUtil(day, month, year);

if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}

n = input.nextInt();

if (n < 0) {
System.out.println("Wrong Format");
System.exit(0);
}

// System.out.print(
//date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:");
System.out.println(date.getPreviousNDays(n).showDate());
} else if (choice == 3) { //test getDaysofDates method
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

int anotherYear = Integer.parseInt(input.next());
int anotherMonth = Integer.parseInt(input.next());
int anotherDay = Integer.parseInt(input.next());

DateUtil fromDate = new DateUtil(day, month, year);
DateUtil toDate = new DateUtil(anotherDay, anotherMonth, anotherYear);

if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
System.out.println(fromDate.getDaysofDates(toDate));
} else {
System.out.println("Wrong Format");
System.exit(0);
}
}
else{
System.out.println("Wrong Format");
System.exit(0);
}
}
}
class DateUtil {
private Day day =new Day();
public DateUtil()
{

}

public DateUtil(int d,int m,int y)
{
day.setValue(d);
day.getMonth().setValue(m);
day.getMonth().getYear().setValue(y);
}

public Day getDay() {
return day;
}

public void setDay(Day day) {
this.day = day;
}
public boolean checkInputValidity()
{
if(day.validate()==false||day.getMonth().validate()==false||day.getMonth().getYear().validate()==false)
return false;
else return true;
}
public boolean compareDates(DateUtil date)
{
if(day.getMonth().getYear().getValue()>date.day.getMonth().getYear().getValue()) return true;
else if(day.getMonth().getYear().getValue()==date.day.getMonth().getYear().getValue()&&day.getMonth().getValue()>date.day.getMonth().getValue()) return true;
else if(day.getMonth().getYear().getValue()==date.day.getMonth().getYear().getValue()&&day.getMonth().getValue()==date.day.getMonth().getValue()&&date.getDay().getValue()>day.getValue()) return true;
else return false;
}
public boolean equalTwoDates(DateUtil date)
{
if(day.getMonth().getYear().getValue()==date.day.getMonth().getYear().getValue()&&day.getMonth().getValue()==date.day.getMonth().getValue()&&date.getDay().getValue()==day.getValue()) return true;
else return false;
}
public String showDate()
{
return day.getMonth().getYear().getValue()+"-"+day.getMonth().getValue()+"-"+day.getValue();
}
public DateUtil getNextNDays(int n)
{
for(;n>0;n--)
{
day.dayIncrement();
//i=day.getValue();
}
return this;
}
public DateUtil getPreviousNDays(int n)
{
for(;n>0;n--)
{
day.dayReduction();
}
return this;
}
public int getDaysofDates(DateUtil date)
{
int i = 1,s = 0,k = 0;
while(i == 1)
{
if(day.getValue() == 31&&day.getMonth().getValue() == 12&&day.getMonth().getYear().getValue() == 2050)break;
day.dayIncrement();
s = s + 1;
}
while(i == 1)
{
if(date.day.getValue() == 31&&date.day.getMonth().getValue() == 12&&date.day.getMonth().getYear().getValue() == 2050)break;
date.day.dayIncrement();
k = k + 1;
}
return Math.abs(s-k);
}
}
class Year{
private int value = 0;
public Year()
{

}
public Year(int value)
{
this.value = value;
}
public int getValue()
{
return 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(value>=1900&&value<=2050) return true;
else return false;
}
public void yearIncrement()
{
value = value + 1;
}
public void yearReduction()
{
value = value - 1;
}

}
class Month {
private int value = 0;
private Year year = new Year();
public Month()
{

}
public Month(int yearValue,int monthValue)
{
this.value = monthValue;
year.setValue(yearValue);
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Year getYear() {
return year;
}
public void setYear(Year year) {
this.year = year;
}
public void resetMin()
{
value = 1 ;
}
public void resetMax()
{
value = 12;
}
public boolean validate()
{
if(value<=12&&value>=1) return true;
else return false;
}
public void monthIncrement()
{
value = value + 1;
if(value == 13)
{value = 1;
year.yearIncrement();
}
}
public void monthReduction()
{
value = value - 1;
if(value == 0)
{value = 12;
year.yearReduction();
}
}
}
class Day {
private int value = 0;
private Month month =new Month();
private int mon_maxnum[] = {31,28,31,30,31,30,31,31,30,31,30,31};

public Day()
{
}
public Day(int yearValue,int monthValue,int dayValue)
{
value = dayValue;
month.setValue(monthValue);
month.getYear().setValue(yearValue);
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Month getMonth() {
return month;
}
public void setMonth(Month month) {
this.month = month;
}
public void resetMin()
{
value = 1;
}
public void resetMax()
{if(month.getYear().isLeapYear()==true) mon_maxnum[1] = 29;
else mon_maxnum[1] = 28;
value = mon_maxnum[month.getValue()-1];
}
public boolean validate()
{ if(month.getYear().isLeapYear()==true) mon_maxnum[1] = 29;
if(month.getValue() <= 0||month.getValue() >= 13) return true;
else { if(value <= mon_maxnum[month.getValue()-1]&&value>0) return true;
else return false;}
}
public void dayIncrement()
{ if(month.getYear().isLeapYear()==true) mon_maxnum[1] = 29;
else mon_maxnum[1] = 28;
value = value + 1;
if(value > mon_maxnum[month.getValue()-1]) {
value = 1;
month.monthIncrement();
}

}
public void dayReduction()
{ if(month.getYear().isLeapYear()==true) mon_maxnum[1] = 29;
else mon_maxnum[1] = 28;
value = value - 1;
if(value == 0)
{
month.monthReduction();
value = mon_maxnum[month.getValue()-1];
}
}

}

解释:Year和Month类之间聚合起来,Month和Day类之间聚合起来,Day和DateUtil类之间聚合起来,使各个类之间给联系了起来,难点在于每个类中需要精确的调用其他类中的方法,但是里面的方法之前已经写过了,就不再具体分析,层层调用,一个地方出错,可能也会导致其他地方报错,所以要确保每个类中方法的正确性。

心得:敲代码时保证每个方法的正确性,否则后面使用该类中的方法时会得到错误的答案。

7-6和上面7-5的要求一样

类图如下

源码:

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int year = 0;
int month = 0;
int day = 0;

int choice = input.nextInt();

if (choice == 1) { // test getNextNDays method
int m = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

DateUtil date = new DateUtil(day, month, year);

if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}

m = input.nextInt();

if (m < 0) {
System.out.println("Wrong Format");
System.exit(0);
}

System.out.print(date.getYear().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " next " + m + " days is:");
System.out.println(date.getNextNDays(m).showDate());
} else if (choice == 2) {
int n = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

DateUtil date = new DateUtil(day, month, year);

if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}

n = input.nextInt();

if (n < 0) {
System.out.println("Wrong Format");
System.exit(0);
}

System.out.print(
date.getYear().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " previous " + n + " days is:");
System.out.println(date.getPreviousNDays(n).showDate());
} else if (choice == 3) { //test getDaysofDates method
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

int anotherYear = Integer.parseInt(input.next());
int anotherMonth = Integer.parseInt(input.next());
int anotherDay = Integer.parseInt(input.next());

DateUtil fromDate = new DateUtil(day, month, year);
DateUtil toDate = new DateUtil(anotherDay, anotherMonth, anotherYear);

if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
System.out.println("The days between " + fromDate.showDate() +
" and " + toDate.showDate() + " are:"
+ fromDate.getDaysofDates(toDate));
} else {
System.out.println("Wrong Format");
System.exit(0);
}
}
else{
System.out.println("Wrong Format");
System.exit(0);
}
}
}

class Day {
private int value;
public Day()
{
}
public Day(int value)
{
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}

public void dayIncrement()
{
value = value + 1;
}
public void dayReduction()
{
value = value - 1;
}

}
class Month {
private int value ;
public Month()
{

}
public Month(int value)
{
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public void monthIncrement()
{
value = value + 1;
}
public void monthReduction()
{
value = value - 1;
}
public void resetMin()
{
value = 1 ;
}
public void resetMax()
{
value = 12;
}
public boolean validate()
{
if(value<=12&&value>=1) return true;
else return false;
}
}
class Year{
private int value;
public Year()
{

}
public Year(int value)
{
this.value = value;
}
public int getValue()
{
return 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(value>=1820&&value<=2020) return true;
else return false;
}
public void yearIncrement()
{
value = value + 1;
}
public void yearReduction()
{
value = value - 1;
}

}class DateUtil {
private Day day= new Day();
private Month month =new Month();
private Year year=new Year();
private int mon_maxnum[] = {31,28,31,30,31,30,31,31,30,31,30,31};
public DateUtil()
{

}

public DateUtil(int d,int m,int y)
{
day.setValue(d);
month.setValue(m);
year.setValue(y);
}

public Month getMonth() {
return month;
}

public void setMonth(Month month) {
this.month = month;
}

public Year getYear() {
return year;
}

public void setYear(Year year) {
this.year = year;
}

public Day getDay() {
return day;
}

public void setDay(Day day) {
this.day = day;
}
public void setDayMin()
{
day.setValue(1);
}
public void setDayMax()
{
if(year.isLeapYear() == true) mon_maxnum[1] = 29;
else mon_maxnum[1] = 28;
day.setValue(mon_maxnum[month.getValue()-1]);
}
public boolean checkInputValidity()
{if(year.isLeapYear() == true) mon_maxnum[1] = 29;
else mon_maxnum[1] = 28;
if(month.validate() == false) return false;
else {if(year.validate() == true&&month.validate() == true&&day.getValue() >= 1&&day.getValue() <= mon_maxnum[month.getValue()-1]) return true;
else return false;}
}
public boolean compareDates(DateUtil date)
{
if(year.getValue() > date.year.getValue()) return true;
else if(year.getValue() == date.year.getValue()&&month.getValue() > date.month.getValue()) return true;
else if(year.getValue() == date.year.getValue()&&month.getValue() > date.month.getValue()&&day.getValue() > date.day.getValue()) return true;
else return false;
}
public boolean equalTwoDates(DateUtil date)
{
if(year.getValue() == date.year.getValue()&&month.getValue() > date.month.getValue()&&day.getValue() == date.day.getValue()) return true;
else return false;
}
public String showDate()
{
return year.getValue()+"-"+month.getValue()+"-"+day.getValue();
}
public DateUtil getNextNDays(int n)
{if(year.isLeapYear() == true) mon_maxnum[1] = 29;
else mon_maxnum[1] = 28;
for(;n > 0;n--)
{
day.dayIncrement();
if(day.getValue()>mon_maxnum[month.getValue() - 1]) {
day.setValue(1);
month.monthIncrement();
if(month.getValue() > 12) {
month.setValue(1);
year.yearIncrement();
if(year.isLeapYear() == true) mon_maxnum[1] = 29;
else mon_maxnum[1] = 28;
}
}
}
return this;
}
public DateUtil getPreviousNDays(int n)
{if(year.isLeapYear()==true) mon_maxnum[1]=29;
else mon_maxnum[1]=28;
for(;n>0;n--)
{
day.dayReduction();
if(day.getValue()<1){
month.monthReduction();
if(month.getValue()<1)
{ month.setValue(12);
day.setValue(mon_maxnum[month.getValue()-1]);
year.yearReduction();
if(year.isLeapYear()==true) mon_maxnum[1]=29;
else mon_maxnum[1]=28;
}
else day.setValue(mon_maxnum[month.getValue()-1]);

}
}
return this;
}
public int getDaysofDates(DateUtil date)
{int i,s = 0 ;
DateUtil a,b;
a=this;
b=date;
if(a.equalTwoDates(b) == true) return 0;
else {
if(a.compareDates(b) == false) {
a = date;
b = this;
}
for(i = b.year.getValue();i<a.year.getValue();i++)
{
s=s+365;
if((i % 4 == 0&&i % 100 !=0)||(i % 400 == 0))
s = s + 1;
}
if(a.year.isLeapYear() == true) mon_maxnum[1] = 29;
else mon_maxnum[1] = 28;
for(i = 0;i < a.month.getValue() - 1;i++)
{
s = s + mon_maxnum[i];
}
s = s + a.day.getValue();
if(b.year.isLeapYear() == true) mon_maxnum[1] = 29;
else mon_maxnum[1] = 28;
for(i = 0;i < b.month.getValue() - 1;i++)
{
s = s - mon_maxnum[i];
}
s = s - b.day.getValue();
return s;
}
}

}

 

 解释:该题聚合的对象和上面那题所聚合的对象不一样,该题所聚合的对象是DateUtil分别和Year类,Month类,Day类三者之间,这样就使这几个类之间给联系了起来。

心得:同一个题目有不同的聚合方法,在考虑问题的时候应该从多个角度思考问题,这样往往会得出不同的解决方案。

题目六:7-1

要求:

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

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

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

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

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

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

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

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

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

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

删除记录格式:序号 delete

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

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

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

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

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

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

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

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

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

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

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

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

Dish {

String name;//菜品名称

int unit_price; //单价

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

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

Menu {

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

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

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

}

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

Record {

int orderNum;//序号

Dish d;//菜品\\

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

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

}

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

Order {

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

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

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

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

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

}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

例如:麻婆豆腐 9 T

菜价的计算方法:

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

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

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

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

输入格式:

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

菜品记录格式:

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

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

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

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

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

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

输出格式:

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

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

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

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

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

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

类图如下:

 源码如下:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Main {

public static void main(String[] args) {
// TODO 自动生成的方法存根
Scanner input = new Scanner(System.in);
Menu menu = new Menu();
String a = input.nextLine();
String[] number = a.split(" ");
String first = number[0];
String second = number[1];
String third = null;
String fourth = null;
int s = 0;
int length = 4;
int o[] = new int [5];
int e = 0;
int t = 0;
int fenshu = 0;
Numbe numbe = new Numbe();
int u = 0;
Boolean g =true;
int price = 0;
int an = 4;
int po = 0;
int teshe = 0;
int mo;
String regex; //= "^[\\u4e00-\\u9fa5]{1,}\\s([0-9]|[1-9][0-9]|[1-2][0-9][0-9])(\\sT$){0,1}";
Pattern pattern; //= Pattern.compile(regex);
Matcher m; //= pattern.matcher(a);
// if(!m.matches()) {
//System.out.println("wrong format");
//[\\u4e00-\\u9fa5]{1,}
// }
if(number.length == 3) third = number[2];
while(!first.equals("table")){
int i = 0;
if(numbe.isInteger(second)==true)
{Integer a1 = Integer.parseInt(second);
if(a1 >= 300||a1 <= 0) {
System.out.println(first+" price out of range "+a1);

}
if(menu.searthDish(first)==null)
menu.addDish(first, a1);
else System.out.println("wrong format");
if(third != null) menu.dishs[i].setDish(true);
i = i + 1;}
else System.out.println("wrong format");
a = input.nextLine();
String []number1 = a.split(" ");
if(number1.length!=4) {regex = "^[\\u4e00-\\u9fa5]{1,}\\s([0-9]|[1-9][0-9]|[1-2][0-9][0-9])(\\sT$){0,1}";
pattern = Pattern.compile(regex);
m = pattern.matcher(a);
if(!m.matches()) {
System.out.println("wrong format");

}
}
first = number1[0];
second = number1[1];
if(number1.length == 3) third = number1[2];
else if(number1.length == 4) {
third = number1[2];
fourth = number1[3];
}
if(number1.length<2||number1.length>4)
{
System.out.println("wrong format");
return;
}
if(number1.length>=4)
{
regex = "^table\\s([1-9]|[1-9][0-9])\\s(202(2|3)/([1-9]||1[0-2])/([1-9]|[1-3][0-9]))\\s([1-9]|1[0-9]|2[0-4])/(0[0-9]|[1-5][0-9]|60)/(0[0-9])|([1-5][0-9])|(60)$";
pattern = Pattern.compile(regex);
m = pattern.matcher(a);
if(!m.matches()) {
System.out.println("wrong format");
return;
}
}
if(number1.length!=4) {regex = "^[\\u4e00-\\u9fa5]{1,}\\s([0-9]|[1-9][0-9]|[1-2][0-9][0-9])(\\sT$){0,1}";
pattern = Pattern.compile(regex);
m = pattern.matcher(a);
if(!m.matches()) {
System.out.println("wrong format");

}
}
t = number1.length;


}
if(t != 4)
{
System.out.println("wrong format");
return;
}
System.out.println(first+" "+second+": ");
Integer a1 = Integer.parseInt(second);
if(a1 < 1||a1 > 55)
{
System.out.println("wrong format");
return;
}
String []number2 = third.split("/");
String []number3 = fourth.split("/");
Integer year = Integer.parseInt(number2[0]);
Integer month = Integer.parseInt(number2[1]);
Integer day = Integer.parseInt(number2[2]);
Integer hour = Integer.parseInt(number3[0]);
Integer minute = Integer.parseInt(number3[1]);
Integer se = Integer.parseInt(number3[2]);

Date date = new Date();
// cal.setTime(date);
date.setYear(year-1900);
date.setMonth(month-1);
date.setDate(day);


Time time =new Time();
if(time.legallyDate(year, month, day)==false)
{
System.out.println(a1+"date error");

}
else if(year < 2022||year > 2023) System.out.println("not a valid time period");

//else
String number4 = number2[0] + number2[1] + number2[2] + number3[0] + number3[1] + number3[2];
regex = "[0-9]{4}[0-9]{1,2}[0-9]{1,2}[0-9]{1,2}[0-9]{1,2}[0-9]{1,2}";
pattern = Pattern.compile(regex);
m = pattern.matcher(number4);
if(!m.matches()) {
System.out.println("wrong format");
return;
}
String order = input.nextLine();
Order order1 =new Order(menu);
String []number5 = order.split("\\s+");

if(number5.length == 4)
{
String word = number5[0];
String word1 = number5[1];
Integer ordernumber = Integer.parseInt(word);
u =ordernumber;
String word2 = number5[2];
String word3 = number5[3];
Integer num = Integer.parseInt(word3);
fenshu = fenshu + num;
while(!word.equals("end")) {
//if(number5.length == 4)
;

Integer portion = Integer.parseInt(word2);
num = Integer.parseInt(word3);
//fenshu = fenshu + num;
if(number5.length == 4&&length == 4) {
if(portion <= 3&&num > 15)
System.out.println(ordernumber + " num out of range "+num);
else if(portion > 3&&num <= 15)
System.out.println(ordernumber+" portion out of range "+portion);
else { if(g==true)
{
if(menu.searthDish(word1)!=null)
{order1.addARecord(ordernumber,word1, portion, num);
System.out.println(word+" "+word1+" "+menu.searthDish(word1).getPrice(portion)*num);
po = po + menu.searthDish(word1).getPrice(portion)*num;
if(menu.searthDish(word1).getDish()==true)
teshe = teshe + menu.searthDish(word1).getPrice(portion)*num;}
else System.out.println(word1+" does not exist");}
}

}
regex = "^([1-9]|[1-4][0-9]|5[0-5])\\s[\\u4e00-\\u9fa5]{1,}\\s([0-3])\\s([0-9]|[1-5][0-9])$";
pattern = Pattern.compile(regex);
m = pattern.matcher(order);
if((an == 4 && number5.length == 4)&&!m.matches()) {
System.out.println("wrong format");
//return;
}

order = input.nextLine();
String []number6 = order.split("\\s+");
an = number.length;
word = number6[0];
if(numbe.isInteger(word))
ordernumber = Integer.parseInt(word);
if(number6.length == 4)
{ word1 = number6[1];
word2 = number6[2];
word3 = number6[3];
if(ordernumber<=u) {System.out.println("record serial number sequence error");g = false;}
else {u = ordernumber;g = true;}
}
else if(number6.length == 2)
{
word1 = number6[1];
if(number6.length == 2&& word1.equals("delete"))
{
Integer du = Integer.parseInt(word);
o[e] = du;
if(e == 0) order1.delARecordByOrderNum(du);
if(e>=1&&o[e]==o[e-1]) order1.delARecordByOrderNum(du);
if(e>=1&&o[e]-o[e-1]==0) {
System.out.println("deduplication"+" "+o[e]);
}
e = e +1;

}
if(number6.length == 2&& !word1.equals("delete"))
{
System.out.println("invalid dish");
}
}
if(number6.length != 4&&number6.length!=1&&number6.length!=2)
{
System.out.println("wrong format");
//break;
}
length = number6.length;

}
}
else if(number5.length == 2) {
System.out.println("invalid dish");
String orderd = input.nextLine();
String []number6 = orderd.split("\\s+");
if(number6.length == 4)
{
String word = number6[0];
String word1 = number6[1];
String word2 = number6[2];
String word3 = number6[3];
//Order order1 =new Order(menu);

while(!word.equals("end")) {
//if(number5.length == 4)
Integer ordernumber = Integer.parseInt(word);
Integer portion = Integer.parseInt(word2);
Integer num = Integer.parseInt(word3);
if(number6.length == 4&&length == 4) {
if(portion <= 3&&num > 15)
System.out.println(ordernumber + " num out of range "+num);
else if(portion > 3&&num <= 15)
System.out.println(ordernumber+" portion out of range "+portion);
else { if(menu.searthDish(word1)!=null)
{order1.addARecord(ordernumber,word1, portion, num);
System.out.println(word+" "+word1+" "+menu.searthDish(word1).getPrice(portion)*num);
po = po + menu.searthDish(word1).getPrice(portion)*num;
if(menu.searthDish(word1).getDish()==true)
teshe = teshe + menu.searthDish(word1).getPrice(portion)*num;}
else System.out.println(word1+" does not exist");

}

}


order = input.nextLine();
String []number7 = order.split("\\s+");
word = number7[0];

if(number7.length == 4)
{ word1 = number7[1];
word2 = number7[2];
word3 = number7[3];}
if(number7.length == 2)
{
word1 = number7[1];
}
if(number7.length == 2&& word1.equals("delete"))
{
Integer du = Integer.parseInt(word);
o[e] = du;
if(e == 0) order1.delARecordByOrderNum(du);
else if(e>=1&&o[e]!=o[e-1]) order1.delARecordByOrderNum(du);

else if(e>=1&&o[e]-o[e-1]==0) {

System.out.println("deduplication"+" "+o[e]);
}
e = e +1;

}
if(number7.length == 2&& !word1.equals("delete"))
{
System.out.println("invalid dish");
}
if(number7.length != 4&&number7.length!=1&&number7.length!=2)
{
System.out.println("wrong format");
//break;
}

length = number7.length;

}
}
}






Xingqi xingqi = new Xingqi();

if((xingqi.getWeekOfDate(date).equals("星期二")||xingqi.getWeekOfDate(date).equals("星期一")||xingqi.getWeekOfDate(date).equals("星期三")||xingqi.getWeekOfDate(date).equals("星期四")||xingqi.getWeekOfDate(date).equals("星期五"))&&hour*3600 + minute*60 + se >= 17 * 3600&&hour * 3600+minute*60+se <= 20*3600+30*60)
{ s = order1.getTotalPrice();
System.out.print("table " + second + ": " + order1.getTotalPrice1());
if((s*0.8+teshe*0.7)-(int)(s*0.8+teshe*0.7) >=0.5)
mo = (int)(s*0.8+teshe*0.7+1);
else mo = (int)(s*0.8+teshe*0.7);
System.out.print(" "+mo);
}
else if(xingqi.getWeekOfDate(date).equals("星期二")||xingqi.getWeekOfDate(date).equals("星期一")||xingqi.getWeekOfDate(date).equals("星期三")||xingqi.getWeekOfDate(date).equals("星期四")||xingqi.getWeekOfDate(date).equals("星期五")&&hour*3600+minute*60+se>=10*3600+30*60&&hour*3600+minute*60+se <= 14*3600+30*60)
{
//s = (int) ();
s = order1.getTotalPrice();
System.out.print("table " + second + ": " + order1.getTotalPrice1());
if((s*0.6+teshe*0.7)-(int)(s*0.6+teshe*0.7) >=0.5)
mo = (int)(s*0.6+teshe*0.7+1);
else mo = (int)(s*0.6+teshe*0.7);
System.out.print(" "+mo);

}
else if((xingqi.getWeekOfDate(date).equals("星期六")||xingqi.getWeekOfDate(date).equals("星期天"))&&hour*3600+minute*60+se >= 9*3600+30*60 &&hour*3600+minute*60+se <= 21*3600 +30*60 )
{
s = order1.getTotalPrice();
System.out.print("table " + second + ": " + order1.getTotalPrice1());
System.out.print(" "+(s+teshe));
}
else System.out.println("table " + second + "out of opening hours");
}

}
class Dish {
private String name;
private int unit_price;
private Boolean dish = false;
int getPrice(int portion)//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
{
if(portion == 1) return unit_price;
else if(portion == 2) {
if(1.5 * unit_price-(int)(1.5 * unit_price)>=0.5) return (int)(1.5 * unit_price+1);
else return (int)(1.5 * unit_price);
}
else if(portion == 3) return 3*unit_price;
return 0;
}
public Dish() {
super();
// TODO 自动生成的构造函数存根
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean getDish() {
return dish;
}
public void setDish(Boolean dish) {
this.dish = dish;
}
public int getUnit_price() {
return unit_price;
}
public void setUnit_price(int unit_price) {
this.unit_price = unit_price;
}

}
class Order {

Record[] records = new Record[10];//保存订单上每一道的记录
int k =0;

private Menu menu;

public Order(Menu menu)
{
this.menu = menu;
}
public void addARecord(int orderNum,String dishName,int portion,int num)//添加一条菜品信息到订单中。
{

records[k] = new Record();
records[k].setD(menu.searthDish(dishName));
records[k].setOrderNum(orderNum);
records[k].setPortion(portion);
records[k].setNum(num);
k = k + 1;
//return records[k];

}
public int getTotalPrice( )//计算订单的总价
{
int i = 0,s = 0;

while(i<k) {
if(records[i].getD().getDish()==false)
{
s = s +records[i].getPrice();
}


i = i + 1;
}


return s;
}
public int getTotalPrice1( )//计算订单的总价
{
int i = 0,s = 0;

while(i<k) {

s = s +records[i].getPrice();


i = i + 1;
}

return s;
}

void delARecordByOrderNum(int orderNum)//根据序号删除一条记录
{int i = 0,t = 0;

for(i = 0;i < k;i ++)
{
if(records[i].getOrderNum() == orderNum)
{
t = i;
}
}
for(i = t;i < k-1;i ++ )
{
records[i]=records[i+1];
}
k = k - 1;
}

Record findRecordByNum(int orderNum)//根据序号查找一条记录
{
int i = 0;
for(i = 0;i < records.length;i ++)
{
if(records[i].getOrderNum() == orderNum)
return records[i];
}
return null;
}


}
class Record {
private int orderNum;//序号
private int num;//数量
public int getNum() {
return num;
}

public void setNum(int num) {
this.num = num;
}

private Dish d ;//菜品\\

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

int getPrice()//计价
{
return d.getPrice(portion)*this.num;
}

public int getOrderNum() {
return orderNum;
}

public void setOrderNum(int orderNum) {
this.orderNum = orderNum;
}

public Dish getD() {
return d;
}

public void setD(Dish d) {
this.d = d;
}

public int getPortion() {
return portion;
}

public void setPortion(int portion) {
this.portion = portion;
}
}
class Time {
public static boolean legallyDate(int year,int month,int day){

 

//定义一个合法月份的天数数组,校验天数是否合法
int [] arr = {0,31,28,31,30,31,30,31,31,30,31,30,31};
//由于平年二月28天,闰年二月29天,需额外赋值
if((year % 4 == 0 && year %100 != 0) || year % 400 == 0){
arr[2] = 29; //闰年
}else{
arr[2] = 28; //平年
}

//校验月份是否合法,0<month<13
if(month >0 && month < 13){
if(day <=arr[month] && day>0){
return true;
}
}
return false;
}

public Time() {
super();
// TODO 自动生成的构造函数存根
}
}
class Xingqi {
public static String getWeekOfDate(Date dt) {
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}

public Xingqi() {
super();
// TODO 自动生成的构造函数存根
}
}
class Numbe {

public static boolean isInteger(String value) {
try {
Integer.parseInt(value);
return true;
} catch (NumberFormatException e) {
return false;
}
}
}
class Menu {
//菜品数组,保存所有菜品信息
Dish[] dishs = new Dish[10];
int k =0;
public Dish searthDish(String dishName)//根据菜名在菜谱中查找菜品信息,返回Dish对象。
{
int i = 0;
//dishs[i] = new Dish();
while(i<k) {
if(dishs[i].getName().equals(dishName) )
return dishs[i];
i = i + 1;
}
return null;

}
public void addDish(String dishName,int unit_price)
{
dishs[k] = new Dish();
dishs[k].setName(dishName);
dishs[k].setUnit_price(unit_price);
k = k + 1;
//return dishs[k];

}

}

 解释:一个Main函数把所以类都给关联起来了,但是这份代码没有考虑多个桌子点菜和代点菜这两个要求,但是其他要求我测试大部分都满足,然而就是过不了测试点,6个样例我全部都能通过。

心得:自己了解的东西还是太少了,碰到难题就想要逃避,没有仔细思考类与类之间的关系。

 三,踩坑心得

踩坑一:习题四中的第三题,在一开始使用ArrayList构造了一个动态数组,再判断数组里面是否有这个元素,如果没有这个元素,就把这个元素加入到动态数组中,但是使用for循环和if判断语句超时了

 

import java.util.Scanner;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<Integer> num=new ArrayList<>();
int a=input.nextInt();
// int []b=new int[a];
int i,p;
for(i=0;i<a;i++)
{
p=input.nextInt();
if(!num.contains(p))
{
num.add(p);
}
}
for(i=0;i<num.size()-1;i++)
{System.out.print(num.get(i)+" ");}
System.out.print(num.get(num.size()-1));

}
}

后面改用Linkhashset创造动态数组,它会自动去除数组中已有的元素,这样就不用判断,解决了超时的问题。

 

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
LinkedHashSet<Integer> h = new LinkedHashSet<Integer>();
int a = input.nextInt();
int i,k,p = 0;
for(i = 0;i < a;i++)
{
k = input.nextInt();
h.add(k);
}
for(Integer s:h)
{
if(p == 0)
{
System.out.print(s);
p = 1;
}
else System.out.print(" "+s);
}
}
}

踩坑二:习题五的最后一题,算两个日期之间相差多少天,一开始使用和7-5一样的方法算两个日期相差多少天,就是把两个日期分别加到最后一天看所需要的天数,然后两者相减就是两个日期相差的天数,但是再7-6里面用答案错误了,但是我自己测试没问题,就是过不了测试点。

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int year = 0;
int month = 0;
int day = 0;

int choice = input.nextInt();

if (choice == 1) { // test getNextNDays method
int m = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

DateUtil date = new DateUtil(day, month, year);

if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}

m = input.nextInt();

if (m < 0) {
System.out.println("Wrong Format");
System.exit(0);
}

System.out.print(date.getYear().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " next " + m + " days is:");
System.out.println(date.getNextNDays(m).showDate());
} else if (choice == 2) {
int n = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

DateUtil date = new DateUtil(day, month, year);

if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}

n = input.nextInt();

if (n < 0) {
System.out.println("Wrong Format");
System.exit(0);
}

System.out.print(
date.getYear().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " previous " + n + " days is:");
System.out.println(date.getPreviousNDays(n).showDate());
} else if (choice == 3) { //test getDaysofDates method
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

int anotherYear = Integer.parseInt(input.next());
int anotherMonth = Integer.parseInt(input.next());
int anotherDay = Integer.parseInt(input.next());

DateUtil fromDate = new DateUtil(day, month, year);
DateUtil toDate = new DateUtil(anotherDay, anotherMonth, anotherYear);

if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
System.out.println("The days between " + fromDate.showDate() +
" and " + toDate.showDate() + " are:"
+ fromDate.getDaysofDates(toDate));
} else {
System.out.println("Wrong Format");
System.exit(0);
}
}
else{
System.out.println("Wrong Format");
System.exit(0);
}
}
}

class Day {
private int value;
public Day()
{
}
public Day(int value)
{
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}

public void dayIncrement()
{
value = value + 1;
}
public void dayReduction()
{
value = value - 1;
}

}
class Month {
private int value ;
public Month()
{

}
public Month(int value)
{
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public void monthIncrement()
{
value = value + 1;
}
public void monthReduction()
{
value = value - 1;
}
public void resetMin()
{
value = 1 ;
}
public void resetMax()
{
value = 12;
}
public boolean validate()
{
if(value<=12&&value>=1) return true;
else return false;
}
}
class Year{
private int value;
public Year()
{

}
public Year(int value)
{
this.value = value;
}
public int getValue()
{
return 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(value>=1820&&value<=2020) return true;
else return false;
}
public void yearIncrement()
{
value = value + 1;
}
public void yearReduction()
{
value = value - 1;
}

}class DateUtil {
private Day day= new Day();
private Month month =new Month();
private Year year=new Year();
private int mon_maxnum[] = {31,28,31,30,31,30,31,31,30,31,30,31};
public DateUtil()
{

}

public DateUtil(int d,int m,int y)
{
day.setValue(d);
month.setValue(m);
year.setValue(y);
}

public Month getMonth() {
return month;
}

public void setMonth(Month month) {
this.month = month;
}

public Year getYear() {
return year;
}

public void setYear(Year year) {
this.year = year;
}

public Day getDay() {
return day;
}

public void setDay(Day day) {
this.day = day;
}
public void setDayMin()
{
day.setValue(1);
}
public void setDayMax()
{
if(year.isLeapYear() == true) mon_maxnum[1] = 29;
else mon_maxnum[1] = 28;
day.setValue(mon_maxnum[month.getValue()-1]);
}
public boolean checkInputValidity()
{if(year.isLeapYear() == true) mon_maxnum[1] = 29;
else mon_maxnum[1] = 28;
if(month.validate() == false) return false;
else {if(year.validate() == true&&month.validate() == true&&day.getValue() >= 1&&day.getValue() <= mon_maxnum[month.getValue()-1]) return true;
else return false;}
}
public boolean compareDates(DateUtil date)
{
if(year.getValue() > date.year.getValue()) return true;
else if(year.getValue() == date.year.getValue()&&month.getValue() > date.month.getValue()) return true;
else if(year.getValue() == date.year.getValue()&&month.getValue() > date.month.getValue()&&day.getValue() > date.day.getValue()) return true;
else return false;
}
public boolean equalTwoDates(DateUtil date)
{
if(year.getValue() == date.year.getValue()&&month.getValue() > date.month.getValue()&&day.getValue() == date.day.getValue()) return true;
else return false;
}
public String showDate()
{
return year.getValue()+"-"+month.getValue()+"-"+day.getValue();
}
public DateUtil getNextNDays(int n)
{if(year.isLeapYear() == true) mon_maxnum[1] = 29;
else mon_maxnum[1] = 28;
for(;n > 0;n--)
{
day.dayIncrement();
if(day.getValue()>mon_maxnum[month.getValue() - 1]) {
day.setValue(1);
month.monthIncrement();
if(month.getValue() > 12) {
month.setValue(1);
year.yearIncrement();
if(year.isLeapYear() == true) mon_maxnum[1] = 29;
else mon_maxnum[1] = 28;
}
}
}
return this;
}
public DateUtil getPreviousNDays(int n)
{if(year.isLeapYear()==true) mon_maxnum[1]=29;
else mon_maxnum[1]=28;
for(;n>0;n--)
{
day.dayReduction();
if(day.getValue()<1){
month.monthReduction();
if(month.getValue()<1)
{ month.setValue(12);
day.setValue(mon_maxnum[month.getValue()-1]);
year.yearReduction();
if(year.isLeapYear()==true) mon_maxnum[1]=29;
else mon_maxnum[1]=28;
}
else day.setValue(mon_maxnum[month.getValue()-1]);

}
}
return this;
}
public int getDaysofDates(DateUtil date)
{int i,s = 0 ;
DateUtil a,b;
a=this;
b=date;
if(a.equalTwoDates(b) == true) return 0;
else {
if(a.compareDates(b) == false) {
a = date;
b = this;
}
}
for(i = b.year.getValue();i<year.getValue();i++)
{
s=s+365;
if((i % 4 == 0&&i % 100 !=0)||(i % 400 == 0))
s = s + 1;
}
if(a.year.isLeapYear() == true) mon_maxnum[1] = 29;
else mon_maxnum[1] = 28;
for(i = 0;i < a.month.getValue() - 1;i++)
{
s = s + mon_maxnum[i];
}
s = s + a.day.getValue();
if(b.year.isLeapYear() == true) mon_maxnum[1] = 29;
else mon_maxnum[1] = 28;
for(i = 0;i < b.month.getValue() - 1;i++)
{
s = s - mon_maxnum[i];
}
s = s - b.year.getValue();
return s;
}
}

后面自己新写了一个方法来求两个日期之间的天数

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int year = 0;
int month = 0;
int day = 0;

int choice = input.nextInt();

if (choice == 1) { // test getNextNDays method
int m = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

DateUtil date = new DateUtil(day, month, year);

if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}

m = input.nextInt();

if (m < 0) {
System.out.println("Wrong Format");
System.exit(0);
}

System.out.print(date.getYear().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " next " + m + " days is:");
System.out.println(date.getNextNDays(m).showDate());
} else if (choice == 2) {
int n = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

DateUtil date = new DateUtil(day, month, year);

if (!date.checkInputValidity()) {
System.out.println("Wrong Format");
System.exit(0);
}

n = input.nextInt();

if (n < 0) {
System.out.println("Wrong Format");
System.exit(0);
}

System.out.print(
date.getYear().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " previous " + n + " days is:");
System.out.println(date.getPreviousNDays(n).showDate());
} else if (choice == 3) { //test getDaysofDates method
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());

int anotherYear = Integer.parseInt(input.next());
int anotherMonth = Integer.parseInt(input.next());
int anotherDay = Integer.parseInt(input.next());

DateUtil fromDate = new DateUtil(day, month, year);
DateUtil toDate = new DateUtil(anotherDay, anotherMonth, anotherYear);

if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
System.out.println("The days between " + fromDate.showDate() +
" and " + toDate.showDate() + " are:"
+ fromDate.getDaysofDates(toDate));
} else {
System.out.println("Wrong Format");
System.exit(0);
}
}
else{
System.out.println("Wrong Format");
System.exit(0);
}
}
}

class Day {
private int value;
public Day()
{
}
public Day(int value)
{
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}

public void dayIncrement()
{
value = value + 1;
}
public void dayReduction()
{
value = value - 1;
}

}
class Month {
private int value ;
public Month()
{

}
public Month(int value)
{
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public void monthIncrement()
{
value = value + 1;
}
public void monthReduction()
{
value = value - 1;
}
public void resetMin()
{
value = 1 ;
}
public void resetMax()
{
value = 12;
}
public boolean validate()
{
if(value<=12&&value>=1) return true;
else return false;
}
}
class Year{
private int value;
public Year()
{

}
public Year(int value)
{
this.value = value;
}
public int getValue()
{
return 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(value>=1820&&value<=2020) return true;
else return false;
}
public void yearIncrement()
{
value = value + 1;
}
public void yearReduction()
{
value = value - 1;
}

}class DateUtil {
private Day day= new Day();
private Month month =new Month();
private Year year=new Year();
private int mon_maxnum[] = {31,28,31,30,31,30,31,31,30,31,30,31};
public DateUtil()
{

}

public DateUtil(int d,int m,int y)
{
day.setValue(d);
month.setValue(m);
year.setValue(y);
}

public Month getMonth() {
return month;
}

public void setMonth(Month month) {
this.month = month;
}

public Year getYear() {
return year;
}

public void setYear(Year year) {
this.year = year;
}

public Day getDay() {
return day;
}

public void setDay(Day day) {
this.day = day;
}
public void setDayMin()
{
day.setValue(1);
}
public void setDayMax()
{
if(year.isLeapYear() == true) mon_maxnum[1] = 29;
else mon_maxnum[1] = 28;
day.setValue(mon_maxnum[month.getValue()-1]);
}
public boolean checkInputValidity()
{if(year.isLeapYear() == true) mon_maxnum[1] = 29;
else mon_maxnum[1] = 28;
if(month.validate() == false) return false;
else {if(year.validate() == true&&month.validate() == true&&day.getValue() >= 1&&day.getValue() <= mon_maxnum[month.getValue()-1]) return true;
else return false;}
}
public boolean compareDates(DateUtil date)
{
if(year.getValue() > date.year.getValue()) return true;
else if(year.getValue() == date.year.getValue()&&month.getValue() > date.month.getValue()) return true;
else if(year.getValue() == date.year.getValue()&&month.getValue() > date.month.getValue()&&day.getValue() > date.day.getValue()) return true;
else return false;
}
public boolean equalTwoDates(DateUtil date)
{
if(year.getValue() == date.year.getValue()&&month.getValue() > date.month.getValue()&&day.getValue() == date.day.getValue()) return true;
else return false;
}
public String showDate()
{
return year.getValue()+"-"+month.getValue()+"-"+day.getValue();
}
public DateUtil getNextNDays(int n)
{if(year.isLeapYear() == true) mon_maxnum[1] = 29;
else mon_maxnum[1] = 28;
for(;n > 0;n--)
{
day.dayIncrement();
if(day.getValue()>mon_maxnum[month.getValue() - 1]) {
day.setValue(1);
month.monthIncrement();
if(month.getValue() > 12) {
month.setValue(1);
year.yearIncrement();
if(year.isLeapYear() == true) mon_maxnum[1] = 29;
else mon_maxnum[1] = 28;
}
}
}
return this;
}
public DateUtil getPreviousNDays(int n)
{if(year.isLeapYear()==true) mon_maxnum[1]=29;
else mon_maxnum[1]=28;
for(;n>0;n--)
{
day.dayReduction();
if(day.getValue()<1){
month.monthReduction();
if(month.getValue()<1)
{ month.setValue(12);
day.setValue(mon_maxnum[month.getValue()-1]);
year.yearReduction();
if(year.isLeapYear()==true) mon_maxnum[1]=29;
else mon_maxnum[1]=28;
}
else day.setValue(mon_maxnum[month.getValue()-1]);

}
}
return this;
}
public int getDaysofDates(DateUtil date)
{int i,s = 0 ;
DateUtil a,b;
a=this;
b=date;
if(a.equalTwoDates(b) == true) return 0;
else {
if(a.compareDates(b) == false) {
a = date;
b = this;
}
for(i = b.year.getValue();i<a.year.getValue();i++)
{
s=s+365;
if((i % 4 == 0&&i % 100 !=0)||(i % 400 == 0))
s = s + 1;
}
if(a.year.isLeapYear() == true) mon_maxnum[1] = 29;
else mon_maxnum[1] = 28;
for(i = 0;i < a.month.getValue() - 1;i++)
{
s = s + mon_maxnum[i];
}
s = s + a.day.getValue();
if(b.year.isLeapYear() == true) mon_maxnum[1] = 29;
else mon_maxnum[1] = 28;
for(i = 0;i < b.month.getValue() - 1;i++)
{
s = s - mon_maxnum[i];
}
s = s - b.day.getValue();
return s;
}
}

}

 四.改进建议

 在题目集五中算两个日期相差多少天的时候因为日期是有个范围,所以我把两个日期加到范围内的最后一天,看各自需要多少天,然后相减,就是两者相差的天数,但是如果这次给的日期没有范围,我这个方法就用不了,所以可以改进,先算两个日期相差的年中有几个闰年和平年然后相加,再加上后面那年属于当年的第几天,再减上前面那年在那年属于第几天,这样就能算这两个日期相差多少天。这个方法应该是个通解,不需要考虑日期是否有范围。

 五.总结

在这三次的题目集训练中,我掌握了如何熟练的调用几个不同的类,并把他们给串联起来,除此之外,还学会了使用正则表达式来判断一行字符串是否输入正确,除此之外还学会使用了动态数组Arraylist和有关的方法,还稍微接触了Harhset,虽然接触不深,就算是开了个头,String类中的split方法也学会了使用,LocalDate类中of()、isAfter()、isBefore()、until()等方法也开始用上了,这是这三次题目集学会的,不足的是,这次有两道较难题没有做出来,感到非常可惜,这说明我在难题方面还存在很多方面的不足还需要我继续改善,除此之外,遇到困难要坚持下去不要轻易放弃,后面只会越来越难。在这里我提一点建议,老师上课讲例题时候多拿源码来讲,这样便于自己写代码时候的应用,否则每次将例题的时候听懂了,但是后面自己写的时候不知道怎么去使用,这是我的一小点建议。

标签:oo,总结,int,value,getValue,博客,month,public,day
From: https://www.cnblogs.com/22201204-cx/p/17363999.html

相关文章

  • Tool-CMake-list
    Tool-CMake-listhttps://www.visgraf.impa.br/seminar/slides/rodlima_cmake_presentation.pdfUsefultomanagelonglistofelementsElementscanbemanipulateddependingonrunningplatformUsefulforsourcefilelistsExample:set(sourcesviewer.cpp......
  • Tool-CMake-Own Finder(-I -L -l)-compiling
    Tool-CMake-OwnFinder(-I-L-l)-compilingWhatisafinderWhencompilingapieceofsoftwarewhichlinkstothird­partylibraries,weneedtoknow:Wheretofindthe.hfiles(­Iingcc)Wheretofindthelibraries(.so/.dll/.lib/.dylib/...)(­Lingcc......
  • 博客园中如何使用HTML所见即可得编辑器TinyMCE
    大纲基本内容:文本、段落规范结构:标题、大纲大量数据:表格、列表内外跳转:链接、锚点视觉媒体:图像、视频、音乐用户交流:表单、交互文本字体、大小、前景色、背景色、加粗、倾斜、下划线、中划线、清楚格式段落段落行间距、段落字间距、段落居中对其、段落居左对齐、......
  • Tool-CMake-OPTION
    Tool-CMake-OPTIONhttps://clubjuggler.livejournal.com/138364.htmlincludesacomponentAsanexample,consideraprojectthatoptionallyincludesacomponentthatcommuniatesviaUSBandallowstheuser,atcompiletime,tospecifywhethertoincludeth......
  • pta第二部分总结oop训练集05-06
    (1)前言训练集05:(题量适中,难度适中)7-5:多个类的互相调用对于日期类的整体优化,聚合初体验。7-6:对7-5的优化,加强聚合训练。训练集06:(题量少,难度大)7-1:多需求的实现。(完成的不好,编程能力还不够)(2)设计与分析7-5:类图: 源码:importjava.util.Scanner;publicclassMain{......
  • 集训总结
    集训总结前言“吹散记忆的蒲公英,散落碧空;回望过往的风景,尤存风味。”离开初中生活,总会幻想回到过去,回到以前的老师同学身边,羞怯而带有稚气。结束了本蒟蒻的第一次NOIP,第一次自己在外地参加集训。内心却也充满无法言表的激动。而今总结半个月的回忆,内心充满了话,却不知从何开......
  • Tool-CMake-How CMake simplifies the build process by Bruno Abinader
    Tool-CMake-HowCMakesimplifiesthebuildprocessbyBrunoAbinaderhttps://gitlab.kitware.com/cmake/community/-/wikis/homehttps://brunoabinader.github.io/2009/12/07/how-cmake-simplifies-the-build-process-part-1-basic-build-system/https://brunoabin......
  • ubuntu安装VMware tools
    以下是在Ubuntu上安装VMwareTools的步骤:代码操作打开VMware虚拟机并进入虚拟机控制台。单击菜单栏的“VM”菜单,选择“InstallVMwareTools”。在弹出窗口中选择“DownloadandInstall”(下载并安装)。在Ubuntu中使用管理员权限打开终端。在终端中输入以下命令,以自动将VMwareTools......
  • 安卓QQ浏览器打开新浪博客
    Update以上问题已经不是问题了,因为况哥不用新浪博客了。况哥登录新浪博客时,提示账户异常,以前的博客无法查看,更别说修改了。为了避免即使可能恢复正常后再次出现这种问题,况哥决定直接弃用。 ......
  • springboot 的三种启动方式
    1、常规操作,都是通过main方法启动了;2、部署到服务器上,一般都是通过java-jarxxxx.jar包的方式启动了;3、第三种是没有main方法的时候,本地想启动,可以通过maven的运行命令来启动,cmd定位到当前项目所在的目录,如果是微服务有很多个模块,定位到自己的模块位置,然后使用:mvnspring......