第三次博客作业
- 前言
- 设计与分析
- 踩坑心得
- 改进建议
- 总结
前言
(总结之前所涉及到的知识点、题量、难度等情况)
我认为这三次PTA作业较为简单,非常人性化,每道题都给了类图,都不怎么要动脑子了 ,
所涉及的知识点还是比较广的,但是并不难,题量也不多,每一次就两三道,
主要是电信计费系列的三次迭代题。
设计与分析
(重点对题目的提交源码进行分析,可参考SourceMonitor的生成报表内容以及PowerDesigner的相应类图,要有相应的解释和心得(做到有图有真相),本次Blog必须分析PTA中的三个资费题目)
7-1 电信计费系列1-座机计费
我认为该题主要考察类间关系的设计以及如何利用正则表达式对输入的字符串进行特判。
还有就是利用Arraylist对字符串进行存储。本来设计类间关系是该题最为困难的环节但是
老师在出题时已经将类图基本给出,所以难点转变为了如何利用正则表达式对输入的字符串进行特判。
(在Main函数中)我之前几次提交也是在特判上出了一点问题。
(下面是已经给出的类图)



1 import java.text.ParseException; 2 import java.text.SimpleDateFormat; 3 import java.util.ArrayList; 4 import java.util.Date; 5 import java.util.Scanner; 6 import java.text.ParsePosition; 7 import java.text.DateFormat; 8 import java.util.*; 9 10 public class Main { 11 public static void main(String[] args) throws ParseException { 12 Scanner in =new Scanner(System.in); 13 ArrayList<User> user = new ArrayList<>(); 14 while(true){ 15 String a = in.nextLine(); 16 if(a.equals("end")) 17 break; 18 String s1 = "u-[0-9]{10,12} 0"; 19 String s2 = "t-[0-9]{10,12} [0-9]{10,12} [0-9]{4}.([1-9]||1[0-2]).([1-9]||[1-3][0-9]) [0-9]{2}.[0-9]{2}.[0-9]{2} [0-9]{4}.([1-9]||1[0-2]).([1-9]||[1-3][0-9]) [0-9]{2}.[0-9]{2}.[0-9]{2}"; 20 if(a.matches(s1)) 21 { 22 String[] a1=a.split(" "); 23 String[] a2=a1[0].split("-"); 24 int f = 0; 25 for(int i=0;i<user.size();i++) 26 { 27 if(a2[1].equals(user.get(i).getNumber())) 28 f = 1; 29 } 30 if(f == 0) { 31 User b = new User(a2[1]); 32 user.add(b); 33 } 34 } 35 if(a.matches(s2)) 36 { 37 String[] a1=a.split(" "); 38 String[] a2=a1[0].split("-"); 39 int f = -1; 40 for(int i=0;i<user.size();i++){ 41 if(Objects.equals(a2[1], user.get(i).getNumber())) 42 f = i; 43 } 44 if(f >= 0){ 45 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); 46 simpleDateFormat.setLenient(false); 47 String time1=a1[2]+" "+a1[3]; 48 String time2=a1[4]+" "+a1[5]; 49 ParsePosition parsePosition1 = new ParsePosition(0); 50 ParsePosition parsePosition2 = new ParsePosition(0); 51 CallRecord call=new CallRecord(); 52 53 if(simpleDateFormat.parse(time1,parsePosition1) != null && simpleDateFormat.parse(time2,parsePosition2) != null ) { 54 Date StartTime = simpleDateFormat.parse(time1); 55 Date EndTime = simpleDateFormat.parse(time2); 56 call.setStartTime(StartTime); 57 call.setEndTime(EndTime); 58 if (a1[1].matches("0791[0-9]{7,8}")) 59 { 60 user.get(f).getUserRecords().addCallingInCityRecords(call); 61 } 62 else if(a1[1].matches("079[0-9][0-9]{7,8}")||a1[1].matches("0701[0-9]{7,8}")) 63 { 64 user.get(f).getUserRecords().addCallingInProvinceRecords(call); 65 } 66 else{ 67 user.get(f).getUserRecords().addCallingInLandRecords(call); 68 } 69 } 70 71 } 72 } 73 } 74 for(int i=1;i<=user.size()-1;i++){ 75 for(int j=0;j<=user.size()-1-i;j++){ 76 if(Double.parseDouble(user.get(j).getNumber())>Double.parseDouble(user.get(j+1).getNumber())){ 77 Collections.swap(user,j,j+1); 78 } 79 } 80 } 81 for(int i=0;i<user.size();i++){ 82 System.out.println(user.get(i).getNumber() + " " + new Formatter().format("%.1f", user.get(i).getChargeMode().calCost(user.get(i).getUserRecords())) + " " + new Formatter().format("%.1f", user.get(i).calBalance())); 83 } 84 85 86 } 87 } 88 89 abstract class CallChargeRule { 90 public abstract double calCost(ArrayList<CallRecord> callRecords); 91 } 92 class CallRecord extends CommunicationRecord{ 93 private Date startTime; 94 private Date endTime; 95 private String callingAddressAreaCode; 96 private String answerAddressAreaCode; 97 public double time() { 98 double time; 99 100 DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 101 102 java.util.Date now = null; 103 try { 104 now = df.parse("startTime"); 105 } catch (ParseException e) { 106 e.printStackTrace(); 107 } 108 109 java.util.Date date = null; 110 try { 111 date = df.parse("endTime"); 112 } catch (ParseException e) { 113 e.printStackTrace(); 114 } 115 116 long l=getEndTime().getTime()-getStartTime().getTime(); 117 118 long day=l/(24*60*60*1000); 119 120 long hour=(l/(60*60*1000)-day*24); 121 122 long min=((l/(60*1000))-day*24*60-hour*60); 123 124 long s=(l/1000-day*24*60*60-hour*60*60-min*60); 125 126 if(s > 0){ 127 time = day*24*60 + hour*60 + min + 1; 128 } 129 else{ 130 time = day*24*60 + hour*60 + min; 131 } 132 133 return time; 134 } 135 136 public Date getStartTime() { 137 return startTime; 138 } 139 public void setStartTime(Date startTime) { 140 this.startTime = startTime; 141 } 142 public Date getEndTime() { 143 return endTime; 144 } 145 public void setEndTime(Date endTime) { 146 this.endTime = endTime; 147 } 148 public String getCallingAddressAreaCode() { 149 return callingAddressAreaCode; 150 } 151 public void setCallingAddressAreaCode(String callingAddressAreaCode) { 152 this.callingAddressAreaCode = callingAddressAreaCode; 153 } 154 public String getAnswerAddressAreaCode() { 155 return answerAddressAreaCode; 156 } 157 public void setAnswerAddressAreaCode(String answerAddressAreaCode) { 158 this.answerAddressAreaCode = answerAddressAreaCode; 159 } 160 } 161 abstract class ChargeMode { 162 private ArrayList<ChargeRule> chargeRules = new ArrayList<>(); 163 public abstract double calCost(UserRecords userRecords); 164 public abstract double getMonthlyRent(); 165 public ArrayList<ChargeRule> getChargeRules() { 166 return chargeRules; 167 } 168 public void setChargeRules(ArrayList<ChargeRule> chargeRules) { 169 this.chargeRules = chargeRules; 170 } 171 } 172 173 abstract class ChargeRule { 174 } 175 abstract class CommunicationRecord { 176 protected String callingNumber; 177 protected String answerNumber; 178 public String getCallingNumber() { 179 return callingNumber; 180 } 181 public void setCallingNumber(String callingNumber) { 182 this.callingNumber = callingNumber; 183 } 184 public String getAnswerNumber() { 185 return answerNumber; 186 } 187 public void setAnswerNumber(String answerNumber) { 188 this.answerNumber = answerNumber; 189 } 190 } 191 class LandlinePhoneCharging extends ChargeMode{ 192 private double monthlyRent = 20; 193 LandPhoneInCityRule a1 = new LandPhoneInCityRule(); 194 LandPhoneInProvinceRule a2 = new LandPhoneInProvinceRule(); 195 LandPhoneInLandRule a3 = new LandPhoneInLandRule(); 196 197 @Override 198 public double calCost(UserRecords userRecords) { 199 return a1.calCost(userRecords.getCallingInCityRecords()) + 200 a2.calCost(userRecords.getCallingInProvinceRecords()) + 201 a3.calCost(userRecords.getCallingInLandRecords()); 202 } 203 @Override 204 public double getMonthlyRent() { 205 return monthlyRent; 206 } 207 } 208 class LandPhoneInCityRule extends CallChargeRule{ 209 @Override 210 public double calCost(ArrayList<CallRecord> callRecords) { 211 double sum = 0; 212 for(int i = 0; i<callRecords.size();i++) { 213 sum+=0.1*callRecords.get(i).time(); 214 } 215 return sum; 216 } 217 } 218 219 class LandPhoneInLandRule extends CallChargeRule{ 220 @Override 221 public double calCost(ArrayList<CallRecord> callRecords) { 222 double sum = 0; 223 for(int i = 0; i<callRecords.size();i++) { 224 sum+=0.6*callRecords.get(i).time(); 225 } 226 return sum; 227 } 228 } 229 class LandPhoneInProvinceRule extends CallChargeRule{ 230 @Override 231 public double calCost(ArrayList<CallRecord> callRecords) { 232 double sum = 0; 233 for(int i = 0; i<callRecords.size();i++) { 234 sum+=0.3*callRecords.get(i).time(); 235 } 236 return sum; 237 } 238 } 239 class MessageRecord extends CommunicationRecord{ 240 private String message; 241 public String getMessage() { 242 return message; 243 } 244 public void setMessage(String message) { 245 this.message = message; 246 } 247 } 248 class User { 249 private UserRecords userRecords = new UserRecords(); 250 private double balance = 100; 251 private LandlinePhoneCharging chargeMode = new LandlinePhoneCharging(); 252 private String number; 253 254 public User(String number) { 255 this.number = number; 256 } 257 258 public double calBalance() { 259 return getBalance() - calCost() - chargeMode.getMonthlyRent(); 260 } 261 262 public double calCost() { 263 return chargeMode.calCost(getUserRecords()); 264 } 265 266 public ChargeMode getChargeMode() { 267 return chargeMode; 268 } 269 public void setChargeMode(LandlinePhoneCharging chargeMode) { 270 this.chargeMode = chargeMode; 271 } 272 public UserRecords getUserRecords() { 273 return userRecords; 274 } 275 public void setUserRecords(UserRecords userRecords) { 276 this.userRecords = userRecords; 277 } 278 public double getBalance() { 279 return balance; 280 } 281 public String getNumber() { 282 return number; 283 } 284 public void setNumber(String number) { 285 this.number = number; 286 } 287 } 288 class UserRecords { 289 private ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>(); 290 private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>(); 291 private ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>(); 292 private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>(); 293 private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>(); 294 private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>(); 295 private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>(); 296 private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>(); 297 298 public void addCallingInCityRecords(CallRecord callRecord) { 299 callingInCityRecords.add(callRecord); 300 } 301 public void addCallingInProvinceRecords(CallRecord callRecord) { 302 callingInProvinceRecords.add(callRecord); 303 } 304 public void addCallingInLandRecords(CallRecord callRecord) { 305 callingInLandRecords.add(callRecord); 306 } 307 public void addAnswerInCityRecords(CallRecord callRecord) {answerInCityRecords.add(callRecord);} 308 public void addAnswerInProvinceRecords(CallRecord callRecord) { 309 answerInProvinceRecords.add(callRecord); 310 } 311 public void addAnswerInLandRecords(CallRecord callRecord) { 312 answerInLandRecords.add(callRecord); 313 } 314 public void addSendMessageRecords(MessageRecord messageRecord) { 315 sendMessageRecords.add(messageRecord); 316 } 317 public void addReceiveMessageRecords(MessageRecord messageRecord) { 318 receiveMessageRecords.add(messageRecord); 319 } 320 public ArrayList<CallRecord> getCallingInCityRecords() { 321 return callingInCityRecords; 322 } 323 public ArrayList<CallRecord> getCallingInProvinceRecords() { 324 return callingInProvinceRecords; 325 } 326 public ArrayList<CallRecord> getCallingInLandRecords() { 327 return callingInLandRecords; 328 } 329 public ArrayList<CallRecord> getAnswerInCityRecords() { 330 return answerInCityRecords; 331 } 332 public ArrayList<CallRecord> getAnswerInProvinceRecords() { 333 return answerInProvinceRecords; 334 } 335 public ArrayList<CallRecord> getAnswerInLandRecords() { 336 return answerInLandRecords; 337 } 338 public ArrayList<MessageRecord> getSendMessageRecords() { 339 return sendMessageRecords; 340 } 341 public ArrayList<MessageRecord> getReceiveMessageRecords() { 342 return receiveMessageRecords; 343 } 344 }
7-1 电信计费系列2-手机+座机计费
该题为上一题的迭代,类间设计基本不变,就是加了一个手机计费设计,
要区分座机计费与手机计费,同时对手机计费要对计费方式进行重新设计。
(下面是已经给出的类图)



