1. 程式人生 > 其它 >Java題目集總結6~8

Java題目集總結6~8

1.前言

  經過這個階段的學習,我們學習了很多具有拓展性、延申性的知識,需要學習和鞏固的知識點也還有很多,此次是最後一次blog總結,但是對我們的Java學習之路來說才剛開始。。。 (( ´Д`)y━・~~)。相較於在第一、二階段的作業而言,此次作業更加針對於在面向物件過程中的三大技術特性,即封裝性、繼承性和多型性,類圖的設計更加普遍。在不斷深入學習Java的過程又延伸到了Javafx,不僅能把圖片放在視窗還可以通過Java程式碼的撰寫來組合建立,充分發揮自我的想象。通過學習類屬性的訪問許可權,瞭解到了四種訪問許可權:public,private,protected,default;以及不同類的型別:實體類,業務類,介面類;對於類設計上,學習的多,代表思路與程式設計思想變得更為開闊且嚴謹,開始真正考慮到如何將一個題目設計到恰如其分,不同的設計思路帶來的不同效果。對於這三次的題目集來講,第一次難度較為大,但依照老師題中所給的類圖編寫程式碼,難度相對降低了不少,後面兩次也逐漸簡單,耗費的時間也相比第一次少了不少,這幾次題目集的數量較少,基本為兩到三道題。題目中所用到的主要知識點為容器的儲存與輸出,同時還有正則表示式的運用,Date類裡面方法的使用和Date型別轉化為long型別進行計算以及利用介面排序的方法等等。

2.設計與分析:

7-1 電信計費系列1-座機計費:

  1 import java.text.DecimalFormat;
  2 import java.math.RoundingMode;
  3 import java.text.ParseException;
  4 import java.text.SimpleDateFormat;
  5 import java.util.*;
  6 import java.util.regex.Matcher;
  7 import java.util.regex.Pattern;
  8 
  9 public class Main{
10 public static void main(String[] args) { 11 Scanner sc = new Scanner(System.in); 12 String str=sc.nextLine(); 13 ArrayList<User> users=new ArrayList<>(); 14 Format input=new Format(); 15 while(!str.equals("end")){ 16 if(input.search(str)==1){
17 input.addnewuse(users,str); 18 } 19 if(input.search(str)==2){ 20 input.addnewcallrecord(users,str); 21 } 22 str=sc.nextLine(); 23 } 24 Collections.sort(users,new Comparator<User>(){ 25 public int compare(User u1, User u2) { 26 if(Double.valueOf(u1.getNumber())>Double.valueOf(u2.getNumber())){ 27 return 1; 28 } 29 else { 30 return -1; 31 } 32 } 33 }); 34 for(int i=0;i<users.size();i++){ 35 System.out.print(users.get(i).getNumber()+" "); 36 System.out.print(new DecimalFormat("0.0#").format(users.get(i).calCost())); 37 System.out.print(" "); 38 System.out.println(new DecimalFormat("0.0#").format(users.get(i).calBalance())); 39 } 40 } 41 } 42 43 class Format { 44 public int search(String str) { 45 String[] arr = str.split(" "); 46 if (arr.length == 2) { 47 if (arr[0].matches("^u-\\d{11,13}$") && arr[1].equals("0")) { 48 return 1; 49 } 50 } 51 if (arr.length == 6) { 52 Pattern pattern = Pattern.compile("^([0-1]?\\d|2[0-3]):([0-5]\\d):([0-5]\\d)$"); 53 Matcher matcher1 = pattern.matcher(arr[3]); 54 Matcher matcher2 = pattern.matcher(arr[5]); 55 if (arr[0].matches("^t-\\d{11,12}") && arr[1].matches("\\d{10,12}")) { 56 if (matcher1.matches() && matcher2.matches()) { 57 if (matchdata(arr[2]) && matchdata(arr[4])) { 58 return 2; 59 } 60 } 61 } 62 } 63 return 0; 64 } 65 66 public boolean matchdata(String str) { 67 if (!str.matches("\\d{4}\\.([1-9]|1[0-2])\\.([1-9]|[1-3]\\d)")) { 68 return false; 69 } 70 String[] arr = str.split("\\."); 71 int year = Integer.parseInt(arr[0]); 72 int month = Integer.parseInt(arr[1]); 73 int day = Integer.parseInt(arr[2]); 74 if (month < 1 || month > 12) { 75 return false; 76 } 77 int[] monthdays = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 78 if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) { 79 monthdays[2] = 29; 80 } 81 if (day < 1 || day > monthdays[month]) { 82 return false; 83 } 84 return true; 85 } 86 87 public void addnewuse(ArrayList<User> users, String str) { 88 User nuse = new User(); 89 String[] arr = str.split(" "); 90 for (int i = 0; i < users.size(); i++) { 91 if (users.get(i).getNumber().equals(arr[0].substring(2))) { 92 return; 93 } 94 } 95 if (arr[1].equals("0")) { 96 nuse.setChargeMode(new LandlinePhoneCharging()); 97 } 98 nuse.setNumber(arr[0].substring(2)); 99 users.add(nuse); 100 } 101 102 public void addnewcallrecord(ArrayList<User> users, String str) { 103 String[] arr = str.split(" "); 104 CallRecord callrecord = new CallRecord(arr); 105 for (int i = 0; i < users.size(); i++) { 106 if (users.get(i).getNumber().equals(arr[0].substring(2))) { 107 if (callrecord.getCallType() == 1) { 108 users.get(i).getUserRecords().addCallingInCityRecords(callrecord); 109 } else if (callrecord.getCallType() == 2) { 110 users.get(i).getUserRecords().addCallingInProvinceRecords(callrecord); 111 } else { 112 users.get(i).getUserRecords().addCallingInLandRecords(callrecord); 113 } 114 } 115 if (users.get(i).getNumber().equals(arr[1])) { 116 if (callrecord.getCallType() == 1) { 117 users.get(i).getUserRecords().addAnswerInCityRecords(callrecord); 118 } else if (callrecord.getCallType() == 2) { 119 users.get(i).getUserRecords().addAnswerInProvinceRecords(callrecord); 120 } else { 121 users.get(i).getUserRecords().addAnswerInLandRecords(callrecord); 122 } 123 } 124 } 125 } 126 } 127 128 class User { 129 private UserRecords userRecords = new UserRecords(); 130 private double balance = 100; 131 private ChargeMode chargeMode; 132 private String number; 133 public double calBalance() { 134 return balance - chargeMode.getMonthlyRent() - chargeMode.calCost(userRecords); 135 } 136 137 public double calCost() { 138 return chargeMode.calCost(userRecords); 139 } 140 141 public UserRecords getUserRecords() { 142 return userRecords; 143 } 144 145 public void setUserRecords(UserRecords userRecords) { 146 this.userRecords = userRecords; 147 } 148 149 public double getBalance() { 150 return balance; 151 } 152 153 public ChargeMode getChargeMode() { 154 return chargeMode; 155 } 156 157 public void setChargeMode(ChargeMode chargeMode) { 158 this.chargeMode = chargeMode; 159 } 160 161 public String getNumber() { 162 return number; 163 } 164 165 public void setNumber(String number) { 166 this.number = number; 167 } 168 } 169 170 class UserRecords { 171 private ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>(); 172 private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>(); 173 private ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>(); 174 private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>(); 175 private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>(); 176 private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>(); 177 private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>(); 178 private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>(); 179 180 public void addCallingInCityRecords(CallRecord callRecord) { 181 callingInCityRecords.add(callRecord); 182 } 183 184 public ArrayList<CallRecord> getCallingInCityRecords() { 185 return callingInCityRecords; 186 } 187 188 public void setCallingInCityRecords(ArrayList<CallRecord> callingInCityRecords) { 189 this.callingInCityRecords = callingInCityRecords; 190 } 191 192 public void addCallingInProvinceRecords(CallRecord callRecord) { 193 callingInProvinceRecords.add(callRecord); 194 } 195 196 public ArrayList<CallRecord> getCallingInProvinceRecords() { 197 return callingInProvinceRecords; 198 } 199 200 public void setCallingInProvinceRecords(ArrayList<CallRecord> callingInProvinceRecords) { 201 this.callingInProvinceRecords = callingInProvinceRecords; 202 } 203 204 public void addCallingInLandRecords(CallRecord callRecord) { 205 callingInLandRecords.add(callRecord); 206 } 207 208 public ArrayList<CallRecord> getCallingInLandRecords() { 209 return callingInLandRecords; 210 } 211 212 public void setCallingInLandRecords(ArrayList<CallRecord> callingInLandRecords) { 213 this.callingInLandRecords = callingInLandRecords; 214 } 215 216 public void addAnswerInCityRecords(CallRecord answerRecord) { 217 answerInCityRecords.add(answerRecord); 218 } 219 220 public ArrayList<CallRecord> getAnswerInCityRecords() { 221 return answerInCityRecords; 222 } 223 224 public void setAnswerInCityRecords(ArrayList<CallRecord> answerInCityRecords) { 225 this.answerInCityRecords = answerInCityRecords; 226 } 227 228 public void addAnswerInProvinceRecords(CallRecord answerRecord) { 229 answerInProvinceRecords.add(answerRecord); 230 } 231 232 public ArrayList<CallRecord> getAnswerInProvinceRecords() { 233 return answerInProvinceRecords; 234 } 235 236 public void setAnswerInProvinceRecords(ArrayList<CallRecord> answerInProvinceRecords) { 237 this.answerInProvinceRecords = answerInProvinceRecords; 238 } 239 240 public void addAnswerInLandRecords(CallRecord answerRecord) { 241 answerInLandRecords.add(answerRecord); 242 } 243 244 public ArrayList<CallRecord> getAnswerInLandRecords() { 245 return answerInLandRecords; 246 } 247 248 public void setAnswerInLandRecords(ArrayList<CallRecord> answerInLandRecords) { 249 this.answerInLandRecords = answerInLandRecords; 250 } 251 252 public void addSendMessageRecords(MessageRecord sendMessageRecord) { 253 sendMessageRecords.add(sendMessageRecord); 254 } 255 256 public ArrayList<MessageRecord> getSendMessageRecords() { 257 return sendMessageRecords; 258 } 259 260 public void setSendMessageRecords(ArrayList<MessageRecord> sendMessageRecords) { 261 this.sendMessageRecords = sendMessageRecords; 262 } 263 264 public void addReceiveMessageRecords(MessageRecord receiveMessageRecord) { 265 receiveMessageRecords.add(receiveMessageRecord); 266 } 267 268 public ArrayList<MessageRecord> getReceiveMessageRecords() { 269 return receiveMessageRecords; 270 } 271 272 public void setReceiveMessageRecords(ArrayList<MessageRecord> receiveMessageRecords) { 273 this.receiveMessageRecords = receiveMessageRecords; 274 } 275 } 276 277 abstract class ChargeMode { 278 public ArrayList<ChargeRule> chargeRules = new ArrayList<>(); 279 280 public abstract double calCost(UserRecords userRecords); 281 282 public abstract double getMonthlyRent(); 283 284 public ArrayList<ChargeRule> getChargeRules() { 285 return chargeRules; 286 } 287 288 public void setChargeRules(ArrayList<ChargeRule> chargeRules) { 289 this.chargeRules = chargeRules; 290 } 291 } 292 293 class LandlinePhoneCharging extends ChargeMode { 294 private double monthlyRent = 20; 295 296 public LandlinePhoneCharging() { 297 chargeRules.add(new LandPhoneInCityRule()); 298 chargeRules.add(new LandPhoneInProvinceRule()); 299 chargeRules.add(new LandPhoneInlandRule()); 300 } 301 302 @Override 303 public double calCost(UserRecords userRecords) { 304 double sum = 0; 305 LandPhoneInCityRule rule1 = new LandPhoneInCityRule(); 306 LandPhoneInProvinceRule rule2 = new LandPhoneInProvinceRule(); 307 LandPhoneInlandRule rule3 = new LandPhoneInlandRule(); 308 sum += rule1.calCost(userRecords.getCallingInCityRecords()); 309 sum += rule2.calCost(userRecords.getCallingInProvinceRecords()); 310 sum += rule3.calCost(userRecords.getCallingInLandRecords()); 311 return sum; 312 } 313 314 @Override 315 public double getMonthlyRent() { 316 return monthlyRent; 317 } 318 } 319 320 abstract class CommunicationRecord { 321 private String callingNumber; 322 private String answerNumber; 323 324 public String getCallingNumber() { 325 return callingNumber; 326 } 327 328 public void setCallingNumber(String callingNumber) { 329 this.callingNumber = callingNumber; 330 } 331 332 public String getAnswerNumber() { 333 return answerNumber; 334 } 335 336 public void setAnswerNumber(String answerNumber) { 337 this.answerNumber = answerNumber; 338 } 339 } 340 341 class CallRecord extends CommunicationRecord { 342 private Date startTime; 343 private Date endTime; 344 private String callingAddressAreaCode; 345 private String answerAddressAreaCode; 346 347 public CallRecord() { 348 } 349 350 public CallRecord(String[] str) { 351 if (str[0].length() == 12) { 352 callingAddressAreaCode = str[0].substring(2, 5); 353 } else { 354 callingAddressAreaCode = str[0].substring(2, 6); 355 } 356 if (str[1].length() == 10) { 357 answerAddressAreaCode = str[1].substring(0, 3); 358 } else { 359 answerAddressAreaCode = str[1].substring(0, 4); 360 } 361 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault()); 362 try { 363 startTime = simpleDateFormat.parse(str[2] + " " + str[3]); 364 endTime = simpleDateFormat.parse(str[4] + " " + str[5]); 365 } catch (ParseException e) { 366 } 367 } 368 369 public int getCallType() { 370 if (callingAddressAreaCode.equals(answerAddressAreaCode)) { 371 return 1; 372 } 373 if (callingAddressAreaCode.matches("(^079\\d$)|(0701)")&&answerAddressAreaCode.matches("(^079\\d$)|(0701)")) { 374 return 2; 375 } 376 return 0; 377 } 378 379 public CallRecord(Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) { 380 this.startTime = startTime; 381 this.endTime = endTime; 382 this.callingAddressAreaCode = callingAddressAreaCode; 383 this.answerAddressAreaCode = answerAddressAreaCode; 384 } 385 386 public Date getStartTime() { 387 return startTime; 388 } 389 390 public void setStartTime(Date startTime) { 391 this.startTime = startTime; 392 } 393 394 public Date getEndTime() { 395 return endTime; 396 } 397 398 public void setEndTime(Date endTime) { 399 this.endTime = endTime; 400 } 401 402 public String getCallingAddressAreaCode() { 403 return callingAddressAreaCode; 404 } 405 406 public void setCallingAddressAreaCode(String callingAddressAreaCode) { 407 this.callingAddressAreaCode = callingAddressAreaCode; 408 } 409 410 public String getAnswerAddressAreaCode() { 411 return answerAddressAreaCode; 412 } 413 414 public void setAnswerAddressAreaCode(String answerAddressAreaCode) { 415 this.answerAddressAreaCode = answerAddressAreaCode; 416 } 417 } 418 419 class MessageRecord extends CommunicationRecord { 420 private String message; 421 422 public String getMessage() { 423 return message; 424 } 425 426 public void setMessage(String message) { 427 this.message = message; 428 } 429 } 430 431 abstract class ChargeRule { 432 } 433 434 abstract class CallChargeRule extends ChargeRule { 435 public abstract double calCost(ArrayList<CallRecord> callRecords); 436 } 437 438 class LandPhoneInCityRule extends CallChargeRule { 439 @Override 440 public double calCost(ArrayList<CallRecord> callRecords) { 441 double sumcost = 0; 442 for (int i = 0; i < callRecords.size(); i++) { 443 double time = (callRecords.get(i).getEndTime().getTime() - callRecords.get(i).getStartTime().getTime()) / 1000; 444 if (time < 0) { 445 continue; 446 } 447 double minute = (int) time / 60; 448 if (time % 60 != 0) { 449 minute += 1; 450 } 451 sumcost += minute * 0.1; 452 } 453 return sumcost; 454 } 455 } 456 457 class LandPhoneInProvinceRule extends CallChargeRule { 458 @Override 459 public double calCost(ArrayList<CallRecord> callRecords) { 460 double sumcost = 0; 461 for (int i = 0; i < callRecords.size(); i++) { 462 double time = (callRecords.get(i).getEndTime().getTime() - callRecords.get(i).getStartTime().getTime()) / 1000; 463 if (time < 0) { 464 continue; 465 } 466 double minute = (int) time / 60; 467 if (time % 60 != 0) { 468 minute += 1; 469 } 470 sumcost += minute * 0.3; 471 } 472 return sumcost; 473 } 474 } 475 476 class LandPhoneInlandRule extends CallChargeRule { 477 @Override 478 public double calCost(ArrayList<CallRecord> callRecords) { 479 double sumcost = 0; 480 for (int i = 0; i < callRecords.size(); i++) { 481 double time = (callRecords.get(i).getEndTime().getTime() - callRecords.get(i).getStartTime().getTime()) / 1000; 482 if (time < 0) { 483 continue; 484 } 485 double minute = (int) time / 60; 486 if (time % 60 != 0) { 487 minute += 1; 488 } 489 sumcost += minute * 0.6; 490 } 491 return sumcost; 492 } 493 }
View Code

 程式碼分析與處理:

  此題的難度在於不好下手,做此類圖較為複雜的題,需要仔細觀察類圖,明確類圖之間的聯絡。主要考查了抽象類,繼承與多型等知識,由於給出了類圖,題目變得更簡單。同時,在本次的類圖框架搭建的基礎上,增加一個輸入判斷類,能為讀入資料的判斷提供巨大的幫助,從而使得題目的資料判斷更為清晰,能為後續題目的擴充套件帶來巨大的幫助。而在此題中 User類中的UserRecord是這道題目的突破口,在這個UserRecord中可以把使用者的通話記錄資料放入,以便於ChargeMode在中間的呼叫。然後再去理解類圖中的ChargeMode類,這是這個程式的核心程式碼,通過這一段計算出使用者的花費費用,以完成程式。雖然類圖看起來非常的複雜,但只要花費足夠的時間將程式碼分解先從User入手,再寫到了UserRecord,接著就補充到ChargeMode,再然後就去構思Main函式,獲取輸入資料即可實現座機的計費。同時注意計費方式中不足一分鐘需要按一分鐘進行計算,以及在不同打電話場景下的計費規則的不同都需要進行不同的判斷。

 類圖:

 分析報告:

 7-1 電信計費系列2-手機+座機計費:

  1 import java.text.DecimalFormat;
  2 import java.text.ParseException;
  3 import java.text.SimpleDateFormat;
  4 import java.util.*;
  5 import java.util.regex.Matcher;
  6 import java.util.regex.Pattern;
  7 
  8 public class Main{
  9     public static void main(String[] args) {
 10         Scanner sc = new Scanner(System.in);
 11         String str=sc.nextLine();
 12         ArrayList<User> users=new ArrayList<>();
 13         Format input=new Format();
 14         while(!str.equals("end")){
 15             if(input.search(str)==1){
 16                 input.addnewuse(users,str);
 17             }
 18             if(input.search(str)==2){
 19                 input.addnewcallrecord(users,str);
 20             }
 21             str=sc.nextLine();
 22         }
 23         Collections.sort(users,new Comparator<User>(){
 24             public int compare(User u1, User u2) {
 25                 if(u1.getNumber().charAt(0)=='1'&&u2.getNumber().charAt(0)=='0'){
 26                     return 1;
 27                 }else if(u1.getNumber().charAt(0)=='0'&&u2.getNumber().charAt(0)=='1'){
 28                     return -1;
 29                 }else if(Double.valueOf(u1.getNumber())>Double.valueOf(u2.getNumber())){
 30                     return 1;
 31                 }else {
 32                     return -1;
 33                 }
 34             }
 35         });
 36 
 37         for(int i=0;i<users.size();i++){
 38             System.out.print(users.get(i).getNumber()+" ");
 39             System.out.print(new DecimalFormat("0.0#").format(users.get(i).calCost()));
 40             System.out.print(" ");
 41             System.out.println(new DecimalFormat("0.0#").format(users.get(i).calBalance()));
 42         }
 43     }
 44 }
 45 
 46 class Format {
 47     public int search(String str) {
 48         String[] arr = str.split(" ");
 49         if (arr.length == 2) {
 50             if ((arr[0].matches("^u-0\\d{9,11}$") && arr[1].equals("0"))||(arr[0].matches("^u-1[3-9]\\d{9}$") && arr[1].equals("1"))){
 51                 return 1;
 52             }
 53         }else if (arr.length == 6) {
 54             Pattern pattern = Pattern.compile("^([0-1]?\\d|2[0-3]):([0-5]\\d):([0-5]\\d)$");
 55             Matcher matcher1 = pattern.matcher(arr[3]);
 56             Matcher matcher2 = pattern.matcher(arr[5]);
 57             if (arr[0].matches("^t-0\\d{9,11}") && arr[1].matches("0\\d{9,11}")) {
 58                 if (matcher1.matches() && matcher2.matches()) {
 59                     if (matchdata(arr[2]) && matchdata(arr[4])) {
 60                         return 2;
 61                     }
 62                 }
 63             }
 64         } else if(arr.length==7){
 65             Pattern pattern = Pattern.compile("^([0-1]?\\d|2[0-3]):([0-5]\\d):([0-5]\\d)$");
 66             Matcher matcher1 = pattern.matcher(arr[4]);
 67             Matcher matcher2 = pattern.matcher(arr[6]);
 68             if(arr[0].matches("^t-0\\d{9,11}") && arr[1].matches("1[3-9]\\d{9}")&&arr[2].matches("\\d{3,4}")){
 69                 if (matcher1.matches() && matcher2.matches()) {
 70                     if (matchdata(arr[3]) && matchdata(arr[5])) {
 71                         return 2;
 72                     }
 73                 }
 74             }
 75             if(arr[0].matches("^t-1[3-9]\\d{9}")&&arr[1].matches("\\d{3,4}")&&arr[2].matches("0\\d{9,11}")){
 76                 if (matcher1.matches() && matcher2.matches()) {
 77                     if (matchdata(arr[3]) && matchdata(arr[5])) {
 78                         return 2;
 79                     }
 80                 }
 81             }
 82         }
 83         else if(arr.length==8){
 84             Pattern pattern = Pattern.compile("^([0-1]?\\d|2[0-3]):([0-5]\\d):([0-5]\\d)$");
 85             Matcher matcher1 = pattern.matcher(arr[5]);
 86             Matcher matcher2 = pattern.matcher(arr[7]);
 87             if(arr[0].matches("^t-1[3-9]\\d{9}")&&arr[1].matches("\\d{3,4}")&&arr[2].matches("1[3-9]\\d{9}")&&arr[3].matches("\\d{3,4}")){
 88                 if(matcher1.matches()&&matcher2.matches()){
 89                     if (matchdata(arr[4]) && matchdata(arr[6])) {
 90                         return 2;
 91                     }
 92                 }
 93             }
 94         }
 95         return 0;
 96     }
 97 
 98     public boolean matchdata(String str) {
 99         if (!str.matches("\\d{4}\\.([1-9]|1[0-2])\\.([1-9]|[1-3]\\d)")) {
100             return false;
101         }
102         String[] arr = str.split("\\.");
103         int year = Integer.parseInt(arr[0]);
104         int month = Integer.parseInt(arr[1]);
105         int day = Integer.parseInt(arr[2]);
106         if (month < 1 || month > 12) {
107             return false;
108         }
109         int[] monthdays = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
110         if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
111             monthdays[2] = 29;
112         }
113         if (day < 1 || day > monthdays[month]) {
114             return false;
115         }
116         return true;
117     }
118 
119     public void addnewuse(ArrayList<User> users, String str) {
120         User nuse = new User();
121         String[] arr = str.split(" ");
122         for (int i = 0; i < users.size(); i++) {
123             if (users.get(i).getNumber().equals(arr[0].substring(2))) {
124                 return;
125             }
126         }
127         if (arr[1].equals("0")) {
128             nuse.setChargeMode(new LandlinePhoneCharging());
129         }
130         if(arr[1].equals("1")){
131             nuse.setChargeMode(new MobilePhoneCharing());
132         }
133         nuse.setNumber(arr[0].substring(2));
134         users.add(nuse);
135     }
136 
137     public void addnewcallrecord(ArrayList<User> users, String str) {
138         String[] arr = str.split(" ");
139         CallRecord callrecord = new CallRecord(arr);
140         String call=null,ans=null;
141         call=arr[0].substring(2);
142         if(arr.length==6){
143             ans=arr[1];
144         }else if(arr.length==7){
145             if(arr[1].length()==11){
146                 ans=arr[1];
147             }else {
148                 ans=arr[2];
149             }
150         }else if(arr.length==8){
151             ans=arr[2];
152         }
153         for (int i = 0; i < users.size(); i++) {
154             if (users.get(i).getNumber().equals(call)) {
155                 if (callrecord.getCallType()[0].equals("1")) {
156                     users.get(i).getUserRecords().addCallingInCityRecords(callrecord);
157                 } else if (callrecord.getCallType()[0].equals("2")) {
158                     users.get(i).getUserRecords().addCallingInProvinceRecords(callrecord);
159                 } else {
160                     users.get(i).getUserRecords().addCallingInLandRecords(callrecord);
161                 }
162             }
163             if(users.get(i).getNumber().equals(ans)) {
164                 if (callrecord.getCallType()[1].equals("1")) {
165                     users.get(i).getUserRecords().addAnswerInCityRecords(callrecord);
166                 } else if (callrecord.getCallType()[1].equals("2")) {
167                     users.get(i).getUserRecords().addAnswerInProvinceRecords(callrecord);
168                 } else {
169                     users.get(i).getUserRecords().addAnswerInLandRecords(callrecord);
170                 }
171             }
172         }
173     }
174 }
175 
176 class User {
177     private UserRecords userRecords = new UserRecords();
178     private double balance = 100;
179     private ChargeMode chargeMode;
180     private String number;
181     public double calBalance() {
182         return balance - chargeMode.getMonthlyRent() - chargeMode.calCost(userRecords);
183     }
184 
185     public double calCost() {
186         return chargeMode.calCost(userRecords);
187     }
188 
189     public UserRecords getUserRecords() {
190         return userRecords;
191     }
192 
193     public void setUserRecords(UserRecords userRecords) {
194         this.userRecords = userRecords;
195     }
196 
197     public double getBalance() {
198         return balance;
199     }
200 
201     public ChargeMode getChargeMode() {
202         return chargeMode;
203     }
204 
205     public void setChargeMode(ChargeMode chargeMode) {
206         this.chargeMode = chargeMode;
207     }
208 
209     public String getNumber() {
210         return number;
211     }
212 
213     public void setNumber(String number) {
214         this.number = number;
215     }
216 }
217 
218 class UserRecords {
219     private ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();
220     private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();
221     private ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();
222     private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();
223     private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();
224     private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();
225     private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();
226     private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();
227 
228     public void addCallingInCityRecords(CallRecord callRecord) {
229         callingInCityRecords.add(callRecord);
230     }
231 
232     public ArrayList<CallRecord> getCallingInCityRecords() {
233         return callingInCityRecords;
234     }
235 
236     public void setCallingInCityRecords(ArrayList<CallRecord> callingInCityRecords) {
237         this.callingInCityRecords = callingInCityRecords;
238     }
239 
240     public void addCallingInProvinceRecords(CallRecord callRecord) {
241         callingInProvinceRecords.add(callRecord);
242     }
243 
244     public ArrayList<CallRecord> getCallingInProvinceRecords() {
245         return callingInProvinceRecords;
246     }
247 
248     public void setCallingInProvinceRecords(ArrayList<CallRecord> callingInProvinceRecords) {
249         this.callingInProvinceRecords = callingInProvinceRecords;
250     }
251 
252     public void addCallingInLandRecords(CallRecord callRecord) {
253         callingInLandRecords.add(callRecord);
254     }
255 
256     public ArrayList<CallRecord> getCallingInLandRecords() {
257         return callingInLandRecords;
258     }
259 
260     public void setCallingInLandRecords(ArrayList<CallRecord> callingInLandRecords) {
261         this.callingInLandRecords = callingInLandRecords;
262     }
263 
264     public void addAnswerInCityRecords(CallRecord answerRecord) {
265         answerInCityRecords.add(answerRecord);
266     }
267 
268     public ArrayList<CallRecord> getAnswerInCityRecords() {
269         return answerInCityRecords;
270     }
271 
272     public void setAnswerInCityRecords(ArrayList<CallRecord> answerInCityRecords) {
273         this.answerInCityRecords = answerInCityRecords;
274     }
275 
276     public void addAnswerInProvinceRecords(CallRecord answerRecord) {
277         answerInProvinceRecords.add(answerRecord);
278     }
279 
280     public ArrayList<CallRecord> getAnswerInProvinceRecords() {
281         return answerInProvinceRecords;
282     }
283 
284     public void setAnswerInProvinceRecords(ArrayList<CallRecord> answerInProvinceRecords) {
285         this.answerInProvinceRecords = answerInProvinceRecords;
286     }
287 
288     public void addAnswerInLandRecords(CallRecord answerRecord) {
289         answerInLandRecords.add(answerRecord);
290     }
291 
292     public ArrayList<CallRecord> getAnswerInLandRecords() {
293         return answerInLandRecords;
294     }
295 
296     public void setAnswerInLandRecords(ArrayList<CallRecord> answerInLandRecords) {
297         this.answerInLandRecords = answerInLandRecords;
298     }
299 
300     public void addSendMessageRecords(MessageRecord sendMessageRecord) {
301         sendMessageRecords.add(sendMessageRecord);
302     }
303 
304     public ArrayList<MessageRecord> getSendMessageRecords() {
305         return sendMessageRecords;
306     }
307 
308     public void setSendMessageRecords(ArrayList<MessageRecord> sendMessageRecords) {
309         this.sendMessageRecords = sendMessageRecords;
310     }
311 
312     public void addReceiveMessageRecords(MessageRecord receiveMessageRecord) {
313         receiveMessageRecords.add(receiveMessageRecord);
314     }
315 
316     public ArrayList<MessageRecord> getReceiveMessageRecords() {
317         return receiveMessageRecords;
318     }
319 
320     public void setReceiveMessageRecords(ArrayList<MessageRecord> receiveMessageRecords) {
321         this.receiveMessageRecords = receiveMessageRecords;
322     }
323 }
324 
325 abstract class ChargeMode {
326     public ArrayList<ChargeRule> chargeRules = new ArrayList<>();
327     public abstract double calCost(UserRecords userRecords);
328     public abstract double getMonthlyRent();
329     public ArrayList<ChargeRule> getChargeRules() {
330         return chargeRules;
331     }
332     public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
333         this.chargeRules = chargeRules;
334     }
335 }
336 
337 class LandlinePhoneCharging extends ChargeMode {
338     private double monthlyRent = 20;
339     public LandlinePhoneCharging() {
340         chargeRules.add(new LandPhoneInCityRule());
341         chargeRules.add(new LandPhoneInProvinceRule());
342         chargeRules.add(new LandPhoneInlandRule());
343     }
344 
345     @Override
346     public double calCost(UserRecords userRecords) {
347         double sum = 0;
348         LandPhoneInCityRule rule1 = new LandPhoneInCityRule();
349         LandPhoneInProvinceRule rule2 = new LandPhoneInProvinceRule();
350         LandPhoneInlandRule rule3 = new LandPhoneInlandRule();
351         sum += rule1.calCost(userRecords.getCallingInCityRecords());
352         sum += rule2.calCost(userRecords.getCallingInProvinceRecords());
353         sum += rule3.calCost(userRecords.getCallingInLandRecords());
354         return sum;
355     }
356 
357     @Override
358     public double getMonthlyRent() {
359         return monthlyRent;
360     }
361 }
362 
363 abstract class CommunicationRecord {
364     private String callingNumber;
365     private String answerNumber;
366 
367     public String getCallingNumber() {
368         return callingNumber;
369     }
370 
371     public void setCallingNumber(String callingNumber) {
372         this.callingNumber = callingNumber;
373     }
374 
375     public String getAnswerNumber() {
376         return answerNumber;
377     }
378 
379     public void setAnswerNumber(String answerNumber) {
380         this.answerNumber = answerNumber;
381     }
382 }
383 
384 class CallRecord extends CommunicationRecord {
385     private Date startTime;
386     private Date endTime;
387     private String callingAddressAreaCode;
388     private String answerAddressAreaCode;
389 
390     public CallRecord() {
391     }
392 
393     public CallRecord(String[] str) {
394         String st1=null,st2=null,ed1=null,ed2=null;
395         if(str.length==6){
396             callingAddressAreaCode = str[0].substring(2, 6);
397             answerAddressAreaCode = str[1].substring(0, 4);
398             st1=str[2];st2=str[3];
399             ed1=str[4];ed2=str[5];
400         }
401         else if(str.length==7){
402             if(str[1].length()==11){
403                 callingAddressAreaCode = str[0].substring(2, 6);
404                 answerAddressAreaCode = str[2];
405             }else{
406                 callingAddressAreaCode = str[1];
407                 answerAddressAreaCode = str[2].substring(0, 4);
408             }
409             st1=str[3];st2=str[4];
410             ed1=str[5];ed2=str[6];
411         }else if(str.length==8){
412             callingAddressAreaCode = str[1];
413             answerAddressAreaCode = str[3];
414             st1=str[4];st2=str[5];
415             ed1=str[6];ed2=str[7];
416         }
417         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault());
418         try {
419             startTime = simpleDateFormat.parse(st1 + " " + st2);
420             endTime = simpleDateFormat.parse(ed1 + " " + ed2);
421         } catch (ParseException e) {
422         }
423     }
424 
425     public String[] getCallType() {
426         String[] type=new String[2];
427         //System.out.println(callingAddressAreaCode);
428         if(callingAddressAreaCode.matches("0791")){
429             type[0]="1";
430         }else if (callingAddressAreaCode.matches("(^079([2-9]|0)$)|(0701)")){
431             type[0]="2";
432         }else{
433             type[0]="3";
434         }
435         //System.out.println(answerAddressAreaCode);
436         if(answerAddressAreaCode.matches("0791")){
437             type[1]="1";
438         }else if(answerAddressAreaCode.matches("(^079([2-9]|0)$)|(0701)")){
439             type[1]="2";
440         }else{
441             type[1]="3";
442         }
443         return type;
444     }
445 
446     public CallRecord(Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) {
447         this.startTime = startTime;
448         this.endTime = endTime;
449         this.callingAddressAreaCode = callingAddressAreaCode;
450         this.answerAddressAreaCode = answerAddressAreaCode;
451     }
452 
453     public Date getStartTime() {
454         return startTime;
455     }
456 
457     public void setStartTime(Date startTime) {
458         this.startTime = startTime;
459     }
460 
461     public Date getEndTime() {
462         return endTime;
463     }
464 
465     public void setEndTime(Date endTime) {
466         this.endTime = endTime;
467     }
468 
469     public String getCallingAddressAreaCode() {
470         return callingAddressAreaCode;
471     }
472 
473     public void setCallingAddressAreaCode(String callingAddressAreaCode) {
474         this.callingAddressAreaCode = callingAddressAreaCode;
475     }
476 
477     public String getAnswerAddressAreaCode() {
478         return answerAddressAreaCode;
479     }
480 
481     public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
482         this.answerAddressAreaCode = answerAddressAreaCode;
483     }
484 }
485 
486 class MessageRecord extends CommunicationRecord {
487     private String message;
488 
489     public String getMessage() {
490         return message;
491     }
492 
493     public void setMessage(String message) {
494         this.message = message;
495     }
496 }
497 
498 abstract class ChargeRule {
499 }
500 
501 abstract class CallChargeRule extends ChargeRule {
502     public abstract double calCost(ArrayList<CallRecord> callRecords);
503 }
504 
505 class LandPhoneInCityRule extends CallChargeRule {
506     @Override
507     public double calCost(ArrayList<CallRecord> callRecords) {
508         double sumcost = 0;
509         for (int i = 0; i < callRecords.size(); i++) {
510             double time = (callRecords.get(i).getEndTime().getTime() - callRecords.get(i).getStartTime().getTime()) / 1000;
511             if (time < 0) {
512                 continue;
513             }
514             double minute = (int) time / 60;
515             if (time % 60 != 0) {
516                 minute += 1;
517             }
518             if(callRecords.get(i).getCallType()[1].equals("1")){
519                 sumcost += minute * 0.1;
520             }else if(callRecords.get(i).getCallType()[1].equals("2")){
521                 sumcost += minute * 0.3;
522             }else if(callRecords.get(i).getCallType()[1].equals("3")){
523                 sumcost +=minute * 0.6;
524             }
525         }
526         return sumcost;
527     }
528 }
529 
530 class LandPhoneInProvinceRule extends CallChargeRule {
531     @Override
532     public double calCost(ArrayList<CallRecord> callRecords) {
533         double sumcost = 0;
534         for (int i = 0; i < callRecords.size(); i++) {
535             double time = (callRecords.get(i).getEndTime().getTime() - callRecords.get(i).getStartTime().getTime()) / 1000;
536             if (time < 0) {
537                 continue;
538             }
539             double minute = (int) time / 60;
540             if (time % 60 != 0) {
541                 minute += 1;
542             }
543             sumcost += minute * 0.3;
544         }
545         return sumcost;
546     }
547 }
548 
549 class LandPhoneInlandRule extends CallChargeRule {
550     @Override
551     public double calCost(ArrayList<CallRecord> callRecords) {
552         double sumcost = 0;
553         for (int i = 0; i < callRecords.size(); i++) {
554             double time = (callRecords.get(i).getEndTime().getTime() - callRecords.get(i).getStartTime().getTime()) / 1000;
555             if (time < 0) {
556                 continue;
557             }
558             double minute = (int) time / 60;
559             if (time % 60 != 0) {
560                 minute += 1;
561             }
562             sumcost += minute * 0.6;
563         }
564         return sumcost;
565     }
566 }
567 class MobilePhoneCharing extends ChargeMode{
568     private double monthlyRent = 15;
569     public MobilePhoneCharing() {
570         chargeRules.add(new MobilePhoneInCityRule());
571         chargeRules.add(new MobilePhoneInProvinceRule());
572         chargeRules.add(new MobilePhoneInlandRule());
573     }
574     @Override
575     public double calCost(UserRecords userRecords) {
576         double sum = 0;
577         MobilePhoneInProvinceRule rule4 =new MobilePhoneInProvinceRule();
578         MobilePhoneInCityRule rule1 = new MobilePhoneInCityRule();
579         MobilePhoneInProvinceRule rule2 = new MobilePhoneInProvinceRule();
580         MobilePhoneInlandRule rule3 = new MobilePhoneInlandRule();
581         sum += rule1.calCost(userRecords.getCallingInCityRecords());
582         sum += rule2.calCost(userRecords.getCallingInProvinceRecords());
583         sum += rule3.calCost(userRecords.getCallingInLandRecords());
584         sum += rule4.calCost(userRecords.getAnswerInLandRecords());
585         return sum;
586     }
587 
588     @Override
589     public double getMonthlyRent() {
590         return monthlyRent;
591     }
592 }
593 class MobilePhoneInCityRule extends CallChargeRule{
594     @Override
595     public double calCost(ArrayList<CallRecord> callRecords) {
596         double sumcost = 0;
597         for (int i = 0; i < callRecords.size(); i++) {
598             double time = (callRecords.get(i).getEndTime().getTime() - callRecords.get(i).getStartTime().getTime()) / 1000;
599             if (time < 0) {
600                 continue;
601             }
602             double minute = (int) time / 60;
603             if (time % 60 != 0) {
604                 minute += 1;
605             }
606             if(callRecords.get(i).getCallType()[1].equals("1")){
607                 sumcost += minute * 0.1;
608             }else if(callRecords.get(i).getCallType()[1].equals("2")){
609                 sumcost += minute * 0.2;
610             }else if(callRecords.get(i).getCallType()[1].equals("3")){
611                 sumcost +=minute * 0.3;
612             }
613         }
614         return sumcost;
615     }
616 }
617 
618 class MobilePhoneInlandRule extends CallChargeRule{
619     @Override
620     public double calCost(ArrayList<CallRecord> callRecords) {
621         double sumcost = 0;
622         for (int i = 0; i < callRecords.size(); i++) {
623             double time = (callRecords.get(i).getEndTime().getTime() - callRecords.get(i).getStartTime().getTime()) / 1000;
624             if (time < 0) {
625                 continue;
626             }
627             double minute = (int) time / 60;
628             if (time % 60 != 0) {
629                 minute += 1;
630             }
631             sumcost += minute * 0.6;
632         }
633         return sumcost;
634     }
635 }
636 
637 class MobilePhoneInProvinceRule extends CallChargeRule{
638     @Override
639     public double calCost(ArrayList<CallRecord> callRecords) {
640         double sumcost = 0;
641         for (int i = 0; i < callRecords.size(); i++) {
642             double time = (callRecords.get(i).getEndTime().getTime() - callRecords.get(i).getStartTime().getTime()) / 1000;
643             if (time < 0) {
644                 continue;
645             }
646             double minute = (int) time / 60;
647             if (time % 60 != 0) {
648                 minute += 1;
649             }
650             sumcost +=minute * 0.3;
651         }
652         return sumcost;
653     }
654 }
View Code

程式碼分析與處理:

  此題為上一題的進階題,相對於單純的座機計費新增加了手機計費的功能,包括四種類型的計費,座機打座機,座機打手機,手機打座機,手機打手機,計費方式也新增加了漫遊,型別更多種,例如有關手機計費:MobilePhoneInCityRule、MobilePhoneInlandRule、MobilePhoneInProvinceRule等。在上題的基礎上,增加儲存手機使用者的容器,增加相應手機計費規則,以及判斷手機通訊資訊的正則表示式,儲存手機使用者的方式和上題儲存座機使用者的方式上基本一致,根據撥打人所在區號根據接聽人的區號判斷是市內省內還是國內長途資訊等型別,同時在使用者資訊裡建立對應的容器儲存它們。以及由於在計費方面,存在座機打手機,手機打手機,手機打座機等,所以在資料儲存時,他們的資訊格式各不相同,應當對不同的分類具體問題進行分析。同時本題中還加入了更多非常複雜的格式判定,對正則表示式的理解和掌握程度有非常大的考察力度。不過首先還是按照先前建立好的類圖並加以適當修改,在第一題的基礎上按照同樣的方式加入手機通訊的付費規則等需要增添的類即可。

類圖:

分析報告:

 7-1 電信計費系列3-簡訊計費:

  1 import java.text.DecimalFormat;
  2 import java.util.*;
  3 
  4 public class Main{
  5     public static void main(String[] args) {
  6         Scanner sc = new Scanner(System.in);
  7         String str=sc.nextLine();
  8         ArrayList<User> users=new ArrayList<>();
  9         Format input=new Format();
 10         while(!str.equals("end")){
 11             if(input.search(str)==1){
 12                 input.addnewuse(users,str);
 13             }
 14             if(input.search(str)==2){
 15                 input.addnewcallrecord(users,str);
 16             }
 17             str=sc.nextLine();
 18         }
 19         Collections.sort(users,new Comparator<User>(){
 20             public int compare(User u1, User u2) {
 21                 if(u1.getNumber().charAt(0)=='1'&&u2.getNumber().charAt(0)=='0'){
 22                     return 1;
 23                 }else if(u1.getNumber().charAt(0)=='0'&&u2.getNumber().charAt(0)=='1'){
 24                     return -1;
 25                 }else if(Double.valueOf(u1.getNumber())>Double.valueOf(u2.getNumber())){
 26                     return 1;
 27                 }else {
 28                     return -1;
 29                 }
 30             }
 31         });
 32         for(int i=0;i<users.size();i++){
 33             System.out.print(users.get(i).getNumber()+" ");
 34             System.out.print(new DecimalFormat("0.0#").format(users.get(i).calCost()));
 35             System.out.print(" ");
 36             System.out.println(new DecimalFormat("0.0#").format(users.get(i).calBalance()));
 37         }
 38     }
 39 }
 40 
 41 class Format {
 42     public int search(String str) {
 43         String[] arr = str.split(" ");
 44         if (arr.length == 2) {
 45             if(arr[0].matches("^u-1[3-9]\\d{9}$")&& arr[1].equals("3")){
 46                 return 1;
 47             }
 48         }
 49         if (str.charAt(0) == 'm') {
 50             if(arr[0].matches("^m-1[3-9]\\d{9}")&&arr[1].matches("1[3-9]\\d{9}")){
 51                 if(str.substring(26).matches("[(\\d)|(\\s\\)|(a-zA-Z)|(\\.\\)|(\\,\\)]+")){
 52                     return 2;
 53                 }
 54             }
 55         }
 56         return 0;
 57     }
 58     public void addnewuse(ArrayList<User> users, String str) {
 59         User nuse = new User();
 60         String[] arr = str.split(" ");
 61         for (int i = 0; i < users.size(); i++) {
 62             if (users.get(i).getNumber().equals(arr[0].substring(2))) {
 63                 return;
 64             }
 65         }
 66         nuse.setNumber(arr[0].substring(2));
 67         if (arr[1].equals("3")) {
 68             nuse.setChargeMode(new MessageCharing());
 69         }
 70         users.add(nuse);
 71     }
 72 
 73     public void addnewcallrecord(ArrayList<User> users, String str) {
 74         String[] arr = str.split(" ");
 75         MessageRecord messageRecord = new MessageRecord(str.substring(26));
 76         String call = null,ans=null;
 77         call = arr[0].substring(2);
 78         ans = arr[1];
 79         for (int i = 0; i < users.size(); i++) {
 80             if (str.charAt(0) == 'm') {
 81                 if (users.get(i).getNumber().equals(call)) {
 82                     users.get(i).getUserRecords().addSendMessageRecords(messageRecord);
 83                 }
 84                 if(users.get(i).getNumber().equals(ans)){
 85                     users.get(i).getUserRecords().addReceiveMessageRecords(messageRecord);
 86                 }
 87             }
 88         }
 89     }
 90 }
 91 
 92 class User {
 93     private UserRecords userRecords = new UserRecords();
 94     private double balance = 100;
 95     private ChargeMode chargeMode;
 96     private String number;
 97     public double calBalance() {
 98         return balance - chargeMode.getMonthlyRent() - chargeMode.calCost(userRecords);
 99     }
100 
101     public double calCost() {
102         return chargeMode.calCost(userRecords);
103     }
104 
105     public UserRecords getUserRecords() {
106         return userRecords;
107     }
108 
109     public void setUserRecords(UserRecords userRecords) {
110         this.userRecords = userRecords;
111     }
112 
113     public double getBalance() {
114         return balance;
115     }
116 
117     public ChargeMode getChargeMode() {
118         return chargeMode;
119     }
120 
121     public void setChargeMode(ChargeMode chargeMode) {
122         this.chargeMode = chargeMode;
123     }
124 
125     public String getNumber() {
126         return number;
127     }
128 
129     public void setNumber(String number) {
130         this.number = number;
131     }
132 }
133 
134 class CallRecord extends CommunicationRecord {
135     public CallRecord() {}
136 }
137 
138 class UserRecords{
139     private ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();
140     private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();
141     private ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();
142     private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();
143     private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();
144     private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();
145     private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();
146     private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();
147     public void addSendMessageRecords(MessageRecord sendMessageRecord) {
148         sendMessageRecords.add(sendMessageRecord);
149     }
150 
151     public ArrayList<MessageRecord> getSendMessageRecords() {
152         return sendMessageRecords;
153     }
154 
155     public void setSendMessageRecords(ArrayList<MessageRecord> sendMessageRecords) {
156         this.sendMessageRecords = sendMessageRecords;
157     }
158 
159     public void addReceiveMessageRecords(MessageRecord receiveMessageRecord) {
160         receiveMessageRecords.add(receiveMessageRecord);
161     }
162 
163     public ArrayList<MessageRecord> getReceiveMessageRecords() {
164         return receiveMessageRecords;
165     }
166 
167     public void setReceiveMessageRecords(ArrayList<MessageRecord> receiveMessageRecords) {
168         this.receiveMessageRecords = receiveMessageRecords;
169     }
170 }
171 
172 abstract class ChargeMode {
173     public ArrayList<ChargeRule> chargeRules = new ArrayList<>();
174     public abstract double calCost(UserRecords userRecords);
175     public abstract double getMonthlyRent();
176     public ArrayList<ChargeRule> getChargeRules() {
177         return chargeRules;
178     }
179     public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
180         this.chargeRules = chargeRules;
181     }
182 }
183 
184 abstract class CommunicationRecord {
185     private String callingNumber;
186     private String answerNumber;
187 
188     public String getCallingNumber() {
189         return callingNumber;
190     }
191 
192     public void setCallingNumber(String callingNumber) {
193         this.callingNumber = callingNumber;
194     }
195 
196     public String getAnswerNumber() {
197         return answerNumber;
198     }
199 
200     public void setAnswerNumber(String answerNumber) {
201         this.answerNumber = answerNumber;
202     }
203 }
204 
205 class MessageRecord extends CommunicationRecord {
206     private String message;
207     public MessageRecord(){};
208     public MessageRecord(String message){
209         this.message = message;
210     }
211     public String getMessage() {
212         return message;
213     }
214     public void setMessage(String message) {
215         this.message = message;
216     }
217 }
218 
219 abstract class ChargeRule {
220 }
221 
222 class MessageCharing extends ChargeMode{
223     private double monthlyRent = 0;
224     public MessageCharing() {
225         chargeRules.add(new MessageRule());
226     }
227     @Override
228     public double calCost(UserRecords userRecords) {
229         double sum=0;
230         MessageRule rule= new MessageRule();
231         sum+=rule.calCost(userRecords.getSendMessageRecords());
232         return sum;
233     }
234     @Override
235     public double getMonthlyRent() {
236         return monthlyRent;
237     }
238 }
239 
240 class MessageRule extends ChargeRule{
241     public double calCost(ArrayList<MessageRecord> messageRecords){
242         double sumcost=0;
243         int sum=0;
244         for (int i=0;i<messageRecords.size();i++){
245             int len=messageRecords.get(i).getMessage().length();
246             if(len>10){
247                 sum+=len/10;
248                 if(len%10!=0){
249                     sum++;
250                 }
251             }else {
252                 sum++;
253             }
254         }
255         if(sum<=3){
256             sumcost=0.1*sum;
257         }else if(sum<=5){
258             sumcost=0.3+0.2*(sum-3);
259         }else{
260             sumcost=0.7+0.3*(sum-5);
261         }
262         return sumcost;
263     }
264 }
View Code

程式碼分析與處理:

  簡訊計費,相對於前面兩次,難度沒有這麼大,計費型別也和前兩次不一樣,前兩次是計算每次的通訊費用再相加,而此次的簡訊計費則必須先算總的簡訊條數,然後再計算簡訊的費用,類設計也可在原來的基礎上進行增加補充修改(此時一開始在前面設計好清晰的類圖結構,就會使得題目變得更加簡單易操作)。本題最關鍵的是判斷簡訊內容的長度,由於資訊輸入格式的格式為:m-主叫號碼 接收號碼 簡訊內容,而m-主叫號碼 接收號碼 的長度確定,可用substring方法將後面的簡訊內容全部擷取,再判斷長度將對應的記錄讀取即可,之後利用對應的計費規則統計計算,同時注意一次傳送簡訊的字元數量超過10個,按每10個字元一條簡訊進行疊加。

類圖:

 分析報告:

3.踩坑心得:

  1.在這一階段題目集中,在7-1電信計費系列1-座機計費裡,需新增判斷座機的最高位應為0以及關於相關的日期的判斷須符合格式例如2022:09:30,月份的判斷需要符合格式例如單位數前者不能含多餘的零以及每個月份的天數需符合對應的月份數。

  2.在7-1 電信計費系列2-手機+座機計費中,關於座機號及手機號的判斷,座機號最高位為0,手機號最高位為1同時手機號第二位只能為[3-9],對輸入的相關的格式進行更近一步的判斷。避免對於類和方法漏寫和判斷情況缺失的判斷,從而導致容易漏掉使得測試點報錯。

  3.7-1 電信計費系列3-簡訊計費中,注意相關的計費規則以及對特殊簡訊(全為空格)的簡訊內容判斷方式以及簡訊內容只能由數字、字母、空格、英文逗號、英文句號等組成,加上新增對應的資訊判斷格式。

  4.這三次電信計費練習都涉及了許多有關輸入有效性的判斷。在第一題時,比較專注於時間輸入有效性的判斷,對SimpleDateFormat類的不瞭解也導致第一題使用了錯誤的方法判斷的時間,對其餘部分的格式判斷也簡單判斷,使用正則表示式判斷,能夠使得座機計費格式判斷更加全面。在手機+座機計費這一題時,在學會使用SimpleDateFormat類進行時間判斷後,又因為不瞭解什麼樣的輸入可以被SimpleDateFormat轉化,不符合yyyy.MM.dd HH:mm:ss的模式判斷,好在後面得以發現問題解決,第三次簡訊收費計算則是在有了前兩次的總結,寫的時候就相對比較順利。

4.改進建議:

  在這幾次題目集的過程在,由於對於對正則表示式的理解匱乏,導致對於一些輸入的判斷出現錯誤。正則表示式是經常應用的一種判斷輸入的方法,對於建立的正則表示式的外圍一定要新增“ ”。在這個階段的學習中,我們在類與物件、方法基本構建、繼承、多型的使用的基礎上,新學習並著重強化了對抽象類、抽象方法的構造,介面的加入,還有JavaFX的使用。加上對於新知識的掌握並不完全,使用起來不夠熟練,需要清楚地知道類間關係,知道每個類中方法的作用。做題時需要先理清思路再寫程式,避免邏輯上的錯誤。以及當發現自己當前的邏輯方法完成不了題目時,可以嘗試重新換個思路,以免執行不下去。做題時查缺補漏,以免下次還要去查前面的bug。。。。 對於題目要求的排序輸出,可以運用集合本身具有的排序方法,新增對應的comparable介面比較即可。 對於一個問題可以思考靈活的方法變通,設計類做到可擴充套件性,增加功能或減少功能時不必修改太多的程式碼,靈活的方法也不需要太多的程式碼來解決一個問題。加強相關知識的聯絡與應用。

五、總結

       經過一個學期的Java課程學習,使得我對於Java,瞭解、探索以及認知這門完全面向物件的語言。從一開始的懵懂,到現在能夠自己編寫一個簡單的程式,學習應該是迴圈漸進,腳踏實地,一步一步循序漸進進步。學習Java,對於書上的概念也要理解透徹,多和同學溝通,互相學習,取長補短,多與別人進行交流。Java基礎課程已經結束,但對於Java知識的學習與提升才剛剛開始。更熟練的使用Java,更好的類關係設計將作為之後學習的目標與方向。相較於Java巨集大的知識體系,所學的知識不過鳳毛麟角,更加豐富的知識還尚未來到。不斷學習,不斷探索,虛心學習,勤於練習。在今後課程的學習中,我們要學會著重分析題目需求、弄懂類中包含的內容以及方法的編寫,搞清各個功能程式碼塊之間的聯絡。通過進一步Java課程的學習,使得我對Java這一門程式語言有了本質上的瞭解,也明白了類與物件作為Java語言核心的原因。在不斷吸收新知的同時,也發現了自己在程式碼程式設計方面還有很多的不足,應在未來繼續進行改正,不斷進步。

五、總結

       經過一個學期的Java課程學習,使得我對於Java,瞭解、探索以及認知這門完全面向物件的語言。從一開始的懵懂,到現在能夠自己編寫一個簡單的程式,學習應該是迴圈漸進,腳踏實地,一步一步循序漸進進步。學習Java,對於書上的概念也要理解透徹,多和同學溝通,互相學習,取長補短,多與別人進行交流。Java基礎課程已經結束,但對於Java知識的學習與提升才剛剛開始。更熟練的使用Java,更好的類關係設計將作為之後學習的目標與方向。相較於Java巨集大的知識體系,所學的知識不過鳳毛麟角,更加豐富的知識還尚未來到。不斷學習,不斷探索,虛心學習,勤於練習。在今後課程的學習中,我們要學會著重分析題目需求、弄懂類中包含的內容以及方法的編寫,搞清各個功能程式碼塊之間的聯絡。通過進一步Java課程的學習,使得我對Java這一門程式語言有了本質上的瞭解,也明白了類與物件作為Java語言核心的原因。在不斷吸收新知的同時,也發現了自己在程式碼程式設計方面還有很多的不足,應在未來繼續進行改正,不斷進步。