blog3
作者:互联网
一、前言
电信计费系列:
题目系列整体理解来比较复杂,需要慢慢观察类图,弄明白类与类之间是如何的关系,是怎么建立起来联系的,而且题目中考察的知识点也非常多,如抽象类的使用和调用,接口的使用,集合框架的理解等等,将最近所学的东西都巧妙的联系了起来,进行的一次综合练习。
2、题量与难度:
(1)总的来说,七八九次题目题量都不算多,每次都只有两三道,但是题目关联性很强,基本上从第六次的电信计费系列1开始,再到第七次第八次的电信计费系列,如果只是单凭题目的话,对于我而言,难度很大。但在第六次题目电信计费系列开始老师非常仁慈地给了我们类图,所以从总体上来说,将难度降低了很多。
(2)除了电信计费系列题目之外,老师还给我们布置了一些比较简单的题目,我认为主要是为了让我们对类与对象的概念更加深入的理解与把握,比如七次题目集的C-K班级,八次题目的shop类和动物发声模拟器(继承与多态)等。
二、设计与分析
7-1 电信计费系列1-座机计费
1 import java.text.SimpleDateFormat; 2 import java.util.*; 3 public class Main { 4 5 public static void main(String[] args) { 6 Scanner input = new Scanner(System.in); 7 HashMap<String,User> map = new HashMap<String,User>(); 8 while (true) { 9 String s = input.nextLine(); 10 if (s.equals("end")) { 11 break; 12 } else { 13 if (s.matches("u-[0-9]{11,12}\\s0")) { 14 String[] s1 = s.split("\\s"); 15 String s2[] = s1[0].split("-"); 16 User user = new User(100, s2[1]); 17 ChargeMode chargeMode = new LandlinePhoneCharging(); 18 ArrayList<ChargeRule> chargeRules = new ArrayList<ChargeRule>(); 19 chargeRules.add(new LandPhonelnCityRule()); 20 chargeRules.add(new LandPhonelnProvinceRule()); 21 chargeRules.add(new LandPhonelnLandRule()); 22 chargeMode.setChargeRules(chargeRules); 23 user.setChargeMode(chargeMode); 24 map.put(s2[1], user); 25 } 26 if (s.matches("t-[0-9]{11,12}\\s[0-9]{10,12}\\s((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.([1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))\\.2\\.29))\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\\s((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.([1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))\\.2\\.29))\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])")) { 27 String[] s1 = s.split("\\s"); 28 String s2[] = s1[0].split("-"); 29 if (map.containsKey(s2[1])) { 30 User user = map.get(s2[1]); 31 CallRecord callRecord = new CallRecord(); 32 try { 33 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault()); 34 Date time1 = simpleDateFormat.parse(s1[2] + " " + s1[3]); 35 Date time2 = simpleDateFormat.parse(s1[4] + " " + s1[5]); 36 callRecord.setStartTime(time1); 37 callRecord.setEndTime(time2); 38 callRecord.setCallingAddressAreaCode(s2[1].substring(0, 4)); 39 callRecord.setAnswerAddressAreaCode(s1[1].substring(0, 4)); 40 callRecord.setCallingNumber(s2[1]); 41 callRecord.setAnswerNumber(s1[1]); 42 if (s1[1].substring(0, 3).equals("079") || s1[1].substring(0, 4).equals("0701")) { 43 if (s1[1].substring(0, 4).equals("0791")) { 44 user.userRecords.addCallinglnCityRecords(callRecord); 45 } else { 46 user.userRecords.addCallinglnProvinceRecords(callRecord); 47 } 48 } else { 49 user.userRecords.addCallinglnLandRecords(callRecord); 50 } 51 } catch (Exception ji) { 52 53 } 54 } 55 } 56 } 57 } 58 for (User value : map.values()) { 59 System.out.printf("%s %.1f %.1f\n",value.getNumber(),value.calCost(),value.calBalance()); 60 } 61 } 62 } 63 class User{ 64 UserRecords userRecords = new UserRecords(); 65 double balance = 100; 66 ChargeMode chargeMode; 67 String number; 68 public double calCost(){ 69 double sum=chargeMode.calCost(userRecords); 70 return sum; 71 } 72 public double calBalance(){ 73 return balance-chargeMode.calCost(userRecords)-chargeMode.getMonthlyRent(); 74 } 75 User(double balance, String number) { 76 this.balance = balance; 77 this.number = number; 78 } 79 80 public UserRecords getUserRecords() { 81 return userRecords; 82 } 83 84 public void setUserRecords(UserRecords userRecords) { 85 this.userRecords = userRecords; 86 } 87 88 public double getBalance() { 89 return balance; 90 } 91 92 public void setBalance(double balance) { 93 this.balance = balance; 94 } 95 96 public ChargeMode getChargeMode() { 97 return chargeMode; 98 } 99 100 public void setChargeMode(ChargeMode chargeMode) { 101 this.chargeMode = chargeMode; 102 } 103 104 public String getNumber() { 105 return number; 106 } 107 108 public void setNumber(String number) { 109 this.number = number; 110 } 111 } 112 abstract class ChargeMode{ 113 ArrayList<ChargeRule> chargeRules = new ArrayList<>(); 114 public ArrayList<ChargeRule> getChargeRules() { 115 return chargeRules; 116 } 117 public void setChargeRules(ArrayList<ChargeRule> chargeRules) { 118 this.chargeRules = chargeRules; 119 } 120 abstract double calCost(UserRecords userRecords); 121 abstract double getMonthlyRent(); 122 } 123 class LandlinePhoneCharging extends ChargeMode{ 124 double monthlyRent = 20; 125 @Override 126 public double calCost(UserRecords userRecords){ 127 double sum=0; 128 sum+=getChargeRules().get(0).calCost(userRecords.getCallinglnCityRecords()); 129 sum+=getChargeRules().get(1).calCost(userRecords.getCallinglnProvinceRecords()); 130 sum+=getChargeRules().get(2).calCost(userRecords.getCallinglnLandRecords()); 131 return sum; 132 } 133 134 @Override 135 public double getMonthlyRent() { 136 return monthlyRent; 137 } 138 } 139 class UserRecords{ 140 141 ArrayList<CallRecord> callinglnCityRecords = new ArrayList<CallRecord>(); 142 ArrayList<CallRecord> callinglnProvinceRecords = new ArrayList<CallRecord>(); 143 ArrayList<CallRecord> callinglnLandRecords = new ArrayList<CallRecord>(); 144 ArrayList<CallRecord> answerlnCityRecords = new ArrayList<CallRecord>(); 145 ArrayList<CallRecord> answerlnProvinceRecords = new ArrayList<CallRecord>(); 146 ArrayList<CallRecord> answerlnLandRecords = new ArrayList<CallRecord>(); 147 ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>(); 148 ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>(); 149 150 void addCallinglnCityRecords(CallRecord callRecord) { 151 callinglnCityRecords.add(callRecord); 152 } 153 void addCallinglnProvinceRecords(CallRecord callRecord) { 154 callinglnProvinceRecords.add(callRecord); 155 } 156 void addCallinglnLandRecords(CallRecord callRecord) { 157 callinglnLandRecords.add(callRecord); 158 } 159 void addAnswerlnCityRecords(CallRecord answerRecord) { 160 answerlnCityRecords.add(answerRecord); 161 } 162 void addAnswerlnProvinceRecords(CallRecord answerRecord) { 163 answerlnProvinceRecords.add(answerRecord); 164 } 165 void addAnswerlnLandRecords(CallRecord answerRecord) { 166 answerlnLandRecords.add(answerRecord); 167 } 168 void addSendMessageRecords(MessageRecord sendMessageRecord) { 169 sendMessageRecords.add(sendMessageRecord); 170 } 171 void addReceiveMessageRecords(MessageRecord receiveMessageRecord) { 172 receiveMessageRecords.add(receiveMessageRecord); 173 } 174 public ArrayList<CallRecord> getCallinglnCityRecords() { 175 return callinglnCityRecords; 176 } 177 178 public void setCallinglnCityRecords(ArrayList<CallRecord> callinglnCityRecords) { 179 this.callinglnCityRecords = callinglnCityRecords; 180 } 181 182 public ArrayList<CallRecord> getCallinglnProvinceRecords() { 183 return callinglnProvinceRecords; 184 } 185 186 public void setCallinglnProvinceRecords(ArrayList<CallRecord> callinglnProvinceRecords) { 187 this.callinglnProvinceRecords = callinglnProvinceRecords; 188 } 189 190 public ArrayList<CallRecord> getCallinglnLandRecords() { 191 return callinglnLandRecords; 192 } 193 194 public void setCallinglnLandRecords(ArrayList<CallRecord> callinglnLandRecords) { 195 this.callinglnLandRecords = callinglnLandRecords; 196 } 197 198 public ArrayList<CallRecord> getAnswerlnCityRecords() { 199 return answerlnCityRecords; 200 } 201 202 public void setAnswerlnCityRecords(ArrayList<CallRecord> answerlnCityRecords) { 203 this.answerlnCityRecords = answerlnCityRecords; 204 } 205 206 public ArrayList<CallRecord> getAnswerlnProvinceRecords() { 207 return answerlnProvinceRecords; 208 } 209 210 public void setAnswerlnProvinceRecords(ArrayList<CallRecord> answerlnProvinceRecords) { 211 this.answerlnProvinceRecords = answerlnProvinceRecords; 212 } 213 214 public ArrayList<CallRecord> getAnswerlnLandRecords() { 215 return answerlnLandRecords; 216 } 217 218 public void setAnswerlnLandRecords(ArrayList<CallRecord> answerlnLandRecords) { 219 this.answerlnLandRecords = answerlnLandRecords; 220 } 221 222 public ArrayList<MessageRecord> getSendMessageRecords() { 223 return sendMessageRecords; 224 } 225 226 public void setSendMessageRecords(ArrayList<MessageRecord> sendMessageRecords) { 227 this.sendMessageRecords = sendMessageRecords; 228 } 229 230 public ArrayList<MessageRecord> getReceiveMessageRecords() { 231 return receiveMessageRecords; 232 } 233 234 public void setReceiveMessageRecords(ArrayList<MessageRecord> receiveMessageRecords) { 235 this.receiveMessageRecords = receiveMessageRecords; 236 } 237 } 238 abstract class CommunicationRecord{ 239 String callingNumber; 240 String answerNumber; 241 abstract String getCallingNumber(); 242 abstract void setCallingNumber(String callingNumber); 243 abstract String getAnswerNumber(); 244 abstract void setAnswerNumber(String answerNumber); 245 } 246 class CallRecord extends CommunicationRecord{ 247 Date startTime; 248 Date endTime; 249 String callingAddressAreaCode; 250 String answerAddressAreaCode; 251 252 public Date getStartTime() { 253 return startTime; 254 } 255 256 public void setStartTime(Date startTime) { 257 this.startTime = startTime; 258 } 259 260 public Date getEndTime() { 261 return endTime; 262 } 263 264 public void setEndTime(Date endTime) { 265 this.endTime = endTime; 266 } 267 268 public String getCallingAddressAreaCode() { 269 return callingAddressAreaCode; 270 } 271 272 public void setCallingAddressAreaCode(String callingAddressAreaCode) { 273 this.callingAddressAreaCode = callingAddressAreaCode; 274 } 275 276 public String getAnswerAddressAreaCode() { 277 return answerAddressAreaCode; 278 } 279 280 public void setAnswerAddressAreaCode(String answerAddressAreaCode) { 281 this.answerAddressAreaCode = answerAddressAreaCode; 282 } 283 284 @Override 285 String getCallingNumber() { 286 return null; 287 } 288 289 @Override 290 void setCallingNumber(String callingNumber) { 291 292 } 293 294 @Override 295 String getAnswerNumber() { 296 return null; 297 } 298 299 @Override 300 void setAnswerNumber(String answerNumber) { 301 302 } 303 } 304 class MessageRecord extends CommunicationRecord{ 305 String message; 306 String getMessage() { 307 return null; 308 } 309 void setMessage(String message) { 310 311 } 312 313 @Override 314 String getCallingNumber() { 315 return null; 316 } 317 318 @Override 319 void setCallingNumber(String callingNumber) { 320 321 } 322 323 @Override 324 String getAnswerNumber() { 325 return null; 326 } 327 328 @Override 329 void setAnswerNumber(String answerNumber) { 330 331 } 332 } 333 abstract class ChargeRule{ 334 abstract public double calCost(ArrayList<CallRecord> callRecords); 335 } 336 abstract class CallChargeRule extends ChargeRule{ 337 abstract public double calCost(ArrayList<CallRecord> callRecords); 338 } 339 class LandPhonelnCityRule extends CallChargeRule{ 340 341 @Override 342 public double calCost(ArrayList<CallRecord> callRecords) { 343 double sum=0; 344 for (int i = 0; i < callRecords.size(); i++) { 345 long time1 = callRecords.get(i).getStartTime().getTime(); 346 long time2 = callRecords.get(i).getEndTime().getTime(); 347 long second = (time2 - time1) / 1000; 348 if (second % 60 != 0) { 349 sum += 0.1 * ((double)(second / 60)+1.0); 350 }else{ 351 sum += 0.1 * (double)(second / 60); 352 } 353 } 354 return sum; 355 } 356 } 357 class LandPhonelnProvinceRule extends CallChargeRule{ 358 359 @Override 360 public double calCost(ArrayList<CallRecord> callRecords) { 361 double sum=0; 362 for (int i = 0; i < callRecords.size(); i++) { 363 long time1 = callRecords.get(i).getStartTime().getTime(); 364 long time2 = callRecords.get(i).getEndTime().getTime(); 365 long second = (time2 - time1) / 1000; 366 if (second % 60 != 0) { 367 sum += 0.3 * ((double)(second / 60)+1.0); 368 }else{ 369 sum += 0.3 * (double)(second / 60); 370 } 371 } 372 return sum; 373 } 374 } 375 class LandPhonelnLandRule extends CallChargeRule{ 376 377 @Override 378 public double calCost(ArrayList<CallRecord> callRecords) { 379 double sum=0; 380 for (int i = 0; i < callRecords.size(); i++) { 381 long time1 = callRecords.get(i).getStartTime().getTime(); 382 long time2 = callRecords.get(i).getEndTime().getTime(); 383 long second = (time2 - time1) / 1000; 384 if (second % 60 != 0) { 385 sum += 0.6 * ((double)(second / 60)+1.0); 386 }else{ 387 sum += 0.6 * (double)(second / 60); 388 } 389 } 390 return sum; 391 } 392 }
分析心得:设计的思路最开始根据老师所给的类图依次创建好,然后计算话费,再创建主函数,利用正则表达式来判断格式的错误,另外我还用了TreeMap集合进行排序,算是一个比较新颖的点。
踩坑心得:在新建类中,如果类中的属性是私有的,则不能直接调用它,而是需要在其中再定义一个方法,来获取类中的属性,从而通过这个方法来调用到想要
获取的类中的属性,同时,还需要定义一个方法来将传进来的参数赋值给此方法中的属性。这样才能在此类中使用属性的值。
改进建议:在本题中的格式错误形式也是多种多样的,按照之前的思路,只能是想到一种错误情况去特判一种错误情况,而这种方法太容易遗漏一些不符合格式
的错误形式,从而导致有些数据本不符合格式要求却通过了特判,进入了数据处理。而如果采用正则表达式的方法,就可以避免这种情况的出现,后续还应继续
深度学习这种表达式的使用及运用。
7-1 电信计费系列2-手机+座机计费
1 import java.text.SimpleDateFormat; 2 import java.util.*; 3 public class Main { 4 5 public static void main(String[] args) { 6 Scanner input = new Scanner(System.in); 7 HashMap<String,User> map = new HashMap<String,User>(); 8 HashMap<String,User> map1 = new HashMap<String,User>(); 9 ArrayList<String> list = new ArrayList<>(); 10 ArrayList<String> list1 = new ArrayList<>(); 11 while (true) { 12 String s = input.nextLine(); 13 if (s.equals("end")) { 14 break; 15 } else { 16 if (s.matches("u-[0-9]{11}\\s(0|1)")) { 17 String[] s1 = s.split("\\s"); 18 String s2[] = s1[0].split("-"); 19 User user = new User(100, s2[1]); 20 ChargeMode chargeMode = new MobilePhoneCharging(); 21 ArrayList<ChargeRule> chargeRules = new ArrayList<ChargeRule>(); 22 chargeRules.add(new MobilePhonelnCityToCityRule()); 23 chargeRules.add(new MobilePhonelnCityToProvinceRule()); 24 chargeRules.add(new MobilePhonelnCityToLandRule()); 25 chargeRules.add(new MobilePhonelnProvinceToCityRule()); 26 chargeRules.add(new MobilePhonelnProvinceToProvinceRule()); 27 chargeRules.add(new MobilePhonelnProvinceToLandRule()); 28 chargeRules.add(new MobilePhonelnLandToCityRule()); 29 chargeRules.add(new MobilePhonelnLandToProvinceRule()); 30 chargeRules.add(new MobilePhonelnLandToLandRule()); 31 chargeRules.add(new MobliePhonelnLandAnswerRule()); 32 chargeMode.setChargeRules(chargeRules); 33 user.setChargeMode(chargeMode); 34 if(!map1.containsKey(s2[1])) { 35 map1.put(s2[1], user); 36 list1.add(s2[1]); 37 } 38 } else if (s.matches("u-[0-9]{11,12}\\s(0|1)")) { 39 String[] s1 = s.split("\\s"); 40 String s2[] = s1[0].split("-"); 41 User user = new User(100, s2[1]); 42 ChargeMode chargeMode = new LandlinePhoneCharging(); 43 ArrayList<ChargeRule> chargeRules = new ArrayList<ChargeRule>(); 44 chargeRules.add(new LandPhonelnCityRule()); 45 chargeRules.add(new LandPhonelnProvinceRule()); 46 chargeRules.add(new LandPhonelnLandRule()); 47 chargeMode.setChargeRules(chargeRules); 48 user.setChargeMode(chargeMode); 49 if(!map.containsKey(s2[1])) { 50 map.put(s2[1], user); 51 list.add(s2[1]); 52 } 53 } else if (s.matches("t-[0-9]{11,12}\\s[0-9]{10,12}\\s((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.([1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))\\.2\\.29))\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\\s((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.([1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))\\.2\\.29))\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])")) { 54 String[] s1 = s.split("\\s"); 55 String s2[] = s1[0].split("-"); 56 if (map.containsKey(s2[1])) { 57 User user = map.get(s2[1]); 58 CallRecord callRecord = new CallRecord(); 59 try { 60 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault()); 61 Date time1 = simpleDateFormat.parse(s1[2] + " " + s1[3]); 62 Date time2 = simpleDateFormat.parse(s1[4] + " " + s1[5]); 63 callRecord.setStartTime(time1); 64 callRecord.setEndTime(time2); 65 callRecord.setCallingAddressAreaCode(s2[1].substring(0, 4)); 66 callRecord.setAnswerAddressAreaCode(s1[1].substring(0, 4)); 67 callRecord.setCallingNumber(s2[1]); 68 callRecord.setAnswerNumber(s1[1]); 69 if (s1[1].substring(0, 3).equals("079") || s1[1].substring(0, 4).equals("0701")) { 70 if (s1[1].substring(0, 4).equals("0791")) { 71 user.userRecords.addCallinglnCityRecords(callRecord); 72 } else { 73 user.userRecords.addCallinglnProvinceRecords(callRecord); 74 } 75 } else { 76 user.userRecords.addCallinglnLandRecords(callRecord); 77 } 78 } catch (Exception ji) { 79 80 } 81 } 82 } else if (s.matches("t-[0-9]{10,12}\\s[0-9]{10,12}\\s[0-9]{3,4}\\s((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.([1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))\\.2\\.29))\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\\s((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.([1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))\\.2\\.29))\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])")) { 83 String[] s1 = s.split("\\s"); 84 String s2[] = s1[0].split("-"); 85 if (map.containsKey(s2[1]) || map1.containsKey(s1[1])) { 86 User user = null; 87 if (map.containsKey(s2[1])) { 88 user = map.get(s2[1]); 89 } 90 User user1 = null; 91 if (map1.containsKey(s1[1])) { 92 user1 = map1.get(s1[1]); 93 } 94 CallRecord callRecord = new CallRecord(); 95 try { 96 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault()); 97 Date time1 = simpleDateFormat.parse(s1[3] + " " + s1[4]); 98 Date time2 = simpleDateFormat.parse(s1[5] + " " + s1[6]); 99 callRecord.setStartTime(time1); 100 callRecord.setEndTime(time2); 101 callRecord.setCallingAddressAreaCode(s2[1].substring(0, 4)); 102 callRecord.setAnswerAddressAreaCode(s1[2]); 103 callRecord.setCallingNumber(s2[1]); 104 callRecord.setAnswerNumber(s1[1]); 105 if (s1[2].substring(0, 3).equals("079") || s1[2].substring(0, 4).equals("0701")) { 106 if (s1[2].substring(0, 4).equals("0791")) { 107 if (map.containsKey(s2[1])) user.userRecords.addCallinglnCityRecords(callRecord); 108 } else { 109 if (map.containsKey(s2[1])) user.userRecords.addCallinglnProvinceRecords(callRecord); 110 } 111 } else { 112 if (map.containsKey(s2[1])) user.userRecords.addCallinglnLandRecords(callRecord); 113 if (map1.containsKey(s1[1])) user1.userRecords.addCallinglnLandAnswerRecords(callRecord); 114 } 115 } catch (Exception ji) { 116 117 } 118 } 119 } else if (s.matches("t-[0-9]{10,12}\\s[0-9]{3,4}\\s[0-9]{10,12}\\s((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.([1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))\\.2\\.29))\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\\s((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.([1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))\\.2\\.29))\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])")) { 120 String[] s1 = s.split("\\s"); 121 String s2[] = s1[0].split("-"); 122 if (map1.containsKey(s2[1]) || map.containsKey(s1[2])) { 123 User user = null; 124 if (map1.containsKey(s2[1])) { 125 user = map1.get(s2[1]); 126 } 127 User user1 = null; 128 if (map.containsKey(s1[2])) { 129 user1 = map.get(s1[2]); 130 } 131 CallRecord callRecord = new CallRecord(); 132 try { 133 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault()); 134 Date time1 = simpleDateFormat.parse(s1[3] + " " + s1[4]); 135 Date time2 = simpleDateFormat.parse(s1[5] + " " + s1[6]); 136 callRecord.setStartTime(time1); 137 callRecord.setEndTime(time2); 138 callRecord.setCallingAddressAreaCode(s1[1]); 139 callRecord.setAnswerAddressAreaCode(s1[2].substring(0, 4)); 140 callRecord.setCallingNumber(s2[1]); 141 callRecord.setAnswerNumber(s1[2]); 142 if (s1[1].substring(0, 3).equals("079") || s1[1].equals("0701")) { 143 if (s1[1].equals("0791")) { 144 if (map1.containsKey(s2[1])) user.userRecords.addCallinglnCityToCityRecords(callRecord); 145 } else { 146 if (map1.containsKey(s2[1])) user.userRecords.addCallinglnProvinceToCityRecords(callRecord); 147 } 148 } else { 149 if (map1.containsKey(s2[1])) user.userRecords.addCallinglnLandToCityRecords(callRecord); 150 } 151 } catch (Exception ji) { 152 153 } 154 } 155 } else if (s.matches("t-[0-9]{10,12}\\s[0-9]{3,4}\\s[0-9]{10,12}\\s[0-9]{3,4}\\s((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.([1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))\\.2\\.29))\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\\s((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.([1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))\\.2\\.29))\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])")) { 156 String[] s1 = s.split("\\s"); 157 String s2[] = s1[0].split("-"); 158 if (map1.containsKey(s2[1]) || map.containsKey(s1[2])) { 159 User user = null; 160 if (map1.containsKey(s2[1])) { 161 user = map1.get(s2[1]); 162 } 163 User user1 = null; 164 if (map1.containsKey(s1[2])) { 165 user1 = map1.get(s1[2]); 166 } 167 CallRecord callRecord = new CallRecord(); 168 try { 169 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault()); 170 Date time1 = simpleDateFormat.parse(s1[4] + " " + s1[5]); 171 Date time2 = simpleDateFormat.parse(s1[6] + " " + s1[7]); 172 callRecord.setStartTime(time1); 173 callRecord.setEndTime(time2); 174 callRecord.setCallingAddressAreaCode(s1[1]); 175 callRecord.setAnswerAddressAreaCode(s1[3]); 176 callRecord.setCallingNumber(s2[1]); 177 callRecord.setAnswerNumber(s1[2]); 178 if (s1[1].substring(0, 3).equals("079") || s1[1].substring(0, 4).equals("0701")) { 179 if (s1[1].substring(0, 4).equals("0791")) { 180 if (s1[3].substring(0, 3).equals("079") || s1[3].substring(0, 4).equals("0701")) { 181 if (s1[3].substring(0, 4).equals("0791")) { 182 if (map1.containsKey(s2[1])) user.userRecords.addCallinglnCityToCityRecords(callRecord); 183 } else { 184 if (map1.containsKey(s2[1])) user.userRecords.addCallinglnCityToProvinceRecords(callRecord); 185 } 186 } else { 187 if (map1.containsKey(s2[1])) user.userRecords.addCallinglnCityToLandRecords(callRecord); 188 if (map1.containsKey(s1[2])) user1.userRecords.addCallinglnLandAnswerRecords(callRecord); 189 } 190 } else { 191 if (map1.containsKey(s2[1])) user.userRecords.addCallinglnProvinceToCityRecords(callRecord); 192 if (!s1[3].substring(0, 3).equals("079") && !s1[3].substring(0, 4).equals("0701")) { 193 if (map1.containsKey(s1[2])) user1.userRecords.addCallinglnLandAnswerRecords(callRecord); 194 } 195 } 196 } else { 197 if (map1.containsKey(s2[1])) user.userRecords.addCallinglnLandToCityRecords(callRecord); 198 if (!s1[3].substring(0, 3).equals("079") && !s1[3].substring(0, 4).equals("0701")) { 199 if (map1.containsKey(s1[2])) user1.userRecords.addCallinglnLandAnswerRecords(callRecord); 200 } 201 } 202 } catch (Exception ji) { 203 204 } 205 } 206 } 207 } 208 } 209 Collections.sort(list); 210 Iterator<String> it = list.iterator(); 211 while (it.hasNext()) { 212 String s = it.next(); 213 User value = map.get(s); 214 System.out.printf("%s %.1f %.1f\n",value.getNumber(),value.calCost(),value.calBalance()); 215 } 216 Collections.sort(list1); 217 Iterator<String> it1 = list1.iterator(); 218 while (it1.hasNext()) { 219 String s = it1.next(); 220 User value = map1.get(s); 221 System.out.printf("%s %.1f %.1f\n",value.getNumber(),value.calCost(),value.calBalance()); 222 } 223 } 224 } 225 class User{ 226 UserRecords userRecords = new UserRecords(); 227 double balance = 100; 228 ChargeMode chargeMode; 229 String number; 230 public double calCost(){ 231 double sum=chargeMode.calCost(userRecords); 232 return sum; 233 } 234 public double calBalance(){ 235 return balance-chargeMode.calCost(userRecords)-chargeMode.getMonthlyRent(); 236 } 237 User(double balance, String number) { 238 this.balance = balance; 239 this.number = number; 240 } 241 242 public UserRecords getUserRecords() { 243 return userRecords; 244 } 245 246 public void setUserRecords(UserRecords userRecords) { 247 this.userRecords = userRecords; 248 } 249 250 public double getBalance() { 251 return balance; 252 } 253 254 public void setBalance(double balance) { 255 this.balance = balance; 256 } 257 258 public ChargeMode getChargeMode() { 259 return chargeMode; 260 } 261 262 public void setChargeMode(ChargeMode chargeMode) { 263 this.chargeMode = chargeMode; 264 } 265 266 public String getNumber() { 267 return number; 268 } 269 270 public void setNumber(String number) { 271 this.number = number; 272 } 273 } 274 abstract class ChargeMode{ 275 ArrayList<ChargeRule> chargeRules = new ArrayList<>(); 276 public ArrayList<ChargeRule> getChargeRules() { 277 return chargeRules; 278 } 279 public void setChargeRules(ArrayList<ChargeRule> chargeRules) { 280 this.chargeRules = chargeRules; 281 } 282 abstract double calCost(UserRecords userRecords); 283 abstract double getMonthlyRent(); 284 } 285 class MobilePhoneCharging extends ChargeMode{ 286 double monthlyRent = 15; 287 @Override 288 double calCost(UserRecords userRecords) { 289 double sum=0; 290 sum+=getChargeRules().get(0).calCost(userRecords.getCallinglnCityToCityRecords()); 291 sum+=getChargeRules().get(1).calCost(userRecords.getCallinglnCityToProvinceRecords()); 292 sum+=getChargeRules().get(2).calCost(userRecords.getCallinglnCityToLandRecords()); 293 sum+=getChargeRules().get(3).calCost(userRecords.getCallinglnProvinceToCityRecords()); 294 sum+=getChargeRules().get(4).calCost(userRecords.getCallinglnProvinceToProvinceRecords()); 295 sum+=getChargeRules().get(5).calCost(userRecords.getCallinglnProvinceToLandRecords()); 296 sum+=getChargeRules().get(6).calCost(userRecords.getCallinglnLandToCityRecords()); 297 sum+=getChargeRules().get(7).calCost(userRecords.getCallinglnLandToProvinceRecords()); 298 sum+=getChargeRules().get(8).calCost(userRecords.getCallinglnLandToLandRecords()); 299 sum+=getChargeRules().get(9).calCost(userRecords.getCallinglnLandAnswerRecords()); 300 return sum; 301 } 302 303 @Override 304 double getMonthlyRent() { 305 return monthlyRent; 306 } 307 } 308 class LandlinePhoneCharging extends ChargeMode{ 309 double monthlyRent = 20; 310 @Override 311 public double calCost(UserRecords userRecords){ 312 double sum=0; 313 sum+=getChargeRules().get(0).calCost(userRecords.getCallinglnCityRecords()); 314 sum+=getChargeRules().get(1).calCost(userRecords.getCallinglnProvinceRecords()); 315 sum+=getChargeRules().get(2).calCost(userRecords.getCallinglnLandRecords()); 316 return sum; 317 } 318 319 @Override 320 public double getMonthlyRent() { 321 return monthlyRent; 322 } 323 } 324 class UserRecords{ 325 ArrayList<CallRecord> callinglnCityToCityRecords = new ArrayList<CallRecord>(); 326 ArrayList<CallRecord> callinglnCityToProvinceRecords = new ArrayList<CallRecord>(); 327 ArrayList<CallRecord> callinglnCityToLandRecords = new ArrayList<CallRecord>(); 328 ArrayList<CallRecord> callinglnProvinceToCityRecords = new ArrayList<CallRecord>(); 329 ArrayList<CallRecord> callinglnProvinceToProvinceRecords = new ArrayList<CallRecord>(); 330 ArrayList<CallRecord> callinglnProvinceToLandRecords = new ArrayList<CallRecord>(); 331 ArrayList<CallRecord> callinglnLandToCityRecords = new ArrayList<CallRecord>(); 332 ArrayList<CallRecord> callinglnLandToProvinceRecords = new ArrayList<CallRecord>(); 333 ArrayList<CallRecord> callinglnLandToLandRecords = new ArrayList<CallRecord>(); 334 ArrayList<CallRecord> callinglnLandAnswerRecords = new ArrayList<CallRecord>(); 335 ArrayList<CallRecord> callinglnCityRecords = new ArrayList<CallRecord>(); 336 ArrayList<CallRecord> callinglnProvinceRecords = new ArrayList<CallRecord>(); 337 ArrayList<CallRecord> callinglnLandRecords = new ArrayList<CallRecord>(); 338 ArrayList<CallRecord> answerlnCityRecords = new ArrayList<CallRecord>(); 339 ArrayList<CallRecord> answerlnProvinceRecords = new ArrayList<CallRecord>(); 340 ArrayList<CallRecord> answerlnLandRecords = new ArrayList<CallRecord>(); 341 ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>(); 342 ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>(); 343 344 void addCallinglnCityRecords(CallRecord callRecord) { 345 callinglnCityRecords.add(callRecord); 346 } 347 void addCallinglnProvinceRecords(CallRecord callRecord) { 348 callinglnProvinceRecords.add(callRecord); 349 } 350 void addCallinglnLandRecords(CallRecord callRecord) { 351 callinglnLandRecords.add(callRecord); 352 } 353 void addAnswerlnCityRecords(CallRecord answerRecord) { 354 answerlnCityRecords.add(answerRecord); 355 } 356 void addAnswerlnProvinceRecords(CallRecord answerRecord) { 357 answerlnProvinceRecords.add(answerRecord); 358 } 359 void addAnswerlnLandRecords(CallRecord answerRecord) { 360 answerlnLandRecords.add(answerRecord); 361 } 362 void addSendMessageRecords(MessageRecord sendMessageRecord) { 363 sendMessageRecords.add(sendMessageRecord); 364 } 365 void addReceiveMessageRecords(MessageRecord receiveMessageRecord) { 366 receiveMessageRecords.add(receiveMessageRecord); 367 } 368 public ArrayList<CallRecord> getCallinglnCityRecords() { 369 return callinglnCityRecords; 370 } 371 372 public void setCallinglnCityRecords(ArrayList<CallRecord> callinglnCityRecords) { 373 this.callinglnCityRecords = callinglnCityRecords; 374 } 375 376 public ArrayList<CallRecord> getCallinglnProvinceRecords() { 377 return callinglnProvinceRecords; 378 } 379 380 public void setCallinglnProvinceRecords(ArrayList<CallRecord> callinglnProvinceRecords) { 381 this.callinglnProvinceRecords = callinglnProvinceRecords; 382 } 383 384 public ArrayList<CallRecord> getCallinglnLandRecords() { 385 return callinglnLandRecords; 386 } 387 388 public void setCallinglnLandRecords(ArrayList<CallRecord> callinglnLandRecords) { 389 this.callinglnLandRecords = callinglnLandRecords; 390 } 391 392 public ArrayList<CallRecord> getAnswerlnCityRecords() { 393 return answerlnCityRecords; 394 } 395 396 public void setAnswerlnCityRecords(ArrayList<CallRecord> answerlnCityRecords) { 397 this.answerlnCityRecords = answerlnCityRecords; 398 } 399 400 public ArrayList<CallRecord> getAnswerlnProvinceRecords() { 401 return answerlnProvinceRecords; 402 } 403 404 public void setAnswerlnProvinceRecords(ArrayList<CallRecord> answerlnProvinceRecords) { 405 this.answerlnProvinceRecords = answerlnProvinceRecords; 406 } 407 408 public ArrayList<CallRecord> getAnswerlnLandRecords() { 409 return answerlnLandRecords; 410 } 411 412 public void setAnswerlnLandRecords(ArrayList<CallRecord> answerlnLandRecords) { 413 this.answerlnLandRecords = answerlnLandRecords; 414 } 415 416 public ArrayList<MessageRecord> getSendMessageRecords() { 417 return sendMessageRecords; 418 } 419 420 public void setSendMessageRecords(ArrayList<MessageRecord> sendMessageRecords) { 421 this.sendMessageRecords = sendMessageRecords; 422 } 423 424 public ArrayList<MessageRecord> getReceiveMessageRecords() { 425 return receiveMessageRecords; 426 } 427 428 public void setReceiveMessageRecords(ArrayList<MessageRecord> receiveMessageRecords) { 429 this.receiveMessageRecords = receiveMessageRecords; 430 } 431 432 public ArrayList<CallRecord> getCallinglnCityToCityRecords() { 433 return callinglnCityToCityRecords; 434 } 435 436 public ArrayList<CallRecord> getCallinglnCityToProvinceRecords() { 437 return callinglnCityToProvinceRecords; 438 } 439 440 public ArrayList<CallRecord> getCallinglnCityToLandRecords() { 441 return callinglnCityToLandRecords; 442 } 443 444 public ArrayList<CallRecord> getCallinglnProvinceToCityRecords() { 445 return callinglnProvinceToCityRecords; 446 } 447 448 public ArrayList<CallRecord> getCallinglnProvinceToProvinceRecords() { 449 return callinglnProvinceToProvinceRecords; 450 } 451 452 public ArrayList<CallRecord> getCallinglnProvinceToLandRecords() { 453 return callinglnProvinceToLandRecords; 454 } 455 456 public ArrayList<CallRecord> getCallinglnLandToCityRecords() { 457 return callinglnLandToCityRecords; 458 } 459 460 public ArrayList<CallRecord> getCallinglnLandToProvinceRecords() { 461 return callinglnLandToProvinceRecords; 462 } 463 464 public ArrayList<CallRecord> getCallinglnLandToLandRecords() { 465 return callinglnLandToLandRecords; 466 } 467 468 public ArrayList<CallRecord> getCallinglnLandAnswerRecords() { 469 return callinglnLandAnswerRecords; 470 } 471 public void addCallinglnLandAnswerRecords(CallRecord callRecord) { 472 callinglnLandAnswerRecords.add(callRecord); 473 } 474 public void addCallinglnCityToCityRecords(CallRecord callRecord) { 475 callinglnCityToCityRecords.add(callRecord); 476 } 477 478 public void addCallinglnCityToProvinceRecords(CallRecord callRecord) { 479 callinglnCityToProvinceRecords.add(callRecord); 480 } 481 482 public void addCallinglnCityToLandRecords(CallRecord callRecord) { 483 callinglnCityToLandRecords.add(callRecord); 484 } 485 486 public void addCallinglnProvinceToCityRecords(CallRecord callRecord) { 487 callinglnProvinceToCityRecords.add(callRecord); 488 } 489 490 public void addCallinglnProvinceToProvinceRecords(CallRecord callRecord) { 491 callinglnProvinceToProvinceRecords.add(callRecord); 492 } 493 494 public void addCallinglnProvinceToLandRecords(CallRecord callRecord) { 495 callinglnProvinceToLandRecords.add(callRecord); 496 } 497 498 public void addCallinglnLandToCityRecords(CallRecord callRecord) { 499 callinglnLandToCityRecords.add(callRecord); 500 } 501 502 public void addCallinglnLandToProvinceRecords(CallRecord callRecord) { 503 callinglnLandToProvinceRecords.add(callRecord); 504 } 505 506 public void addCallinglnLandToLandRecords(CallRecord callRecord) { 507 callinglnLandToLandRecords.add(callRecord); 508 } 509 } 510 abstract class CommunicationRecord{ 511 String callingNumber; 512 String answerNumber; 513 abstract String getCallingNumber(); 514 abstract void setCallingNumber(String callingNumber); 515 abstract String getAnswerNumber(); 516 abstract void setAnswerNumber(String answerNumber); 517 } 518 class CallRecord extends CommunicationRecord{ 519 Date startTime; 520 Date endTime; 521 String callingAddressAreaCode; 522 String answerAddressAreaCode; 523 524 public Date getStartTime() { 525 return startTime; 526 } 527 528 public void setStartTime(Date startTime) { 529 this.startTime = startTime; 530 } 531 532 public Date getEndTime() { 533 return endTime; 534 } 535 536 public void setEndTime(Date endTime) { 537 this.endTime = endTime; 538 } 539 540 public String getCallingAddressAreaCode() { 541 return callingAddressAreaCode; 542 } 543 544 public void setCallingAddressAreaCode(String callingAddressAreaCode) { 545 this.callingAddressAreaCode = callingAddressAreaCode; 546 } 547 548 public String getAnswerAddressAreaCode() { 549 return answerAddressAreaCode; 550 } 551 552 public void setAnswerAddressAreaCode(String answerAddressAreaCode) { 553 this.answerAddressAreaCode = answerAddressAreaCode; 554 } 555 556 @Override 557 String getCallingNumber() { 558 return null; 559 } 560 561 @Override 562 void setCallingNumber(String callingNumber) { 563 564 } 565 566 @Override 567 String getAnswerNumber() { 568 return null; 569 } 570 571 @Override 572 void setAnswerNumber(String answerNumber) { 573 574 } 575 } 576 class MessageRecord extends CommunicationRecord{ 577 String message; 578 String getMessage() { 579 return null; 580 } 581 void setMessage(String message) { 582 583 } 584 585 @Override 586 String getCallingNumber() { 587 return null; 588 } 589 590 @Override 591 void setCallingNumber(String callingNumber) { 592 593 } 594 595 @Override 596 String getAnswerNumber() { 597 return null; 598 } 599 600 @Override 601 void setAnswerNumber(String answerNumber) { 602 603 } 604 } 605 abstract class ChargeRule{ 606 607 abstract public double calCost(ArrayList<CallRecord> callinglnCityRecords); 608 } 609 abstract class CallChargeRule extends ChargeRule{ 610 abstract public double calCost(ArrayList<CallRecord> callRecords); 611 } 612 class LandPhonelnCityRule extends CallChargeRule{ 613 614 @Override 615 public double calCost(ArrayList<CallRecord> callRecords) { 616 double sum=0; 617 for (int i = 0; i < callRecords.size(); i++) { 618 long time1 = callRecords.get(i).getStartTime().getTime(); 619 long time2 = callRecords.get(i).getEndTime().getTime(); 620 long second = (time2 - time1) / 1000; 621 if (second % 60 != 0) { 622 sum += 0.1 * ((double)(second / 60)+1.0); 623 }else{ 624 sum += 0.1 * (double)(second / 60); 625 } 626 } 627 return sum; 628 } 629 } 630 class LandPhonelnProvinceRule extends CallChargeRule{ 631 632 @Override 633 public double calCost(ArrayList<CallRecord> callRecords) { 634 double sum=0; 635 for (int i = 0; i < callRecords.size(); i++) { 636 long time1 = callRecords.get(i).getStartTime().getTime(); 637 long time2 = callRecords.get(i).getEndTime().getTime(); 638 long second = (time2 - time1) / 1000; 639 if (second % 60 != 0) { 640 sum += 0.3 * ((double)(second / 60)+1.0); 641 }else{ 642 sum += 0.3 * (double)(second / 60); 643 } 644 } 645 return sum; 646 } 647 } 648 class LandPhonelnLandRule extends CallChargeRule{ 649 650 @Override 651 public double calCost(ArrayList<CallRecord> callRecords) { 652 double sum=0; 653 for (int i = 0; i < callRecords.size(); i++) { 654 long time1 = callRecords.get(i).getStartTime().getTime(); 655 long time2 = callRecords.get(i).getEndTime().getTime(); 656 long second = (time2 - time1) / 1000; 657 if (second % 60 != 0) { 658 sum += 0.6 * ((double)(second / 60)+1.0); 659 }else{ 660 sum += 0.6 * (double)(second / 60); 661 } 662 } 663 return sum; 664 } 665 } 666 // abstract class MessageChargeRule extends ChargeRule{ 667 // abstract public double calCost(ArrayList<MessageRecord> messageRecords); 668 // } 669 // class SendMessageRule extends MessageChargeRule{ 670 // 671 // @Override 672 // public double calCost(ArrayList<MessageRecord> messageRecords) { 673 // return 0; 674 // } 675 // } 676 class MobilePhonelnCityToCityRule extends CallChargeRule{ 677 678 @Override 679 public double calCost(ArrayList<CallRecord> callRecords) { 680 double sum=0; 681 for (int i = 0; i < callRecords.size(); i++) { 682 long time1 = callRecords.get(i).getStartTime().getTime(); 683 long time2 = callRecords.get(i).getEndTime().getTime(); 684 long second = (time2 - time1) / 1000; 685 if (second % 60 != 0) { 686 sum += 0.1 * ((double)(second / 60)+1.0); 687 }else{ 688 sum += 0.1 * (double)(second / 60); 689 } 690 } 691 return sum; 692 } 693 } 694 class MobilePhonelnCityToProvinceRule extends CallChargeRule{ 695 696 @Override 697 public double calCost(ArrayList<CallRecord> callRecords) { 698 double sum=0; 699 for (int i = 0; i < callRecords.size(); i++) { 700 long time1 = callRecords.get(i).getStartTime().getTime(); 701 long time2 = callRecords.get(i).getEndTime().getTime(); 702 long second = (time2 - time1) / 1000; 703 if (second % 60 != 0) { 704 sum += 0.2 * ((double)(second / 60)+1.0); 705 }else{ 706 sum += 0.2 * (double)(second / 60); 707 } 708 } 709 return sum; 710 } 711 } 712 class MobilePhonelnCityToLandRule extends CallChargeRule{ 713 714 @Override 715 public double calCost(ArrayList<CallRecord> callRecords) { 716 double sum=0; 717 for (int i = 0; i < callRecords.size(); i++) { 718 long time1 = callRecords.get(i).getStartTime().getTime(); 719 long time2 = callRecords.get(i).getEndTime().getTime(); 720 long second = (time2 - time1) / 1000; 721 if (second % 60 != 0) { 722 sum += 0.3 * ((double)(second / 60)+1.0); 723 }else{ 724 sum += 0.3 * (double)(second / 60); 725 } 726 } 727 return sum; 728 } 729 }class MobilePhonelnProvinceToCityRule extends CallChargeRule{ 730 731 @Override 732 public double calCost(ArrayList<CallRecord> callRecords) { 733 double sum=0; 734 for (int i = 0; i < callRecords.size(); i++) { 735 long time1 = callRecords.get(i).getStartTime().getTime(); 736 long time2 = callRecords.get(i).getEndTime().getTime(); 737 long second = (time2 - time1) / 1000; 738 if (second % 60 != 0) { 739 sum += 0.3 * ((double)(second / 60)+1.0); 740 }else{ 741 sum += 0.3 * (double)(second / 60); 742 } 743 } 744 return sum; 745 } 746 } 747 class MobilePhonelnProvinceToProvinceRule extends CallChargeRule{ 748 749 @Override 750 public double calCost(ArrayList<CallRecord> callRecords) { 751 double sum=0; 752 for (int i = 0; i < callRecords.size(); i++) { 753 long time1 = callRecords.get(i).getStartTime().getTime(); 754 long time2 = callRecords.get(i).getEndTime().getTime(); 755 long second = (time2 - time1) / 1000; 756 if (second % 60 != 0) { 757 sum += 0.3 * ((double)(second / 60)+1.0); 758 }else{ 759 sum += 0.3 * (double)(second / 60); 760 } 761 } 762 return sum; 763 } 764 } 765 class MobilePhonelnProvinceToLandRule extends CallChargeRule{ 766 767 @Override 768 public double calCost(ArrayList<CallRecord> callRecords) { 769 double sum=0; 770 for (int i = 0; i < callRecords.size(); i++) { 771 long time1 = callRecords.get(i).getStartTime().getTime(); 772 long time2 = callRecords.get(i).getEndTime().getTime(); 773 long second = (time2 - time1) / 1000; 774 if (second % 60 != 0) { 775 sum += 0.3 * ((double)(second / 60)+1.0); 776 }else{ 777 sum += 0.3 * (double)(second / 60); 778 } 779 } 780 return sum; 781 } 782 } 783 class MobilePhonelnLandToProvinceRule extends CallChargeRule{ 784 785 @Override 786 public double calCost(ArrayList<CallRecord> callRecords) { 787 double sum=0; 788 for (int i = 0; i < callRecords.size(); i++) { 789 long time1 = callRecords.get(i).getStartTime().getTime(); 790 long time2 = callRecords.get(i).getEndTime().getTime(); 791 long second = (time2 - time1) / 1000; 792 if (second % 60 != 0) { 793 sum += 0.6 * ((double)(second / 60)+1.0); 794 }else{ 795 sum += 0.6 * (double)(second / 60); 796 } 797 } 798 return sum; 799 } 800 } 801 class MobilePhonelnLandToLandRule extends CallChargeRule{ 802 803 @Override 804 public double calCost(ArrayList<CallRecord> callRecords) { 805 double sum=0; 806 for (int i = 0; i < callRecords.size(); i++) { 807 long time1 = callRecords.get(i).getStartTime().getTime(); 808 long time2 = callRecords.get(i).getEndTime().getTime(); 809 long second = (time2 - time1) / 1000; 810 if (second % 60 != 0) { 811 sum += 0.6 * ((double)(second / 60)+1.0); 812 }else{ 813 sum += 0.6 * (double)(second / 60); 814 } 815 } 816 return sum; 817 } 818 } 819 class MobilePhonelnLandToCityRule extends CallChargeRule{ 820 821 @Override 822 public double calCost(ArrayList<CallRecord> callRecords) { 823 double sum=0; 824 for (int i = 0; i < callRecords.size(); i++) { 825 long time1 = callRecords.get(i).getStartTime().getTime(); 826 long time2 = callRecords.get(i).getEndTime().getTime(); 827 long second = (time2 - time1) / 1000; 828 if (second % 60 != 0) { 829 sum += 0.6 * ((double)(second / 60)+1.0); 830 }else{ 831 sum += 0.6 * (double)(second / 60); 832 } 833 } 834 return sum; 835 } 836 } 837 class MobliePhonelnLandAnswerRule extends CallChargeRule{ 838 839 @Override 840 public double calCost(ArrayList<CallRecord> callRecords) { 841 double sum=0; 842 for (int i = 0; i < callRecords.size(); i++) { 843 long time1 = callRecords.get(i).getStartTime().getTime(); 844 long time2 = callRecords.get(i).getEndTime().getTime(); 845 long second = (time2 - time1) / 1000; 846 if (second % 60 != 0) { 847 sum += 0.3 * ((double)(second / 60)+1.0); 848 }else{ 849 sum += 0.3 * (double)(second / 60); 850 } 851 } 852 return sum; 853 } 854 }
分析心得:在最开始使用TreeMap的时候,在后面<>中定义了三个变量名称,报错之后才发现只能定义两个。此次大作业的类有很多,在写代码之后最好在纸上先理清楚它们的关系,这又有助于后面写代码。
踩坑心得:在将整个字符串进行分割的时候,一开始先将数据以空格分开,放入字符串数组中去,但这样做的问题就是,将数据分开后,字符串数组中的第一个数组的前面是带着编号的,这就导致按空格分割后的字符串数组中的数据不都全一样,且对于之后的编号判断也有一定麻烦。所以在以空格分割之前应先以分号分割一边,将编号字符分割出去,这样也利于后续编号的判断。
改进建议:在本题中的格式错误形式也是多种多样的,按照之前的思路,只能是想到一种错误情况去特判一种错误情况,而这种方法太容易遗漏一些不符合格式的错误形式,从而导致有些数据本不符合格式要求却通过了特判,进入了数据处理。而如果采用正则表达式的方法,就可以避免这种情况的出现,后续还应继续深度学习这种表达式的使用及运用。
7-1 电信计费系列3-短信计费
1 import java.text.SimpleDateFormat; 2 import java.util.*; 3 public class Main { 4 5 public static void main(String[] args) { 6 Scanner input = new Scanner(System.in); 7 HashMap<String,User> map = new HashMap<String,User>(); 8 HashMap<String,User> map1 = new HashMap<String,User>(); 9 ArrayList<String> list = new ArrayList<>(); 10 ArrayList<String> list1 = new ArrayList<>(); 11 while (true) { 12 String s = input.nextLine(); 13 if (s.equals("end")) { 14 break; 15 } else { 16 if (s.matches("u-[0-9]{11,12}\\s3")) { 17 String[] s1 = s.split("\\s"); 18 String s2[] = s1[0].split("-"); 19 User user = new User(100, s2[1]); 20 ChargeMode chargeMode = new MobilePhoneCharging(); 21 ArrayList<ChargeRule> chargeRules = new ArrayList<ChargeRule>(); 22 chargeRules.add(new SendMessageRule()); 23 chargeMode.setChargeRules(chargeRules); 24 user.setChargeMode(chargeMode); 25 map1.put(s2[1], user); 26 list1.add(s2[1]); 27 } 28 if (s.matches("m-[0-9]{11,12}\\s[0-9]{10,12}\\s([0-9]|[a-z]|[A-Z]|,|\\.|\\s)+")) { 29 String[] s1 = s.split("\\s"); 30 String s2[] = s1[0].split("-"); 31 if (map1.containsKey(s2[1])) { 32 User user = map1.get(s2[1]); 33 MessageRecord messageRecord = new MessageRecord(); 34 String s3 = s.substring(26, s.length()); 35 messageRecord.setMessage(s3); 36 user.userRecords.addSendMessageRecords(messageRecord); 37 38 } 39 } 40 } 41 } 42 Collections.sort(list); 43 Iterator<String> it = list.iterator(); 44 while (it.hasNext()) { 45 String s = it.next(); 46 User value = map.get(s); 47 System.out.printf("%s %.1f %.1f\n",value.getNumber(),value.calCost(),value.calBalance()); 48 } 49 Collections.sort(list1); 50 Iterator<String> it1 = list1.iterator(); 51 while (it1.hasNext()) { 52 String s = it1.next(); 53 User value = map1.get(s); 54 System.out.printf("%s %.1f %.1f\n",value.getNumber(),value.calCost(),value.calBalance()); 55 } 56 } 57 } 58 class User{ 59 UserRecords userRecords = new UserRecords(); 60 double balance = 100; 61 ChargeMode chargeMode; 62 String number; 63 public double calCost(){ 64 double sum=chargeMode.calCost(userRecords); 65 return sum; 66 } 67 public double calBalance(){ 68 return balance-chargeMode.calCost(userRecords)-chargeMode.getMonthlyRent(); 69 } 70 User(double balance, String number) { 71 this.balance = balance; 72 this.number = number; 73 } 74 75 public UserRecords getUserRecords() { 76 return userRecords; 77 } 78 79 public void setUserRecords(UserRecords userRecords) { 80 this.userRecords = userRecords; 81 } 82 83 public double getBalance() { 84 return balance; 85 } 86 87 public void setBalance(double balance) { 88 this.balance = balance; 89 } 90 91 public ChargeMode getChargeMode() { 92 return chargeMode; 93 } 94 95 public void setChargeMode(ChargeMode chargeMode) { 96 this.chargeMode = chargeMode; 97 } 98 99 public String getNumber() { 100 return number; 101 } 102 103 public void setNumber(String number) { 104 this.number = number; 105 } 106 } 107 abstract class ChargeMode{ 108 ArrayList<ChargeRule> chargeRules = new ArrayList<>(); 109 public ArrayList<ChargeRule> getChargeRules() { 110 return chargeRules; 111 } 112 public void setChargeRules(ArrayList<ChargeRule> chargeRules) { 113 this.chargeRules = chargeRules; 114 } 115 abstract double calCost(UserRecords userRecords); 116 abstract double getMonthlyRent(); 117 } 118 class MobilePhoneCharging extends ChargeMode{ 119 double monthlyRent = 0; 120 @Override 121 double calCost(UserRecords userRecords) { 122 double sum=0; 123 sum+=getChargeRules().get(0).calCost1(userRecords.getSendMessageRecords() ); 124 return sum; 125 } 126 127 @Override 128 double getMonthlyRent() { 129 return monthlyRent; 130 } 131 } 132 class LandlinePhoneCharging extends ChargeMode{ 133 double monthlyRent = 20; 134 @Override 135 public double calCost(UserRecords userRecords){ 136 double sum=0; 137 return sum; 138 } 139 140 @Override 141 public double getMonthlyRent() { 142 return monthlyRent; 143 } 144 } 145 class UserRecords{ 146 147 ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>(); 148 ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>(); 149 150 151 152 public ArrayList<MessageRecord> getSendMessageRecords() { 153 return sendMessageRecords; 154 } 155 public void addSendMessageRecords(MessageRecord message){ 156 sendMessageRecords.add(message); 157 } 158 public void setSendMessageRecords(ArrayList<MessageRecord> sendMessageRecords) { 159 this.sendMessageRecords = sendMessageRecords; 160 } 161 162 public ArrayList<MessageRecord> getReceiveMessageRecords() { 163 return receiveMessageRecords; 164 } 165 166 public void setReceiveMessageRecords(ArrayList<MessageRecord> receiveMessageRecords) { 167 this.receiveMessageRecords = receiveMessageRecords; 168 } 169 170 171 172 } 173 abstract class CommunicationRecord{ 174 String callingNumber; 175 String answerNumber; 176 abstract String getCallingNumber(); 177 abstract void setCallingNumber(String callingNumber); 178 abstract String getAnswerNumber(); 179 abstract void setAnswerNumber(String answerNumber); 180 } 181 class CallRecord extends CommunicationRecord{ 182 Date startTime; 183 Date endTime; 184 String callingAddressAreaCode; 185 String answerAddressAreaCode; 186 187 public Date getStartTime() { 188 return startTime; 189 } 190 191 public void setStartTime(Date startTime) { 192 this.startTime = startTime; 193 } 194 195 public Date getEndTime() { 196 return endTime; 197 } 198 199 public void setEndTime(Date endTime) { 200 this.endTime = endTime; 201 } 202 203 public String getCallingAddressAreaCode() { 204 return callingAddressAreaCode; 205 } 206 207 public void setCallingAddressAreaCode(String callingAddressAreaCode) { 208 this.callingAddressAreaCode = callingAddressAreaCode; 209 } 210 211 public String getAnswerAddressAreaCode() { 212 return answerAddressAreaCode; 213 } 214 215 public void setAnswerAddressAreaCode(String answerAddressAreaCode) { 216 this.answerAddressAreaCode = answerAddressAreaCode; 217 } 218 219 @Override 220 String getCallingNumber() { 221 return null; 222 } 223 224 @Override 225 void setCallingNumber(String callingNumber) { 226 227 } 228 229 @Override 230 String getAnswerNumber() { 231 return null; 232 } 233 234 @Override 235 void setAnswerNumber(String answerNumber) { 236 237 } 238 } 239 class MessageRecord extends CommunicationRecord{ 240 String message; 241 String getMessage() { 242 return message; 243 } 244 public void setMessage(String message) { 245 this.message = message; 246 } 247 248 @Override 249 String getCallingNumber() { 250 return null; 251 } 252 253 @Override 254 void setCallingNumber(String callingNumber) { 255 256 } 257 258 @Override 259 String getAnswerNumber() { 260 return null; 261 } 262 263 @Override 264 void setAnswerNumber(String answerNumber) { 265 266 } 267 } 268 abstract class ChargeRule{ 269 abstract public double calCost1(ArrayList<MessageRecord> messageRecords); 270 abstract public double calCost(ArrayList<CallRecord> callinglnCityRecords); 271 } 272 abstract class MessageChargeRule extends ChargeRule{ 273 abstract public double calCost1(ArrayList<MessageRecord> messageRecords); 274 } 275 class SendMessageRule extends MessageChargeRule{ 276 277 @Override 278 public double calCost(ArrayList<CallRecord> callinglnCityRecords) { 279 return 0; 280 } 281 282 @Override 283 public double calCost1(ArrayList<MessageRecord> messageRecords) { 284 double sum=0; 285 double l=0; 286 for (int i = 0; i < messageRecords.size(); i++) { 287 if(messageRecords.get(i).message.length()%10!=0) { 288 l+=messageRecords.get(i).message.length()/10+1; 289 }else{ 290 l+=messageRecords.get(i).message.length()/10; 291 } 292 } 293 if(l<=3){ 294 sum = 0.1*l; 295 }else{ 296 if(l<=5){ 297 sum=0.3+(l-3)*0.2; 298 }else{ 299 sum = 0.7+(l-5)*0.3; 300 } 301 } 302 return sum; 303 } 304 } 305 abstract class CallChargeRule extends ChargeRule{ 306 abstract public double calCost(ArrayList<CallRecord> callRecords); 307 } 308 class LandPhonelnCityRule extends CallChargeRule{ 309 310 @Override 311 public double calCost1(ArrayList<MessageRecord> messageRecords) { 312 return 0; 313 } 314 315 @Override 316 public double calCost(ArrayList<CallRecord> callRecords) { 317 double sum=0; 318 for (int i = 0; i < callRecords.size(); i++) { 319 long time1 = callRecords.get(i).getStartTime().getTime(); 320 long time2 = callRecords.get(i).getEndTime().getTime(); 321 long second = (time2 - time1) / 1000; 322 if (second % 60 != 0) { 323 sum += 0.1 * ((double)(second / 60)+1.0); 324 }else{ 325 sum += 0.1 * (double)(second / 60); 326 } 327 } 328 return sum; 329 } 330 }
Map
HashMap:JDK1.8 之前 HashMap 由数组+链表组成的,数组是 HashMap 存储元素的主体,链表则是主要为了解决哈希冲突而存在的,即 “拉链法” 解决冲突。JDK1.8 以后在解决哈希冲突时有了较大的变化,当链表长度大于阈值(默认为8)时,将链表转化为红黑树,以减少搜索时间(哈希表对键进行散列,Map结构即映射表存放键值对)
LinkedHashMap:LinkedHashMap 继承自 HashMap,所以它的底层仍然是基于拉链式散列结构即由数组和链表或红黑树组成。另外,LinkedHashMap 在上面结构的基础上,增加了一条双向链表,使得键值对的插入顺序以及访问顺序等逻辑可以得以实现。
Hashtable:数组+链表组成的,数组是 HashMap 的主体,链表则是主要为了解决哈希冲突而存在的
TreeMap:红黑树(平衡二叉排序树)
Set
HashSet: 基于 HashMap 实现的,底层采用 HashMap 来保存元素(不保证有序,唯一)
LinkedHashSet:LinkedHashSet 继承与 HashSet,并且其内部是通过 LinkedHashMap 来实现的。有点类似于我们之前说的LinkedHashMap 其内部是基于 Hashmap 实现一样,不过还是有一点点区别的。
TreeSet:红黑树,自平衡的排序二叉树(可实现自然排序,例如 a-z)
三、踩坑心得
虽然编写的代码大多数都能通过PTA的测试点,但其中还是有一些问题存在的。
这三次的大作业,让我收获颇丰,但同时也让我踩坑不少。
难度主要集中在对类这一大模块的语法,类与类之间的关系的掌握,正则表达式的使用也是一块硬骨头。但这次大作业的正则表达式相对上次对正则表达式的使用难度明显提高,让我一开始有些无所适从。
这次的题目还涉及到对Map,List的使用,让我们初步了解了java中的链表、集合等数据结构。
本次Blog题量和涉及到的知识点相比起第一次Blog多了不少,通过自己实现类与类之间关系而得到一些经验。实现LIst,Map接口的使用的过程中遇到了很多的困难,在同学们的帮助下都一一得到了解决。新的知识点例如多态和接口还需要进一步地夯实基础,勤看课本。
四、改进建议
1.我应多在类方面下功夫,我认为类是可以很好的避免不同的错误。并且学了Java的几个原则之后,更好的体会到了如何完成类的设计。
2.部分框架的使用比c语言数组要简单,运用的更容易,可以帮我们减少很多错误,减少我们在修改时浪费的时间,在今后的设计中,自己要学习使用这些方便的集合框架的,使代码更加的简洁、方便,且实现功能。
3.多使用Java中的思想封装啊多态啊接口啊理解其中的优缺点以及适用性;
4.对于很多未知的功能查阅资料 最快的还是看实例 有时候一大堆文字可能看不懂这个时候需要我们通过实例去理解以及自己去设断点去调试;
五、总结
到了激动人心的总结时刻了,这又是我编程学习的一个进度点,也是这次blog作业的总结。回看自己以前写的代码,实在不堪入目,不过这也让我意识到,我实实在在的在进步,这是让我十分开心的,并且让我找到将编程学习下去的动力。
1.感觉到自己学到了很多东西,从刚开始连创建 Scanner 类对象都要百度,到后面类的继承,看着自己写的代码越来越长,越来越复杂,感觉到自己的能力也越来越强。
2.自己还是需要去理解很多概念,很多东西我其实都会用,但让我讲出来却是十分难的。
3.自己还需多空出时间来学习,很多题目中涉及的知识点老师不会讲的很详细,自学能力还是要加强。
4.对老师的建议就是希望在每次题目结束后,老师能给出一份正规的答案供大家参考学习,我相信这不仅可以方便同学们的学习,而且也可以规范大家的代码格式。众所周知,代码规范性是以后工作中重要的一个考察点。有一个良好的编写习惯,能为以后的找工作避免许多不必要的麻烦,同时也可以在招聘中为自己加分
标签:return,String,double,blog3,public,sum,ArrayList 来源: https://www.cnblogs.com/21201115-myc/p/16389102.html