godness4

导航

PTA总结报告2

 

相较于前一轮的三次作业,这次难了不止一点。这次的作业有了封装,正则表达式,聚合模式的使用,以及一个复杂的点菜系统。

第四次作业:

 这次的作业真的是惨不忍睹,这次的pta也是将将及格了,这次作业的7-1是第一道较为复杂的题目,由于错误的估计题目难度和自身的惰性,我没有留够时间来写第一题,导致我只写了一部分类和方法,主函数才动手没多少。

而其他几题是对算法的改进,我通过学习一些较为优秀的算法,完成了大部分内容。

第五次作业:

这次的两个计算天数的题目需要用到聚合这一模式,通过学习,我尝试开始写这道题目。

 这种模式的聚合要加上一连串的的前缀,写起来确实不方便。

    while(date2.getDay().getMonth().getYear().getValue() != date1.getDay().getMonth().getYear().getValue()||
                        date2.getDay().getMonth().getValue()!=date1.getDay().getMonth().getValue()||
                        date2.getDay().getValue()!=date1.getDay().getValue()) {
                    
                     if(date1.getDay().getMonth().getValue()==12&&date1.getDay().getValue()==31) {
                         date1.getDay().getMonth().getYear().yearIncrement();//date1.year++;
                         date1.getDay().getMonth().resetMin(); // date1.month=1;
                        date1.getDay().resetMin() ;
                        
                     }
                     else if(date1.getDay().getMonth().getYear().isLeapYear()&&date1.getDay().getMonth().getValue() == 2
                             &&date1.getDay().getValue()==29) {//是闰年且2月29号
                         date1.getDay().getMonth().monthIncreament(); // date1.month++;
                         date1.getDay().resetMin();//day=1;
                     }
                      else if(date1.getDay().getMonth().getYear().isLeapYear()&&date1.getDay().getMonth().getValue() == 2 && date1.getDay().getValue() == 28) {//是闰年且2月28号
                          date1.getDay().dayIncreament();  //day++;
                     }
                     else {
                         if( date1.getDay().getValue() == getDay().mon_maxnum [this.getDay().month.getValue()-1]){
                             date1.getDay().getMonth().monthIncreament();  // month++;
                             date1.getDay().resetMin();   //day=1;
                         }
                         else {
                             date1.getDay().dayIncreament();//day++
                         }
                     }
             i++;        
            }
            
        }
        return i;
    }
    }

 

 

 

题目将Year、Month、Day、DateUtil一层一层聚合,在DateUtil中运用day.getmonth().getvalue()的形式访问,通过一层一层访问来调用Year、Month、Day,实现多个相关类之间的相互调用,相较于前一个题目中的方法,本题目的聚合方法更加方便简单,Year、Month、Day全部在DateUtil里聚合,直接通过访问DateUtil来调用Year、Month、Day,使代码一目了然,清晰简洁,更加方便书写,是对前一个题目聚合方法的一种改进。

 

	while(date2.getYear().getValue() != date1.getYear().getValue()||
	    				date2.getMonth().getValue()!=date1.getMonth().getValue()||
	    				date2.getDay().getValue()!=date1.getDay().getValue()) {
	    			
	    	    	 if(date1.getMonth().getValue()==12&&date1.getDay().getValue()==31) {
	    	    		 date1.getYear().yearIncrement();//date1.year++;
	    	    		 date1.getMonth().resetMin(); // date1.month=1;
	    	    		 date1.getDay().setValue(1) ;//day = 1
	    	    		
	    	    	 }
	    	    	 else if(date1.getYear().isLeapYear()&&date1.getMonth().getValue() == 2
		            		 &&date1.getDay().getValue()==29) {//是闰年且2月29号
	    	    		 date1.getMonth().monthIncreament(); // date1.month++;
	    	    		 date1.getDay().setValue(1) ;//day=1;
	    	    	 }
	    	          else if(date1.getYear().isLeapYear()&&date1.getMonth().getValue() == 2 && date1.getDay().getValue() == 28) {//是闰年且2月28号
	    	        	  date1.getDay().dayIncreament();  //day++;
	    	    	 }
	    	    	 else {
	    	    		 if( date1.getDay().getValue() == mon_maxnum [this.getMonth().getValue()-1]){
	    	    			 date1.getMonth().monthIncreament();  // month++;
	    	    			 date1.getDay().setValue(1) ;   //day=1;
	    	    		 }
	    	    		 else {
	    	    			 date1.getDay().dayIncreament();//day++
	    	    		 }
	    	    	 }

  相较于上一题的聚合,本题的代码书写明显更加方便,也更加易懂。

       为了减少运行时间,当日期相差数字较大时,我以四百年为一个期限,以此减少代码运行量。

 7-1 菜单计价程序-4

这题一开始我是嫌麻烦,因为题目长度太长,而且看到好多人都没得分,于是我也没怎么写,但经过听课,必须要重新审视起来,先说这题的思路吧,首先需要把题目弄懂,然后再写相关的类,很明显有多个桌子,而且每个桌子的属性有桌号、日期和时间,而且菜的折扣和时间有关,所以需要设计一个桌子类来处理这些信息,基本的类题目已经给出。其次是写主类,先要考虑如何输入,格式是如何的,一开始我以为输入的信息是一次性输入,然后把字符串处理为Array,然后再转变为数组。

根据题目:

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

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

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)//根据序号查找一条记录

}

