pta第二部分总结oop训练集05-06
(1)前言
训练集05:(题量适中,难度适中)
7-5:多个类的互相调用对于日期类的整体优化,聚合初体验。
7-6:对7-5 的优化,加强聚合训练。
训练集06:(题量少,难度大)
7-1:多需求的实现。
(完成的不好,编程能力还不够)
(2)设计与分析
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(year, month, day); 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.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(year, month, day); 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.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(year, month, day); DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay); 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 Year{ private int value; Year(){ } Year(int value){ this.value=value; } public int getValue(){ return value; } public void setValue(int value){ this.value=value; } public boolean isLeapYear(){ if(value%400==0||(value%4==0&&value%100!=0)){ return true; }else{ return false; } } public boolean validate(){ if(value<1900||value>2050){ return false; }else{ return true; } } public void yearIncrement(){ this.value++; } public void yearReduction(){ this.value--; } } class Month{ private int value; private Year year; Month(){ } Month(int yearValue,int monthValue){ this.year=new Year(yearValue); this.value=monthValue; } 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(){ this.value=1; } public void resetMax(){ this.value=12; } public boolean validate(){ if(value<1||value>12){ return false; }else{ return true; } } public void monthIncrement(){ this.value++; } public void monthReduction(){ this.value--; } } 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}; Day(int yearValue,int monthValue,int dayValue){ this.month=new Month(yearValue,monthValue); this.value=dayValue; } public int getValue(){ return value; } public void setValue(int value){ this.value=value; } public Month getMonth(){ return month; } public void setMonth(Month value){ this.month=value; } public void resetMin(){ month.setValue(1); } public void resetMax(){ if(month.getYear().isLeapYear()){ this.mon_maxnum[1]=29; }else{ this.mon_maxnum[1]=28; } this.value=mon_maxnum[month.getValue()-1]; } public boolean validate(){ if(value<1||value>mon_maxnum[month.getValue()-1]){ return false; }else{ return true; } } public void dayIncrement(){ this.value++; } public void dayReduction(){ this.value--; } } class DateUtil{ private Day day; DateUtil(){ } DateUtil(int y,int m,int d){ day=new Day(y,m,d); } public Day getDay(){ return day; } public void setDay(Day d){ this.day=d; } public boolean checkInputValidity(){ if(day.getMonth().getYear().validate()&&day.getMonth().validate()&&day.validate()){ return true; }else{ return false; } } 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()){ if(day.getMonth().getValue()<date.day.getMonth().getValue()){ return true; }else if(day.getMonth().getValue()==date.day.getMonth().getValue()){ if(day.getValue()<date.day.getValue()){ return true; }else{ return false; } }else{ return false; } }else{ return false; } } public boolean equalTwoDays(DateUtil date){ if(day.getValue()==date.day.getValue()&&day.getMonth().getYear().getValue()==date.day.getMonth().getYear().getValue()&&day.getMonth().getValue()==date.day.getMonth().getValue()){ return true; } else { return false; } } public String showDate(){ return day.getMonth().getYear().getValue() + "-" + day.getMonth().getValue()+ "-" + day.getValue(); } public DateUtil getNextNDays(int n){ long newday=(long)day.getValue()+(long)n; day.resetMax(); while(newday>day.getValue()){ newday-=(long)day.getValue(); day.getMonth().monthIncrement(); if(day.getMonth().validate()==false){ day.getMonth().resetMin(); day.getMonth().getYear().yearIncrement(); } day.resetMax(); } DateUtil last=new DateUtil(day.getMonth().getYear().getValue(), day.getMonth().getValue(), (int)newday); return last; } public DateUtil getPreviousNDays(int n){ int newday=day.getValue() -n; day.getMonth().monthReduction(); while(newday<=0){ day.resetMax(); newday+=day.getValue(); day.getMonth().monthReduction(); if(day.getMonth().validate()==false){ day.getMonth().resetMax(); day.getMonth().getYear().yearReduction(); } day.resetMax(); } day.getMonth().monthIncrement(); DateUtil before=new DateUtil(day.getMonth().getYear().getValue(), day.getMonth().getValue(), newday); return before; } public int getDaysofDates(DateUtil date){ int dayflag=0; if(equalTwoDays(date)==true){ dayflag=0; }else{ dayflag+=Math.abs(day.getValue()-date.day.getValue()); if(compareDates(date)==true){ //date大 while(day.getMonth().getValue()!=date.day.getMonth().getValue()||day.getMonth().getYear().getValue()!=date.day.getMonth().getYear().getValue()){ day.resetMax(); dayflag+=day.getValue(); day.getMonth().monthIncrement(); if(day.getMonth().validate()==false){ day.getMonth().resetMin(); day.getMonth().getYear().yearIncrement(); } } }else{ while(day.getMonth().getValue()!=date.day.getMonth().getValue()||day.getMonth().getYear().getValue()!=date.day.getMonth().getYear().getValue()){ date.day.resetMax(); dayflag+=date.day.getValue(); date.day.getMonth().monthIncrement(); if(date.day.getMonth().validate()==false){ date.day.getMonth().resetMin(); date.day.getMonth().getYear().yearIncrement(); } } } } return dayflag; } }
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(year, month, day); 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(year, month, day); 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(year, month, day); DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay); 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 DateUtil{ private int year; private int month; private int day; int[] mon_maxnum=new int[]{31,31,28,31,30,31,30,31,31,30,31,30,31}; DateUtil(int year,int month,int day){ this.year=year; this.month=month; this.day=day; } //检测输入的年、月、日是否合法 public boolean checkInputValidity(){ if(isLeapYear(year)==true){ if((year<1820||year>2020)||(month<1||month>12)||(day<1||day>31)){ return false; }else if(month==2&&day>29){ return false; }else{ return true; } }else{ if((year<1820||year>2020)||(month<1||month>12)||(day<1||day>31)){ return false; }else if(month==2&&day>28){ return false; }else{ return true; } } } //判断year是否为闰年 public boolean isLeapYear(int year){ if(year%400==0||(year%4==0&&year%100!=0)){ return true; }else{ return false; } } //取得year-month-day的下n天日期 public DateUtil getNextNDays(int n){ long newday=(long)this.day+(long)n; int newmonth=this.month; int newyear=this.year; if(isLeapYear(year)==true){ mon_maxnum[2]=29; while(newday>mon_maxnum[newmonth]){ newday-=mon_maxnum[newmonth]; newmonth++; if(newmonth==13){ newmonth=1; newyear++; if(isLeapYear(newyear)==false){ mon_maxnum[2]=28; } if(isLeapYear(newyear)==true){ mon_maxnum[2]=29; } } } }else{ mon_maxnum[2]=28; while(newday>mon_maxnum[newmonth]){ newday-=mon_maxnum[newmonth]; newmonth++; if(newmonth==13){ newmonth=1; newyear++; if(isLeapYear(newyear)==true){ mon_maxnum[2]=29; } if(isLeapYear(newyear)==false){ mon_maxnum[2]=28; } } } } DateUtil last=new DateUtil(newyear, newmonth, (int)newday); return last; } //取得year-month-day的前n天日期 public DateUtil getPreviousNDays(int n){ int newday=this.day-n; int newmonth=this.month; int newyear=this.year; if(isLeapYear(year)==true){ mon_maxnum[2]=29; while(newday<=0){ newday+=mon_maxnum[newmonth-1]; newmonth--; if(newmonth==0){ newmonth=12; newyear--; if(isLeapYear(newyear)==false){ mon_maxnum[2]=28; } if(isLeapYear(newyear)==true){ mon_maxnum[2]=29; } } } }else{ mon_maxnum[2]=28; while(newday<=0){ newday+=mon_maxnum[newmonth-1]; newmonth--; if(newmonth==0){ newmonth=12; newyear--; if(isLeapYear(newyear)==true){ mon_maxnum[2]=29; } if(isLeapYear(newyear)==false){ mon_maxnum[2]=28; } } } } DateUtil before=new DateUtil(newyear, newmonth, newday); return before; } //比较当前日期与date的大小(先后) public boolean compareDates(DateUtil date){ // date大返回true if(this.year<date.getYear()){ return true; }else if(this.year==date.getYear()){ if(this.month<date.getMonth()){ return true; }else if(this.month==date.getMonth()){ if(this.day<date.getDay()){ return true; }else{ return false; } }else{ return false; } }else{ return false; } } //判断两个日期是否相等 public boolean equalTwoDates(DateUtil date){ if(this.day==date.getDay()&&this.year==date.getYear()&&this.month==date.getMonth()){ return true; } else { return false; } } //求当前日期与date之间相差的天数 public int getDaysofDates(DateUtil date){ int dayflag=0; int newday=this.day; int newmonth=this.month; int newyear=this.year; int myday=date.getDay(); int mymonth=date.getMonth(); int myyear=date.getYear(); if(equalTwoDates(date)==true){ dayflag=0; }else{ if(compareDates(date)==true){//date大 while(myyear>newyear){ if(isLeapYear(myyear)==true){ dayflag+=366; }else{ dayflag+=365; } myyear--; } while(mymonth!=newmonth){ if(isLeapYear(newyear)==true){ mon_maxnum[2]=29; }else{ mon_maxnum[2]=28; } if(mymonth>newmonth){ dayflag+=mon_maxnum[mymonth-1]; mymonth--; }else{ dayflag+=mon_maxnum[newmonth-1]; newmonth--; } } dayflag=dayflag+myday-newday; }else{ while(myyear<newyear){ if(isLeapYear(newyear)==true){ dayflag+=366; }else{ dayflag+=365; } newyear--; } while(mymonth!=newmonth){ if(isLeapYear(newyear)==true){ mon_maxnum[2]=29; }else{ mon_maxnum[2]=28; } if(mymonth>newmonth){ dayflag+=mon_maxnum[mymonth-1]; mymonth--; }else{ dayflag+=mon_maxnum[newmonth-1]; newmonth--; } } dayflag=dayflag-myday+newday; } } return dayflag; } //以“year-month-day”格式返回日期值 public String showDate(){ return year + "-" + month + "-" + day; } public int getYear(){ return year; } public int getMonth(){ return month; } public int getDay(){ return day; } }
7-1:
类图:
源码:
import java.util.*; import java.util.Set; import java.util.HashSet; public class Main { public static void main(String[] args) { // TODO 自动生成的方法存根 Menu menu=new Menu(); Order order=new Order(); Scanner in=new Scanner(System.in); String Line=in.nextLine(); int i=0; while (!Line.equals("T")) { String[] thelineArray = Line.split(" "); if(thelineArray.length==3) { // 特色菜 String dishName=thelineArray[0]; int unit_price=Integer.parseInt(thelineArray[1]); // menu.addDish(dishName, unit_price, i); menu.dishs[i].dishpriceError(menu.dishs[i]); menu.ifdeleteDish(menu.dishs[i]); menu.dishs[i].setflag("T"); }else { String dishName=thelineArray[0]; int unit_price=Integer.parseInt(thelineArray[1]); menu.addDish(dishName, unit_price); menu.dishs[i].dishpriceError(menu.dishs[i]); menu.ifdeleteDish(menu.dishs[i]); menu.dishs[i].setflag("P"); } i++; Line=in.nextLine(); } i=0; Line=in.nextLine(); String[] date=Line.split(" |/"); int[] tableNum=new int[500]; Table[] table=null; if(date.length==4) { tableNum[i]=Integer.parseInt(date[1]); String year=date[2]; int[] time=new int[5]; for(int j=0,k=3;j<5;j++) { time[j]=Integer.parseInt(date[k]); k++; } table[i]=new Table(tableNum[i],year,time); table[i].iftableNumerror(tableNum[i]); table[i].deleteTable(table[i]); table[i].theTime.check(); i++; } i=0; int[] orderNum=new int[500]; while (!Line.equals("end")) { String[] lineArray = Line.split(" "); for(int j=0;j<orderNum.length;j++) { orderNum[j]=Integer.parseInt(lineArray[0]); } if (lineArray.length ==3) { int orderNum1=Integer.parseInt(lineArray[0]); String dishName = lineArray[1]; int parseInt = Integer.parseInt(lineArray[2]); int parseInt1 = Integer.parseInt(lineArray[3]); order.addARecord(orderNum1, dishName, parseInt, parseInt1); if(order.records[i].d.flag==1) { order.records[i].checkTPortion(order.records[i]); }else { order.records[i].checkPortion(order.records[i]); } } else if ("delete".equals(lineArray[1])) { int deleteNum=Integer.parseInt(lineArray[0]); int flage=0; for(int j=0;j<orderNum.length-1;j++) { if(orderNum[j]==deleteNum) { flage=1; break; }else { flage=0; } } if(flage==0) { System.out.println("delete error"); } order.delARecordByOrderNum(Integer.parseInt(lineArray[0])); } Line=in.nextLine(); } System.out.println("wrong format"); } // public static boolean theSametime(Table table1,Table table2) { // int time1; // int time2; // // if(table1.tableNum==table2.tableNum) { // if // }else { // return false; // } // } // public static void dishMerge(boolean timeResult) { // // } } class Dish { String name;//菜品名称 int unit_price; //单价 int flag; Dish(){ } Dish(String name,int unit_price){ this.name=name; this.unit_price=unit_price; } public void setflag(String T) { if(T=="T") { flag=1; }else { flag=0; } } public int getPrice(int portion){ //计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份) } int price=0; if(portion==1) { price=unit_price; }else if(portion==2) { price=(int)(1.5*unit_price); }else if (portion==3) { price=unit_price*2; } return price; } public boolean checkPrice(Dish D) { if(D.unit_price<=0||D.unit_price>=300) { return false; }else { return true; } } public void dishpriceError(Dish D) { if(!checkPrice(D)) { System.out.println(" price out of range "); } } } //菜谱类:对应菜谱,包含饭店提供的所有菜的信息。 class Menu { Dish[] dishs ;//菜品数组,保存所有菜品信息 public Menu() { } public Dish searthDish(String dishName){ //根据菜名在菜谱中查找菜品信息,返回Dish对象。 Dish D=null; for(int i=0;i<dishs.length;i++) { if(dishs[i].name==dishName) { D=dishs[i]; break; } } return D; } public Dish addDish(String dishName,int unit_price){ //添加一道菜品信息 dishs=new Dish(dishName,unit_price); return dishs; } public void ifdeleteDish(Dish D) { if(!D.checkPrice(D)) { D=null; } } } //点菜记录类:保存订单上的一道菜品记录 class Record { int orderNum;//序号 Dish d;//菜品\\ int portion;//份额(1/2/3代表小/中/大份) int num; Menu menu; Record(){ } Record(int orderNum,String dishName,int portion,int num){ this.orderNum=orderNum; this.d=menu.searthDish(dishName); this.portion=portion; this.num=num; } public int getPrice(){ //计价,计算本条记录的价格 int price=0; for(int i=0;i<num;i++) { price+=d.unit_price; } return price; } public void checkPortion(Record R) { if(R.portion!=1||R.portion!=2||R.portion!=3) { System.out.println(orderNum+" portion out of range "+portion); } } public void checkTPortion(Record R) { if(R.portion!=1||R.portion!=3) { System.out.println(orderNum+" portion out of range "+portion); } } } //订单类:保存用户点的所有菜的信息。 class Order { Record[] records;//保存订单上每一道的记录 Order(){ } int price=0; public int getTotalPrice(){ //计算订单的总价 for(int i=0;i<records.length;i++) { price+=records[i].getPrice(); } return price; } public Record addARecord(int orderNum,String dishName,int portion,int num){ //添加一条菜品信息到订单中。 records[orderNum]=new Record(orderNum,dishName,portion,num); return records[orderNum]; } public Record delARecordByOrderNum(int orderNum){ //根据序号删除一条记录 Record R=this.findRecordByNum(orderNum); R=null; return R; } public Record findRecordByNum(int orderNum){ //根据序号查找一条记录 Record R=records[orderNum]; return R; } } class Table{ int tableNum; Time theTime; Record records; Table(){ } Table(int tableNum,String year,int[] time){ this.tableNum=tableNum; theTime=new Time(year,time); } public void iftableNumerror(int tableNum) { if(!checktableNum(tableNum)) { System.out.println(" table num out of range"); } } private boolean checktableNum(int tableNum) { if(tableNum<1||tableNum>55) { return false; }else { return true; } } public void deleteTable(Table table) { if(!checktableNum(table.tableNum)) { table=null; } } } class Time{ String year; int[] time; Time(){ } Time(String year,int[] time){ this.year=year; this.time=time; } public void check() { if(!Datejudg(year,time)) { System.out.println("date error"); } if(!checkDate()) { System.out.println("not a valid time period"); } } private boolean Datejudg(String year,int[] time) { if(year.length()!=4||checkInputValidity(year,time)==false) { return false; }else{ return true; } } private boolean checkInputValidity(String year,int[] time){ int month=time[0]; int day=time[1]; int hour=time[2]; int min=time[3]; int second=time[4]; int[] mon_max=new int[] {0,31,28,31,30,31,30,31,31,30,31,30,31}; if((day>mon_max[month])||(hour<1||hour>24)||(min<1||min>60)||(second<1||second>60)) { return false; }else { return true; } } private boolean checkDate() { if((year!="2022"||year!="2023")||checkInputValidity(year,time)==false) { return false; }else { return true; } } }
(3)踩坑心得:
关于多个类的时候对类与类之间的关系的理解还要加强
(4)改进建议:
需要增强对编程的训练。
(5)总结:
7-1的pta训练没有完成的很好原因是编程能力不够,不能攻坚克难,对于编程能力的训练有懈怠。