一前言
题目集四主要考察的是对LocalDate,ArrayList,HashSet等Java自带类的使用
题目集五主要考察的是对正则表达式的使用,以及对其题目集三的时间题目的进行类结构的改变
题目集六只有一道题,主要是对题目集四的第一题进行加大难度
总的来说这几次的题目量比前面几次都要少,但是题目难度开始加深。
二设计与分析
由于部分题目过于简单,所以这里主要是对题目集四的7-1,题目集五的7-5,7-6,题目集六的7-1进行分析
题目集四7-1:当时见到这道题时认为难度较大且复杂,所以在这次题目集时放弃了写这道题,但由于题目集六的7-1题目是在此基础上的加深所以主要在后面分析
题目集五7-5:此题目主要是我们将前面题目集三的7-3的进行如下类图的类结构更改
所以我们主要是对类图进行分析,我们发现各个类之间是聚合的关系,每一个类与类之间聚合,所以我们按照此类图设计类时应该由year到month到day到DateUtil之间一个个写。最后的题目要求的主要功能则在DateUtil这里实现,因为这里是最后一个聚合。
代码如下:
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 year = 0; 6 int month = 0; 7 int day = 0; 8 9 int choice = input.nextInt(); 10 11 if (choice == 1) { // test getNextNDays method 12 int m = 0; 13 year = Integer.parseInt(input.next()); 14 month = Integer.parseInt(input.next()); 15 day = Integer.parseInt(input.next()); 16 17 DateUtil date = new DateUtil(year, month, day); 18 19 if (!date.checkInputValidity()) { 20 System.out.println("Wrong Format"); 21 System.exit(0); 22 } 23 24 m = input.nextInt(); 25 26 if (m < 0) { 27 System.out.println("Wrong Format"); 28 System.exit(0); 29 } 30 System.out.println(date.getNextNDays(m).showDate()); 31 } else if (choice == 2) { // test getPreviousNDays method 32 int n = 0; 33 year = Integer.parseInt(input.next()); 34 month = Integer.parseInt(input.next()); 35 day = Integer.parseInt(input.next()); 36 37 DateUtil date = new DateUtil(year, month, day); 38 39 if (!date.checkInputValidity()) { 40 System.out.println("Wrong Format"); 41 System.exit(0); 42 } 43 44 n = input.nextInt(); 45 46 if (n < 0) { 47 System.out.println("Wrong Format"); 48 System.exit(0); 49 } 50 System.out.println(date.getPreviousNDays(n).showDate()); 51 } else if (choice == 3) { //test getDaysofDates method 52 year = Integer.parseInt(input.next()); 53 month = Integer.parseInt(input.next()); 54 day = Integer.parseInt(input.next()); 55 56 int anotherYear = Integer.parseInt(input.next()); 57 int anotherMonth = Integer.parseInt(input.next()); 58 int anotherDay = Integer.parseInt(input.next()); 59 60 DateUtil fromDate = new DateUtil(year, month, day); 61 DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay); 62 63 if (fromDate.checkInputValidity() && toDate.checkInputValidity()) { 64 System.out.println(fromDate.getDaysofDates(toDate)); 65 } else { 66 System.out.println("Wrong Format"); 67 System.exit(0); 68 } 69 } 70 else{ 71 System.out.println("Wrong Format"); 72 System.exit(0); 73 } 74 } 75 public static class Day { 76 int value; 77 int mon_maxnum[]= new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};//数组储存各月份天数 78 Month month = new Month(); 79 Day(){ 80 81 } 82 Day(int yearValue,int monthValue,int dayValue){ 83 value = dayValue; 84 month = new Month( yearValue, monthValue); 85 } 86 87 public void setValue(int value) { 88 this.value = value; 89 } 90 91 public int getValue() { 92 return value; 93 } 94 95 public Month getMonth() { 96 return month; 97 } 98 99 public void setMonth(Month month) { 100 this.month = month; 101 } 102 public void resetMin(){ 103 value = 1; 104 } 105 public void resetMax(){ 106 if(month.year.isLeapYear()){ 107 mon_maxnum[2]=29; 108 }else{ 109 mon_maxnum[2]=28; 110 } 111 value = mon_maxnum[month.value]; 112 } 113 boolean validate(){ 114 if(value>mon_maxnum[month.value]||value<1){ 115 return false; 116 }else{ 117 return true; 118 } 119 } 120 public void dayIncrement(){ 121 value++; 122 } 123 public void dayReduction(){ 124 value--; 125 } 126 } 127 public static class Month { 128 int value; 129 Year year = new Year(); 130 Month(){ 131 132 } 133 Month(int yearValue,int monthValue){ 134 value = monthValue; 135 year.setValue(yearValue); 136 } 137 138 public void setValue(int value) { 139 this.value = value; 140 } 141 142 public int getValue() { 143 return value; 144 } 145 146 public Year getYear() { 147 return year; 148 } 149 150 public void setYear(Year year) { 151 this.year = year; 152 } 153 public void resetMin(){ 154 value = 1; 155 } 156 public void resetMax(){ 157 value = 12; 158 } 159 boolean validate(){ 160 if(value>12||value<1){ 161 return false; 162 }else{ 163 return true; 164 } 165 } 166 public void monthIncrement(){ 167 value++; 168 } 169 public void montReduction(){ 170 value--; 171 } 172 } 173 public static class Year { 174 int value; 175 Year(){ 176 177 } 178 Year(int value){ 179 this.value = value; 180 } 181 182 public int getValue() { 183 return value; 184 } 185 186 public void setValue(int value) { 187 this.value = value; 188 } 189 boolean validate(){ 190 if(value>2050||value<1900){ 191 return false; 192 }else{ 193 return true; 194 } 195 } 196 boolean isLeapYear(){ 197 if((value%4==0&&value%100!=0)||value%400==0){ 198 return true; 199 } 200 return false; 201 } 202 void yearIncrement(){ 203 value++; 204 } 205 void yearReduction(){ 206 value--; 207 } 208 } 209 public static class DateUtil { 210 Day day = new Day(); 211 DateUtil(){ 212 213 } 214 DateUtil(int y,int m,int d){ 215 day = new Day(y,m,d); 216 } 217 218 public Day getDay() { 219 return day; 220 } 221 222 public void setDay(Day day) { 223 this.day = day; 224 } 225 boolean checkInputValidity(){ 226 if(day.month.year.isLeapYear()){ 227 day.mon_maxnum[2]=29; 228 } 229 if(day.month.year.value<1900||day.month.year.value>2050||day.month.value<1||day.month.value>12||day.value<1||day.value>day.mon_maxnum[day.month.value]){ 230 return false; 231 } 232 else{ 233 return true; 234 } 235 } 236 boolean compareDates(DateUtil date){ 237 if(this.day.month.year.value>date.day.month.year.value){ 238 return true; 239 }else if(this.day.month.year.value==date.day.month.year.value){ 240 if(this.day.month.value>date.day.month.value){ 241 return true; 242 }else if(this.day.month.value==date.day.month.value){ 243 if(this.day.value>date.day.value){ 244 return true; 245 }else{ 246 return false; 247 } 248 } 249 }else if(this.day.month.year.value<date.day.month.year.value){ 250 return false; 251 } 252 return false; 253 } 254 boolean equalTwoDates(DateUtil date){ 255 if(this.day.month.year.value==date.day.month.year.value&&this.day.month.value==date.day.month.value&&this.day.value==date.day.value){ 256 return true; 257 }else{ 258 return false; 259 } 260 } 261 262 DateUtil getNextNDays(int n){ 263 int i; 264 for(i=0;i<n;i++) { 265 if (day.month.year.isLeapYear()) { 266 day.mon_maxnum[2] = 29; 267 } else { 268 day.mon_maxnum[2] = 28; 269 } 270 if (day.value != day.mon_maxnum[day.month.value]) { 271 day.dayIncrement(); 272 273 } else { 274 if (day.month.value == 12) { 275 day.month.resetMin(); 276 day.resetMin(); 277 day.month.year.yearIncrement(); 278 } else { 279 day.resetMin(); 280 day.month.monthIncrement(); 281 } 282 } 283 } 284 return this; 285 } 286 DateUtil getPreviousNDays(int n){ 287 int i; 288 for(i=0;i<n;i++){ 289 if(day.month.year.isLeapYear()){ 290 day.mon_maxnum[2]=29; 291 }else{ 292 day.mon_maxnum[2]=28; 293 } 294 if(day.value!=1){ 295 day.dayReduction();//判断月份是否需要变化 296 297 }else{ 298 if(day.month.value==1){//判断年份是否需要变化 299 day.month.value=12; 300 day.value=31; 301 day.month.year.yearReduction(); 302 }else{ 303 day.month.montReduction(); 304 day.value = day.mon_maxnum[day.month.value]; 305 } 306 } 307 } 308 return this; 309 } 310 int getDaysofDates(DateUtil date){ 311 int i,n=0; 312 int j=365,k=366; 313 if(this.compareDates(date)){//比较两个日期判断是否进行进行更换操作 314 if(this.day.month.year.value==date.day.month.year.value){ 315 if(day.month.year.isLeapYear()){ 316 day.mon_maxnum[2]=29; 317 }else{ 318 day.mon_maxnum[2]=28; 319 } 320 for(i=date.day.month.value+1;i<this.day.month.value;i++){ 321 n=n+day.mon_maxnum[i]; 322 } 323 n=n+day.mon_maxnum[date.day.month.value]-date.day.value+this.day.value; 324 }else{ 325 for(i=date.day.month.year.value+1;i<this.day.month.year.value;i++){ 326 Year flag = new Year(i); 327 if(flag.isLeapYear()){ 328 n=n+k; 329 }else{ 330 n=n+j; 331 } 332 } 333 for(i=date.day.month.value+1;i<=12;i++){ 334 if(day.month.year.isLeapYear()){ 335 day.mon_maxnum[2]=29; 336 }else{ 337 day.mon_maxnum[2]=28; 338 } 339 n=n+day.mon_maxnum[i]; 340 } 341 for(i=1;i<this.day.month.value;i++){ 342 if(day.month.year.isLeapYear()){ 343 day.mon_maxnum[2]=29; 344 }else{ 345 day.mon_maxnum[2]=28; 346 } 347 n=n+day.mon_maxnum[i]; 348 } 349 n=n+day.mon_maxnum[date.day.month.value]-date.day.value+this.day.value; 350 } 351 }else{ 352 int z,x,c;//中间变量更换日期 353 z=this.day.month.year.value; 354 x=this.day.month.value; 355 c=this.day.value; 356 this.day.month.year.value=date.day.month.year.value; 357 this.day.month.value=date.day.month.value; 358 this.day.value=date.day.value; 359 if(this.day.month.year.value==z){ 360 if(day.month.year.isLeapYear()){ 361 day.mon_maxnum[2]=29; 362 }else{ 363 day.mon_maxnum[2]=28; 364 } 365 if(this.day.value==c){//判断是否日期相等 366 n=0; 367 }else { 368 for(i=x+1;i<this.day.month.value;i++){ 369 n=n+day.mon_maxnum[i]; 370 } 371 n=n+day.mon_maxnum[x]-c+this.day.value; 372 } 373 }else{ 374 for(i=z+1;i<this.day.month.year.value;i++){ 375 Year flag = new Year(i); 376 if(flag.isLeapYear()){ 377 n=n+k; 378 }else{ 379 n=n+j; 380 } 381 } 382 for(i=x+1;i<=12;i++){ 383 if(day.month.year.isLeapYear()){ 384 day.mon_maxnum[2]=29; 385 }else{ 386 day.mon_maxnum[2]=28; 387 } 388 n=n+day.mon_maxnum[i]; 389 } 390 for(i=1;i<this.day.month.value;i++){ 391 if(day.month.year.isLeapYear()){ 392 day.mon_maxnum[2]=29; 393 }else{ 394 day.mon_maxnum[2]=28; 395 } 396 n=n+day.mon_maxnum[i]; 397 } 398 n=n+day.mon_maxnum[x]-c+this.day.value; 399 } 400 401 } 402 return n; 403 } 404 String showDate(){ 405 String showDate; 406 showDate=day.month.year.value+"-"+day.month.value+"-"+day.value; 407 return showDate; 408 } 409 410 } 411 }
题目集五7-6
这题与前面题目相差不多主要是按照题目要求的类图结构进行更改
我们对类图进行分析,我们发现这题与前面的题目相似的是:类之间的关系仍然是聚合,不一样的是:类聚合不在是一个类一个类的聚合而是别的类聚合一个主要菜单实现功能的类。
代码如下:
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 year = 0; 6 int month = 0; 7 int day = 0; 8 9 int choice = input.nextInt(); 10 11 if (choice == 1) { // test getNextNDays method 12 int m = 0; 13 year = Integer.parseInt(input.next()); 14 month = Integer.parseInt(input.next()); 15 day = Integer.parseInt(input.next()); 16 17 DateUtil date = new DateUtil(year, month, day); 18 19 if (!date.checkInputValidity()) { 20 System.out.println("Wrong Format"); 21 System.exit(0); 22 } 23 24 m = input.nextInt(); 25 26 if (m < 0) { 27 System.out.println("Wrong Format"); 28 System.exit(0); 29 } 30 31 System.out.print(date.year.value + "-" + date.month.value + "-" + date.day.value + " next " + m + " days is:"); 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.print( 54 date.year.value + "-" + date.month.value + "-" + date.day.value + " previous " + n + " days is:"); 55 System.out.println(date.getPreviousNDays(n).showDate()); 56 } else if (choice == 3) { //test getDaysofDates method 57 year = Integer.parseInt(input.next()); 58 month = Integer.parseInt(input.next()); 59 day = Integer.parseInt(input.next()); 60 61 int anotherYear = Integer.parseInt(input.next()); 62 int anotherMonth = Integer.parseInt(input.next()); 63 int anotherDay = Integer.parseInt(input.next()); 64 65 DateUtil fromDate = new DateUtil(year, month, day); 66 DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay); 67 68 if (fromDate.checkInputValidity() && toDate.checkInputValidity()) { 69 System.out.println("The days between " + fromDate.showDate() + 70 " and " + toDate.showDate() + " are:" 71 + fromDate.getDaysofDates(toDate)); 72 } else { 73 System.out.println("Wrong Format"); 74 System.exit(0); 75 } 76 } 77 else{ 78 System.out.println("Wrong Format"); 79 System.exit(0); 80 } 81 } 82 public static class Day { 83 Day(){ 84 85 } 86 Day(int value){ 87 88 } 89 int value; 90 91 public void setValue(int value) { 92 this.value = value; 93 } 94 95 public int getValue() { 96 return value; 97 } 98 void dayIncrement(){ 99 value++; 100 } 101 void dayReduction(){ 102 value--; 103 } 104 } 105 public static class Year { 106 int value; 107 Year(){ 108 109 } 110 Year(int value){ 111 this.value = value; 112 } 113 114 public int getValue() { 115 return value; 116 } 117 118 public void setValue(int value) { 119 this.value = value; 120 } 121 boolean validate(){ 122 if(value>2020||value<1820){ 123 return false; 124 }else{ 125 return true; 126 } 127 } 128 boolean isLeapYear(){ 129 if((value%4==0&&value%100!=0)||value%400==0){ 130 return true; 131 } 132 return false; 133 } 134 void yearIncrement(){ 135 value++; 136 } 137 void yearReduction(){ 138 value--; 139 } 140 } 141 public static class Month { 142 Month(){ 143 144 } 145 Month(int value){ 146 this.value = value; 147 } 148 int value; 149 150 public int getValue() { 151 return value; 152 } 153 154 public void setValue(int value) { 155 this.value = value; 156 } 157 boolean validate(){ 158 if(value>12||value<1){ 159 return false; 160 }else{ 161 return true; 162 } 163 } 164 public void monthIncrement(){ 165 value++; 166 } 167 public void montReduction(){ 168 value--; 169 } 170 public void resetMin(){ 171 value = 1; 172 } 173 public void resetMax(){ 174 value = 12; 175 } 176 } 177 public static class DateUtil { 178 Day day = new Day(); 179 Month month = new Month(); 180 Year year = new Year(); 181 int mon_maxnum[]= new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};//数组储存各月份天数 182 DateUtil(){ 183 184 } 185 DateUtil(int y, int m,int d){ 186 this.day.value = d; 187 this.month.value = m; 188 this.year.value = y; 189 } 190 191 public Year getYear() { 192 return year; 193 } 194 195 public void setYear(Year year) { 196 this.year = year; 197 } 198 199 public Month getMonth() { 200 return month; 201 } 202 203 public void setMonth(Month month) { 204 this.month = month; 205 } 206 207 public Day getDay() { 208 return day; 209 } 210 211 public void setDay(Day day) { 212 this.day = day; 213 } 214 public void setDayMin(){ 215 day.value = 1; 216 } 217 public void setDayMax(){ 218 if(year.isLeapYear()){ 219 mon_maxnum[2] = 29; 220 }else{ 221 mon_maxnum[2] = 28; 222 } 223 day.value = mon_maxnum[month.value]; 224 } 225 public boolean checkInputValidity(){ 226 if(year.isLeapYear()){ 227 mon_maxnum[2] = 29; 228 }else{ 229 mon_maxnum[2] = 28; 230 } 231 if(year.validate()&&month.validate()&&day.value>=1&&day.value<=mon_maxnum[month.value]){ 232 return true; 233 }else { 234 return false; 235 } 236 } 237 DateUtil getNextNDays(int n){ 238 int i; 239 for(i=0;i<n;i++) { 240 if (year.isLeapYear()) { 241 mon_maxnum[2] = 29; 242 } else { 243 mon_maxnum[2] = 28; 244 } 245 if (day.value != mon_maxnum[month.value]) { 246 day.dayIncrement(); 247 248 } else { 249 if (month.value == 12) { 250 month.resetMin(); 251 setDayMin(); 252 year.yearIncrement(); 253 } else { 254 setDayMin(); 255 month.monthIncrement(); 256 } 257 } 258 } 259 return this; 260 } 261 DateUtil getPreviousNDays(int n){ 262 int i; 263 for(i=0;i<n;i++){ 264 if(year.isLeapYear()){ 265 mon_maxnum[2]=29; 266 }else{ 267 mon_maxnum[2]=28; 268 } 269 if(day.value!=1){ 270 day.dayReduction(); 271 272 }else{ 273 if(month.value==1){ 274 month.value=12; 275 day.value=31; 276 year.yearReduction(); 277 }else{ 278 month.montReduction(); 279 day.value = mon_maxnum[month.value]; 280 } 281 } 282 } 283 return this; 284 } 285 boolean compareDates(DateUtil date) { 286 if (this.year.value > date.year.value) 287 return true; 288 else if (this.year.value == date.year.value) { 289 if (this.month.value > date.month.value) 290 return true; 291 else if (this.month.value == date.month.value) { 292 if (this.day.value > date.day.value) 293 return true; 294 else 295 return false; 296 } 297 } else if (this.year.value < date.year.value) 298 return false; 299 return false; 300 } 301 boolean equalTwoDates(DateUtil date){ 302 if(this.year.value==date.year.value&&this.month.value==date.month.value&&this.day.value==date.day.value){ 303 return true; 304 }else{ 305 return false; 306 } 307 } 308 int getDaysofDates(DateUtil date){ 309 int i,n=0; 310 int j=365,k=366; 311 if(this.compareDates(date)){//比较两个日期判断是否进行进行更换操作 312 if(this.year.value==date.year.value){ 313 if(this.year.isLeapYear()){ 314 mon_maxnum[2]=29; 315 }else{ 316 mon_maxnum[2]=28; 317 } 318 for(i=date.month.value+1;i<this.month.value;i++){ 319 n=n+mon_maxnum[i]; 320 } 321 n=n+mon_maxnum[date.month.value]-date.day.value+this.day.value; 322 }else{ 323 for(i=date.year.value+1;i<this.year.value;i++){ 324 Year flag = new Year(i); 325 if(flag.isLeapYear()){ 326 n=n+k; 327 }else{ 328 n=n+j; 329 } 330 } 331 for(i=date.month.value+1;i<=12;i++){ 332 if(year.isLeapYear()){ 333 mon_maxnum[2]=29; 334 }else{ 335 mon_maxnum[2]=28; 336 } 337 n=n+mon_maxnum[i]; 338 } 339 for(i=1;i<this.month.value;i++){ 340 if(year.isLeapYear()){ 341 mon_maxnum[2]=29; 342 }else{ 343 mon_maxnum[2]=28; 344 } 345 n=n+mon_maxnum[i]; 346 } 347 n=n+mon_maxnum[date.month.value]-date.day.value+this.day.value; 348 } 349 }else{ 350 int z,x,c;//中间变量更换日期 351 z=this.year.value; 352 x=this.month.value; 353 c=this.day.value; 354 this.year.value=date.year.value; 355 this.month.value=date.month.value; 356 this.day.value=date.day.value; 357 if(this.year.value==z){ 358 if(year.isLeapYear()){ 359 mon_maxnum[2]=29; 360 }else{ 361 mon_maxnum[2]=28; 362 } 363 if(this.day.value==c){//判断是否日期相等 364 n=0; 365 }else { 366 for(i=x+1;i<this.month.value;i++){ 367 n=n+mon_maxnum[i]; 368 } 369 n=n+mon_maxnum[x]-c+this.day.value; 370 } 371 }else{ 372 for(i=z+1;i<this.year.value;i++){ 373 Year flag = new Year(i); 374 if(flag.isLeapYear()){ 375 n=n+k; 376 }else{ 377 n=n+j; 378 } 379 } 380 for(i=x+1;i<=12;i++){ 381 if(year.isLeapYear()){ 382 mon_maxnum[2]=29; 383 }else{ 384 mon_maxnum[2]=28; 385 } 386 n=n+mon_maxnum[i]; 387 } 388 for(i=1;i<this.month.value;i++){ 389 if(year.isLeapYear()){ 390 mon_maxnum[2]=29; 391 }else{ 392 mon_maxnum[2]=28; 393 } 394 n=n+mon_maxnum[i]; 395 } 396 n=n+mon_maxnum[x]-c+this.day.value; 397 } 398 399 } 400 return n; 401 } 402 String showDate(){ 403 String showDate; 404 showDate=year.value+"-"+month.value+"-"+day.value; 405 return showDate; 406 } 407 } 408 }
题目集六 7-1
此题与前面的题目集四7-1的大体相同,但是加入了带点菜功能,特色菜和各种异常情况处理,所以这道题是及其复杂的,但是我们首先明白一个程序的运行离不开的是输入,进行运行,输出。所以我们一步一步慢慢来就行。
首先我们对题目进行分析我们发现这个题目的输入方式为直到输入end才会结束,所以显而易见的我们这里要用一个循环输入,且要判断输入end的时候结束循环,所以我们要以end为标识符作为首要判断,根据题目的空格输入我们就应该明白这里我们要使用split方法,进行去空格产生字符数组,根据数组的大小内容进行判断进行相应方法。
代码如下:
while (true){ String s1 = input.nextLine(); s = s1.split(" "); if(s1.equals("end")){ break; } flag = s.length; if(flag == 2){ if(s[1].equals("delete")){ a1 = Integer.parseInt(s[0]); if (table[a4].order.findRecordByNum(a1)!=null) { if(table[a4].order.findRecordByNum(a1).portion != 0) { a6 = table[a4].order.findRecordByNum(a1).getPrice(); table[a4].order.findRecordByNum(a1).portion = 0; if(table[a4].order.findRecordByNum(a1).d.flag!=0) { table[a4].sum = table[a4].sum - a6; table[a4].order.findRecordByNum(a1).portion = 0; }else { float h = a6*0.7f; table[a4].sum = table[a4].sum - Math.round(h); } }else { System.out.println("deduplication "+a1); } }else { System.out.println("delete error"); } }else{ if(table[a4] == null) { dishName = s[0]; if( s[1].matches(ruller)) { a1 = Integer.parseInt(s[1]); menu.dishs[a5] = menu.addDish(dishName, a1); a5++; }else { System.out.println("wrong format"); } }else { System.out.println("invalid dish"); } } } else if (flag == 3){ if(s[2].equals("T")){ dishName = s[0]; a1 = Integer.parseInt(s[1]); menu.dishs[a5] = menu.addDish(dishName,a1); menu.dishs[a5].flag = 0; a5++; } } else if (flag == 4) { if(s[0].equals("table")){ if( s[1].matches(ruller)) { a4 = Integer.parseInt(s[1]); if(a4<=55&&a4>=1) { table[a4] = new Table(a4); table[a4].Doing(s[2], s[3]); if (table[a4].checkTime()) { System.out.println("table" + " " + table[a4].tableNum + ":" + " "); } else { System.out.println(a4 + " date error"); } }else { System.out.println(s[1]+" table num out of range"); } }else { System.out.println("wrong format"); } }else { a1 = Integer.parseInt(s[0]); dishName = s[1]; a2 = Integer.parseInt(s[2]); a3 = Integer.parseInt(s[3]); if (table[a4] != null) { if (a2 >= 1 && a2 <= 3 && a3 > 0 && a3 <= 15) { Dish otherDish = menu.searthDish(dishName); if (otherDish != null) { table[a4].order.addARecord(a1, dishName, a2, a3); table[a4].order.findRecordByNum(a1).d = otherDish; a7 = table[a4].order.findRecordByNum(a1).getPrice(); if (table[a4].order.findRecordByNum(a1).d.flag == -1) { float k = a7 * table[a4].discount; int t = Math.round(k); table[a4].sum += t; } else { if (table[a4].week <= 5 && table[a4].week >= 1) { float v = a7 * 0.7f; table[a4].sum += Math.round(v); } else if (table[a4].week == 6 || table[a4].week == 7) { table[a4].sum += a7; } } System.out.println(a1 + " " + dishName + " " + table[a4].order.findRecordByNum(a1).getPrice()); } else { } } else if (a2 < 1 || a2 > 3 && a3 > 0 && a3 <= 15) { System.out.println(a1 + " portion out of range " + a2); } else if (a3 < 0 || a3 > 15) { System.out.println(a1 + " num out of range " + a3); } }else { } } } else if (flag ==5) { }else { System.out.println("wrong format"); } }
我们对输入数据进行判断后我们应该要进行对主要方法的实现,所以我们要开始对其他类的设计,如dish,tabale等类
我们首先判断类与类之间的关系,我们可以知道各个类之间的关系为依赖,所以可以开始对各个类进行实现,代码如下
1 class Dish { 2 String name = " " ;//菜品名称 3 4 int unit_price = 0; //单价 5 int flag = -1; 6 int portion; 7 Dish(){ 8 9 } 10 Dish(String name,int unit_price){ 11 this.name = name; 12 this.unit_price = unit_price; 13 } 14 int getPrice(int portion){ 15 int p2 = 0; 16 if(portion == 1){ 17 p2 = this.unit_price; 18 }else if (portion == 2){ 19 float p1 = (float) (this.unit_price * 1.5); 20 p2 = Math.round(p1); 21 }else if (portion == 3){ 22 float p1 = this.unit_price * 2; 23 p2 = Math.round(p1); 24 }else { 25 26 } 27 return p2; 28 }//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份) 29 30 public void setName(String name) { 31 this.name = name; 32 } 33 34 public String getName() { 35 return name; 36 } 37 38 public int getUnit_price() { 39 return unit_price; 40 } 41 42 public void setUnit_price(int unit_price) { 43 this.unit_price = unit_price; 44 } 45 } 46 class Menu { 47 Dish[] dishs = new Dish[10];//菜品数组,保存所有菜品信息 48 int count = 0; 49 Dish searthDish(String dishName){ 50 Dish temd = null; 51 for(int i=count-1;i>=0;i--){ 52 if(dishName.equals(dishs[i].name)){ 53 temd = dishs[i]; 54 break; 55 } 56 } 57 if(temd==null){ 58 System.out.println(dishName+" does not exist"); 59 } 60 return temd; 61 }//根据菜名在菜谱中查找菜品信息,返回Dish对象。 62 Dish addDish(String dishName,int unit_price){ 63 Dish dh = new Dish(); 64 dh.name = dishName; 65 dh.unit_price = unit_price; 66 count++; 67 return dh; 68 }//添加一道菜品信息 69 } 70 class Record { 71 int orderNum;//序号 72 int Num = 1;//份额 73 Dish d = new Dish();//菜品\\ 74 int portion = 0;//份额(1/2/3代表小/中/大份) 75 Record(){ 76 77 } 78 Record(int orderNum,int num,int portion){ 79 80 this.orderNum = orderNum; 81 this.Num = num; 82 this.portion = portion; 83 } 84 85 86 87 int getPrice(){ 88 return d.getPrice(portion)*Num; 89 }//计价,计算本条记录的价格 90 91 public Dish getD() { 92 return d; 93 } 94 95 public int getNum() { 96 return Num; 97 } 98 99 public int getOrderNum() { 100 return orderNum; 101 } 102 103 public int getPortion() { 104 return portion; 105 } 106 107 public void setD(Dish d) { 108 d = new Dish(); 109 this.d = d; 110 } 111 112 public void setNum(int num) { 113 Num = num; 114 } 115 116 public void setOrderNum(int orderNum) { 117 this.orderNum = orderNum; 118 } 119 120 public void setPortion(int portion) { 121 this.portion = portion; 122 } 123 124 } 125 class Table { 126 int mon_maxnum[]= new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};//数组储存各月份天数 127 Table(){ 128 129 } 130 Table(int tableNum){ 131 this.tableNum = tableNum; 132 } 133 Table(Order order){ 134 this.order = order; 135 } 136 Order order = new Order(); 137 String time; 138 int week; 139 int hour,minute,second,year,month,day; 140 float discount = -1; 141 int sum = 0; 142 int tableNum = -1; 143 //处理时间 144 public void processTime(String time1,String time2){ 145 DateTimeFormatter JEFormatter = DateTimeFormatter.ofPattern("yyyy/M/d"); 146 LocalDate l1 = LocalDate.parse(time1,JEFormatter); 147 week = l1.getDayOfWeek().getValue(); 148 String[] time3 = time2.split("/"); 149 hour = Integer.parseInt(time3[0]); 150 minute = Integer.parseInt(time3[1]); 151 second = Integer.parseInt(time3[2]); 152 String[] time4 = time1.split("/"); 153 year = Integer.parseInt(time4[0]); 154 month = Integer.parseInt(time4[1]); 155 day = Integer.parseInt(time4[2]); 156 } 157 public void setDiscount(){ 158 if(week<=5&&week>=1){ 159 if(hour>=17&&hour<20) 160 discount=0.8F; 161 else if(hour==20&&minute<30) 162 discount=0.8F; 163 else if(hour==20&&minute==30&&second==0) 164 discount=0.8F; 165 else if(hour>=11&&hour<=13||hour==10&&minute>=30) 166 discount=0.6F; 167 else if(hour==14&&minute<30) 168 discount=0.6F; 169 else if(hour==14&&minute==30&&second==0) 170 discount=0.6F; 171 }else{ 172 if(hour>=10&&hour<=20) 173 discount= 1.0F; 174 else if(hour==9&&minute>=30) 175 discount= 1.0F; 176 else if(hour==21&&minute<30||hour==21&&minute==30&&second==0) 177 discount= 1.0F; 178 } 179 } 180 public int getMoney(){ 181 return Math.round(sum * discount); 182 } 183 public void Doing(String time1,String time2){ 184 processTime(time1,time2); 185 setDiscount(); 186 187 } 188 189 public void setTableNum(int tableNum) { 190 this.tableNum = tableNum; 191 } 192 193 public void setOrder(Order order) { 194 this.order = order; 195 } 196 197 public Order getOrder() { 198 return order; 199 } 200 public void setSum(){ 201 sum = this.order.getTotalPrice(); 202 } 203 void Gettottalprice(){ 204 if(discount>0){ 205 System.out.println("table " + tableNum + ": " + this.order.getTotalPrice()+" "+ sum); 206 }else { 207 System.out.println("table " + tableNum + " out of opening hours"); 208 } 209 } 210 boolean checkTime(){ 211 if(year<1000||month<1||month>12||day<0||day>mon_maxnum[month]||hour<1||hour>24||minute<0||minute>60||second<0||second>60){ 212 return false; 213 }else { 214 return true; 215 } 216 } 217 boolean checkTimeRange(){ 218 if(year<2022||year>2023){ 219 return false; 220 }else { 221 return true; 222 } 223 } 224 public int getWeek() { 225 return week; 226 } 227 } 228 class Order { 229 private List<Record> records = new ArrayList<>(); //保存订单上每一道的记录 230 int totalPrice; 231 int tableNum; 232 Menu menu = new Menu(); 233 int getTotalPrice(){ 234 for(Record record : records){ 235 totalPrice = record.getPrice()+totalPrice; 236 } 237 return totalPrice; 238 }//计算订单的总价 239 240 Record addARecord(int orderNum,String dishName,int portion,int num){ 241 Record record = new Record(orderNum,num,portion); 242 record.d.setName(dishName); 243 records.add(record); 244 return record; 245 }//添加一条菜品信息到订单中。 246 247 void delARecordByOrderNum(int orderNum){ 248 for(Record record : records){ 249 if(record.getOrderNum() == orderNum){ 250 records.remove(record); 251 } 252 } 253 }//根据序号删除一条记录 254 255 Record findRecordByNum(int orderNum){ 256 for(Record record : records){ 257 if(record.getOrderNum() == orderNum){ 258 return record; 259 } 260 } 261 return null; 262 }//根据序号查找一条记录 263 264 public int getTableNum() { 265 return tableNum; 266 } 267 268 public void setRecords(ArrayList<Record> records) { 269 this.records = records; 270 } 271 272 public void setTableNum(int tableNum) { 273 this.tableNum = tableNum; 274 } 275 276 public List<Record> getRecords() { 277 return records; 278 } 279 280 public void setMenu(Menu menu) { 281 this.menu = menu; 282 }
在各个类中我设置了各个类数组,有用Arreylist数组可以简单的实现添加删除功能,以及对输入的时间处理时用的时间类LocalDate处理得到星期数,对时间的处理,和对价格的具体处理则这里不进行赘述请观察代码。
最后我们对题目在进一步深入剖解,发现我们这道题可以一个个输出,及每输入一个内容就输出一个结果,因为每个部分正好切合,感觉是出题老师的降低难度,不然应该是保存每一个输入再进行输出,这里由于我想着简单点所以就使用一个个输出,代码请观察上面的循环输入代码。
这题的算法并不难,主要考察的是我们对题目的剖析能力,能不能理解题目的意思以及考虑全面,对各个情况的分析,这道题由于自己过于放松导致时间不足只拿到了36分,并没有实现带点菜功能和部分异常情况使得我有点后悔,因为在后面看来这道题目的难度真的不是做不出来的地步,主要是自己不愿意花时间不说满分,但是80分肯定是可以拿到的。
三 踩坑心得
这里对题目集五的7-5和7-6题不进行说明,因为主要是对类结构的更改,并没有出现错误。
题目集六7-1:
1 出现空指针的情况:
进行相关错误查找发现是在设置menu类的数组时发现menu类中的dish数组有时并没有设置,即为空,这里我就把使用arrylist数组改为使用正常数组
更改前:
1 public static class Menu { 2 private ArrayList<Dish> dishs = new ArrayList<>();//菜品数组,保存所有菜品信息 3 4 Dish searthDish(String dishName){ 5 for(Dish dish : dishs){ 6 if(dish.getName() == dishName){ 7 return dish; 8 } 9 } 10 return null; 11 }//根据菜名在菜谱中查找菜品信息,返回Dish对象。 12 13 Dish addDish(String dishName,int unit_price){ 14 for(Dish dish : dishs){ 15 if(dish.getName() == dishName){ 16 dish.setUnit_price(unit_price); 17 } 18 return dish; 19 } 20 Dish dish = new Dish(dishName,unit_price); 21 dishs.add(dish); 22 return dish; 23 }//添加一道菜品信息 24 }
更改后:
1 class Menu { 2 Dish[] dishs = new Dish[10];//菜品数组,保存所有菜品信息 3 int count = 0; 4 Dish searthDish(String dishName){ 5 Dish temd = null; 6 for(int i=count-1;i>=0;i--){ 7 if(dishName.equals(dishs[i].name)){ 8 temd = dishs[i]; 9 break; 10 } 11 } 12 if(temd==null){ 13 System.out.println(dishName+" does not exist"); 14 } 15 return temd; 16 }//根据菜名在菜谱中查找菜品信息,返回Dish对象。 17 Dish addDish(String dishName,int unit_price){ 18 Dish dh = new Dish(); 19 dh.name = dishName; 20 dh.unit_price = unit_price; 21 count++; 22 return dh; 23 }//添加一道菜品信息 24 }
2 计算的算法出现错误
经过测试计算每个桌的价格都是错的,对代码查看,发现是我在得出价格的方法里直接乘以了折扣,但是特色菜的折扣不一样,所以应该在循环中进行折扣的计算而不是在table类里直接乘以折扣
更改前的价格计算:
1 void Gettottalprice(){ 2 if(discount>0){ 3 System.out.println("table " + tableNum + ": " + this.order.getTotalPrice()+" "+ Math.round(sum*discount)); 4 }else { 5 System.out.println("table " + tableNum + " out of opening hours"); 6 } 7 }
更改后的价格计算:
1 if (a2 >= 1 && a2 <= 3 && a3 > 0 && a3 <= 15) { 2 Dish otherDish = menu.searthDish(dishName); 3 if (otherDish != null) { 4 table[a4].order.addARecord(a1, dishName, a2, a3); 5 table[a4].order.findRecordByNum(a1).d = otherDish; 6 a7 = table[a4].order.findRecordByNum(a1).getPrice(); 7 if (table[a4].order.findRecordByNum(a1).d.flag == -1) { 8 float k = a7 * table[a4].discount; 9 int t = Math.round(k); 10 table[a4].sum += t; 11 } else { 12 if (table[a4].week <= 5 && table[a4].week >= 1) { 13 float v = a7 * 0.7f; 14 table[a4].sum += Math.round(v); 15 } else if (table[a4].week == 6 || table[a4].week == 7) { 16 table[a4].sum += a7; 17 } 18 }
四 改进建议
对于题目集五的两道题,我的算法过于简单导致代码行数冗杂,可以对算法部分进行更改使代码量减少。对于题目集六的这道题,我的主函数里的代码过多,应该使用MVC模式进行更改使主函数里不这么复杂,且应该使用存储输入的方式进行输出,这样更加高效且错误率低。
五 总结
对于本三次题目集,我学会了对正则表达式的使用,学会了对类结构进一步认识,对聚合的使用。最重要的是体会到了不要对一道看起来难的题目放弃,只要逐渐剖析付出时间这道难题也会解决,在日后的学习中我要不能这么容易退缩,对于题目要多付出时间,来解决相应问题。
标签:总结,题目,int,System,value,else,PTA,return,month From: https://www.cnblogs.com/rccxs/p/17364904.html