BLOG-3~
前言
本次BLOG主要是围绕近期所做的PTA实验进行探究,反思,尤其是最近所做的电信计费问题,进行总结讨论,但是笔者的能力有限,能讨论到什么地步属实未知,如有不好的地方,请多多见谅。本次作业和实验的题量适宜,但是难度可以说是很大的(对笔者来说哈)
设计与分析
1.电信计费系列1-座机计费
实现一个简单的电信计费程序:
假设南昌市电信分公司针对市内座机用户采用的计费方式:
月租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、用户区号不为南昌市的区号也作为正常用户处理。
输出格式:
根据输入的详细通讯信息,计算所有已开户的用户的当月费用(精确到小数点后2位,
单位元)。假设每个用户初始余额是100元。
每条通讯信息单独计费后累加,不是将所有时间累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。
错误处理:
输入数据中出现的不符合格式要求的行一律忽略。



图1
图1中User是用户类,包括属性:
userRecords (用户记录)、balance(余额)、chargeMode(计费方式)、number(号码)。
ChargeMode是计费方式的抽象类:
chargeRules是计费方式所包含的各种计费规则的集合,ChargeRule类的定义见图3。
getMonthlyRent()方法用于返回月租(monthlyRent)。
UserRecords是用户记录类,保存用户各种通话、短信的记录,
各种计费规则将使用其中的部分或者全部记录。
其属性从上到下依次是:
市内拨打电话、省内(不含市内)拨打电话、省外拨打电话、
市内接听电话、省内(不含市内)接听电话、省外接听电话的记录
以及发送短信、接收短信的记录。
图2
图2中CommunicationRecord是抽象的通讯记录类:
包含callingNumber拨打号码、answerNumber接听号码两个属性。
CallRecord(通话记录)、MessageRecord(短信记录)是它的子类。
CallRecord(通话记录类)包含属性:
通话的起始、结束时间以及
拨号地点的区号(callingAddressAreaCode)、接听地点的区号(answerAddressAreaCode)。
区号用于记录在哪个地点拨打和接听的电话。座机无法移动,就是本机区号,如果是手机号,则会有差异。
图3 图3是计费规则的相关类,这些类的核心方法是: calCost(ArrayList<CallRecord> callRecords)。 该方法针根据输入参数callRecords中的所有记录计算某用户的某一项费用;如市话费。 输入参数callRecords的约束条件:必须是某一个用户的符合计费规则要求的所有记录。 LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三个类分别是 座机拨打市内、省内、省外电话的计费规则类,用于实现这三种情况的费用计算。 (提示:可以从UserRecords类中获取各种类型的callRecords)。
后续扩展说明:
后续题目集将增加手机用户,手机用户的计费方式中除了与座机计费类似的主叫通话费之外,还包含市外接听电话的漫游费以及发短信的费用。在本题的设计时可统一考虑。
通话记录中,手机需要额外记录拨打/接听的地点的区号,比如:
座机打手机:t-主叫号码 接听号码 接听地点区号 起始时间 结束时间
t-079186330022 13305862264 020 2022.1.3 10:00:25 2022.1.3 10:05:11
手机互打:t-主叫号码 拨号地点 接听号码 接听地点区号 起始时间 结束时间
t-18907910010 0791 13305862264 0371 2022.1.3 10:00:25 2022.1.3 10:05:11
短信的格式:m-主叫号码,接收号码,短信内容
m-18907910010 13305862264 welcome to jiangxi
m-13305862264 18907910010 thank you
输入样例:
在这里给出一组输入。例如:
u-079186300001 0
t-079186300001 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:25
end
输出样例:
在这里给出相应的输出。例如:
079186300001 3.0 77.0
(一)主类
1 public class Main { 2 3 public static void main(String[] args) throws ParseException { 4 5 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); 6 Scanner sc = new Scanner(System.in); 7 8 9 ArrayList<String> list = new ArrayList<String>(); 10 String line = null ; 11 while(!"end".equals(line = sc.nextLine())){ 12 list.add(line); 13 } 14 ArrayList<CallRecord> callRecordArrayList = new ArrayList<>(); 15 16 for (String next : list) { 17 String[] s1 = next.split(" "); 18 String zhujiaohaoma = s1[0]; 19 String jietinghaoma = s1[1]; 20 String startTime = s1[2] + " " + s1[3]; 21 String endTime = s1[4] + " " + s1[5]; 22 23 24 Date startTime1 = dateFormat.parse(startTime); 25 Date endTime1 = dateFormat.parse(startTime); 26 CallRecord callRecord = new CallRecord(); 27 callRecord.setStartTime(startTime1); 28 callRecord.setEndTime(endTime1); 29 callRecord.setCallingNumber(zhujiaohaoma); 30 callRecord.setAnswerNumber(jietinghaoma); 31 32 callRecordArrayList.add(callRecord); 33 } 34 UserRecords userRecords = new UserRecords(); 35 userRecords.addAnswerInCityRecords(callRecordArrayList); 36 37 // TODO 38 } 39 String s = sc.nextLine(); 40 zifuchuan.dasbduwac(s); 41 } 42 abstract class ChargeRule { 43 44 } 45 46 abstract class CallChargeRule extends ChargeRule { 47 48 double calCost(List<CallRecord> callRecords) { 49 return 0; 50 } 51 52 }
(二)市-电话费
1 class LandPhoneInCityRule extends CallChargeRule{ 2 3 @Override 4 double calCost(List<CallRecord> callRecords) { 5 double sum = 0; 6 for (CallRecord callRecord : callRecords) { 7 int seconds = (int)((callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime())/1000); 8 int min = (int)Math.ceil(seconds * 1.0 / 60); 9 sum += min * 0.1; 10 } 11 return sum; 12 } 13 }
(三)省-电话费
1 class LandPhoneInProvinceRule extends CallChargeRule{ 2 3 @Override 4 double calCost(List<CallRecord> callRecords) { 5 double sum = 0; 6 for (CallRecord callRecord : callRecords) { 7 int seconds = (int)((callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime())/1000); 8 int min = (int)Math.ceil(seconds * 1.0 / 60); 9 sum += min * 0.3; 10 } 11 return sum; 12 } 13 }
(四)国-电话费
1 class LandPhoneInlandRule extends CallChargeRule{ 2 3 @Override 4 double calCost(List<CallRecord> callRecords) { 5 double sum = 0; 6 for (CallRecord callRecord : callRecords) { 7 int seconds = (int)((callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime())/1000); 8 int min = (int)Math.ceil(seconds * 1.0 / 60); 9 sum += min * 0.6; 10 } 11 return sum; 12 } 13 }
(五)⏲计时
1 class CallRecord extends CommunicationRecord { 2 3 private Date startTime; 4 private Date endTime; 5 6 private String callingAddressAreaCode; 7 private String answerAddressAreaCode; 8 9 public Date getStartTime() { 10 return startTime; 11 } 12 13 public void setStartTime(Date startTime) { 14 this.startTime = startTime; 15 } 16 17 public Date getEndTime() { 18 return endTime; 19 } 20 21 public void setEndTime(Date endTime) { 22 this.endTime = endTime; 23 } 24 25 public String getCallingAddressAreaCode() { 26 return callingAddressAreaCode; 27 } 28 29 public void setCallingAddressAreaCode(String callingAddressAreaCode) { 30 this.callingAddressAreaCode = callingAddressAreaCode; 31 } 32 33 public String getAnswerAddressAreaCode() { 34 return answerAddressAreaCode; 35 } 36 37 public void setAnswerAddressAreaCode(String answerAddressAreaCode) { 38 this.answerAddressAreaCode = answerAddressAreaCode; 39 } 40 }
1 class MessageRecord extends CommunicationRecord { 2 3 private String message; 4 5 public String getMessage() { 6 return message; 7 } 8 9 public void setMessage(String message) { 10 this.message = message; 11 } 12 }
(六)付费
1 abstract class ChargeMode { 2 private List<ChargeRule> chargeRules = new ArrayList<>(); 3 4 public double calCost(UserRecords userRecords) { 5 6 return 0; 7 } 8 public double getMonthlyRent() { 9 10 return 0; 11 } 12 13 public List<ChargeRule> getChargeRules() { 14 return chargeRules; 15 } 16 17 public void setChargeRules(List<ChargeRule> chargeRules) { 18 this.chargeRules = chargeRules; 19 } 20 }
(七)电话接/打
1 abstract class CommunicationRecord { 2 private String callingNumber; 3 private String answerNumber; 4 public String getCallingNumber() { 5 return callingNumber; 6 } 7 public void setCallingNumber(String callingNumber) { 8 this.callingNumber = callingNumber; 9 } 10 public String getAnswerNumber() { 11 return answerNumber; 12 } 13 public void setAnswerNumber(String answerNumber) { 14 this.answerNumber = answerNumber; 15 } 16 }
(八)月租
1 class LandlinePhoneCharging extends ChargeMode { 2 3 private double monthlyRent = 20; 4 5 @Override 6 public double calCost(UserRecords userRecords) { 7 return super.calCost(userRecords); 8 } 9 10 @Override 11 public double getMonthlyRent() { 12 return this.monthlyRent; 13 } 14 }
(九)用户
1 class User { 2 3 private UserRecords userRecords = new UserRecords(); 4 private double balance = 100d; 5 private ChargeMode chargeMode; 6 private String number; 7 8 public double calBalance() { 9 10 return this.balance; 11 } 12 13 public double calCost() { 14 15 return 0; 16 } 17 18 public UserRecords getUserRecords() { 19 return userRecords; 20 } 21 22 public void setUserRecords(UserRecords userRecords) { 23 this.userRecords = userRecords; 24 } 25 26 public double getBalance() { 27 return balance; 28 } 29 30 public void setBalance(double balance) { 31 this.balance = balance; 32 } 33 34 public ChargeMode getChargeMode() { 35 return chargeMode; 36 } 37 38 public void setChargeMode(ChargeMode chargeMode) { 39 this.chargeMode = chargeMode; 40 } 41 42 public String getNumber() { 43 return number; 44 } 45 46 public void setNumber(String number) { 47 this.number = number; 48 } 49 }
1 class UserRecords { 2 3 private List<CallRecord> callingInCityRecords; 4 private List<CallRecord> callingInProvinceRecords; 5 private List<CallRecord> callingInLandRecords; 6 private List<CallRecord> answerInCityRecords; 7 private List<CallRecord> answerInProvinceRecords; 8 private List<CallRecord> answerInLandRecords; 9 10 private List<MessageRecord> sendMessageRecords; 11 private List<MessageRecord> receiveMessageRecords; 12 13 public List<CallRecord> getCallingInCityRecords() { 14 return callingInCityRecords; 15 } 16 17 public void addCallingInCityRecords(List<CallRecord> callingInCityRecords) { 18 this.callingInCityRecords = callingInCityRecords; 19 } 20 21 public List<CallRecord> getCallingInProvinceRecords() { 22 return callingInProvinceRecords; 23 } 24 25 public void addCallingInProvinceRecords(List<CallRecord> callingInProvinceRecords) { 26 this.callingInProvinceRecords = callingInProvinceRecords; 27 } 28 29 public List<CallRecord> getCallingInLandRecords() { 30 return callingInLandRecords; 31 } 32 33 public void addCallingInLandRecords(List<CallRecord> callingInLandRecords) { 34 this.callingInLandRecords = callingInLandRecords; 35 } 36 37 public List<CallRecord> getAnswerInCityRecords() { 38 return answerInCityRecords; 39 } 40 41 public void addAnswerInCityRecords(List<CallRecord> answerInCityRecords) { 42 this.answerInCityRecords = answerInCityRecords; 43 } 44 45 public List<CallRecord> getAnswerInProvinceRecords() { 46 return answerInProvinceRecords; 47 } 48 49 public void addAnswerInProvinceRecords(List<CallRecord> answerInProvinceRecords) { 50 this.answerInProvinceRecords = answerInProvinceRecords; 51 } 52 53 public List<CallRecord> getAnswerInLandRecords() { 54 return answerInLandRecords; 55 } 56 57 public void addAnswerInLandRecords(List<CallRecord> answerInLandRecords) { 58 this.answerInLandRecords = answerInLandRecords; 59 } 60 61 public List<MessageRecord> getSendMessageRecords() { 62 return sendMessageRecords; 63 } 64 65 public void addSendMessageRecords(List<MessageRecord> sendMessageRecords) { 66 this.sendMessageRecords = sendMessageRecords; 67 } 68 69 public List<MessageRecord> getReceiveMessageRecords() { 70 return receiveMessageRecords; 71 } 72 73 public void addReceiveMessageRecords(List<MessageRecord> receiveMessageRecords) { 74 this.receiveMessageRecords = receiveMessageRecords; 75 } 76 }
2.电信计费系列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
输入:
输入信息包括两种类型
1、逐行输入南昌市用户开户的信息,每行一个用户,含手机和座机用户
格式:u-号码 计费类型 (计费类型包括:0-座机 1-手机实时计费 2-手机A套餐)
例如:u-079186300001 0
座机号码由区号和电话号码拼接而成,电话号码包含7-8位数字,区号最高位是0。
手机号码由11位数字构成,最高位是1。
本题在电信计费系列1基础上增加类型1-手机实时计费。
手机设置0或者座机设置成1,此种错误可不做判断。
2、逐行输入本月某些用户的通讯信息,通讯信息格式:
座机呼叫座机:t-主叫号码 接听号码 起始时间 结束时间
t-079186330022 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:11
以上四项内容之间以一个英文空格分隔,
时间必须符合"yyyy.MM.dd HH:mm:ss"格式。提示:使用SimpleDateFormat类。
输入格式增加手机接打电话以及收发短信的格式,手机接打电话的信息除了号码之外需要额外记录拨打/接听的地点的区号,比如:
座机打手机:
t-主叫号码 接听号码 接听地点区号 起始时间 结束时间
t-079186330022 13305862264 020 2022.1.3 10:00:25 2022.1.3 10:05:11
手机互打:
t-主叫号码 拨号地点 接听号码 接听地点区号 起始时间 结束时间
t-18907910010 0791 13305862264 0371 2022.1.3 10:00:25 2022.1.3 10:05:11
注意:以上两类信息,先输入所有开户信息,再输入所有通讯信息,最后一行以“end”结束。
输出:
根据输入的详细通讯信息,计算所有已开户的用户的当月费用(精确到小数点后2位,单位元)。假设每个用户初始余额是100元。
每条通讯、短信信息均单独计费后累加,不是将所有信息累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。
错误处理:
输入数据中出现的不符合格式要求的行一律忽略。
本题只做格式的错误判断,无需做内容上不合理的判断,比如同一个电话两条通讯记录的时间有重合、开户号码非南昌市的号码等,此类情况都当成正确的输入计算。但时间的输入必须符合要求,比如不能输入2022.13.61 28:72:65。
建议类图:
图1
图1中User是用户类,包括属性:
userRecords (用户记录)、balance(余额)、chargeMode(计费方式)、number(号码)。
ChargeMode是计费方式的抽象类:
chargeRules是计费方式所包含的各种计费规则的集合,ChargeRule类的定义见图3。
getMonthlyRent()方法用于返回月租(monthlyRent)。
UserRecords是用户记录类,保存用户各种通话、短信的记录,
各种计费规则将使用其中的部分或者全部记录。
其属性从上到下依次是:
市内拨打电话、省内(不含市内)拨打电话、省外拨打电话、
市内接听电话、省内(不含市内)接听电话、省外接听电话的记录
以及发送短信、接收短信的记录。

图2
图2中CommunicationRecord是抽象的通讯记录类:
包含callingNumber拨打号码、answerNumber接听号码两个属性。
CallRecord(通话记录)、MessageRecord(短信记录)是它的子类。CallRecord(通话记录类)包含属性:
通话的起始、结束时间以及
拨号地点的区号(callingAddressAreaCode)、接听地点的区号(answerAddressAreaCode)。
区号用于记录在哪个地点拨打和接听的电话。座机无法移动,就是本机区号,如果是手机号,则会有差异。

图3
图3是计费规则的相关类,这些类的核心方法是:
calCost(ArrayList<CallRecord> callRecords)。
该方法针根据输入参数callRecords中的所有记录计算某用户的某一项费用;如市话费。
输入参数callRecords的约束条件:必须是某一个用户的符合计费规则要求的所有记录。
SendMessageRule是发送短信的计费规则类,用于计算发送短信的费用。
LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三个类分别是座机拨打市内、省内、省外电话的计费规则类,用于实现这三种情况的费用计算。
(提示:可以从UserRecords类中获取各种类型的callRecords)。
注意:以上图中所定义的类不是限定要求,根据实际需要自行补充或修改。
输入样例:
在这里给出一组输入。例如:
u-13811111111 1
t-13811111111 0791 13811111110 020 2022.1.3 08:00:00 2022.1.3 08:09:20
end
输出样例:
在这里给出相应的输出。例如:
13811111111 3.0 82.0
3.电信计费系列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.
注意:以上两类信息,先输入所有开户信息,再输入所有通讯信息,最后一行以“end”结束。
输出:
根据输入的详细短信信息,计算所有已开户的用户的当月短信费用(精确到小数点后2位,单位元)。假设每个用户初始余额是100元。
每条短信信息均单独计费后累加,不是将所有信息累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。
错误处理:
输入数据中出现的不符合格式要求的行一律忽略。
本题只做格式的错误判断,无需做内容上不合理的判断,比如同一个电话两条通讯记录的时间有重合、开户号码非南昌市的号码、自己给自己打电话等,此类情况都当成正确的输入计算。但时间的输入必须符合要求,比如不能输入2022.13.61 28:72:65。
本题只考虑短信计费,不考虑通信费用以及月租费。
建议类图:
参见图1、2、3:

图1
图1中User是用户类,包括属性:
userRecords (用户记录)、balance(余额)、chargeMode(计费方式)、number(号码)。
ChargeMode是计费方式的抽象类:
chargeRules是计费方式所包含的各种计费规则的集合,ChargeRule类的定义见图3。
getMonthlyRent()方法用于返回月租(monthlyRent)。
UserRecords是用户记录类,保存用户各种通话、短信的记录,
各种计费规则将使用其中的部分或者全部记录。
其属性从上到下依次是:
市内拨打电话、省内(不含市内)拨打电话、省外拨打电话、
市内接听电话、省内(不含市内)接听电话、省外接听电话的记录
以及发送短信、接收短信的记录。

图2
图2中CommunicationRecord是抽象的通讯记录类:
包含callingNumber拨打号码、answerNumber接听号码两个属性。
CallRecord(通话记录)、MessageRecord(短信记录)是它的子类。

图3
图3是计费规则的相关类,这些类的核心方法是:
calCost(ArrayList callRecords)。
该方法针根据输入参数callRecords中的所有记录计算某用户的某一项费用;如市话费。
输入参数callRecords的约束条件:必须是某一个用户的符合计费规则要求的所有记录。
SendMessageRule是发送短信的计费规则类,用于计算发送短信的费用。
LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三个类分别是座机拨打市内、省内、省外电话的计费规则类,用于实现这三种情况的费用计算。
(提示:可以从UserRecords类中获取各种类型的callRecords)。
注意:以上图中所定义的类不是限定要求,根据实际需要自行补充或修改。
输入样例:
在这里给出一组输入。例如:
u-18907910010 3
m-18907910010 13305862264 aaaaaaaaaaaaaaaaaaaaaaa
end
输出样例:
在这里给出相应的输出。例如:
18907910010 0.3 99.7
### 输入样例1:
在这里给出一组输入。例如:
u-18907910010 3
m-18907910010 13305862264 aaaaaaaaaaaa
m-18907910010 13305862264 aaaaaaa.
m-18907910010 13305862264 bb,bbbb
end
输出样例1:
在这里给出相应的输出。例如:
18907910010 0.5 99.5
1 public class Main { 2 public static void main(String[] args){ 3 Scanner in = new Scanner(System.in); 4 HashSet<User> alluserArrayList = new HashSet<>(); 5 ArrayList<CommunicationRecord> allcommunicationRecords = new ArrayList<>(); 6 String Line; 7 do { 8 Line = in.nextLine(); 9 if (Line.equals("")) continue; 10 if (Line.charAt(0) == 'u') { 11 alluserArrayList.add(LineToUser(Line)); 12 } else if (Line.charAt(0) == 't') { 13 allcommunicationRecords.add(LineToCallRecord(Line)); 14 } 15 } while (!"end".equals(Line)); 16 List<User> res = new ArrayList<>(alluserArrayList); 17 Collections.sort(res); 18 for(User u : res){ 19 double cost = 0; 20 for (CommunicationRecord cRecord : allcommunicationRecords){ 21 if (isZhujiao(u,cRecord)){ 22 if (u.getChargeMode() instanceof LandlinePhoneCharging){ 23 //zuoji da 24 if (ChargeRule.InCity(((CallRecord)cRecord).getAnswerAddressAreaCode())){ 25 long tm = CommunicationRecord.CalTimeCha(((CallRecord)cRecord).getStartTime(),((CallRecord)cRecord).getEndTime()); 26 long min = tm/60,other = tm%60; 27 min +=other!=0?1:0; 28 cost += min*0.1; 29 }else if(ChargeRule.InProvince(((CallRecord)cRecord).getAnswerAddressAreaCode())){ 30 long tm = CommunicationRecord.CalTimeCha(((CallRecord)cRecord).getStartTime(),((CallRecord)cRecord).getEndTime()); 31 long min = tm/60,other = tm%60; 32 min +=other!=0?1:0; 33 cost += min*0.3; 34 }else if (ChargeRule.InLand(((CallRecord)cRecord).getAnswerAddressAreaCode())){ 35 long tm = CommunicationRecord.CalTimeCha(((CallRecord)cRecord).getStartTime(),((CallRecord)cRecord).getEndTime()); 36 long min = tm/60,other = tm%60; 37 min +=other!=0?1:0; 38 cost += min*0.6; 39 } 40 } else if(u.getChargeMode() instanceof PhoneCharging){ 41 // shouji da 42 if (ChargeRule.InCity(((CallRecord)cRecord).getCallingAddressAreaCode())){ 43 if (ChargeRule.InCity(cRecord.getAnswerNumber().substring(0,4))){ 44 long tm = CommunicationRecord.CalTimeCha(((CallRecord)cRecord).getStartTime(),((CallRecord)cRecord).getEndTime()); 45 long min = tm/60,other = tm%60; 46 min +=other!=0?1:0; 47 cost += min*0.1; 48 }else if (ChargeRule.InProvince(((CallRecord)cRecord).getAnswerAddressAreaCode())){ 49 long tm = CommunicationRecord.CalTimeCha(((CallRecord)cRecord).getStartTime(),((CallRecord)cRecord).getEndTime()); 50 long min = tm/60,other = tm%60; 51 min +=other!=0?1:0; 52 cost += min*0.2; 53 }else if (ChargeRule.InLand(((CallRecord)cRecord).getAnswerAddressAreaCode())){ 54 long tm = CommunicationRecord.CalTimeCha(((CallRecord)cRecord).getStartTime(),((CallRecord)cRecord).getEndTime()); 55 long min = tm/60,other = tm%60; 56 min +=other!=0?1:0; 57 cost += min*0.3; 58 } 59 }else if(ChargeRule.InProvince(((CallRecord)cRecord).getCallingAddressAreaCode())){ 60 long tm = CommunicationRecord.CalTimeCha(((CallRecord)cRecord).getStartTime(),((CallRecord)cRecord).getEndTime()); 61 long min = tm/60,other = tm%60; 62 min +=other!=0?1:0; 63 cost += min*0.3; 64 }else if (ChargeRule.InLand(((CallRecord)cRecord).getCallingAddressAreaCode())){ 65 long tm = CommunicationRecord.CalTimeCha(((CallRecord)cRecord).getStartTime(),((CallRecord)cRecord).getEndTime()); 66 long min = tm/60,other = tm%60; 67 min +=other!=0?1:0; 68 cost += min*0.6; 69 } 70 } 71 } 72 // else 73 if (isJieTing(u,cRecord)){ 74 if (u.getChargeMode() instanceof LandlinePhoneCharging){ 75 //zuoji jie free 76 } else if(u.getChargeMode() instanceof PhoneCharging){ 77 // shouji jie 78 if (ChargeRule.InLand(((CallRecord)cRecord).getAnswerAddressAreaCode())){ 79 long tm = CommunicationRecord.CalTimeCha(((CallRecord)cRecord).getStartTime(),((CallRecord)cRecord).getEndTime()); 80 long min = tm/60,other = tm%60; 81 min +=other!=0?1:0; 82 cost += min*0.3; 83 } 84 } 85 } 86 } 87 System.out.printf("%s %.1f %.1f\n",u.getNumber(),cost,u.getBalance()-u.getChargeMode().getMonthlyRent()-cost); 88 } 89 }
(二)主叫号码
1 private static boolean isZhujiao(User u , CommunicationRecord communicationRecord){ 2 return communicationRecord.getCallingNumber().equals(u.getNumber()); 3 } 4 private static boolean isJieTing(User u , CommunicationRecord communicationRecord){ 5 return communicationRecord.getAnswerNumber().equals(u.getNumber()); 6 } 7 private static User LineToUser(String str){ 8 str = str.substring(2); 9 String[] data = str.split(" "); 10 return new User(data[0],Integer.parseInt(data[1])); 11 } 12 private static CallRecord LineToCallRecord(String str){ 13 str = str.substring(2); 14 String[] data = str.split(" "); 15 int blk = BlockTimeFun(str); 16 int tmBlk = BlockID(str,blk-3); 17 18 String Time1,Time2; 19 Time1 = str.substring(tmBlk,BlockID(str,blk-1)); 20 Time2 = str.substring(BlockID(str,blk-1)); 21 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); 22 simpleDateFormat.setLenient(false); 23 Date date1, date2; 24 try { 25 date1 = simpleDateFormat.parse(Time1); 26 date2 = simpleDateFormat.parse(Time2); 27 } catch (ParseException e) { 28 return null; 29 } 30 if (data.length==2+4){ 31 return new CallRecord(data[0],data[1],date1,date2); 32 }else if(data.length==4+4){ 33 return new CallRecord(data[0],data[1],data[2],data[3],date1,date2); 34 }else if (data.length==3+4){ 35 CallRecord t = new CallRecord(); 36 if (isNumb(data[1])){ 37 t.setCallingNumber(data[0]); 38 t.setAnswerNumber(data[1]); 39 t.setCallingAddressAreaCode(data[0].substring(0,4)); 40 t.setAnswerAddressAreaCode(data[2]); 41 t.setStartTime(date1); 42 t.setEndTime(date2); 43 return t; 44 }else { 45 t.setCallingNumber(data[0]); 46 t.setCallingAddressAreaCode(data[1]); 47 t.setAnswerNumber(data[2]); 48 t.setAnswerAddressAreaCode(data[2].substring(0,4)); 49 t.setStartTime(date1); 50 t.setEndTime(date2); 51 return t; 52 } 53 } 54 return null; 55 } 56 private static boolean isNumb(String s){ 57 return s.length()>4; 58 } 59 private static boolean isAreaNum(String s){ 60 return s.length()<=4; 61 } 62 public static int BlockTimeFun(String s){ 63 int t = 0; 64 for (int i=0;i<s.length();i++){ 65 if (s.charAt(i)==' ')t++; 66 } 67 return t; 68 } 69 private static int BlockID(String str, int cnt){ 70 for (int i = 0, t = 0; i < str.length(); i++) { 71 if (t == cnt) return i; 72 if (str.charAt(i) == ' ') t++; 73 } 74 return str.length(); 75 } 76 private static void handle1() { 77 78 } 79 private static void handle2() { 80 81 }
(三)支付方式
1 abstract class ChargeRule { 2 public static boolean InCity(String callingAddressAreaCode) { 3 return "0791".equals(callingAddressAreaCode); 4 } 5 public static boolean InProvince(String callingAddressAreaCode) { 6 if((InCity(callingAddressAreaCode))||(callingAddressAreaCode.length()<3))return false; 7 if("0791".equals(callingAddressAreaCode))return true; 8 9 return "079".equals(callingAddressAreaCode.substring(0, 3))&&(callingAddressAreaCode.charAt(3)>='0'&&callingAddressAreaCode.charAt(3)<='9'); 10 } 11 public static boolean InLand(String callingAddressAreaCode){ 12 if (InCity(callingAddressAreaCode))return false; 13 return !InProvince(callingAddressAreaCode); 14 } 15 16 }
1 abstract class CallChargeRule extends ChargeRule { 2 3 double calCost(List<CallRecord> callRecords) { 4 return 0; 5 } 6 7 }
(四)城市
1 class LandPhoneInCityRule extends CallChargeRule{ 2 3 @Override 4 double calCost(List<CallRecord> callRecords) { 5 double sum = 0; 6 for (CallRecord callRecord : callRecords) { 7 int seconds = (int)((callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime())/1000); 8 int min = (int)Math.ceil(seconds * 1.0 / 60); 9 sum += min * 0.1; 10 } 11 return sum; 12 } 13 }
(五)省内
class LandPhoneInProvinceRule extends CallChargeRule{ @Override double calCost(List<CallRecord> callRecords) { double sum = 0; for (CallRecord callRecord : callRecords) { int seconds = (int)((callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime())/1000); int min = (int)Math.ceil(seconds * 1.0 / 60); sum += min * 0.3; } return sum; } } abstract class CommunicationRecord { private String callingNumber; private String answerNumber; public CommunicationRecord() { } public CommunicationRecord(String callingNumber, String answerNumber) { super(); this.callingNumber = callingNumber; this.answerNumber = 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; } public static long CalTimeCha(Date a, Date b){ return (b.getTime() - a.getTime()) /1000; } } class CallRecord extends CommunicationRecord { private Date startTime; private Date endTime; private String callingAddressAreaCode; private String answerAddressAreaCode; public CallRecord() { } //zuoji to zuoji public CallRecord(String callingNumber, String answerNumber, Date startTime, Date endTime) { super(callingNumber, answerNumber); this.startTime = startTime; this.endTime = endTime; callingAddressAreaCode=callingNumber.substring(0,4); answerAddressAreaCode=answerNumber.substring(0,4); } //phone to phone public CallRecord(String callingNumber, String callingAddressAreaCode, String answerNumber, String answerAddressAreaCode, Date startTime, Date endTime) { super(callingNumber, answerNumber); this.startTime = startTime; this.endTime = endTime; this.callingAddressAreaCode = callingAddressAreaCode; this.answerAddressAreaCode = answerAddressAreaCode; } 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; } } class MessageRecord extends CommunicationRecord { private String message; public MessageRecord(String callingNumber, String answerNumber, String message) { super(callingNumber, answerNumber); this.message = message; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } abstract class ChargeMode { private double monthlyRent = 20; private List<ChargeRule> chargeRules = new ArrayList<>(); public double calCost(UserRecords userRecords) { double sum = 0; chargeRules.add(new LandPhoneInCityRule()); chargeRules.add(new LandPhoneInProvinceRule()); chargeRules.add(new LandPhoneInlandRule()); LandPhoneInCityRule city = (LandPhoneInCityRule) chargeRules.get(0); LandPhoneInProvinceRule province = (LandPhoneInProvinceRule) chargeRules.get(1); LandPhoneInlandRule land = (LandPhoneInlandRule) chargeRules.get(2); sum = sum + city.calCost(userRecords.getCallingInCityRecords()) + province.calCost(userRecords.getCallingInProvinceRecords()) + land.calCost(userRecords.getCallingInLandRecords()); return sum; } public List<ChargeRule> getChargeRules() { return chargeRules; } public void setChargeRules(List<ChargeRule> chargeRules) { this.chargeRules = chargeRules; } public double getMonthlyRent() { return monthlyRent; } }
(六)国家
1 class LandPhoneInlandRule extends CallChargeRule{ 2 3 @Override 4 double calCost(List<CallRecord> callRecords) { 5 double sum = 0; 6 for (CallRecord callRecord : callRecords) { 7 int seconds = (int)((callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime())/1000); 8 int min = (int)Math.ceil(seconds * 1.0 / 60); 9 sum += min * 0.6; 10 } 11 return sum; 12 } 13 }
(七)月租
1 class ChargeMode0 extends ChargeMode { 2 private double monthlyRent = 20; 3 4 @Override 5 public double calCost(UserRecords userRecords) { 6 // TODO Auto-generated method stub 7 return super.calCost(userRecords); 8 } 9 10 @Override 11 public double getMonthlyRent() { 12 // TODO Auto-generated method stub 13 return monthlyRent; 14 } 15 16 } 17 18 class ChargeMode1 extends ChargeMode { 19 private double monthlyRent = 15; 20 @Override 21 public double calCost(UserRecords userRecords) { 22 // TODO Auto-generated method stub 23 return super.calCost(userRecords); 24 } 25 26 @Override 27 public double getMonthlyRent() { 28 // TODO Auto-generated method stub 29 return super.getMonthlyRent(); 30 } 31 32 } 33 34 class PhoneCharging extends ChargeMode { 35 private double monthlyRent = 15; 36 37 public double getMonthlyRent() { 38 return monthlyRent; 39 } 40 } 41 42 class LandlinePhoneCharging extends ChargeMode { 43 private double monthlyRent = 20; 44 45 public double calCost(UserRecords userRecords) { 46 double cost = 0; 47 ArrayList<CallRecord> callInCityRecords = (ArrayList<CallRecord>) userRecords.getCallingInCityRecords(); 48 ArrayList<CallRecord> callInProvinceRecords = (ArrayList<CallRecord>) userRecords.getCallingInProvinceRecords(); 49 ArrayList<CallRecord> callInLandRecords = (ArrayList<CallRecord>) userRecords.getCallingInLandRecords(); 50 51 CallChargeRule landPhoneInCityRule = new LandPhoneInCityRule(); 52 CallChargeRule landPhoneInProvinceRule = new LandPhoneInProvinceRule(); 53 CallChargeRule landPhoneInLandRule = new LandPhoneInlandRule(); 54 55 cost += landPhoneInCityRule.calCost(callInCityRecords); 56 cost += landPhoneInProvinceRule.calCost(callInProvinceRecords); 57 cost += landPhoneInLandRule.calCost(callInLandRecords); 58 return cost; 59 } 60 61 public double getMonthlyRent() { 62 return monthlyRent; 63 } 64 }
(八)用户
1 class User implements Comparable<User>{ 2 3 private UserRecords userRecords = new UserRecords(); 4 private double balance = 100; 5 private ChargeMode chargeMode; 6 private String number; 7 8 public User(String number, int mod) { 9 this.number = number; 10 switch (mod) { 11 case 0: { 12 chargeMode = new LandlinePhoneCharging(); 13 break; 14 } 15 case 1: { 16 chargeMode = new PhoneCharging(); 17 } 18 case 2: { 19 20 } 21 default: 22 } 23 }
(九)花费
public double calCost() { return chargeMode.calCost(userRecords); } public UserRecords getUserRecords() { return userRecords; } public void setUserRecords(UserRecords userRecords) { this.userRecords = userRecords; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public ChargeMode getChargeMode() { return chargeMode; } public void setChargeMode(ChargeMode chargeMode) { this.chargeMode = chargeMode; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } @Override public int hashCode() { return 123; } @Override public boolean equals(Object obj) { return getNumber().equals(((User)obj).getNumber()); } @Override public int compareTo(User o) { return getNumber().compareTo(o.getNumber()); // return Integer.compare(Integer.parseInt(getNumber()),Integer.parseInt(o.getNumber())); } }
(十)用户
1 class UserRecords { 2 3 private List<CallRecord> callingInCityRecords = new ArrayList<CallRecord>(); 4 private List<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>(); 5 private List<CallRecord> callingInLandRecords = new ArrayList<CallRecord>(); 6 private List<CallRecord> answerInCityRecords = new ArrayList<CallRecord>(); 7 private List<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>(); 8 private List<CallRecord> answerInLandRecords = new ArrayList<CallRecord>(); 9 10 private List<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>(); 11 private List<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>(); 12 private CallRecord callRecord; 13 14 public List<CallRecord> getCallingInCityRecords() { 15 return callingInCityRecords; 16 } 17 18 public void addCallingInCityRecords(List<CallRecord> callingInCityRecords) { 19 callingInCityRecords.add(callRecord); 20 } 21 22 public List<CallRecord> getCallingInProvinceRecords() { 23 return callingInProvinceRecords; 24 } 25 26 public void addCallingInProvinceRecords(List<CallRecord> callingInProvinceRecords) { 27 callingInProvinceRecords.add(callRecord); 28 } 29 30 public List<CallRecord> getCallingInLandRecords() { 31 return callingInLandRecords; 32 } 33 34 public void addCallingInLandRecords(List<CallRecord> callingInLandRecords) { 35 callingInLandRecords.add(callRecord); 36 } 37 38 public List<CallRecord> getAnswerInCityRecords() { 39 return answerInCityRecords; 40 } 41 42 public void addAnswerInCityRecords(List<CallRecord> answerInCityRecords) { 43 answerInCityRecords.add(callRecord); 44 } 45 46 public List<CallRecord> getAnswerInProvinceRecords() { 47 return answerInProvinceRecords; 48 } 49 50 public void addAnswerInProvinceRecords(List<CallRecord> answerInProvinceRecords) { 51 answerInProvinceRecords.add(callRecord); 52 } 53 54 public List<CallRecord> getAnswerInLandRecords() { 55 return answerInLandRecords; 56 } 57 58 public void addAnswerInLandRecords(List<CallRecord> answerInLandRecords) { 59 answerInLandRecords.add(callRecord); 60 } 61 62 public List<MessageRecord> getSendMessageRecords() { 63 return sendMessageRecords; 64 } 65 66 public void addSendMessageRecords(List<MessageRecord> sendMessageRecords) { 67 this.sendMessageRecords = sendMessageRecords; 68 } 69 70 public List<MessageRecord> getReceiveMessageRecords() { 71 return receiveMessageRecords; 72 } 73 74 public void addReceiveMessageRecords(List<MessageRecord> receiveMessageRecords) { 75 this.receiveMessageRecords = receiveMessageRecords; 76 } 77 }
踩坑心得
- 过不了很多琐碎的测试点,还会出现一些不存在的问题,甚至可能过了一个测试点,等到过下个测试点时发现之前写的不正确,推翻了别的测试点。
- 经常需要自己一遍又一遍的测试
- ascll需要引入,需要查资料,容易分数越改越少。
- 有的题目测试点有问题,正确的代码无法通过测试。
- 有些题目内容不够详细,需要详细一点的提示,要不不懂,一直过不去,只能不断试探,浪费时间。
- 第三次做作业,整体难度偏高,需要引入正则表达式,对于我这种基础不好的同学来讲很难上手,所以我应该致力于攻克测试点。作业没过的测试点还有很多,希望有机会可以重新尝试。
改进建议
有些题目的猜测点可以提示一下,不必去一个一个试,会节省一点时间,而且很多测试点有BUG。
输入正确错误的格式掌握不够透彻,需要进一步学习使用Java语言对格式进行判断。例如第三次作业,基本上每一次都要进行格式的判断。
总结
- 在三次作业过程中,我也拓宽了自己的视野,学会借助外部资料来解决自己的某一个针对性问题,合理使用手中的资源。
- 由于第三次作业写得匆忙,我更加意识到时间的宝贵,意识到作业之所以给一整周的时间是为了让我们时时刻刻可以修改自己的错误,从作业发布就要及时下手。
- 遇到问题要积极求助,虚心学习,如果问题不能自己解决就要学习,身边的同学和老师都会帮助你。
- 由于作业的需求,我们迫不得已学习了ascll语言以及正则表达式等等,但这是一件对于我们来说很有利的事情
- .深刻了解了关于Java程序结构和代码书写的规范,以及要遵守的代码书写规范,拔高了我对程序设计的眼界和深度,学习了很多编程时使用的技巧和方法
- 我能看出每次作业和考试难度跨度较大,说明我们必须要用尽可能多的时间提升自己。

浙公网安备 33010602011771号