一、题目涉及的知识点
1、容器的使用
2、抛出异常
3、抽象类
4、继承与多态
5、正则表达式
二、题目分析总结
1.题目集08:7-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、2、3,可根据理解自行调整:
图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)。
源码如下:
![复制代码](https://common.cnblogs.com/images/copycode.gif)
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) throws ParseException { LandPhonelnlandRule landPhonelnlandRule = new LandPhonelnlandRule(); LandPhoneInCityRule landPhoneInCityRule = new LandPhoneInCityRule(); LandPhoneInProvinceRule landPhoneInProvinceRule = new LandPhoneInProvinceRule(); LandlinePhoneCharging landlinePhoneCharging = new LandlinePhoneCharging(); landlinePhoneCharging.getChargeRules().add(landPhoneInCityRule); landlinePhoneCharging.getChargeRules().add(landPhoneInProvinceRule); landlinePhoneCharging.getChargeRules().add(landPhonelnlandRule); ArrayList<User> UserList = new ArrayList<>(); Scanner in = new Scanner(System.in); String m = in.nextLine(); while (!m.equals("end")){ Judge judge = new Judge(); if (judge.isUserInformation(m)){ String regex= "(0791)[\\d]{7,8}"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(m); while (matcher.find()){ int flag=0; for (User user:UserList ) { if (user.getNumber().equals(matcher.group(0))){ flag=1; break; } } if (flag==0) { UserList.add(new User(matcher.group(0))); } } } if (judge.isCallingInformation(m)){ String []a = m.split(" "); try{ SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); Date startTime =format.parse(a[2]+" "+a[3]); Date endTime = format.parse(a[4]+" "+a[5]); for (User user:UserList ) { if (user.getNumber().equals(a[0].substring(2))){ if (judge.isInCityCode(a[1].substring(0,4))){ user.getUserRecords().addCallingInCityRecords(new CallRecord(startTime,endTime,a[0].substring(2,6),a[1].substring(0,4))); } else if (judge.isInProvinceCode(a[1].substring(0,4))){ user.getUserRecords().addCallingInProvinceRecords(new CallRecord(startTime,endTime,a[0].substring(2,6),a[1].substring(0,4))); } else { user.getUserRecords().addCallingInLandRecords(new CallRecord(startTime,endTime,a[0].substring(2,6),a[1].substring(0,4))); } } } } catch (ParseException e) { e.printStackTrace(); } } m = in.nextLine(); } for (User user:UserList ) { user.setChargeMode(landlinePhoneCharging); } for (int i= 0;i<UserList.size();i++){ int index=-1; Double min = Double.parseDouble(UserList.get(i).getNumber()); for (int j = i+1;j<UserList.size();j++){ if (Double.parseDouble(UserList.get(j).getNumber())<min){ min = Double.parseDouble(UserList.get(j).getNumber()); index=j; } } if (min!=Double.parseDouble(UserList.get(i).getNumber())){ User user = UserList.get(i); UserList.set(i,UserList.get(index)); UserList.set(index,user); } } for (User user:UserList ) { System.out.printf("%s %.1f %.1f\n",user.getNumber(),user.getChargeMode().calCost(user.getUserRecords()),user.getBalance()); } } } abstract class CallChargeRule extends ChargeRule{ public double calCost(ArrayList<CallRecord> callRecords){ return 0; } } class CallRecord extends CommunicationRecord{ private Date startTime; private Date endTime; private String callingAddressAreaCode; private String answerAddressAreaCode; public CallRecord(Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) { 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; } } abstract class ChargeMode { private ArrayList<ChargeRule> chargeRules = new ArrayList<>(); public ArrayList<ChargeRule> getChargeRules() { return chargeRules; } public void setChargeRules(ArrayList<ChargeRule> chargeRules) { this.chargeRules = chargeRules; } public double calCost(UserRecords userRecords){ return 0; } public double getMonthlyRent(){ return 0; } } abstract class ChargeRule { public double calCost(ArrayList<CallRecord> callRecords){ return 0; } } 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 Judge { public boolean isUserInformation(String s){ String pattern ="(u-0791)[\\d]{7,8}[\\s][0-2]"; return s.matches(pattern); } public boolean isCallingInformation(String s){ String pattern = "[t]-0791[0-9]{7,8}\\s" + "0[0-9]{9,11}\\s" + "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.(((0?[13578]|1[02])\\.(0?" + "[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" + "[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" + "\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\\s" + "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.(" + "[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" + "[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" + "\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])"; return s.matches(pattern); } public boolean isInCityCode(String s){ String pattern = "0791"; return s.equals(pattern); } public boolean isInProvinceCode(String s){ String pattern = "((0790)|(0701)|((079)[2-9]))"; return s.matches(pattern); } } class LandlinePhoneCharging extends ChargeMode{ private double monthlyRent = 20; public double calCost(UserRecords userRecords){ double sum=0; sum = getChargeRules().get(0).calCost(userRecords.getCallingInCityRecords())+getChargeRules().get(1).calCost(userRecords.getCallingInProvinceRecords())+getChargeRules().get(2).calCost(userRecords.getCallingInLandRecords()); return sum; } public double getMonthlyRent(){ return monthlyRent; } } class LandPhoneCharging { private double monthly = 20; } class LandPhoneInCityRule extends CallChargeRule{ public double calCost(ArrayList<CallRecord> callRecords) { double sumCost=0; long minute; for (CallRecord callRecord:callRecords ) { long startTime = callRecord.getStartTime().getTime(); long endTime = callRecord.getEndTime().getTime(); long second = (endTime-startTime)/1000; if (second%60>0){ minute = second/60+1; } else { minute = second/60; } sumCost = sumCost+minute*0.1; } return sumCost; } } class LandPhoneInProvinceRule extends CallChargeRule{ public double calCost(ArrayList<CallRecord> callRecords){ double sumCost=0; long minute; for (CallRecord callRecord:callRecords ) { long startTime = callRecord.getStartTime().getTime(); long endTime = callRecord.getEndTime().getTime(); long second = (endTime-startTime)/1000; if (second%60>0){ minute = second/60+1; } else { minute = second/60; } sumCost = sumCost+minute*0.3; } return sumCost; } } class LandPhonelnlandRule extends CallChargeRule{ public double calCost(ArrayList<CallRecord> callRecords){ double sumCost=0; long minute; for (CallRecord callRecord:callRecords ) { long startTime = callRecord.getStartTime().getTime(); long endTime = callRecord.getEndTime().getTime(); long second = (endTime-startTime)/1000; if (second%60>0){ minute = second/60+1; } else { minute = second/60; } sumCost = sumCost+minute*0.6; } return sumCost; } } class MessageRecord extends CommunicationRecord{ private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } 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 0; } public double calCost(){ return 0; } public double getBalance() { return balance-chargeMode.calCost(userRecords)-chargeMode.getMonthlyRent(); } public UserRecords getUserRecords() { return userRecords; } public void setUserRecords(UserRecords userRecords) { this.userRecords = userRecords; } 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; } } class UserRecords { private ArrayList<CallRecord> callingInCityRecords = new ArrayList<>(); private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<>(); private ArrayList<CallRecord> callingInLandRecords = new ArrayList<>(); private ArrayList<CallRecord> answerInCityRecords = new ArrayList<>(); private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<>(); private ArrayList<CallRecord> answerInLandRecords = new ArrayList<>(); private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<>(); private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<>(); public void addCallingInCityRecords(CallRecord callRecord){ callingInCityRecords.add(callRecord); } public void addCallingInProvinceRecords(CallRecord callRecord){ callingInProvinceRecords.add(callRecord); } public void addCallingInLandRecords(CallRecord callRecord){ callingInLandRecords.add(callRecord); } public void addAnswerInCityRecords(CallRecord callRecord){ answerInCityRecords.add(callRecord); } public void addAnswerInProvinceRecords(CallRecord callRecord){ answerInProvinceRecords.add(callRecord); } public void addAnswerInLandRecords(CallRecord callRecord){ answerInLandRecords.add(callRecord); } 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; } }
![复制代码](https://common.cnblogs.com/images/copycode.gif)
2.题目分析:本题是电信计费系列的第一题,也是相对后面两题难度最大的一题,因为需要根据所给的类图构建本系列题目的大体框架,后续题目再在框架上继续延伸扩展,设计难度和代码量较大。
本题需要注意的有以下几个点:
1.输入格式的正则表达式,要注意日期的合法性。
2.根据题目要求,忽略重复开户的操作。
3.具体实现思路:
创建一个可储存用户的容器,判断输入的信息,如果判断为开户信息,再判断容器中是否存在相同的账户号码,不存在则在容器中添加该用户,如果判断为通话信息,在判断拨打号码和收听号码是否是容器中的号码,若是则将
拨打(收听)信息存入用户中的储存相应信息的容器中。最后的计费则是将用户里的每条拨打信息的时间提取出来进行计算即可。
4.遇到的bug
因为座机计费规则还是比较简单,并且只有座机与座机的通话所以没有遇到很大的bug,只遇到了将通讯时间计算错误,以及日期的正则表达式错误两个较小的bug。
2.7-1 电信计费系列2-手机+座机计费
实现南昌市电信分公司的计费程序,假设该公司针对手机和座机用户分别采取了两种计费方案,分别如下:
1、针对市内座机用户采用的计费方式(与电信计费系列1内容相同):
月租20元,接电话免费,市内拨打电话0.1元/分钟,省内长途0.3元/分钟,国内长途拨打0.6元/分钟。不足一分钟按一分钟计。
假设本市的区号:0791,江西省内各地市区号包括:0790~0799以及0701。
2、针对手机用户采用实时计费方式:
月租15元,市内省内接电话均免费,市内拨打市内电话0.1元/分钟,市内拨打省内电话0.2元/分钟,市内拨打省外电话0.3元/分钟,省内漫游打电话0.3元/分钟,省外漫游接听0.3元/分钟,省外漫游拨打0.6元/分钟;
注:被叫电话属于市内、省内还是国内由被叫电话的接听地点区号决定,比如以下案例中,南昌市手机用户13307912264在区号为020的广州接听了电话,主叫号码应被计算为拨打了一个省外长途,同时,手机用户13307912264也要被计算省外接听漫游费:
u-13307912264 1
t-079186330022 13307912264 020 2022.1.3 10:00:25 2022.1.3 10:05:11
输入:
输入信息包括两种类型
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、2、3:
图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三个类分别是座机拨打市内、
源码:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) throws ParseException {
LandPhonelnlandRule landPhonelnlandRule = new LandPhonelnlandRule();
LandPhoneInCityRule landPhoneInCityRule = new LandPhoneInCityRule();
LandPhoneInProvinceRule landPhoneInProvinceRule = new LandPhoneInProvinceRule();
LandlinePhoneCharging landlinePhoneCharging = new LandlinePhoneCharging();
landlinePhoneCharging.getChargeRules().add(landPhoneInCityRule);
landlinePhoneCharging.getChargeRules().add(landPhoneInProvinceRule);
landlinePhoneCharging.getChargeRules().add(landPhonelnlandRule);
MobilePhoneInCityRule mobilePhoneInCityRule = new MobilePhoneInCityRule();
MobilePhoneInProvinceRule mobilePhoneInProvinceRule = new MobilePhoneInProvinceRule();
MobilePhoneInLandRule mobilePhoneInLandRule = new MobilePhoneInLandRule();
MobilePhoneCallOutCityRule mobilePhoneCallOutCityRule = new MobilePhoneCallOutCityRule();
MobilePhoneCallOutProvince mobilePhoneCallOutProvince = new MobilePhoneCallOutProvince();
MobilePhoneAnswerOutProvince mobilePhoneAnswerOutProvince = new MobilePhoneAnswerOutProvince();
MobilePhoneCharging mobilePhoneCharging = new MobilePhoneCharging();
mobilePhoneCharging.getChargeRules1().add(mobilePhoneInCityRule);
mobilePhoneCharging.getChargeRules1().add(mobilePhoneInProvinceRule);
mobilePhoneCharging.getChargeRules1().add(mobilePhoneInLandRule);
mobilePhoneCharging.getChargeRules1().add(mobilePhoneCallOutCityRule);
mobilePhoneCharging.getChargeRules1().add(mobilePhoneCallOutProvince);
mobilePhoneCharging.getChargeRules1().add(mobilePhoneAnswerOutProvince);
ArrayList<User> UserList = new ArrayList<>();
ArrayList<User> User1List = new ArrayList<>();
Scanner in = new Scanner(System.in);
String m = in.nextLine();
int flag;
while (!m.equals("end")) {
Judge judge = new Judge();
if (judge.isUserInformation(m)) {
String regex = "(0791)[\\d]{7,8}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(m);
while (matcher.find()) {
flag = 0;
for (User user : UserList
) {
if (user.getNumber().equals(matcher.group(0))) {
flag = 1;
break;
}
}
if (flag == 0) {
UserList.add(new User(matcher.group(0)));
}
}
}
if (judge.isUser1Information(m)) {
String regex = "[\\d][\\d]{10}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(m);
while (matcher.find()) {
flag = 0;
for (User user : User1List
) {
if (user.getNumber().equals(matcher.group(0))) {
flag = 1;
break;
}
}
if (flag == 0) {
User1List.add(new User(matcher.group(0)));
}
}
}
if (judge.isCallingInformation(m)) {
String[] a = m.split(" ");
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
Date startTime = format.parse(a[2] + " " + a[3]);
Date endTime = format.parse(a[4] + " " + a[5]);
for (User user : UserList
) {
if (user.getNumber().equals(a[0].substring(2))) {
if (judge.isInCityCode(a[1].substring(0, 4))) {
user.getUserRecords().addCallingInCityRecords(new CallRecord(startTime, endTime, a[0].substring(2, 6), a[1].substring(0, 4)));
} else if (judge.isInProvinceCode(a[1].substring(0, 4))) {
user.getUserRecords().addCallingInProvinceRecords(new CallRecord(startTime, endTime, a[0].substring(2, 6), a[1].substring(0, 4)));
} else {
user.getUserRecords().addCallingInLandRecords(new CallRecord(startTime, endTime, a[0].substring(2, 6), a[1].substring(0, 4)));
}
}
}
} catch (ParseException e) {
e.printStackTrace();
}
}
if (judge.isCalling01Information(m)) {
String[] a = m.split(" ");
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
Date startTime = format.parse(a[3] + " " + a[4]);
Date endTime = format.parse(a[5] + " " + a[6]);
for (User user : UserList
) {
if (user.getNumber().equals(a[0].substring(2))) {
if (judge.isInCityCode(a[2])) {
user.getUserRecords().addCallingInCityRecords(new CallRecord(startTime, endTime, a[0].substring(2, 6), a[2]));
} else if (judge.isInProvinceCode(a[2])) {
user.getUserRecords().addCallingInProvinceRecords(new CallRecord(startTime, endTime, a[0].substring(2, 6), a[2]));
} else {
user.getUserRecords().addCallingInLandRecords(new CallRecord(startTime, endTime, a[0].substring(2, 6), a[2]));
}
}
}
for (User user : User1List
) {
if (user.getNumber().equals(a[1])) {
if (judge.isAnswerInCityCode1(a[2])) {
;
} else if (judge.isAnswerInProvinceCode1(a[2])) {
;
} else {
user.getUserRecords().addAnswerInLandRecords(new CallRecord(startTime, endTime, a[0].substring(2, 6), a[2]));
}
}
}
} catch (ParseException e) {
e.printStackTrace();
}
}
if (judge.isCalling11Information(m)) {
String[] a = m.split(" ");
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
Date startTime = format.parse(a[4] + " " + a[5]);
Date endTime = format.parse(a[6] + " " + a[7]);
for (User user : User1List
) {
if (user.getNumber().equals(a[0].substring(2))) {
if (judge.isCallInCity(a[1])) {
if (judge.isInCityCode(a[3])) {
user.getUserRecords().addCallingInCityRecords(new CallRecord(startTime, endTime, a[1], a[3]));
} else if (judge.isInProvinceCode(a[3])) {
user.getUserRecords().addCallingInProvinceRecords(new CallRecord(startTime, endTime, a[1], a[3]));
} else {
user.getUserRecords().addCallingInLandRecords(new CallRecord(startTime, endTime, a[1], a[3]));
}
} else if (judge.isInProvinceCode(a[1])) {
user.getUserRecords().addManYouInProvinceRecords(new CallRecord(startTime, endTime, a[1], a[3]));
} else {
user.getUserRecords().addManYouOutProvinceRecords(new CallRecord(startTime, endTime, a[1], a[3]));
}
}
}
for (User user : User1List
) {
if (user.getNumber().equals(a[2])) {
if (judge.isAnswerInCityCode1(a[3])) {
;
} else if (judge.isAnswerInProvinceCode1(a[3])) {
;
} else {
user.getUserRecords().addAnswerInLandRecords(new CallRecord(startTime, endTime, a[0].substring(2, 6), a[2]));
}
}
}
} catch (ParseException e) {
e.printStackTrace();
}
}
if (judge.isCalling10Information(m)) {
String[] a = m.split(" ");
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
Date startTime = format.parse(a[3] + " " + a[4]);
Date endTime = format.parse(a[5] + " " + a[6]);
for (User user : User1List
) {
if (user.getNumber().equals(a[0].substring(2))) {
if (judge.isCallInCity(a[1])) {
if (judge.isInCityCode(a[2].substring(0,4))) {
user.getUserRecords().addCallingInCityRecords(new CallRecord(startTime, endTime, a[1], a[3]));
} else if (judge.isInProvinceCode(a[2].substring(0,4))) {
user.getUserRecords().addCallingInProvinceRecords(new CallRecord(startTime, endTime, a[1], a[3]));
} else {
user.getUserRecords().addCallingInLandRecords(new CallRecord(startTime, endTime, a[1], a[3]));
}
} else if (judge.isInProvinceCode(a[1])) {
user.getUserRecords().addManYouInProvinceRecords(new CallRecord(startTime, endTime, a[1], a[3]));
} else {
user.getUserRecords().addManYouOutProvinceRecords(new CallRecord(startTime, endTime, a[1], a[3]));
}
}
}
} catch (ParseException e) {
e.printStackTrace();
}
}
m = in.nextLine();
}
for (User user : UserList
) {
user.setChargeMode(landlinePhoneCharging);
}
for (User user:User1List
) {
user.setChargeMode(mobilePhoneCharging);
}
for (int i = 0; i < UserList.size(); i++) {
int index = -1;
Double min = Double.parseDouble(UserList.get(i).getNumber());
for (int j = i + 1; j < UserList.size(); j++) {
if (Double.parseDouble(UserList.get(j).getNumber()) < min) {
min = Double.parseDouble(UserList.get(j).getNumber());
index = j;
}
}
if (min != Double.parseDouble(UserList.get(i).getNumber())) {
User user = UserList.get(i);
UserList.set(i, UserList.get(index));
UserList.set(index, user);
}
}
for (int i = 0; i < User1List.size(); i++) {
int index = -1;
Double min = Double.parseDouble(User1List.get(i).getNumber());
for (int j = i + 1; j < User1List.size(); j++) {
if (Double.parseDouble(User1List.get(j).getNumber()) < min) {
min = Double.parseDouble(User1List.get(j).getNumber());
index = j;
}
}
if (min != Double.parseDouble(User1List.get(i).getNumber())) {
User user = User1List.get(i);
User1List.set(i, User1List.get(index));
User1List.set(index, user);
}
}
for (User user : UserList
) {
System.out.printf("%s %.1f %.1f\n", user.getNumber(), user.getChargeMode().calCost(user.getUserRecords()), user.getBalance());
}
for (User user : User1List
) {
System.out.printf("%s %.1f %.1f\n", user.getNumber(), user.getChargeMode().calCost(user.getUserRecords()), user.getBalance());
}
}
}
abstract class CallChargeRule extends ChargeRule{
public double calCost(ArrayList<CallRecord> callRecords){
return 0;
}
}
class CallRecord extends CommunicationRecord{
private Date startTime;
private Date endTime;
private String callingAddressAreaCode;
private String answerAddressAreaCode;
public CallRecord(Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) {
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;
}
}
abstract class ChargeMode {
private ArrayList<ChargeRule> chargeRules = new ArrayList<>();
private ArrayList<ChargeRule> chargeRules1 = new ArrayList<>();
public ArrayList<ChargeRule> getChargeRules1() {
return chargeRules1;
}
public ArrayList<ChargeRule> getChargeRules() {
return chargeRules;
}
public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
this.chargeRules = chargeRules;
}
public double calCost(UserRecords userRecords){
return 0;
}
public double getMonthlyRent(){
return 0;
}
}
abstract class ChargeRule {
public double calCost(ArrayList<CallRecord> callRecords){
return 0;
}
}
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 Judge {
public boolean isUserInformation(String s){
String pattern ="(u-0791)[\\d]{7,8}[\\s][0-2]";
return s.matches(pattern);
}
public boolean isUser1Information(String s){
String pattern = "(u-)[1][\\d]{10}[\\s][0-2]";
return s.matches(pattern);
}
public boolean isCallingInformation(String s){
String pattern = "[t]-0791[0-9]{7,8}\\s" + "0[0-9]{9,11}\\s" +
"((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.(((0?[13578]|1[02])\\.(0?" +
"[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" +
"[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" +
"\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\\s" +
"((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.(" +
"[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" +
"[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" +
"\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])";
return s.matches(pattern);
}
public boolean isCalling01Information(String s){
String pattern = "[t]-0[0-9]{9,11}\\s" + "[1][\\d]{10}\\s"+"[\\d]{3,4}\\s"+
"((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.(((0?[13578]|1[02])\\.(0?" +
"[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" +
"[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" +
"\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\\s" +
"((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.(" +
"[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" +
"[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" +
"\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])";
return s.matches(pattern);
}
public boolean isCalling11Information(String s){
String pattern = "(t-)[1][\\d]{10}\\s"+"[\\d]{3,4}\\s" + "[1][\\d]{10}\\s"+"[\\d]{3,4}\\s"+
"((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.(((0?[13578]|1[02])\\.(0?" +
"[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" +
"[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" +
"\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\\s" +
"((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.(" +
"[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" +
"[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" +
"\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])";
return s.matches(pattern);
}
public boolean isCalling10Information(String s){
String pattern = "(t-)[1][\\d]{10}\\s"+"[\\d]{3,4}\\s" + "0[0-9]{9,11}\\s"+
"((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.(((0?[13578]|1[02])\\.(0?" +
"[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" +
"[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" +
"\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\\s" +
"((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.(" +
"[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" +
"[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" +
"\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])";
return s.matches(pattern);
}
public boolean isInCityCode(String s){
String pattern = "0791";
return s.equals(pattern);
}
public boolean isInProvinceCode(String s){
String pattern = "((0790)|(0701)|((079)[2-9]))";
return s.matches(pattern);
}
public boolean isAnswerInCityCode1(String s){
String pattern = "0791";
return s.equals(pattern);
}
public boolean isAnswerInProvinceCode1(String s){
String pattern = "((0790)|(0701)|((079)[2-9]))";
return s.matches(pattern);
}
public boolean isCallInCity(String s){
String pattern = "0791";
return s.equals(pattern);
}
public boolean isCallInProvince(String s){
String pattern = "((0790)|(0701)|((079)[2-9]))";
return s.matches(pattern);
}
}
class LandlinePhoneCharging extends ChargeMode{
private double monthlyRent = 20;
public double calCost(UserRecords userRecords){
double sum=0;
sum = getChargeRules().get(0).calCost(userRecords.getCallingInCityRecords())+getChargeRules().get(1).calCost(userRecords.getCallingInProvinceRecords())+getChargeRules().get(2).calCost(userRecords.getCallingInLandRecords());
return sum;
}
public double getMonthlyRent(){
return monthlyRent;
}
}
class LandPhoneCharging {
private double monthly = 20;
}
class LandPhoneInCityRule extends CallChargeRule{
public double calCost(ArrayList<CallRecord> callRecords) {
double sumCost=0;
long minute;
for (CallRecord callRecord:callRecords
) {
long startTime = callRecord.getStartTime().getTime();
long endTime = callRecord.getEndTime().getTime();
long second = (endTime-startTime)/1000;
if (second%60>0){
minute = second/60+1;
}
else {
minute = second/60;
}
sumCost = sumCost+minute*0.1;
}
return sumCost;
}
}
class LandPhoneInProvinceRule extends CallChargeRule{
public double calCost(ArrayList<CallRecord> callRecords){
double sumCost=0;
long minute;
for (CallRecord callRecord:callRecords
) {
long startTime = callRecord.getStartTime().getTime();
long endTime = callRecord.getEndTime().getTime();
long second = (endTime-startTime)/1000;
if (second%60>0){
minute = second/60+1;
}
else {
minute = second/60;
}
sumCost = sumCost+minute*0.3;
}
return sumCost;
}
}
class LandPhonelnlandRule extends CallChargeRule{
public double calCost(ArrayList<CallRecord> callRecords){
double sumCost=0;
long minute;
for (CallRecord callRecord:callRecords
) {
long startTime = callRecord.getStartTime().getTime();
long endTime = callRecord.getEndTime().getTime();
long second = (endTime-startTime)/1000;
if (second%60>0){
minute = second/60+1;
}
else {
minute = second/60;
}
sumCost = sumCost+minute*0.6;
}
return sumCost;
}
}
class MessageRecord extends CommunicationRecord{
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
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 0;
}
public double calCost(){
return 0;
}
public double getBalance() {
return balance-chargeMode.calCost(userRecords)-chargeMode.getMonthlyRent();
}
public UserRecords getUserRecords() {
return userRecords;
}
public void setUserRecords(UserRecords userRecords) {
this.userRecords = userRecords;
}
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;
}
}
class UserRecords {
private ArrayList<CallRecord> callingInCityRecords = new ArrayList<>();
private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<>();
private ArrayList<CallRecord> callingInLandRecords = new ArrayList<>();
private ArrayList<CallRecord> answerInCityRecords = new ArrayList<>();
private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<>();
private ArrayList<CallRecord> answerInLandRecords = new ArrayList<>();
private ArrayList<CallRecord> manYouInProvinceRecords = new ArrayList<>();
private ArrayList<CallRecord> manYouOutProvinceRecords = new ArrayList<>();
private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<>();
private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<>();
public void addManYouInProvinceRecords(CallRecord callRecord){
manYouInProvinceRecords.add(callRecord);
}
public ArrayList<CallRecord> getManYouInProvinceRecords() {
return manYouInProvinceRecords;
}
public ArrayList<CallRecord> getManYouOutProvinceRecords() {
return manYouOutProvinceRecords;
}
public void addManYouOutProvinceRecords(CallRecord callRecord){
manYouOutProvinceRecords.add(callRecord);
}
public void addCallingInCityRecords(CallRecord callRecord){
callingInCityRecords.add(callRecord);
}
public void addCallingInProvinceRecords(CallRecord callRecord){
callingInProvinceRecords.add(callRecord);
}
public void addCallingInLandRecords(CallRecord callRecord){
callingInLandRecords.add(callRecord);
}
public void addAnswerInCityRecords(CallRecord callRecord){
answerInCityRecords.add(callRecord);
}
public void addAnswerInProvinceRecords(CallRecord callRecord){
answerInProvinceRecords.add(callRecord);
}
public void addAnswerInLandRecords(CallRecord callRecord){
answerInLandRecords.add(callRecord);
}
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;
}
}
class MobilePhoneCallOutCityRule extends CallChargeRule{
public double calCost(ArrayList<CallRecord> callRecords) {
double sumCost=0;
long minute;
for (CallRecord callRecord:callRecords
) {
long startTime = callRecord.getStartTime().getTime();
long endTime = callRecord.getEndTime().getTime();
long second = (endTime-startTime)/1000;
if (second%60>0){
minute = second/60+1;
}
else {
minute = second/60;
}
sumCost = sumCost+minute*0.3;
}
return sumCost;
}
}
class MobilePhoneAnswerOutProvince extends CallChargeRule{
public double calCost(ArrayList<CallRecord> callRecords) {
double sumCost=0;
long minute;
for (CallRecord callRecord:callRecords
) {
long startTime = callRecord.getStartTime().getTime();
long endTime = callRecord.getEndTime().getTime();
long second = (endTime-startTime)/1000;
if (second%60>0){
minute = second/60+1;
}
else {
minute = second/60;
}
sumCost = sumCost+minute*0.3;
}
return sumCost;
}
}
class MobilePhoneCallOutProvince extends CallChargeRule{
public double calCost(ArrayList<CallRecord> callRecords) {
double sumCost=0;
long minute;
for (CallRecord callRecord:callRecords
) {
long startTime = callRecord.getStartTime().getTime();
long endTime = callRecord.getEndTime().getTime();
long second = (endTime-startTime)/1000;
if (second%60>0){
minute = second/60+1;
}
else {
minute = second/60;
}
sumCost = sumCost+minute*0.6;
}
return sumCost;
}
}
class MobilePhoneCharging extends ChargeMode{
private double monthlyRent = 15;
public double calCost(UserRecords userRecords){
double sum=0;
sum = getChargeRules1().get(0).calCost(userRecords.getCallingInCityRecords())+getChargeRules1().get(1).calCost(userRecords.getCallingInProvinceRecords())+getChargeRules1().get(2).calCost(userRecords.getCallingInLandRecords())+getChargeRules1().get(3).calCost(userRecords.getManYouInProvinceRecords())+getChargeRules1().get(4).calCost(userRecords.getManYouOutProvinceRecords())+getChargeRules1().get(5).calCost(userRecords.getAnswerInLandRecords());;
return sum;
}
public double getMonthlyRent(){
return monthlyRent;
}
}
class MobilePhoneInCityRule extends CallChargeRule{
public double calCost(ArrayList<CallRecord> callRecords) {
double sumCost=0;
long minute;
for (CallRecord callRecord:callRecords
) {
long startTime = callRecord.getStartTime().getTime();
long endTime = callRecord.getEndTime().getTime();
long second = (endTime-startTime)/1000;
if (second%60>0){
minute = second/60+1;
}
else {
minute = second/60;
}
sumCost = sumCost+minute*0.1;
}
return sumCost;
}
}
class MobilePhoneInLandRule extends CallChargeRule{
public double calCost(ArrayList<CallRecord> callRecords) {
double sumCost=0;
long minute;
for (CallRecord callRecord:callRecords
) {
long startTime = callRecord.getStartTime().getTime();
long endTime = callRecord.getEndTime().getTime();
long second = (endTime-startTime)/1000;
if (second%60>0){
minute = second/60+1;
}
else {
minute = second/60;
}
sumCost = sumCost+minute*0.3;
}
return sumCost;
}
}
class MobilePhoneInProvinceRule extends CallChargeRule{
public double calCost(ArrayList<CallRecord> callRecords) {
double sumCost=0;
long minute;
for (CallRecord callRecord:callRecords
) {
long startTime = callRecord.getStartTime().getTime();
long endTime = callRecord.getEndTime().getTime();
long second = (endTime-startTime)/1000;
if (second%60>0){
minute = second/60+1;
}
else {
minute = second/60;
}
sumCost = sumCost+minute*0.2;
}
return sumCost;
}
}
1.题目分析:本题的计费形式对于上一题多了很多种,主要是手机的计费规则多了省内漫游和省外漫游。
本题需要注意以下几点:
1.手机计费根据哪套规则记录费用不是按照开户时的地址进行计算,而是根据拨打人拨打时所在的区号以及接听人接听所在区号进行计费。
实现思路:在上题的基础上,增加储存手机用户的容器,增加相应手机计费规则,以及判断手机通讯信息的正则表达式,储存手机用户的方式和上题储存座机用户的方式基本一致,根据拨打人所在区号根据接听人的区号判断是市内省内还是国内长途信息等类型,在用户信息里建立对应的容器储存它们。
3.遇到的bug
这道题遇到bug的原因大多是在计费方面,由于存在座机打手机,手机打手机,手机打座机等,所以在数据储存时,他们的信息格式各不相同,出现了一些信息储存错误的小bug。
3.7-1 电信计费系列3-短信计费
其他题目信息与上题相同,主要多了以下内容:
实现一个简单的电信计费程序,针对手机的短信采用如下计费方式:
1、接收短信免费,发送短信0.1元/条,超过3条0.2元/条,超过5条0.3元/条。
2、如果一次发送短信的字符数量超过10个,按每10个字符一条短信进行计算。
3、逐行输入本月某些用户的短信信息,短信的格式:
m-主叫号码,接收号码,短信内容 (短信内容只能由数字、字母、空格、英文逗号、英文句号组成)
m-18907910010 13305862264 welcome to jiangxi.
m-13305862264 18907910010 thank you.
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.text.ParseException; 2 import java.text.SimpleDateFormat; 3 import java.util.ArrayList; 4 import java.util.Date; 5 import java.util.Scanner; 6 import java.util.regex.Matcher; 7 import java.util.regex.Pattern; 8 9 public class Main { 10 public static void main(String[] args) throws ParseException { 11 ArrayList<User> User1List = new ArrayList<>(); 12 ArrayList<MessageChargeRule> messageChargeRules = new ArrayList<>(); 13 SendMessageRule sendMessageRule = new SendMessageRule(); 14 MessageCharging messageCharging = new MessageCharging(); 15 messageCharging.getMessageChargeRules().add(sendMessageRule); 16 Scanner in = new Scanner(System.in); 17 String m = in.nextLine(); 18 int flag; 19 while (!m.equals("end")) { 20 Judge judge = new Judge(); 21 22 if (judge.isUser1Information(m)) { 23 String regex = "[1][\\d]{10}"; 24 Pattern pattern = Pattern.compile(regex); 25 Matcher matcher = pattern.matcher(m); 26 while (matcher.find()) { 27 flag = 0; 28 for (User user : User1List 29 ) { 30 if (user.getNumber().equals(matcher.group(0))) { 31 flag = 1; 32 break; 33 } 34 } 35 if (flag == 0) { 36 User1List.add(new User(matcher.group(0))); 37 } 38 } 39 } 40 if (judge.isMessageInformation(m)){ 41 for (User user:User1List 42 ) { 43 if (user.getNumber().equals(m.substring(2,13))){ 44 user.getUserRecords().addSendMessageRecords(new MessageRecord(m.substring(26))); 45 } 46 } 47 } 48 m = in.nextLine(); 49 } 50 for (User user:User1List 51 ) { 52 user.setChargeMode(messageCharging); 53 } 54 55 for (int i = 0; i < User1List.size(); i++) { 56 int index = -1; 57 Double min = Double.parseDouble(User1List.get(i).getNumber()); 58 for (int j = i + 1; j < User1List.size(); j++) { 59 if (Double.parseDouble(User1List.get(j).getNumber()) < min) { 60 min = Double.parseDouble(User1List.get(j).getNumber()); 61 index = j; 62 } 63 } 64 if (min != Double.parseDouble(User1List.get(i).getNumber())) { 65 User user = User1List.get(i); 66 User1List.set(i, User1List.get(index)); 67 User1List.set(index, user); 68 } 69 } 70 for (User user:User1List 71 ) { 72 System.out.printf("%s %.1f %.1f\n",user.getNumber(),user.getChargeMode().calCost(user.getUserRecords()),user.getMessageBalance()); 73 } 74 75 76 } 77 } 78 class MessageCharging extends ChargeMode{ 79 public double calCost(UserRecords userRecords){ 80 return getMessageChargeRules().get(0).calCost(userRecords.getSendMessageRecords()); 81 } 82 } 83 abstract class CallChargeRule extends ChargeRule{ 84 public double calCost(ArrayList<CallRecord> callRecords){ 85 return 0; 86 } 87 } 88 abstract class MessageChargeRule extends ChargeRule{ 89 public double calCost(ArrayList<MessageRecord> messageRecords){ 90 return 0; 91 } 92 } 93 class SendMessageRule extends MessageChargeRule{ 94 public double calCost(ArrayList<MessageRecord> messageRecords){ 95 int count = 0; 96 double sum; 97 for (MessageRecord messageRecord:messageRecords 98 ) { 99 if (messageRecord.getMessage().length()%10!=0){ 100 count+=messageRecord.getMessage().length()/10+1; 101 } 102 else { 103 count+=messageRecord.getMessage().length()/10; 104 } 105 } 106 if (count<=3){ 107 sum=count*0.1; 108 } 109 else if (count>3&&count<=5){ 110 sum=0.3+(count-3)*0.2; 111 } 112 else { 113 sum=0.3+0.4+(count-5)*0.3; 114 } 115 return sum; 116 } 117 } 118 class CallRecord extends CommunicationRecord{ 119 private Date startTime; 120 private Date endTime; 121 private String callingAddressAreaCode; 122 private String answerAddressAreaCode; 123 124 public CallRecord(Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) { 125 this.startTime = startTime; 126 this.endTime = endTime; 127 this.callingAddressAreaCode = callingAddressAreaCode; 128 this.answerAddressAreaCode = answerAddressAreaCode; 129 } 130 131 public Date getStartTime() { 132 return startTime; 133 } 134 135 public void setStartTime(Date startTime) { 136 this.startTime = startTime; 137 } 138 139 public Date getEndTime() { 140 return endTime; 141 } 142 143 public void setEndTime(Date endTime) { 144 this.endTime = endTime; 145 } 146 147 public String getCallingAddressAreaCode() { 148 return callingAddressAreaCode; 149 } 150 151 public void setCallingAddressAreaCode(String callingAddressAreaCode) { 152 this.callingAddressAreaCode = callingAddressAreaCode; 153 } 154 155 public String getAnswerAddressAreaCode() { 156 return answerAddressAreaCode; 157 } 158 159 public void setAnswerAddressAreaCode(String answerAddressAreaCode) { 160 this.answerAddressAreaCode = answerAddressAreaCode; 161 } 162 } 163 abstract class ChargeMode { 164 private ArrayList<ChargeRule> chargeRules = new ArrayList<>(); 165 private ArrayList<ChargeRule> chargeRules1 = new ArrayList<>(); 166 private ArrayList<MessageChargeRule> messageChargeRules = new ArrayList<>(); 167 168 public void setChargeRules1(ArrayList<ChargeRule> chargeRules1) { 169 this.chargeRules1 = chargeRules1; 170 } 171 172 public ArrayList<MessageChargeRule> getMessageChargeRules() { 173 return messageChargeRules; 174 } 175 176 public void setMessageChargeRules(ArrayList<MessageChargeRule> messageChargeRules) { 177 this.messageChargeRules = messageChargeRules; 178 } 179 180 public ArrayList<ChargeRule> getChargeRules1() { 181 return chargeRules1; 182 } 183 public ArrayList<ChargeRule> getChargeRules() { 184 return chargeRules; 185 } 186 187 public void setChargeRules(ArrayList<ChargeRule> chargeRules) { 188 this.chargeRules = chargeRules; 189 } 190 191 192 public double calCost(UserRecords userRecords){ 193 return 0; 194 } 195 public double getMonthlyRent(){ 196 return 0; 197 } 198 } 199 abstract class ChargeRule { 200 ; 201 202 203 204 } 205 abstract class CommunicationRecord { 206 protected String callingNumber; 207 protected String answerNumber; 208 209 public String getCallingNumber() { 210 return callingNumber; 211 } 212 213 public void setCallingNumber(String callingNumber) { 214 this.callingNumber = callingNumber; 215 } 216 217 public String getAnswerNumber() { 218 return answerNumber; 219 } 220 221 public void setAnswerNumber(String answerNumber) { 222 this.answerNumber = answerNumber; 223 } 224 } 225 class Judge { 226 public boolean isMessageInformation(String s){ 227 String pattern = "(m-)1[\\d]{10}\\s1[\\d]{10}\\s[A-Za-z0-9\\s,.]+"; 228 return s.matches(pattern); 229 } 230 public boolean isUser1Information(String s){ 231 String pattern = "(u-)[1][\\d]{10}[\\s][0-3]"; 232 return s.matches(pattern); 233 } 234 } 235 class LandlinePhoneCharging extends ChargeMode{ 236 private double monthlyRent = 20; 237 238 public double calCost(UserRecords userRecords){ 239 240 return 0; 241 } 242 public double getMonthlyRent(){ 243 return monthlyRent; 244 } 245 } 246 class LandPhoneCharging { 247 private double monthly = 20; 248 249 } 250 class LandPhoneInCityRule extends CallChargeRule{ 251 public double calCost(ArrayList<CallRecord> callRecords) { 252 double sumCost=0; 253 long minute; 254 for (CallRecord callRecord:callRecords 255 ) { 256 long startTime = callRecord.getStartTime().getTime(); 257 long endTime = callRecord.getEndTime().getTime(); 258 long second = (endTime-startTime)/1000; 259 if (second%60>0){ 260 minute = second/60+1; 261 } 262 else { 263 minute = second/60; 264 } 265 sumCost = sumCost+minute*0.1; 266 } 267 return sumCost; 268 } 269 } 270 class LandPhoneInProvinceRule extends CallChargeRule{ 271 public double calCost(ArrayList<CallRecord> callRecords){ 272 double sumCost=0; 273 long minute; 274 for (CallRecord callRecord:callRecords 275 ) { 276 long startTime = callRecord.getStartTime().getTime(); 277 long endTime = callRecord.getEndTime().getTime(); 278 long second = (endTime-startTime)/1000; 279 if (second%60>0){ 280 minute = second/60+1; 281 } 282 else { 283 minute = second/60; 284 } 285 sumCost = sumCost+minute*0.3; 286 } 287 return sumCost; 288 } 289 } 290 class LandPhonelnlandRule extends CallChargeRule{ 291 public double calCost(ArrayList<CallRecord> callRecords){ 292 double sumCost=0; 293 long minute; 294 for (CallRecord callRecord:callRecords 295 ) { 296 long startTime = callRecord.getStartTime().getTime(); 297 long endTime = callRecord.getEndTime().getTime(); 298 long second = (endTime-startTime)/1000; 299 if (second%60>0){ 300 minute = second/60+1; 301 } 302 else { 303 minute = second/60; 304 } 305 sumCost = sumCost+minute*0.6; 306 } 307 return sumCost; 308 } 309 } 310 class MessageRecord extends CommunicationRecord{ 311 private String message; 312 313 public MessageRecord(String message) { 314 this.message = message; 315 } 316 317 public String getMessage() { 318 return message; 319 } 320 321 public void setMessage(String message) { 322 this.message = message; 323 } 324 } 325 class User { 326 private UserRecords userRecords = new UserRecords(); 327 private double balance = 100; 328 private ChargeMode chargeMode; 329 private String number; 330 public User(String number) { 331 this.number = number; 332 } 333 public double getMessageBalance(){ 334 return balance-chargeMode.calCost(userRecords); 335 } 336 public double getBalance() { 337 return balance-chargeMode.calCost(userRecords)-chargeMode.getMonthlyRent(); 338 } 339 340 public UserRecords getUserRecords() { 341 return userRecords; 342 } 343 344 public void setUserRecords(UserRecords userRecords) { 345 this.userRecords = userRecords; 346 } 347 348 public ChargeMode getChargeMode() { 349 return chargeMode; 350 } 351 352 public void setChargeMode(ChargeMode chargeMode) { 353 this.chargeMode = chargeMode; 354 } 355 356 public String getNumber() { 357 return number; 358 } 359 360 public void setNumber(String number) { 361 this.number = number; 362 } 363 } 364 class UserRecords { 365 private ArrayList<CallRecord> callingInCityRecords = new ArrayList<>(); 366 private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<>(); 367 private ArrayList<CallRecord> callingInLandRecords = new ArrayList<>(); 368 private ArrayList<CallRecord> answerInCityRecords = new ArrayList<>(); 369 private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<>(); 370 private ArrayList<CallRecord> answerInLandRecords = new ArrayList<>(); 371 private ArrayList<CallRecord> manYouInProvinceRecords = new ArrayList<>(); 372 private ArrayList<CallRecord> manYouOutProvinceRecords = new ArrayList<>(); 373 private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<>(); 374 private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<>(); 375 376 377 public void addManYouInProvinceRecords(CallRecord callRecord){ 378 manYouInProvinceRecords.add(callRecord); 379 } 380 381 public ArrayList<CallRecord> getManYouInProvinceRecords() { 382 return manYouInProvinceRecords; 383 } 384 385 public ArrayList<CallRecord> getManYouOutProvinceRecords() { 386 return manYouOutProvinceRecords; 387 } 388 389 public void addManYouOutProvinceRecords(CallRecord callRecord){ 390 manYouOutProvinceRecords.add(callRecord); 391 } 392 393 public void addCallingInCityRecords(CallRecord callRecord){ 394 callingInCityRecords.add(callRecord); 395 } 396 public void addCallingInProvinceRecords(CallRecord callRecord){ 397 callingInProvinceRecords.add(callRecord); 398 } 399 public void addCallingInLandRecords(CallRecord callRecord){ 400 callingInLandRecords.add(callRecord); 401 } 402 public void addAnswerInCityRecords(CallRecord callRecord){ 403 answerInCityRecords.add(callRecord); 404 } 405 public void addAnswerInProvinceRecords(CallRecord callRecord){ 406 answerInProvinceRecords.add(callRecord); 407 } 408 public void addAnswerInLandRecords(CallRecord callRecord){ 409 answerInLandRecords.add(callRecord); 410 } 411 412 public ArrayList<CallRecord> getCallingInCityRecords() { 413 return callingInCityRecords; 414 } 415 416 public ArrayList<CallRecord> getCallingInProvinceRecords() { 417 return callingInProvinceRecords; 418 } 419 420 public ArrayList<CallRecord> getCallingInLandRecords() { 421 return callingInLandRecords; 422 } 423 424 public ArrayList<CallRecord> getAnswerInCityRecords() { 425 return answerInCityRecords; 426 } 427 428 public ArrayList<CallRecord> getAnswerInProvinceRecords() { 429 return answerInProvinceRecords; 430 } 431 432 public ArrayList<CallRecord> getAnswerInLandRecords() { 433 return answerInLandRecords; 434 } 435 436 public ArrayList<MessageRecord> getSendMessageRecords() { 437 return sendMessageRecords; 438 } 439 440 public ArrayList<MessageRecord> getReceiveMessageRecords() { 441 return receiveMessageRecords; 442 } 443 public void addSendMessageRecords(MessageRecord messageRecord){ 444 sendMessageRecords.add(messageRecord); 445 } 446 447 448 } 449 class MobilePhoneCallOutCityRule extends CallChargeRule{ 450 public double calCost(ArrayList<CallRecord> callRecords) { 451 double sumCost=0; 452 long minute; 453 for (CallRecord callRecord:callRecords 454 ) { 455 long startTime = callRecord.getStartTime().getTime(); 456 long endTime = callRecord.getEndTime().getTime(); 457 long second = (endTime-startTime)/1000; 458 if (second%60>0){ 459 minute = second/60+1; 460 } 461 else { 462 minute = second/60; 463 } 464 sumCost = sumCost+minute*0.3; 465 } 466 return sumCost; 467 } 468 } 469 class MobilePhoneAnswerOutProvince extends CallChargeRule{ 470 public double calCost(ArrayList<CallRecord> callRecords) { 471 double sumCost=0; 472 long minute; 473 for (CallRecord callRecord:callRecords 474 ) { 475 long startTime = callRecord.getStartTime().getTime(); 476 long endTime = callRecord.getEndTime().getTime(); 477 long second = (endTime-startTime)/1000; 478 if (second%60>0){ 479 minute = second/60+1; 480 } 481 else { 482 minute = second/60; 483 } 484 sumCost = sumCost+minute*0.3; 485 } 486 return sumCost; 487 } 488 } 489 class MobilePhoneCallOutProvince extends CallChargeRule{ 490 public double calCost(ArrayList<CallRecord> callRecords) { 491 double sumCost=0; 492 long minute; 493 for (CallRecord callRecord:callRecords 494 ) { 495 long startTime = callRecord.getStartTime().getTime(); 496 long endTime = callRecord.getEndTime().getTime(); 497 long second = (endTime-startTime)/1000; 498 if (second%60>0){ 499 minute = second/60+1; 500 } 501 else { 502 minute = second/60; 503 } 504 sumCost = sumCost+minute*0.6; 505 } 506 return sumCost; 507 } 508 } 509 class MobilePhoneCharging extends ChargeMode{ 510 private double monthlyRent = 15; 511 512 public double calCost(UserRecords userRecords){ 513 return 0; 514 } 515 public double getMonthlyRent(){ 516 return monthlyRent; 517 } 518 } 519 class MobilePhoneInCityRule extends CallChargeRule{ 520 public double calCost(ArrayList<CallRecord> callRecords) { 521 double sumCost=0; 522 long minute; 523 for (CallRecord callRecord:callRecords 524 ) { 525 long startTime = callRecord.getStartTime().getTime(); 526 long endTime = callRecord.getEndTime().getTime(); 527 long second = (endTime-startTime)/1000; 528 if (second%60>0){ 529 minute = second/60+1; 530 } 531 else { 532 minute = second/60; 533 } 534 sumCost = sumCost+minute*0.1; 535 } 536 return sumCost; 537 } 538 } 539 class MobilePhoneInLandRule extends CallChargeRule{ 540 public double calCost(ArrayList<CallRecord> callRecords) { 541 double sumCost=0; 542 long minute; 543 for (CallRecord callRecord:callRecords 544 ) { 545 long startTime = callRecord.getStartTime().getTime(); 546 long endTime = callRecord.getEndTime().getTime(); 547 long second = (endTime-startTime)/1000; 548 if (second%60>0){ 549 minute = second/60+1; 550 } 551 else { 552 minute = second/60; 553 } 554 sumCost = sumCost+minute*0.3; 555 } 556 return sumCost; 557 } 558 } 559 class MobilePhoneInProvinceRule extends CallChargeRule{ 560 public double calCost(ArrayList<CallRecord> callRecords) { 561 double sumCost=0; 562 long minute; 563 for (CallRecord callRecord:callRecords 564 ) { 565 long startTime = callRecord.getStartTime().getTime(); 566 long endTime = callRecord.getEndTime().getTime(); 567 long second = (endTime-startTime)/1000; 568 if (second%60>0){ 569 minute = second/60+1; 570 } 571 else { 572 minute = second/60; 573 } 574 sumCost = sumCost+minute*0.2; 575 } 576 return sumCost; 577 } 578 }View Code
这个学期的JAVA编程学期收获颇多,面向对面的设计能力得到了巨大的提升,这次的学期对以后大二大三的的学习肯定能提供巨大的帮助,但在某些方面还需改进,如多态、抽象类、接口方面还需要进一步的深入学习,总的概括如下:
1:学到了什么:
①:可以根据需求分析来解决问题。
②:对面向对象的能力得到了提升。
③:能通过类间关系来降低代码的耦合性。
④:抽象类与堕胎方法的使用。
⑤:对书上后几个章节得到了学习,了解到了 各种在实际的JAVA编程方法(如JAVAFX、正则表达式,各种方法(如:Hashset、Array list等等))
2:需要进一步改进的地方:
①:自主学习的能力,由于这门课程采用的是线上线下相结合的方式,通过老师指定
的方式或其他方式自学是很重要的。
②:编码时的调试能力,在pta作业的完成中,错误无可避免,但是要学会自己通过调
试来发现问题所在并解决它。
③:对简便的追求要给与肯定,越复杂的题目越要追求简单化,从而获得更加好的交互体验,让程序更加 有可用性。
标签:return,String,ArrayList,callRecord,BLOG,new,public
From: https://www.cnblogs.com/fanyiguan/p/16970045.html