《面向对象编程学习的BLOG3》
《面向对象编程的BLOG3》
21201705-单梓洋
前言:
本Blog基于南昌航空大学软件工程专业“面向对象程序设计”笔者的收获所写,内容大体关于配套的PTA题集,收获以及笔记等,辛苦批改的同学可以直接翻到下划线后面的正文部分.
由于疫情的原因,本门课程或多或少的受到一些影响,诚如老师所言,我们做的作业是前一级的学生删改过的,这并不一定算一件好事,需要学生更好的去总结吸收,正因为如此这一次的blog显得更为重要,凭心而论,这几次题目集难度并不大,主要是基于电信收费,进行迭代,这已经是我们习以为常的日常操作了,除此之外就是javaFX的应用,应用在农夫过河的实验以及应用在宾馆,客房的迭代等等,最后一次blog,用两个字来形容,我觉得是设计,如何合理的发挥不同模式的优劣,如何最大程度的优化,如何满足七大设计原则等等等等
其实归总,最后的几个礼拜,是无功无过的礼拜,没有之前未掌握的领域的新知识,更多的是对于已有知识的巩固复习,不得不说,这也增强了部分的信心,话归正题,开始总结
实现一个简单的电信计费程序:
假设南昌市电信分公司针对市内座机用户采用的计费方式:
月租20元,接电话免费,市内拨打电话0.1元/分钟,省内长途0.3元/分钟,国内长途拨打0.6元/分钟。不足一分钟按一分钟计。
南昌市的区号:0791,江西省内各地市区号包括:0790~0799以及0701。
输入格式:
输入信息包括两种类型
1、逐行输入南昌市用户开户的信息,每行一个用户,
格式:u-号码 计费类型 (计费类型包括:0-座机 1-手机实时计费 2-手机A套餐)
例如:u-079186300001 0
座机号码除区号外由是7-8位数字组成。
本题只考虑计费类型0-座机计费,电信系列2、3题会逐步增加计费类型。
2、逐行输入本月某些用户的通讯信息,通讯信息格式:
座机呼叫座机:t-主叫号码 接听号码 起始时间 结束时间
t-079186330022 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:11
以上四项内容之间以一个英文空格分隔,
时间必须符合"yyyy.MM.dd HH:mm:ss"格式。提示:使用SimpleDateFormat类。
以上两类信息,先输入所有开户信息,再输入所有通讯信息,最后一行以“end”结束。
注意:
本题非法输入只做格式非法的判断,不做内容是否合理的判断(时间除外,否则无法计算),比如:
1、输入的所有通讯信息均认为是同一个月的通讯信息,不做日期是否在同一个月还是多个月的判定,直接将通讯费用累加,因此月租只计算一次。
2、记录中如果同一电话号码的多条通话记录时间出现重合,这种情况也不做判断,直接 计算每条记录的费用并累加。
3、用户区号不为南昌市的区号也作为正常用户处理。
老师给的类图如下:
关于这道题主要是要读懂类图吧,我觉得,熟练的使用spilt函数去切分输入,做分别的判断, 类图中的设计是很值得用来参考的,数据的保存,都是保存在ArrayList里。先是保存输入的用户的电话,然后再用一个ArrayList保存输入的用户的通话信息,用户电话只要把区号和电话用split给分开就行。通话信息要先用split分开各项数据,再把时间给通过simpleDateFormat给转化为Date类型,这样的设计使后面的迭代变得好做。
源码如下:

