4到6次作业总结

发布时间 2023-04-26 09:38:00作者: snnw

 

一、作业概述

   前言:在第4到6次作业中,难的题目是真的难,但简单的题目也是真的简单。难题主要是第四次作业中的第一题和第六次作业,而且六是四的迭代,是相关联的。在第四次作业中主要考查了对数据进行去重、统计与排序,以及对对象属性的封装性的考察。第五次作业是正则表达式的使用和类的聚合。第六次作业是一个综合考查,有循环、选择结构的使用,数据的封装性,类之间的依赖、关联、聚合,方法、参数的传递以及逻辑关系的判断等等,可谓是有一定的难度。下面是对这三次作业的详细分析。

二、源码分析

  第四次作业:

  本次作业需要编写的代码运行时间不要太长且所占空间不能太大,还需要学习Scanner类、String类、和LocalDate类中的一些方法以及Array类、ArrayList类、HashSet类、LinkedHashSet类的使用,通过使用其自带的方法,能够用更少的代码实现所需的功能。这提高了我们写代码的效率,节约了我们的时间。熟话说“君子善假于物也”,因此借助一些现有的方法对编码是十分有帮助的,而这正是我们应该自主学习的地方。

  7-2 判断一个整数中是否有重复的数据。

  这里我一开始只是简单地想通过双重循环来查找是否有重复数据,结果虽然测试样例通过了,但运行超时了。当我听室友说用HashSet可以使程序运行时间大大缩短从而通过时,我立马对其进行了搜索,并运用起来,结果果真通过了。之后,我又搜索了HashSet为什么会这么快,发现原来是HashSet是通过关键字直接得到要储存记录内存的储存位置,而不用对所有数据进行一一比对,因此能直接判断出要存储的数据是否已经存储过了,这个优势在大数据中尤其明显。且它不会存储重复的数据,利用这个在许多方面都能发挥作用,如数据的查重与去重等方面。假如有100万个数据,那么双重循环对其进行处理至多要循环100万的平方次,而HashSet至多只要100万次,两者对比是十分明显的。因此不难看出,为什么使用HashSet能更快。

通过前:

 

 1 import java.util.Scanner;
 2 public class Main {//主类
 3     public static void main(String[] args) {
 4         Scanner input = new Scanner(System.in);
 5         int n = input.nextInt();
 6         int i = 0;
 7         int[] num;
 8         num = new int[n];
 9         for(i = 0;i < n;i++)
10             num[i] = input.nextInt();
11         CheckReretition(num, n);
12     } 
13     public static void CheckReretition(int[] num, int n) {
14             int i = 0;
15             int j = 0;
16             int t = 0;
17             for(i= 0;i < n-1;i++) {
18                 for(j = i+1;j < n; j++) {
19                 if(num[i] == num[j]) {
20                     t = 1;
21                     break;
22                 }
23                 }
24                 if(t == 1) 
25                     break;    
26             }
27             if(t == 1)
28                 System.out.print("YES");
29             else
30                 System.out.print("NO");
31         }
32 } 

 

通过后:

 1 import java.util.HashSet;
 2 import java.util.Scanner;
 3 public class Main {//主类
 4     public static void main(String[] args) {
 5         Scanner input = new Scanner(System.in);
 6         int n = input.nextInt();
 7         if(n >= 1 && n <= 100000) {
 8             int i;
 9             HashSet<Integer> num = new HashSet<>();
10             for (i = 0; i < n; i++) {
11                 if (!num.add(input.nextInt())) {
12                     i = -1;
13                     break;
14                 }
15             }
16             if (i == -1)
17                 System.out.print("YES");
18             else
19                 System.out.print("NO");
20         }
21     }
22 }

 

 

 

   7-4   对一段英文文本中的所有单词进行升序排序,在过程中还要把重复单词去除。

    在一开始我选择的是使用正则表达式来将英文文本分割为一个个单词存储到字符串数组中,再去重,然后使用冒泡排序法进行排序,最后输出。初次代码对测试样例正确,但对较长文本是错误的,当我拿一个长文本测试时发现排序后的某些顺序有问题,这是我发现原来是排序时少循环了一遍。当时我还不知道HashSet,因此使用的还是双重循环去重,因此后来我加入了LinkedHashSet,它较之HashSet的区别是LinkedHashSet存入的数据是有序的,而HashSet的是无序的,然后转化为ArrayList再进行操作。这样就提高了代码的效率。

通过前:

 1 import java.util.Scanner;
 2 public class Main {//主类
 3     public static void main(String[] args) {
 4         Scanner input = new Scanner(System.in);
 5         String str = input.nextLine();
 6         String[] str1 = str.split("[,. ]");
 7         int i;
 8         int j;
 9         String s = null;
10         for(i = 0; i < str1.length - 1; i++) {
11             for(j = i + 1; j <str1.length; j++) {
12                 if(str1[i].length() < str1[j].length()) {
13                     s = str1[j];
14                     str1[j] = str1[i];
15                     str1[i] = s; }                
16                 }
17             }
18         for(i = 1; i < str1.length - 1; i++) {
19             if(str1[i - 1].length() == str1[i].length()) {
20                 if(str1[i - 1].compareToIgnoreCase(str1[i]) > 0) {
21                     s = str1[i - 1];
22                     str1[i - 1] = str1[i];
23                     str1[i] = s;
24                 }
25             }
26                 
27         }
28         for(i = 0; i < str1.length; i++) {
29             if(i == str1.length - 1)
30                 System.out.print(str1[i]);
31             else
32                 System.out.println(str1[i]);
33         }
34     }
35 }

通过后:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Scanner;
public class Main {//主类
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String str = input.nextLine();
        
        String[] str0 = str.split("[,. ]");
      
        int i;
        int j;
        LinkedHashSet<String> Set = new LinkedHashSet<>();
        for(i = 0; i < str0.length; i++) {

            Set.add(str0[i]);
        }
       
        ArrayList<String> str1 = new ArrayList<>(Set);
       

