渐入佳境--我的第三次Blog作业

渐入佳境--我的第三次Blog作业

我的第三次Blog作业

一.前言--说说自己对题的总结;

1.题目集07

这个题目集里就两题,题量很少,但每个都很难,这也是点线形系列计算的迭代。需要满足五边形的各个判定条件。超级难,我前几次迭代几乎都没满分过,这一次对于我来说也超级得难,很诚实的说,有的时候是为了测试样例去完善代码,这样对于我的提升可以忽略不计。

我学到了:数学知识倒是提升了,我利用线性代数里的矩阵做的关于是否为五边形的判断,还有判断每两条线是不是在同一条直线上。

2.题目集08

这个题目集也只有两题,题量很少,后一题多态测试简直就是送分,代码都已经写好了,就需要你去修改一下。当然,第一题是根据老师给的需求与类图,做出一个符合SOLID原则的优质代码,从这一次开始,我才明白什么是真正的优质代码,优质代码该如何去写,这也是话费计算的第一次迭代--座机收费。类图都已经给你了,只需要完善与调用便可以写出一份优秀的作业。

我学到了:一份真正的优质代码如何去写,如何去设计,如何做出一个符合SOLID原则代码。同时,正则表达式与split()方法也加深了理解。哦对,还有simpleDate方法。这是一个更加完善的计算时间的方式。有类似于正则的功能,也有计算时间之差的功能。

3.题目集09

类似于题目集08,本题目集后两次就像是老师发现我们对数据排序不太擅长专门做的题,或者本身就想考考我们对于set还有Iterator的使用,就像题目集08里有一个输出要求,是按照号码的排序大小从小到大输出。第一题便是手机收费的第二次迭代,它分的情况更多

比题目集08要稍微难一点,但是说实话,也就是多加几条情况,多加几条正则的事,毕竟地基打的牢,迭代完全不怕。

我学到了;提高了使用set方法的熟练度,存入排序可以用set方法里的treeset,重复的数据会被删除,并且会按照从小到大输出。

4.题目集10

后两题分别考查练习内部类和抽象类多态,不太难,但我感觉内部类那一道题,好像不太符合题目要求,但是我也没有别的好办法。这两题是送分题。剩下的那道便是通信收费的最后一次迭代--短信收费。短信收费的正则比之前两个更难以表达一些,在查阅了大量csdn的博客之后实现了正则,之后也没什么好说的,不过类图与类稍微发生了改变,需要添加关于短信收费信息与短信收费方式的类。

我学到了:内部类的调用,抽象类多态的实现,提高了正则的使用熟练度。

二.关于通信收费系列的设计与分析.

我们把对应的开户信息,开户,存入数组,下面输出会用到。然后对应通讯信息存入对于的类里的数组。然后计费方法调用这些信息的数组进行计费。输出消费与剩余话费。

首先是输入:

我的正则主要是检测输入的nextLine()整体,依据的是字母与数字的位数。

用split()分割“/t”,获得各个信息。在这里需要提的是,相比较与其他同学的数十行的正则,我用了几行就可以完成,主要是simpleDate自带的检测时间输入是否正确。需要注意的是,月份前不能加0,如一月不能写成01月。时间必须是两位数,如01:01:01是正确输入。

其次是主方法里的判断条件

对应了正则,就进入对应方法。对于要检测的数据,有时候可能是主叫号码,也有可能是被叫号码。而且存入得不同的对应的通讯信息。

最后是输入需要提一下,输出时需要按照号码的大小排序输出。还有计费需要保留两位小数。

