第三次博客作业

1|0前言:

题目集六:

  电信计费系列一,难度较前几次的数学计算有所下降,但是更为繁琐。

题目集七:

  电信计费系列二在系列一上面添加了手机计费规则,规则更加灵活,但是整体难度仍然不大,由于系列一搭好了框架,繁琐程度大大的降低了。

题目集八:

  电信计费系列三添加了短信的计费规则,更加灵活了,尤其是对手机号和短信内容的处理。

2|0设计与分析:

2|17-1 电信计费系列1-座机计费:

实现一个简单的电信计费程序:
假设南昌市电信分公司针对市内座机用户采用的计费方式:
月租20元,接电话免费,市内拨打电话0.1元/分钟,省内长途0.3元/分钟,国内长途拨打0.6元/分钟。不足一分钟按一分钟计。
南昌市的区号:0791,江西省内各地市区号包括:0790~0799以及0701。

1|0输入格式:

输入信息包括两种类型
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、用户区号不为南昌市的区号也作为正常用户处理。

1|0输出格式:

根据输入的详细通讯信息,计算所有已开户的用户的当月费用(精确到小数点后2位,
单位元)。假设每个用户初始余额是100元。
每条通讯信息单独计费后累加,不是将所有时间累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。

错误处理:
输入数据中出现的不符合格式要求的行一律忽略。

建议类图:
参见图1、2、3,可根据理解自行调整:

image.png

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

image.png

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

image.png

图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

1|0输入样例:

在这里给出一组输入。例如:

u-079186300001 0 t-079186300001 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:25 end
 

1|0输出样例:

在这里给出相应的输出。例如:

079186300001 3.0 77.0
 

其余参考样例详见附件,未尽事宜以附件样例为准:

电信计费简化版-座机计费说明.pdf

1|0代码