1 import java.text.ParseException; 2 import java.text.SimpleDateFormat; 3 import java.util.ArrayList; 4 import java.util.Date; 5 import java.util.Scanner; 6 import java.text.ParsePosition; 7 import java.text.DateFormat; 8 import java.util.*; 9 10 public class Main { 11 public static void main(String[] args) throws ParseException { 12 Scanner in =new Scanner(System.in); 13 ArrayList<User> user = new ArrayList<>(); 14 while(true){ 15 String a = in.nextLine(); 16 if(a.equals("end")) 17 break; 18 //座机开户 19 String s1 = "u-0791[0-9]{7,8} 0"; 20 //手机开户 21 String s2 = "u-1[0-9]{10} 1"; 22 //座机打座机 23 String s3 = "t-0791\\d{7,8} 0\\d{9,11} \\d{4}.\\d{1,2}.\\d{1,2} \\d{2}:\\d{2}:\\d{2} \\d{4}.\\d{1,2}.\\d{1,2} \\d{2}:\\d{2}:\\d{2}"; 24 //座机打手机 25 String s4 = "t-0\\d{9,11} 1\\d{10} 0\\d{2,3} \\d{4}.\\d{1,2}.\\d{1,2} \\d{2}:\\d{2}:\\d{2} \\d{4}.\\d{1,2}.\\d{1,2} \\d{2}:\\d{2}:\\d{2}"; 26 //手机打座机 27 String s5 = "t-1\\d{10} 0\\d{2,3} 0\\d{9,11} \\d{4}.\\d{1,2}.\\d{1,2} \\d{2}:\\d{2}:\\d{2} \\d{4}.\\d{1,2}.\\d{1,2} \\d{2}:\\d{2}:\\d{2}"; 28 //手机打手机 29 String s6 = "t-1\\d{10} 0\\d{2,3} 1\\d{10} 0\\d{2,3} \\d{4}.\\d{1,2}.\\d{1,2} \\d{2}:\\d{2}:\\d{2} \\d{4}.\\d{1,2}.\\d{1,2} \\d{2}:\\d{2}:\\d{2}"; 30 31 if(!a.matches(s1)&&!a.matches(s2)&&!a.matches(s3)&&!a.matches(s4)&&!a.matches(s5)&&!a.matches(s6)){ 32 continue; 33 } 34 35 if(a.matches(s1)) 36 { 37 String[] a1=a.split(" "); 38 String[] a2=a1[0].split("-"); 39 int f = 0; 40 for(int i=0;i<user.size();i++) 41 { 42 if(a2[1].equals(user.get(i).getNumber())) 43 f = 1; 44 } 45 if(f == 0) { 46 User b = new User(a2[1],0); 47 user.add(b); 48 } 49 } 50 if(a.matches(s2)) 51 { 52 String[] a1=a.split(" "); 53 String[] a2=a1[0].split("-"); 54 int f = 0; 55 for(int i=0;i<user.size();i++) 56 { 57 if(a2[1].equals(user.get(i).getNumber())) 58 f = 1; 59 } 60 if(f == 0) { 61 User b = new User(a2[1],1); 62 user.add(b); 63 } 64 } 65 if(a.matches(s3))//座机打座机 66 { 67 String[] a1=a.split(" "); 68 String[] a2=a1[0].split("-"); 69 int f = -1; 70 for(int i=0;i<user.size();i++){ 71 if(Objects.equals(a2[1], user.get(i).getNumber())) 72 f = i; 73 } 74 if(f >= 0){ 75 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); 76 simpleDateFormat.setLenient(false); 77 String time1=a1[2]+" "+a1[3]; 78 String time2=a1[4]+" "+a1[5]; 79 ParsePosition parsePosition1 = new ParsePosition(0); 80 ParsePosition parsePosition2 = new ParsePosition(0); 81 CallRecord call=new CallRecord(); 82 83 if(simpleDateFormat.parse(time1,parsePosition1) != null && simpleDateFormat.parse(time2,parsePosition2) != null ) { 84 Date StartTime = simpleDateFormat.parse(time1); 85 Date EndTime = simpleDateFormat.parse(time2); 86 call.setStartTime(StartTime); 87 call.setEndTime(EndTime); 88 if (a1[1].matches("0791[0-9]{7,8}")) 89 { 90 user.get(f).getUserRecords().addCallingInCityRecords(call); 91 } 92 else if(a1[1].matches("079[0-9][0-9]{7,8}")||a1[1].matches("0701[0-9]{7,8}")) 93 { 94 user.get(f).getUserRecords().addCallingInProvinceRecords(call); 95 } 96 else{ 97 user.get(f).getUserRecords().addCallingInLandRecords(call); 98 } 99 } 100 101 } 102 } 103 104 if(a.matches(s4))//座机打手机 105 { 106 String[] a1=a.split(" "); 107 String[] a2=a1[0].split("-"); 108 int f = -1; 109 int o=-1; 110 for(int i=0;i<user.size();i++){ 111 if(Objects.equals(a2[1], user.get(i).getNumber())) 112 f = i; 113 } 114 for(int i=0;i<user.size();i++){ 115 if(Objects.equals(a1[1], user.get(i).getNumber())) 116 o = i; 117 } 118 119 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); 120 simpleDateFormat.setLenient(false); 121 String time1=a1[3]+" "+a1[4]; 122 String time2=a1[5]+" "+a1[6]; 123 ParsePosition parsePosition1 = new ParsePosition(0); 124 ParsePosition parsePosition2 = new ParsePosition(0); 125 CallRecord call=new CallRecord(); 126 127 if(simpleDateFormat.parse(time1,parsePosition1) != null && simpleDateFormat.parse(time2,parsePosition2) != null ) { 128 Date StartTime = simpleDateFormat.parse(time1); 129 Date EndTime = simpleDateFormat.parse(time2); 130 call.setStartTime(StartTime); 131 call.setEndTime(EndTime); 132 if(f >= 0){ 133 if (a1[2].matches("0791")) 134 { 135 user.get(f).getUserRecords().addCallingInCityRecords(call); 136 } 137 else if(a1[2].matches("079[0-9]")||a1[2].matches("0701")) 138 { 139 user.get(f).getUserRecords().addCallingInProvinceRecords(call); 140 } 141 else{ 142 user.get(f).getUserRecords().addCallingInLandRecords(call); 143 } 144 } 145 if(o >= 0){ 146 if(!a1[2].matches("079[0-9]")&&!a1[2].matches("0701")){ 147 user.get(o).getUserRecords().add5(call);//接电话的手机在省外 148 } 149 } 150 } 151 } 152 153 if(a.matches(s5))//手机打座机 154 { 155 String[] a1=a.split(" "); 156 String[] a2=a1[0].split("-"); 157 int f = -1; 158 for(int i=0;i<user.size();i++){ 159 if(Objects.equals(a2[1], user.get(i).getNumber())) 160 f = i; 161 } 162 if(f >= 0){ 163 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); 164 simpleDateFormat.setLenient(false); 165 String time1=a1[3]+" "+a1[4]; 166 String time2=a1[5]+" "+a1[6]; 167 ParsePosition parsePosition1 = new ParsePosition(0); 168 ParsePosition parsePosition2 = new ParsePosition(0); 169 CallRecord call=new CallRecord(); 170 171 if(simpleDateFormat.parse(time1,parsePosition1) != null && simpleDateFormat.parse(time2,parsePosition2) != null ) { 172 Date StartTime = simpleDateFormat.parse(time1); 173 Date EndTime = simpleDateFormat.parse(time2); 174 call.setStartTime(StartTime); 175 call.setEndTime(EndTime); 176 if (a1[1].matches("0791")) 177 { 178 user.get(f).getUserRecords().add1(call);//接电话的手机在市内 179 } 180 else if(a1[1].matches("079[0-9]")||a1[1].matches("0701")) 181 { 182 user.get(f).getUserRecords().add4(call);//打电话的手机在省内 183 } 184 else{ 185 user.get(f).getUserRecords().add6(call);//打电话的手机在省外 186 } 187 } 188 189 } 190 } 191 192 if(a.matches(s6))//手机打手机 193 { 194 String[] a1=a.split(" "); 195 String[] a2=a1[0].split("-"); 196 int f = -1; 197 int o=-1; 198 for(int i=0;i<user.size();i++){ 199 if(Objects.equals(a2[1], user.get(i).getNumber())) 200 f = i; 201 } 202 for(int i=0;i<user.size();i++){ 203 if(Objects.equals(a1[2], user.get(i).getNumber())) 204 o = i; 205 } 206 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); 207 simpleDateFormat.setLenient(false); 208 String time1=a1[4]+" "+a1[5]; 209 String time2=a1[6]+" "+a1[7]; 210 ParsePosition parsePosition1 = new ParsePosition(0); 211 ParsePosition parsePosition2 = new ParsePosition(0); 212 CallRecord call=new CallRecord(); 213 214 if(simpleDateFormat.parse(time1,parsePosition1) != null && simpleDateFormat.parse(time2,parsePosition2) != null ) { 215 Date StartTime = simpleDateFormat.parse(time1); 216 Date EndTime = simpleDateFormat.parse(time2); 217 call.setStartTime(StartTime); 218 call.setEndTime(EndTime); 219 if(f > 0){ 220 if (a1[1].matches("0791"))//打电话的手机在市内 221 { 222 if(a1[3].matches("0791")) 223 user.get(f).getUserRecords().add1(call);//接电话的手机在市内 224 else if(a1[3].matches("079[0-9]")||a1[3].matches("0701")) 225 user.get(f).getUserRecords().add2(call);//接电话的手机在省内 226 else 227 user.get(f).getUserRecords().add3(call);//接电话的手机在省外 228 } 229 else if(a1[1].matches("079[0-9]")||a1[1].matches("0701"))//打电话的手机在省内 230 { 231 user.get(f).getUserRecords().add4(call);//打电话的手机在省内 232 } 233 else {//打电话的手机在省外 234 user.get(f).getUserRecords().add6(call);//打电话的手机在省外 235 } 236 237 } 238 239 } 240 if(o>0){ 241 if(!a1[3].matches("079[0-9]")&&!a1[3].matches("0701")){ 242 user.get(o).getUserRecords().add5(call);//接电话的手机在省外 243 } 244 } 245 } 246 } 247 for(int i=1;i<=user.size()-1;i++){ 248 for(int j=0;j<=user.size()-1-i;j++){ 249 if(user.get(j).getNumber().compareTo(user.get(j+1).getNumber())>0){ 250 Collections.swap(user,j,j+1); 251 } 252 } 253 } 254 for(int i=0;i<user.size();i++){ 255 if(user.get(i).getType()==0) { 256 System.out.println(user.get(i).getNumber() + " " + String.format("%.1f", user.get(i).getChargeMode().calCost(user.get(i).getUserRecords())) 257 + " " + String.format("%.1f", user.get(i).calBalance())); 258 } 259 else { 260 System.out.println(user.get(i).getNumber() + " " + String.format("%.1f", user.get(i).getChargeMode2().calCost(user.get(i).getUserRecords())) 261 + " " + String.format("%.1f", user.get(i).calBalance2())); 262 } 263 } 264 265 266 267 } 268 } 269 270 271 abstract class CallChargeRule { 272 public abstract double calCost(ArrayList<CallRecord> callRecords); 273 } 274 class CallRecord extends CommunicationRecord{ 275 private Date startTime; 276 private Date endTime; 277 private String callingAddressAreaCode; 278 private String answerAddressAreaCode; 279 280 public double time() { 281 double time; 282 283 DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 284 285 java.util.Date now = null; 286 try { 287 now = df.parse("startTime"); 288 } catch (ParseException e) { 289 e.printStackTrace(); 290 } 291 292 java.util.Date date = null; 293 try { 294 date = df.parse("endTime"); 295 } catch (ParseException e) { 296 e.printStackTrace(); 297 } 298 299 long l=getEndTime().getTime()-getStartTime().getTime(); 300 301 long day=l/(24*60*60*1000); 302 303 long hour=(l/(60*60*1000)-day*24); 304 305 long min=((l/(60*1000))-day*24*60-hour*60); 306 307 long s=(l/1000-day*24*60*60-hour*60*60-min*60); 308 309 if(s > 0){ 310 time = day*24*60 + hour*60 + min + 1; 311 } 312 else{ 313 time = day*24*60 + hour*60 + min; 314 } 315 316 return time; 317 } 318 319 public Date getStartTime() { 320 return startTime; 321 } 322 public void setStartTime(Date startTime) { 323 this.startTime = startTime; 324 } 325 public Date getEndTime() { 326 return endTime; 327 } 328 public void setEndTime(Date endTime) { 329 this.endTime = endTime; 330 } 331 public String getCallingAddressAreaCode() { 332 return callingAddressAreaCode; 333 } 334 public void setCallingAddressAreaCode(String callingAddressAreaCode) { 335 this.callingAddressAreaCode = callingAddressAreaCode; 336 } 337 public String getAnswerAddressAreaCode() { 338 return answerAddressAreaCode; 339 } 340 public void setAnswerAddressAreaCode(String answerAddressAreaCode) { 341 this.answerAddressAreaCode = answerAddressAreaCode; 342 } 343 } 344 abstract class ChargeMode { 345 private ArrayList<ChargeRule> chargeRules = new ArrayList<>(); 346 public abstract double calCost(UserRecords userRecords); 347 public abstract double getMonthlyRent(); 348 public ArrayList<ChargeRule> getChargeRules() { 349 return chargeRules; 350 } 351 public void setChargeRules(ArrayList<ChargeRule> chargeRules) { 352 this.chargeRules = chargeRules; 353 } 354 } 355 356 abstract class ChargeRule { 357 } 358 abstract class CommunicationRecord { 359 protected String callingNumber; 360 protected String answerNumber; 361 public String getCallingNumber() { 362 return callingNumber; 363 } 364 public void setCallingNumber(String callingNumber) { 365 this.callingNumber = callingNumber; 366 } 367 public String getAnswerNumber() { 368 return answerNumber; 369 } 370 public void setAnswerNumber(String answerNumber) { 371 this.answerNumber = answerNumber; 372 } 373 } 374 375 class CellPhoneCharging extends ChargeMode{ 376 private double monthlyRent = 15; 377 CellPhoneInCityRule1 a1 = new CellPhoneInCityRule1(); 378 CellPhoneInCityRule2 a2 = new CellPhoneInCityRule2(); 379 CellPhoneInCityRule3 a3 = new CellPhoneInCityRule3(); 380 CellPhoneInProvinceRule a4 = new CellPhoneInProvinceRule(); 381 CellPhoneInLandRule a5 = new CellPhoneInLandRule(); 382 CellPhoneInLandRule2 a6 = new CellPhoneInLandRule2(); 383 //省外听未写 0.3 384 @Override 385 public double calCost(UserRecords userRecords) { 386 return a1.calCost(userRecords.getAdd1()) + 387 a2.calCost(userRecords.getAdd2()) + 388 a3.calCost(userRecords.getAdd3()) + 389 a4.calCost(userRecords.getAdd4()) + 390 a5.calCost(userRecords.getAdd5()) + 391 a6.calCost(userRecords.getAdd6()) ; 392 393 } 394 @Override 395 public double getMonthlyRent() { 396 return monthlyRent; 397 } 398 } 399 class CellPhoneInCityRule1 extends CallChargeRule{ 400 @Override 401 public double calCost(ArrayList<CallRecord> callRecords) { 402 double sum = 0; 403 for(int i = 0; i<callRecords.size();i++) { 404 405 sum+=0.1*callRecords.get(i).time(); 406 } 407 return sum; 408 } 409 } 410 411 class CellPhoneInCityRule2 extends CallChargeRule{ 412 @Override 413 public double calCost(ArrayList<CallRecord> callRecords) { 414 double sum = 0; 415 for(int i = 0; i<callRecords.size();i++) { 416 417 sum+=0.2*callRecords.get(i).time(); 418 } 419 return sum; 420 } 421 } 422 423 class CellPhoneInCityRule3 extends CallChargeRule{ 424 @Override 425 public double calCost(ArrayList<CallRecord> callRecords) { 426 double sum = 0; 427 for(int i = 0; i<callRecords.size();i++) { 428 429 sum+=0.3*callRecords.get(i).time(); 430 } 431 return sum; 432 } 433 } 434 435 class CellPhoneInProvinceRule extends CallChargeRule{ 436 @Override 437 public double calCost(ArrayList<CallRecord> callRecords) { 438 double sum = 0; 439 for(int i = 0; i<callRecords.size();i++) { 440 441 sum+=0.3*callRecords.get(i).time(); 442 } 443 return sum; 444 } 445 } 446 447 448 class CellPhoneInLandRule extends CallChargeRule{ 449 @Override 450 public double calCost(ArrayList<CallRecord> callRecords) { 451 double sum = 0; 452 for(int i = 0; i<callRecords.size();i++) { 453 sum+=0.3*callRecords.get(i).time(); 454 } 455 return sum; 456 } 457 } 458 459 class CellPhoneInLandRule2 extends CallChargeRule{ 460 @Override 461 public double calCost(ArrayList<CallRecord> callRecords) { 462 double sum = 0; 463 for(int i = 0; i<callRecords.size();i++) { 464 sum+=0.6*callRecords.get(i).time(); 465 } 466 return sum; 467 } 468 } 469 470 471 class LandlinePhoneCharging extends ChargeMode{ 472 private double monthlyRent = 20; 473 LandPhoneInCityRule a1 = new LandPhoneInCityRule(); 474 LandPhoneInProvinceRule a2 = new LandPhoneInProvinceRule(); 475 LandPhoneInLandRule a3 = new LandPhoneInLandRule(); 476 477 @Override 478 public double calCost(UserRecords userRecords) { 479 return a1.calCost(userRecords.getCallingInCityRecords()) + 480 a2.calCost(userRecords.getCallingInProvinceRecords()) + 481 a3.calCost(userRecords.getCallingInLandRecords()); 482 } 483 @Override 484 public double getMonthlyRent() { 485 return monthlyRent; 486 } 487 } 488 class LandPhoneInCityRule extends CallChargeRule{ 489 @Override 490 public double calCost(ArrayList<CallRecord> callRecords) { 491 double sum = 0; 492 for(int i = 0; i<callRecords.size();i++) { 493 sum+=0.1*callRecords.get(i).time(); 494 } 495 return sum; 496 } 497 } 498 499 class LandPhoneInProvinceRule extends CallChargeRule{ 500 @Override 501 public double calCost(ArrayList<CallRecord> callRecords) { 502 double sum = 0; 503 for(int i = 0; i<callRecords.size();i++) { 504 sum+=0.3*callRecords.get(i).time(); 505 } 506 return sum; 507 } 508 } 509 510 511 class LandPhoneInLandRule extends CallChargeRule{ 512 @Override 513 public double calCost(ArrayList<CallRecord> callRecords) { 514 double sum = 0; 515 for(int i = 0; i<callRecords.size();i++) { 516 sum+=0.6*callRecords.get(i).time(); 517 } 518 return sum; 519 } 520 } 521 522 class MessageRecord extends CommunicationRecord{ 523 private String message; 524 public String getMessage() { 525 return message; 526 } 527 public void setMessage(String message) { 528 this.message = message; 529 } 530 } 531 class User { 532 private UserRecords userRecords = new UserRecords(); 533 private double balance = 100; 534 private LandlinePhoneCharging chargeMode = new LandlinePhoneCharging(); 535 private CellPhoneCharging chargeMode2 = new CellPhoneCharging(); 536 private String number; 537 private int i; 538 539 540 541 public User(String number, int i) { 542 this.number = number; 543 this.i = i; 544 } 545 546 public double calBalance() { 547 return getBalance() - calCost() - chargeMode.getMonthlyRent(); 548 } 549 550 public double calBalance2() { 551 return getBalance() - calCost2() - chargeMode2.getMonthlyRent(); 552 } 553 554 public double calCost() { 555 return chargeMode.calCost(getUserRecords()); 556 } 557 558 public double calCost2() { 559 return chargeMode2.calCost(getUserRecords()); 560 } 561 562 public ChargeMode getChargeMode() { 563 return chargeMode; 564 } 565 566 public ChargeMode getChargeMode2(){ 567 return chargeMode2; 568 } 569 570 public void setChargeMode2(CellPhoneCharging chargeMode2) { 571 this.chargeMode2 = chargeMode2; 572 } 573 574 public void setChargeMode(LandlinePhoneCharging chargeMode) { 575 this.chargeMode = chargeMode; 576 } 577 public UserRecords getUserRecords() { 578 return userRecords; 579 } 580 public void setUserRecords(UserRecords userRecords) { 581 this.userRecords = userRecords; 582 } 583 public double getBalance() { 584 return balance; 585 } 586 public String getNumber() { 587 return number; 588 } 589 public void setNumber(String number) { 590 this.number = number; 591 } 592 593 public int getType() { 594 return i; 595 } 596 597 } 598 class UserRecords { 599 600 private ArrayList<CallRecord> add1 = new ArrayList<CallRecord>(); 601 private ArrayList<CallRecord> add2 = new ArrayList<CallRecord>(); 602 private ArrayList<CallRecord> add3 = new ArrayList<CallRecord>(); 603 private ArrayList<CallRecord> add4 = new ArrayList<CallRecord>(); 604 private ArrayList<CallRecord> add5 = new ArrayList<CallRecord>(); 605 private ArrayList<CallRecord> add6 = new ArrayList<CallRecord>(); 606 607 private ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>(); 608 private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>(); 609 private ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>(); 610 private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>(); 611 private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>(); 612 private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>(); 613 private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>(); 614 private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>(); 615 616 public void add1(CallRecord callRecord) { 617 add1.add(callRecord); 618 } 619 public void add2(CallRecord callRecord) { 620 add2.add(callRecord); 621 } 622 public void add3(CallRecord callRecord) { 623 add3.add(callRecord); 624 } 625 public void add4(CallRecord callRecord) { 626 add4.add(callRecord); 627 } 628 public void add5(CallRecord callRecord) { 629 add5.add(callRecord); 630 } 631 public void add6(CallRecord callRecord) { 632 add6.add(callRecord); 633 } 634 635 636 public void addCallingInCityRecords(CallRecord callRecord) { 637 callingInCityRecords.add(callRecord); 638 } 639 public void addCallingInProvinceRecords(CallRecord callRecord) { 640 callingInProvinceRecords.add(callRecord); 641 } 642 public void addCallingInLandRecords(CallRecord callRecord) { 643 callingInLandRecords.add(callRecord); 644 } 645 public void addAnswerInCityRecords(CallRecord callRecord) { 646 answerInCityRecords.add(callRecord); 647 } 648 public void addAnswerInProvinceRecords(CallRecord callRecord) { 649 answerInProvinceRecords.add(callRecord); 650 } 651 public void addAnswerInLandRecords(CallRecord callRecord) { 652 answerInLandRecords.add(callRecord); 653 } 654 public void addSendMessageRecords(MessageRecord messageRecord) { 655 sendMessageRecords.add(messageRecord); 656 } 657 public void addReceiveMessageRecords(MessageRecord messageRecord) { 658 receiveMessageRecords.add(messageRecord); 659 } 660 public ArrayList<CallRecord> getCallingInCityRecords() { 661 return callingInCityRecords; 662 } 663 public ArrayList<CallRecord> getCallingInProvinceRecords() { 664 return callingInProvinceRecords; 665 } 666 public ArrayList<CallRecord> getCallingInLandRecords() { 667 return callingInLandRecords; 668 } 669 670 671 public ArrayList<CallRecord> getAdd1() { 672 return add1; 673 } 674 public ArrayList<CallRecord> getAdd2() { 675 return add2; 676 } 677 public ArrayList<CallRecord> getAdd3() { 678 return add3; 679 } 680 public ArrayList<CallRecord> getAdd4() { 681 return add4; 682 } 683 public ArrayList<CallRecord> getAdd5() { 684 return add5; 685 } 686 public ArrayList<CallRecord> getAdd6() { 687 return add6; 688 } 689 690 691 public ArrayList<CallRecord> getAnswerInCityRecords() { 692 return answerInCityRecords; 693 } 694 public ArrayList<CallRecord> getAnswerInProvinceRecords() { 695 return answerInProvinceRecords; 696 } 697 public ArrayList<CallRecord> getAnswerInLandRecords() { 698 return answerInLandRecords; 699 } 700 public ArrayList<MessageRecord> getSendMessageRecords() { 701 return sendMessageRecords; 702 } 703 public ArrayList<MessageRecord> getReceiveMessageRecords() { 704 return receiveMessageRecords; 705 } 706 }
1 import java.text.ParseException; 2 import java.text.SimpleDateFormat; 3 import java.util.ArrayList; 4 import java.util.Date; 5 import java.util.Scanner; 6 import java.text.ParsePosition; 7 import java.text.DateFormat; 8 import java.util.*; 9 10 public class Main { 11 public static void main(String[] args) throws ParseException { 12 Scanner in =new Scanner(System.in); 13 ArrayList<User> user = new ArrayList<>(); 14 while(true){ 15 String a = in.nextLine(); 16 if(a.equals("end")) 17 break; 18 //座机开户 19 String s1 = "u-0791[0-9]{7,8} 3"; 20 //手机开户 21 String s2 = "u-1[0-9]{10} 3"; 22 //手机短信计费 23 String s7 = "m-1[3-9]\\d{9} 1[3-9]\\d{9} [a-z|0-9| |,|.]++"; 24 if(a.matches(s1)) 25 { 26 String[] a1=a.split(" "); 27 String[] a2=a1[0].split("-"); 28 int f = 0; 29 for(int i=0;i<user.size();i++) 30 { 31 if(a2[1].equals(user.get(i).getNumber())) 32 f = 1; 33 } 34 if(f == 0) { 35 User b = new User(a2[1],0); 36 user.add(b); 37 } 38 } 39 else if(a.matches(s2)) 40 { 41 String[] a1=a.split(" "); 42 String[] a2=a1[0].split("-"); 43 int f = 0; 44 for(int i=0;i<user.size();i++) 45 { 46 if(a2[1].equals(user.get(i).getNumber())) 47 f = 1; 48 } 49 if(f == 0) { 50 User b = new User(a2[1],1); 51 user.add(b); 52 } 53 } 54 if(a.matches(s7)){//短信计费 55 String[] a1=a.split(" "); 56 String[] a2=a1[0].split("-"); 57 int f = -1; 58 for(int i=0;i<user.size();i++){ 59 if(Objects.equals(a2[1], user.get(i).getNumber())) 60 f = i; 61 } 62 if(f >= 0){ 63 MessageRecord messageRecord = new MessageRecord(); 64 messageRecord.setMessage(a); 65 if(a1[0].matches("m-1[3-9]\\d{9}")){ 66 user.get(f).getUserRecords().addSendMessageRecords(messageRecord); 67 } 68 else if(a1[1].matches("1[3-9]\\d{9}")){ 69 user.get(f).getUserRecords().addReceiveMessageRecords(messageRecord); 70 } 71 } 72 } 73 74 75 } 76 for(int i=1;i<=user.size()-1;i++){ 77 for(int j=0;j<=user.size()-1-i;j++){ 78 if(Double.parseDouble(user.get(j).getNumber())>Double.parseDouble(user.get(j+1).getNumber())){ 79 Collections.swap(user,j,j+1); 80 } 81 } 82 } 83 for(int i=0;i<user.size();i++){ 84 System.out.println(user.get(i).getNumber() + " " + new Formatter().format("%.1f", user.get(i).getChargeMode3().calCost(user.get(i).getUserRecords())) + " " + new Formatter().format("%.1f", user.get(i).calBalance3())); 85 } 86 } 87 } 88 89 90 91 abstract class ChargeMode { 92 public abstract double calCost(UserRecords userRecords); 93 } 94 95 class MessageCharging extends ChargeMode{ 96 SendMessageRule b1 = new SendMessageRule(); 97 @Override 98 public double calCost(UserRecords userRecords) { 99 return b1.calCost(userRecords.getSendMessageRecords()); 100 } 101 } 102 103 abstract class ChargeRule { 104 } 105 106 abstract class MessageChargeRule extends ChargeRule{ 107 public double calCost(ArrayList<MessageRecord> messageRecords){ 108 return 0; 109 } 110 } 111 112 class SendMessageRule extends MessageChargeRule{ 113 114 @Override 115 public double calCost(ArrayList<MessageRecord> messageRecords) { 116 double sum = 0; 117 int t = 0; 118 for(int i = 0;i < messageRecords.size();i++){ 119 double a = 0; 120 if (messageRecords.get(i).Long()<=10){ 121 t = t + 1; 122 } 123 else { 124 a = messageRecords.get(i).Long(); 125 a = a/10; 126 if(a % 1!=0){ 127 a = a+1; 128 } 129 a = (int)a; 130 t += a; 131 } 132 } 133 if(t <= 3){ 134 sum = sum + t*0.1; 135 } 136 else if(t>3&&t<=5){ 137 sum+=0.3+(t-3)*0.2; 138 } 139 else { 140 sum+=0.7 + (t - 5)*0.3; 141 } 142 return sum; 143 } 144 } 145 146 abstract class CommunicationRecord { 147 } 148 149 class MessageRecord extends CommunicationRecord{ 150 private String message; 151 public String getMessage() { 152 return message; 153 } 154 public void setMessage(String message) { 155 this.message = message; 156 } 157 public double Long(){ 158 double l; 159 l = message.length() - 26; 160 return l; 161 } 162 } 163 164 class User { 165 private UserRecords userRecords = new UserRecords(); 166 private double balance = 100; 167 private MessageCharging chargeMode3 = new MessageCharging(); 168 private String number; 169 private int i; 170 171 public User(String number, int i) { 172 this.number = number; 173 this.i = i; 174 } 175 176 177 public double calBalance3(){ 178 return getBalance() - calCost3(); 179 } 180 181 public double calCost3() { 182 return chargeMode3.calCost(getUserRecords()); 183 } 184 185 public ChargeMode getChargeMode3() { 186 return chargeMode3; 187 } 188 189 public void setChargeMode3(MessageCharging chargeMode3) { 190 this.chargeMode3 = chargeMode3; 191 } 192 193 public UserRecords getUserRecords() { 194 return userRecords; 195 } 196 public void setUserRecords(UserRecords userRecords) { 197 this.userRecords = userRecords; 198 } 199 public double getBalance() { 200 return balance; 201 } 202 public String getNumber() { 203 return number; 204 } 205 public void setNumber(String number) { 206 this.number = number; 207 } 208 209 } 210 class UserRecords { 211 212 private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>(); 213 private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>(); 214 215 public void addSendMessageRecords(MessageRecord messageRecord) { 216 sendMessageRecords.add(messageRecord); 217 } 218 public void addReceiveMessageRecords(MessageRecord messageRecord) { 219 receiveMessageRecords.add(messageRecord); 220 } 221 222 public ArrayList<MessageRecord> getSendMessageRecords() { 223 return sendMessageRecords; 224 } 225 public ArrayList<MessageRecord> getReceiveMessageRecords() { 226 return receiveMessageRecords; 227 } 228 }
第三题较为简单就不加以累述了。就是加一个电信计费就行。
(类图如下)


踩坑心得
(对源码的提交过程中出现的问题及心得进行总结,务必做到详实,拿数据、源码及测试结果说话,切忌假大空)

第3次在设计的时候算法出了一点问题,信息存储其实没有出问题,就是算钱的时候出了一点问题,但是后面自己通过测试点改过来了

第2次的问题一直没有找出来,但是我觉得应该是在时间的特判上出了问题
改进建议
(对相应题目的编码改进给出自己的见解,做到可持续改进)
具体怎么改进,我自己本身也不是很清楚,但我觉得代码风格一定要简约,
同时对函数的构造以及类的分类必须清晰,要不然一旦出现格式错误,
很难在短时间内发现自己代内部的问题,
在构造类的过程中,命名尽量贴合实际,切记不要随意命名,变量名也应尽量切合题意,
这样做可以极大程度的方便后续代码的维护以及修改。
同时在书写代码的时候,量在关键处,尤其是逻辑紧密的地方加上一定量的注释,
这样做既可以方便自己后续的阅读,也可以方便别人对代码进行理解
而且要加强对字符串这方面的练习,包括正则表达式的练习,在这方面还有着较大的漏洞
总结
(对本阶段(12-15周)综合性总结,学到了什么,哪些地方需要进一步学习及研究,对教师、课程、作业、实验、课上及课下组织方式等方面的改进建议及意见。2.在截止时间之前将发布的Blog正文网址链接(非编辑网址链接)直接发到超星系统即可)
对于本阶段的综合训练,我觉得自己在类间设计这一块还需要下功夫,
对于类之间的理解以及调用仍然不太熟悉,当然了在正则上面的使用也有一些欠缺,
对于字符串的特判有时还是会出现一些问题

浙公网安备 33010602011771号