经过一段时间,我勉强将这几个类书写出来了,但菜的折扣是根据时间来定的,所以我写了如下,将日期转化为星期的格式:

	boolean isrightTime() {
		if(hour <= 21&& hour >=9) {
			if(hour == 21 && minute >=30 ||hour == 9 && minute <= 30) {
				return false;
			}
			return true;
		}
		return false;
	}
	public int[] monday = new int[] {0,31,28,31,30,31,30,31,31,30,31,30,31};
	//判断日期是否合法
	     boolean isLeapYear(int year) {
	    	 if(year%4==0&&year%100!=0||year%400==0) {
	    		 return true;//是闰年
	    	 }
	    	 else {
	    		 return false;//不是闰年;
	    	 }
	     }	     
	     boolean checkRight(int year ,int month, int day ) {
	    	 if(month<1||month>12) {
	    		 return false;
	    	 }
	    	 if(isLeapYear(year)) {//是闰年
	    		 if(month==2&&day>29) {
	    			 return false;
	    		 }
	    	 }
	    	 else {//不是闰年
	    		 if(day<0||day>monday[month]) {
	    			 return false;
	    		 }
	    	 }
	    	 return true;
	     }
	     
	     boolean checkDate(int year ,int month, int day ,int hour ,int minute ,int second) {//判断是否为合法数据
	     if(year < 2022||year > 2023) {
	    		 return false;
	    	 }
	     if(hour < 1 || hour > 24 || minute < 1|| minute >60 ||second < 1 || second > 60) {
	    	 return false ;
	     }
		return true;
	     
	     }
	     String getWeekday() {//将日期变为星期
	    	 String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
	 		Calendar cal = Calendar.getInstance();
	 		cal.set(Calendar.MONTH,this.month);
	 		cal.set(Calendar.YEAR, 5);
	 		cal.set(Calendar.DAY_OF_WEEK, day);
	 		Date date = cal.getTime();
	 		cal.setTime(date);
	 		int weekday = cal.get(Calendar.DAY_OF_WEEK) - 1;
	 		if (weekday < 0)
	 			weekday = 0;
	 		return weekDays[weekday];
	     }

	}

  由于惰性和侥幸心理,往后就没有什么突破性进展了

 

 

总的来说:这次作业是一次不小的挑战,考察了对我们的理解分析问题的能力。同时也让我对于正则表达式在代码中的运用有了更加深刻的理解,学会了比较基础的正则表达式,但是较难的还是无法掌握,明白了不同聚合的优劣势,能够更加理解聚合对于参数的赋值与调用。

posted on 2023-04-30 17:14  水温寒  阅读(28)  评论(0编辑  收藏  举报