import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; class work { ArrayList<String> strr = new ArrayList<>(); ArrayList<User> uesrs = new ArrayList<>(); ArrayList<CallRecord> callRecords = new ArrayList<>(); int usernum = 0; public work() { Scanner sc = new Scanner(System.in); while (true) { String str = sc.nextLine(); if (str.equals("end")) return; strr.add(str); } } void tran() { String uPattern = "^u-[0-9]{11,12} 0$"; String tPattern = "^t-\\d{10,12}\\s\\d{10,12}\\s([1-9][0-9]*\\.[1-9][0-9]?\\.[1-9][0-9]? ([0|1]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] ?){2}$"; boolean lock = false; // 11,12位只包含座机 for (String s : strr) { if (Pattern.compile("[^-ut\\d: \\.]").matcher(s).find()) //出现的字符集合 continue; if (s.matches(uPattern) && !lock) { Pattern r = Pattern.compile("^u-([0-9]{11,12}) (\\d)$"); User user = new User(); Matcher m = r.matcher(s); if (m.find()) { user.model = Integer.parseInt(m.group(2)); user.number = m.group(1); if (!user.number.startsWith("0791")) continue; user.chargeMode = new LandlinePhoneCharging(); if (!is_in_users(user.number)) { usernum++; uesrs.add(user); } } } else if (s.matches( tPattern // "^t-([0-9]{10,12}) ([0-9]{10,12}) \\d{4}+\\.\\d{1,2}+\\.\\d{1,2}+ ([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]) \\d{4}+\\.\\d{1,2}+\\.\\d{1,2}+ ([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$" )) { // ^t-[0-9]{11,13} [0-9]{11,13} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}$ CallRecord callRecord = new CallRecord(); Pattern r = Pattern.compile("t-([0-9]{10,12}) ([0-9]{10,12}) ([0-9]{4}\\.[0-9]{1,2}\\.[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}) ([0-9]{4}\\.[0-9]{1,2}\\.[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2})"); //^t-([0-9]{11,13}) ([0-9]{11,13}) \\d{4}+[\\.]\\d{1,2}+[\\.]\\d{1,2}+ ([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]) \\d{4}+[\\.]\\d{1,2}+[\\.]\\d{1,2}+ ([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$ Matcher m = r.matcher(s); if (m.find()) { callRecord.callingNumber = m.group(1); if (is_three_area_code(m.group(1))) { if (m.group(1).length() == 10 || m.group(1).length() == 11) callRecord.callingAddressAreaCode = m.group(1).substring(0, 3); else continue; } else { if (m.group(1).length() == 11 || m.group(1).length() == 12) callRecord.callingAddressAreaCode = m.group(1).substring(0, 4); else continue; } callRecord.callingAddressAreaCode = m.group(1).substring(0, 4); callRecord.answerNumber = m.group(2); if (is_three_area_code(m.group(2))) { if (m.group(2).length() == 10 || m.group(2).length() == 11) callRecord.answerAddressAreaCode = m.group(2).substring(0, 3); else continue; } else { if (m.group(2).length() == 11 || m.group(2).length() == 12) callRecord.answerAddressAreaCode = m.group(2).substring(0, 4); else continue; } Date sd; Date fd; SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); try { sd = format.parse(m.group(3)); } catch (ParseException e) { continue; } try { fd = format.parse(m.group(4)); } catch (ParseException e) { continue; } callRecord.startTime = sd; callRecord.endTime = fd; if (sd.getTime() < fd.getTime() && dateStrIsValid(m.group(3)) && dateStrIsValid(m.group(4))) { callRecords.add(callRecord); lock = true; } } } } } void start_work() { for (CallRecord callRecord : callRecords) { int num = user_num(callRecord.callingNumber); if (num != -1) { switch (code_num_where(callRecord.callingAddressAreaCode , callRecord.answerAddressAreaCode)) { case 0: this.uesrs.get(num).userRecords.callingInCityRecords.add(callRecord); break; case 1: this.uesrs.get(num).userRecords.callingInProvinceRecords.add(callRecord); break; case 2: this.uesrs.get(num).userRecords.callingInLandRecords.add(callRecord); break; default: break; } } } this.sort_users(); for (int i = 0; i < usernum; i++) if (i != usernum - 1) System.out.println(uesrs.get(i).number + " " + Print(uesrs.get(i).calCost()) + " " + Print(uesrs.get(i).calBalance())); else System.out.print(uesrs.get(i).number + " " + Print(uesrs.get(i).calCost()) + " " + Print(uesrs.get(i).calBalance())); } //判断时间是否合法 public boolean dateStrIsValid(String str) { SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");//括号内为日期格式,y代表年份,M代表年份中的月份(为避免与小时中的分钟数m冲突,此处用M),d代表月份中的天数 try { sd.setLenient(false);//此处指定日期/时间解析是否不严格,在true是不严格,false时为严格 sd.parse(str);//从给定字符串的开始解析文本,以生成一个日期 } catch (Exception e) { return false; } return true; } // 不足一分钟按一分钟计算时间 static long howlong(Date start, Date end) { long sec = (end.getTime() - start.getTime()) / 1000; if (sec % 60 == 0) return sec / 60; else return sec / 60 + 1; } // 返回用户组下标 int user_num(String num) { if (this.uesrs.isEmpty()) return -1; for (int i = 0; i < this.usernum; i++) { if (uesrs.get(i).number.equals(num)) return i; } return -1; } //是否重复用户判断 boolean is_in_users(String num) { if (this.uesrs.isEmpty()) return false; for (int i = 0; i < this.usernum; i++) { if (uesrs.get(i).number.equals(num)) return true; } return false; } // 两区号比较 市内0 省内长途1 国内长途2 int code_num_where(String code1, String code2) { int[] code1_num = area(code1); int[] code2_num = area(code2); if (code1.equals(code2)) return 0; else if ((code1_num[0] == code2_num[0] && code1_num[1] != code2_num[1])) return 1; else return 2; } // 返回是否为3位区号 boolean is_three_area_code(String num) { String[] arecode = new String[]{"010", "021", "022", "023", "852", "853", "020", "028", "029"}; for (String code : arecode) if (num.startsWith(code)) return true; return false; } //返回地区号 地区号两位组成 第一位为省份号 第二为为地区号 int[] area(String code) { int[] area_num = new int[2]; area_num[0] = 9; area_num[1] = 9; String[] area1 = {"0790", "0791", "0792", "0793", "0794", "0795", "0796", "0797", "0798", "0799", "0701"}; for (int i = 0; i < area1.length; i++) { if (code.equals(area1[i])) { area_num[0] = 1; area_num[1] = i; } } return area_num; } // 打印标准格式 static String Print(double a) { if (Math.abs((int) a - a) < 1e-6) return (int) a + ".0"; else if ((Math.abs((int) a - a + 1) < 1e-6)) return (int) a + 1 + ".0"; else return new DecimalFormat("#.#").format(a); } // 用户按号码排序 void sort_users() { long[] user_num = new long[usernum]; User[] Users = new User[usernum]; for (int i = 0; i < usernum; i++) { user_num[i] = Long.parseLong(uesrs.get(i).number); Users[i] = this.uesrs.get(i); } for (int i = 0; i < usernum - 1; i++) { int min = i; // 每轮需要比较的次数 N-i for (int j = i + 1; j < usernum; j++) { if (user_num[j] < user_num[min]) { // 记录目前能找到的最小值元素的下标 min = j; } // 将找到的最小值和i位置所在的值进行交换 if (i != min) { long tmp = user_num[min]; user_num[min] = user_num[i]; user_num[i] = tmp; User temp = Users[min]; Users[min] = Users[i]; Users[i] = temp; } } } this.uesrs = new ArrayList<>(Arrays.asList(Users).subList(0, usernum)); } } //UserRecords是用户记录类,保存用户各种通话、短信的记录, //各种计费规则将使用其中的部分或者全部记录。 //其属性从上到下依次是: //以及发送短信、接收短信的记录。 class UserRecords { //市内拨打电话 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> sendMessageRecords = new ArrayList<MessageRecord>(); //接收短信的记录 ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>(); void addCallingInCityRecords(CallRecord callRecord) { callingInCityRecords.add(callRecord); } void addCallingInProvinceRecords(CallRecord callRecord) { callingInProvinceRecords.add(callRecord); } void CallingInLandRecords(CallRecord callRecord) { callingInLandRecords.add(callRecord); } void adddAnswerInCityRecords(CallRecord answerRecord) { answerInCityRecords.add(answerRecord); } void addSendMessageRecords(MessageRecord sendMessageRecord) { sendMessageRecords.add(sendMessageRecord); } void addReceiveMessageRecords(MessageRecord receiveMessageRecord) { receiveMessageRecords.add(receiveMessageRecord); } ArrayList<MessageRecord> getSendMessageRecords() { return sendMessageRecords; } ArrayList<MessageRecord> getReceiveMessageRecords() { return receiveMessageRecords; } ArrayList<CallRecord> getCallingInCityRecords() { return callingInCityRecords; } ArrayList<CallRecord> getCallingInProvinceRecords() { return callingInProvinceRecords; } ArrayList<CallRecord> getCallingInLandRecords() { return callingInLandRecords; } ArrayList<CallRecord> getAnswerInCityRecords() { return answerInCityRecords; } ArrayList<CallRecord> getAnswerInProvinceRecords() { return answerInProvinceRecords; } ArrayList<CallRecord> getAnswerInLandRecords() { return answerInLandRecords; } } //User是用户类,包括属性: //userRecords (用户记录)、balance(余额)、chargeMode(计费方式)、number(号码) class User { String number; int model;//0为座机用户 double balance = 100; ChargeMode chargeMode;//ChargeMode是计费方式的抽象类: UserRecords userRecords = new UserRecords();// //计算余额 public double calBalance() { balance = balance - calCost() - chargeMode.getMonthlyRent(); return balance; } double calCost() { LandPhoneIInlandRule lpi = new LandPhoneIInlandRule(); LandPhoneInCityRule lpc = new LandPhoneInCityRule(); LandPhoneInProvinceRule lpp = new LandPhoneInProvinceRule(); return lpi.callCost(userRecords.callingInLandRecords) + lpc.callCost(userRecords.callingInCityRecords) + lpp.callCost(userRecords.callingInProvinceRecords); } UserRecords getUserRecords() { return userRecords; } void setUserRecords(UserRecords userRecords) { this.userRecords = userRecords; } double getBalance() { return balance; } ChargeMode getChargeMode() { return chargeMode; } void setChargeMode(ChargeMode chargeMode) { this.chargeMode = chargeMode; } String getNumber() { return number; } void setNumber(String number) { this.number = number; } } // MessageRecord(短信记录) class MessageRecord extends CommunicationRecord { String message; String getMessage() { return message; } void setMessage(String message) { this.message = message; } @Override String getCallingNumber() { return callingNumber; } @Override void setCallingNumber(String callingNumber) { this.callingNumber = callingNumber; } @Override String getAnswerNumber() { return answerNumber; } @Override void setAnswerNumber(String answerNumber) { this.answerNumber = answerNumber; } } public class Main { public static void main(String[] args) { work work = new work(); work.tran(); work.start_work(); } } class LandPhoneInProvinceRule extends CallChargeRule { @Override double callCost(ArrayList<CallRecord> callRecords) { double cost = 0.0; for (CallRecord callRecord : callRecords) cost += 0.3 * work.howlong(callRecord.startTime, callRecord.endTime); return cost; } } class LandPhoneInCityRule extends CallChargeRule { @Override double callCost(ArrayList<CallRecord> callRecords) { double cost = 0.0; for (CallRecord callRecord : callRecords) cost += 0.1 * work.howlong(callRecord.startTime, callRecord.endTime); return cost; } } class LandPhoneIInlandRule extends CallChargeRule { @Override double callCost(ArrayList<CallRecord> callRecords) { double cost = 0.0; for (CallRecord callRecord : callRecords) cost += 0.6 * work.howlong(callRecord.startTime, callRecord.endTime); return cost; } } class LandlinePhoneCharging extends ChargeMode { double monthlyRent = 20; @Override ArrayList<ChargeRule> getChargeRules() { return null; } @Override void setChargeRules(ArrayList<ChargeRule> chargeRules) { } @Override double calCost(UserRecords userRecords) { return 0.0; } @Override double getMonthlyRent() { return monthlyRent; } } abstract class CommunicationRecord { String callingNumber;//拨打号码 String answerNumber;//接听号码 abstract String getCallingNumber(); abstract void setCallingNumber(String callingNumber); abstract String getAnswerNumber(); abstract void setAnswerNumber(String answerNumber); } abstract class ChargeRule { } //ChargeMode是计费方式的抽象类: abstract class ChargeMode { ArrayList<ChargeRule> chargeRules = new ArrayList<>(); abstract ArrayList<ChargeRule> getChargeRules(); abstract void setChargeRules(ArrayList<ChargeRule> chargeRules); abstract double calCost(UserRecords userRecords); abstract double getMonthlyRent(); } abstract class CallChargeRule extends ChargeRule { abstract double callCost(ArrayList<CallRecord> callRecords); } //CallRecord(通话记录类)包含属性: //通话的起始、结束时间以及 拨号地点的区号(callingAddressAreaCode)、接听地点的区号(answerAddressAreaCode)。 //区号用于记录在哪个地点拨打和接听的电话。对于座机,就是本机区号。 // 如果是手机号,则接打地点的区号和本机的开户地区会有差异。 class CallRecord extends CommunicationRecord { Date startTime; Date endTime; String callingAddressAreaCode; String answerAddressAreaCode; @Override String getCallingNumber() { return callingNumber; } @Override void setCallingNumber(String callingNumber) { this.callingNumber = callingNumber; } @Override String getAnswerNumber() { return answerNumber; } @Override void setAnswerNumber(String answerNumber) { this.answerNumber = answerNumber; } Date getStartTime() { return startTime; } void setStartTime(Date startTime) { this.startTime = startTime; } Date getEndTime() { return endTime; } void setEndTime(Date endTime) { this.endTime = endTime; } String getCallingAddressAreaCode() { return callingAddressAreaCode; } void setCallingAddressAreaCode(String callingAddressAreaCode) { this.callingAddressAreaCode = callingAddressAreaCode; } String getAnswerAddressAreaCode() { return answerAddressAreaCode; } void setAnswerAddressAreaCode(String answerAddressAreaCode) { this.answerAddressAreaCode = answerAddressAreaCode; } }

1|0类图与分析

  本次作业主要是针对类图结构写代码,十分繁琐。,看懂类图并用提供的类图写代码便成为这道题难点。题目算法不难,各项指标都处于一个较为合理的范围。

2|27-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:

image.png
图1

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

image.png

图2

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

image.png
图3

图3是计费规则的相关类,这些类的核心方法是: calCost(ArrayList<CallRecord> callRecords)。 该方法针根据输入参数callRecords中的所有记录计算某用户的某一项费用;如市话费。 输入参数callRecords的约束条件:必须是某一个用户的符合计费规则要求的所有记录。 SendMessageRule是发送短信的计费规则类,用于计算发送短信的费用。 LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三个类分别是座机拨打市内、省内、省外电话的计费规则类,用于实现这三种情况的费用计算。
 
 

(提示:可以从UserRecords类中获取各种类型的callRecords)。
注意:以上图中所定义的类不是限定要求,根据实际需要自行补充或修改。

1|0输入样例:

在这里给出一组输入。例如:

u-13811111111 1 t-13811111111 0791 13811111110 020 2022.1.3 08:00:00 2022.1.3 08:09:20 end
 

1|0输出样例:

在这里给出相应的输出。例如:

13811111111 3.0 82.0
 

更多内容详见附件:

电信计费简化版-座机计费说明.pdf

1|0代码

import javax.print.DocFlavor; import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; class work { ArrayList<String> strr = new ArrayList<>(); ArrayList<User> uesrs = new ArrayList<>(); ArrayList<CallRecord> callRecords = new ArrayList<>(); int usernum = 0; public work() { Scanner sc = new Scanner(System.in); while (true) { String str = sc.nextLine(); if (str.equals("end")) return; strr.add(str); } } void tran() { String tPattern = "t-0\\d{9,11}\\s" + "0\\d{9,11}\\s" + "([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3}\\.(1[0-2]|[1-9])\\.([1-9]|(1|2)[0-9]|(30|31)) ([0|1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] ?){2}"; String tPattern2 = "t-\\d{11}\\s\\d{3,4}\\s" + "\\d{11}\\s\\d{3,4}\\s" + "([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3}\\.(1[0-2]|[1-9])\\.([1-9]|(1|2)[0-9]|(30|31)) ([0|1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] ?){2}"; String tPattern3 = "t-\\d{11}\\s\\d{3,4}\\s" + "\\d{10,12}\\s" + "([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3}\\.(1[0-2]|[1-9])\\.([1-9]|(1|2)[0-9]|(30|31)) ([0|1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] ?){2}"; String tPattern1 = "t-\\d{10,12}\\s" + "1\\d{10}\\s\\d{3,4}\\s" + "([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3}\\.(1[0-2]|[1-9])\\.([1-9]|(1|2)[0-9]|(30|31)) ([0|1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] ?){2}"; String uPattern = "^u-[0-9]{11,12} 0$"; String uPattern1 = "^u-[0-9]{11} 1$"; //座机呼叫座机 //String tPattern = "^t-\\d{10,12}\\s\\d{10,12}\\s([1-9][0-9]*\\.[1-9][0-9]?\\.[1-9][0-9]? ([0|1]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] ?){2}$"; //座机打手机 // String tPattern1 = "^t-\\d{10,12}\\s\\d{11}\\s\\d{3,4}\\s([1-9][0-9]*\\.[1-9][0-9]?\\.[1-9][0-9]? ([0|1]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] ?){2}$"; // 手机互打 // String tPattern2 = "^t-\\d{11}\\s\\d{3,4}\\s\\d{11}\\s\\d{3,4}\\s([1-9][0-9]*\\.[1-9][0-9]?\\.[1-9][0-9]? ([0|1]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] ?){2}$"; //手机打座机 // String tPattern3 = "^t-\\d{11}\\s\\d{3,4}\\s\\d{10,12}\\s([1-9][0-9]*\\.[1-9][0-9]?\\.[1-9][0-9]? ([0|1]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] ?){2}$"; boolean lock = false; // 11,12位只包含座机 for (String s : strr) { if (Pattern.compile("[^-ut\\d: \\.]").matcher(s).find()) //出现的字符集合 continue; if (s.matches(uPattern) && !lock) { Pattern r = Pattern.compile("^u-([0-9]{11,12}) (\\d)$"); User user = new User(0); Matcher m = r.matcher(s); if (m.find()) { user.model = Integer.parseInt(m.group(2)); user.number = m.group(1); where_is_me(user); if (!user.number.startsWith("0791")) continue; if (!is_in_users(user.number)) { usernum++; uesrs.add(user); } } } else if (s.matches(uPattern1) && !lock) { Pattern r = Pattern.compile("^u-([0-9]{11}) (1)$"); User user = new User(1); Matcher m = r.matcher(s); if (m.find()) { user.model = Integer.parseInt(m.group(2)); user.number = m.group(1); where_is_me(user); if (!is_in_users(user.number)) { usernum++; uesrs.add(user); } } } else if (s.matches(tPattern)) { CallRecord callRecord = new CallRecord(); Pattern r = Pattern.compile("t-([0-9]{10,12}) ([0-9]{10,12}) ([0-9]{4}\\.[0-9]{1,2}\\.[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}) ([0-9]{4}\\.[0-9]{1,2}\\.[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2})"); Matcher m = r.matcher(s); if (m.find()) { callRecord.callingNumber = m.group(1); callRecord.callingAddressAreaCode = myCode(m.group(1)); callRecord.answerNumber = m.group(2); callRecord.answerAddressAreaCode = myCode(m.group(2)); Date sd; Date fd; SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); try { sd = format.parse(m.group(3)); } catch (ParseException e) { continue; } try { fd = format.parse(m.group(4)); } catch (ParseException e) { continue; } callRecord.startTime = sd; callRecord.endTime = fd; if (sd.getTime() < fd.getTime() && dateStrIsValid(m.group(3)) && dateStrIsValid(m.group(4))) { callRecords.add(callRecord); lock = true; } } } else if (s.matches(tPattern1)) { CallRecord callRecord = new CallRecord(); Pattern r = Pattern.compile("^t-(\\d{10,12})\\s(\\d{11})\\s(\\d{3,4})\\s([0-9]{4}\\.[0-9]{1,2}\\.[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}) ([0-9]{4}\\.[0-9]{1,2}\\.[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2})"); Matcher m = r.matcher(s); if (m.find()) { callRecord.callingNumber = m.group(1); if (is_three_area_code(m.group(1))) { if (m.group(1).length() == 10 || m.group(1).length() == 11) callRecord.callingAddressAreaCode = m.group(1).substring(0, 3); else continue; } else { if (m.group(1).length() == 11 || m.group(1).length() == 12) callRecord.callingAddressAreaCode = m.group(1).substring(0, 4); else continue; } callRecord.callingAddressAreaCode = m.group(1).substring(0, 4); callRecord.answerNumber = m.group(2); callRecord.answerAddressAreaCode = m.group(3); Date sd; Date fd; SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); try { sd = format.parse(m.group(4)); } catch (ParseException e) { continue; } try { fd = format.parse(m.group(5)); } catch (ParseException e) { continue; } callRecord.startTime = sd; callRecord.endTime = fd; if (sd.getTime() < fd.getTime() && dateStrIsValid(m.group(4)) && dateStrIsValid(m.group(5))) { callRecords.add(callRecord); lock = true; } } } else if (s.matches(tPattern2)) { CallRecord callRecord = new CallRecord(); Pattern r = Pattern.compile("^t-(\\d{11})\\s(\\d{3,4})\\s(\\d{11})\\s(\\d{3,4}) ([0-9]{4}\\.[0-9]{1,2}\\.[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}) ([0-9]{4}\\.[0-9]{1,2}\\.[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2})"); Matcher m = r.matcher(s); if (m.find()) { callRecord.callingNumber = m.group(1); callRecord.callingAddressAreaCode = m.group(2); callRecord.answerNumber = m.group(3); callRecord.answerAddressAreaCode = m.group(4); Date sd; Date fd; SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); try { sd = format.parse(m.group(5)); } catch (ParseException e) { continue; } try { fd = format.parse(m.group(6)); } catch (ParseException e) { continue; } callRecord.startTime = sd; callRecord.endTime = fd; if (sd.getTime() < fd.getTime() && dateStrIsValid(m.group(5)) && dateStrIsValid(m.group(6))) { callRecords.add(callRecord); lock = true; } } } else if (s.matches(tPattern3)) { CallRecord callRecord = new CallRecord(); Pattern r = Pattern.compile("^t-(\\d{11})\\s(\\d{3,4})\\s(\\d{10,12}) ([0-9]{4}\\.[0-9]{1,2}\\.[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}) ([0-9]{4}\\.[0-9]{1,2}\\.[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2})"); Matcher m = r.matcher(s); if (m.find()) { callRecord.callingNumber = m.group(1); callRecord.callingAddressAreaCode = m.group(2); callRecord.answerNumber = m.group(3); callRecord.answerAddressAreaCode = myCode(callRecord.answerNumber); Date sd; Date fd; SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); try { sd = format.parse(m.group(4)); } catch (ParseException e) { continue; } try { fd = format.parse(m.group(5)); } catch (ParseException e) { continue; } callRecord.startTime = sd; callRecord.endTime = fd; if (sd.getTime() < fd.getTime() && dateStrIsValid(m.group(4)) && dateStrIsValid(m.group(5))) { callRecords.add(callRecord); lock = true; } } } } } void start_work() { for (CallRecord callRecord : callRecords) { // 拨打电话记录 int num = user_num(callRecord.callingNumber); if (num != -1) { if (this.uesrs.get(num).model == 0) switch (code_num_where(callRecord.callingAddressAreaCode , callRecord.answerAddressAreaCode)) { case 0: this.uesrs.get(num).userRecords.callingInCityRecords.add(callRecord); break; case 1: this.uesrs.get(num).userRecords.callingInProvinceRecords.add(callRecord); break; case 2: this.uesrs.get(num).userRecords.callingInLandRecords.add(callRecord); break; default: break; } else { switch (work.is_(callRecord.callingAddressAreaCode)) { case 0: this.uesrs.get(num).userRecords.callingInCityRecords.add(callRecord); break; case 1: this.uesrs.get(num).userRecords.callingInProvinceRecords.add(callRecord); break; case 2: this.uesrs.get(num).userRecords.callingInLandRecords.add(callRecord); break; default: break; } } } num = user_num(callRecord.answerNumber); if (num != -1) { switch (work.is_(callRecord.answerAddressAreaCode)) { case 0: this.uesrs.get(num).userRecords.answerInCityRecords.add(callRecord); break; case 1: this.uesrs.get(num).userRecords.answerInProvinceRecords.add(callRecord); break; case 2: this.uesrs.get(num).userRecords.answerInLandRecords.add(callRecord); break; default: break; } } } this.sort_users(); for (int i = 0; i < usernum; i++) if (i != usernum - 1) System.out.println(uesrs.get(i).number + " " + Print(uesrs.get(i).calCost()) + " " + Print(uesrs.get(i).calBalance())); else System.out.print(uesrs.get(i).number + " " + Print(uesrs.get(i).calCost()) + " " + Print(uesrs.get(i).calBalance())); } //判断时间是否合法 public boolean dateStrIsValid(String str) { SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");//括号内为日期格式,y代表年份,M代表年份中的月份(为避免与小时中的分钟数m冲突,此处用M),d代表月份中的天数 try { sd.setLenient(false);//此处指定日期/时间解析是否不严格,在true是不严格,false时为严格 sd.parse(str);//从给定字符串的开始解析文本,以生成一个日期 } catch (Exception e) { return false; } return true; } // 不足一分钟按一分钟计算时间 static long howlong(Date start, Date end) { long sec = (end.getTime() - start.getTime()) / 1000; if (sec % 60 == 0) return sec / 60; else return sec / 60 + 1; } // 返回用户组下标 int user_num(String num) { if (this.uesrs.isEmpty()) return -1; for (int i = 0; i < this.usernum; i++) { if (uesrs.get(i).number.equals(num)) return i; } return -1; } //是否重复用户判断 boolean is_in_users(String num) { if (this.uesrs.isEmpty()) return false; for (int i = 0; i < this.usernum; i++) { if (uesrs.get(i).number.equals(num)) return true; } return false; } // 输入号码,返回用户区号和号码数组 void where_is_me(User user) { if (user.model == 0) { String number = user.number; String[] cods = new String[2]; if (is_three_area_code(number)) { user.code = number.substring(0, 3); user.no = number.substring(3); } else { user.code = number.substring(0, 4); user.no = number.substring(4); } } else user.no = user.number; } // 返回座机号码区号 String myCode(String number) { if (is_three_area_code(number)) return number.substring(0, 3); else { return number.substring(0, 4); } } // 两区号比较 市内0 省内长途1 国内长途2 int code_num_where(String code1, String code2) { int[] code1_num = area(code1); int[] code2_num = area(code2); if (code1.equals(code2)) return 0; else if ((code1_num[0] == code2_num[0] && code1_num[1] != code2_num[1])) return 1; else return 2; } // 市内省内接电话均免费,市内拨打市内电话0.1元/分钟,市内拨打省内电话0.2元/分钟,市内拨打省外电话0.3元/分钟, // 省内漫游打电话0.3元/分钟,省外漫游接听0.3元/分钟,省外漫游拨打0.6元/分钟; // int code_where(String code1, String code2) { // if(work.is_(code1)==0&&work.is_(code2)==0) // } // 返回是否为3位区号 boolean is_three_area_code(String num) { String[] arecode = new String[]{"010", "021", "022", "023", "852", "853", "020", "028", "029"}; for (String code : arecode) if (num.startsWith(code)) return true; return false; } //返回地区号 地区号两位组成 第一位为省份号 第二为为地区号 //00 为南昌 其他0开头的为江西省内其他 int[] area(String code) { int[] area_num = new int[2]; area_num[0] = 9; area_num[1] = 9; String[] area1 = {"0790", "0791", "0792", "0793", "0794", "0795", "0796", "0797", "0798", "0799", "0701"}; for (int i = 0; i < area1.length; i++) { if (code.equals(area1[i])) { area_num[0] = 1; area_num[1] = i; } } return area_num; } //南昌 0 江西省内 1 外2 static int is_(String code) { String[] codes = {"0790", "0791", "0792", "0793", "0794", "0795", "0796", "0797", "0798", "0799", "0701"}; if (code.equals("0791")) return 0; for (String codee : codes) if (code.equals(codee)) return 1; return 2; } // 打印标准格式 static String Print(double a) { if (Math.abs((int) a - a) < 1e-6) return (int) a + ".0"; else if ((Math.abs((int) a - a + 1) < 1e-6)) return (int) a + 1 + ".0"; else return new DecimalFormat("#.#").format(a); } // 用户按号码排序 void sort_users() { long[] user_num = new long[usernum]; User[] Users = new User[usernum]; for (int i = 0; i < usernum; i++) { user_num[i] = Long.parseLong(uesrs.get(i).no); Users[i] = this.uesrs.get(i); } for (int i = 0; i < usernum - 1; i++) { int min = i; // 每轮需要比较的次数 N-i for (int j = i + 1; j < usernum; j++) { if (user_num[j] < user_num[min]) { // 记录目前能找到的最小值元素的下标 min = j; } } // 将找到的最小值和i位置所在的值进行交换 if (i != min) { long tmp = user_num[min]; user_num[min] = user_num[i]; user_num[i] = tmp; User temp = Users[min]; Users[min] = Users[i]; Users[i] = temp; } } this.uesrs = new ArrayList<>(Arrays.asList(Users).subList(0, usernum)); } } //UserRecords是用户记录类,保存用户各种通话、短信的记录, //各种计费规则将使用其中的部分或者全部记录。 //其属性从上到下依次是: //以及发送短信、接收短信的记录。 class UserRecords { //市内拨打电话 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> sendMessageRecords = new ArrayList<MessageRecord>(); //接收短信的记录 ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>(); void addCallingInCityRecords(CallRecord callRecord) { callingInCityRecords.add(callRecord); } void addCallingInProvinceRecords(CallRecord callRecord) { callingInProvinceRecords.add(callRecord); } void CallingInLandRecords(CallRecord callRecord) { callingInLandRecords.add(callRecord); } void adddAnswerInCityRecords(CallRecord answerRecord) { answerInCityRecords.add(answerRecord); } void addSendMessageRecords(MessageRecord sendMessageRecord) { sendMessageRecords.add(sendMessageRecord); } void addReceiveMessageRecords(MessageRecord receiveMessageRecord) { receiveMessageRecords.add(receiveMessageRecord); } ArrayList<MessageRecord> getSendMessageRecords() { return sendMessageRecords; } ArrayList<MessageRecord> getReceiveMessageRecords() { return receiveMessageRecords; } ArrayList<CallRecord> getCallingInCityRecords() { return callingInCityRecords; } ArrayList<CallRecord> getCallingInProvinceRecords() { return callingInProvinceRecords; } ArrayList<CallRecord> getCallingInLandRecords() { return callingInLandRecords; } ArrayList<CallRecord> getAnswerInCityRecords() { return answerInCityRecords; } ArrayList<CallRecord> getAnswerInProvinceRecords() { return answerInProvinceRecords; } ArrayList<CallRecord> getAnswerInLandRecords() { return answerInLandRecords; } } //User是用户类,包括属性: //userRecords (用户记录)、balance(余额)、chargeMode(计费方式)、number(号码) class User { String number, code, no; int model;//0为座机用户 1为手机用户 double balance = 100; ChargeMode chargeMode;//ChargeMode是计费方式的类: UserRecords userRecords = new UserRecords();// public User(int model) { this.model = model; this.chargeMode = new ChargeMode(model); } //计算余额 public double calBalance() { balance = balance - calCost() - this.chargeMode.getMonthlyRent(); return balance; } double calCost() { LandPhoneIInlandRule lpi = new LandPhoneIInlandRule(); LandPhoneInCityRule lpc = new LandPhoneInCityRule(); LandPhoneInProvinceRule lpp = new LandPhoneInProvinceRule(); double cost = 0; if (model == 0) return lpi.callCost(userRecords.callingInLandRecords) + lpc.callCost(userRecords.callingInCityRecords) + lpp.callCost(userRecords.callingInProvinceRecords); else { for (CallRecord callRecord : this.userRecords.callingInCityRecords) { //市内拨打市内电话 if (callRecord.answerAddressAreaCode.equals("0791")) cost += 0.1 * work.howlong(callRecord.startTime, callRecord.endTime); //市内拨打省内电话 else if (work.is_(callRecord.answerAddressAreaCode) == 1) cost += 0.2 * work.howlong(callRecord.startTime, callRecord.endTime); //市内拨打省外电话 else if (work.is_(callRecord.answerAddressAreaCode) == 2) cost += 0.3 * work.howlong(callRecord.startTime, callRecord.endTime); } for (CallRecord callRecord : this.userRecords.callingInProvinceRecords) cost += 0.3 * work.howlong(callRecord.startTime, callRecord.endTime);//省内漫游打电话0.3元/分钟 for (CallRecord callRecord : this.userRecords.callingInLandRecords) cost += 0.6 * work.howlong(callRecord.startTime, callRecord.endTime);//省外漫游拨打0.6元/分钟 for (CallRecord callRecord : this.userRecords.answerInLandRecords) cost += 0.3 * work.howlong(callRecord.startTime, callRecord.endTime);//省外漫游接听0.3元/分钟 return cost; } } UserRecords getUserRecords() { return userRecords; } void setUserRecords(UserRecords userRecords) { this.userRecords = userRecords; } double getBalance() { return balance; } ChargeMode getChargeMode() { return chargeMode; } void setChargeMode(ChargeMode chargeMode) { this.chargeMode = chargeMode; } String getNumber() { return number; } void setNumber(String number) { this.number = number; } } // MessageRecord(短信记录) class MessageRecord extends CommunicationRecord { String message; String getMessage() { return message; } void setMessage(String message) { this.message = message; } @Override String getCallingNumber() { return callingNumber; } @Override void setCallingNumber(String callingNumber) { this.callingNumber = callingNumber; } @Override String getAnswerNumber() { return answerNumber; } @Override void setAnswerNumber(String answerNumber) { this.answerNumber = answerNumber; } } public class Main { public static void main(String[] args) { work work = new work(); work.tran(); work.start_work(); } } class LandPhoneInProvinceRule extends CallChargeRule { @Override double callCost(ArrayList<CallRecord> callRecords) { double cost = 0.0; for (CallRecord callRecord : callRecords) cost += 0.3 * work.howlong(callRecord.startTime, callRecord.endTime); return cost; } } class LandPhoneInCityRule extends CallChargeRule { @Override double callCost(ArrayList<CallRecord> callRecords) { double cost = 0.0; for (CallRecord callRecord : callRecords) cost += 0.1 * work.howlong(callRecord.startTime, callRecord.endTime); return cost; } } class LandPhoneIInlandRule extends CallChargeRule { @Override double callCost(ArrayList<CallRecord> callRecords) { double cost = 0.0; for (CallRecord callRecord : callRecords) cost += 0.6 * work.howlong(callRecord.startTime, callRecord.endTime); return cost; } } abstract class CommunicationRecord { String callingNumber;//拨打号码 String answerNumber;//接听号码 abstract String getCallingNumber(); abstract void setCallingNumber(String callingNumber); abstract String getAnswerNumber(); abstract void setAnswerNumber(String answerNumber); } abstract class ChargeRule { } //ChargeMode是计费方式 class ChargeMode { ArrayList<ChargeRule> chargeRules = new ArrayList<>(); double MonthlyRent; double ccc, ccp, ccl;//市内拨打电话,省内长途,国内长途 double cp_, cl_;//省内漫游打电话,省外漫游拨打 double rp_;//省外漫游接听 public ChargeMode(int model) { if (model == 0) { MonthlyRent = 20; rp_ = 0; ccc = 0.1; ccp = 0.3; ccl = 0.6; } if (model == 1) { MonthlyRent = 15; ccc = 0.1; ccp = 0.2; ccl = 0.3; cp_ = 0.3; rp_ = 0.3; cl_ = 0.6; } } public ChargeMode() { } ArrayList<ChargeRule> getChargeRules() { return chargeRules; } void setChargeRules(ArrayList<ChargeRule> chargeRules) { this.chargeRules = chargeRules; } double getMonthlyRent() { return MonthlyRent; } } abstract class CallChargeRule extends ChargeRule { abstract double callCost(ArrayList<CallRecord> callRecords); } //CallRecord(通话记录类)包含属性: //通话的起始、结束时间以及 拨号地点的区号(callingAddressAreaCode)、接听地点的区号(answerAddressAreaCode)。 //区号用于记录在哪个地点拨打和接听的电话。对于座机,就是本机区号。 // 如果是手机号,则接打地点的区号和本机的开户地区会有差异。 class CallRecord extends CommunicationRecord { Date startTime; Date endTime; String callingAddressAreaCode; String answerAddressAreaCode; @Override String getCallingNumber() { return callingNumber; } @Override void setCallingNumber(String callingNumber) { this.callingNumber = callingNumber; } @Override String getAnswerNumber() { return answerNumber; } @Override void setAnswerNumber(String answerNumber) { this.answerNumber = answerNumber; } Date getStartTime() { return startTime; } void setStartTime(Date startTime) { this.startTime = startTime; } Date getEndTime() { return endTime; } void setEndTime(Date endTime) { this.endTime = endTime; } String getCallingAddressAreaCode() { return callingAddressAreaCode; } void setCallingAddressAreaCode(String callingAddressAreaCode) { this.callingAddressAreaCode = callingAddressAreaCode; } String getAnswerAddressAreaCode() { return answerAddressAreaCode; } void setAnswerAddressAreaCode(String answerAddressAreaCode) { this.answerAddressAreaCode = answerAddressAreaCode; } }

1|0类图与分析

本次作业主要是加入了手机业务,数据处理更加复杂了。最大复杂度和深度超出了合理范围。还有非常大的改进空间。

2|37-1 电信计费系列3-短信计费

实现一个简单的电信计费程序,针对手机的短信采用如下计费方式
1、接收短信免费,发送短信0.1元/条,超过3条0.2元/条,超过5条0.3元/条。
2、如果一次发送短信的字符数量超过10个,按每10个字符一条短信进行计算。

输入:
输入信息包括两种类型
1、逐行输入南昌市手机用户开户的信息,每行一个用户。
格式:u-号码 计费类型 (计费类型包括:0-座机 1-手机实时计费 2-手机A套餐 3-手机短信计费)
例如:u-13305862264 3
座机号码由区号和电话号码拼接而成,电话号码包含7-8位数字,区号最高位是0。
手机号码由11位数字构成,最高位是1。
本题只针对类型3-手机短信计费。
2、逐行输入本月某些用户的短信信息,短信的格式:
m-主叫号码,接收号码,短信内容 (短信内容只能由数字、字母、空格、英文逗号、英文句号组成)
m-18907910010 13305862264 welcome to jiangxi.
m-13305862264 18907910010 thank you.

注意:以上两类信息,先输入所有开户信息,再输入所有通讯信息,最后一行以“end”结束。
输出:
根据输入的详细短信信息,计算所有已开户的用户的当月短信费用(精确到小数点后2位,单位元)。假设每个用户初始余额是100元。
每条短信信息均单独计费后累加,不是将所有信息累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。
错误处理:
输入数据中出现的不符合格式要求的行一律忽略。
本题只做格式的错误判断,无需做内容上不合理的判断,比如同一个电话两条通讯记录的时间有重合、开户号码非南昌市的号码、自己给自己打电话等,此类情况都当成正确的输入计算。但时间的输入必须符合要求,比如不能输入2022.13.61 28:72:65。

本题只考虑短信计费,不考虑通信费用以及月租费。

建议类图:
参见图1、2、3:

image.png

图1

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

image.png

图2

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

image.png

图3
图3是计费规则的相关类,这些类的核心方法是:
calCost(ArrayList callRecords)。
该方法针根据输入参数callRecords中的所有记录计算某用户的某一项费用;如市话费。
输入参数callRecords的约束条件:必须是某一个用户的符合计费规则要求的所有记录。
SendMessageRule是发送短信的计费规则类,用于计算发送短信的费用。
LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三个类分别是座机拨打市内、省内、省外电话的计费规则类,用于实现这三种情况的费用计算。

(提示:可以从UserRecords类中获取各种类型的callRecords)。
 

注意:以上图中所定义的类不是限定要求,根据实际需要自行补充或修改。

1|0输入样例:

在这里给出一组输入。例如:

u-18907910010 3 m-18907910010 13305862264 aaaaaaaaaaaaaaaaaaaaaaa end
 

1|0输出样例:

在这里给出相应的输出。例如:

18907910010 0.3 99.7
 
### 输入样例1:
 

在这里给出一组输入。例如:

u-18907910010 3 m-18907910010 13305862264 aaaaaaaaaaaa m-18907910010 13305862264 aaaaaaa. m-18907910010 13305862264 bb,bbbb end
 
 

1|0输出样例1:

在这里给出相应的输出。例如:

18907910010 0.5 99.5

1|0代码(本题虽只需计算短信业务且PTA的提交系统允许大小太小,实际只提交了下面代码的短信部分,下方代码为这几次作业完整的电信系统)

import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; class work { ArrayList<String> strr = new ArrayList<>(); ArrayList<User> uesrs = new ArrayList<>(); ArrayList<CallRecord> callRecords = new ArrayList<>(); ArrayList<MessageRecord> messageRecords = new ArrayList<>(); int usernum = 0; public work() { Scanner sc = new Scanner(System.in); while (true) { String str = sc.nextLine(); if (str.equals("end")) return; strr.add(str); } } void tran() { // String tPattern0 = "t-0\\d{9,11}\\s" + "0\\d{9,11}\\s" // + "([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3}\\.(1[0-2]|[1-9])\\.([1-9]|(1|2)[0-9]|(30|31)) ([0|1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] ?){2}"; // String tPattern1 = "t-\\d{11}\\s\\d{3,4}\\s" + "\\d{11}\\s\\d{3,4}\\s" // + "([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3}\\.(1[0-2]|[1-9])\\.([1-9]|(1|2)[0-9]|(30|31)) ([0|1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] ?){2}"; // String tPattern2 = "t-\\d{11}\\s\\d{3,4}\\s" + "\\d{10,12}\\s" // + "([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3}\\.(1[0-2]|[1-9])\\.([1-9]|(1|2)[0-9]|(30|31)) ([0|1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] ?){2}"; // String tPattern3 = "t-\\d{10,12}\\s" + "1\\d{10}\\s\\d{3,4}\\s" // + "([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3}\\.(1[0-2]|[1-9])\\.([1-9]|(1|2)[0-9]|(30|31)) ([0|1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] ?){2}"; String uPattern = "^u-[0-9]{11,12} 0$"; String uPattern1 = "^u-[0-9]{11} 1$"; String uPattern2 = "^u-[0-9]{11} 3$"; //座机呼叫座机 String tPattern = "^t-\\d{10,12}\\s\\d{10,12}\\s([1-9][0-9]*\\.[1-9][0-9]?\\.[1-9][0-9]? ([0|1]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] ?){2}$"; //座机打手机 String tPattern1 = "^t-\\d{10,12}\\s\\d{11}\\s\\d{3,4}\\s([1-9][0-9]*\\.[1-9][0-9]?\\.[1-9][0-9]? ([0|1]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] ?){2}$"; // 手机互打 String tPattern2 = "^t-\\d{11}\\s\\d{3,4}\\s\\d{11}\\s\\d{3,4}\\s([1-9][0-9]*\\.[1-9][0-9]?\\.[1-9][0-9]? ([0|1]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] ?){2}$"; String tPattern3 = "[m]-1[0-9]{10}\\s" + "1[0-9]{10}\\s" + "[0-9a-zA-Z\\s\\.,]+"; boolean lock = false; // 11,12位只包含座机 for (String s : strr) { // if (Pattern.compile("[^-ut\\d: \\.]").matcher(s).find()) //出现的字符集合 // continue; if (s.matches(uPattern) && !lock) { Pattern r = Pattern.compile("^u-([0-9]{11,12}) (\\d)$"); User user = new User(0); Matcher m = r.matcher(s); if (m.find()) { user.model = Integer.parseInt(m.group(2)); user.number = m.group(1); if (!user.number.startsWith("0791")) continue; user.chargeMode = new LandlinePhoneCharging(); if (!is_in_users(user.number)) { usernum++; uesrs.add(user); } } } else if (s.matches(uPattern1) && !lock) { Pattern r = Pattern.compile("^u-([0-9]{11}) (1)$"); User user = new User(1); Matcher m = r.matcher(s); if (m.find()) { user.model = Integer.parseInt(m.group(2)); user.number = m.group(1); user.chargeMode = new LandlinePhoneCharging(); if (!is_in_users(user.number)) { usernum++; uesrs.add(user); } } } else if (s.matches(uPattern2) && !lock) { Pattern r = Pattern.compile("^u-([0-9]{11}) (3)$"); User user = new User(3); Matcher m = r.matcher(s); if (m.find()) { user.model = Integer.parseInt(m.group(2)); user.number = m.group(1); user.chargeMode = new LandlinePhoneCharging(); if (!is_in_users(user.number)) { usernum++; uesrs.add(user); } } } else if (s.matches(tPattern)) { CallRecord callRecord = new CallRecord(); Pattern r = Pattern.compile("t-([0-9]{10,12}) ([0-9]{10,12}) ([0-9]{4}\\.[0-9]{1,2}\\.[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}) ([0-9]{4}\\.[0-9]{1,2}\\.[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2})"); Matcher m = r.matcher(s); if (m.find()) { callRecord.callingNumber = m.group(1); if (is_three_area_code(m.group(1))) { if (m.group(1).length() == 10 || m.group(1).length() == 11) callRecord.callingAddressAreaCode = m.group(1).substring(0, 3); else continue; } else { if (m.group(1).length() == 11 || m.group(1).length() == 12) callRecord.callingAddressAreaCode = m.group(1).substring(0, 4); else continue; } callRecord.callingAddressAreaCode = m.group(1).substring(0, 4); callRecord.answerNumber = m.group(2); if (is_three_area_code(m.group(2))) { if (m.group(2).length() == 10 || m.group(2).length() == 11) callRecord.answerAddressAreaCode = m.group(2).substring(0, 3); else continue; } else { if (m.group(2).length() == 11 || m.group(2).length() == 12) callRecord.answerAddressAreaCode = m.group(2).substring(0, 4); else continue; } Date sd; Date fd; SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); try { sd = format.parse(m.group(3)); } catch (ParseException e) { continue; } try { fd = format.parse(m.group(4)); } catch (ParseException e) { continue; } callRecord.startTime = sd; callRecord.endTime = fd; if (sd.getTime() < fd.getTime() && dateStrIsValid(m.group(3)) && dateStrIsValid(m.group(4))) { callRecords.add(callRecord); lock = true; } } } else if (s.matches(tPattern1)) { CallRecord callRecord = new CallRecord(); Pattern r = Pattern.compile("^t-(\\d{10,12})\\s(\\d{11})\\s(\\d{3,4})\\s([0-9]{4}\\.[0-9]{1,2}\\.[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}) ([0-9]{4}\\.[0-9]{1,2}\\.[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2})"); Matcher m = r.matcher(s); if (m.find()) { callRecord.callingNumber = m.group(1); if (is_three_area_code(m.group(1))) { if (m.group(1).length() == 10 || m.group(1).length() == 11) callRecord.callingAddressAreaCode = m.group(1).substring(0, 3); else continue; } else { if (m.group(1).length() == 11 || m.group(1).length() == 12) callRecord.callingAddressAreaCode = m.group(1).substring(0, 4); else continue; } callRecord.callingAddressAreaCode = m.group(1).substring(0, 4); callRecord.answerNumber = m.group(2); callRecord.answerAddressAreaCode = m.group(3); Date sd; Date fd; SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); try { sd = format.parse(m.group(4)); } catch (ParseException e) { continue; } try { fd = format.parse(m.group(5)); } catch (ParseException e) { continue; } callRecord.startTime = sd; callRecord.endTime = fd; if (sd.getTime() < fd.getTime() && dateStrIsValid(m.group(3)) && dateStrIsValid(m.group(4))) { callRecords.add(callRecord); lock = true; } } } else if (s.matches(tPattern2)) { CallRecord callRecord = new CallRecord(); Pattern r = Pattern.compile("^t-(\\d{11})\\s(\\d{3,4})\\s(\\d{11})\\s(\\d{3,4}) ([0-9]{4}\\.[0-9]{1,2}\\.[0-9]{1,2}) ([0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2})"); Matcher m = r.matcher(s); if (m.find()) { callRecord.callingNumber = m.group(1); callRecord.callingAddressAreaCode = m.group(2); callRecord.answerNumber = m.group(3); callRecord.answerAddressAreaCode = m.group(4); Date sd; Date fd; SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); try { sd = format.parse(m.group(5)); } catch (ParseException e) { continue; } try { fd = format.parse(m.group(6)); } catch (ParseException e) { continue; } callRecord.startTime = sd; callRecord.endTime = fd; if (sd.getTime() < fd.getTime() && dateStrIsValid(m.group(3)) && dateStrIsValid(m.group(4))) { callRecords.add(callRecord); lock = true; } } } else if (s.matches(tPattern3)) { // String tPattern3 = "^m-\\d{11} \\d{11} .{1,}"; MessageRecord messageRecord = new MessageRecord(); Pattern r = Pattern.compile("^m-(\\d{11}) (\\d{11}) (.{1,})"); Matcher m = r.matcher(s); if (m.find()) { messageRecord.callingNumber = m.group(1); messageRecord.answerNumber = m.group(2); messageRecord.message = m.group(3); messageRecords.add(messageRecord); } } } } void start_work() { for (CallRecord callRecord : callRecords) { // 拨打电话记录 int num = user_num(callRecord.callingNumber); if (num != -1) { if (this.uesrs.get(num).model == 0) switch (code_num_where(callRecord.callingAddressAreaCode, callRecord.answerAddressAreaCode)) { case 0: this.uesrs.get(num).userRecords.callingInCityRecords.add(callRecord); break; case 1: this.uesrs.get(num).userRecords.callingInProvinceRecords.add(callRecord); break; case 2: this.uesrs.get(num).userRecords.callingInLandRecords.add(callRecord); break; default: break; } else { switch (work.is_(callRecord.callingAddressAreaCode)) { case 0: this.uesrs.get(num).userRecords.callingInCityRecords.add(callRecord); break; case 1: this.uesrs.get(num).userRecords.callingInProvinceRecords.add(callRecord); break; case 2: this.uesrs.get(num).userRecords.callingInLandRecords.add(callRecord); break; default: break; } } num = user_num(callRecord.answerNumber); if (num != -1) { switch (work.is_(callRecord.answerAddressAreaCode)) { case 0: this.uesrs.get(num).userRecords.answerInCityRecords.add(callRecord); break; case 1: this.uesrs.get(num).userRecords.answerInProvinceRecords.add(callRecord); break; case 2: this.uesrs.get(num).userRecords.answerInLandRecords.add(callRecord); break; default: break; } } } } for (MessageRecord messageRecord : messageRecords) { int num = user_num(messageRecord.callingNumber); if (num != -1) { if (this.uesrs.get(num).model == 3) { this.uesrs.get(num).userRecords.sendMessageRecords.add(messageRecord); } } num = user_num(messageRecord.answerNumber); if (num != -1) { if (this.uesrs.get(num).model == 3) { this.uesrs.get(num).userRecords.receiveMessageRecords.add(messageRecord); } } } this.sort_users(); for (int i = 0; i < usernum; i++) if (i != usernum - 1) System.out.println(uesrs.get(i).number + " " + Print(uesrs.get(i).calCost()) + " " + Print(uesrs.get(i).calBalance())); else System.out.print(uesrs.get(i).number + " " + Print(uesrs.get(i).calCost()) + " " + Print(uesrs.get(i).calBalance())); } // 输入字符计算短信条数 static int what_num(String str) { if (str.length() % 10 == 0) return str.length() / 10; else return str.length() / 10 + 1; } //判断时间是否合法 public boolean dateStrIsValid(String str) { SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");//括号内为日期格式,y代表年份,M代表年份中的月份(为避免与小时中的分钟数m冲突,此处用M),d代表月份中的天数 try { sd.setLenient(false);//此处指定日期/时间解析是否不严格,在true是不严格,false时为严格 sd.parse(str);//从给定字符串的开始解析文本,以生成一个日期 } catch (Exception e) { return false; } return true; } // 不足一分钟按一分钟计算时间 static long howlong(Date start, Date end) { long sec = (end.getTime() - start.getTime()) / 1000; if (sec % 60 == 0) return sec / 60; else return sec / 60 + 1; } // 返回用户组下标 int user_num(String num) { if (this.uesrs.isEmpty()) return -1; for (int i = 0; i < this.usernum; i++) { if (uesrs.get(i).number.equals(num)) return i; } return -1; } //是否重复用户判断 boolean is_in_users(String num) { if (this.uesrs.isEmpty()) return false; for (int i = 0; i < this.usernum; i++) { if (uesrs.get(i).number.equals(num)) return true; } return false; } // 两区号比较 市内0 省内长途1 国内长途2 int code_num_where(String code1, String code2) { int[] code1_num = area(code1); int[] code2_num = area(code2); if (code1.equals(code2)) return 0; else if ((code1_num[0] == code2_num[0] && code1_num[1] != code2_num[1])) return 1; else return 2; } // 市内省内接电话均免费,市内拨打市内电话0.1元/分钟,市内拨打省内电话0.2元/分钟,市内拨打省外电话0.3元/分钟, // 省内漫游打电话0.3元/分钟,省外漫游接听0.3元/分钟,省外漫游拨打0.6元/分钟; // int code_where(String code1, String code2) { // if(work.is_(code1)==0&&work.is_(code2)==0) // } // 返回是否为3位区号 boolean is_three_area_code(String num) { String[] arecode = new String[]{"010", "021", "022", "023", "852", "853", "020", "028", "029"}; for (String code : arecode) if (num.startsWith(code)) return true; return false; } //返回地区号 地区号两位组成 第一位为省份号 第二为为地区号 //00 为南昌 其他0开头的为江西省内其他 int[] area(String code) { int[] area_num = new int[2]; area_num[0] = 9; area_num[1] = 9; String[] area1 = {"0790", "0791", "0792", "0793", "0794", "0795", "0796", "0797", "0798", "0799", "0701"}; for (int i = 0; i < area1.length; i++) { if (code.equals(area1[i])) { area_num[0] = 1; area_num[1] = i; } } return area_num; } //南昌 0 江西省内 1 外2 static int is_(String code) { String[] codes = {"0790", "0791", "0792", "0793", "0794", "0795", "0796", "0797", "0798", "0799", "0701"}; if (code.equals("0791")) return 0; for (String codee : codes) if (code.equals(codee)) return 1; return 2; } // 打印标准格式 static String Print(double a) { if (Math.abs((int) a - a) < 1e-6) return (int) a + ".0"; else if ((Math.abs((int) a - a + 1) < 1e-6)) return (int) a + 1 + ".0"; else return new DecimalFormat("#.#").format(a); } // 用户按号码排序 void sort_users() { long[] user_num = new long[usernum]; User[] Users = new User[usernum]; for (int i = 0; i < usernum; i++) { user_num[i] = Long.parseLong(uesrs.get(i).number); Users[i] = this.uesrs.get(i); } for (int i = 0; i < usernum - 1; i++) { int min = i; // 每轮需要比较的次数 N-i for (int j = i + 1; j < usernum; j++) { if (user_num[j] < user_num[min]) { // 记录目前能找到的最小值元素的下标 min = j; } // 将找到的最小值和i位置所在的值进行交换 if (i != min) { long tmp = user_num[min]; user_num[min] = user_num[i]; user_num[i] = tmp; User temp = Users[min]; Users[min] = Users[i]; Users[i] = temp; } } } this.uesrs = new ArrayList<>(Arrays.asList(Users).subList(0, usernum)); } } //UserRecords是用户记录类,保存用户各种通话、短信的记录, //各种计费规则将使用其中的部分或者全部记录。 //其属性从上到下依次是: //以及发送短信、接收短信的记录。 class UserRecords { //市内拨打电话 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> sendMessageRecords = new ArrayList<MessageRecord>(); //接收短信的记录 ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>(); void addCallingInCityRecords(CallRecord callRecord) { callingInCityRecords.add(callRecord); } void addCallingInProvinceRecords(CallRecord callRecord) { callingInProvinceRecords.add(callRecord); } void CallingInLandRecords(CallRecord callRecord) { callingInLandRecords.add(callRecord); } void adddAnswerInCityRecords(CallRecord answerRecord) { answerInCityRecords.add(answerRecord); } void addSendMessageRecords(MessageRecord sendMessageRecord) { sendMessageRecords.add(sendMessageRecord); } void addReceiveMessageRecords(MessageRecord receiveMessageRecord) { receiveMessageRecords.add(receiveMessageRecord); } ArrayList<MessageRecord> getSendMessageRecords() { return sendMessageRecords; } ArrayList<MessageRecord> getReceiveMessageRecords() { return receiveMessageRecords; } ArrayList<CallRecord> getCallingInCityRecords() { return callingInCityRecords; } ArrayList<CallRecord> getCallingInProvinceRecords() { return callingInProvinceRecords; } ArrayList<CallRecord> getCallingInLandRecords() { return callingInLandRecords; } ArrayList<CallRecord> getAnswerInCityRecords() { return answerInCityRecords; } ArrayList<CallRecord> getAnswerInProvinceRecords() { return answerInProvinceRecords; } ArrayList<CallRecord> getAnswerInLandRecords() { return answerInLandRecords; } } //User是用户类,包括属性: //userRecords (用户记录)、balance(余额)、chargeMode(计费方式)、number(号码) class User { String number; int model;//0为座机用户 1为手机用户 3-手机短信计费 double balance = 100; ChargeMode chargeMode;//ChargeMode是计费方式的抽象类: UserRecords userRecords = new UserRecords();// public User(int model) { this.model = model; chargeMode = new ChargeMode(model); } //计算余额 public double calBalance() { if (this.model != 3) balance = balance - calCost() - chargeMode.getMonthlyRent(); else balance = balance - calCost(); return balance; } double calCost() { LandPhoneIInlandRule lpi = new LandPhoneIInlandRule(); LandPhoneInCityRule lpc = new LandPhoneInCityRule(); LandPhoneInProvinceRule lpp = new LandPhoneInProvinceRule(); if (model == 0) return lpi.callCost(userRecords.callingInLandRecords) + lpc.callCost(userRecords.callingInCityRecords) + lpp.callCost(userRecords.callingInProvinceRecords); else if (model == 1) { double cost = 0; for (CallRecord callRecord : this.userRecords.callingInCityRecords) { //市内拨打市内电话 if (callRecord.callingAddressAreaCode.equals("0791") && callRecord.answerAddressAreaCode.equals("0791")) cost += 0.1 * work.howlong(callRecord.startTime, callRecord.endTime); else if (work.is_(callRecord.callingAddressAreaCode) == 0 && work.is_(callRecord.answerAddressAreaCode) == 1) //市内拨打省内电话 cost += 0.2 * work.howlong(callRecord.startTime, callRecord.endTime); else if (work.is_(callRecord.callingAddressAreaCode) == 0 && work.is_(callRecord.answerAddressAreaCode) == 2) //市内拨打省外电话 cost += 0.3 * work.howlong(callRecord.startTime, callRecord.endTime); else if (work.is_(callRecord.callingAddressAreaCode) == 1) //省内漫游打电话 cost += 0.3 * work.howlong(callRecord.startTime, callRecord.endTime); // 省外漫游拨打0.6元/分钟 else if (work.is_(callRecord.callingAddressAreaCode) == 2) cost += 0.6 * work.howlong(callRecord.startTime, callRecord.endTime); } for (CallRecord callRecord : this.userRecords.callingInProvinceRecords) cost += chargeMode.ccp * work.howlong(callRecord.startTime, callRecord.endTime); for (CallRecord callRecord : this.userRecords.callingInLandRecords) cost += chargeMode.ccc * work.howlong(callRecord.startTime, callRecord.endTime); return 0; } else { int count = 0; double cost = 0; for (MessageRecord messageRecord : this.userRecords.sendMessageRecords) count += work.what_num(messageRecord.message); if (count < 4) cost = 0.1 * count; else if (count < 6) cost = 0.3 + (count - 3) * 0.2; else cost = 0.3 + 0.4 + (count - 5) * 0.3; return cost; } } UserRecords getUserRecords() { return userRecords; } void setUserRecords(UserRecords userRecords) { this.userRecords = userRecords; } double getBalance() { return balance; } ChargeMode getChargeMode() { return chargeMode; } void setChargeMode(ChargeMode chargeMode) { this.chargeMode = chargeMode; } String getNumber() { return number; } void setNumber(String number) { this.number = number; } } // MessageRecord(短信记录) class MessageRecord extends CommunicationRecord { String message; String getMessage() { return message; } void setMessage(String message) { this.message = message; } @Override String getCallingNumber() { return callingNumber; } @Override void setCallingNumber(String callingNumber) { this.callingNumber = callingNumber; } @Override String getAnswerNumber() { return answerNumber; } @Override void setAnswerNumber(String answerNumber) { this.answerNumber = answerNumber; } } public class Main { public static void main(String[] args) { work work = new work(); work.tran(); work.start_work(); } } class LandPhoneInProvinceRule extends CallChargeRule { @Override double callCost(ArrayList<CallRecord> callRecords) { double cost = 0.0; for (CallRecord callRecord : callRecords) cost += 0.3 * work.howlong(callRecord.startTime, callRecord.endTime); return cost; } } class LandPhoneInCityRule extends CallChargeRule { @Override double callCost(ArrayList<CallRecord> callRecords) { double cost = 0.0; for (CallRecord callRecord : callRecords) cost += 0.1 * work.howlong(callRecord.startTime, callRecord.endTime); return cost; } } class LandPhoneIInlandRule extends CallChargeRule { @Override double callCost(ArrayList<CallRecord> callRecords) { double cost = 0.0; for (CallRecord callRecord : callRecords) cost += 0.6 * work.howlong(callRecord.startTime, callRecord.endTime); return cost; } } class LandlinePhoneCharging extends ChargeMode { double monthlyRent = 20; @Override ArrayList<ChargeRule> getChargeRules() { return null; } @Override void setChargeRules(ArrayList<ChargeRule> chargeRules) { } @Override double getMonthlyRent() { return monthlyRent; } } abstract class CommunicationRecord { String callingNumber;//拨打号码 String answerNumber;//接听号码 abstract String getCallingNumber(); abstract void setCallingNumber(String callingNumber); abstract String getAnswerNumber(); abstract void setAnswerNumber(String answerNumber); } abstract class ChargeRule { } //ChargeMode是计费方式 class ChargeMode { ArrayList<ChargeRule> chargeRules = new ArrayList<>(); double MonthlyRent; double ccc, ccp, ccl;//市内拨打电话,省内长途,国内长途 double cp_, cl_;//省内漫游打电话,省外漫游拨打 double rp_;//省外漫游接听 public ChargeMode(int model) { if (model == 0) { MonthlyRent = 20; rp_ = 0; ccc = 0.1; ccp = 0.3; ccl = 0.6; } if (model == 1) { MonthlyRent = 15; ccc = 0.1; ccp = 0.2; ccl = 0.3; cp_ = 0.3; rp_ = 0.3; cl_ = 0.6; } } public ChargeMode() { } ArrayList<ChargeRule> getChargeRules() { return chargeRules; } void setChargeRules(ArrayList<ChargeRule> chargeRules) { this.chargeRules = chargeRules; } double getMonthlyRent() { return MonthlyRent; } } abstract class CallChargeRule extends ChargeRule { abstract double callCost(ArrayList<CallRecord> callRecords); } //CallRecord(通话记录类)包含属性: //通话的起始、结束时间以及 拨号地点的区号(callingAddressAreaCode)、接听地点的区号(answerAddressAreaCode)。 //区号用于记录在哪个地点拨打和接听的电话。对于座机,就是本机区号。 // 如果是手机号,则接打地点的区号和本机的开户地区会有差异。 class CallRecord extends CommunicationRecord { Date startTime; Date endTime; String callingAddressAreaCode; String answerAddressAreaCode; @Override String getCallingNumber() { return callingNumber; } @Override void setCallingNumber(String callingNumber) { this.callingNumber = callingNumber; } @Override String getAnswerNumber() { return answerNumber; } @Override void setAnswerNumber(String answerNumber) { this.answerNumber = answerNumber; } Date getStartTime() { return startTime; } void setStartTime(Date startTime) { this.startTime = startTime; } Date getEndTime() { return endTime; } void setEndTime(Date endTime) { this.endTime = endTime; } String getCallingAddressAreaCode() { return callingAddressAreaCode; } void setCallingAddressAreaCode(String callingAddressAreaCode) { this.callingAddressAreaCode = callingAddressAreaCode; } String getAnswerAddressAreaCode() { return answerAddressAreaCode; } void setAnswerAddressAreaCode(String answerAddressAreaCode) { this.answerAddressAreaCode = answerAddressAreaCode; } }

1|0类图与分析

由于本次代码是由上次的基础上添加而来,所以也继承了复杂度过高的问题,有非常大的改进空间。

3|0采坑心得

  1. 座机号有3位的,如北京区号为010,记得区分
  2. 正则匹配的时候,应该用较复杂的正则进行校验是否存在,再用比较简单的正则进行捕获。
  3. 时间之间的计算可以用getTime()方法相减,得出的是它们相差的秒数。

4|0改进建议

  整个系列的代码都存在复杂度过高的问题,改进空间非常大,增加代码易读性的路程任重道远。

5|0总结:

  老师最近几周讲起了代码规范性和结构的设计等,对把代码设计好有一定的帮助。下一步打算用老师讲的javafx把我的农夫过河实验给做好。


__EOF__

本文作者愿原力与你同在!
本文链接https://www.cnblogs.com/Saul-Goodman/p/16967999.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   浮云战争  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示