面对对象程序设计题目分析与心得3
2022/6/11 1:20:04
本文主要是介绍面对对象程序设计题目分析与心得3,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
一、前言
这一系列较其他的题目集题目虽然更多,但是此次的难度大大降低,再加上老师给的类图做起来可谓如鱼得水了,这次给我的感觉主要考查对类图的理解是否到位,还有正则表达式对用户输入的格式判断,创建接口对输入日期是否合法的判断,对选择容器是恰当,对容器中的对象按某一种方式排序,对SimpleDateFormat类创建yyyy.MM.dd HH:mm:ss对象中字母表达意思,格式化输出,抽象和继承等为一体的综合题目,但是实际难度小,难度主要还是集中在第一次,毕竟第一次接触这类题目,对类图的理解需要一段时间。
二、设计与分析
1.第六次题目集
1.设计
设计主要源于老师所给类图,整体构架搭好后,填入方法的主体,方法主体也大多都是根据老师提示走,此次只加入了一个格式判断的的类来判断用户是否输入正确。
排序方法代码,传入排序规则匿名类用Collections.sort进行排序,代码如下:
1 Collections.sort(users, new Comparator<User>() { 2 // 对users中的用户根据电话号码大小进行升序排序 3 @Override 4 public int compare(User o1, User o2) { 5 double d1 = Double.parseDouble(o1.getNumber()); 6 double d2 = Double.parseDouble(o2.getNumber()); 7 if(d1 > d2) { 8 return 1; 9 } 10 return -1; 11 } 12 });View Code
此次对用户记录存储没有给特定的类,写在了Main函数里面,代码如下:
1 for(int i=0; i<users.size(); i++) { 2 UserRecords userRecords = new UserRecords();//用户记录 3 for(int j=0; j<callRecords.size(); j++) { 4 // 将用户记录添加到对应的用户记录当中 5 if(users.get(i).getNumber().equals(callRecords.get(j).getCallingNumber())) { 6 if(callRecords.get(j).getAnswerAddressAreaCode().matches("0791")) { 7 // 市内打电话 8 userRecords.addCallingInCityRecords(callRecords.get(j)); 9 } 10 else if(callRecords.get(j).getAnswerAddressAreaCode().matches("((079\\d)|0701)")) { 11 // 省内打电话 12 userRecords.addCallingInProvinceRecords(callRecords.get(j)); 13 } 14 else { 15 // 国内打电话 16 userRecords.addCallingInLandRecords(callRecords.get(j)); 17 } 18 } 19 if(users.get(i).getNumber().equals(callRecords.get(j).getAnswerNumber())) { 20 if(callRecords.get(j).getCallingAddressAreaCode().matches("0791")) { 21 // 市内接电话 22 userRecords.addanswerInCityRecords(callRecords.get(j)); 23 } 24 else if(callRecords.get(j).getCallingAddressAreaCode().matches("((079\\d)|0701)")) { 25 // 省内接电话 26 userRecords.addanswerInProvinceRecords(callRecords.get(j)); 27 } 28 else { 29 // 国内接电话 30 userRecords.addanswerInLandRecords(callRecords.get(j)); 31 } 32 } 33 } 34 // 将用户记录存入对应用户中 35 users.get(i).setUserRecords(userRecords); 36 }View Code
2.类图
3.圈复杂度
4.码源
1 import java.text.ParseException; 2 import java.text.SimpleDateFormat; 3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.Comparator; 6 import java.util.Date; 7 import java.util.Scanner; 8 9 public class Main { 10 11 public static void main(String[] args) throws ParseException { 12 Scanner in = new Scanner(System.in); 13 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); 14 ArrayList<User> users = new ArrayList<User>(); 15 ChargeMode chargeMode; 16 Date start;//开始时间 17 Date end;//结束时间 18 String callAddress;//拨打地址 19 String receiveAddress;//接通地址 20 ArrayList<CallRecord> callRecords = new ArrayList<CallRecord>(); 21 String s = in.nextLine(); 22 23 while(!s.equals("end")) { 24 String[] ss = s.split(" "); 25 String[] sss = ss[0].split("-"); 26 if(InputFormat.Ujudge(s)) { 27 // 判断是否为开户信息,若是,则sss[1]为开户号码,ss[1]为计费类型 28 LandlinePhoneCharging l = new LandlinePhoneCharging();//座机 29 chargeMode = l; 30 boolean f = true; 31 for(User user : users) { 32 if(user.getNumber().equals(sss[1])) { 33 // 判断是否已经有此开户信息 34 f = false; 35 } 36 } 37 if(f) { 38 // 如果没有就开户 39 User u = new User(chargeMode, sss[1]);//开户 40 users.add(u); 41 } 42 } 43 if(InputFormat.Tjudge(s)) { 44 // 判断是否为通讯信息,若是,则sss[1]为拨打号码,ss[1]为接通号码 45 CallRecord callRecord = new CallRecord(); 46 start = dateFormat.parse(ss[2]+" "+ss[3]);//开始时间 47 end = dateFormat.parse(ss[4]+" "+ss[5]);//结束时间 48 callAddress = sss[1].substring(0,4);//拨打地址 49 receiveAddress = ss[1].substring(0, 4);//接通地址 50 callRecord.setCallingNumber(sss[1]); 51 callRecord.setAnswerNumber(ss[1]); 52 callRecord.setStartTime(start); 53 callRecord.setEndTime(end); 54 callRecord.setCallingAddressAreaCode(callAddress); 55 callRecord.setAnswerAddressAreaCode(receiveAddress); 56 callRecords.add(callRecord);//添加用户记录 57 } 58 s = in.nextLine(); 59 } 60 Collections.sort(users, new Comparator<User>() { 61 // 对users中的用户根据电话号码大小进行升序排序 62 @Override 63 public int compare(User o1, User o2) { 64 double d1 = Double.parseDouble(o1.getNumber()); 65 double d2 = Double.parseDouble(o2.getNumber()); 66 if(d1 > d2) { 67 return 1; 68 } 69 return -1; 70 } 71 }); 72 for(int i=0; i<users.size(); i++) { 73 UserRecords userRecords = new UserRecords();//用户记录 74 for(int j=0; j<callRecords.size(); j++) { 75 // 将用户记录添加到对应的用户记录当中 76 if(users.get(i).getNumber().equals(callRecords.get(j).getCallingNumber())) { 77 if(callRecords.get(j).getAnswerAddressAreaCode().matches("0791")) { 78 // 市内打电话 79 userRecords.addCallingInCityRecords(callRecords.get(j)); 80 } 81 else if(callRecords.get(j).getAnswerAddressAreaCode().matches("((079\\d)|0701)")) { 82 // 省内打电话 83 userRecords.addCallingInProvinceRecords(callRecords.get(j)); 84 } 85 else { 86 // 国内打电话 87 userRecords.addCallingInLandRecords(callRecords.get(j)); 88 } 89 } 90 if(users.get(i).getNumber().equals(callRecords.get(j).getAnswerNumber())) { 91 if(callRecords.get(j).getCallingAddressAreaCode().matches("0791")) { 92 // 市内接电话 93 userRecords.addanswerInCityRecords(callRecords.get(j)); 94 } 95 else if(callRecords.get(j).getCallingAddressAreaCode().matches("((079\\d)|0701)")) { 96 // 省内接电话 97 userRecords.addanswerInProvinceRecords(callRecords.get(j)); 98 } 99 else { 100 // 国内接电话 101 userRecords.addanswerInLandRecords(callRecords.get(j)); 102 } 103 } 104 } 105 // 将用户记录存入对应用户中 106 users.get(i).setUserRecords(userRecords); 107 } 108 for(User u : users) { 109 System.out.println(u.getNumber()+" "+String.format("%.1f", u.calCost())+" "+String.format("%.1f", u.getBalance())); 110 } 111 } 112 113 } 114 abstract class CallChargeRule extends ChargeRule { 115 116 public abstract double calCost(ArrayList<CallRecord> callRecords); 117 } 118 class CallRecord extends CommunicationRecord { 119 private Date startTime;//开始时间 120 private Date endTime;//结束时间 121 private String callingAddressAreaCode;//拨打地址 122 private String answerAddressAreaCode;//接通地址 123 public Date getStartTime() { 124 return startTime; 125 } 126 public void setStartTime(Date startTime) { 127 this.startTime = startTime; 128 } 129 public Date getEndTime() { 130 return endTime; 131 } 132 public void setEndTime(Date endTime) { 133 this.endTime = endTime; 134 } 135 public String getCallingAddressAreaCode() { 136 return callingAddressAreaCode; 137 } 138 public void setCallingAddressAreaCode(String callingAddressAreaCode) { 139 this.callingAddressAreaCode = callingAddressAreaCode; 140 } 141 public String getAnswerAddressAreaCode() { 142 return answerAddressAreaCode; 143 } 144 public void setAnswerAddressAreaCode(String answerAddressAreaCode) { 145 this.answerAddressAreaCode = answerAddressAreaCode; 146 } 147 148 } 149 abstract class ChargeMode { 150 // 计费 151 private ArrayList<ChargeRule> chargeRules = new ArrayList<ChargeRule>(); 152 153 public ArrayList<ChargeRule> getChargeRules() { 154 return chargeRules; 155 } 156 157 public void setChargeRules(ArrayList<ChargeRule> chargeRules) { 158 // 计费规则 159 this.chargeRules = chargeRules; 160 } 161 162 public abstract double calCost(UserRecords userRecords); 163 public abstract double getMonthlyRent(); 164 } 165 abstract class ChargeRule { 166 167 } 168 abstract class CommunicationRecord { 169 private String callingNumber;//拨打电话 170 private String answerNumber;//接通电话 171 172 public String getCallingNumber() { 173 return callingNumber; 174 } 175 public void setCallingNumber(String callingNumber) { 176 this.callingNumber = callingNumber; 177 } 178 public String getAnswerNumber() { 179 return answerNumber; 180 } 181 public void setAnswerNumber(String answerNumber) { 182 this.answerNumber = answerNumber; 183 } 184 185 } 186 class InputFormat { 187 188 public static boolean Ujudge(String s) { 189 // 判断输入是否符合要求 190 if(s.matches("u-\\d{11,13} 0")) { 191 return true; 192 } 193 else 194 return false; 195 } 196 197 public static boolean Tjudge(String s) { 198 // 判断输入是否符合要求 199 if(s.matches("t-\\d{11,13} \\d{11,13} \\d{4}\\.([1-9]|1[0-2])\\.([1-9]|[1-2][0-9]|3[0-1]) (2[0-3]|[0-1]\\d):[0-5]\\d:[0-5]\\d \\d{4}\\.([1-9]|1[0-2])\\.([1-9]|[1-2][0-9]|3[0-1]) (2[0-3]|[0-1]\\d):[0-5]\\d:[0-5]\\d")) { 200 return true; 201 } 202 else 203 return false; 204 } 205 } 206 class LandlinePhoneCharging extends ChargeMode { 207 // 座机 208 private double monthlyRent = 20;//月租 209 @Override 210 public double calCost(UserRecords userRecords) { 211 //话费 212 double cost = 0; 213 CallChargeRule callChargeRule = new LandPhoneCityRule(); 214 cost+=callChargeRule.calCost(userRecords.getCallingInCityRecords()); 215 callChargeRule = new LandPhoneInProvinceRule(); 216 cost+=callChargeRule.calCost(userRecords.getCallingInProvinceRecords()); 217 callChargeRule = new LandPhoneInlandRule(); 218 cost+=callChargeRule.calCost(userRecords.getCallingInLandRecords()); 219 return cost; 220 } 221 222 @Override 223 public double getMonthlyRent() { 224 // 月租 225 return monthlyRent; 226 } 227 228 } 229 class LandPhoneCityRule extends CallChargeRule { 230 // 座机市内计费规则 231 @Override 232 public double calCost(ArrayList<CallRecord> callRecords) { 233 double cost = 0; 234 for(int i=0; i<callRecords.size(); i++) { 235 long time = ((callRecords.get(i).getEndTime().getTime() 236 - callRecords.get(i).getStartTime().getTime()) / 1000) / 60;//将时间化为多少分钟 237 if((((callRecords.get(i).getEndTime().getTime() 238 - callRecords.get(i).getStartTime().getTime()) / 1000) % 60) !=0) { 239 // 不足一分钟按一分钟计 240 time++; 241 } 242 cost+=time*0.1; 243 } 244 return cost; 245 } 246 247 } 248 class LandPhoneInlandRule extends CallChargeRule { 249 // 座机国内计费规则 250 @Override 251 public double calCost(ArrayList<CallRecord> callRecords) { 252 double cost = 0; 253 for(int i=0; i<callRecords.size(); i++) { 254 long time = ((callRecords.get(i).getEndTime().getTime() 255 - callRecords.get(i).getStartTime().getTime()) / 1000) / 60;//将时间化为多少分钟 256 if((((callRecords.get(i).getEndTime().getTime() 257 - callRecords.get(i).getStartTime().getTime()) / 1000) % 60) !=0) { 258 // 不足一分钟按一分钟计 259 time++; 260 } 261 cost+=time*0.6; 262 } 263 return cost; 264 } 265 266 } 267 class LandPhoneInProvinceRule extends CallChargeRule { 268 // 座机省内计费规则 269 @Override 270 public double calCost(ArrayList<CallRecord> callRecords) { 271 double cost = 0; 272 for(int i=0; i<callRecords.size(); i++) { 273 long time = ((callRecords.get(i).getEndTime().getTime() 274 - callRecords.get(i).getStartTime().getTime()) / 1000) / 60;//将时间化为多少分钟 275 if((((callRecords.get(i).getEndTime().getTime() 276 - callRecords.get(i).getStartTime().getTime()) / 1000) % 60) !=0) { 277 // 不足一分钟按一分钟计 278 time++; 279 } 280 cost+=time*0.3; 281 } 282 return cost; 283 } 284 285 } 286 class MessageRecord extends CommunicationRecord { 287 private String message;//信息 288 289 public String getMessage() { 290 return message; 291 } 292 293 public void setMessage(String message) { 294 this.message = message; 295 } 296 297 } 298 class User { 299 private UserRecords userRecords;//用户记录 300 private double balance = 100;//用户余额 301 private ChargeMode chargeMode;//计费方式 302 private String number;//号码 303 304 public double calCost() { 305 double cost = chargeMode.calCost(userRecords); 306 balance -= (cost + chargeMode.getMonthlyRent()); 307 return cost; 308 } 309 310 public UserRecords getUserRecords() { 311 return userRecords; 312 } 313 public void setUserRecords(UserRecords userRecords) { 314 this.userRecords = userRecords; 315 } 316 public ChargeMode getChargeMode() { 317 return chargeMode; 318 } 319 public void setChargeMode(ChargeMode chargeMode) { 320 this.chargeMode = chargeMode; 321 } 322 public String getNumber() { 323 return number; 324 } 325 public void setNumber(String number) { 326 this.number = number; 327 } 328 public double getBalance() { 329 return balance; 330 } 331 332 public User(ChargeMode chargeMode, String number) { 333 this.chargeMode = chargeMode; 334 this.number = number; 335 } 336 337 } 338 class UserRecords { 339 // 保存用户记录 340 ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();//市内打电话 341 ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();//省内打长途 342 ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();//国内打长途 343 ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();//市内接电话 344 ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();//省内接电话 345 ArrayList<CallRecord> answerInlandRecords = new ArrayList<CallRecord>();//国内接电话 346 ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();//发信息 347 ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();//收信息 348 public void addCallingInCityRecords (CallRecord callRecord) { 349 // 添加市内打电话记录 350 callingInCityRecords.add(callRecord); 351 } 352 public void addCallingInProvinceRecords (CallRecord callRecord) { 353 // 添加省内打电话记录 354 callingInProvinceRecords.add(callRecord); 355 } 356 public void addCallingInLandRecords (CallRecord callRecord) { 357 // 添加国内打电话记录 358 callingInLandRecords.add(callRecord); 359 } 360 public void addanswerInCityRecords (CallRecord answerRecord) { 361 // 添加市内接电话记录 362 answerInCityRecords.add(answerRecord); 363 } 364 public void addanswerInProvinceRecords (CallRecord answerRecord) { 365 // 添加市内接电话记录 366 answerInProvinceRecords.add(answerRecord); 367 } 368 public void addanswerInLandRecords (CallRecord answerRecord) { 369 // 添加市内接电话记录 370 answerInlandRecords.add(answerRecord); 371 } 372 public void addSendMessageRecords (MessageRecord sendMessageRecord) { 373 // 添加发消息记录 374 sendMessageRecords.add(sendMessageRecord); 375 } 376 public void addreceiveMessageRecords (MessageRecord receiveMessageRecord) { 377 // 添加收消息记录 378 receiveMessageRecords.add(receiveMessageRecord); 379 } 380 // get方法 381 public ArrayList<CallRecord> getCallingInCityRecords() { 382 return callingInCityRecords; 383 } 384 public ArrayList<CallRecord> getCallingInProvinceRecords() { 385 return callingInProvinceRecords; 386 } 387 public ArrayList<CallRecord> getCallingInLandRecords() { 388 return callingInLandRecords; 389 } 390 public ArrayList<CallRecord> getAnswerInCityRecords() { 391 return answerInCityRecords; 392 } 393 public ArrayList<CallRecord> getAnswerInProvinceRecords() { 394 return answerInProvinceRecords; 395 } 396 public ArrayList<CallRecord> getAnswerInlandRecords() { 397 return answerInlandRecords; 398 } 399 public ArrayList<MessageRecord> getSendMessageRecords() { 400 return sendMessageRecords; 401 } 402 public ArrayList<MessageRecord> getReceiveMessageRecords() { 403 return receiveMessageRecords; 404 } 405 406 }View Code
2.第七次题目集
1.设计
设计与第六次相同,不同的是使用了两个User容器分别来存储手机用户和座机用户,只是稍作修改将每种正确的输入的正则表达式做成一个方法放入InputFormat类中,在主函数中调用不同的方法判断进入不同的主体,再加一个类来处理输入信息根据输入的哪种信息来返回User或者UserRecord,保存用户对应的记录也做成方法。
2.类图
3.圈复杂度
4.码源
1 import java.text.ParseException; 2 import java.text.SimpleDateFormat; 3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.Comparator; 6 import java.util.Date; 7 import java.util.Scanner; 8 9 public class Main { 10 11 public static void main(String[] args) throws ParseException { 12 Scanner in = new Scanner(System.in); 13 ArrayList<User> users1 = new ArrayList<User>(); 14 ArrayList<User> users2 = new ArrayList<User>(); 15 ArrayList<CallRecord> callRecords = new ArrayList<CallRecord>(); 16 String s = in.nextLine(); 17 while(!s.equals("end")) { 18 if(InputFormat.LandUjudge(s)) { 19 // 判断是否为座机开户信息 20 User us = InputData.getUser(s); 21 boolean f = true; 22 for(User user : users1) { 23 if(user.getNumber().equals(us.getNumber())) { 24 // 遍历判断是否已经有此开户信息 25 f = false; 26 } 27 } 28 if(f) { 29 users1.add(us); 30 } 31 } 32 if(InputFormat.PhoneUjudge(s)) { 33 // 判断是否为电话开户信息 34 User us = InputData.getUser(s); 35 boolean f = true; 36 for(User user : users2) { 37 if(user.getNumber().equals(us.getNumber())) { 38 // 遍历判断是否已经有此开户信息 39 f = false; 40 } 41 } 42 if(f) { 43 users2.add(us); 44 } 45 } 46 if(InputFormat.LandTjudge(s) || InputFormat.LandPhoneTjudge(s) || InputFormat.PhoneTjudge(s) || InputFormat.PhoneLandTjudge(s)) { 47 CallRecord callRecord = InputData.getCallRecord(s); 48 callRecords.add(callRecord); 49 } 50 s = in.nextLine(); 51 } 52 Collections.sort(users1, new Comparator<User>() { 53 // 对users1座机中的用户根据电话号码大小进行升序排序 54 @Override 55 public int compare(User o1, User o2) { 56 double d1 = Double.parseDouble(o1.getNumber()); 57 double d2 = Double.parseDouble(o2.getNumber()); 58 if(d1 > d2) { 59 return 1; 60 } 61 return -1; 62 } 63 }); 64 Collections.sort(users2, new Comparator<User>() { 65 // 对users1座机中的用户根据电话号码大小进行升序排序 66 @Override 67 public int compare(User o1, User o2) { 68 double d1 = Double.parseDouble(o1.getNumber()); 69 double d2 = Double.parseDouble(o2.getNumber()); 70 if(d1 > d2) { 71 return 1; 72 } 73 return -1; 74 } 75 }); 76 InputData.saveLandUserRecords(users1, callRecords); 77 InputData.saveUsePhonerRecords(users2, callRecords); 78 for(User u : users1) { 79 System.out.println(u.getNumber()+" "+String.format("%.1f", u.calCost())+" "+String.format("%.1f", u.getBalance())); 80 } 81 for(User u : users2) { 82 System.out.println(u.getNumber()+" "+String.format("%.1f", u.calCost())+" "+String.format("%.1f", u.getBalance())); 83 } 84 } 85 86 } 87 abstract class CallChargeRule extends ChargeRule { 88 89 public abstract double calCost(ArrayList<CallRecord> callRecords); 90 } 91 class CallRecord extends CommunicationRecord { 92 private Date startTime;//开始时间 93 private Date endTime;//结束时间 94 private String callingAddressAreaCode;//拨打地址 95 private String answerAddressAreaCode;//接通地址 96 public Date getStartTime() { 97 return startTime; 98 } 99 public void setStartTime(Date startTime) { 100 this.startTime = startTime; 101 } 102 public Date getEndTime() { 103 return endTime; 104 } 105 public void setEndTime(Date endTime) { 106 this.endTime = endTime; 107 } 108 public String getCallingAddressAreaCode() { 109 return callingAddressAreaCode; 110 } 111 public void setCallingAddressAreaCode(String callingAddressAreaCode) { 112 this.callingAddressAreaCode = callingAddressAreaCode; 113 } 114 public String getAnswerAddressAreaCode() { 115 return answerAddressAreaCode; 116 } 117 public void setAnswerAddressAreaCode(String answerAddressAreaCode) { 118 this.answerAddressAreaCode = answerAddressAreaCode; 119 } 120 121 } 122 abstract class ChargeMode { 123 // 计费 124 private ArrayList<ChargeRule> chargeRules = new ArrayList<ChargeRule>(); 125 126 public ArrayList<ChargeRule> getChargeRules() { 127 return chargeRules; 128 } 129 130 public void setChargeRules(ArrayList<ChargeRule> chargeRules) { 131 // 计费规则 132 this.chargeRules = chargeRules; 133 } 134 135 public abstract double calCost(UserRecords userRecords); 136 public abstract double getMonthlyRent(); 137 } 138 abstract class ChargeRule { 139 140 } 141 abstract class CommunicationRecord { 142 private String callingNumber;//拨打电话 143 private String answerNumber;//接通电话 144 145 public String getCallingNumber() { 146 return callingNumber; 147 } 148 public void setCallingNumber(String callingNumber) { 149 this.callingNumber = callingNumber; 150 } 151 public String getAnswerNumber() { 152 return answerNumber; 153 } 154 public void setAnswerNumber(String answerNumber) { 155 this.answerNumber = answerNumber; 156 } 157 158 } 159 class InputData { 160 /* 座机 */ 161 public static void saveLandUserRecords(ArrayList<User> users, ArrayList<CallRecord> callRecords) { 162 for(int i=0; i<users.size(); i++) { 163 UserRecords userRecords = new UserRecords();//用户记录 164 for(int j=0; j<callRecords.size(); j++) { 165 // 将用户记录添加到对应的用户记录当中 166 if(users.get(i).getNumber().equals(callRecords.get(j).getCallingNumber())) { 167 if(callRecords.get(j).getAnswerAddressAreaCode().matches("0791")) { 168 // 打市内电话 169 userRecords.addCallingInCityRecords(callRecords.get(j)); 170 } 171 else if(callRecords.get(j).getAnswerAddressAreaCode().matches("((079\\d)|0701)")) { 172 // 打省内电话 173 userRecords.addCallingInProvinceRecords(callRecords.get(j)); 174 } 175 else { 176 // 打国内电话 177 userRecords.addCallingInLandRecords(callRecords.get(j)); 178 } 179 } 180 if(users.get(i).getNumber().equals(callRecords.get(j).getAnswerNumber())) { 181 if(callRecords.get(j).getCallingAddressAreaCode().matches("0791")) { 182 // 市内接电话 183 userRecords.addanswerInCityRecords(callRecords.get(j)); 184 } 185 else if(callRecords.get(j).getCallingAddressAreaCode().matches("((079\\d)|0701)")) { 186 // 省内接电话 187 userRecords.addanswerInProvinceRecords(callRecords.get(j)); 188 } 189 else { 190 // 国内接电话 191 userRecords.addanswerInLandRecords(callRecords.get(j)); 192 } 193 } 194 } 195 // 将用户记录存入对应用户中 196 users.get(i).setUserRecords(userRecords); 197 } 198 } 199 200 /* 手机 */ 201 public static void saveUsePhonerRecords(ArrayList<User> users, ArrayList<CallRecord> callRecords) { 202 for(int i=0; i<users.size(); i++) { 203 UserRecords userRecords = new UserRecords();//用户记录 204 for(int j=0; j<callRecords.size(); j++) { 205 if(users.get(i).getNumber().equals(callRecords.get(j).getCallingNumber())) { 206 if(callRecords.get(j).getCallingAddressAreaCode().equals("0791")) { 207 if(callRecords.get(j).getAnswerAddressAreaCode().matches("0791")) { 208 // 市内打市内电话 209 userRecords.addCallingInCityRecords(callRecords.get(j)); 210 } 211 else if(callRecords.get(j).getAnswerAddressAreaCode().matches("((079\\d)|0701)")) { 212 // 市内打省内电话 213 userRecords.addCallingInProvinceRecords(callRecords.get(j)); 214 } 215 else { 216 // 市内打国内电话 217 userRecords.addCallingInLandRecords(callRecords.get(j)); 218 } 219 } 220 else if(callRecords.get(j).getCallingAddressAreaCode().matches("((079\\d)|0701)")) { 221 userRecords.addPcallingRecords(callRecords.get(j)); 222 } 223 else { 224 userRecords.addLcallingRecords(callRecords.get(j)); 225 } 226 } 227 if(users.get(i).getNumber().equals(callRecords.get(j).getAnswerNumber())) { 228 if(callRecords.get(j).getAnswerAddressAreaCode().matches("0791")) { 229 // 市内接电话 230 userRecords.addanswerInCityRecords(callRecords.get(j)); 231 } 232 else if(callRecords.get(j).getAnswerAddressAreaCode().matches("((079\\d)|0701)")) { 233 // 省内接电话 234 userRecords.addanswerInProvinceRecords(callRecords.get(j)); 235 } 236 else { 237 // 国内接电话 238 userRecords.addanswerInLandRecords(callRecords.get(j)); 239 } 240 } 241 } 242 // 将用户记录存入对应用户中 243 users.get(i).setUserRecords(userRecords); 244 } 245 } 246 247 public static User getUser(String s) { 248 User user = new User(); 249 if(InputFormat.Alljudge(s)) { 250 String[] ss = s.split(" "); 251 String[] sss = ss[0].split("-"); 252 if(InputFormat.LandUjudge(s)) { 253 LandlinePhoneCharging l = new LandlinePhoneCharging(); 254 user.setChargeMode(l);user.setNumber(sss[1]);//sss[1]为座机号码 255 } 256 if(InputFormat.PhoneUjudge(s)) { 257 if(ss[1].matches("1")) { 258 Phone p= new Phone(); 259 user.setChargeMode(p);user.setNumber(sss[1]); 260 } 261 if(ss[1].matches("2")) { 262 PhoneA p= new PhoneA(); 263 user.setChargeMode(p);user.setNumber(sss[1]); 264 } 265 } 266 } 267 return user; 268 } 269 public static CallRecord getCallRecord(String s) throws ParseException { 270 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); 271 CallRecord callRecord = new CallRecord(); 272 Date start; 273 Date end; 274 if(InputFormat.Alljudge(s)) { 275 String[] ss = s.split(" "); 276 String[] sss = ss[0].split("-"); 277 if(InputFormat.LandPhoneTjudge(s)) { 278 start = dateFormat.parse(ss[3]+" "+ss[4]); 279 end = dateFormat.parse(ss[5]+" "+ss[6]); 280 callRecord.setCallingNumber(sss[1]);callRecord.setCallingAddressAreaCode(sss[1].substring(0, 4)); 281 callRecord.setAnswerNumber(ss[1]);callRecord.setAnswerAddressAreaCode(ss[2]); 282 callRecord.setStartTime(start);callRecord.setEndTime(end); 283 } 284 if(InputFormat.LandTjudge(s)) { 285 start = dateFormat.parse(ss[2]+" "+ss[3]); 286 end = dateFormat.parse(ss[4]+" "+ss[5]); 287 callRecord.setCallingNumber(sss[1]);callRecord.setCallingAddressAreaCode(sss[1].substring(0, 4)); 288 callRecord.setAnswerNumber(ss[1]);callRecord.setAnswerAddressAreaCode(ss[1].substring(0, 4)); 289 callRecord.setStartTime(start);callRecord.setEndTime(end); 290 } 291 if(InputFormat.PhoneLandTjudge(s)) { 292 start = dateFormat.parse(ss[3]+" "+ss[4]); 293 end = dateFormat.parse(ss[5]+" "+ss[6]); 294 callRecord.setCallingNumber(sss[1]);callRecord.setCallingAddressAreaCode(ss[1]); 295 callRecord.setAnswerNumber(ss[2]);callRecord.setAnswerAddressAreaCode(ss[2].substring(0, 4)); 296 callRecord.setStartTime(start);callRecord.setEndTime(end); 297 } 298 if(InputFormat.PhoneTjudge(s)) { 299 start = dateFormat.parse(ss[4]+" "+ss[5]); 300 end = dateFormat.parse(ss[6]+" "+ss[7]); 301 callRecord.setCallingNumber(sss[1]);callRecord.setCallingAddressAreaCode(ss[1]); 302 callRecord.setAnswerNumber(ss[2]);callRecord.setAnswerAddressAreaCode(ss[3]); 303 callRecord.setStartTime(start);callRecord.setEndTime(end); 304 } 305 } 306 return callRecord; 307 } 308 309 } 310 class InputFormat { 311 312 public static boolean LandUjudge(String s) { 313 // 判断座机开户输入是否符合要求 314 if(s.matches("u-\\d{10,13} 0")) { 315 return true; 316 } 317 else 318 return false; 319 } 320 321 public static boolean LandTjudge(String s) { 322 // 座机打座机 323 // 判断通话记录输入是否符合要求 324 if(s.matches("t-\\d{10,13} \\d{10,13} \\d{4}\\.([1-9]|1[0-2])\\.([1-9]|[1-2][0-9]|3[0-1]) (2[0-3]|[0-1]\\d):[0-5]\\d:[0-5]\\d \\d{4}\\.([1-9]|1[0-2])\\.([1-9]|[1-2][0-9]|3[0-1]) (2[0-3]|[0-1]\\d):[0-5]\\d:[0-5]\\d")) { 325 return true; 326 } 327 else 328 return false; 329 } 330 public static boolean PhoneUjudge(String s) { 331 // 电话开户是否符合输入要求 332 if(s.matches("u-1\\d{10} [1-2]")) { 333 return true; 334 } 335 else 336 return false; 337 } 338 339 public static boolean LandPhoneTjudge(String s) { 340 // 座机打手机 341 // 判断通话记录输入是否符合要求 342 if(s.matches("t-\\d{10,13} 1\\d{10} \\d{3,5} \\d{4}\\.([1-9]|1[0-2])\\.([1-9]|[1-2][0-9]|3[0-1]) (2[0-3]|[0-1]\\d):[0-5]\\d:[0-5]\\d \\d{4}\\.([1-9]|1[0-2])\\.([1-9]|[1-2][0-9]|3[0-1]) (2[0-3]|[0-1]\\d):[0-5]\\d:[0-5]\\d")) { 343 return true; 344 } 345 else 346 return false; 347 } 348 349 public static boolean PhoneTjudge(String s) { 350 // 手机打手机 351 // 判断通话记录输入是否符合要求 352 if(s.matches("t-1\\d{10} \\d{3,5} 1\\d{10} \\d{3,5} \\d{4}\\.([1-9]|1[0-2])\\.([1-9]|[1-2][0-9]|3[0-1]) (2[0-3]|[0-1]\\d):[0-5]\\d:[0-5]\\d \\d{4}\\.([1-9]|1[0-2])\\.([1-9]|[1-2][0-9]|3[0-1]) (2[0-3]|[0-1]\\d):[0-5]\\d:[0-5]\\d")) { 353 return true; 354 } 355 else 356 return false; 357 } 358 public static boolean PhoneLandTjudge(String s) { 359 // 手机打座机 360 // 判断通话记录输入是否符合要求 361 if(s.matches("t-1\\d{10} \\d{3,5} \\d{10,13} \\d{4}\\.([1-9]|1[0-2])\\.([1-9]|[1-2][0-9]|3[0-1]) (2[0-3]|[0-1]\\d):[0-5]\\d:[0-5]\\d \\d{4}\\.([1-9]|1[0-2])\\.([1-9]|[1-2][0-9]|3[0-1]) (2[0-3]|[0-1]\\d):[0-5]\\d:[0-5]\\d")) { 362 return true; 363 } 364 else 365 return false; 366 } 367 public static boolean Alljudge(String s) { 368 if(LandUjudge(s) || LandTjudge(s) || PhoneUjudge(s) || LandPhoneTjudge(s) || PhoneTjudge(s) || PhoneLandTjudge(s)) 369 return true; 370 else 371 return false; 372 } 373 } 374 class LandlinePhoneCharging extends ChargeMode { 375 // 座机 376 private double monthlyRent = 20;//月租 377 @Override 378 public double calCost(UserRecords userRecords) { 379 //话费 380 double cost = 0; 381 CallChargeRule callChargeRule = new LandPhoneCityRule(); 382 cost+=callChargeRule.calCost(userRecords.getCallingInCityRecords()); 383 callChargeRule = new LandPhoneInProvinceRule(); 384 cost+=callChargeRule.calCost(userRecords.getCallingInProvinceRecords()); 385 callChargeRule = new LandPhoneInlandRule(); 386 cost+=callChargeRule.calCost(userRecords.getCallingInLandRecords()); 387 return cost; 388 } 389 390 @Override 391 public double getMonthlyRent() { 392 // 月租 393 return monthlyRent; 394 } 395 396 } 397 class LandPhoneCityRule extends CallChargeRule { 398 // 座机市内计费规则 399 @Override 400 public double calCost(ArrayList<CallRecord> callRecords) { 401 double cost = 0; 402 for(int i=0; i<callRecords.size(); i++) { 403 long time = ((callRecords.get(i).getEndTime().getTime() 404 - callRecords.get(i).getStartTime().getTime()) / 1000) / 60;//将时间化为多少分钟 405 if((((callRecords.get(i).getEndTime().getTime() 406 - callRecords.get(i).getStartTime().getTime()) / 1000) % 60) !=0) { 407 // 不足一分钟按一分钟计 408 time++; 409 } 410 cost+=time*0.1; 411 } 412 return cost; 413 } 414 415 } 416 class LandPhoneInlandRule extends CallChargeRule { 417 // 座机国内计费规则 418 @Override 419 public double calCost(ArrayList<CallRecord> callRecords) { 420 double cost = 0; 421 for(int i=0; i<callRecords.size(); i++) { 422 long time = ((callRecords.get(i).getEndTime().getTime() 423 - callRecords.get(i).getStartTime().getTime()) / 1000) / 60;//将时间化为多少分钟 424 if((((callRecords.get(i).getEndTime().getTime() 425 - callRecords.get(i).getStartTime().getTime()) / 1000) % 60) !=0) { 426 // 不足一分钟按一分钟计 427 time++; 428 } 429 cost+=time*0.6; 430 } 431 return cost; 432 } 433 434 } 435 class LandPhoneInProvinceRule extends CallChargeRule { 436 // 座机省内计费规则 437 @Override 438 public double calCost(ArrayList<CallRecord> callRecords) { 439 double cost = 0; 440 for(int i=0; i<callRecords.size(); i++) { 441 long time = ((callRecords.get(i).getEndTime().getTime() 442 - callRecords.get(i).getStartTime().getTime()) / 1000) / 60;//将时间化为多少分钟 443 if((((callRecords.get(i).getEndTime().getTime() 444 - callRecords.get(i).getStartTime().getTime()) / 1000) % 60) !=0) { 445 // 不足一分钟按一分钟计 446 time++; 447 } 448 cost+=time*0.3; 449 } 450 return cost; 451 } 452 453 } 454 abstract class MessageChargeRule extends ChargeRule { 455 456 public abstract double calCost (ArrayList<MessageRecord> messageRecords); 457 } 458 class MessageRecord extends CommunicationRecord { 459 private String message;//信息 460 461 public String getMessage() { 462 return message; 463 } 464 465 public void setMessage(String message) { 466 this.message = message; 467 } 468 469 } 470 class Phone extends ChargeMode { 471 472 double MonthlyRent = 15; 473 @Override 474 public double calCost(UserRecords userRecords) { 475 double cost = 0; 476 CallChargeRule callChargeRule = new PhoneinCityRule(); 477 cost+=callChargeRule.calCost(userRecords.getCallingInCityRecords()); 478 callChargeRule = new PhoneInProvinceRule(); 479 cost+=callChargeRule.calCost(userRecords.getCallingInProvinceRecords()); 480 callChargeRule = new PhoneInlandRule(); 481 cost+=callChargeRule.calCost(userRecords.getCallingInLandRecords()); 482 callChargeRule = new PhoneCallinProvinceRule(); 483 cost+=callChargeRule.calCost(userRecords.getPcallingRecords()); 484 callChargeRule = new PhoneCallinLandRule(); 485 cost+=callChargeRule.calCost(userRecords.getLcallingRecords()); 486 callChargeRule = new PhoneAnswerinLandRule(); 487 cost+=callChargeRule.calCost(userRecords.getAnswerInlandRecords()); 488 return cost; 489 } 490 491 @Override 492 public double getMonthlyRent() { 493 return MonthlyRent; 494 } 495 496 } 497 class PhoneA extends ChargeMode{ 498 499 @Override 500 public double calCost(UserRecords userRecords) { 501 // TODO 自动生成的方法存根 502 return 0; 503 } 504 505 @Override 506 public double getMonthlyRent() { 507 // TODO 自动生成的方法存根 508 return 0; 509 } 510 511 } 512 class PhoneAnswerinLandRule extends CallChargeRule{ 513 // 手机在省内接电话 514 public double calCost(ArrayList<CallRecord> callRecords) { 515 double cost = 0; 516 for(int i=0; i<callRecords.size(); i++) { 517 long time = ((callRecords.get(i).getEndTime().getTime() 518 - callRecords.get(i).getStartTime().getTime()) / 1000) / 60;//将时间化为多少分钟 519 if((((callRecords.get(i).getEndTime().getTime() 520 - callRecords.get(i).getStartTime().getTime()) / 1000) % 60) !=0) { 521 // 不足一分钟按一分钟计 522 time++; 523 } 524 cost+=time*0.3; 525 } 526 return cost; 527 } 528 } 529 class PhoneCallinLandRule extends CallChargeRule{ 530 // 手机在省外打电话 531 @Override 532 public double calCost(ArrayList<CallRecord> callRecords) { 533 double cost = 0; 534 for(int i=0; i<callRecords.size(); i++) { 535 long time = ((callRecords.get(i).getEndTime().getTime() 536 - callRecords.get(i).getStartTime().getTime()) / 1000) / 60;//将时间化为多少分钟 537 if((((callRecords.get(i).getEndTime().getTime() 538 - callRecords.get(i).getStartTime().getTime()) / 1000) % 60) !=0) { 539 // 不足一分钟按一分钟计 540 time++; 541 } 542 cost+=time*0.6; 543 } 544 return cost; 545 } 546 547 } 548 class PhoneCallinProvinceRule extends CallChargeRule{ 549 // 手机在省内打电话 550 @Override 551 public double calCost(ArrayList<CallRecord> callRecords) { 552 double cost = 0; 553 for(int i=0; i<callRecords.size(); i++) { 554 long time = ((callRecords.get(i).getEndTime().getTime() 555 - callRecords.get(i).getStartTime().getTime()) / 1000) / 60;//将时间化为多少分钟 556 if((((callRecords.get(i).getEndTime().getTime() 557 - callRecords.get(i).getStartTime().getTime()) / 1000) % 60) !=0) { 558 // 不足一分钟按一分钟计 559 time++; 560 } 561 cost+=time*0.3; 562 } 563 return cost; 564 } 565 566 } 567 class PhoneinCityRule extends CallChargeRule { 568 // 手机在市内打市内电话 569 @Override 570 public double calCost(ArrayList<CallRecord> callRecords) { 571 double cost = 0; 572 for(int i=0; i<callRecords.size(); i++) { 573 long time = ((callRecords.get(i).getEndTime().getTime() 574 - callRecords.get(i).getStartTime().getTime()) / 1000) / 60;//将时间化为多少分钟 575 if((((callRecords.get(i).getEndTime().getTime() 576 - callRecords.get(i).getStartTime().getTime()) / 1000) % 60) !=0) { 577 // 不足一分钟按一分钟计 578 time++; 579 } 580 cost+=time*0.1; 581 } 582 return cost; 583 } 584 585 } 586 class PhoneInlandRule extends CallChargeRule { 587 // 手机在市内打省外电话 588 @Override 589 public double calCost(ArrayList<CallRecord> callRecords) { 590 double cost = 0; 591 for(int i=0; i<callRecords.size(); i++) { 592 long time = ((callRecords.get(i).getEndTime().getTime() 593 - callRecords.get(i).getStartTime().getTime()) / 1000) / 60;//将时间化为多少分钟 594 if((((callRecords.get(i).getEndTime().getTime() 595 - callRecords.get(i).getStartTime().getTime()) / 1000) % 60) !=0) { 596 // 不足一分钟按一分钟计 597 time++; 598 } 599 cost+=time*0.3; 600 } 601 return cost; 602 } 603 604 } 605 class PhoneInProvinceRule extends CallChargeRule { 606 // 手机在市内打省内电话 607 @Override 608 public double calCost(ArrayList<CallRecord> callRecords) { 609 double cost = 0; 610 for(int i=0; i<callRecords.size(); i++) { 611 long time = ((callRecords.get(i).getEndTime().getTime() 612 - callRecords.get(i).getStartTime().getTime()) / 1000) / 60;//将时间化为多少分钟 613 if((((callRecords.get(i).getEndTime().getTime() 614 - callRecords.get(i).getStartTime().getTime()) / 1000) % 60) !=0) { 615 // 不足一分钟按一分钟计 616 time++; 617 } 618 cost+=time*0.2; 619 } 620 return cost; 621 } 622 } 623 class SendMessageRule extends MessageChargeRule { 624 625 @Override 626 public double calCost(ArrayList<MessageRecord> messageRecords) { 627 return 0; 628 } 629 630 } 631 class User { 632 private UserRecords userRecords;//用户记录 633 private double balance = 100;//用户余额 634 private ChargeMode chargeMode;//计费方式 635 private String number;//号码 636 637 public double calCost() { 638 double cost = chargeMode.calCost(userRecords); 639 balance -= (cost + chargeMode.getMonthlyRent()); 640 return cost; 641 } 642 643 public UserRecords getUserRecords() { 644 return userRecords; 645 } 646 public void setUserRecords(UserRecords userRecords) { 647 this.userRecords = userRecords; 648 } 649 public ChargeMode getChargeMode() { 650 return chargeMode; 651 } 652 public void setChargeMode(ChargeMode chargeMode) { 653 this.chargeMode = chargeMode; 654 } 655 public String getNumber() { 656 return number; 657 } 658 public void setNumber(String number) { 659 this.number = number; 660 } 661 public double getBalance() { 662 return balance; 663 } 664 665 } 666 class UserRecords { 667 // 保存用户记录 668 ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();//打市内电话 669 ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();//打省内长途 670 ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();//打国内长途 671 ArrayList<CallRecord> PcallingRecords = new ArrayList<CallRecord>();//省内打电话 672 ArrayList<CallRecord> LcallingRecords = new ArrayList<CallRecord>();//国内打电话 673 ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();//市内接电话 674 ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();//省内接电话 675 ArrayList<CallRecord> answerInlandRecords = new ArrayList<CallRecord>();//国内接电话 676 ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();//发信息 677 ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();//收信息 678 public void addCallingInCityRecords (CallRecord callRecord) { 679 // 添加市内打电话记录 680 callingInCityRecords.add(callRecord); 681 } 682 public void addCallingInProvinceRecords (CallRecord callRecord) { 683 // 添加省内打电话记录 684 callingInProvinceRecords.add(callRecord); 685 } 686 public void addCallingInLandRecords (CallRecord callRecord) { 687 // 添加国内打电话记录 688 callingInLandRecords.add(callRecord); 689 } 690 public void addanswerInCityRecords (CallRecord answerRecord) { 691 // 添加市内接电话记录 692 answerInCityRecords.add(answerRecord); 693 } 694 public void addanswerInProvinceRecords (CallRecord answerRecord) { 695 // 添加市内接电话记录 696 answerInProvinceRecords.add(answerRecord); 697 } 698 public void addanswerInLandRecords (CallRecord answerRecord) { 699 // 添加市内接电话记录 700 answerInlandRecords.add(answerRecord); 701 } 702 public void addSendMessageRecords (MessageRecord sendMessageRecord) { 703 // 添加发消息记录 704 sendMessageRecords.add(sendMessageRecord); 705 } 706 public void addreceiveMessageRecords (MessageRecord receiveMessageRecord) { 707 // 添加收消息记录 708 receiveMessageRecords.add(receiveMessageRecord); 709 } 710 public void addPcallingRecords (CallRecord callRecord) { 711 PcallingRecords.add(callRecord); 712 } 713 public void addLcallingRecords (CallRecord callRecord) { 714 LcallingRecords.add(callRecord); 715 } 716 // get方法 717 public ArrayList<CallRecord> getCallingInCityRecords() { 718 return callingInCityRecords; 719 } 720 public ArrayList<CallRecord> getCallingInProvinceRecords() { 721 return callingInProvinceRecords; 722 } 723 public ArrayList<CallRecord> getCallingInLandRecords() { 724 return callingInLandRecords; 725 } 726 public ArrayList<CallRecord> getAnswerInCityRecords() { 727 return answerInCityRecords; 728 } 729 public ArrayList<CallRecord> getAnswerInProvinceRecords() { 730 return answerInProvinceRecords; 731 } 732 public ArrayList<CallRecord> getAnswerInlandRecords() { 733 return answerInlandRecords; 734 } 735 public ArrayList<MessageRecord> getSendMessageRecords() { 736 return sendMessageRecords; 737 } 738 public ArrayList<MessageRecord> getReceiveMessageRecords() { 739 return receiveMessageRecords; 740 } 741 public ArrayList<CallRecord> getPcallingRecords() { 742 return PcallingRecords; 743 } 744 public ArrayList<CallRecord> getLcallingRecords() { 745 return LcallingRecords; 746 } 747 748 }View Code
3.第八次题目集
1.设计
由于老师此次只要求短信计费,所以这次只用了其中几个类,因为收短信不计费,所以没做存储收信息记录的方法,这次主要难在提取出短信内容,我用的方法是String[] ssss = s.split("m-1\\d{10} 1\\d{10} ");然后ssss[1]就是信息内容,其他没有太大变化
2.类图
3.圈复杂度
4.源码
1 import java.text.ParseException; 2 import java.util.ArrayList; 3 import java.util.Collections; 4 import java.util.Comparator; 5 import java.util.Scanner; 6 7 public class Main { 8 9 public static void main(String[] args) throws ParseException { 10 Scanner in = new Scanner(System.in); 11 ArrayList<User> users1 = new ArrayList<User>(); 12 ArrayList<MessageRecord> messageRecords = new ArrayList<MessageRecord>(); 13 String s = in.nextLine(); 14 while(!s.equals("end")) { 15 if(InputFormat.UFormat(s)) { 16 // 判断是否为手机开户信息 17 User us = InputData.getUser(s); 18 boolean f = true; 19 for(User user : users1) { 20 if(user.getNumber().equals(us.getNumber())) { 21 // 遍历判断是否已经有此开户信息 22 f = false; 23 } 24 } 25 if(f) { 26 users1.add(us); 27 } 28 } 29 if(InputFormat.MFormat(s)) { 30 // 判断是否为电话开户信息 31 MessageRecord m = InputData.getMessageRecord(s); 32 messageRecords.add(m); 33 } 34 s = in.nextLine(); 35 } 36 Collections.sort(users1, new Comparator<User>() { 37 // 对users1座机中的用户根据电话号码大小进行升序排序 38 @Override 39 public int compare(User o1, User o2) { 40 double d1 = Double.parseDouble(o1.getNumber()); 41 double d2 = Double.parseDouble(o2.getNumber()); 42 if(d1 > d2) { 43 return 1; 44 } 45 return -1; 46 } 47 }); 48 InputData.saveUserRecords(users1, messageRecords); 49 for(User u : users1) { 50 System.out.println(u.getNumber()+" "+String.format("%.1f", u.calCost())+" "+String.format("%.1f", u.getBalance())); 51 } 52 } 53 54 } 55 abstract class ChargeMode { 56 // 计费 57 private ArrayList<ChargeRule> chargeRules = new ArrayList<ChargeRule>(); 58 59 public ArrayList<ChargeRule> getChargeRules() { 60 return chargeRules; 61 } 62 63 public void setChargeRules(ArrayList<ChargeRule> chargeRules) { 64 // 计费规则 65 this.chargeRules = chargeRules; 66 } 67 68 public abstract double calCost(UserRecords userRecords); 69 public abstract double getMonthlyRent(); 70 } 71 abstract class ChargeRule { 72 73 } 74 abstract class CommunicationRecord { 75 private String callingNumber;//拨打电话 76 private String answerNumber;//接通电话 77 78 public String getCallingNumber() { 79 return callingNumber; 80 } 81 public void setCallingNumber(String callingNumber) { 82 this.callingNumber = callingNumber; 83 } 84 public String getAnswerNumber() { 85 return answerNumber; 86 } 87 public void setAnswerNumber(String answerNumber) { 88 this.answerNumber = answerNumber; 89 } 90 91 } 92 class InputData { 93 94 public static User getUser(String s) { 95 User u = new User(); 96 String[] ss = s.split(" "); 97 String[] sss = ss[0].split("-"); 98 Message m = new Message(); 99 u.setNumber(sss[1]);u.setChargeMode(m); 100 return u; 101 } 102 public static MessageRecord getMessageRecord(String s) { 103 MessageRecord m = new MessageRecord(); 104 String[] ss = s.split(" "); 105 String[] sss = ss[0].split("-"); 106 String[] ssss = s.split("m-1\\d{10} 1\\d{10} "); 107 m.setCallingNumber(sss[1]);m.setAnswerNumber(ss[1]); 108 m.setMessage(ssss[1]); 109 return m; 110 } 111 public static void saveUserRecords(ArrayList<User> users, ArrayList<MessageRecord> messageRecords) { 112 for(int i=0; i<users.size(); i++) { 113 UserRecords userRecords = new UserRecords();//用户记录 114 for(int j=0; j<messageRecords.size(); j++) { 115 // 将用户记录添加到对应的用户记录当中 116 if(users.get(i).getNumber().equals(messageRecords.get(j).getCallingNumber())) { 117 userRecords.addSendMessageRecords(messageRecords.get(j)); 118 } 119 } 120 // 将用户记录存入对应用户中 121 users.get(i).setUserRecords(userRecords); 122 } 123 } 124 } 125 class InputFormat { 126 127 public static boolean UFormat(String s) { 128 if(s.matches("u-1\\d{10} 3")) 129 return true; 130 else 131 return false; 132 } 133 public static boolean MFormat(String s) { 134 if(s.matches("m-1\\d{10} 1\\d{10} (\\w|\\d|\\.|\\s|,)+")) 135 return true; 136 else 137 return false; 138 } 139 } 140 class Message extends ChargeMode { 141 142 @Override 143 public double calCost(UserRecords userRecords) { 144 double cost = 0; 145 SendMessageChargeRule sendMessageChargeRule = new SendMessageChargeRule(); 146 cost += sendMessageChargeRule.calCost(userRecords.getSendMessageRecords()); 147 return cost; 148 } 149 150 @Override 151 public double getMonthlyRent() { 152 return 0; 153 } 154 155 } 156 abstract class MessageChargeRule extends ChargeRule { 157 158 public abstract double calCost (ArrayList<MessageRecord> messageRecords); 159 } 160 class MessageRecord extends CommunicationRecord { 161 private String message;//信息 162 163 public String getMessage() { 164 return message; 165 } 166 167 public void setMessage(String message) { 168 this.message = message; 169 } 170 171 } 172 class SendMessageChargeRule extends MessageChargeRule { 173 174 @Override 175 public double calCost(ArrayList<MessageRecord> messageRecords) { 176 double cost = 0; 177 int n = 0; 178 for(int i=0; i<messageRecords.size(); i++) { 179 n+=messageRecords.get(i).getMessage().length() / 10; 180 if(messageRecords.get(i).getMessage().length() % 10 != 0) { 181 n++; 182 } 183 } 184 if(n<=3) { 185 cost = n*0.1; 186 } 187 else if(n<=5) { 188 cost = 0.3+(n-3)*0.2; 189 } 190 else if(n>5) { 191 cost = 0.7 + (n-5)*0.3; 192 } 193 return cost; 194 } 195 196 } 197 class User { 198 private UserRecords userRecords;//用户记录 199 private double balance = 100;//用户余额 200 private ChargeMode chargeMode;//计费方式 201 private String number;//号码 202 203 public double calCost() { 204 double cost = chargeMode.calCost(userRecords); 205 balance -= cost; 206 return cost; 207 } 208 209 public UserRecords getUserRecords() { 210 return userRecords; 211 } 212 public void setUserRecords(UserRecords userRecords) { 213 this.userRecords = userRecords; 214 } 215 public ChargeMode getChargeMode() { 216 return chargeMode; 217 } 218 public void setChargeMode(ChargeMode chargeMode) { 219 this.chargeMode = chargeMode; 220 } 221 public String getNumber() { 222 return number; 223 } 224 public void setNumber(String number) { 225 this.number = number; 226 } 227 public double getBalance() { 228 return balance; 229 } 230 231 } 232 class UserRecords { 233 // 保存用户记录 234 ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();//发信息 235 ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();//收信息 236 public void addSendMessageRecords (MessageRecord sendMessageRecord) { 237 // 添加发消息记录 238 sendMessageRecords.add(sendMessageRecord); 239 } 240 public void addreceiveMessageRecords (MessageRecord receiveMessageRecord) { 241 // 添加收消息记录 242 receiveMessageRecords.add(receiveMessageRecord); 243 } 244 245 // get方法 246 public ArrayList<MessageRecord> getSendMessageRecords() { 247 return sendMessageRecords; 248 } 249 public ArrayList<MessageRecord> getReceiveMessageRecords() { 250 return receiveMessageRecords; 251 } 252 253 }View Code
三、踩坑心得
这一系列没有设置太多的坑,只是理解类图需要一段时间,只有一个时间是否合法需要着重看待
心得:这一系列的心得还是比较多的,了解两个新的类SimpleDateFormat类和Date类;SimpleDateFormat类创建不同的对象,可以将不同格式的字符串转化为时间类的对象;对容器里面的对象按某一种规则排序,导入
import java.util.Comparator;import java.util.Collections;使用Collections.sort方法传入new Comparator<User>()对象覆盖compare方法;对正则表达式有了更深层次的理解,能够更好地运用;Date类中的getTime的方法获取出来的时间为毫秒形式;目前只能想到这么多,以后想到继续添加。
四、改进建议
1.第六次题目集中的存储用户信息方法在主函数中过于笨重,改为在某个类中创建方法
1 /* 座机 */ 2 public static void saveLandUserRecords(ArrayList<User> users, ArrayList<CallRecord> callRecords) { 3 for(int i=0; i<users.size(); i++) { 4 UserRecords userRecords = new UserRecords();//用户记录 5 for(int j=0; j<callRecords.size(); j++) { 6 // 将用户记录添加到对应的用户记录当中 7 if(users.get(i).getNumber().equals(callRecords.get(j).getCallingNumber())) { 8 if(callRecords.get(j).getAnswerAddressAreaCode().matches("0791")) { 9 // 打市内电话 10 userRecords.addCallingInCityRecords(callRecords.get(j)); 11 } 12 else if(callRecords.get(j).getAnswerAddressAreaCode().matches("((079\\d)|0701)")) { 13 // 打省内电话 14 userRecords.addCallingInProvinceRecords(callRecords.get(j)); 15 } 16 else { 17 // 打国内电话 18 userRecords.addCallingInLandRecords(callRecords.get(j)); 19 } 20 } 21 if(users.get(i).getNumber().equals(callRecords.get(j).getAnswerNumber())) { 22 if(callRecords.get(j).getCallingAddressAreaCode().matches("0791")) { 23 // 市内接电话 24 userRecords.addanswerInCityRecords(callRecords.get(j)); 25 } 26 else if(callRecords.get(j).getCallingAddressAreaCode().matches("((079\\d)|0701)")) { 27 // 省内接电话 28 userRecords.addanswerInProvinceRecords(callRecords.get(j)); 29 } 30 else { 31 // 国内接电话 32 userRecords.addanswerInLandRecords(callRecords.get(j)); 33 } 34 } 35 } 36 // 将用户记录存入对应用户中 37 users.get(i).setUserRecords(userRecords); 38 } 39 } 40 41 /* 手机 */ 42 public static void saveUsePhonerRecords(ArrayList<User> users, ArrayList<CallRecord> callRecords) { 43 for(int i=0; i<users.size(); i++) { 44 UserRecords userRecords = new UserRecords();//用户记录 45 for(int j=0; j<callRecords.size(); j++) { 46 if(users.get(i).getNumber().equals(callRecords.get(j).getCallingNumber())) { 47 if(callRecords.get(j).getCallingAddressAreaCode().equals("0791")) { 48 if(callRecords.get(j).getAnswerAddressAreaCode().matches("0791")) { 49 // 市内打市内电话 50 userRecords.addCallingInCityRecords(callRecords.get(j)); 51 } 52 else if(callRecords.get(j).getAnswerAddressAreaCode().matches("((079\\d)|0701)")) { 53 // 市内打省内电话 54 userRecords.addCallingInProvinceRecords(callRecords.get(j)); 55 } 56 else { 57 // 市内打国内电话 58 userRecords.addCallingInLandRecords(callRecords.get(j)); 59 } 60 } 61 else if(callRecords.get(j).getCallingAddressAreaCode().matches("((079\\d)|0701)")) { 62 userRecords.addPcallingRecords(callRecords.get(j)); 63 } 64 else { 65 userRecords.addLcallingRecords(callRecords.get(j)); 66 } 67 } 68 if(users.get(i).getNumber().equals(callRecords.get(j).getAnswerNumber())) { 69 if(callRecords.get(j).getAnswerAddressAreaCode().matches("0791")) { 70 // 市内接电话 71 userRecords.addanswerInCityRecords(callRecords.get(j)); 72 } 73 else if(callRecords.get(j).getAnswerAddressAreaCode().matches("((079\\d)|0701)")) { 74 // 省内接电话 75 userRecords.addanswerInProvinceRecords(callRecords.get(j)); 76 } 77 else { 78 // 国内接电话 79 userRecords.addanswerInLandRecords(callRecords.get(j)); 80 } 81 } 82 } 83 // 将用户记录存入对应用户中 84 users.get(i).setUserRecords(userRecords); 85 }View Code
2.第六次生成User和UserRecord的过程在主函数中,太过繁琐,改进后
1 public static User getUser(String s) { 2 User user = new User(); 3 if(InputFormat.Alljudge(s)) { 4 String[] ss = s.split(" "); 5 String[] sss = ss[0].split("-"); 6 if(InputFormat.LandUjudge(s)) { 7 LandlinePhoneCharging l = new LandlinePhoneCharging(); 8 user.setChargeMode(l);user.setNumber(sss[1]);//sss[1]为座机号码 9 } 10 if(InputFormat.PhoneUjudge(s)) { 11 if(ss[1].matches("1")) { 12 Phone p= new Phone(); 13 user.setChargeMode(p);user.setNumber(sss[1]); 14 } 15 if(ss[1].matches("2")) { 16 PhoneA p= new PhoneA(); 17 user.setChargeMode(p);user.setNumber(sss[1]); 18 } 19 } 20 } 21 return user; 22 } 23 public static CallRecord getCallRecord(String s) throws ParseException { 24 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); 25 CallRecord callRecord = new CallRecord(); 26 Date start; 27 Date end; 28 if(InputFormat.Alljudge(s)) { 29 String[] ss = s.split(" "); 30 String[] sss = ss[0].split("-"); 31 if(InputFormat.LandPhoneTjudge(s)) { 32 start = dateFormat.parse(ss[3]+" "+ss[4]); 33 end = dateFormat.parse(ss[5]+" "+ss[6]); 34 callRecord.setCallingNumber(sss[1]);callRecord.setCallingAddressAreaCode(sss[1].substring(0, 4)); 35 callRecord.setAnswerNumber(ss[1]);callRecord.setAnswerAddressAreaCode(ss[2]); 36 callRecord.setStartTime(start);callRecord.setEndTime(end); 37 } 38 if(InputFormat.LandTjudge(s)) { 39 start = dateFormat.parse(ss[2]+" "+ss[3]); 40 end = dateFormat.parse(ss[4]+" "+ss[5]); 41 callRecord.setCallingNumber(sss[1]);callRecord.setCallingAddressAreaCode(sss[1].substring(0, 4)); 42 callRecord.setAnswerNumber(ss[1]);callRecord.setAnswerAddressAreaCode(ss[1].substring(0, 4)); 43 callRecord.setStartTime(start);callRecord.setEndTime(end); 44 } 45 if(InputFormat.PhoneLandTjudge(s)) { 46 start = dateFormat.parse(ss[3]+" "+ss[4]); 47 end = dateFormat.parse(ss[5]+" "+ss[6]); 48 callRecord.setCallingNumber(sss[1]);callRecord.setCallingAddressAreaCode(ss[1]); 49 callRecord.setAnswerNumber(ss[2]);callRecord.setAnswerAddressAreaCode(ss[2].substring(0, 4)); 50 callRecord.setStartTime(start);callRecord.setEndTime(end); 51 } 52 if(InputFormat.PhoneTjudge(s)) { 53 start = dateFormat.parse(ss[4]+" "+ss[5]); 54 end = dateFormat.parse(ss[6]+" "+ss[7]); 55 callRecord.setCallingNumber(sss[1]);callRecord.setCallingAddressAreaCode(ss[1]); 56 callRecord.setAnswerNumber(ss[2]);callRecord.setAnswerAddressAreaCode(ss[3]); 57 callRecord.setStartTime(start);callRecord.setEndTime(end); 58 } 59 } 60 return callRecord; 61 }View Code
五、总结
对于这一阶段大部分还是在继承方面走,但是明显感觉到再向抽象与接口这边走了,虽然没有用到。对于继承的学习,可能就作业来说到此结束,但是通过这阶段的学习(三个星期)不能说长,也不能说短,要说把继承学好也不可能,但是总能了解基础的用法。对于ArrayList容器来说,从网课开始老师就讲了,所以到现在也能熟练掌握其用法。学习到了许多几何方法,对特殊情况的判断特殊处理。到了现在,也基本达到从面对过程到面向对象的蜕变。对于作业这方面虽然不算多,但是都挤在一起就会没有时间写。作业和实验上,老师可以继续将作业发布可以把题目提前一点,可以在星期五的下午发布PTA,还有作业或实验提交在哪里也可以先发布。
这篇关于面对对象程序设计题目分析与心得3的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南