类图:

 

 

 

 

 

 

 

 

 代码:(已折叠)

  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 
  7 public class Main {
  8     public static void main(String[] args) throws ParseException{
  9         Scanner in = new Scanner(System.in);
 10         SimpleDateFormat s = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
 11         String regex8 = "m-1[0-9]{10} 1[0-9]{10} [A-Za-z0-9., ]+";
 12         String regex7 = "u-1[0-9]{10} 3";
 13         String num1;
 14         String num2;
 15         String message;
 16         String ReGex = " ";
 17         ArrayList<User> users = new ArrayList<>();
 18         while (true) {
 19             String string = in.nextLine();
 20             if(string.equals("end"))
 21                 break;
 22             if(string.matches(regex7) && users.size() == 0) {
 23                 num1 = string.substring(2,string.indexOf(" "));
 24                 User user = new User(num1);
 25                 user.setChargeMode(new SendMessageCharging());
 26                 users.add(user);
 27             }
 28             else if(string.matches(regex7) && users.size() > 0) {
 29                 boolean b = true;
 30                 num1 = string.substring(2,string.indexOf(" "));
 31                 for (User value : users) {
 32                     if (value.getNumber().equals(num1)) {
 33                         b = false;
 34                         break;
 35                     }
 36                 }
 37                 if(b) {
 38                     User user = new User(num1);
 39                     user.setChargeMode(new SendMessageCharging());
 40                     users.add(user);
 41                 }
 42             }
 43             else {
 44                 String[] arr = string.split(ReGex);
 45                 if(string.matches(regex8)) {
 46                     num1 = arr[0].substring(2);
 47                     num2 = arr[1];
 48                     message = string.substring(26);
 49                     int k = 0;
 50                     for(int i = 0;i < users.size();i ++)
 51                         if(users.get(i).getNumber().equals(num1)) {
 52                             k = i;
 53                             break;
 54                         }
 55                     MessageRecord messageRecord = new MessageRecord();
 56                     messageRecord.setMessage(message);
 57                     users.get(k).getUserRecords().addSendMessageRecords(messageRecord);
 58                 }
 59             }
 60         }
 61         User[] users1 = new User[users.size()];
 62         for(int i = 0;i < users.size() ; i++)
 63             users1[i] = users.get(i);
 64         for(int i = 0;i < users.size() - 1;i ++) {
 65             for(int j = i + 1;j < users.size();j ++) {
 66                 User user;
 67                 if(users1[j].getNumber().compareTo(users1[i].getNumber()) < 0) {
 68                     user = users1[j];
 69                     users1[j] = users1[i];
 70                     users1[i] = user;
 71                 }
 72             }
 73         }
 74         for(int i = 0;i < users.size();i ++) {
 75             System.out.print(users1[i].getNumber() + " ");
 76             System.out.printf("%.1f ",users1[i].calCost());
 77             System.out.printf("%.1f\n",users1[i].calBalance());
 78         }
 79     }
 80 }
 81 class MobilePhoneCharging extends ChargeMode {
 82     MobilePhoneInCityRule mobilePhoneInCityRule = new MobilePhoneInCityRule();
 83     MobilePhoneInProvinceRule mobilePhoneInProvinceRule = new MobilePhoneInProvinceRule();
 84     MobilePhoneInLandRule mobilePhoneInLandRule = new MobilePhoneInLandRule();
 85     MobilePhoneRoamingChargeRule mobilePhoneRoamingChargeRule =new MobilePhoneRoamingChargeRule();
 86     @Override
 87     public double calCost(UserRecords userRecords) {
 88         return mobilePhoneInCityRule.calCost(userRecords.getCallingInCityRecords()) +
 89                 mobilePhoneInProvinceRule.calCost(userRecords.getCallingInProvinceRecords()) +
 90                 mobilePhoneInLandRule.calCost(userRecords.getCallingInLandRecords()) +
 91                 mobilePhoneRoamingChargeRule.calCost(userRecords.getCallingInLandRoamingRecords(), userRecords.getAnswerInLandRoamingRecords()) +
 92                 mobilePhoneRoamingChargeRule.calCost(userRecords.getCallingInProvinceRoamingRecords());
 93     }
 94     @Override
 95     public double getMonthlyRent() {
 96         return 15;
 97     }
 98 }
 99 class MobilePhoneRoamingChargeRule extends CallChargeRule {
100     @Override
101     public double calCost(ArrayList<CallRecord> callRecords1,ArrayList<CallRecord> callRecords2){
102         double cost = 0;
103         for(int i = 0;i < callRecords1.size();i ++) {
104             cost = callRecords1.get(i).getDuration() * 0.6 + cost;
105         }
106         for(int i = 0;i < callRecords2.size();i ++) {
107             cost = callRecords2.get(i).getDuration() * 0.3 + cost;
108         }
109         return cost;
110     }
111     @Override
112     public double calCost(ArrayList<CallRecord> callRecords1) {
113         double cost = 0;
114         for(int i = 0;i < callRecords1.size();i ++) {
115             cost = callRecords1.get(i).getDuration() * 0.3 + cost;
116         }
117         return cost;
118     }
119 }
120 class User {
121     private UserRecords userRecords = new UserRecords();
122     private double balance = 100;
123     private ChargeMode chargeMode;
124     private String number;
125     public User(String number) {
126         this.number = number;
127     }
128     public double calBalance() {
129         return getBalance() - calCost() - chargeMode.getMonthlyRent();
130     }
131     public double calCost() {
132         return chargeMode.calCost(getUserRecords()) ;
133     }
134     public ChargeMode getChargeMode() {
135         return chargeMode;
136     }
137     public void setChargeMode(LandlinePhoneCharging landlinePhoneCharging) {
138         chargeMode = landlinePhoneCharging;
139     }
140     public void setChargeMode(MobilePhoneCharging mobilePhoneCharging) {
141         chargeMode = mobilePhoneCharging;
142     }
143     public void setChargeMode(SendMessageCharging sendMessageCharging) {
144         chargeMode = sendMessageCharging;
145     }
146     public UserRecords getUserRecords() {
147         return userRecords;
148     }
149     public void setUserRecords(UserRecords userRecords) {
150         this.userRecords = userRecords;
151     }
152     public double getBalance() {
153         return balance;
154     }
155     public String getNumber() {
156         return number;
157     }
158     public void setNumber(String number) {
159         this.number = number;
160     }
161 }
162 
163 abstract class CallChargeRule {
164     public double calCost(ArrayList<CallRecord> callRecords){
165         return 0;
166     }
167     public double calCost(ArrayList<CallRecord> callRecords1,ArrayList<CallRecord> callRecords2){
168         return 0;
169     }
170 }
171 class CallRecord extends CommunicationRecord{
172     private Date startTime;
173     private Date endTime;
174     private String callingAddressAreaCode;
175     private String answerAddressAreaCode;
176     public double getDuration() {
177         long sec = (getEndTime().getTime() - getStartTime().getTime()) / 1000 / 60;
178         long m = (getEndTime().getTime() - getStartTime().getTime()) / 1000;
179         if(m %100 != 0)
180             sec ++;
181         return sec;
182     }
183     public Date getStartTime() {
184         return startTime;
185     }
186     public void setStartTime(Date startTime) {
187         this.startTime = startTime;
188     }
189     public Date getEndTime() {
190         return endTime;
191     }
192     public void setEndTime(Date endTime) {
193         this.endTime = endTime;
194     }
195     public String getCallingAddressAreaCode() {
196         return callingAddressAreaCode;
197     }
198     public void setCallingAddressAreaCode(String callingAddressAreaCode) {
199         this.callingAddressAreaCode = callingAddressAreaCode;
200     }
201     public String getAnswerAddressAreaCode() {
202         return answerAddressAreaCode;
203     }
204     public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
205         this.answerAddressAreaCode = answerAddressAreaCode;
206     }
207 }
208 abstract class ChargeMode {
209     private ArrayList<ChargeRule> chargeRules = new ArrayList<>();
210     public abstract double calCost(UserRecords userRecords);
211     public abstract double getMonthlyRent();
212 
213     public ArrayList<ChargeRule> getChargeRules() {
214         return chargeRules;
215     }
216     public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
217         this.chargeRules = chargeRules;
218     }
219 }
220 abstract class ChargeRule {
221 }
222 abstract class CommunicationRecord {
223     protected String callingNumber;
224     protected String answerNumber;
225     public String getCallingNumber() {
226         return callingNumber;
227     }
228     public void setCallingNumber(String callingNumber) {
229         this.callingNumber = callingNumber;
230     }
231     public String getAnswerNumber() {
232         return answerNumber;
233     }
234     public void setAnswerNumber(String answerNumber) {
235         this.answerNumber = answerNumber;
236     }
237 }
238 class LandlinePhoneCharging extends ChargeMode{
239     LandPhoneInCityRule landPhoneInCityRule = new LandPhoneInCityRule();
240     LandPhoneInProvinceRule landPhoneInProvinceRule = new LandPhoneInProvinceRule();
241     LandPhoneInLandRule landPhoneInLandRule = new LandPhoneInLandRule();
242 
243     @Override
244     public double calCost(UserRecords userRecords) {
245         return landPhoneInCityRule.calCost(userRecords.getCallingInCityRecords()) +
246                 landPhoneInProvinceRule.calCost(userRecords.getCallingInProvinceRecords()) +
247                 landPhoneInLandRule.calCost(userRecords.getCallingInLandRecords()) ;
248     }
249     @Override
250     public double getMonthlyRent() {
251         return 20;
252     }
253 }
254 
255 class MobilePhoneInCityRule extends CallChargeRule {
256     @Override
257     public double calCost(ArrayList<CallRecord> callRecords1) {
258         double cost = 0;
259         for(int i = 0;i < callRecords1.size();i ++) {
260             cost = callRecords1.get(i).getDuration() * 0.1 + cost;
261         }
262         return cost;
263     }
264 }
265 class MobilePhoneInProvinceRule extends CallChargeRule {
266     @Override
267     public double calCost(ArrayList<CallRecord> callRecords1) {
268         double cost = 0;
269         for(int i = 0;i < callRecords1.size();i ++) {
270             cost = callRecords1.get(i).getDuration() * 0.2 + cost;
271         }
272         return cost;
273     }
274 }
275 class MobilePhoneInLandRule extends CallChargeRule {
276     @Override
277     public double calCost(ArrayList<CallRecord> callRecords1) {
278         double cost = 0;
279         for(int i = 0;i < callRecords1.size();i ++) {
280             cost = callRecords1.get(i).getDuration() * 0.3 + cost;
281         }
282         return cost;
283     }
284 }
285 class LandPhoneInCityRule extends CallChargeRule{
286     @Override
287     public double calCost(ArrayList<CallRecord> callRecords) {
288         double cost = 0;
289         for(int i = 0;i < callRecords.size();i ++) {
290             cost = callRecords.get(i).getDuration() * 0.1 + cost;
291         }
292         return cost;
293     }
294 }
295 class LandPhoneInLandRule extends CallChargeRule{
296     @Override
297     public double calCost(ArrayList<CallRecord> callRecords) {
298         double cost = 0;
299         for(int i = 0;i < callRecords.size();i ++) {
300             cost = callRecords.get(i).getDuration() * 0.6 + cost;
301         }
302         return cost;
303     }
304 }
305 class LandPhoneInProvinceRule extends CallChargeRule{
306     @Override
307     public double calCost(ArrayList<CallRecord> callRecords) {
308         double cost = 0;
309         for(int i = 0;i < callRecords.size();i ++) {
310             cost = callRecords.get(i).getDuration() * 0.3 + cost;
311         }
312         return cost;
313     }
314 }
315 abstract class MessageChargeRule extends ChargeRule {
316     public abstract double calCost(ArrayList<MessageRecord> messageRecords);
317 }
318 class SendMessageRule extends MessageChargeRule {
319     @Override
320     public double calCost(ArrayList<MessageRecord> messageRecords) {
321         double cost = 0;
322         int count = 0;
323         for(int i = 0;i < messageRecords.size();i++){
324             int num = messageRecords.get(i).getMessage().length() + 9;
325             for(; num / 10 > 0; num -= 10){
326                 count ++;
327                 if(num != 0 )
328                 {
329                     if (count < 4)
330                         cost = cost + 0.1;
331                     else if (count < 6)
332                         cost = cost + 0.2;
333                     else
334                         cost = cost + 0.3;
335                 }
336             }
337         }
338         return cost;
339     }
340 }
341 class SendMessageCharging extends ChargeMode{
342     SendMessageRule sendMessageRule = new SendMessageRule();
343     @Override
344     public double calCost(UserRecords userRecords) {
345         return sendMessageRule.calCost(userRecords.getSendMessageRecords());
346     }
347 
348     @Override
349     public double getMonthlyRent() {
350         return 0;
351     }
352 }
353 class MessageRecord extends CommunicationRecord{
354     private String message;
355     public String getMessage() {
356         return message;
357     }
358     public void setMessage(String message) {
359         this.message = message;
360     }
361 }
362 class UserRecords {
363     private ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();
364     private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();
365     private ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();
366     private ArrayList<CallRecord> callingInProvinceRoamingRecords = new ArrayList<CallRecord>();
367     private ArrayList<CallRecord> callingInLandRoamingRecords = new ArrayList<CallRecord>();
368     private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();
369     private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();
370     private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();
371     private ArrayList<CallRecord> answerInLandRoamingRecords = new ArrayList<CallRecord>();
372     private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();
373     private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();
374 
375     public void addCallingInCityRecords(CallRecord callRecord) {
376         callingInCityRecords.add(callRecord);
377     }
378     public void addCallingInProvinceRecords(CallRecord callRecord) {
379         callingInProvinceRecords.add(callRecord);
380     }
381     public void addCallingInLandRecords(CallRecord callRecord) {
382         callingInLandRecords.add(callRecord);
383     }
384     public void addCallingInProvinceRoamingRecords(CallRecord callRecord) {
385         callingInProvinceRoamingRecords.add(callRecord);
386     }
387     public void addCallingInLandRoamingRecords(CallRecord callRecord) {
388         callingInLandRoamingRecords.add(callRecord);
389     }
390     public void addAnswerInCityRecords(CallRecord callRecord) {
391         answerInCityRecords.add(callRecord);
392     }
393     public void addAnswerInProvinceRecords(CallRecord callRecord) {
394         answerInProvinceRecords.add(callRecord);
395     }
396     public void addAnswerInLandRecords(CallRecord callRecord) {
397         answerInLandRecords.add(callRecord);
398     }
399     public void addAnswerInLandRoamingRecords(CallRecord callRecord) {
400         answerInLandRoamingRecords.add(callRecord);
401     }
402     public void addSendMessageRecords(MessageRecord messageRecord) {
403         sendMessageRecords.add(messageRecord);
404     }
405     public void addReceiveMessageRecords(MessageRecord messageRecord) {
406         receiveMessageRecords.add(messageRecord);
407     }
408     public ArrayList<CallRecord> getCallingInCityRecords() {
409         return callingInCityRecords;
410     }
411     public ArrayList<CallRecord> getCallingInProvinceRecords() {
412         return callingInProvinceRecords;
413     }
414     public ArrayList<CallRecord> getCallingInLandRecords() {
415         return callingInLandRecords;
416     }
417     public ArrayList<CallRecord> getAnswerInCityRecords() {
418         return answerInCityRecords;
419     }
420     public ArrayList<CallRecord> getAnswerInProvinceRecords() {
421         return answerInProvinceRecords;
422     }
423     public ArrayList<CallRecord> getAnswerInLandRecords() {
424         return answerInLandRecords;
425     }
426     public ArrayList<CallRecord> getCallingInProvinceRoamingRecords() {
427         return callingInProvinceRoamingRecords;
428     }
429     public ArrayList<CallRecord> getCallingInLandRoamingRecords() {
430         return callingInLandRoamingRecords;
431     }
432     public ArrayList<CallRecord> getAnswerInLandRoamingRecords() {
433         return answerInLandRoamingRecords;
434     }
435     public ArrayList<MessageRecord> getSendMessageRecords() {
436         return sendMessageRecords;
437     }
438     public ArrayList<MessageRecord> getReceiveMessageRecords() {
439         return receiveMessageRecords;
440     }
441 }
View Code

 

 

 

