首页 > 编程语言 >Java题目集总结6~8

Java题目集总结6~8

时间:2022-12-06 20:45:38浏览次数:39  
标签:总结 题目 String double ArrayList Java new return public

1.前言

  经过这个阶段的学习,我们学习了很多具有拓展性、延申性的知识,需要学习和巩固的知识点也还有很多,此次是最后一次blog总结,但是对我们的Java学习之路来说才刚开始。。。 (( ´Д`)y━・~~)。相较于在第一、二阶段的作业而言,此次作业更加针对于在面向对象过程中的三大技术特性,即封装性、继承性和多态性,类图的设计更加普遍。在不断深入学习Java的过程又延伸到了Javafx,不仅能把图片放在窗口还可以通过Java代码的撰写来组合创建,充分发挥自我的想象。通过学习类属性的访问权限,了解到了四种访问权限:public,private,protected,default;以及不同类的类型:实体类,业务类,接口类;对于类设计上,学习的多,代表思路与编程思想变得更为开阔且严谨,开始真正考虑到如何将一个题目设计到恰如其分,不同的设计思路带来的不同效果。对于这三次的题目集来讲,第一次难度较为大,但依照老师题中所给的类图编写代码,难度相对降低了不少,后面两次也逐渐简单,耗费的时间也相比第一次少了不少,这几次题目集的数量较少,基本为两到三道题。题目中所用到的主要知识点为容器的存储与输出,同时还有正则表达式的运用,Date类里面方法的使用和Date类型转化为long类型进行计算以及利用接口排序的方法等等。

2.设计与分析:

7-1 电信计费系列1-座机计费:

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

 代码分析与处理:

  此题的难度在于不好下手,做此类图较为复杂的题,需要仔细观察类图,明确类图之间的联系。主要考查了抽象类,继承与多态等知识,由于给出了类图,题目变得更简单。同时,在本次的类图框架搭建的基础上,增加一个输入判断类,能为读入数据的判断提供巨大的帮助,从而使得题目的数据判断更为清晰,能为后续题目的扩展带来巨大的帮助。而在此题中 User类中的UserRecord是这道题目的突破口,在这个UserRecord中可以把用户的通话记录数据放入,以便于ChargeMode在中间的调用。然后再去理解类图中的ChargeMode类,这是这个程序的核心代码,通过这一段计算出用户的花费费用,以完成程序。虽然类图看起来非常的复杂,但只要花费足够的时间将代码分解先从User入手,再写到了UserRecord,接着就补充到ChargeMode,再然后就去构思Main函数,获取输入数据即可实现座机的计费。同时注意计费方式中不足一分钟需要按一分钟进行计算,以及在不同打电话场景下的计费规则的不同都需要进行不同的判断。

 类图:

 分析报告:

 7-1 电信计费系列2-手机+座机计费:

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

代码分析与处理:

  此题为上一题的进阶题,相对于单纯的座机计费新增加了手机计费的功能,包括四种类型的计费,座机打座机,座机打手机,手机打座机,手机打手机,计费方式也新增加了漫游,类型更多种,例如有关手机计费:MobilePhoneInCityRule、MobilePhoneInlandRule、MobilePhoneInProvinceRule等。在上题的基础上,增加储存手机用户的容器,增加相应手机计费规则,以及判断手机通讯信息的正则表达式,储存手机用户的方式和上题储存座机用户的方式上基本一致,根据拨打人所在区号根据接听人的区号判断是市内省内还是国内长途信息等类型,同时在用户信息里建立对应的容器储存它们。以及由于在计费方面,存在座机打手机,手机打手机,手机打座机等,所以在数据储存时,他们的信息格式各不相同,应当对不同的分类具体问题进行分析。同时本题中还加入了更多非常复杂的格式判定,对正则表达式的理解和掌握程度有非常大的考察力度。不过首先还是按照先前建立好的类图并加以适当修改,在第一题的基础上按照同样的方式加入手机通信的付费规则等需要增添的类即可。

类图:

分析报告:

 7-1 电信计费系列3-短信计费:

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

代码分析与处理:

  短信计费,相对于前面两次,难度没有这么大,计费类型也和前两次不一样,前两次是计算每次的通讯费用再相加,而此次的短信计费则必须先算总的短信条数,然后再计算短信的费用,类设计也可在原来的基础上进行增加补充修改(此时一开始在前面设计好清晰的类图结构,就会使得题目变得更加简单易操作)。本题最关键的是判断短信内容的长度,由于信息输入格式的格式为:m-主叫号码 接收号码 短信内容,而m-主叫号码 接收号码 的长度确定,可用substring方法将后面的短信内容全部截取,再判断长度将对应的记录读取即可,之后利用对应的计费规则统计计算,同时注意一次发送短信的字符数量超过10个,按每10个字符一条短信进行叠加。

类图:

 分析报告:

 

3.踩坑心得:

  1.在这一阶段题目集中,在7-1电信计费系列1-座机计费里,需添加判断座机的最高位应为0以及关于相关的日期的判断须符合格式例如2022:09:30,月份的判断需要符合格式例如单位数前者不能含多余的零以及每个月份的天数需符合对应的月份数。

  2.在7-1 电信计费系列2-手机+座机计费中,关于座机号及手机号的判断,座机号最高位为0,手机号最高位为1同时手机号第二位只能为[3-9],对输入的相关的格式进行更近一步的判断。避免对于类和方法漏写和判断情况缺失的判断,从而导致容易漏掉使得测试点报错。

  3.7-1 电信计费系列3-短信计费中,注意相关的计费规则以及对特殊短信(全为空格)的短信内容判断方式以及短信内容只能由数字、字母、空格、英文逗号、英文句号等组成,加上添加对应的信息判断格式。

  4.这三次电信计费练习都涉及了许多有关输入有效性的判断。在第一题时,比较专注于时间输入有效性的判断,对SimpleDateFormat类的不了解也导致第一题使用了错误的方法判断的时间,对其余部分的格式判断也简单判断,使用正则表达式判断,能够使得座机计费格式判断更加全面。在手机+座机计费这一题时,在学会使用SimpleDateFormat类进行时间判断后,又因为不了解什么样的输入可以被SimpleDateFormat转化,不符合yyyy.MM.dd HH:mm:ss的模式判断,好在后面得以发现问题解决,第三次短信收费计算则是在有了前两次的总结,写的时候就相对比较顺利。

4.改进建议:

  在这几次题目集的过程在,由于对于对正则表达式的理解匮乏,导致对于一些输入的判断出现错误。正则表达式是经常应用的一种判断输入的方法,对于建立的正则表达式的外围一定要添加“ ”。在这个阶段的学习中,我们在类与对象、方法基本构建、继承、多态的使用的基础上,新学习并着重强化了对抽象类、抽象方法的构造,接口的加入,还有JavaFX的使用。加上对于新知识的掌握并不完全,使用起来不够熟练,需要清楚地知道类间关系,知道每个类中方法的作用。做题时需要先理清思路再写程序,避免逻辑上的错误。以及当发现自己当前的逻辑方法完成不了题目时,可以尝试重新换个思路,以免执行不下去。做题时查缺补漏,以免下次还要去查前面的bug。。。。 对于题目要求的排序输出,可以运用集合本身具有的排序方法,添加对应的comparable接口比较即可。 对于一个问题可以思考灵活的方法变通,设计类做到可扩展性,增加功能或减少功能时不必修改太多的代码,灵活的方法也不需要太多的代码来解决一个问题。加强相关知识的联系与应用。

五、总结

       经过一个学期的Java课程学习,使得我对于Java,了解、探索以及认知这门完全面向对象的语言。从一开始的懵懂,到现在能够自己编写一个简单的程序,学习应该是循环渐进,脚踏实地,一步一步循序渐进进步。学习Java,对于书上的概念也要理解透彻,多和同学沟通,互相学习,取长补短,多与别人进行交流。Java基础课程已经结束,但对于Java知识的学习与提升才刚刚开始。更熟练的使用Java,更好的类关系设计将作为之后学习的目标与方向。相较于Java宏大的知识体系,所学的知识不过凤毛麟角,更加丰富的知识还尚未来到。不断学习,不断探索,虚心学习,勤于练习。在今后课程的学习中,我们要学会着重分析题目需求、弄懂类中包含的内容以及方法的编写,搞清各个功能代码块之间的联系。通过进一步Java课程的学习,使得我对Java这一门编程语言有了本质上的了解,也明白了类与对象作为Java语言内核的原因。在不断吸收新知的同时,也发现了自己在代码编程方面还有很多的不足,应在未来继续进行改正,不断进步。

标签:总结,题目,String,double,ArrayList,Java,new,return,public
From: https://www.cnblogs.com/2992616blogs/p/16956055.html

相关文章

  • 洛谷月赛简单题目选做
    简单题目指黄+~蓝P5888传球游戏Easy考虑朴素dp,设\(dp[i][j]\),表示第\(j\)轮球在\(i\)手中的方案数,时间复杂度\(O(nm)\)。观察到如果两个人均不是\(1\)号......
  • idea java 关于mysql数据库连接的相关步骤
    1.首先在你的项目中建立一个lib文件夹(表示加入依赖)(资源在上面)       2.选择这个项目的Properties在下面页面中添加Libraries进入这个项目jar配置到项......
  • java基础之标识符与数据类型
    一标识符1.以字母,下划线,美元符开始。2.不能使用关键字作为变量名与方法名。3.标识符是大小敏感的。二数据类型A.整数类型:1.byte占一个字节其范围为-127——128,超......
  • hdu1253 胜利大逃亡--BFS & BFS的总结
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1253​​一:题意一个三维A*B*C,起点(0,0,0),终点(A-1,B-1,C-1),求在t时间内(包括t)能否到达?可以,输出花费的最少时间,否则......
  • 1.5 HDFS分布式文件系统-hadoop-最全最完整的保姆级的java大数据学习资料
    目录1.5HDFS分布式文件系统1.5.1HDFS简介1.5.2HDFS的重要概念1.5.3HDFS架构1.5HDFS分布式文件系统1.5.1HDFS简介HDFS(全称:HadoopDistributeFileSystem,Hadoop......
  • tomcat_动态java项目的目录结构与tomcat_与IDEA集成&创建web项目
    tomcat_动态java项目的目录结构静态项目和动态项目目录结构java动态项目的目录结构:--项......
  • 智慧自贸,美华半年度总结计划会议成功召开
    版权声明:本文章由“上海美华系统有限公司”编辑组汇编而成,未经授权和许可,任何个人或媒体不得对本网站的文章及其他信息资料予以复制、转载、抄袭、改编。上海美华系统有限......
  • NOIP2022 总结
    一定要先把可能写出的正解写好了再去打别的暴力(时间不足导致T3建造军营推出式子但没时间写\(100\to0\))。特殊的多测不清空(T1种花未清空读入时的字符串数组\(100\t......
  • java文件传输简单方法
    java文件传输简单方法假设现在已经打包了一个文件(1233444333),要将这个文件传输给另一方:importjava.io.*;publicclassF_PasswordUnPassword{publicstaticvoidmain(S......
  • Javascript
    内容概要BOM操作BOM操作基础DOM操作jQuery类库BOM操作BOM操作基础BOM(BrowserObjectModel)是指浏览器对象模型,它使用Javascript有能力与浏览器进行'对话'......