1 import java.util.Scanner; 2 import java.util.Date; 3 import java.text.ParseException; 4 import java.text.SimpleDateFormat; 5 import java.util.ArrayList; 6 public class Main { 7 public static void main(String[] args) { 8 Processing data = new Processing(); 9 Scanner in = new Scanner(System.in); 10 String arr = in.nextLine(); 11 while (true) { 12 int m=0; 13 if (inputright.inputright(arr) == 0) 14 break; 15 if (inputright.inputright(arr) == 1) { 16 String[] x = arr.split(" ");//去除空格符 17 User user=new User() ; 18 String[] num = x[0].split("-"); 19 int flag = 0; 20 for (User userc : data.getUserList()) { 21 if (userc.getNumber().equals(num[1])) 22 flag++; 23 } 24 if (flag != 0) { 25 arr = in.nextLine(); 26 continue; 27 } 28 user.setChargeMode(Processing.getMode(String.valueOf(num[1].charAt(0))));//Moda chossing 29 user.setNumber(num[1]); 30 data.AddUserList(user); 31 arr = in.nextLine(); 32 m++; 33 } 34 if (inputright.inputright(arr) == 2) { 35 // CommunicationRecord communicationRecord=new CommunicationRecord() ; 36 String[] x = arr.split("-"); 37 String[] num = x[1].split(" ");//去除空格符 38 int flag = 0; 39 // System.out.println(num[0]); 40 for (User user : data.getUserList()) { 41 if (user.getNumber().equals(num[0])) 42 flag++; 43 } 44 if (flag == 0) { 45 arr = in.nextLine(); 46 continue; 47 } 48 m++; 49 data.addRecord(num); 50 arr = in.nextLine(); 51 } 52 if(m==0) 53 arr = in.nextLine(); 54 } 55 for(int i = 0; i < data.getUserList().size();i++) { 56 int min = i; 57 for(int j = i+1; j < data.getUserList().size(); j++) { 58 if(Double.parseDouble(data.getUserList().get(j).getNumber()) < Double.parseDouble(data.getUserList().get(min).getNumber())) 59 min = j; 60 } 61 if(min != i) { 62 User temp = data.getUserList().get(min); 63 data.getUserList().set(min, data.getUserList().get(i)); 64 data.getUserList().set(i, temp); 65 } 66 } 67 for(User user : data.getUserList()) { 68 System.out.print(user.getNumber()+" "); 69 System.out.print(String.format("%.1f",user.calCost())+" "); 70 user.calBalance(); 71 System.out.println(String.format("%.1f",user.getBalance())); 72 } 73 } 74 75 static class inputright { 76 static int inputright(String s) { 77 String first = "[u]-0791[0-9]{7,8}\\s[0]"; 78 String second = "[t]-0791[0-9]{7,8}\\s" + "0[0-9]{9,11}\\s" + 79 "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.(((0?[13578]|1[02])\\.(0?" + 80 "[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" + 81 "[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" + 82 "\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\\s" + 83 "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.(" + 84 "[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" + 85 "[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" + 86 "\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])"; 87 String end="end"; 88 if (s.matches(end))return 0; 89 if (s.matches(first))return 1; 90 if (s.matches(second))return 2; 91 else return 5; 92 } 93 } 94 } 95 abstract class CallChargeRule extends ChargeRule{ 96 public abstract double calCost(ArrayList<CallRecord> callRecords); 97 // public abstract double calCost(ArrayList<MessageRecord> messageRecords); 98 } 99 100 class CallRecord extends CommunicationRecord{ 101 private Date startTime; 102 private Date endTime; 103 private String callingAddressAreaCode; 104 private String answerAddressAreaCode; 105 public void setStartTime(Date startTime) { 106 this.startTime = startTime; 107 } 108 public void setEndTime(Date endTime) { 109 this.endTime = endTime; 110 } 111 public void setCallingAddressAreaCode(String callingAddressAreaCode) { 112 this.callingAddressAreaCode = callingAddressAreaCode; 113 } 114 public void setAnswerAddressAreaCode(String answerAddressAreaCode) { 115 this.answerAddressAreaCode = answerAddressAreaCode; 116 } 117 public Date getStartTime() { 118 return startTime; 119 } 120 public Date getEndTime() { 121 return endTime; 122 } 123 public String getAnswerAddressAreaCode() { 124 return answerAddressAreaCode; 125 } 126 public String getCallingAddressAreaCode() { 127 return callingAddressAreaCode; 128 } 129 } 130 abstract class ChargeMode { 131 ArrayList<ChargeRule> chargeRules = new ArrayList<>(); 132 public void setChargeRules(ArrayList<ChargeRule> chargeRules) { 133 this.chargeRules = chargeRules; 134 } 135 public ArrayList<ChargeRule> getChargeRules() { 136 return chargeRules; 137 } 138 public abstract double calCost(UserRecords userRecords); 139 public abstract double getMonthlyRent (); 140 } 141 abstract class ChargeRule { 142 } 143 class CommunicationRecord { 144 protected String callingNumber; 145 protected String answerNumber; 146 public void setAnswerNumber(String answerNumber) { 147 this.answerNumber = answerNumber; 148 } 149 public void setCallingNumber(String callingNumber) { 150 this.callingNumber = callingNumber; 151 } 152 public String getAnswerNumber() { 153 return answerNumber; 154 } 155 public String getCallingNumber() { 156 return callingNumber; 157 } 158 } 159 class LandlinePhoneCharging extends ChargeMode { 160 private final double monthlyRent= 20; 161 @Override 162 public double calCost(UserRecords userRecords) { 163 // System.out.println("city"+new LandPhoneInCityRule().calCost(userRecords.getCallingInCityRecords())); 164 // System.out.println("Land"+new LandPhoneInCityRule().calCost(userRecords.getCallingInLandRecords())); 165 return new LandPhoneInCityRule().calCost(userRecords.getCallingInCityRecords()) 166 +new LandPhoneInProvinceRule().calCost(userRecords.getCallingInProvinceRecords()) 167 168 +new LandPhoneInLandRule().calCost(userRecords.getCallingInLandRecords()) ; 169 // + new LandPhoneInCityRule().calCost(this.userRecords.getAnswerInCityRecords()) 170 // +new LandPhoneInProvinceRule().calCost(this.userRecords.getAnswerInProvinceRecords()) 171 // +new LandPhoneInLandRule().calCost(this.userRecords.getAnswerInLandRecords()) 172 } 173 // for(CallRecord callRecord: userRecords.callingInCityRecords) { 174 // } 175 // for(CallRecord callRecord: userRecords.answerInLandRecords) { 176 //// Cost +=Processing.time( callRecord.getStartTime(), callRecord.getEndTime())*0; 177 // } 178 // for(MessageRecord messageRecord: userRecords.sendMessageRecords) { 179 // } 180 // for(MessageRecord messageRecord: userRecords.receiveMessageRecords) { 181 // } 182 // return Cost ; 183 @Override 184 public double getMonthlyRent() { 185 return monthlyRent; 186 } 187 } 188 class LandPhoneInCityRule extends CallChargeRule{ 189 @Override 190 public double calCost(ArrayList<CallRecord> callRecords) { 191 double Cost=0; 192 for(CallRecord callRecord: callRecords) { 193 Cost +=Processing.time( callRecord.getStartTime(), callRecord.getEndTime())*0.1; 194 } 195 return Cost ; 196 } 197 // public double calCost(ArrayList<MessageRecord> messageRecords) { 198 // double Cost=0; 199 // for(MessageRecord messageRecord: messageRecords) { 200 //// Cost +=Processing.time( callRecord.getStartTime(), callRecord.getEndTime())*0.1; 201 // } 202 // for(MessageRecord messageRecord: messageRecords) { 203 //// Cost +=Processing.time( callRecord.getStartTime(), callRecord.getEndTime())*0; 204 // } 205 // return Cost ; 206 // } 207 208 } 209 class LandPhoneInLandRule extends CallChargeRule { 210 @Override 211 public double calCost(ArrayList<CallRecord> callRecords) { 212 double Cost=0; 213 for(CallRecord callRecord: callRecords) { 214 Cost +=Processing.time( callRecord.getStartTime(), callRecord.getEndTime())*0.6; 215 } 216 for(CallRecord callRecord: callRecords) { 217 Cost +=Processing.time( callRecord.getStartTime(), callRecord.getEndTime())*0; 218 } 219 return Cost ; 220 } 221 } 222 class LandPhoneInProvinceRule extends CallChargeRule{ 223 @Override 224 public double calCost(ArrayList<CallRecord> callRecords) { 225 double Cost=0; 226 for(CallRecord callRecord: callRecords) { 227 Cost +=Processing.time( callRecord.getStartTime(), callRecord.getEndTime())*0.3; 228 } 229 for(CallRecord callRecord: callRecords) { 230 Cost +=Processing.time( callRecord.getStartTime(), callRecord.getEndTime())*0; 231 } 232 return Cost ; 233 } 234 } 235 class MessageRecord extends CommunicationRecord{ 236 private String message; 237 public void setMessage(String message) { 238 this.message = message; 239 } 240 public String getMessage() { 241 return message; 242 } 243 } 244 245 class Processing { 246 private ArrayList<User> userList = new ArrayList<>(); 247 public ArrayList<User> getUserList() { 248 return userList; 249 } 250 public void setUserList(ArrayList<User> userList) { 251 this.userList = userList; 252 } 253 public void AddUserList(User user) { 254 userList.add(user); 255 } 256 public void addRecord(String[] strings) { 257 String callingAddressAreaCode=strings[0].substring(0,4); 258 // System.out.println(callingAddressAreaCode); 259 String answerAddressAreaCode=strings[1].substring(0,4); 260 String StartTime= strings[2] + " " + strings[3]; 261 String EndTime= strings[4] + " " + strings[5]; 262 CallRecord addrecord = new CallRecord();//添加记录 263 addrecord.setCallingNumber(strings[0]); 264 addrecord.setAnswerNumber(strings[1]); 265 // System.out.println(StartTime); 266 addrecord.setStartTime(StringToDate.StringToDate(StartTime)); 267 addrecord.setEndTime(StringToDate.StringToDate(EndTime)); 268 addrecord.setCallingAddressAreaCode(callingAddressAreaCode); 269 addrecord.setAnswerAddressAreaCode(answerAddressAreaCode); 270 for(User user : userList) { 271 UserRecords records = user.getUserRecords(); 272 if(user.getNumber().equals(addrecord.callingNumber)) //caller 273 { 274 user.setUserRecords(Processing.callTowhere(records,addrecord,answerAddressAreaCode)); 275 } 276 if(user.getNumber().equals(addrecord.callingNumber)) //answerer 277 { 278 user.setUserRecords(Processing.answerTowhere(records,addrecord,callingAddressAreaCode)); 279 } 280 } 281 282 } 283 public static ChargeMode getMode (String s) { 284 if(s.equals("0")){ 285 return new LandlinePhoneCharging();} 286 // if (s.equals("1")){} 287 // if(s.equals("2")){} 288 else return null; 289 } 290 static UserRecords callTowhere(UserRecords records, CallRecord callRecord,String areaCode) { 291 if(areaCode.equals("0791")) 292 records.addCallingInCityRecords(callRecord); 293 else if(areaCode.matches("(079[0-9])|(0701)")) 294 records.addCallingInProvinceRecords(callRecord); 295 else if(areaCode.matches("\\d{4}")) 296 records.addCallingInLandRecords(callRecord); 297 return records; 298 } 299 static UserRecords answerTowhere(UserRecords records, CallRecord callRecord,String areaCode) { 300 if(areaCode.equals("0791") ) 301 records.addanswerInCityRecords(callRecord); 302 else if(areaCode.matches("(079[0-9])|(0701)")) 303 records.addanswerInProvinceRecords(callRecord); 304 else 305 records.addanswerInLandRecords(callRecord); 306 return records; 307 } 308 static long time(Date startTime,Date endTime) { 309 long time= (endTime.getTime()-startTime.getTime())/1000; 310 if(time%60 == 0) 311 return (long) time/60; 312 else 313 return (long)(time/60)+1; 314 } 315 } 316 317 class StringToDate { 318 static Date StringToDate(String s){ 319 Date date = null; 320 try { 321 date = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").parse(s); 322 } 323 catch (ParseException e) { 324 // TODO Auto-generated catch block 325 e.printStackTrace(); 326 } 327 return date; 328 } 329 330 } 331 class User{ 332 private UserRecords userRecords=new UserRecords(); 333 private double balance=100; 334 private ChargeMode chargeMode; 335 private String number; 336 public double calCost(){ 337 return this.chargeMode.calCost(this.userRecords); 338 } 339 public void calBalance(){ 340 this.balance -= this.calCost()+this.chargeMode.getMonthlyRent(); 341 } 342 public void setBalance(double balance) { 343 this.balance = balance; 344 } 345 public void setChargeMode(ChargeMode chargeMode) { 346 this.chargeMode = chargeMode; 347 } 348 public void setUserRecords(UserRecords userRecords) { 349 this.userRecords = userRecords; 350 } 351 public void setNumber(String number) { 352 this.number = number; 353 } 354 public ChargeMode getChargeMode() {return chargeMode;} 355 public String getNumber() { 356 return number; 357 } 358 public UserRecords getUserRecords() { 359 return userRecords; 360 } 361 public double getBalance() { 362 return balance; 363 } 364 } 365 class UserRecords { 366 ArrayList<CallRecord> callingInCityRecords = new ArrayList<>(); 367 ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<>(); 368 ArrayList<CallRecord> callingInLandRecords= new ArrayList<>(); 369 ArrayList<CallRecord> answerInCityRecords = new ArrayList<>(); 370 ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<>(); 371 ArrayList<CallRecord> answerInLandRecords= new ArrayList<>(); 372 ArrayList<MessageRecord>sendMessageRecords= new ArrayList<>(); 373 ArrayList<MessageRecord> receiveMessageRecords= new ArrayList<>(); 374 public void addCallingInCityRecords(CallRecord callRecord) { 375 callingInCityRecords.add(callRecord); 376 } 377 public void addCallingInProvinceRecords(CallRecord callRecord) { 378 callingInProvinceRecords.add(callRecord); 379 } 380 public void addCallingInLandRecords(CallRecord callRecord) { 381 callingInLandRecords.add(callRecord); 382 } 383 public void addanswerInCityRecords(CallRecord answerRecord) { 384 answerInCityRecords.add(answerRecord); 385 } 386 public void addanswerInProvinceRecords(CallRecord answerRecord) { 387 answerInProvinceRecords.add(answerRecord); 388 } 389 public void addanswerInLandRecords(CallRecord answerRecord) { 390 answerInLandRecords.add(answerRecord); 391 } 392 public void addsendMessageRecords(MessageRecord sendMessageRecord) { 393 sendMessageRecords.add(sendMessageRecord); 394 } 395 public void addreceiveMessageRecords(MessageRecord receiveMessageRecord) { 396 receiveMessageRecords.add(receiveMessageRecord); 397 } 398 public ArrayList<CallRecord> getAnswerInCityRecords() { 399 return answerInCityRecords; 400 } 401 public ArrayList<CallRecord> getAnswerInLandRecords() { 402 return answerInLandRecords; 403 } 404 public ArrayList<CallRecord> getAnswerInProvinceRecords() { 405 return answerInProvinceRecords; 406 } 407 public ArrayList<CallRecord> getCallingInCityRecords() { 408 return callingInCityRecords; 409 } 410 public ArrayList<CallRecord> getCallingInLandRecords() { 411 return callingInLandRecords; 412 } 413 public ArrayList<CallRecord> getCallingInProvinceRecords() { 414 return callingInProvinceRecords; 415 } 416 public ArrayList<MessageRecord> getReceiveMessageRecords() { 417 return receiveMessageRecords; 418 } 419 public ArrayList<MessageRecord> getSendMessageRecords() { 420 return sendMessageRecords; 421 } 422 }
7-1 电信计费系列2-手机+座机计费
实现南昌市电信分公司的计费程序,假设该公司针对手机和座机用户分别采取了两种计费方案,分别如下:
1、针对市内座机用户采用的计费方式(与电信计费系列1内容相同):
月租20元,接电话免费,市内拨打电话0.1元/分钟,省内长途0.3元/分钟,国内长途拨打0.6元/分钟。不足一分钟按一分钟计。
假设本市的区号:0791,江西省内各地市区号包括:0790~0799以及0701。
2、针对手机用户采用实时计费方式:
月租15元,市内省内接电话均免费,市内拨打市内电话0.1元/分钟,市内拨打省内电话0.2元/分钟,市内拨打省外电话0.3元/分钟,省内漫游打电话0.3元/分钟,省外漫游接听0.3元/分钟,省外漫游拨打0.6元/分钟;
注:被叫电话属于市内、省内还是国内由被叫电话的接听地点区号决定,比如以下案例中,南昌市手机用户13307912264在区号为020的广州接听了电话,主叫号码应被计算为拨打了一个省外长途,同时,手机用户13307912264也要被计算省外接听漫游费:
u-13307912264 1
t-079186330022 13307912264 020 2022.1.3 10:00:25 2022.1.3 10:05:11
对于新添加的功能,我们只需要添加一些新的类;来实现上面的功能来对相关的数据进行存储和计算,这道题目使得对象的可能变多了,座机座机,座机手机,手机手机,手机座机,此外就是手机端考虑省内省外.根据不同内容分别判定.
代码如下:

7-1 电信计费系列3-短信计费
实现一个简单的电信计费程序,针对手机的短信采用如下计费方式:
1、接收短信免费,发送短信0.1元/条,超过3条0.2元/条,超过5条0.3元/条。
2、如果一次发送短信的字符数量超过10个,按每10个字符一条短信进行计算。
输入:
输入信息包括两种类型
1、逐行输入南昌市手机用户开户的信息,每行一个用户。
格式:u-号码 计费类型 (计费类型包括:0-座机 1-手机实时计费 2-手机A套餐 3-手机短信计费)
例如:u-13305862264 3
座机号码由区号和电话号码拼接而成,电话号码包含7-8位数字,区号最高位是0。
手机号码由11位数字构成,最高位是1。
本题只针对类型3-手机短信计费。
2、逐行输入本月某些用户的短信信息,短信的格式:
m-主叫号码,接收号码,短信内容 (短信内容只能由数字、字母、空格、英文逗号、英文句号组成)
m-18907910010 13305862264 welcome to jiangxi.
m-13305862264 18907910010 thank you.
这次pta的代码长度给了限制要删去一部分代码不然就提示代码过长
短信的计费规则更简单,短信的计费的方式是根据它的发送短信的条数,以及根据它的每条短信的发送的字符数,如果一条短信的的字符数大于十个,就要额外当做另外一条信息进行处理。不算困难不多赘叙
代码如下:

import java.util.Scanner; import java.util.ArrayList; import java.util.Date; class Main { public static void main(String[] args) { Processing data = new Processing(); Scanner in = new Scanner(System.in); String arr = in.nextLine(); while (true) { if (inputright.inputright(arr) == 0) break; if (inputright.inputright(arr) == 1) { String[] x = arr.split(" ");//去除空格符 User user=new User() ; String[] num = x[0].split("-"); int flag = 0; for (User userc : data.getUserList()) { if (userc.getNumber().equals(num[1])) flag++; } if (flag != 0) { arr = in.nextLine(); continue; } user.setChargeMode(Processing.getMode(String.valueOf(num[1].charAt(0))));//Moda chossing user.setNumber(num[1]); data.AddUserList(user); } if (inputright.inputright(arr) == 6) { User user=new User() ; String[] num = arr.split("-"); String[] x = num[1].split(" ");//去除空格符 int flag = 0; for (User userc : data.getUserList()) { if (userc.getNumber().equals(num[1])) flag++; } if (flag != 0) { arr = in.nextLine(); continue; } user.setChargeMode(Processing.getMode(String.valueOf(x[1])));//Moda chossing user.setNumber(x[0]); data.setStautus(1); data.AddUserList(user); } if (inputright.inputright(arr) == 55) { String[] x = arr.split("-"); String[] num = x[1].split(" ");//去除空格符 int flag = 0; // System.out.println(num[0]); for (User user : data.getUserList()) { if (user.getNumber().equals(num[0])) flag++; } if (flag == 0) { arr = in.nextLine(); continue; } data.setStautus(10); data.addRecord(num); } if (inputright.inputright(arr) == 2) { // CommunicationRecord communicationRecord=new CommunicationRecord() ; String[] x = arr.split("-"); String[] num = x[1].split(" ");//去除空格符 int flag = 0; // System.out.println(num[0]); for (User user : data.getUserList()) { if (user.getNumber().equals(num[0])) flag++; } if (flag == 0) { arr = in.nextLine(); continue; } data.setStautus(2); data.addRecord(num); } if (inputright.inputright(arr) == 3) { // CommunicationRecord communicationRecord=new CommunicationRecord() ; String[] x = arr.split("-"); String[] num = x[1].split(" ");//去除空格符 int flag = 0; // System.out.println(num[0]); for (User user : data.getUserList()) { if (user.getNumber().equals(num[0])) flag++; } if (flag == 0) { arr = in.nextLine(); continue; } data.setStautus(3); data.addRecord(num); } if (inputright.inputright(arr) == 4) { // CommunicationRecord communicationRecord=new CommunicationRecord() ; String[] x = arr.split("-"); String[] num = x[1].split(" ");//去除空格符 int flag = 0; // System.out.println(num[0]); for (User user : data.getUserList()) { if (user.getNumber().equals(num[0])) flag++; } if (flag == 0) { arr = in.nextLine(); continue; } data.setStautus(4); data.addRecord(num); } if (inputright.inputright(arr) == 5) { // CommunicationRecord communicationRecord=new CommunicationRecord() ; String[] x = arr.split("-"); String[] num = x[1].split(" ");//去除空格符 int flag = 0; // System.out.println(num[0]); for (User user : data.getUserList()) { if (user.getNumber().equals(num[0])) flag++; } if (flag == 0) { arr = in.nextLine(); continue; } data.setStautus(5); data.addRecord(num); } arr = in.nextLine(); } for (int i = 0; i < data.getUserList().size(); i++) { int min = i; for (int j = i + 1; j < data.getUserList().size(); j++) { if (Double.parseDouble(data.getUserList().get(j).getNumber()) < Double.parseDouble(data.getUserList().get(min).getNumber())) min = j; } if (min != i) { User temp = data.getUserList().get(min); data.getUserList().set(min, data.getUserList().get(i)); data.getUserList().set(i, temp); } } for (User user : data.getUserList()) { System.out.print(user.getNumber() + " "); // System.out.println(user.getChargeMode()); System.out.print(String.format("%.1f", user.calCost()) + " "); user.calBalance(); System.out.println(String.format("%.1f", user.getBalance())); } } static class inputright { static int inputright(String s) { String sixth = ("[u]-1\\d{10}\\s[3]"); String first = "[u]-0791[0-9]{7,8}\\s[0]"; String second = "[t]-0791[0-9]{7,8}\\s" + "0[0-9]{9,11}\\s" + "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.(((0?[13578]|1[02])\\.(0?" + "[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})([48]|[2468][048]|[13579][26])|(([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]|[0-9][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})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" + "\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])"; String third = "[t]-0[0-9]{9,11}\\s1[0-9]{10}\\s0[0-9]{2,3}\\s((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9]" + "[0-9]{2}|[1-9][0-9]{3})\\.(((0?[13578]|1[02])\\.(0?[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})([48]|[2468][048]|[13579][26])|(([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]|[0-9][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})([48]|[2468][048]|" + "[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])"; String fourth = "[t]-1[0-9]{10}\\s0[0-9]{2,3}\\s1[0-9]{10}\\s0[0-9]{2,3}\\s((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|" + "[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.(((0?[13578]|1[02])\\.(0?[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})([48]|[2468][048]|[13579][26])|" + "(([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]|[0-9][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})" + "([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))\\s([0-1]?[0-9]|2[0-3])" + ":([0-5][0-9]):([0-5][0-9])"; String fifth = "[t]-1[0-9]{10}\\s0[0-9]{2,3}\\s0[0-9]{9,11}\\s((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9]" + "[0-9]{2}|[1-9][0-9]{3})\\.(((0?[13578]|1[02])\\.(0?[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})([48]|[2468][048]|[13579][26])|(([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]|[0-9][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})([48]|[2468][048]|" + "[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9])" + ":([0-5][0-9])"; String end="end"; if (s.matches(end))return 0; if (s.matches(first))return 1; if (s.matches(second))return 2; if (s.matches(third))return 3; if (s.matches(fourth))return 4; if (s.matches(fifth))return 5; if (s.matches(sixth))return 6; else return 55; } } } class CallRecord extends CommunicationRecord{ private Date startTime; private Date endTime; private String callingAddressAreaCode; private String answerAddressAreaCode; public void setStartTime(Date startTime) { this.startTime = startTime; } public void setEndTime(Date endTime) { this.endTime = endTime; } public void setCallingAddressAreaCode(String callingAddressAreaCode) { this.callingAddressAreaCode = callingAddressAreaCode; } public void setAnswerAddressAreaCode(String answerAddressAreaCode) { this.answerAddressAreaCode = answerAddressAreaCode; } public Date getStartTime() { return startTime; } public Date getEndTime() { return endTime; } public String getAnswerAddressAreaCode() { return answerAddressAreaCode; } public String getCallingAddressAreaCode() { return callingAddressAreaCode; } } class Messagecharge extends ChargeMode{ @Override public double calCost(UserRecords userRecords) { double Cost=0; int num=0; //发送短信0.1元/条,超过3条0.2元/条,超过5条0.3元/条。 for(MessageRecord messageRecord: userRecords.sendMessageRecords) { // System.out.println(messageRecord.getMessage().length()); for (int flag=messageRecord.getMessage().length();flag>0;flag-=10,num++){ if(num<=3){ Cost+=0.1; } if(num>3&&num<=5){ Cost+=0.2; } if(num>5){ Cost+=0.3; } } } return Cost ; } @Override public double getMonthlyRent() { return 0; } } class MessageRecord extends CommunicationRecord{ private String message; public void setMessage(String message) { this.message = message; } public String getMessage() { return message; } } abstract class MessageChargeRule extends ChargeRule{ public abstract double calCost(ArrayList<MessageRecord> messageRecords); } class Processing { private int stautus; private ArrayList<User> userList = new ArrayList<>(); public ArrayList<User> getUserList() { return userList; } public void setUserList(ArrayList<User> userList) { this.userList = userList; } public void AddUserList(User user) { userList.add(user); } public void setStautus(int stautus) { this.stautus = stautus; } public void addRecord(String[] strings) { if (stautus == 10) {//手机打座机 String CallingAddressAreaCode = strings[0]; // System.out.println(callingAddressAreaCode); // String answerAddressAreaCode=strings[1].substring(0,4); // String AnswerAddressAreaCode = strings[2].substring(0, 4); CallRecord addrecord = new CallRecord();//添加记录 addrecord.setCallingNumber(strings[0]); addrecord.setAnswerNumber(strings[1]); MessageRecord record = new MessageRecord(); record.setMessage(strings[2]); // System.out.println(StartTime); //市内拨打市内电话0.1元/分钟,市内拨打省内电 //话0.2元/分钟,市内拨打省外电话0.3元/分钟,省内漫游打电话(无论打哪里)0.3元/分 //钟,省外漫游接听0.3元/分钟,省外漫游拨打(无论打哪里)0.6元/分钟; for (User user : userList) { UserRecords records = user.getUserRecords(); if (user.getNumber().equals(addrecord.callingNumber)) //caller { records.addsendMessageRecords(record); } if (user.getNumber().equals(addrecord.answerNumber)) //answerer { records.addreceiveMessageRecords(record); } } } } // } public static ChargeMode getMode(String s) { if (s.equals("3")) { return new Messagecharge(); } else return null; } } class User{ private UserRecords userRecords=new UserRecords(); private double balance=100; private ChargeMode chargeMode; private String number; public double calCost(){ return this.chargeMode.calCost(this.userRecords); } public void calBalance(){ this.balance -= this.calCost()+this.chargeMode.getMonthlyRent(); } public void setBalance(double balance) { this.balance = balance; } public void setChargeMode(ChargeMode chargeMode) { this.chargeMode = chargeMode; } public void setUserRecords(UserRecords userRecords) { this.userRecords = userRecords; } public void setNumber(String number) { this.number = number; } public ChargeMode getChargeMode() {return chargeMode;} public String getNumber() { return number; } public UserRecords getUserRecords() { return userRecords; } public double getBalance() { return balance; } } class UserRecords { ArrayList<CallRecord> callingInCityRecords = new ArrayList<>(); ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<>(); ArrayList<CallRecord> callingInLandRecords= new ArrayList<>(); ArrayList<CallRecord> answerInCityRecords = new ArrayList<>(); ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<>(); ArrayList<CallRecord> answerInLandRecords= new ArrayList<>(); ArrayList<MessageRecord>sendMessageRecords= new ArrayList<>(); ArrayList<MessageRecord> receiveMessageRecords= new ArrayList<>(); public void addCallingInCityRecords(CallRecord callRecord) { callingInCityRecords.add(callRecord); } public void addCallingInProvinceRecords(CallRecord callRecord) { callingInProvinceRecords.add(callRecord); } public void addCallingInLandRecords(CallRecord callRecord) { callingInLandRecords.add(callRecord); } public void addanswerInCityRecords(CallRecord answerRecord) { answerInCityRecords.add(answerRecord); } public void addanswerInProvinceRecords(CallRecord answerRecord) { answerInProvinceRecords.add(answerRecord); } public void addanswerInLandRecords(CallRecord answerRecord) { answerInLandRecords.add(answerRecord); } public void addsendMessageRecords(MessageRecord sendMessageRecord) { sendMessageRecords.add(sendMessageRecord); } public void addreceiveMessageRecords(MessageRecord receiveMessageRecord) { receiveMessageRecords.add(receiveMessageRecord); } public ArrayList<CallRecord> getAnswerInCityRecords() { return answerInCityRecords; } public ArrayList<CallRecord> getAnswerInLandRecords() { return answerInLandRecords; } public ArrayList<CallRecord> getAnswerInProvinceRecords() { return answerInProvinceRecords; } public ArrayList<CallRecord> getCallingInCityRecords() { return callingInCityRecords; } public ArrayList<CallRecord> getCallingInLandRecords() { return callingInLandRecords; } public ArrayList<CallRecord> getCallingInProvinceRecords() { return callingInProvinceRecords; } public ArrayList<MessageRecord> getReceiveMessageRecords() { return receiveMessageRecords; } public ArrayList<MessageRecord> getSendMessageRecords() { return sendMessageRecords; } } class CommunicationRecord { protected String callingNumber; protected String answerNumber; public void setAnswerNumber(String answerNumber) { this.answerNumber = answerNumber; } public void setCallingNumber(String callingNumber) { this.callingNumber = callingNumber; } public String getAnswerNumber() { return answerNumber; } public String getCallingNumber() { return callingNumber; } } abstract class ChargeRule { } abstract class ChargeMode { ArrayList<ChargeRule> chargeRules = new ArrayList<>(); public void setChargeRules(ArrayList<ChargeRule> chargeRules) { this.chargeRules = chargeRules; } public ArrayList<ChargeRule> getChargeRules() { return chargeRules; } public abstract double calCost(UserRecords userRecords); public abstract double getMonthlyRent (); }
实验五的javafx
就是之前的农夫过河加界面
类图基本还是如下
最后结果如下开始运行后出现如下:
Start后进入选择界面
选择错误则会报错不然就接着选择
游戏成功后
-----------------------------------------------------------------------------------------
<改进建议>
pta的题目可以讲的不多,主要是要注意使用时间函数以及注意使用正则表达式来排除格式要求,以及计算是其实并不考虑时间交叉的这种情况
关于实验的改进建议的话实验报告里也有关于这个的总结,实验报告里我总结了如下不足:
第四次的实验中我一共有五个对象载人也分为带一个和带两个,选项一共存在十六个,这给我这一次的迭代带来了莫大的麻烦,注释的代码大几百行,带两个选项太多了,最后按要求留下了四个,另外就是因为我设计时使用鼠标事件setchoice方法赋值switch导致可迁移度差,showstaus也会有删除不及时,Java的事件驱动机制不熟悉,以及自学javafx时没能熟练掌握导致布局调了很久,也因此没加cry和happy接口的输出.
另外是对循环事件驱动机制导致text重叠
这里我想再多说一点,就还是归于课程本身,这几次的pta归功于老师的类图,我们的迭代不算困难,但实验五则由于实验四的设计吃尽苦头,设计,这是一个programmer绕不开也离不远的话题.
实验完成了基本的界面要求,但对于实验要求掌握的内容我觉得还是有很多可以进步的空间,同样,课程也是如此.
-----------------------------------------------------------------------------------------