三.我的踩坑心得.

 

大部分问题出现在了正则及计费信息没有存入上面。

1. 正则里出错的大概有三个部分。一个是时间虽然用simpleDate,但是它不会帮你注意的是,月份前不能加0,如一月不能写成01月。一个是时间必须是两位数,如01:01:01是正确输入。还有一个是区号可以为三位数,比如北京为010,当时我的正则是按照四位数,测试点没过。最后在通讯收费里,空格也算是输入一部分。

2. 计费信息里,需要额外创建一个userRecord对象,然后用set方法存入信息各种。否则将不会被录入信息。也不会被计费。

3. 正则表达式的测试工具很难识别%d,%s等,最好用[0-9][a-zA-Z]来代替测试。

4. 正则和string自带方法可以灵活使用,不一定非得用某一种。

四.建议.

按我的意思,以前作业都按这样发布,我会高兴死,难度适中且能学到不少有用的东西。都按照这样出题我没有任何意见和建议。

五总结一下下.

这次通讯计费的迭代我学到超级多东西,感觉这个学期下来,前面的时间都白费了,这一次学到的比以前加一起都多,也让我喜欢上了Java这个优秀的语言。马上就结束作业了,开心,激动。

 

 

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

public class Main {
public static void main(String[] args) throws ParseException{
Scanner in = new Scanner(System.in);
SimpleDateFormat s = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
String regex8 = "m-1[0-9]{10} 1[0-9]{10} [A-Za-z0-9., ]+";
String regex7 = "u-1[0-9]{10} 3";
String num1;
String num2;
String message;
String ReGex = " ";
ArrayList<User> users = new ArrayList<>();
while (true) {
String string = in.nextLine();
if(string.equals("end"))
break;
if(string.matches(regex7) && users.size() == 0) {
num1 = string.substring(2,string.indexOf(" "));
User user = new User(num1);
user.setChargeMode(new SendMessageCharging());
users.add(user);
}
else if(string.matches(regex7) && users.size() > 0) {
boolean b = true;
num1 = string.substring(2,string.indexOf(" "));
for (User value : users) {
if (value.getNumber().equals(num1)) {
b = false;
break;
}
}
if(b) {
User user = new User(num1);
user.setChargeMode(new SendMessageCharging());
users.add(user);
}
}
else {
String[] arr = string.split(ReGex);
if(string.matches(regex8)) {
num1 = arr[0].substring(2);
num2 = arr[1];
message = string.substring(26);
int k = 0;
for(int i = 0;i < users.size();i ++)
if(users.get(i).getNumber().equals(num1)) {
k = i;
break;
}
MessageRecord messageRecord = new MessageRecord();
messageRecord.setMessage(message);
users.get(k).getUserRecords().addSendMessageRecords(messageRecord);
}
}
}
User[] users1 = new User[users.size()];
for(int i = 0;i < users.size() ; i++)
users1[i] = users.get(i);
for(int i = 0;i < users.size() - 1;i ++) {
for(int j = i + 1;j < users.size();j ++) {
User user;
if(users1[j].getNumber().compareTo(users1[i].getNumber()) < 0) {
user = users1[j];
users1[j] = users1[i];
users1[i] = user;
}
}
}
for(int i = 0;i < users.size();i ++) {
System.out.print(users1[i].getNumber() + " ");
System.out.printf("%.1f ",users1[i].calCost());
System.out.printf("%.1f\n",users1[i].calBalance());
}
}
}
class MobilePhoneCharging extends ChargeMode {
MobilePhoneInCityRule mobilePhoneInCityRule = new MobilePhoneInCityRule();
MobilePhoneInProvinceRule mobilePhoneInProvinceRule = new MobilePhoneInProvinceRule();
MobilePhoneInLandRule mobilePhoneInLandRule = new MobilePhoneInLandRule();
MobilePhoneRoamingChargeRule mobilePhoneRoamingChargeRule =new MobilePhoneRoamingChargeRule();
@Override
public double calCost(UserRecords userRecords) {
return mobilePhoneInCityRule.calCost(userRecords.getCallingInCityRecords()) +
mobilePhoneInProvinceRule.calCost(userRecords.getCallingInProvinceRecords()) +
mobilePhoneInLandRule.calCost(userRecords.getCallingInLandRecords()) +
mobilePhoneRoamingChargeRule.calCost(userRecords.getCallingInLandRoamingRecords(), userRecords.getAnswerInLandRoamingRecords()) +
mobilePhoneRoamingChargeRule.calCost(userRecords.getCallingInProvinceRoamingRecords());
}
@Override
public double getMonthlyRent() {
return 15;
}
}
class MobilePhoneRoamingChargeRule extends CallChargeRule {
@Override
public double calCost(ArrayList<CallRecord> callRecords1,ArrayList<CallRecord> callRecords2){
double cost = 0;
for(int i = 0;i < callRecords1.size();i ++) {
cost = callRecords1.get(i).getDuration() * 0.6 + cost;
}
for(int i = 0;i < callRecords2.size();i ++) {
cost = callRecords2.get(i).getDuration() * 0.3 + cost;
}
return cost;
}
@Override
public double calCost(ArrayList<CallRecord> callRecords1) {
double cost = 0;
for(int i = 0;i < callRecords1.size();i ++) {
cost = callRecords1.get(i).getDuration() * 0.3 + cost;
}
return cost;
}
}
class User {
private UserRecords userRecords = new UserRecords();
private double balance = 100;
private ChargeMode chargeMode;
private String number;
public User(String number) {
this.number = number;
}
public double calBalance() {
return getBalance() - calCost() - chargeMode.getMonthlyRent();
}
public double calCost() {
return chargeMode.calCost(getUserRecords()) ;
}
public ChargeMode getChargeMode() {
return chargeMode;
}
public void setChargeMode(LandlinePhoneCharging landlinePhoneCharging) {
chargeMode = landlinePhoneCharging;
}
public void setChargeMode(MobilePhoneCharging mobilePhoneCharging) {
chargeMode = mobilePhoneCharging;
}
public void setChargeMode(SendMessageCharging sendMessageCharging) {
chargeMode = sendMessageCharging;
}
public UserRecords getUserRecords() {
return userRecords;
}
public void setUserRecords(UserRecords userRecords) {
this.userRecords = userRecords;
}
public double getBalance() {
return balance;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}

abstract class CallChargeRule {
public double calCost(ArrayList<CallRecord> callRecords){
return 0;
}
public double calCost(ArrayList<CallRecord> callRecords1,ArrayList<CallRecord> callRecords2){
return 0;
}
}
class CallRecord extends CommunicationRecord{
private Date startTime;
private Date endTime;
private String callingAddressAreaCode;
private String answerAddressAreaCode;
public double getDuration() {
long sec = (getEndTime().getTime() - getStartTime().getTime()) / 1000 / 60;
long m = (getEndTime().getTime() - getStartTime().getTime()) / 1000;
if(m %100 != 0)
sec ++;
return sec;
}
public Date getStartTime() {
return startTime;
}
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
public Date getEndTime() {
return endTime;
}
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
public String getCallingAddressAreaCode() {
return callingAddressAreaCode;
}
public void setCallingAddressAreaCode(String callingAddressAreaCode) {
this.callingAddressAreaCode = callingAddressAreaCode;
}
public String getAnswerAddressAreaCode() {
return answerAddressAreaCode;
}
public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
this.answerAddressAreaCode = answerAddressAreaCode;
}
}
abstract class ChargeMode {
private ArrayList<ChargeRule> chargeRules = new ArrayList<>();
public abstract double calCost(UserRecords userRecords);
public abstract double getMonthlyRent();

public ArrayList<ChargeRule> getChargeRules() {
return chargeRules;
}
public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
this.chargeRules = chargeRules;
}
}
abstract class ChargeRule {
}
abstract class CommunicationRecord {
protected String callingNumber;
protected String answerNumber;
public String getCallingNumber() {
return callingNumber;
}
public void setCallingNumber(String callingNumber) {
this.callingNumber = callingNumber;
}
public String getAnswerNumber() {
return answerNumber;
}
public void setAnswerNumber(String answerNumber) {
this.answerNumber = answerNumber;
}
}
class LandlinePhoneCharging extends ChargeMode{
LandPhoneInCityRule landPhoneInCityRule = new LandPhoneInCityRule();
LandPhoneInProvinceRule landPhoneInProvinceRule = new LandPhoneInProvinceRule();
LandPhoneInLandRule landPhoneInLandRule = new LandPhoneInLandRule();

@Override
public double calCost(UserRecords userRecords) {
return landPhoneInCityRule.calCost(userRecords.getCallingInCityRecords()) +
landPhoneInProvinceRule.calCost(userRecords.getCallingInProvinceRecords()) +
landPhoneInLandRule.calCost(userRecords.getCallingInLandRecords()) ;
}
@Override
public double getMonthlyRent() {
return 20;
}
}

