一、前言
6-8的大作业主要是实现电信计费系统。
二、设计与分析
-
6-7-1 电信计费系列1-座机计费
实现一个简单的电信计费程序:
假设南昌市电信分公司针对市内座机用户采用的计费方式:
月租20元,接电话免费,市内拨打电话0.1元/分钟,省内长途0.3元/分钟,国内长途拨打0.6元/分钟。不足一分钟按一分钟计。
南昌市的区号:0791,江西省内各地市区号包括:0790~0799以及0701。
输入格式:
输入信息包括两种类型
1、逐行输入南昌市用户开户的信息,每行一个用户,
格式:u-号码 计费类型 (计费类型包括:0-座机 1-手机实时计费 2-手机A套餐)
例如:u-079186300001 0
座机号码除区号外由是7-8位数字组成。
本题只考虑计费类型0-座机计费,电信系列2、3题会逐步增加计费类型。
2、逐行输入本月某些用户的通讯信息,通讯信息格式:
座机呼叫座机:t-主叫号码 接听号码 起始时间 结束时间
t-079186330022 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:11
以上四项内容之间以一个英文空格分隔,
时间必须符合"yyyy.MM.dd HH:mm:ss"格式。提示:使用SimpleDateFormat类。
以上两类信息,先输入所有开户信息,再输入所有通讯信息,最后一行以“end”结束。
注意:
本题非法输入只做格式非法的判断,不做内容是否合理的判断(时间除外,否则无法计算),比如:
1、输入的所有通讯信息均认为是同一个月的通讯信息,不做日期是否在同一个月还是多个月的判定,直接将通讯费用累加,因此月租只计算一次。
2、记录中如果同一电话号码的多条通话记录时间出现重合,这种情况也不做判断,直接 计算每条记录的费用并累加。
3、用户区号不为南昌市的区号也作为正常用户处理。
输出格式:
根据输入的详细通讯信息,计算所有已开户的用户的当月费用(精确到小数点后2位,
单位元)。假设每个用户初始余额是100元。
每条通讯信息单独计费后累加,不是将所有时间累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。
错误处理:
输入数据中出现的不符合格式要求的行一律忽略。
建议类图:
参见图1、2、3,可根据理解自行调整:
图1中User是用户类,包括属性:
userRecords (用户记录)、balance(余额)、chargeMode(计费方式)、number(号码)。
ChargeMode是计费方式的抽象类:
chargeRules是计费方式所包含的各种计费规则的集合,ChargeRule类的定义见图3。
getMonthlyRent()方法用于返回月租(monthlyRent)。
UserRecords是用户记录类,保存用户各种通话、短信的记录,
各种计费规则将使用其中的部分或者全部记录。
其属性从上到下依次是:
市内拨打电话、省内(不含市内)拨打电话、省外拨打电话、
市内接听电话、省内(不含市内)接听电话、省外接听电话的记录
以及发送短信、接收短信的记录。
图2中CommunicationRecord是抽象的通讯记录类:
包含callingNumber拨打号码、answerNumber接听号码两个属性。
CallRecord(通话记录)、MessageRecord(短信记录)是它的子类。
CallRecord(通话记录类)包含属性:
通话的起始、结束时间以及
拨号地点的区号(callingAddressAreaCode)、接听地点的区号(answerAddressAreaCode)。
区号用于记录在哪个地点拨打和接听的电话。座机无法移动,就是本机区号,如果是手机号,则会有差异。
图3是计费规则的相关类,这些类的核心方法是:
calCost(ArrayList<CallRecord> callRecords)。
该方法针根据输入参数callRecords中的所有记录计算某用户的某一项费用;如市话费。
输入参数callRecords的约束条件:必须是某一个用户的符合计费规则要求的所有记录。
LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三个类分别是
座机拨打市内、省内、省外电话的计费规则类,用于实现这三种情况的费用计算。
(提示:可以从UserRecords类中获取各种类型的callRecords)。
-
源码
import java.util.ArrayList; import java.util.Scanner; import java.text.SimpleDateFormat; import java.util.Date; public class Main { private ArrayList<User> users = new ArrayList<User>(); private void input() throws Exception { // TODO 自动生成的方法存根 Input input = new Input(); Scanner sc = new Scanner(System.in); String s = sc.nextLine(); while(!s.equals("end")) { input.format(s,this.users); s = sc.nextLine(); } } private void sort() { for( int i=0 ; i< users.size(); i++) { for( int j=i+1; j< users.size();j++) { if( users.get(i).getNumber().compareTo(users.get(j).getNumber())>0) { User u = users.get(i); //users.remove(j); users.set(i, users.get(j)); users.set(j, u); /*String temp = users.get(i).getNumber(); users.get(i).setNumber(users.get(j).getNumber()); users.get(j).setNumber(temp);*/ } } } } private void output() { for(int i=0 ;i<users.size();i++) { //System.out.println(users.get(i).getNumber()+" "+users.get(i).calCost()+" "+users.get(i).calBalance()); System.out.print(users.get(i).getNumber()); System.out.print(" "); print(users.get(i).calCost()); System.out.print(" "); print(users.get(i).calBalance()); System.out.println(); } } private void print(double num) { if (num * 1000 % 10 != 0) { System.out.printf("%.2f", num); } else { System.out.print(num); } } public static void main(String[] args) throws Exception { Main main = new Main(); main.input(); main.sort(); main.output(); } } class Input { //private String str2 = "t-\\d{10,12}\\s\\d{10,12}(\\s\\d{4}\\.([1-9]|(1[0-2]))\\.([1-9]|([12][0-9])|(3[01]))\\s(([01][0-9])|(2[0-3]))\\:[0-5][0-9]\\:[0-5][0-9]){2}"; private String str1 = "u-\\d{10,12}\\s[0-2]"; private String str2 = "t-\\d{10,12}\\s\\d{10,12}(\\s\\d{4}\\.[1-9][0-9]?\\.[1-9][0-9]?\\s(([01][0-9])|(2[0-3]))\\:[0-5][0-9]\\:[0-5][0-9]){2}"; // private String end = "end"; // 合法格式插入表中 public void format(String string, ArrayList<User> users) throws Exception { if (string.matches(str1)) { User u = new User(); String[] str = string.split("-| "); u.setNumber(str[1]); if (str[2].equals("0")) { ChargeMode chargeMode = new LandlinePhoneCharging(); u.setChargeMode(chargeMode ); } if(!users.contains(u)) users.add(u); } if (string.matches(str2)) { String[] str = string.split("-| "); String[] strDate1 = str[3].split("\\."); String[] strDate2 = str[5].split("\\."); if(this.dataLegal(strDate1,strDate2)) { this.scanf(str,users); } } } //输入记录 private void scanf(String[] str, ArrayList<User> users) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); CallRecord callRecord = new CallRecord(); callRecord.setCallingAddressAreaCode(str[1].substring(0, 4)); callRecord.setCallingNumber(str[1]); callRecord.setAnswerAddressAreaCode(str[2].substring(0, 4)); callRecord.setAnswerNumber(str[2]); String s1 = str[3].concat(" "); s1 = s1.concat(str[4]); Date date1 = sdf.parse(s1); String s2 = str[5].concat(" "); s2 = s2.concat(str[6]); Date date2 = sdf.parse(s2); callRecord.setStartTime(date1); callRecord.setEndTime(date2); for (int i = 0; i < users.size(); i++) { if (users.get(i).getNumber().equals(str[1])) { users.get(i).addUserRecords(callRecord); } } } // 判断月份是否符合实际 private boolean dataLegal(String[] strDate1, String[] strDate2) { int[] months = new int[] {0,31,0,31,30,31,30,31,31,30,31,30,31}; int year1 = Integer.parseInt(strDate1[0]); int month1 = Integer.parseInt(strDate1[1]); int day1 = Integer.parseInt(strDate1[2]); if(this.leapYear(year1)) months[2] = 29; else months[2] = 28; if(month1<1 || month1>12) { return false; } if( day1<1 || day1>months[month1]) { return false; } int year2 = Integer.parseInt(strDate2[0]); int month2 = Integer.parseInt(strDate2[1]); int day2 = Integer.parseInt(strDate2[2]); if(this.leapYear(year2)) months[2] = 29; else months[2] = 28; if(month2<1 || month2>12) { return false; } if( day2<1 || day2>months[month2]) { return false; } return true; } // 判断是否是闰年 private boolean leapYear(int year) { return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0); } } class User { private UserRecords userRecords = new UserRecords(); //使用记录 private double balance = 100 ; // 话费余额 private ChargeMode chargeMode ; //计费方式 private String number; // 号码 // 余额 public double calBalance() { double num = 0; num = this.balance - this.chargeMode.getMonthlyRent()-this.calCost(); return num; } // 通讯费用 public double calCost() { return this.chargeMode.calCost(userRecords); } public UserRecords getUserRecords() { return this.userRecords; } public void setUserRecords( UserRecords UserRecords) { this.userRecords = UserRecords; } // 添加通话记录 public void addUserRecords( CallRecord callRecord) { if(callRecord.getAnwserAddressAreaCode().matches("0791") && callRecord.getAnswerNumber().length()!=10 ) { this.userRecords.addCallingInCityRecords(callRecord);; }else if(callRecord.getAnwserAddressAreaCode().matches("079[0-9]") || (callRecord.getAnwserAddressAreaCode().matches("0701")&& callRecord.getAnswerNumber().length()!=10) ) { this.userRecords.addCallingInProvinceRecords(callRecord); }else { this.userRecords.addCallingInLandRecords(callRecord); } } public double getBalance() { return this.balance; } public ChargeMode getChargeMode() { return this.chargeMode; } public void setChargeMode( ChargeMode chargeMode) { this.chargeMode = chargeMode; } public String getNumber() { return this.number; } public void setNumber(String number) { this.number = number; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; User other = (User) obj; return this.number.equals(other.number); } } 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> sendMessageRecord = new ArrayList<MessageRecord>(); // 短信发送记录 private ArrayList<MessageRecord> receiveMessageRecord = new ArrayList<MessageRecord>(); // 短信接收记录 public void addCallingInCityRecords( CallRecord callRecord) { this.callingInCityRecords.add(callRecord); } public void addCallingInProvinceRecords( CallRecord callRecord) { this.callingInProvinceRecords.add(callRecord); } public void addCallingInLandRecords( CallRecord callRecord) { this.callingInLandRecords.add(callRecord); } public void addAnswerInCityRecords( CallRecord callRecord) { this.answerInCityRecords.add(callRecord); } public void addAnswerInProvinceRecords( CallRecord callRecord) { this.answerInProvinceRecords.add(callRecord); } public void addAnswerInLandRecords( CallRecord callRecord) { this.answerInLandRecords.add(callRecord); } public void addSendMessageRecords( MessageRecord sendMessageRecord) { this.sendMessageRecord.add(sendMessageRecord); } public void addRecevinceMessageRecords( MessageRecord receiveMessageRecord) { this.receiveMessageRecord.add(receiveMessageRecord); } public ArrayList<MessageRecord> getSendMessageRecords(){ return this.sendMessageRecord; } public ArrayList<MessageRecord> getReceiveMessageRecords(){ return this.receiveMessageRecord; } public ArrayList<CallRecord> getCallingInCityRecords(){ return this.callingInCityRecords; } public ArrayList<CallRecord> getCallingInProvinceRecords(){ return this.callingInProvinceRecords; } public ArrayList<CallRecord> getCallingInLandRecords(){ return this.callingInLandRecords; } public ArrayList<CallRecord> getAnswerInCityRecords(){ return this.answerInCityRecords; } public ArrayList<CallRecord> getAnswerInProvinceRecords(){ return this.answerInProvinceRecords; } public ArrayList<CallRecord> getAnswerInLandRecords(){ return this.answerInLandRecords; } } abstract class ChargeMode { private ArrayList<ChargeRule> chargeRules = new ArrayList<ChargeRule>(); //扣费规则表 public ArrayList<ChargeRule> getChargeRules(){ return this.chargeRules; } public void setChargeRules( ArrayList<ChargeRule> chargeRules) { this.chargeRules = chargeRules; } // 计算一通电话的花费 public abstract double calCost( UserRecords userRecords ) ; // 得到月租 public abstract double getMonthlyRent(); } class LandlinePhoneCharging extends ChargeMode { double monthlyRent = 20; @Override public double calCost(UserRecords userRecords) { double cost = 0; LandPhoneInCityRule l1 = new LandPhoneInCityRule(); LandPhoneInProvinceRule l2 = new LandPhoneInProvinceRule(); LandPhoneInlandRule l3 = new LandPhoneInlandRule(); cost = l1.calCost(userRecords.getCallingInCityRecords()) + l2.calCost(userRecords.getCallingInProvinceRecords())+ l3.calCost(userRecords.getCallingInLandRecords()); return cost; } @Override public double getMonthlyRent() { return this.monthlyRent; } } class CallRecord extends CommunicationRecord{ private Date startTime; // 通话开始时间 private Date endTime; // 通话结束时间 private String callingAddressAreaCode; // 通话拨出方所在地区 private String answerAddressAreaCode; // 通话接听方所在地区 public Date getStartTime() { return this.startTime; } public void setStartTime( Date startTime) { this.startTime = startTime; } public Date getEndTime() { return this.endTime; } public void setEndTime( Date endTime) { this.endTime = endTime; } public String getCallingAddressAreaCode() { return this.callingAddressAreaCode; } public void setCallingAddressAreaCode( String callingAddressAreaCode) { this.callingAddressAreaCode = callingAddressAreaCode; } public String getAnwserAddressAreaCode() { return this.answerAddressAreaCode; } public void setAnswerAddressAreaCode( String answerAddressAreaCode) { this.answerAddressAreaCode = answerAddressAreaCode; } } abstract class CommunicationRecord { protected String callingNumber; // 通话拨出方号码 protected String answerNumber; // 通话接听方号码 String getCallingNumber() { return this.callingNumber; } void setCallingNumber( String callingNumber) { this.callingNumber = callingNumber; } String getAnswerNumber() { return this.answerNumber; } void setAnswerNumber( String answerNumber) { this.answerNumber = answerNumber; } } class LandPhoneInCityRule extends CallChargeRule { @Override public double calCost(ArrayList<CallRecord> callRecords) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); double cost = 0; for( int i = 0 ; i<callRecords.size() ; i++) { String start = sdf.format(callRecords.get(i).getStartTime()); String end = sdf.format(callRecords.get(i).getEndTime()); String[] startTime = start.split(" |\\:|\\."); String[] endTime = end.split(" |\\:|\\."); double startd = Double.parseDouble(startTime[2]); double endd = Double.parseDouble(endTime[2]); double startH = Double.parseDouble(startTime[3]); double endH = Double.parseDouble(endTime[3]); double startm = Double.parseDouble(startTime[4]); double endm = Double.parseDouble(endTime[4]); double starts = Double.parseDouble(startTime[5]); double ends = Double.parseDouble(endTime[5]); double time = (endd-startd)*24*60+(endH-startH)*60+(endm-startm); if(ends-starts>0) { time++; } cost += time; } return cost/10; } } class LandPhoneInProvinceRule extends CallChargeRule { @Override public double calCost(ArrayList<CallRecord> callRecords) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); double cost = 0; for( int i = 0 ; i<callRecords.size() ; i++) { String start = sdf.format(callRecords.get(i).getStartTime()); String end = sdf.format(callRecords.get(i).getEndTime()); String[] startTime = start.split(" |\\:|\\."); String[] endTime = end.split(" |\\:|\\."); double startd = Double.parseDouble(startTime[2]); double endd = Double.parseDouble(endTime[2]); double startH = Double.parseDouble(startTime[3]); double endH = Double.parseDouble(endTime[3]); double startm = Double.parseDouble(startTime[4]); double endm = Double.parseDouble(endTime[4]); double starts = Double.parseDouble(startTime[5]); double ends = Double.parseDouble(endTime[5]); double time = (endd-startd)*24*60+(endH-startH)*60+(endm-startm); if(ends-starts>0) { time++; } cost += time; } return cost*3/10; } } class LandPhoneInlandRule extends CallChargeRule { @Override public double calCost(ArrayList<CallRecord> callRecords) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); double cost = 0; double sum = 0; for( int i = 0 ; i<callRecords.size() ; i++) { String start = sdf.format(callRecords.get(i).getStartTime()); String end = sdf.format(callRecords.get(i).getEndTime()); String[] startTime = start.split(" |\\:|\\."); String[] endTime = end.split(" |\\:|\\."); double startd = Double.parseDouble(startTime[2]); double endd = Double.parseDouble(endTime[2]); double startH = Double.parseDouble(startTime[3]); double endH = Double.parseDouble(endTime[3]); double startm = Double.parseDouble(startTime[4]); double endm = Double.parseDouble(endTime[4]); double starts = Double.parseDouble(startTime[5]); double ends = Double.parseDouble(endTime[5]); sum = (endd-startd)*24*60+(endH-startH)*60+(endm-startm); if(ends-starts>0) { sum++; } cost = cost+sum; } return cost*6/10; } } abstract class ChargeRule { } abstract class CallChargeRule extends ChargeRule { public abstract double calCost( ArrayList<CallRecord> callRecords); } class MessageRecord extends CommunicationRecord { private String message; public String getMessage() { return this.message; } public void setMessage( String message) { this.message = message; } }View Code
-
思路
首先搞清楚类图中每个类的功能,和每个类其中的方法都能实现什么,这样就完成了一半以上了,因为这个题目我感觉最大的难点就在与无法得到准确的正则表达式,进行输入数据是否符合要求的。还有就是要注意有的区号是三个字符的。
-
类图
-
代码复杂度
-
改进建议
这题我是没拿到满分的,当时还有4个点没过,我感觉是正则表达式出现了问题,因为在后面第7次作业的时候我全部点都过了,思路还是使用在这题一样的思路,唯一改变的只有正则表达式,所以我建议可以修改下正则表达式,当然也可以直接在下面题目中拿正则表达式。
-
7-7-1 电信计费系列2-手机+座机计费
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
-
源码
import java.text.ParseException; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Scanner; abstract class AbstractCallChargeRule extends AbstractChargeRule { public AbstractCallChargeRule(double money) { super(money); // TODO 自动生成的构造函数存根 } public abstract double calCost(UserRecords userRecord) ; } abstract class AbstractChargeMode { // 计费规则表 private ArrayList<AbstractChargeRule> chargeRuleList = new ArrayList<AbstractChargeRule>(); // 得到计费规则表 public ArrayList<AbstractChargeRule> getChargeRuleList(){ return this.chargeRuleList; } // 设置计费规则表 public void addChargeRuleList(AbstractChargeRule abstractChargeRule){ this.chargeRuleList.add(abstractChargeRule); } // 计算通信费用 public abstract double calCost(UserRecords userRecords) ; // 得到月租 public abstract double getMonthlyRent() ; } abstract class AbstractChargeRule { private double money; public AbstractChargeRule(double money) { super(); this.money = money; } public double getMoney() { return this.money; } // 计算通话费用 public abstract double calCost(UserRecords userRecord) ; } abstract class AbstractCommunicationRecord { private String callingNumber; private String answerNumber; public String getCallingNumber() { return this.callingNumber; } public String getAnswerNumber() { return this.answerNumber; } public void setCallingNumber(String callingNumber) { this.callingNumber = callingNumber; } public void setAnswerNumber(String answerNumber) { this.answerNumber = answerNumber; } } abstract class AbstractMessageChargeRule extends AbstractChargeRule { public AbstractMessageChargeRule(double money) { super(money); // TODO 自动生成的构造函数存根 } public abstract double calCost(UserRecords userRecord) ; } class CallRecord extends AbstractCommunicationRecord { private Date startTime; private Date endTime; private String callingAddressAreaCode; private String answerAddressAreaCode; public Date getStartTime() { return this.startTime; } public Date getEndTime() { return this.endTime; } public void setStartTime(Date startTime) { this.startTime = startTime; } public void setEndTime(Date endTime) { this.endTime = endTime; } public String getCallingAddressAreaCode() { return this.callingAddressAreaCode; } public String getAnswerAddressAreaCode() { return this.answerAddressAreaCode; } public void setCallingAddressAreaCode(String callingAddressAreaCode) { this.callingAddressAreaCode = callingAddressAreaCode; } public void setAnswerAddressAreaCode(String answerAddressAreaCode) { this.answerAddressAreaCode = answerAddressAreaCode; } } class CityCallCityRule extends AbstractCallChargeRule { public CityCallCityRule(double money) { super(money); // TODO 自动生成的构造函数存根 } public double calCost(UserRecords userRecord) { double cost = 0; SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); ArrayList<CallRecord> list = userRecord.getCityCallCityRecords(); for(CallRecord c:list) { long msec = c.getEndTime().getTime() - c.getStartTime().getTime(); long sec = msec / 1000 ; long min = sec / 60; if(sec % 60 != 0) min++; cost += min*this.getMoney(); } return cost; } } class CityCallLandRule extends AbstractCallChargeRule { public CityCallLandRule(double money) { super(money); // TODO 自动生成的构造函数存根 } public double calCost(UserRecords userRecord) { double cost = 0; SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); ArrayList<CallRecord> list = userRecord.getCityCallLandRecords(); for(CallRecord c:list) { long msec = c.getEndTime().getTime() - c.getStartTime().getTime(); long sec = msec / 1000 ; long min = sec / 60; if(sec % 60 != 0) min++; cost += min*this.getMoney(); } return cost; } } class CityCallProvinceRule extends AbstractCallChargeRule { public CityCallProvinceRule(double money) { super(money); // TODO 自动生成的构造函数存根 } public double calCost(UserRecords userRecord) { double cost = 0; SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); ArrayList<CallRecord> list = userRecord.getCityCallProvinceRecords(); for(CallRecord c:list) { long msec = c.getEndTime().getTime() - c.getStartTime().getTime(); long sec = msec / 1000 ; long min = sec / 60; if(sec % 60 != 0) min++; cost += min*this.getMoney(); } return cost; } } class InputTool { private String str1 = "u-\\d{10,12}\\s[0-2]"; private String str2 = "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}"; private String str3 = "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}"; private String str5 = "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}"; private String str4 = "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}"; /* // 座机打座机 private String str2 = "t-[0]\\d{9,11}\\s[0]\\d{9,11}(\\s\\d{4}\\.\\d{1,2}\\.\\d{1,2}\\s(([01][0-9])|(2[0-3]))\\:[0-5][0-9]\\:[0-5][0-9]){2}"; // 手机打手机 private String str3 = "t-([1]\\d{10}\\s[0]\\d{2,3}\\s){2}\\d{4}\\.\\d{1,2}\\.\\d{1,2}\\s(([01][0-9])|([2][0-3]))\\:[0-5][0-9]\\:[0-5][0-9]\\s\\d{4}\\.\\d{1,2}\\.\\d{1,2}\\s(([01][0-9])|([2][0-3]))\\:[0-5][0-9]\\:[0-5][0-9]"; // 座机打手机 t-079182200000 13986300066 0791 2022.8.31 23:50:25 2022.9.1 00:00:11 private String str4 = "t-[0]\\d{9,11}\\s[1]\\d{10}\\s[0]\\d{3}(\\s\\d{4}\\.\\d{1,2}\\.\\d{1,2}\\s(([01][0-9])|(2[0-3]))\\:[0-5][0-9]\\:[0-5][0-9]){2}"; // 手机打座机 t-13986300001 0791 079186300001 2022.12.31 23:50:25 2023.1.1 00:05:11 private String str5 = "t-[1]\\d{10}\\s[0]\\d{3}\\s[0]\\d{9,11}(\\s\\d{4}\\.\\d{1,2}\\.\\d{1,2}\\s(([01][0-9])|(2[0-3]))\\:[0-5][0-9]\\:[0-5][0-9]){2}"; */ public int format(String string) { if (string.matches(str1)) return 1; if (string.matches(str2) && this.formatDate(string)) { return 2; } if (string.matches(str3) && this.formatDate(string)) { return 3; } if (string.matches(str4) && this.formatDate(string)) { return 4; } if (string.matches(str5) && this.formatDate(string)) { return 5; } return 0; } private boolean formatDate(String string) { String[] str = string.split(" |\\.|\\:"); int k = str.length - 12; int[] a = new int[12]; for (int i = 0; i < 12; i++) { a[i] = Integer.parseInt(str[i + k]); } return this.judgeLegal(a); } private boolean judgeLegal(int[] a) { if (a[0] > 9999 || a[6] > 9999 || a[0] == 0 || a[6] == 0) return false; if (a[1] > 12 || a[7] > 12 || a[1] == 0 || a[7] == 0) { return false; } if (a[2] > 31 || a[8] > 31 || a[2] == 0 || a[8] == 0) { return false; } return true; } } class LandAnswerRule extends AbstractCallChargeRule { public LandAnswerRule(double money) { super(money); // TODO 自动生成的构造函数存根 } @Override public double calCost(UserRecords userRecord) { double cost = 0; SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); ArrayList<CallRecord> list = userRecord.getLandAnswerRecords(); for(CallRecord c:list) { long msec = c.getEndTime().getTime() - c.getStartTime().getTime(); long sec = msec / 1000 ; long min = sec / 60; if(sec % 60 != 0) min++; cost += min*this.getMoney(); } return cost; } } class LandCallLandRule extends AbstractCallChargeRule { public LandCallLandRule(double money) { super(money); // TODO 自动生成的构造函数存根 } @Override public double calCost(UserRecords userRecord) { double cost = 0; SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); ArrayList<CallRecord> list = userRecord.getLandCallLandRecords(); for(CallRecord c:list) { long msec = c.getEndTime().getTime() - c.getStartTime().getTime(); long sec = msec / 1000 ; long min = sec / 60; if(sec % 60 != 0) min++; cost += min*this.getMoney(); } return cost; } } class LandlinePhoneCharging extends AbstractChargeMode { private double monthlyRent = 20; @Override public double getMonthlyRent() { return this.monthlyRent; } @Override public double calCost(UserRecords userRecords) { ArrayList<AbstractChargeRule> ruleList = this.getChargeRuleList(); double cost = 0; for(AbstractChargeRule a:ruleList) { cost += a.calCost(userRecords); } return cost; } } public class Main { private ArrayList<User> userList = new ArrayList<User>(); public static void main(String[] args) throws Exception { Main main = new Main(); Scanner sc = new Scanner(System.in); InputTool input = new InputTool(); OutputTool output = new OutputTool(); String string = sc.nextLine(); while(!string.equals("end")) { int flag = input.format(string); if( flag != 0) { main.play(flag,string); } // System.out.println(input.format(string)); string = sc.nextLine(); } main.sortUserList(); for(User u:main.userList) { output.print(u); System.out.println(); } } private void sortUserList() { ArrayList<User> list = new ArrayList<User>(); User u; while(userList.size()>0) { u = userList.get(0); for(int i=1;i<userList.size();i++) { if(u.getNumber().compareTo(userList.get(i).getNumber())>0) { u = userList.get(i); } } list.add(u); userList.remove(u); } this.userList = list; } private void play(int flag, String string) throws Exception { String[] str = string.split(" |\\-"); if(flag == 1) { this.addUser(str); }else if(flag == 2) { // 座机打座机 this.addLandlinePhoneRecord(str); }else if(flag == 3) { // 手机打手机 this.addMobliePhoneRecord(str); }else if(flag == 4) { // 座机打手机 this.addLandlineCallMoblieRecord(str); }else if(flag == 5) { // 手机打座机 this.addMoblieCallLandlineRecord5(str); } } // t-13986300001 0791 079186300001 2022.12.31 23:50:25 2023.1.1 00:05:11 private void addMoblieCallLandlineRecord5(String[] str) throws Exception { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); CallRecord callRecord = new CallRecord(); callRecord.setCallingNumber(str[1]); callRecord.setAnswerNumber(str[3]); callRecord.setCallingAddressAreaCode(str[2]); callRecord.setAnswerAddressAreaCode(str[3].substring(0,4)); str[4]=str[4].concat(" "); str[4]=str[4].concat(str[5]); callRecord.setStartTime(simpleDateFormat.parse(str[4])); str[6]=str[6].concat(" "); str[6]=str[6].concat(str[7]); callRecord.setEndTime(simpleDateFormat.parse(str[6])); for(User u:userList) { if(u.getNumber().equals(str[1])) { //打出去 this.addCallRecord(u,callRecord); } if(u.getNumber().equals(str[3])) { //接进来 this.addAnswerRecord(u,callRecord); } } } // t-079182200000 13986300066 0791 2022.8.31 23:50:25 2022.9.1 00:00:11 private void addLandlineCallMoblieRecord(String[] str) throws Exception { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); CallRecord callRecord = new CallRecord(); callRecord.setCallingNumber(str[1]); callRecord.setAnswerNumber(str[2]); callRecord.setCallingAddressAreaCode(str[1].substring(0,4)); callRecord.setAnswerAddressAreaCode(str[3]); str[4]=str[4].concat(" "); str[4]=str[4].concat(str[5]); callRecord.setStartTime(simpleDateFormat.parse(str[4])); str[6]=str[6].concat(" "); str[6]=str[6].concat(str[7]); callRecord.setEndTime(simpleDateFormat.parse(str[6])); for(User u:userList) { if(u.getNumber().equals(str[1])) { //打出去 this.addCallRecord(u,callRecord); } if(u.getNumber().equals(str[2])) { //接进来 this.addAnswerRecord(u,callRecord); } } } private void addMobliePhoneRecord(String[] str) throws Exception { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); CallRecord callRecord = new CallRecord(); callRecord.setCallingNumber(str[1]); callRecord.setAnswerNumber(str[3]); callRecord.setCallingAddressAreaCode(str[2]); callRecord.setAnswerAddressAreaCode(str[4]); str[5]=str[5].concat(" "); str[5]=str[5].concat(str[6]); callRecord.setStartTime(simpleDateFormat.parse(str[5])); str[7]=str[7].concat(" "); str[7]=str[7].concat(str[8]); callRecord.setEndTime(simpleDateFormat.parse(str[7])); for(User u:userList) { if(u.getNumber().equals(str[1])) { //打出去 this.addCallRecord(u,callRecord); } if(u.getNumber().equals(str[3])) { //接进来 this.addAnswerRecord(u,callRecord); } } } private void addAnswerRecord(User u, CallRecord callRecord) { String answerAddress = callRecord.getAnswerAddressAreaCode(); if(!answerAddress.equals("0701")&&!answerAddress.matches("079[0-9]?")) { u.getUserRecord().addLandAnswerRecords(callRecord); } } private void addCallRecord(User u, CallRecord callRecord) { String callAddress = callRecord.getCallingAddressAreaCode(); String answerAddress = callRecord.getAnswerAddressAreaCode(); if(callAddress.equals("0791")) { if(answerAddress.equals("0791")) { u.getUserRecord().addCityCallCityRecords(callRecord); }else if(answerAddress.equals("0701")||answerAddress.matches("079[0-9]?")) { u.getUserRecord().addCityCallProvinceRecords(callRecord); }else { u.getUserRecord().addCityCallLandRecords(callRecord); } }else if(callAddress.equals("0701")||callAddress.matches("079[0-9]?")) { u.getUserRecord().addProvinceCallLandRecords(callRecord); }else { u.getUserRecord().addLandCallLandRecords(callRecord); } } private void addLandlinePhoneRecord(String[] str) throws Exception { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); //String dateStr = "2019-01-03 10:59:27"; //Date date = simpleDateFormat.parse(dateStr); CallRecord callRecord = new CallRecord(); callRecord.setCallingNumber(str[1]); callRecord.setAnswerNumber(str[2]); callRecord.setCallingAddressAreaCode(str[1].substring(0, 4)); callRecord.setAnswerAddressAreaCode(str[2].substring(0, 4)); str[3]=str[3].concat(" "); str[3]=str[3].concat(str[4]); callRecord.setStartTime(simpleDateFormat.parse(str[3])); str[5]=str[5].concat(" "); str[5]=str[5].concat(str[6]); callRecord.setEndTime(simpleDateFormat.parse(str[5])); for(User u:userList) { if(u.getNumber().equals(str[1])) { if(str[2].substring(0, 4).equals("0791")) { u.getUserRecord().addCityCallCityRecords(callRecord); }else if(str[2].substring(0, 4).equals("0701")||str[2].substring(0, 4).matches("079[0-9]")) { u.getUserRecord().addCityCallProvinceRecords(callRecord); }else { u.getUserRecord().addCityCallLandRecords(callRecord); } } } } private void addUser(String[] str) { if(this.noHaveUser(str)) { if(str[2].equals("0")) { //座机 User u = new User(); u.setNumber(str[1]); LandlinePhoneCharging a = new LandlinePhoneCharging(); CityCallCityRule r1 = new CityCallCityRule(0.1); CityCallProvinceRule r2 = new CityCallProvinceRule(0.3); CityCallLandRule r3 = new CityCallLandRule(0.6); a.addChargeRuleList(r1); a.addChargeRuleList(r2); a.addChargeRuleList(r3); u.setChargeMode(a); this.userList.add(u); } if(str[2].equals("1")) { //手机 User u = new User(); u.setNumber(str[1]); MobilePhoneCharging a = new MobilePhoneCharging(); CityCallCityRule r1 = new CityCallCityRule(0.1); CityCallProvinceRule r2 = new CityCallProvinceRule(0.2); CityCallLandRule r3 = new CityCallLandRule(0.3); ProvinceCallLandRule r4 = new ProvinceCallLandRule(0.3); LandCallLandRule r5 = new LandCallLandRule(0.6); LandAnswerRule r6 = new LandAnswerRule(0.3); a.addChargeRuleList(r1); a.addChargeRuleList(r2); a.addChargeRuleList(r3); a.addChargeRuleList(r4); a.addChargeRuleList(r5); a.addChargeRuleList(r6); u.setChargeMode(a); this.userList.add(u); } } } private boolean noHaveUser(String[] str) { for(User u:this.userList) { if(u.getNumber().compareTo(str[1])==0) return false; } return true; } } class MessageRecord extends AbstractCommunicationRecord { private String message; public String getMessgae() { return this.message; } public void setMessage(String message) { this.message = message; } } class MobilePhoneCharging extends AbstractChargeMode { private double monthlyRent = 15; @Override public double calCost(UserRecords userRecords) { ArrayList<AbstractChargeRule> ruleList = this.getChargeRuleList(); double cost = 0; for(AbstractChargeRule a:ruleList) { cost += a.calCost(userRecords); } return cost; } @Override public double getMonthlyRent() { // TODO 自动生成的方法存根 return this.monthlyRent; } } class OutputTool { public void print(User u) { DecimalFormat df = new DecimalFormat("#.00"); System.out.print(u.getNumber()+" "); double a = Double.parseDouble( df.format(u.calCost())); double b = Double.parseDouble( df.format(u.calBalance())); System.out.print(a+" "+b); } } class ProvinceCallLandRule extends AbstractCallChargeRule { public ProvinceCallLandRule(double money) { super(money); // TODO 自动生成的构造函数存根 } @Override public double calCost(UserRecords userRecord) { double cost = 0; SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); ArrayList<CallRecord> list = userRecord.getProvinceCallLandRecords(); for(CallRecord c:list) { long msec = c.getEndTime().getTime() - c.getStartTime().getTime(); long sec = msec / 1000 ; long min = sec / 60; if(sec % 60 != 0) min++; cost += min*this.getMoney(); } return cost; } } class SendMessageRule extends AbstractMessageChargeRule { public SendMessageRule(double money) { super(money); // TODO 自动生成的构造函数存根 } @Override public double calCost(UserRecords userRecord) { // TODO 自动生成的方法存根 return 0; } } class User { // 用户通信记录 private UserRecords userRecords= new UserRecords(); // 用户余额,初始100 private double balance = 100 ; // 用户套餐 private AbstractChargeMode chargeMode; // 用户号码 private String number; // 计算余额 public double calBalance() { this.balance = this.balance - this.calCost() - chargeMode.getMonthlyRent(); return this.balance; } // 计算费用 public double calCost() { return chargeMode.calCost(userRecords); } // 得到通信记录 public UserRecords getUserRecord() { return this.userRecords; } // 设置通信记录 public void setUserRecord(UserRecords userRecords) { this.userRecords = userRecords; } // 得到用户余额 public double getBalance() { return this.balance; } // 得到用户套餐 public AbstractChargeMode getChargeMode() { return this.chargeMode; } // 设置用户套餐 public void setChargeMode(AbstractChargeMode chargeMode) { this.chargeMode = chargeMode; } // 得到用户号码 public String getNumber() { return this.number; } // 设置用户号码 public void setNumber(String number) { this.number = number; } } class UserRecords { private ArrayList<CallRecord> cityCallCityRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> cityCallProvinceRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> cityCallLandRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> provinceCallLandRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> landCallLandRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> cityAnswerRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> provinceAnswerRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> landAnswerRecords = new ArrayList<CallRecord>(); private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>(); private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>(); public void addCityCallCityRecords(CallRecord callRecords) { this.cityCallCityRecords.add(callRecords); } public void addCityCallProvinceRecords(CallRecord callRecords) { this.cityCallProvinceRecords.add(callRecords); } public void addCityCallLandRecords(CallRecord callRecords) { this.cityCallLandRecords.add(callRecords); } public void addProvinceCallLandRecords(CallRecord callRecords) { this.provinceCallLandRecords.add(callRecords); } public void addLandCallLandRecords(CallRecord callRecords) { this.landCallLandRecords.add(callRecords); } public void addCityAnswerRecords(CallRecord callRecords) { this.cityAnswerRecords.add(callRecords); } public void addProvinceAnswerRecords(CallRecord callRecords) { this.provinceAnswerRecords.add(callRecords); } public void addLandAnswerRecords(CallRecord callRecords) { this.landAnswerRecords.add(callRecords); } public void addSendMessageRecords(MessageRecord messageRecord) { this.sendMessageRecords.add(messageRecord); } public void addReceiveMessageRecords(MessageRecord messageRecord) { this.receiveMessageRecords.add(messageRecord); } public ArrayList<CallRecord> getCityCallCityRecords(){ return this.cityCallCityRecords; } public ArrayList<CallRecord> getCityCallProvinceRecords(){ return this.cityCallProvinceRecords; } public ArrayList<CallRecord> getCityCallLandRecords(){ return this.cityCallLandRecords; } public ArrayList<CallRecord> getProvinceCallLandRecords(){ return this.provinceCallLandRecords; } public ArrayList<CallRecord> getLandCallLandRecords(){ return this.landCallLandRecords; } public ArrayList<CallRecord> getCityAnswerRecords(){ return this.cityAnswerRecords; } public ArrayList<CallRecord> getProvinceAnswerRecords(){ return this.provinceAnswerRecords; } public ArrayList<CallRecord> getLandAnswerRecords(){ return this.landAnswerRecords; } public ArrayList<MessageRecord> getSendMessageRecords(){ return this.sendMessageRecords; } public ArrayList<MessageRecord> getReceiveMessageRecords(){ return this.receiveMessageRecords; } }View Code
-
思路分析
在上一次作业没有的的作业没有满分后,我选择了删除整个项目,然后重新编写,最后还是发现问题出现在了正则表达式上面。
-
类图
-
代码复杂度
-
改进建议
我这次是使用面对抽象层来进行编码的,不过还有有些问题,就比如有些应该单独放在特定子类的属性和方法,我选择了编写的方便直接放在父类中,减少了代码的工作量,但是如果后期相对代码进行扩展和修改就会有点麻烦,所以有兴趣的话,也可以去修改下我上面父类的属性和代码。
-
8-7-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”结束。
-
思路分析
太简单,没有思路。
-
类图
-
代码复杂度
-
改进建议
太简单,没有建议,唯一要注意就是短信有合法符号范围限定。
总结
JAVA课程到这里也应该算正式结束了,我也收获到了很多,自我感觉还算掌握了面对对象编程的思想。