Blog-3
题目集总结:
(1)前言
题目集8:
知识点:类与对象、容器、泛型、抽象类、继承多态、正则表达式
题量:中
难度:难
题目集9:
知识点:类与对象、容器、泛型、抽象类、继承多态、正则表达式
题量:中
难度:难
题目集10:
知识点:类与对象、容器、泛型、抽象类、继承多态、正则表达式
题量:中
难度:中
(2)设计与分析
题目集8:
7-1 电信计费系列1-座机计费
分数 80
作者 蔡轲
单位 南昌航空大学
实现一个简单的电信计费程序:
假设南昌市电信分公司针对市内座机用户采用的计费方式:
月租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)。
后续扩展说明:
后续题目集将增加手机用户,手机用户的计费方式中除了与座机计费类似的主叫通话费之外,还包含市外接听电话的漫游费以及发短信的费用。在本题的设计时可统一考虑。
通话记录中,手机需要额外记录拨打/接听的地点的区号,比如:
座机打手机: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
分析:本题要求根据类图设计一个电信计费程序,最大的麻烦之处在于看懂整个类图,清楚每个类的用处,User类是用来储存用户的各类信息,包括电话号码、通话记录、计费方式等;ChargeMode类是用来确定用户的计费类型;UserRecords是用来储存用户的通讯记录,包括接电话与打电话在市内、省内、国内长途的记录和用户接发短信记录;CommunicationRecord是抽象的通话记录类,记录通话与短信;CallRecord用于记录通话起始时间和拨号、接听电话的区号;ChargeRule类是计费的具体规则,通过LandPhoneInCityRule、LandPhoneInLandRule、LandPhoneInProvinceRule三个类分别进行市内、省外、省内的计费计算。其次,要实现计费程序,步骤为先创建用户,再实现用户的通信计费。由于本次题目只需实现座机计费,因此不必考虑短信计费。在主函数中,依次输入开户信息和通讯信息后,依靠正则表达式对输入信息进行判断。开户信息通过创建user类的容器ArrayList中,通讯信息通过匹配到储存到容器中的用户信息进行具体运算后得到话费与余额信息。
题目集9:
7-1 电信计费系列2-手机+座机计费
分数 80
作者 蔡轲
单位 南昌航空大学
实现南昌市电信分公司的计费程序,假设该公司针对手机和座机用户分别采取了两种计费方案,分别如下:
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三个类分别是座机拨打市内、省内、省外电话的计费规则类,用于实现这三种情况的费用计算。
(提示:可以从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
分析:本题在上一题的基础上增加了手机计费功能,大体结构与上一题相似,根据新的手机计费规则写即可。
题目集10:
7-1 电信计费系列3-短信计费
分数 50
作者 蔡轲
单位 南昌航空大学
实现一个简单的电信计费程序,针对手机的短信采用如下计费方式:
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
分析:本题在上一题的基础上增加了短信计费功能,大体结构与上一题相似,根据新的短信计费规则写即可。
(3)采坑心得
题目集8:
7-1:在输出用户的话费与余额时需根据电话号码进行排序后在输出
我用的是Collection方法进行排序。
代码如下:
import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); ArrayList<Users> usersArrayList = new ArrayList<>(); String str = input.nextLine(); boolean repetition = false; while (!str.equals("end")){ if(str.matches("u\\-0791\\d{7,8}\\s0")){ String str1[] = str.split("-"); String str2[] = str1[1].split(" "); for(int i = 0;i < usersArrayList.size();i++){ if(usersArrayList.get(i).number.equals(str2[0])) { repetition = true; break; } } if(repetition == false){ usersArrayList.add(new Users(str2[0])); } } if(str.matches("t-(\\d){11,12}\\s(\\d){10,12}\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d")){ String str3[] = str.split("-"); String str4[] = str3[1].split(" "); for (int i = 0;i < usersArrayList.size();i++){ if(usersArrayList.get(i).number.equals(str4[0])){ if(str4[1].substring(0,4).matches("0791")){ usersArrayList.get(i).getUserRecords().addgetCallingInCityRecords(new CallRecord(str4[2]+" "+str4[3],str4[4]+" "+str4[5])); usersArrayList.get(i).setUserRecords(usersArrayList.get(i).getUserRecords()); } else if(str4[1].substring(0,4).matches("079\\d|0701")){ usersArrayList.get(i).getUserRecords().addgetCallingInProvinceRecords(new CallRecord(str4[2]+" "+str4[3],str4[4]+" "+str4[5])); usersArrayList.get(i).setUserRecords(usersArrayList.get(i).getUserRecords()); } else usersArrayList.get(i).getUserRecords().addgetCallingInLandRecords(new CallRecord(str4[2]+" "+str4[3],str4[4]+" "+str4[5])); usersArrayList.get(i).setUserRecords(usersArrayList.get(i).getUserRecords()); } } } str = input.nextLine(); } Collections.sort(usersArrayList,new Comparator<Users>() { @Override public int compare(Users o1, Users o2) { return o1.getNumber().compareTo(o2.getNumber()); } }); for(int i = 0;i<usersArrayList.size();i++){ System.out.print(usersArrayList.get(i).number+" "); System.out.print(new DecimalFormat("0.0#").format(usersArrayList.get(i).calCost())+" "); System.out.print(new DecimalFormat("0.0#").format(usersArrayList.get(i).getBalance()-usersArrayList.get(i).calCost()-20)); System.out.println(); } } } class Users{ private UserRecords userRecords = new UserRecords(); double balance = 100; String number; ChargeMode chargeMode; public Users(String number) { this.number = number; } public double CalBalance(){ return 0; } public double calCost(){ LandPhoneInCityRule landPhoneInCityRule = new LandPhoneInCityRule(); LandPhoneInLandRule landPhoneInLandRule = new LandPhoneInLandRule(); LandPhoneInProvinceRule landPhoneInProvinceRule = new LandPhoneInProvinceRule(); return landPhoneInLandRule.calCost(userRecords.getCallingInLandRecords())+landPhoneInCityRule.calCost(userRecords.getCallingInCityRecords())+landPhoneInProvinceRule.calCost(userRecords.getCallingInProvinceRecords()); } 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; } public ChargeMode getChargeMode() { return chargeMode; } public void setChargeMode(ChargeMode chargeMode) { this.chargeMode = chargeMode; } } 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> answerInCityRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>(); private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>(); private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<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<MessageRecord> getSendMessageRecords() { return sendMessageRecords; } public ArrayList<MessageRecord> getReceiveMessageRecords() { return receiveMessageRecords; } public void addgetCallingInCityRecords(CallRecord callRecord){ getCallingInCityRecords().add(callRecord); } public void addgetCallingInProvinceRecords(CallRecord callRecord){ getCallingInProvinceRecords().add(callRecord); } public void addgetCallingInLandRecords(CallRecord callRecord){ getCallingInLandRecords().add(callRecord); } public void addAnswerInCityRecords(CallRecord answerRecord){ answerInCityRecords.add(answerRecord); } public void addAnswerInProvinceRecords(CallRecord answerRecord){ answerInProvinceRecords.add(answerRecord); } public void addAnswerInLandRecords(CallRecord answerRecord){ answerInLandRecords.add(answerRecord); } public void addSendMessageRecords(MessageRecord sendMessageRecord){ sendMessageRecords.add(sendMessageRecord); } public void addReceiveMessageRecords(MessageRecord receiveMessageRecord){ receiveMessageRecords.add(receiveMessageRecord); } } 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 20; } } class LandlinePhoneCharging extends ChargeMode{ private double monthlyRent = 20; @Override public double calCost(UserRecords userRecords) { return 0; } @Override public double getMonthlyRent() { return monthlyRent; } } 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 CallRecord extends CommunicationRecord{ private Date startTime; private Date endTime; private String callingAddressAreaCode; private String answerAddressAreaCode; public CallRecord(String startTime, String endTime) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); try { this.startTime = simpleDateFormat.parse(startTime); } catch (ParseException e) { e.printStackTrace(); } try { this.endTime = simpleDateFormat.parse(endTime); } catch (ParseException e) { e.printStackTrace(); } } public Date getStartTime() { return startTime; } public void setStartTime(Date startTime) { this.startTime = startTime; } public String getCallingAddressAreaCode() { return callingAddressAreaCode; } public void setCallingAddressAreaCode(String callingAddressAreaCode) { this.callingAddressAreaCode = callingAddressAreaCode; } public Date getEndTime() { return endTime; } public void setEndTime(Date endTime) { this.endTime = endTime; } public String getAnswerAddressAreaCode() { return answerAddressAreaCode; } public void setAnswerAddressAreaCode(String answerAddressAreaCode) { this.answerAddressAreaCode = answerAddressAreaCode; } } class MessageRecord extends CommunicationRecord{ private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } abstract class ChargeRule{ } abstract class CallChargeRule extends ChargeRule{ public double calCost(ArrayList<CallRecord> callRecords){ return 0; } } class LandPhoneInCityRule extends CallChargeRule{ @Override public double calCost(ArrayList<CallRecord> callRecords) { double cost = 0; double minutes; for(CallRecord e:callRecords){ long diff = e.getEndTime().getTime() - e.getStartTime().getTime(); minutes = (int) Math.ceil((double) diff / (double) 60000); cost += 0.1*minutes; } return cost; } } class LandPhoneInLandRule extends CallChargeRule{ @Override public double calCost(ArrayList<CallRecord> callRecords) { double cost = 0; double minutes; for(CallRecord e:callRecords){ long diff = e.getEndTime().getTime() - e.getStartTime().getTime(); minutes = (int) Math.ceil((double) diff / (double) 60000); cost += 0.6*minutes; } return cost; } } class LandPhoneInProvinceRule extends CallChargeRule{ @Override public double calCost(ArrayList<CallRecord> callRecords) { double cost = 0; double minutes; for(CallRecord e:callRecords){ long diff = e.getEndTime().getTime() - e.getStartTime().getTime(); minutes = (int) Math.ceil((double) diff / (double) 60000); cost += 0.3*minutes; } return cost; } }
题目集9:
7-1:正则表达式有误导致没拿到满分
代码如下:
import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); ArrayList<Users> usersArrayList = new ArrayList<>(); String str = input.nextLine(); boolean repetition = false; while (!str.equals("end")){ if(str.matches("u\\-0791\\d{7,8}\\s0")){//座机开户 String str1[] = str.split("-"); String str2[] = str1[1].split(" "); for(int i = 0;i < usersArrayList.size();i++){ if(usersArrayList.get(i).number.equals(str2[0])) { repetition = true; break; } } if(repetition == false){ usersArrayList.add(new Users(str2[0])); } repetition = false; } //手机开户 else if (str.matches("u-1[0-9]{10}\\s1")){ String str5[] = str.split("-"); String str6[] = str5[1].split(" "); for(int i = 0;i < usersArrayList.size();i++){ if(usersArrayList.get(i).number.equals(str6[0])) { repetition = true; break; } } if(repetition == false){ usersArrayList.add(new Users(str6[0])); } repetition = false; } //座机打座机 if(str.matches("t-0791\\d{7,8}\\s\\d{11,12}\\s\\d{4}\\.\\d{1,2}\\.\\d{1,2}\\s\\d{1,2}\\:\\d{1,2}\\:\\d{1,2}\\s\\d{4}\\.\\d{1,2}\\.\\d{1,2}\\s\\d{1,2}\\:\\d{1,2}\\:\\d{1,2}")){ String str3[] = str.split("-"); String str4[] = str3[1].split(" "); for (int i = 0;i < usersArrayList.size();i++){ if(usersArrayList.get(i).number.equals(str4[0])){ if(str4[1].substring(0,4).matches("0791")){ usersArrayList.get(i).getUserRecords().addgetCallingInCityRecords(new CallRecord(str4[2]+" "+str4[3],str4[4]+" "+str4[5])); // usersArrayList.get(i).setUserRecords(usersArrayList.get(i).getUserRecords()); } else if(str4[1].substring(0,4).matches("079\\d|0701")){ usersArrayList.get(i).getUserRecords().addgetCallingInProvinceRecords(new CallRecord(str4[2]+" "+str4[3],str4[4]+" "+str4[5])); // usersArrayList.get(i).setUserRecords(usersArrayList.get(i).getUserRecords()); } else usersArrayList.get(i).getUserRecords().addgetCallingInLandRecords(new CallRecord(str4[2]+" "+str4[3],str4[4]+" "+str4[5])); //usersArrayList.get(i).setUserRecords(usersArrayList.get(i).getUserRecords()); } } } //座机打手机 else if (str.matches("t-0\\d{9,11} 1\\d{10} 0\\d{2,3}\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d")){ String str7[] = str.substring(2,str.length()).split("\\s"); for (int i=0;i< usersArrayList.size();i++){ if (usersArrayList.get(i).number.equals(str7[0])){ if (str7[2].startsWith("0791")){ usersArrayList.get(i).getUserRecords().addgetCallingInCityRecords(new CallRecord(str7[3] + " " + str7[4], str7[5] + " " + str7[6])); } else if (str7[2].matches("(079\\d|0701)")){ usersArrayList.get(i).getUserRecords().addgetCallingInProvinceRecords(new CallRecord(str7[3] + " " + str7[4], str7[5] + " " + str7[6])); } else { usersArrayList.get(i).getUserRecords().addgetCallingInLandRecords(new CallRecord(str7[3] + " " + str7[4], str7[5] + " " + str7[6])); } } if (usersArrayList.get(i).number.equals(str7[1])){ if (!str7[2].matches("(079\\d|0701)")) usersArrayList.get(i).getUserRecords().addAnswerInLandRecords(new CallRecord(str7[3] + " " + str7[4], str7[5] + " " + str7[6])); } } } //手机打座机 else if (str.matches( "t-1\\d{10} 0\\d{2,3} 0\\d{9,11}\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d" )){ String str9[] = str.substring(2,str.length()).split(" "); for (int i=0;i< usersArrayList.size();i++){ if (usersArrayList.get(i).number.equals(str9[0])){ if (str9[1].equals("0791")){ if (str9[2].matches("0791\\d{6,8}")) usersArrayList.get(i).getUserRecords().addgetCallingInCityRecords(new CallRecord(str9[3] + " " + str9[4], str9[5] + " " + str9[6])); else if (str9[2].matches("(079\\d|0701)\\d{6,8}")) usersArrayList.get(i).getUserRecords().addgetCallingInProvinceRecords(new CallRecord(str9[3] + " " + str9[4], str9[5] + " " + str9[6])); else usersArrayList.get(i).getUserRecords().addgetCallingInLandRecords(new CallRecord(str9[3] + " " + str9[4], str9[5] + " " + str9[6])); } else if (str9[1].matches("(0701|0790|079[2-9])")){ usersArrayList.get(i).getUserRecords().addgetCallingInProvinceRoamRecords(new CallRecord(str9[3] + " " + str9[4], str9[5] + " " + str9[6])); } else usersArrayList.get(i).getUserRecords().addCallingInLandRoamRecords(new CallRecord(str9[3] + " " + str9[4], str9[5] + " " + str9[6])); } } } //手机打手机 else if (str.matches("t-1\\d{10} 0\\d{2,3} 1\\d{10} 0\\d{2,3}\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d")){ String str10[] = str.substring(2,str.length()).split(" "); for (int i=0;i< usersArrayList.size();i++){ if (usersArrayList.get(i).number.equals(str10[0])){ if (str10[1].equals("0791")) { if (str10[3].matches("0791")) usersArrayList.get(i).getUserRecords().addgetCallingInCityRecords(new CallRecord(str10[4] + " " + str10[5], str10[6] + " " + str10[7])); else if (str10[3].matches("(079\\d|0701)")) usersArrayList.get(i).getUserRecords().addgetCallingInProvinceRecords(new CallRecord(str10[4] + " " + str10[5], str10[6] + " " + str10[7])); else usersArrayList.get(i).getUserRecords().addgetCallingInLandRecords(new CallRecord(str10[4] + " " + str10[5], str10[6] + " " + str10[7])); } else if (str10[1].matches("(0701|079\\d)")){ usersArrayList.get(i).getUserRecords().addgetCallingInProvinceRoamRecords(new CallRecord(str10[4] + " " + str10[5], str10[6] + " " + str10[7])); } else usersArrayList.get(i).getUserRecords().addCallingInLandRoamRecords(new CallRecord(str10[4] + " " + str10[5], str10[6] + " " + str10[7])); } if (usersArrayList.get(i).number.equals(str10[2])){ if (!str10[3].matches("(079\\d|0701)")) usersArrayList.get(i).getUserRecords().addAnswerInLandRecords(new CallRecord(str10[4] + " " + str10[5], str10[6] + " " + str10[7])); } } } str = input.nextLine(); } Collections.sort(usersArrayList,new Comparator<Users>() { @Override public int compare(Users o1, Users o2) { return o1.getNumber().compareTo(o2.getNumber()); } }); for(int i = 0;i<usersArrayList.size();i++){ System.out.print(usersArrayList.get(i).number+" "); System.out.print(new DecimalFormat("0.0#").format(usersArrayList.get(i).calCost())+" "); System.out.print(new DecimalFormat("0.0#").format(usersArrayList.get(i).calBalance())); System.out.println(); } } } class Users{ private UserRecords userRecords = new UserRecords(); double balance = 100; String number; ChargeMode chargeMode; public Users(String number) { this.number = number; } public double CalBalance(){ return 0; } public double calCost(){ LandPhoneInCityRule landPhoneInCityRule = new LandPhoneInCityRule(); LandPhoneInLandRule landPhoneInLandRule = new LandPhoneInLandRule(); LandPhoneInProvinceRule landPhoneInProvinceRule = new LandPhoneInProvinceRule(); PhoneInCityRule phoneInCityRule = new PhoneInCityRule(); PhoneInIandRule phoneInIandRule = new PhoneInIandRule(); PhoneInProvinceRule phoneInProvinceRule = new PhoneInProvinceRule(); PhoneInIandRoamRule phoneInIandRoamRule = new PhoneInIandRoamRule(); PhoneInProvinceRoamRule phoneInProvinceRoamRule = new PhoneInProvinceRoamRule(); ReceptionPhoneInLandRoamRule receptionPhoneInLandRoamRule = new ReceptionPhoneInLandRoamRule(); if(number.matches("(0791)\\d{6,8}")) return landPhoneInLandRule.calCost(userRecords.getCallingInLandRecords())+landPhoneInCityRule.calCost(userRecords.callingInCityRecords)+landPhoneInProvinceRule.calCost(userRecords.getCallingInProvinceRecords()); return phoneInIandRule.calCost(userRecords.getCallingInLandRecords())+phoneInCityRule.calCost(userRecords.callingInCityRecords)+phoneInProvinceRule.calCost(userRecords.getCallingInProvinceRecords())+receptionPhoneInLandRoamRule.calCost(userRecords.getAnswerInLandRecords())+phoneInIandRoamRule.calCost(userRecords.getCallingInLandRoamRecords())+phoneInProvinceRoamRule.calCost(userRecords.getCallingInProvinceRoamRecords()); } public double calBalance(){ if(number.matches("(0791)\\d{6,8}")) return balance-calCost()-20; return balance-calCost()-15; } 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; } public ChargeMode getChargeMode() { return chargeMode; } public void setChargeMode(ChargeMode chargeMode) { this.chargeMode = chargeMode; } } class UserRecords{ ArrayList<CallRecord> callingInProvinceRoamRecords = new ArrayList<CallRecord>(); ArrayList<CallRecord> callingInLandRoamRecords = new ArrayList<CallRecord>(); ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>(); ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>(); ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>(); ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>(); ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>(); ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>(); ArrayList<MessageRecord> sendMessageRecord = new ArrayList<MessageRecord>(); 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> getCallingInProvinceRoamRecords() { return callingInProvinceRoamRecords; } public ArrayList<CallRecord> getCallingInLandRoamRecords() { return callingInLandRoamRecords; } public void addgetCallingInCityRecords(CallRecord callRecord){ callingInCityRecords.add(callRecord); } public void addgetCallingInProvinceRecords(CallRecord callRecord){ getCallingInProvinceRecords().add(callRecord); } public void addgetCallingInLandRecords(CallRecord callRecord){ getCallingInLandRecords().add(callRecord); } public void addAnswerInCityRecords(CallRecord answerRecord){ answerInCityRecords.add(answerRecord); } public void addAnswerInProvinceRecords(CallRecord answerRecord){ answerInProvinceRecords.add(answerRecord); } public void addAnswerInLandRecords(CallRecord answerRecord){ answerInLandRecords.add(answerRecord); } public void addgetCallingInProvinceRoamRecords(CallRecord callRecord) { callingInProvinceRoamRecords.add(callRecord); } public void addCallingInLandRoamRecords(CallRecord callRecord) { callingInLandRoamRecords.add(callRecord); } } 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 20; } } class LandlinePhoneCharging extends ChargeMode{ private double monthlyRent = 20; @Override public double calCost(UserRecords userRecords) { return 0; } @Override public double getMonthlyRent() { return monthlyRent; } } 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 CallRecord extends CommunicationRecord{ private Date startTime; private Date endTime; private String callingAddressAreaCode; private String answerAddressAreaCode; public CallRecord(String startTime, String endTime) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); try { this.startTime = simpleDateFormat.parse(startTime); } catch (ParseException e) { e.printStackTrace(); } try { this.endTime = simpleDateFormat.parse(endTime); } catch (ParseException e) { e.printStackTrace(); } } public Date getStartTime() { return startTime; } public void setStartTime(Date startTime) { this.startTime = startTime; } public String getCallingAddressAreaCode() { return callingAddressAreaCode; } public void setCallingAddressAreaCode(String callingAddressAreaCode) { this.callingAddressAreaCode = callingAddressAreaCode; } public Date getEndTime() { return endTime; } public void setEndTime(Date endTime) { this.endTime = endTime; } public String getAnswerAddressAreaCode() { return answerAddressAreaCode; } public void setAnswerAddressAreaCode(String answerAddressAreaCode) { this.answerAddressAreaCode = answerAddressAreaCode; } } class MessageRecord extends CommunicationRecord{ private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } abstract class ChargeRule{ } abstract class CallChargeRule extends ChargeRule{ public double calCost(ArrayList<CallRecord> callRecords){ return 0; } } class LandPhoneInCityRule extends CallChargeRule{ @Override public double calCost(ArrayList<CallRecord> callRecords) { double cost = 0; double minutes; for(CallRecord e:callRecords){ long diff = e.getEndTime().getTime() - e.getStartTime().getTime(); minutes = (int) Math.ceil((double) diff / (double) 60000); cost += 0.1*minutes; } return cost; } } class LandPhoneInLandRule extends CallChargeRule{ @Override public double calCost(ArrayList<CallRecord> callRecords) { double cost = 0; double minutes; for(CallRecord e:callRecords){ long diff = e.getEndTime().getTime() - e.getStartTime().getTime(); minutes = (int) Math.ceil((double) diff / (double) 60000); cost += 0.6*minutes; } return cost; } } class LandPhoneInProvinceRule extends CallChargeRule{ @Override public double calCost(ArrayList<CallRecord> callRecords) { double cost = 0; double minutes; for(CallRecord e:callRecords){ long diff = e.getEndTime().getTime() - e.getStartTime().getTime(); minutes = (int) Math.ceil((double) diff / (double) 60000); cost += 0.3*minutes; } return cost; } } class PhoneInProvinceRule extends CallChargeRule{ public double calCost(ArrayList<CallRecord> callRecords){ double money = 0.2; double cost = 0; double minutes; for (CallRecord e: callRecords){ minutes = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime())/ (double) 60000); cost += money*minutes;} return cost; } } class PhoneInCityRule extends CallChargeRule{ public double calCost(ArrayList<CallRecord> callRecords){ double money = 0.1; double cost = 0; double minutes; for (CallRecord e: callRecords){ minutes = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime())/ (double) 60000); cost += money*minutes;} return cost; } } class PhoneInIandRule extends CallChargeRule{ public double calCost(ArrayList<CallRecord> callRecords){ double money = 0.3; double cost = 0; double minutes; for (CallRecord e: callRecords){ minutes = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime())/ (double) 60000); cost += money*minutes;} return cost; } } class PhoneInIandRoamRule extends CallChargeRule{//国内漫游打电话 public double calCost(ArrayList<CallRecord> callRecords){ double money = 0.6; double cost = 0; double minutes; for (CallRecord e: callRecords){ minutes = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime())/ (double) 60000); cost += money*minutes;} return cost; } } class PhoneInProvinceRoamRule extends CallChargeRule{//省内漫游打电话 public double calCost(ArrayList<CallRecord> callRecords){ double money = 0.3; double cost = 0; double minutes; for (CallRecord e: callRecords){ minutes = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime())/ (double) 60000); cost += money*minutes;} return cost; } } class ReceptionPhoneInLandRoamRule extends CallChargeRule{//接电话 public double calCost(ArrayList<CallRecord> callRecords){ double money = 0.3; double cost = 0; double minutes; for (CallRecord e: callRecords){ minutes = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime())/ (double) 60000); cost += money*minutes;} return cost; } }
题目集10:
7-1:无
代码如下:
import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); ArrayList<Users> usersArrayList = new ArrayList<>(); String str = input.nextLine(); boolean repetition = false; while (!str.equals("end")){ //手机开户 if (str.matches("u-1[0-9]{10} [3]")){ String str1[] = str.split("-"); String str2[] = str1[1].split(" "); for (Users users : usersArrayList) { if (users.number.equals(str2[0])) { repetition = true; } } if(!repetition){ usersArrayList.add(new Users(str2[0])); } repetition = false; } else if (str.matches("m-1\\d{10} 1\\d{10} (\\w|\\,|\\.| )+")||str.matches("m-1\\d{10} 1\\d{10}")){ String[] s = str.substring(2).split(" "); for (Users e:usersArrayList){ if(s[0].equals(e.getNumber())){ e.getUserRecords().addSendMessageRecord(new MessageRecord(str.substring(26))); } } } str = input.nextLine(); } usersArrayList.sort(Comparator.comparing(Users::getNumber)); for (Users users : usersArrayList) { System.out.print(users.number + " "); System.out.print(new DecimalFormat("0.0#").format(users.calCost()) + " "); System.out.print(new DecimalFormat("0.0#").format(users.calBalance())); System.out.println(); } } } class Users{ private UserRecords userRecords = new UserRecords(); double balance = 100; String number; public Users(String number) { this.number = number; } public double calCost(){ MessageChargeRule messageChargeRule = new MessageChargeRule(); return messageChargeRule.calCost(userRecords.sendMessageRecord); } public double calBalance(){ return balance-calCost(); } public UserRecords getUserRecords() { return userRecords; } public String getNumber() { return number; } } class UserRecords{ ArrayList<MessageRecord> sendMessageRecord = new ArrayList<MessageRecord>(); public void addSendMessageRecord(MessageRecord sendMessageRecord){ this.sendMessageRecord.add(sendMessageRecord); } } abstract class ChargeMode{ private ArrayList<ChargeRule> chargeRules = new ArrayList<>(); public double calCost(UserRecords userRecords){ return 0; } public double getMonthlyRent(){ return 20; } } 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 CallRecord extends CommunicationRecord{ private Date startTime; private Date endTime; private String callingAddressAreaCode; private String answerAddressAreaCode; public CallRecord(String startTime, String endTime) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); try { this.startTime = simpleDateFormat.parse(startTime); } catch (ParseException e) { e.printStackTrace(); } try { this.endTime = simpleDateFormat.parse(endTime); } catch (ParseException e) { e.printStackTrace(); } } } class MessageRecord extends CommunicationRecord{ private String message; public MessageRecord(String message) { this.message = message; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } abstract class ChargeRule{ } abstract class ChargeMessage{ ArrayList<ChargeRule> list = new ArrayList<ChargeRule>(); public ArrayList<ChargeRule> getChargeRule(){ return list; } public void setChargeRule(ArrayList<ChargeRule> chargeRule){ this.list = list; } public double calCost(UserRecords userRecords){ return 0; } public double getMonthlyRent(){ return 0; } } abstract class ChargePhone{ ArrayList<ChargeRule> list = new ArrayList<ChargeRule>(); public ArrayList<ChargeRule> getChargeRule(){ return list; } public void setChargeRule(ArrayList<ChargeRule> chargeRule){ this.list = list; } public double calCost(UserRecords userRecords){ return 0; } public double getMonthlyRent(){ return 15; } } abstract class CallChargeRule extends ChargeRule{ public double calCost(ArrayList<CallRecord> callRecords){ return 0; } } class MessageChargeRule extends ChargeRule{ public double calCost(ArrayList<MessageRecord> messageRecords) { double cost = 0; double f = 0; for (MessageRecord messageRecord : messageRecords) { double a = messageRecord.getMessage().length(); while (a > 0) { f++; a = a - 10; } } if(f<=3){ cost = 0.1*f; }else if(f>3&&f<=5){ cost = 0.3+0.2*(f-3); }else if(f>5){ cost = 0.3*(f-5)+0.7; } return cost; } } class MessageCharging extends ChargeMessage{ double monthlyRent = 0; public double calCost(UserRecords userRecords){ return 0; } public double getMonthlyRent(){ return monthlyRent; } } class MobilePhoneCharging extends ChargePhone{ double monthlyRent = 15; public double calCost(UserRecords userRecords){ return 0; } public double getMonthlyRent(){ return monthlyRent; } } class SendMessageRule extends MessageChargeRule{ @Override public double calCost(ArrayList<MessageRecord> messageRecords) { return super.calCost(messageRecords); } }
(4)改进建议
三次题目集类设计合理。无改进建议
(5)总结
经过一学期pta*题的洗礼,我的编程能力有了明显的提高,对类的设计有了更深的理解,以前在我看来没必要写的类如今也明白了它们存在的意义,尤其是这最后三次的迭代作业充分展示合理的类设计,受益匪浅。最后三次题目集的难度在类图的引导上难度比起先前图形类的迭代作业有了明显下降,更多的是对所学知识的总结。
这学期的java学*,过程较为坎坷,课程本身的难度与其他课程的压力常常让我感到分身乏术,不只一次想过放弃,好在最后还是坚持了下来。Java学*不仅提升了我的编程能力,更重要的提升了我面对困难时的毅力与对新知识的自学能力。在自学过程中,无论是在官方文档的阅读还是网上慕课的学*,都让我在投入了时间精力后得到不少的收获,比起从前纯粹依靠老师讲解而言,学*得更*深刻了。
存在的不足:由于本学期大多数课均为网课的原因,受限于此和自身自觉性不足,java学*还存在很大的进步空间,也没能体验到老师口中一节课讲解,一节课写代码的模式,有些遗憾,在今后的学*中我会增强自身自觉性,自主学*!

浙公网安备 33010602011771号