class MobilePhoneInCityRule extends CallChargeRule {
@Override
public double calCost(ArrayList<CallRecord> callRecords1) {
double cost = 0;
for(int i = 0;i < callRecords1.size();i ++) {
cost = callRecords1.get(i).getDuration() * 0.1 + cost;
}
return cost;
}
}
class MobilePhoneInProvinceRule extends CallChargeRule {
@Override
public double calCost(ArrayList<CallRecord> callRecords1) {
double cost = 0;
for(int i = 0;i < callRecords1.size();i ++) {
cost = callRecords1.get(i).getDuration() * 0.2 + cost;
}
return cost;
}
}
class MobilePhoneInLandRule extends CallChargeRule {
@Override
public double calCost(ArrayList<CallRecord> callRecords1) {
double cost = 0;
for(int i = 0;i < callRecords1.size();i ++) {
cost = callRecords1.get(i).getDuration() * 0.3 + cost;
}
return cost;
}
}
class LandPhoneInCityRule extends CallChargeRule{
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double cost = 0;
for(int i = 0;i < callRecords.size();i ++) {
cost = callRecords.get(i).getDuration() * 0.1 + cost;
}
return cost;
}
}
class LandPhoneInLandRule extends CallChargeRule{
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double cost = 0;
for(int i = 0;i < callRecords.size();i ++) {
cost = callRecords.get(i).getDuration() * 0.6 + cost;
}
return cost;
}
}
class LandPhoneInProvinceRule extends CallChargeRule{
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double cost = 0;
for(int i = 0;i < callRecords.size();i ++) {
cost = callRecords.get(i).getDuration() * 0.3 + cost;
}
return cost;
}
}
abstract class MessageChargeRule extends ChargeRule {
public abstract double calCost(ArrayList<MessageRecord> messageRecords);
}
class SendMessageRule extends MessageChargeRule {
@Override
public double calCost(ArrayList<MessageRecord> messageRecords) {
double cost = 0;
int count = 0;
for(int i = 0;i < messageRecords.size();i++){
int num = messageRecords.get(i).getMessage().length() + 9;
for(; num / 10 > 0; num -= 10){
count ++;
if(num != 0 )
{
if (count < 4)
cost = cost + 0.1;
else if (count < 6)
cost = cost + 0.2;
else
cost = cost + 0.3;
}
}
}
return cost;
}
}
class SendMessageCharging extends ChargeMode{
SendMessageRule sendMessageRule = new SendMessageRule();
@Override
public double calCost(UserRecords userRecords) {
return sendMessageRule.calCost(userRecords.getSendMessageRecords());
}

@Override
public double getMonthlyRent() {
return 0;
}
}
class MessageRecord extends CommunicationRecord{
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
class UserRecords {
private ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();
private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();
private ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();
private ArrayList<CallRecord> callingInProvinceRoamingRecords = new ArrayList<CallRecord>();
private ArrayList<CallRecord> callingInLandRoamingRecords = new ArrayList<CallRecord>();
private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();
private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();
private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();
private ArrayList<CallRecord> answerInLandRoamingRecords = new ArrayList<CallRecord>();
private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();
private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();

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 addCallingInProvinceRoamingRecords(CallRecord callRecord) {
callingInProvinceRoamingRecords.add(callRecord);
}
public void addCallingInLandRoamingRecords(CallRecord callRecord) {
callingInLandRoamingRecords.add(callRecord);
}
public void addAnswerInCityRecords(CallRecord callRecord) {
answerInCityRecords.add(callRecord);
}
public void addAnswerInProvinceRecords(CallRecord callRecord) {
answerInProvinceRecords.add(callRecord);
}
public void addAnswerInLandRecords(CallRecord callRecord) {
answerInLandRecords.add(callRecord);
}
public void addAnswerInLandRoamingRecords(CallRecord callRecord) {
answerInLandRoamingRecords.add(callRecord);
}
public void addSendMessageRecords(MessageRecord messageRecord) {
sendMessageRecords.add(messageRecord);
}
public void addReceiveMessageRecords(MessageRecord messageRecord) {
receiveMessageRecords.add(messageRecord);
}
public ArrayList<CallRecord> getCallingInCityRecords() {
return callingInCityRecords;
}
public ArrayList<CallRecord> getCallingInProvinceRecords() {
return callingInProvinceRecords;
}
public ArrayList<CallRecord> getCallingInLandRecords() {
return callingInLandRecords;
}
public ArrayList<CallRecord> getAnswerInCityRecords() {
return answerInCityRecords;
}
public ArrayList<CallRecord> getAnswerInProvinceRecords() {
return answerInProvinceRecords;
}
public ArrayList<CallRecord> getAnswerInLandRecords() {
return answerInLandRecords;
}
public ArrayList<CallRecord> getCallingInProvinceRoamingRecords() {
return callingInProvinceRoamingRecords;
}
public ArrayList<CallRecord> getCallingInLandRoamingRecords() {
return callingInLandRoamingRecords;
}
public ArrayList<CallRecord> getAnswerInLandRoamingRecords() {
return answerInLandRoamingRecords;
}
public ArrayList<MessageRecord> getSendMessageRecords() {
return sendMessageRecords;
}
public ArrayList<MessageRecord> getReceiveMessageRecords() {
return receiveMessageRecords;
}
}
posted @ 2022-06-16 15:54  一只不会程序的猿  阅读(41)  评论(0)    收藏  举报