        String s;
        for(i = 0; i < str1.size() - 1; i++) {
            for(j = i + 1; j <str1.size(); j++) {
                if(str1.get(i).length() < str1.get(j).length()) {
                    s = str1.get(j);
                    str1.set(j, str1.get(i));
                    str1.set(i, s); }
            }
        }
        for(i = 0; i < str1.size() - 1; i++) {
            for(j = i + 1; j <str1.size(); j++) {
            if(str1.get(i).length() == str1.get(j).length()) {
                if(str1.get(i).compareToIgnoreCase(str1.get(j)) > 0) {
                    s = str1.get(i);
                    str1.set(i, str1.get(j));
                    str1.set(j, s);
                }
            }
            }

        }
        for(i = 0; i < str1.size(); i++) {
            if(i == str1.size() - 1)
                System.out.print(str1.get(i));
            else
                System.out.println(str1.get(i));
        }
    }
}

  第五次作业:

      本次作业是对如何使用正则表达式的训练及类的聚合关系的熟悉,需要我们检验相应数据的格式是否正确,使用聚合来设计相应的对象。

  7-5  这是对第三次作业中的7-3的改进,上次只有一个Date类,年、月、日作为它的属性。这次是将年、月、日单独拿出来作为三个类且为聚合关系,功能保持不变。在这次作业中,由于我对年、月、日的数据没传递好。使得数据会在某个阶段丢失,造成最终结果出错。其次,在写代码的过程中,我也碰到过逻辑错误,如输入样例后输出的结果较之正确结果会偏差几天。这时我发现检查错误点十分困难,往往需要不断进行调试,耗费相当的时间,开始我想要取巧,直接在最后结果输出前补上天数差,但并未通过,最后只好老老实实通过一次次调试来确定哪里有逻辑错误。

  1 import java.util.Scanner;
  2 
  3 public class Main {
  4     public static void main(String[] args) {
  5         Scanner input = new Scanner(System.in);
  6         int year = 0;
  7         int month = 0;
  8         int day = 0;
  9 
 10         int choice = input.nextInt();
 11 
 12         if (choice == 1) { // test getNextNDays method
 13             int m = 0;
 14             year = Integer.parseInt(input.next());
 15             month = Integer.parseInt(input.next());
 16             day = Integer.parseInt(input.next());
 17 
 18             DateUtil date = new DateUtil(year, month, day);
 19 
 20             if (!date.checkInputValidity()) {
 21                 System.out.println("Wrong Format");
 22                 System.exit(0);
 23             }
 24 
 25             m = input.nextInt();
 26 
 27             if (m < 0) {
 28                 System.out.println("Wrong Format");
 29                 System.exit(0);
 30             }
 31             
 32             System.out.println(date.getNextNDays(m).showDate());
 33         } else if (choice == 2) { // test getPreviousNDays method
 34             int n = 0;
 35             year = Integer.parseInt(input.next());
 36             month = Integer.parseInt(input.next());
 37             day = Integer.parseInt(input.next());
 38 
 39             DateUtil date = new DateUtil(year, month, day);
 40 
 41             if (!date.checkInputValidity()) {
 42                 System.out.println("Wrong Format");
 43                 System.exit(0);
 44             }
 45 
 46             n = input.nextInt();
 47 
 48             if (n < 0) {
 49                 System.out.println("Wrong Format");
 50                 System.exit(0);
 51             }
 52            
 53             System.out.println(date.getPreviousNDays(n).showDate());
 54         } else if (choice == 3) {    //test getDaysofDates method
 55             year = Integer.parseInt(input.next());
 56             month = Integer.parseInt(input.next());
 57             day = Integer.parseInt(input.next());
 58 
 59             int anotherYear = Integer.parseInt(input.next());
 60             int anotherMonth = Integer.parseInt(input.next());
 61             int anotherDay = Integer.parseInt(input.next());
 62 
 63             DateUtil fromDate = new DateUtil(year, month, day);
 64             DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);
 65 
 66             if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
 67                 System.out.println(fromDate.getDaysofDates(toDate));
 68             } else {
 69                 System.out.println("Wrong Format");
 70                 System.exit(0);
 71             }
 72         }
 73         else{
 74             System.out.println("Wrong Format");
 75             System.exit(0);
 76         }        
 77     }
 78 }
 79 
 80 class DateUtil{
 81     private Day day = new Day();
 82     
 83         public DateUtil(){
 84             
 85         }
 86     
 87         public DateUtil(int year, int month, int day) {
 88             this.day.getMonth().getYear().setValue(year);
 89             this.day.getMonth().setValue(month);
 90             this.day.setValue(day);
 91         }
 92 
 93         public Day getDay() {
 94             return day;
 95         }
 96         
 97         public void setDay(Day day) {
 98             this.day = day;
 99         } 
100         
101         public boolean checkInputValidity() {
102             if(this.day.getMonth().getYear().validate() && this.day.getMonth().validate() && this.day.validate())
103                 return true;
104             else
105                 return false;
106         }
107         
108         public DateUtil getNextNDays(int n) {
109             if(n + this.getDay().getValue() <= this.getDay().mon_maxnum[this.getDay().getMonth().getValue()]){
110                 this.getDay().setValue(n + this.getDay().getValue());
111                 n = 0;
112             }
113             else{
114             if(this.getDay().getMonth().getValue() == 12) {
115                 n = n - (31 - this.getDay().getValue() + 1);
116                 this.getDay().getMonth().getYear().yearIncremrnt();
117                 this.getDay().getMonth().resetMin();
118                 this.getDay().resetMin();
119                 }
120             else {
121                 n = n - (this.getDay().mon_maxnum[this.getDay().getMonth().getValue()] - this.getDay().getValue() + 1);
122                 this.getDay().getMonth().monthIncremrnt();
123                 this.getDay().resetMin();
124                 if(this.getDay().getMonth().getYear().isLeapYear())
125                     n = n - 1;
126             }               
127             }
128             for(;n > 0;) {
129             if(!this.getDay().getMonth().getYear().isLeapYear() && n >= 365) {
130                 n = n - 365;
131                 this.getDay().getMonth().getYear().yearIncremrnt();
132             }
133             if(this.getDay().getMonth().getYear().isLeapYear() && n >= 366) {
134                 n = n - 366;
135                 this.getDay().getMonth().getYear().yearIncremrnt();
136             }
137             
138             if(this.getDay().getMonth().getValue() == 12 && n + this.getDay().getValue() > 31) {
139                 n = n - (31 - this.getDay().getValue() + 1);
140                 this.getDay().getMonth().getYear().yearIncremrnt();
141                 this.getDay().getMonth().resetMin();
142                 this.getDay().resetMin();
143                 }
144             if(!this.getDay().getMonth().getYear().isLeapYear() && n < 365) {
145                 if(n < this.getDay().mon_maxnum[this.getDay().getMonth().getValue()]) {
146                     this.getDay().setValue(n + 1);
147                     n = 0;
148                 }
149                 if(n >= this.getDay().mon_maxnum[this.getDay().getMonth().getValue()]) {
150                     n = n - this.getDay().mon_maxnum[this.getDay().getMonth().getValue()];
151                     this.getDay().getMonth().monthIncremrnt();
152                 } 
153             }
154             if(this.getDay().getMonth().getYear().isLeapYear() && n < 366) {
155                 if(this.getDay().getMonth().getValue() == 2 && n <29) {
156                     this.getDay().setValue(n + 1);
157                     n = 0;
158                 }
159                 if(this.getDay().getMonth().getValue() == 2 && n >=29) {
160                     n = n - 29;
161                     this.getDay().getMonth().setValue(3);
162                     this.getDay().resetMin();
163                 }
164                 if(n < this.getDay().mon_maxnum[this.getDay().getMonth().getValue()]) {
165                     this.getDay().setValue(n + 1);
166                     n = 0;
167                 }
168                 if(n >= this.getDay().mon_maxnum[this.getDay().getMonth().getValue()]) {
169                     n = n - this.getDay().mon_maxnum[this.getDay().getMonth().getValue()];        
170                     this.getDay().getMonth().monthIncremrnt();
171                 } 
172             }
173 
174         }
175             
176                 return new DateUtil(this.getDay().getMonth().getYear().getValue(), this.getDay().getMonth().getValue(), this.getDay().getValue());
177                
178             }
179                     
180             
181                 public DateUtil getPreviousNDays(int n) {
182                    if(n <= 0 || n>= 1000000){
183                         
184                     }
185                    else {
186                   if(this.getDay().getMonth().getValue() == 1) {
187                       n = n - (this.getDay().getValue() - 1);                                    
188                       this.getDay().resetMin(); 
189               } 
190                   else { 
191                       n = n - (this.getDay().getValue() - 1);
192                       this.getDay().resetMin(); 
193                       } 
194                   for(;n > 0;){ 
195                       if(!this.getDay().getMonth().getYear().isLeapYear() && n >= 365) { 
196                           n = n - 365;
197                           this.getDay().getMonth().getYear().yearReduction(); 
198                           } 
199                       if(this.getDay().getMonth().getYear().isLeapYear() && n >= 366) {
200                           n = n - 366;
201                           this.getDay().getMonth().getYear().yearReduction();  
202                           }
203                       if(this.getDay().getMonth().getValue() == 1) {
204                           if(n > 31) {
205                               n = n - 31;
206                               this.getDay().getMonth().getYear().yearReduction(); 
207                               this.getDay().getMonth().resetMax();
208                               this.getDay().resetMin();                           
209                               }
210                               else {
211                                   this.getDay().getMonth().getYear().yearReduction(); 
212                                   this.getDay().getMonth().resetMax();
213                                   this.getDay().setValue(31 - (n - 1) + 1);
214                                   break;
215                               }
216                           } 
217                       if(!this.getDay().getMonth().getYear().isLeapYear() && n < 365) {
218                           if(n <= this.getDay().mon_maxnum[this.getDay().getMonth().getValue() - 1]) {
219                               this.getDay().setValue(this.getDay().mon_maxnum[this.getDay().getMonth().getValue() - 1] - n + 1); 
220                               this.getDay().getMonth().monthReduction();
221                               n = 0;
222                               }
223                           if(n > this.getDay().mon_maxnum[this.getDay().getMonth().getValue() - 1]) {
224                               n = n - this.getDay().mon_maxnum[this.getDay().getMonth().getValue() - 1];
225                               this.getDay().getMonth().monthReduction(); 
226                               } 
227                           }
228                       if(this.getDay().getMonth().getYear().isLeapYear() && n < 366) { 
229                           if(this.getDay().getMonth().getValue() == 3 && n <= 29) {
230                               this.getDay().setValue(this.getDay().mon_maxnum[this.getDay().getMonth().getValue() - 1] - n + 1);
231                               this.getDay().getMonth().monthReduction();
232                               n = 0;
233                               } 
234                           if(this.getDay().getMonth().getValue() == 3 && n >29) {
235                               n = n - 29;
236                               this.getDay().getMonth().setValue(2);
237                               this.getDay().resetMin(); 
238                               }
239                           if(n <= this.getDay().mon_maxnum[this.getDay().getMonth().getValue() - 1]) { 
240                               this.getDay().setValue(this.getDay().mon_maxnum[this.getDay().getMonth().getValue() - 1] - n + 1); 
241                               this.getDay().getMonth().monthReduction();
242                               n = 0; 
243                               }
244                           if(n > this.getDay().mon_maxnum[this.getDay().getMonth().getValue() - 1]) {
245                               n = n - this.getDay().mon_maxnum[this.getDay().getMonth().getValue() - 1];
246                               this.getDay().getMonth().monthReduction(); 
247                               }
248               }
249                   }
250               } 
251                   
252                   return new DateUtil(this.getDay().getMonth().getYear().getValue(), this.getDay().getMonth().getValue(), this.getDay().getValue());
253               
254               }
255              
256         public String showDate() {
257                 return this.day.getMonth().getYear().getValue() + "-" + this.day.getMonth().getValue() + "-" + this.day.getValue();
258         }
259         
260         public boolean compareDates(DateUtil date) {
261             if(this.day.getMonth().getYear().getValue() > date.day.getMonth().getYear().getValue())
262                 return true;            
263             if(this.day.getMonth().getYear().getValue() == date.day.getMonth().getYear().getValue()) {
264                 if(this.day.getMonth().getValue() > date.day.getMonth().getValue())
265                     return true;
266                 if(this.day.getMonth().getValue() == date.day.getMonth().getValue()) {
267                     if(this.day.getValue() > date.day.getValue())
268                         return true;
269                 }
270             }
271             return false;
272         }
273         
274         public boolean equalTwoDates(DateUtil date) {
275             if(this.day.getMonth().getYear().getValue() == date.day.getMonth().getYear().getValue() && this.day.getMonth().getValue() == date.day.getMonth().getValue() && this.day.getValue() > date.day.getValue())
276                 return true;
277             else
278                 return false;
279         }
280         
281         public int getDaysofDates(DateUtil date) {
282             int num = 0;
283             if(equalTwoDates(date))
284                 return 0;
285             else {
286                 if(this.compareDates(date)) {
287                     DateUtil temp = new DateUtil();
288                     temp.day = this.day;
289                     this.day = date.day;
290                     date.day = temp.day;
291                 }
292                 
293                 for(;this.getDay().getMonth().getYear().getValue() != date.getDay().getMonth().getYear().getValue();) {
294                     
295                     if(this.getDay().getMonth().getYear().isLeapYear()) {
296                         num += 366;
297                         this.getDay().getMonth().getYear().yearIncremrnt();
298                     }
299                     if(!this.getDay().getMonth().getYear().isLeapYear()) {
300                         num += 365;
301                         this.getDay().getMonth().getYear().yearIncremrnt();
302                     }
303                     
304                     }
305                 
306                 for(;this.getDay().getMonth().getValue() != date.getDay().getMonth().getValue();) {
307                     
308                     if(this.getDay().getMonth().getValue() == 12) {
309                         num += 31;
310                         this.getDay().getMonth().resetMin();
311                         this.getDay().getMonth().getYear().yearIncremrnt();
312                     }
313                     if(this.getDay().getMonth().getYear().isLeapYear()) {
314                         if(this.getDay().getMonth().getValue() == 2) {
315                         num += 29;
316                         this.getDay().getMonth().monthIncremrnt();
317                         }
318                         else {
319                             num += this.getDay().mon_maxnum[this.getDay().getMonth().getValue()];
320                             this.getDay().getMonth().monthIncremrnt();
321                         }
322                     }
323                     else {
324                         num += this.getDay().mon_maxnum[this.getDay().getMonth().getValue()];
325                         this.getDay().getMonth().monthIncremrnt();
326                     }
327                 }
328                     
329                         num += date.getDay().getValue() - this.getDay().getValue();
330         
331             }
332             return num;
333             
334         }
335 }
336 
337 
338 class Year{
339     private  int value;
340 
341     public Year() {
342         
343     }
344 
345     public Year(int value) {
346         this.value = value;
347     }
348 
349     public int getValue() {
350         return value;
351     }
352 
353     public void setValue(int value) {
354         this.value = value;
355     }
356     
357     public boolean isLeapYear() {
358         if(this.value % 4 == 0 && this.value % 100 != 0 || this.value % 400 == 0)
359             return true;
360         else
361             return false;
362     }
363     
364     public boolean validate() {
365         if(this.value >= 1900 && this.value <= 2050)
366             return true;
367         else
368             return false;
369     }
370     
371     public void yearIncremrnt() {
372         this.value += 1;
373     }
374     
375     public void yearReduction() {
376         this.value -= 1;
377     }
378 }
379 
380 class Month{
381     private int value;
382     private Year year = new Year();
383     
384     public Month() {
385         
386     }
387 
388     public Month(int monthValue, int yearValue) {
389         this.value = monthValue;
390         this.year.setValue(yearValue);
391     }
392 
393     public int getValue() {
394         return value;
395     }
396 
397     public void setValue(int value) {
398         this.value = value;
399     }
400 
401     public Year getYear() {
402         return year;
403     }
404 
405     public void setYear(Year year) {
406         this.year = year;
407     }
408     
409     public void resetMin() {
410         this.value = 1;
411     }
412     
413     public void resetMax() {
414         this.value = 12;
415     }
416     
417     public boolean validate() {
418         if(this.value >= 1 && this.value <= 12)
419             return true;
420         else
421             return false;
422         
423     }
424     
425     public void monthIncremrnt() {
426         this.value += 1;
427     }
428     
429     public void monthReduction() {
430         this.value -= 1;
431     }
432 }
433 
434 class Day{
435     private int value;
436     private Month month = new Month();
437     public int mon_maxnum[] = new int[] {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
438     
439     public Day() {
440         
441     }
442 
443     public Day(int yearValue, int monthValue, int dayValue) {
444         this.value = dayValue;
445         this.month.setValue(monthValue);
446         this.month.getYear().setValue(yearValue);
447     }
448 
449     public int getValue() {
450         return value;
451     }
452 
453     public void setValue(int value) {
454         this.value = value;
455     }
456 
457     public Month getMonth() {
458         return month;
459     }
460 
461     public void setMonth(Month month) {
462         this.month = month;
463     }
464     
465     public void resetMin() {
466         this.value = 1;
467     }
468     
469     public void resetMax() {
470         if(this.getMonth().getYear().isLeapYear() && this.getMonth().getValue() == 2)
471             this.value = 29;
472         else
473             this.value  = mon_maxnum[this.getMonth().getValue()];
474     }
475     
476     public boolean validate() {
477         if(this.value >= 1 && this.value <= mon_maxnum[this.month.getValue()])
478             return true;
479         else
480             return false;
481     }
482     
483     public void dayIncremrnt() {
484         this.value += 1;
485     }
486     
487     public void dayReduction() {
488         this.value -= 1;
489     }
490 }
Code

  7-6  本题是在上题的基础上再次改进,使其更加符合面对对象的编程。这次年、月、日不再像上题那样相互关联,而是通过DateUtil类来传递数据,符合了迪米特法则。

现在看来,当时的代码还有许多能改进的地方。比如说,给年、月、日类创建一个父类,因为它们拥有的属性是相同的且一些方法的功能是一样的。如果将那些共通部分提取出来创建一个父类,那么当时的代码就能简化,减少代码的冗余还能提高程序的可扩展性和效率。除此之外,对那些功能相同的方法还能进一步将其写成接口,使其更加面向对象,提高了代码的可读性和方法的可复用性。

  

  1 import java.util.Scanner;
  2 
  3 public class Main {
  4     public static void main(String[] args) {
  5         Scanner input = new Scanner(System.in);
  6         int year = 0;
  7         int month = 0;
  8         int day = 0;
  9 
 10         int choice = input.nextInt();
 11 
 12         if (choice == 1) { // test getNextNDays method
 13             int m = 0;
 14             year = Integer.parseInt(input.next());
 15             month = Integer.parseInt(input.next());
 16             day = Integer.parseInt(input.next());
 17 
 18             DateUtil date = new DateUtil(year, month, day);
 19 
 20             if (!date.checkInputValidity()) {
 21                 System.out.println("Wrong Format");
 22                 System.exit(0);
 23             }
 24 
 25             m = input.nextInt();
 26 
 27             if (m < 0) {
 28                 System.out.println("Wrong Format");
 29                 System.exit(0);
 30             }
 31             date.getMonth();
 32             System.out.print(date.getYear().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " next " + m + " days is:");
 33             System.out.println(date.getNextNDays(m).showDate());
 34         } else if (choice == 2) { // test getPreviousNDays method
 35             int n = 0;
 36             year = Integer.parseInt(input.next());
 37             month = Integer.parseInt(input.next());
 38             day = Integer.parseInt(input.next());
 39 
 40             DateUtil date = new DateUtil(year, month, day);
 41 
 42             if (!date.checkInputValidity()) {
 43                 System.out.println("Wrong Format");
 44                 System.exit(0);
 45             }
 46 
 47             n = input.nextInt();
 48 
 49             if (n < 0) {
 50                 System.out.println("Wrong Format");
 51                 System.exit(0);
 52             }
 53             date.getMonth();
 54             System.out.print(
 55                     date.getYear().getValue() + "-" + date.getMonth().getValue() + "-" + date.getDay().getValue() + " previous " + n + " days is:");
 56             System.out.println(date.getPreviousNDays(n).showDate());
 57         } else if (choice == 3) {    //test getDaysofDates method
 58             year = Integer.parseInt(input.next());
 59             month = Integer.parseInt(input.next());
 60             day = Integer.parseInt(input.next());
 61 
 62             int anotherYear = Integer.parseInt(input.next());
 63             int anotherMonth = Integer.parseInt(input.next());
 64             int anotherDay = Integer.parseInt(input.next());
 65 
 66             DateUtil fromDate = new DateUtil(year, month, day);
 67             DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);
 68 
 69             if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
 70                 System.out.println("The days between " + fromDate.showDate() +
 71                         " and " + toDate.showDate() + " are:");
 72                 System.out.println(fromDate.getDaysofDates(toDate));
 73             } else {
 74                 System.out.println("Wrong Format");
 75                 System.exit(0);
 76             }
 77         }
 78         else{
 79             System.out.println("Wrong Format");
 80             System.exit(0);
 81         }
 82     }
 83 }
 84 
 85 class DateUtil{
 86     private Year year = new Year();
 87     private static Month month = new Month();
 88     private Day day = new Day();
 89     public static int mon_maxnum[] = new int[] {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 90 
 91     public DateUtil() {
 92 
 93     }
 94 
 95     public DateUtil(int y, int m, int d) {
 96         this.getYear().setValue(y);
 97         DateUtil.getMonth().setValue(m);
 98         this.getDay().setValue(d);
 99     }
100 
101     public Year getYear() {
102         return year;
103     }
104 
105     public void setYear(Year year) {
106         this.year = year;
107     }
108 
109     public static Month getMonth() {
110         return month;
111     }
112 
113     public void setMonth(Month month) {
114         DateUtil.month = month;
115     }
116 
117     public Day getDay() {
118         return day;
119     }
120 
121     public void setDay(Day day) {
122         this.day = day;
123     }
124 
125     public void setdayMin() {
126         this.getDay().setValue(1);
127     }
128 
129     public void setdayMax() {
130         this.getDay().setValue(mon_maxnum[DateUtil.getMonth().getValue()]);
131     }
132 
133     public boolean checkInputValidity() {
134         if(this.getYear().validate() && DateUtil.getMonth().validate() && this.getDay().validate())
135             return true;
136         else
137             return false;
138     }
139 
140     public DateUtil getNextNDays(int n) {
141         if(n + this.getDay().getValue() <= mon_maxnum[DateUtil.getMonth().getValue()]){
142             this.getDay().setValue(n + this.getDay().getValue());
143             n = 0;
144         }
145         else{           
146             if(DateUtil.getMonth().getValue() == 12) {
147                 n = n - (31 - this.getDay().getValue() + 1);
148                 this.getYear().yearIncremrnt();
149                 DateUtil.getMonth().resetMin();
150                 this.setdayMin();
151             }
152             else {
153                 n = n - (mon_maxnum[DateUtil.getMonth().getValue()] - this.getDay().getValue() + 1);
154                 DateUtil.getMonth().monthIncremrnt();
155                 this.setdayMin();
156                 if(this.getYear().isLeapYear())
157                     n = n - 1;
158             }
159         }
160         for(;n > 0;) {
161             if(!this.getYear().isLeapYear() && n >= 365) {
162                 n = n - 365;
163                 this.getYear().yearIncremrnt();
164             }
165             if(this.getYear().isLeapYear() && n >= 366) {
166                 n = n - 366;
167                 this.getYear().yearIncremrnt();
168             }
169 
170             if(DateUtil.getMonth().getValue() == 12 && n + this.getDay().getValue() > 31) {
171                 n = n - (31 - this.getDay().getValue() + 1);
172                 this.getYear().yearIncremrnt();
173                 DateUtil.getMonth().resetMin();
174                 this.setdayMin();
175             }
176             if(!this.getYear().isLeapYear() && n < 365) {
177                 if(n < mon_maxnum[DateUtil.getMonth().getValue()]) {
178                     this.getDay().setValue(n + 1);
179                     n = 0;
180                 }
181                 if(n >= mon_maxnum[DateUtil.getMonth().getValue()]) {
182                     n = n - mon_maxnum[DateUtil.getMonth().getValue()];
183                     DateUtil.getMonth().monthIncremrnt();
184                 }
185             }
186             if(this.getYear().isLeapYear() && n < 366) {
187                 if(DateUtil.getMonth().getValue() == 2 && n <29) {
188                     this.getDay().setValue(n + 1);
189                     n = 0;
190                 }
191 
192                 if(DateUtil.getMonth().getValue() == 2 && n >=29) {
193                     n = n - 29;
194                     DateUtil.getMonth().setValue(3);
195                     this.setdayMin();
196                 }
197                 if(n < mon_maxnum[DateUtil.getMonth().getValue()]) {
198                     this.getDay().setValue(n + 1);
199                     n = 0;
200                 }
201                 if(n >= mon_maxnum[DateUtil.getMonth().getValue()]) {
202                     n = n - mon_maxnum[DateUtil.getMonth().getValue()];
203                     DateUtil.getMonth().monthIncremrnt();
204                 }
205             }
206 
207         }
208 
209 
210 
211         return new DateUtil(this.getYear().getValue(), DateUtil.getMonth().getValue(), this.getDay().getValue());
212 
213     }
214 
215 
216     public DateUtil getPreviousNDays(int n) {
217         if(n - this.getDay().getValue() <= 0) {
218             this.getDay().setValue(this.getDay().getValue() - n);
219             n = 0;
220         }
221 
222         if(DateUtil.getMonth().getValue() == 1) {
223             n = n - (this.getDay().getValue() - 1);
224             this.setdayMin();
225         }
226         else {
227             n = n - (this.getDay().getValue() - 1);
228             this.setdayMin();
229         }
230         for(;n>365;){
231             if(!this.getYear().isLeapYear() && n >= 365) {
232                 n = n - 365;
233                 this.getYear().yearReduction();
234             }
235             if(this.getYear().isLeapYear() && n >= 366) {
236                 n = n - 366;
237                 this.getYear().yearReduction();
238             }
239         }
240         for(;n > 0;){
241             if(!this.getYear().isLeapYear() && n >= 365) {
242                 n = n - 365;
243                 this.getYear().yearReduction();
244             }
245             if(this.getYear().isLeapYear() && n >= 366) {
246                 n = n - 366;
247                 this.getYear().yearReduction();
248             }
249             if(DateUtil.getMonth().getValue() == 1) {
250                 if(n > 31) {
251                     n = n - 31;
252                     this.getYear().yearReduction();
253                     DateUtil.getMonth().resetMax();
254                     this.setdayMin();
255                 }
256                 else {
257                     this.getYear().yearReduction();
258                     DateUtil.getMonth().resetMax();
259                     this.getDay().setValue(31 - (n - 1) + 1);
260                     break;
261                 }
262             }
263             if(!this.getYear().isLeapYear() && n < 365) {
264                 if(n <= mon_maxnum[DateUtil.getMonth().getValue() - 1]) {
265                     DateUtil.getMonth();
266                     this.getDay().setValue(mon_maxnum[DateUtil.getMonth().getValue() - 1] - n + 1);
267 
268                     n = 0;
269                 }
270                 if(n > mon_maxnum[DateUtil.getMonth().getValue() - 1]) {
271                     n = n - mon_maxnum[DateUtil.getMonth().getValue() - 1];
272                     DateUtil.getMonth().monthReduction();
273                 }
274             }
275             if(this.getYear().isLeapYear() && n < 366) {
276                 if(DateUtil.getMonth().getValue() == 3 && n <= 29) {
277                     this.getDay().setValue(mon_maxnum[DateUtil.getMonth().getValue() - 1] - n + 1);
278                     DateUtil.getMonth().monthReduction();
279                     n = 0;
280                 }
281                 if(DateUtil.getMonth().getValue() == 3 && n >29) {
282                     n = n - 29;
283                     DateUtil.getMonth().setValue(2);
284                     this.setdayMin();
285                 }
286                 if(n <= mon_maxnum[DateUtil.getMonth().getValue() - 1]) {
287                     DateUtil.getMonth();
288                     this.getDay().setValue(mon_maxnum[DateUtil.getMonth().getValue() - 1] - n + 1);
289 
290                     n = 0;
291                 }
292                 if(n > mon_maxnum[DateUtil.getMonth().getValue() - 1]) {
293                     n = n - mon_maxnum[DateUtil.getMonth().getValue() - 1];
294                     DateUtil.getMonth().monthReduction();
295                 }
296             }
297 
298         }
299 
300         return new DateUtil(this.getYear().getValue(),DateUtil.getMonth().getValue() - 1, this.getDay().getValue());
301 
302     }
303 
304     public String showDate() {
305         return this.getYear().getValue() + "-" + DateUtil.getMonth().getValue() + "-" + this.getDay().getValue();
306     }
307 
308     public boolean compareDates(DateUtil date) {
309         if(this.getYear().getValue() > date.getYear().getValue())
310             return true;
311         if(this.getYear().getValue() == date.getYear().getValue()) {
312 
313             date.getMonth();
314             if(DateUtil.getMonth().getValue() > date.getMonth().getValue())
315                 return true;
316             if(DateUtil.getMonth().getValue() == date.getMonth().getValue()) {
317                 if(this.getDay().getValue() > date.getDay().getValue())
318                     return true;
319             }
320         }
321         return false;
322     }
323 
324     public boolean equalTwoDates(DateUtil date) {
325         date.getMonth();
326         if(this.getYear().getValue() == date.getYear().getValue() && DateUtil.getMonth().getValue() == date.getMonth().getValue() && this.day.getValue() > date.day.getValue())
327             return true;
328         else
329             return false;
330     }
331 
332     public int getDaysofDates(DateUtil date) {
333         int num = 0;
334         if(this.equalTwoDates(date))
335             return 0;
336         else {
337             if(this.compareDates(date)) {
338 
339             DateUtil temp = new DateUtil(this.getYear().getValue(), DateUtil.getMonth().getValue(), this.getDay().getValue());
340                 this.getDay().setValue(date.getDay().getValue());
341                 date.getMonth();
342                 DateUtil.getMonth().setValue(date.getMonth().getValue());
343                 this.getYear().setValue(date.getYear().getValue());
344                 date = temp;
345             }
346 
347             for(;this.getYear().getValue() != date.getYear().getValue();) {
348 
349                 if(this.getYear().isLeapYear()) {
350                     num += 366;
351                     this.getYear().yearIncremrnt();
352                 }
353                 if(!this.getYear().isLeapYear()) {
354                     num += 365;
355                     this.getYear().yearIncremrnt();
356                 }
357 
358             }
359 
360             date.getMonth();
361             for(;DateUtil.getMonth().getValue() != date.getMonth().getValue();) {
362 
363                 if(DateUtil.getMonth().getValue() == 12) {
364                     num += 31;
365                     DateUtil.getMonth().resetMin();
366                     this.getYear().yearIncremrnt();
367                 }
368                 if(this.getYear().isLeapYear()) {
369                     if(DateUtil.getMonth().getValue() == 2) {
370                         num += 29;
371                         DateUtil.getMonth().monthIncremrnt();
372                     }
373                     else {
374                         num += mon_maxnum[DateUtil.getMonth().getValue()];
375                         DateUtil.getMonth().monthIncremrnt();
376                     }
377                 }
378                 else {
379                     num += mon_maxnum[DateUtil.getMonth().getValue()];
380                     DateUtil.getMonth().monthIncremrnt();
381                 }
382             }
383 
384             num += date.getDay().getValue() - this.getDay().getValue();
385 
386         }
387         return num;
388 
389     }
390 }
391 
392 class Year{
393     private  int value;
394 
395     public Year() {
396 
397     }
398 
399     public Year(int value) {
400         this.value = value;
401     }
402 
403     public int getValue() {
404         return value;
405     }
406 
407     public void setValue(int value) {
408         this.value = value;
409     }
410 
411     public boolean isLeapYear() {
412         if(this.value % 4 == 0 && this.value % 100 != 0 || this.value % 400 == 0)
413             return true;
414         else
415             return false;
416     }
417 
418     public boolean validate() {
419         if(this.value >= 1820 && this.value <= 2020)
420             return true;
421         else
422             return false;
423     }
424 
425     public void yearIncremrnt() {
426         this.value += 1;
427     }
428 
429     public void yearReduction() {
430         this.value -= 1;
431     }
432 }
433 
434 class Month{
435     private  int value;
436 
437 
438     public Month() {
439 
440     }
441 
442     public Month(int value) {
443         this.value = value;
444     }
445 
446     public int getValue() {
447         return this.value;
448     }
449 
450     public void setValue(int value) {
451         this.value = value;
452     }
453 
454     public void resetMin() {
455         this.value = 1;
456     }
457 
458     public void resetMax() {
459         this.value = 12;
460     }
461 
462     public boolean validate() {
463         if(this.value >= 1 && this.value <= 12)
464             return true;
465         else
466             return false;
467 
468     }
469 
470     public void monthIncremrnt() {
471         this.value += 1;
472     }
473 
474     public void monthReduction() {
475         this.value -= 1;
476     }
477 }
478 
479 class Day{
480     private int value;
481 
482     public Day() {
483 
484     }
485 
486     public Day(int value) {
487         this.value = value;
488     }
489 
490     public int getValue() {
491         return value;
492     }
493 
494     public void setValue(int value) {
495         this.value = value;
496     }
497 
498     public boolean validate() {
499         if(this.value >= 1 && this.value <= DateUtil.mon_maxnum[DateUtil.getMonth().getValue()])
500             return true;
501         else
502             return false;
503     }
504 
505     public void dayIncremrnt() {
506         this.value += 1;
507     }
508 
509     public void dayReduction() {
510         this.value -= 1;
511     }
512     
513 }
Code

 

  第六次作业:编写菜单计价程序。

  本次作业是在第四次作业的7-1基础上再次增加一些功能,较之以前的题目是很难的。因为需要考虑许多因素,如输入的格式,点菜序号顺序,桌类信息,是否打折,两个点菜记录是否为同一桌等。它除主类外还有菜品类、菜谱类、点菜记录类、订单类和桌类组成。在写程序时,我遇到了许多的问题,有空指针问题、输入错误问题、逻辑问题等。其中有许多我以为没问题的点在实际测试中却并未通过,一时就不知道从哪入手,虽然用大量的时间来修改调试,其中的一些成功了,但仍有许多并未解决。我在编写添加菜品或点菜记录时不知是不是隔了一段时间,结果在添加时并未用带参构造函数来创建相应对象,将输入的数据存储到对象数组中,导致我输入的数据全部没被接收到,在最后输出总价时只会为零。这也导致了在使用对象数组时一直会出现空指针报错。除此之外,我当时在输入某个样例时出现了数字输入错误问题,一开始我还一脸困惑,输入的不都是字符串吗,需要实型我也是实时转化的,怎么会出错。仔细检查后发现原来是输入的格式有误,这个我没考虑进去,如在桌号的位置输入了字母。所以我先通过正则表达式对输入的数据类型进行匹配,然后在对数据进行处理,其余对应的错误就输出相应的内容。在这次作业中我又发现了一个很有用的类——Calendar类。它能够通过输入的Date型日期来判断那天是星期几,可以解决判断当时是否处于打折时间。

  1 import java.text.ParseException;
  2 import java.text.SimpleDateFormat;
  3 import java.util.Calendar;
  4 import java.util.Date;
  5 import java.util.Scanner;
  6 
  7 public class Main {//主类
  8     public static void main(String[] args) {
  9         Scanner input = new Scanner(System.in);
 10         Menu menu = new Menu();
 11         Table[] tables = new Table[10];
 12         String[] temp0 = {"0", "0"};
 13         int k = 0;
 14         int num = 0;
 15         int cnt;
 16         int totalprice = 0;
 17         int[] month_max = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 18         while(true) {
 19             int t = 0;
 20             String inform = input.nextLine();
 21             String[] temp = inform.split(" ");
 22             cnt = inform.length() - temp.length;
 23             if (temp[0].equals("end")) {
 24                 break;
 25             }
 26             int length = temp.length;
 27             if (length == 2) {
 28                 int i = 1;
 29                 if (temp[1].equals("delete")) {
 30                     for (; i < tables[num].order.i; i++) {
 31                         if (Integer.parseInt(temp[0]) == tables[num].order.records[i].orderNum) {
 32                             i = -1;
 33                             break;
 34                         }
 35                     }
 36                     if (i == -1) {
 37                         if (temp[0].equals(temp0[0]) && temp0[1].equals("delete")) {
 38                             temp0 = temp;
 39                             continue;
 40                         } else {
 41                             tables[num].order.delARecordByOrderNum(Integer.parseInt(temp[0]));
 42                         }
 43                     } else {
 44                         System.out.println("delete error");
 45                     }
 46                 } else {
 47 
 48                     if (temp[1].matches("[0-9]+") && temp[1].charAt(0) != 0) {
 49                         if (temp0.length == 2 || temp0.length == 3) {
 50 
 51                             if (Integer.parseInt((temp[1])) <= 0 || Integer.parseInt(temp[1]) >= 300) {
 52                                 System.out.println(temp[0] + " price out of range " + temp[1]);
 53                             }
 54                             menu.addDish(temp[0], Integer.parseInt(temp[1]), 'F');
 55                         } else {
 56                             System.out.println("invalid dish");
 57                         }
 58                     } else {
 59                         System.out.println("wrong format");
 60                     }
 61 
 62                 }
 63             }
 64             if (length == 3) {
 65 
 66                 if (temp[1].matches("[0-9]+") && temp[1].charAt(0) != 0) {
 67 
 68                     if (Integer.parseInt(temp[1]) <= 0 || Integer.parseInt(temp[1]) >= 300) {
 69                         System.out.println(temp[0] + " price out of range " + temp[1]);
 70                     }
 71                     menu.addDish(temp[0], Integer.parseInt(temp[1]), 'T');
 72                 } else {
 73                     System.out.println("wrong format");
 74                 }
 75             }
 76             if (length == 4) {
 77                 if (temp[0].equals("table")) {
 78                     if (temp[1].matches("[0-9]+") && temp[2].matches("[\\d]{4}/(\\d|\\d\\d)/(\\d|\\d\\d)") && temp[3].matches("[\\d]{2}/[\\d]{2}/[\\d]{2}")) {
 79                         if (temp[1].charAt(0) != 0) {
 80                             num++;
 81                             tables[num] = new Table();
 82                             tables[num].getDate(temp);
 83                             tables[num].getTime(temp);
 84                             if (tables[num].weekday >= 1 && tables[num].weekday <= 5) {
 85                                 if (tables[num].hours >= 17 && tables[num].hours < 20 || tables[num].hours == 20 && tables[num].minutes <= 30 || tables[num].hours >= 11 && tables[num].hours < 14 || tables[num].hours == 14 && tables[num].minutes <= 30 || tables[num].hours == 10 && tables[num].minutes >= 30) {
 86 
 87                                 } else {
 88                                     System.out.println("table " + tables[num].tablenum + " out of opening hours");
 89 
 90                                 }
 91                             }
 92                             if (tables[num].weekday >= 6 && tables[num].weekday <= 7) {
 93                                 if (tables[num].hours > 10 && tables[num].hours < 21 || tables[num].hours == 9 && tables[num].minutes >= 30 || tables[num].hours == 21 && tables[num].minutes <= 30) {
 94 
 95                                 } else {
 96                                     System.out.println("table " + tables[num].tablenum + " out of opening hours");
 97                                 }
 98                             }
 99                             if (tables[num].year >= 2022 && tables[num].year <= 2023 && tables[num].month >= 1 && tables[num].month <= 12 && tables[num].day > 0 && tables[num].day <= month_max[tables[num].month]) {
100                                 if (tables[num].tablenum < 1 || tables[num].tablenum > 55) {
101                                     System.out.println(temp[1] + " table num out of range");
102                                 } else {
103                                     System.out.println(temp[0] + " " + temp[1] + ": ");
104                                 }
105                             } else {
106                                 System.out.println("not a valid time period");
107                             }
108                         }
109                     } else {
110                         System.out.println("wrong format");
111                         k = 2;
112                         break;
113                     }
114                 } else {
115                     if (temp[2].matches("\\d") && temp[3].matches("[0-9]+") && temp[2].charAt(0) != 0 && temp[3].charAt(0) != 0) {
116                         if (tables[num].order.i >= 2 && Integer.parseInt(temp[0]) <= tables[num].order.records[tables[num].order.i-1].orderNum) {
117                             System.out.println("record serial number sequence error");
118                             t = 1;
119                             k = 1;
120                             continue;
121                         }
122                         if (menu.searthDish(temp[1]) == null) {
123                             System.out.println(temp[1] + " does not exist");
124                             t = 1;
125                             k = 1;
126                             continue;
127                         }
128 
129 
130                         if (menu.searthDish(temp[1]).isSpecial == 'F' && !temp[2].matches("1|2|3") || menu.searthDish(temp[1]).isSpecial == 'T' && !temp[2].matches("1|3")) {
131                             System.out.println(temp[0] + " portion out of range " + temp[2]);
132                             k = 1;
133                             t = 1;
134                             continue;
135                         }
136 
137                         if (Integer.parseInt(temp[3]) > 15) {
138                             System.out.println(temp[0] + " num out of range " + temp[3]);
139                             k = 1;
140                             t = 1;
141                             continue;
142                         }
143                         tables[num].order.addARecord(Integer.parseInt(temp[0]), temp[1], Integer.parseInt(temp[2]), Integer.parseInt(temp[3]), menu);
144                         if (t == 0) {
145                             System.out.println(temp[0] + " " + temp[1] + " " + tables[num].order.records[Integer.parseInt(temp[0])].getPrice());
146                         }
147                     } else {
148                         System.out.println("wrong format");
149                         k = 1;
150                     }
151 
152                 }
153             }
154 
155             if (length == 5) {
156                 if (!temp[0].matches("[\\d]+")) {
157                     System.out.println("wrong format");
158                     continue;
159                 }
160                 if (temp[2].matches("\\d") && temp[3].matches("[0-9]+") && temp[2].charAt(0) != 0 && temp[3].charAt(0) != 0) {
161                     if (tables[num].order.i > 2 && temp0[0].matches("[\\d]+") && Integer.parseInt(temp[0]) <= Integer.parseInt(temp0[0])) {
162                         System.out.println("record serial number sequence error");
163                         t = 1;
164                         k = 1;
165                         continue;
166                     }
167                     if (menu.searthDish(temp[1]) == null) {
168                         System.out.println(temp[1] + " does not exist");
169                         t = 1;
170                         k = 1;
171                         continue;
172                     }
173 
174 
175                     if (menu.searthDish(temp[1]).isSpecial == 'F' && !temp[2].matches("1|2|3") || menu.searthDish(temp[1]).isSpecial == 'T' && !temp[2].matches("1|3")) {
176                         System.out.println(temp[0] + " portion out of range " + temp[2]);
177                         k = 1;
178                         t = 1;
179                         continue;
180                     }
181 
182                     if (Integer.parseInt(temp[3]) > 15) {
183                         System.out.println(temp[0] + " num out of range " + temp[3]);
184                         k = 1;
185                         t = 1;
186                         continue;
187                     }
188                     tables[num].order.addARecord(Integer.parseInt(temp[0]), temp[1], Integer.parseInt(temp[2]), Integer.parseInt(temp[3]), menu);
189                     if (t == 0) {
190                         System.out.println(temp[0] + " " + temp[1] + " " + tables[num].order.records[Integer.parseInt(temp[0])].getPrice());
191                     } else {
192                         System.out.println("wrong format");
193                         k = 1;
194                     }
195                 }
196                 else{
197                     System.out.println("wrong format");
198                 }
199             }
200             temp0 = temp;
201 
202         }
203         for(int i = 1; i <= num; i ++) {
204             if (k == 1) {
205                 System.out.println("table " + tables[num].tablenum + ":" + " 0 0");
206             }
207             if (k == 0) {
208                 System.out.println("table " + tables[num].tablenum + ":" + " " + tables[num].order.getTotalPrice() + " " + tables[num].getPrice(totalprice));
209             }
210         }
211     }
212 
213 }
214 class Dish {
215     String name;//菜品名称
216     int unit_price = 0; //单价
217     char isSpecial;//是否为特色菜
218 
219     public Dish() {
220 
221         // TODO 自动生成的构造函数存根
222     }
223 
224     public Dish(String name, int unit_price, char a) {
225         this.name = name;
226         this.unit_price = unit_price;
227         this.isSpecial = a;
228     }
229 
230     int getPrice(int portion) {//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份) }
231         int price = 0;
232         if(portion == 1)
233             price = this.unit_price;
234         if(portion == 2)
235             price = Math.round((float)(this.unit_price * 1.5));
236         if(portion == 3)
237             price = this.unit_price * 2;
238         return price;
239     }
240 }
241 class Menu {
242     Dish[] dishes = new Dish[10];//菜品数组,保存所有菜品信息
243     int i = 0;
244 
245     public Menu() {
246 
247         // TODO 自动生成的构造函数存根
248     }
249 
250     public Menu(Dish[] dishes) {
251 
252         this.dishes = dishes;
253     }
254 
255     Dish searthDish(String dishName) {//根据菜名在菜谱中查找菜品信息,返回Dish对象。
256         int j;
257         int k = 0;
258         for(j = 0; j < i; j ++) {
259             if(dishes[j].name.equals(dishName)) {
260                 k = 1;
261                 break;
262             }
263         }
264         if(k == 1) {
265             return dishes[j];
266         }
267         else {
268             return null;
269         }
270     }
271 
272     Dish addDish(String dishName,int unit_price, char a) {//添加一道菜品信息
273         if(searthDish(dishName) != null){
274             i --;
275             dishes[i].unit_price = unit_price;
276             return dishes[i];
277         }
278         else {
279             dishes[i] = new Dish(dishName, unit_price, a);
280             i++;
281             return dishes[i];
282         }
283     }
284 
285 }
286 class Record {
287 
288     int orderNum;//序号\\
289     Dish d = new Dish();//菜品\\
290 
291     int portion;//份额(1/2/3代表小/中/大份)\\
292     int num;//份数
293 
294     public Record() {
295 
296         // TODO 自动生成的构造函数存根
297     }
298 
299     public Record(int orderNum, String dishName, int portion, int num, Menu m) {
300         this.orderNum = orderNum;
301         this.d = m.searthDish(dishName);
302         this.portion = portion;
303         this.num = num;
304     }
305 
306     int getPrice() {//计价,计算本条记录的价格\\
307         return d.getPrice(this.portion) * this.num;
308     }
309 }
310 class Order {
311 
312     Record[] records = new Record[10];//保存订单上每一道的记录
313     int j = 1;
314     int i = 1;
315 
316     public Order() {
317 
318         // TODO 自动生成的构造函数存根
319     }
320 
321     public Order(Record[] records) {
322 
323         this.records = records;
324     }
325 
326     int getTotalPrice() {//计算订单的总价
327         int sum = 0;
328         for(int j = 1; j < i; j ++) {
329 
330             sum += this.records[j].getPrice();
331         }
332         return sum;
333     }
334     Record addARecord(int orderNum,String dishName, int portion,int num, Menu menu) {//添加一条菜品信息到订单中。
335         records[i] = new Record(orderNum, dishName, portion, num, menu);
336         i++;
337         return records[i];
338 
339     }
340 
341     void delARecordByOrderNum(int orderNum) {//根据序号删除一条记录
342 
343         System.out.println("deduplication" + " " + orderNum);
344     }
345     Record findRecordByNum(String dishName) {//根据序号查找一条记录
346         int k = 0;
347         int i;
348         for (i = 1; i < records.length; i++) {
349             if (this.records[i].d.name.equals(dishName)) {
350                 k = 1;
351                 break;
352             }
353         }
354         if (k == 1) {
355             return this.records[i];
356         }
357         else {
358             return null;
359         }
360     }
361 }
362 
363 class Table {
364     int tablenum;
365     int year;
366     int month;
367     int day;
368     int hours;
369     int minutes;
370     int seconds;
371     int weekday;
372     Order order = new Order();
373     public Table() {
374 
375         // TODO 自动生成的构造函数存根
376     }
377 
378     public Table(int tablenum, int year, int month, int day, int hours, int minutes, int seconds, int weekday) {
379         this.tablenum = tablenum;
380         this.year = year;
381         this.month = month;
382         this.day = day;
383         this.hours = hours;
384         this.minutes = minutes;
385         this.seconds = seconds;
386         this.weekday = weekday;
387     }
388 
389     void tableIdentify() {
390 
391     }
392 
393     void getDate(String[] temp) {
394         String[] temp1 = temp[2].split("/");
395         this.tablenum = Integer.parseInt(temp[1]);
396         this.year = Integer.parseInt(temp1[0]);
397         this.month = Integer.parseInt(temp1[1]);
398         this.day = Integer.parseInt(temp1[2]);
399         Date date = null;
400         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
401         try {
402             date = format.parse(this.year + "-" + this.month + "-" + this.day);
403         } catch (ParseException e) {
404             e.printStackTrace();
405         }
406         Calendar calendar = Calendar.getInstance();
407         calendar.setTime(date);
408         int index = calendar.get(Calendar.DAY_OF_WEEK);
409         if(index == 1){
410             this.weekday = 7;
411         }
412         else {
413             this.weekday = index - 1;
414         }
415     }
416 
417     void getTime(String[] temp) {
418         String[] temp2 = temp[3].split("/");
419         this.hours = Integer.parseInt(temp2[0]);
420         this.minutes = Integer.parseInt(temp2[1]);
421         this.seconds = Integer.parseInt(temp2[2]);
422     }
423 
424     public boolean isExist(int num, String[] temp){
425         int k = 0;
426         for(int i = 1; i < num; i ++){
427             if(this.tablenum == Integer.parseInt(temp[0])){
428                 k = 1;
429                 break;
430             }
431         }
432         if(k == 1){
433             return true;
434         }
435         else {
436             return false;
437         }
438     }
439     int getPrice(int totalprice) {
440         int sum = 0;
441         if(this.weekday >= 1 && this.weekday <= 5){
442             if(this.hours >= 17 && this.hours <= 20 || this.hours == 20 && this.minutes <= 30){
443                 sum = (int)Math.round(totalprice * 0.8);
444             }
445             else if (this.hours >= 11 && this.hours <= 14 || this.hours == 14 && this.minutes <= 30 || this.hours == 10 && this.minutes >= 30){
446                 sum = (int)Math.round(totalprice * 0.6);
447             }
448             else {
449                 sum = totalprice;
450             }
451             return sum;
452         }
453 
454         return sum;
455     }
456 }
View Code

 

  三、总结

  在这三次作业中,我对于先前的循环、选择结构、输入、输出等已经十分熟练了,同时我也学到了许多新知识。

  关于next()是逐个输入;nextline()是逐行输入,可以对大量数据进行处理。对于字符串来说,正则表达式更是一大利器。通过正则表达式,我们能够对长文本指定在什么地方分隔成多个子字符串;设计好相应的匹配格式来匹配输入的数据是否符合我们想要的。此外LacalDate类和Calendar类可以对关于时间的数据进行处理,判断是否为闰年;通过输入的日期得到一年、一月、一星期中的第几天;判断两个日期的前后及之间间隔的天数。不过判断两个日期间隔的天数时只能在同一个月间判断,有一定缺陷。Integer类中的parseInt()等方法很实用,它能将字符串类型数据强制转化为其它类型。

  不过我认为其中最重要的还是集合及其子接口,关系图如下。我至今使用过的类有ArrayList、HashSet、LinkedHashSet,还只是粗略使用,真是不用不知道,用了直叫好。它们与数组相比有自己的优点也有相应的缺点。

List接口:其中的数据有顺序且可以重复。

  ArrayList:底层是数组,查询快,但增删慢。

  LinkedList:数组列表,采用数组方式存储,底层是链表,增删快,但查询慢。

  Vect:数组列表,添加同步锁,线程是安全的。

Set接口:数据没有顺序且不可重复,其中的元素无索引。

  HashSet:元素不能重复。

  TreeSet:

Map接口:将键映射到值的对象,一个映射不能包含重复的键,每个键最多只能映射到一个值。

HashMap:元素的key值不能重复;排列顺序是不固定的,可以存储一个为null的键。

TreeMap:元素保持着某种固定的顺序。

HashMap:实现了同步;不能存储为null的键。

  对于以上知识,相信系统学习后对编码有帮助。