第三次Blog作业-21201903-陈靖宇
OO第三次博客作业
目录
1.前言
2.设计分析
3.踩坑心得
4.改进建议
5.总结
1.前言
本次Blog主要讨论PTA三次电信计费作业。
2.设计分析
PTA-电信计费系列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,可根据理解自行调整:
源代码如下:
import java.math.RoundingMode; import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); Date dateSt = null; Date dateEd = null; String dateS; String dateE; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); ArrayList<User> users = new ArrayList<>(); String x = in.nextLine(); while (!x.equals("")) { if(x.charAt(0)!='t'){ if (x.charAt(0) == 'u') { String t = x.substring(2); String[] ak = t.split(" +"); if((ak[0].length()!=11&&ak[0].length()!=12)||!ak[0].substring(0,4).equals("0791")){ continue; } users.add(new User(x.substring(2, 14))); //x = in.nextLine(); } } else { break; } x = in.nextLine(); } while (!x.equals("end")){ if (!x.equals("")) { if(x.charAt(0)=='t'){ int jud = 0; if(Ks(x)){ x = x.substring(2); } else{ x=in.nextLine(); continue; } String[] ak = x.split(" +"); if(ak.length!=6){ x=in.nextLine(); continue; } dateS = ak[2] + " " + ak[3]; dateE = ak[4] + " " + ak[5]; if((ak[1].length()!=11&&ak[1].length()!=12)||(ak[0].length()!=11&&ak[0].length()!=12)){ x=in.nextLine(); continue; } try { dateSt = simpleDateFormat.parse(dateS); } catch (ParseException e) { x=in.nextLine(); continue; } try { dateEd = simpleDateFormat.parse(dateE); } catch (ParseException e) { x=in.nextLine(); continue; } CallRecord callRecord = new CallRecord(); callRecord.setStartTime(dateSt); callRecord.setEndTime(dateEd); if (judgePr(ak[0], ak[1])) { jud++; } if (judgeCity(ak[0], ak[1])) { jud++; } for (User user : users) { if (ak[0].equals(user.getNumber())) { switch (jud) { case 0: user.getUserRecords().addCallingInLandRecords(callRecord); //user. break; case 1: user.getUserRecords().addCallingInProvinceRecords(callRecord); break; case 2: user.getUserRecords().addCallingInCityRecords(callRecord); break; } } }//拨号通讯记录录入,如果下次要再匹配接听记录再加一个for循环即可 //x = in.nextLine(); } } //System.out.println(dateEd.getTime()-dateSt.getTime()); dateEd=null; dateSt=null; x = in.nextLine(); } //System.out.println(dateEd.getTime()-dateSt.getTime()); //users= String []all=new String[users.size()]; for (int i=0;i<users.size();i++){ all[i]=users.get(i).getNumber()+" "+tran(users.get(i).calBalance())+" "+tran(users.get(i).getBalance()); } Arrays.sort(all); if(users.size()!=0){ for (int i=0;i<users.size();i++){ System.out.println(all[i]); } } /*for (User user:users) { System.out.println(user.getUserRecords().getCallingInCityRecords()); }*/ } public static String format1(double x){ return String.format("%.1f",x); } public static boolean judgePr(String x,String y){ return x.substring(0, 2).equals(y.substring(0, 2)); } public static boolean judgeCity(String x,String y){ return x.substring(0, 4).equals(y.substring(0, 4)); } public static String tran(double x) { String str_d = String.valueOf(x); str_d = str_d.substring(str_d.indexOf(".") + 1); int len = str_d.length(); len = 1; String out = String.format("%." + len + "f", x); return out; } public static boolean Ks(String x) { String w; //w = "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d$"; w = "[t]-0791[0-9]{7,8}\\s" + "0[0-9]{9,11}\\s" + "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.(((0?[13578]|1[02])\\.(0?" + "[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" + "[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" + "\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\\s" + "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.(" + "[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" + "[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" + "\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])"; /*for(int i=1;i<x.length();i++){ if(x.charAt(0)=='0'&&x.length()>1&&x.charAt(1)!='.') return false; }*/ if(x.matches(w)) return true; else return false; } } abstract class CommunicationRecord{ String callingNumber; String answerNumber; public String getCallingNumber(){ return callingNumber; } public String getAnswerNumber() { return answerNumber; } public void setAnswerNumber(String answerNumber) { this.answerNumber = answerNumber; } public void setCallingNumber(String callingNumber) { this.callingNumber = callingNumber; } } class CallRecord extends CommunicationRecord{ private Date startTime=new Date(); private Date endTime=new Date(); private String callingAddressAreaCode; private String answerAddressAreaCode; public Date getStartTime() { return startTime; } public void setStartTime(Date startTime) { this.startTime = startTime; } public Date getEndTime() { return endTime; } public void setEndTime(Date endTime) { this.endTime = endTime; } public String getCallingAddressAreaCode() { return callingAddressAreaCode; } public void setCallingAddressAreaCode(String callingAddressAreaCode) { this.callingAddressAreaCode = callingAddressAreaCode; } public String getAnswerAddressAreaCode() { return answerAddressAreaCode; } public void setAnswerAddressAreaCode(String answerAddressAreaCode) { this.answerAddressAreaCode = answerAddressAreaCode; } public long timeToTime(){ return endTime.getTime()-startTime.getTime(); } } class MessageRecord extends CommunicationRecord{ private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } abstract class ChargeRule{ } abstract class CallChargeRule{ abstract double calCost(ArrayList<CallRecord> callRecords); } class LandPhoneInlandRule extends CallChargeRule{ @Override double calCost(ArrayList<CallRecord> callRecords) { long nm=1000*60; double money=0; long min = 0; for (CallRecord callRecord:callRecords){ //SimpleDateFormat sdt=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); long time=callRecord.getEndTime().getTime()-callRecord.getStartTime().getTime(); if(time%6000>0){ min=1+time/nm; money=money+min*0.1; } else{ min=time/nm; money=money+min*0.6; } } return money; } } class LandPhoneInCityRule extends CallChargeRule{ @Override double calCost(ArrayList<CallRecord> callRecords) { long nm=1000*60; double money=0; long min = 0; for (CallRecord callRecord:callRecords){ //SimpleDateFormat sdt=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); long time=callRecord.getEndTime().getTime()-callRecord.getStartTime().getTime(); if(time%6000>0){ min=1+time/nm; money=money+min*0.1; } else{ min=time/nm; money=money+min*0.1; } } return money; } } class LandPhoneInProvinceRule extends CallChargeRule{ @Override double calCost(ArrayList<CallRecord> callRecords) { long nm=1000*60; double money=0; long min = 0; for (CallRecord callRecord:callRecords){ //SimpleDateFormat sdt=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); long time=callRecord.getEndTime().getTime()-callRecord.getStartTime().getTime(); if(time%6000>0){ min=1+time/nm; money=money+min*0.3; } else{ min=time/nm; money=money+min*0.3; } } return money; } } class UserRecords{ private ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>(); private ArrayList<MessageRecord> sendMessageRecords =new ArrayList<MessageRecord>(); private ArrayList<MessageRecord> receiveMessageRecords =new ArrayList<MessageRecord>(); public void addCallingInCityRecords (CallRecord callRecord){ callingInCityRecords.add(callRecord); } public void addCallingInProvinceRecords (CallRecord callRecord){ callingInProvinceRecords.add(callRecord); } public void addCallingInLandRecords (CallRecord callRecord){ callingInLandRecords.add(callRecord); } public void addAnswerInCityRecords (CallRecord answerRecord){ answerInCityRecords.add(answerRecord); } public void addAnswerInProvinceRecords (CallRecord answerRecord){ answerInProvinceRecords.add(answerRecord); } public void addAnswerInLandRecords (CallRecord answerRecord){ answerInLandRecords.add(answerRecord); } public void addSendMessageRecords (MessageRecord sendMessageRecord){ sendMessageRecords.add(sendMessageRecord); } public void addReceiveMessageRecords (MessageRecord receiveMessageRecord){ receiveMessageRecords.add(receiveMessageRecord); } public ArrayList<CallRecord> getAnswerInCityRecords() { return answerInCityRecords; } public ArrayList<CallRecord> getAnswerInLandRecords() { return answerInLandRecords; } public ArrayList<CallRecord> getAnswerInProvinceRecords() { return answerInProvinceRecords; } public ArrayList<CallRecord> getCallingInCityRecords() { return callingInCityRecords; } public ArrayList<CallRecord> getCallingInLandRecords() { return callingInLandRecords; } public ArrayList<CallRecord> getCallingInProvinceRecords() { return callingInProvinceRecords; } public ArrayList<MessageRecord> getReceiveMessageRecords() { return receiveMessageRecords; } public ArrayList<MessageRecord> getSendMessageRecords() { return sendMessageRecords; } public void setAnswerInCityRecords(ArrayList<CallRecord> answerInCityRecords) { this.answerInCityRecords = answerInCityRecords; } public void setAnswerInLandRecords(ArrayList<CallRecord> answerInLandRecords) { this.answerInLandRecords = answerInLandRecords; } public void setAnswerInProvinceRecords(ArrayList<CallRecord> answerInProvinceRecords) { this.answerInProvinceRecords = answerInProvinceRecords; } public void setCallingInCityRecords(ArrayList<CallRecord> callingInCityRecords) { this.callingInCityRecords = callingInCityRecords; } public void setCallingInLandRecords(ArrayList<CallRecord> callingInLandRecords) { this.callingInLandRecords = callingInLandRecords; } public void setCallingInProvinceRecords(ArrayList<CallRecord> callingInProvinceRecords) { this.callingInProvinceRecords = callingInProvinceRecords; } public void setReceiveMessageRecords(ArrayList<MessageRecord> receiveMessageRecords) { this.receiveMessageRecords = receiveMessageRecords; } public void setSendMessageRecords(ArrayList<MessageRecord> sendMessageRecords) { this.sendMessageRecords = sendMessageRecords; } } abstract class ChargeMode{ private ArrayList<ChargeRule> chargeRules=new ArrayList<>(); public ArrayList<ChargeRule> getChargeRules() { return chargeRules; } public void setChargeRules(ArrayList<ChargeRule> chargeRules) { this.chargeRules = chargeRules; } abstract public double calCost(UserRecords userRecords); abstract public double getMonthlyRent(); } class LandlinePhoneCharging extends ChargeMode{ private double monthlyRent=20; @Override public double calCost(UserRecords userRecords) { LandPhoneInCityRule landPhoneInCityRule = new LandPhoneInCityRule(); LandPhoneInlandRule landPhoneInlandRule = new LandPhoneInlandRule(); LandPhoneInProvinceRule landPhoneInProvinceRule = new LandPhoneInProvinceRule(); return landPhoneInCityRule.calCost(userRecords.getCallingInCityRecords())+landPhoneInlandRule.calCost(userRecords.getCallingInLandRecords())+landPhoneInProvinceRule.calCost(userRecords.getCallingInProvinceRecords()); } @Override public double getMonthlyRent() { return monthlyRent; } } class User implements Comparable<User>{ private UserRecords userRecords=new UserRecords(); private double balance=100; private ChargeMode chargeMode; private String number; public User(String number){ this.number=number; } public UserRecords getUserRecords() { return userRecords; } public void setUserRecords(UserRecords userRecords) { this.userRecords = userRecords; } public double getBalance() { chargeMode=new LandlinePhoneCharging(); balance=balance-chargeMode.getMonthlyRent()-chargeMode.calCost(userRecords); return balance; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public ChargeMode getChargeMode() { return chargeMode; } public void setChargeMode(ChargeMode chargeMode) { this.chargeMode = chargeMode; } public double calBalance(){ chargeMode=new LandlinePhoneCharging(); return chargeMode.calCost(userRecords); } public int compareTo(User o){ return Integer.getInteger(this.getNumber().substring(10,12))-Integer.getInteger(o.getNumber().substring(10,12)); } @Override public String toString() { return number+" "+calBalance()+" "+getBalance(); } }
PTA-电信计费系列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元。
每条通讯、短信信息均单独计费后累加,不是将所有信息累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。
错误处理:
输入数据中出现的不符合格式要求的行一律忽略。
源代码如下:
import java.math.RoundingMode; import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String dateS; String dateE; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); ArrayList<User> users = new ArrayList<>(); String x = in.nextLine(); while (!x.equals("")) { if(x.charAt(0)!='t'){ if (x.charAt(0) == 'u') { String t = x.substring(2); String[] ak = t.split(" +"); /*if((ak[0].length()!=11&&ak[0].length()!=12)||!ak[0].substring(0,4).equals("0791")){ continue; }*/ if((Th(x)||Ph(x))&&!repeat(users,ak[0])){ if(Th(x)){ users.add(new User(ak[0]));} else { User user=new User(ak[0]); users.add(user); } } x = in.nextLine(); continue; } x = in.nextLine(); } else { if(PP(x)||PT(x)||TP(x)||TT(x)){ break;} else { x = in.nextLine(); } } //x = in.nextLine(); } while (!x.equals("end")){ Date dateSt = null; Date dateEd = null; if (!x.equals("")) { if(x.charAt(0)=='t'){ String t=x; int jud = 0; if(PP(x)||PT(x)||TP(x)||TT(x)){ x = x.substring(2); } else{ x=in.nextLine(); continue; } String[] ak = x.split(" +"); /*if(ak.length!=6){ x=in.nextLine(); continue; }*/ if(PP(t)){ dateS = ak[4] + " " + ak[5]; dateE = ak[6] + " " + ak[7]; /*if((ak[1].length()!=11&&ak[1].length()!=12)||(ak[0].length()!=11&&ak[0].length()!=12)){ x=in.nextLine(); continue; }*/ try { dateSt = simpleDateFormat.parse(dateS); } catch (ParseException e) { x=in.nextLine(); continue; } try { dateEd = simpleDateFormat.parse(dateE); } catch (ParseException e) { x=in.nextLine(); continue; } CallRecord callRecord = new CallRecord(); CallRecord anRecord=new CallRecord(); callRecord.setStartTime(dateSt); callRecord.setEndTime(dateEd); anRecord.setStartTime(dateSt); anRecord.setEndTime(dateEd); for (User user : users) { if(ak[1].equals("0791")){ jud++; if(ak[3].substring(0,3).equals("079")){ jud++; if(ak[3].equals("0791")){ jud++; } } } else { jud--; if(!ak[1].substring(0,3).equals("079")){ jud--; } } if (ak[0].equals(user.getNumber())) { switch (jud) { case 1: case -1: user.getUserRecords().addCallingInProvinceRecords(callRecord); //user. break; case 2: user.getUserRecords().addCallingCityToPrinceRecords(callRecord); break; case 3: user.getUserRecords().addCallingInCityRecords(callRecord); break; case -2:user.getUserRecords().addCallingInLandRecords(callRecord); break; } } jud=0; }//拨号通讯记录录入,如果下次要再匹配接听记录再加一个for循环即可 for (User user : users) { jud=0; if(!ak[3].substring(0,3).equals("079")){ jud++; } if (ak[2].equals(user.getNumber())&&jud>0) { user.getUserRecords().addCallingInProvinceRecords(anRecord); } }//拨号通讯记录录入,如果下次要再匹配接听记录再加一个for循环即可 //x = in.nextLine(); } else if(PT(t)){ dateS = ak[3] + " " + ak[4]; dateE = ak[5] + " " + ak[6]; /*if((ak[1].length()!=11&&ak[1].length()!=12)||(ak[0].length()!=11&&ak[0].length()!=12)){ x=in.nextLine(); continue; }*/ try { dateSt = simpleDateFormat.parse(dateS); } catch (ParseException e) { x=in.nextLine(); continue; } try { dateEd = simpleDateFormat.parse(dateE); } catch (ParseException e) { x=in.nextLine(); continue; } CallRecord callRecord = new CallRecord(); CallRecord anRecord=new CallRecord(); callRecord.setStartTime(dateSt); callRecord.setEndTime(dateEd); anRecord.setStartTime(dateSt); anRecord.setEndTime(dateEd); for (User user : users) { if(ak[1].equals("0791")){ jud++; if(ak[2].substring(0,3).equals("079")){ jud++; if(ak[2].substring(0,4).equals("0791")){ jud++; } } } else { jud--; if(!ak[1].substring(0,3).equals("079")){ jud--; } } if (ak[0].equals(user.getNumber())) { switch (jud) { case 1: case -1: user.getUserRecords().addCallingInProvinceRecords(callRecord); //user. break; case 2: user.getUserRecords().addCallingCityToPrinceRecords(callRecord); break; case 3: user.getUserRecords().addCallingInCityRecords(callRecord); break; case -2:user.getUserRecords().addCallingInLandRecords(callRecord); break; } } jud=0; }//拨号通讯记录录入,如果下次要再匹配接听记录再加一个for循环即可 } else if(TP(t)){ dateS = ak[3] + " " + ak[4]; dateE = ak[5] + " " + ak[6]; /*if((ak[1].length()!=11&&ak[1].length()!=12)||(ak[0].length()!=11&&ak[0].length()!=12)){ x=in.nextLine(); continue; }*/ try { dateSt = simpleDateFormat.parse(dateS); } catch (ParseException e) { x=in.nextLine(); continue; } try { dateEd = simpleDateFormat.parse(dateE); } catch (ParseException e) { x=in.nextLine(); continue; } CallRecord callRecord = new CallRecord(); CallRecord anRecord=new CallRecord(); callRecord.setStartTime(dateSt); callRecord.setEndTime(dateEd); anRecord.setStartTime(dateSt); anRecord.setEndTime(dateEd); for (User user : users) { if(ak[2].equals("0791")){ jud+=3; } else { jud+=2; if(!ak[2].substring(0,3).equals("079")){ jud--; } } if (ak[0].equals(user.getNumber())) { switch (jud) { case 1: user.getUserRecords().addCallingInLandRecords(callRecord); //user. break; case 2: user.getUserRecords().addCallingInProvinceRecords(callRecord); break; case 3: user.getUserRecords().addCallingInCityRecords(callRecord); break; } } jud=0; } for (User user : users) { jud=0; if(!ak[2].substring(0,3).equals("079")){ jud++; } if (ak[1].equals(user.getNumber())&&jud==1) { user.getUserRecords().addCallingInProvinceRecords(anRecord); } } } else if(TT(t)){ dateS = ak[2] + " " + ak[3]; dateE = ak[4] + " " + ak[5]; /*if((ak[1].length()!=11&&ak[1].length()!=12)||(ak[0].length()!=11&&ak[0].length()!=12)){ x=in.nextLine(); continue; }*/ try { dateSt = simpleDateFormat.parse(dateS); } catch (ParseException e) { x=in.nextLine(); continue; } try { dateEd = simpleDateFormat.parse(dateE); } catch (ParseException e) { x=in.nextLine(); continue; } CallRecord callRecord = new CallRecord(); CallRecord anRecord=new CallRecord(); callRecord.setStartTime(dateSt); callRecord.setEndTime(dateEd); anRecord.setStartTime(dateSt); anRecord.setEndTime(dateEd); if (judgePr(ak[0], ak[1])) { jud++; } if (judgeCity(ak[0], ak[1])) { jud++; } for (User user : users) { if (ak[0].equals(user.getNumber())) { switch (jud) { case 0: user.getUserRecords().addCallingInLandRecords(callRecord); //user. break; case 1: user.getUserRecords().addCallingInProvinceRecords(callRecord); break; case 2: user.getUserRecords().addCallingInCityRecords(callRecord); break; } } }//拨号通讯记录录入,如果下次要再匹配接听记录再加一个for循环即可 //x = in.nextLine(); } } //System.out.println(dateEd.getTime()-dateSt.getTime()); dateEd=null; dateSt=null; x = in.nextLine(); continue; } x=in.nextLine(); } //System.out.println(dateEd.getTime()-dateSt.getTime()); //users= String []all=new String[users.size()]; for (int i=0;i<users.size();i++){ all[i]=users.get(i).getNumber()+" "+tran(users.get(i).calBalance())+" "+tran(users.get(i).getBalance()); } Arrays.sort(all); if(users.size()!=0){ for (int i=0;i<users.size();i++){ System.out.println(all[i]); } } /*for (User user:users) { System.out.println(user.getUserRecords().getCallingInCityRecords()); }*/ } public static String format1(double x){ return String.format("%.1f",x); } public static boolean judgePr(String x,String y){ return x.substring(0, 3).equals(y.substring(0, 3)); } public static boolean judgeCity(String x,String y){ return x.substring(0, 4).equals(y.substring(0, 4)); } public static String tran(double x) { String str_d = String.valueOf(x); str_d = str_d.substring(str_d.indexOf(".") + 1); int len = str_d.length(); len = 1; String out = String.format("%." + len + "f", x); return out; } public static boolean Ks(String x) { String w; //w = "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d$"; w = "[t]-0791[0-9]{7,8}\\s" + "0[0-9]{9,11}\\s" + "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.(((0?[13578]|1[02])\\.(0?" + "[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" + "[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" + "\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\\s" + "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.(" + "[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" + "[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" + "\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])"; /*for(int i=1;i<x.length();i++){ if(x.charAt(0)=='0'&&x.length()>1&&x.charAt(1)!='.') return false; }*/ if(x.matches(w)) return true; else return false; } public static boolean repeat(ArrayList<User> users,String x){ int i=0; for (User user1:users){ if(user1.getNumber().equals(x)){ i++; } } return i!=0; } public static boolean Ph(String x) { String w; //w = "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d$"; w = "[u][-][1][3][0-9]{9}\\s[1]"; /*for(int i=1;i<x.length();i++){ if(x.charAt(0)=='0'&&x.length()>1&&x.charAt(1)!='.') return false; }*/ if(x.matches(w)) return true; else return false; } public static boolean Th(String x) { String w; //w = "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d$"; w = "[u][-][0][7][9][1][0-9]{7,8}\\s[0]"; /*for(int i=1;i<x.length();i++){ if(x.charAt(0)=='0'&&x.length()>1&&x.charAt(1)!='.') return false; }*/ if(x.matches(w)) return true; else return false; } public static boolean TT(String x) { String datePatten = "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.(((0?[13578]|1[02])\\.(" + "[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" + "[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" + "\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])"; String w; //w = "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d$"; w = "[t]-0791[0-9]{7,8}\\s" + "0[0-9]{9,11}\\s"+datePatten + "\\s" + datePatten; /*for(int i=1;i<x.length();i++){ if(x.charAt(0)=='0'&&x.length()>1&&x.charAt(1)!='.') return false; }*/ if(x.matches(w)) return true; else return false; } public static boolean TP(String x) { String datePatten = "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.(((0?[13578]|1[02])\\.(" + "[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" + "[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" + "\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])"; String w; //w = "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d$"; w = "[t]-0791[0-9]{7,8}\\s" +"1[0-9]{10}\\s"+"0[0-9]{2,3}\\s"+datePatten + "\\s" + datePatten; /*for(int i=1;i<x.length();i++){ if(x.charAt(0)=='0'&&x.length()>1&&x.charAt(1)!='.') return false; }*/ if(x.matches(w)) return true; else return false; } public static boolean PT(String x) { String datePatten = "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.(((0?[13578]|1[02])\\.(" + "[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" + "[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" + "\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])"; String w; //w = "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d$"; w = "[t]-1[0-9]{10}\\s" +"0[0-9]{2,3}\\s"+"0[0-9]{10,11}\\s"+datePatten + "\\s" + datePatten; /*for(int i=1;i<x.length();i++){ if(x.charAt(0)=='0'&&x.length()>1&&x.charAt(1)!='.') return false; }*/ if(x.matches(w)) return true; else return false; } public static boolean PP(String x) { String datePatten = "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.(((0?[13578]|1[02])\\.(" + "[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" + "[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" + "\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])"; String w; //w = "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d$"; w = "[t]-1[0-9]{10}\\s" +"0[0-9]{2,3}\\s"+"1[0-9]{10}\\s"+"0[0-9]{2,3}\\s"+datePatten + "\\s" + datePatten; /*for(int i=1;i<x.length();i++){ if(x.charAt(0)=='0'&&x.length()>1&&x.charAt(1)!='.') return false; }*/ if(x.matches(w)) return true; else return false; } } abstract class CommunicationRecord{ String callingNumber; String answerNumber; public String getCallingNumber(){ return callingNumber; } public String getAnswerNumber() { return answerNumber; } public void setAnswerNumber(String answerNumber) { this.answerNumber = answerNumber; } public void setCallingNumber(String callingNumber) { this.callingNumber = callingNumber; } } class CallRecord extends CommunicationRecord{ private Date startTime=new Date(); private Date endTime=new Date(); private String callingAddressAreaCode; private String answerAddressAreaCode; public Date getStartTime() { return startTime; } public void setStartTime(Date startTime) { this.startTime = startTime; } public Date getEndTime() { return endTime; } public void setEndTime(Date endTime) { this.endTime = endTime; } public String getCallingAddressAreaCode() { return callingAddressAreaCode; } public void setCallingAddressAreaCode(String callingAddressAreaCode) { this.callingAddressAreaCode = callingAddressAreaCode; } public String getAnswerAddressAreaCode() { return answerAddressAreaCode; } public void setAnswerAddressAreaCode(String answerAddressAreaCode) { this.answerAddressAreaCode = answerAddressAreaCode; } public long timeToTime(){ return endTime.getTime()-startTime.getTime(); } } class MessageRecord extends CommunicationRecord{ private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } abstract class ChargeRule{ } abstract class CallChargeRule{ abstract double calCost(ArrayList<CallRecord> callRecords); } class LandPhoneInlandRule extends CallChargeRule{ @Override double calCost(ArrayList<CallRecord> callRecords) { long nm=1000*60; double money=0; long min = 0; for (CallRecord callRecord:callRecords){ //SimpleDateFormat sdt=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); long time=callRecord.getEndTime().getTime()-callRecord.getStartTime().getTime(); if(time%6000>0){ min=1+time/nm; } else{ min=time/nm; } money=money+min*0.6; } return money; } } class LandPhoneInCityRule extends CallChargeRule{ @Override double calCost(ArrayList<CallRecord> callRecords) { long nm=1000*60; double money=0; long min = 0; for (CallRecord callRecord:callRecords){ //SimpleDateFormat sdt=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); long time=callRecord.getEndTime().getTime()-callRecord.getStartTime().getTime(); if(time%6000>0){ min=1+time/nm; } else{ min=time/nm; } money=money+min*0.1; } return money; } } class LandPhoneInProvinceRule extends CallChargeRule{ @Override double calCost(ArrayList<CallRecord> callRecords) { long nm=1000*60; double money=0; long min = 0; for (CallRecord callRecord:callRecords){ //SimpleDateFormat sdt=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); long time=callRecord.getEndTime().getTime()-callRecord.getStartTime().getTime(); if(time%6000>0){ min=1+time/nm; } else{ min=time/nm; } money=money+min*0.3; } return money; } } class PhoneInCityRule extends CallChargeRule{ @Override double calCost(ArrayList<CallRecord> callRecords) { long nm=1000*60; double money=0; long min = 0; for (CallRecord callRecord:callRecords){ //SimpleDateFormat sdt=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); long time=callRecord.getEndTime().getTime()-callRecord.getStartTime().getTime(); if(time%6000>0){ min=1+time/nm; } else{ min=time/nm; } money=money+min*0.1; } return money; } } class PhoneCityToPrinceRule extends CallChargeRule{ @Override double calCost(ArrayList<CallRecord> callRecords) { long nm=1000*60; double money=0; long min = 0; for (CallRecord callRecord:callRecords){ //SimpleDateFormat sdt=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); long time=callRecord.getEndTime().getTime()-callRecord.getStartTime().getTime(); if(time%6000>0){ min=1+time/nm; } else{ min=time/nm; } money=money+min*0.2; } return money; } } class PhoneCityToOutPrinceRule extends CallChargeRule{ @Override double calCost(ArrayList<CallRecord> callRecords) { long nm=1000*60; double money=0; long min = 0; for (CallRecord callRecord:callRecords){ //SimpleDateFormat sdt=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); long time=callRecord.getEndTime().getTime()-callRecord.getStartTime().getTime(); if(time%6000>0){ min=1+time/nm; } else{ min=time/nm; } money=money+min*0.3; } return money; } } class PhoneInPrinceRule extends CallChargeRule{ @Override double calCost(ArrayList<CallRecord> callRecords) { long nm=1000*60; double money=0; long min = 0; for (CallRecord callRecord:callRecords){ //SimpleDateFormat sdt=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); long time=callRecord.getEndTime().getTime()-callRecord.getStartTime().getTime(); if(time%6000>0){ min=1+time/nm; } else{ min=time/nm; } money=money+min*0.3; } return money; } } class PhoneOutPrinceRule extends CallChargeRule{ @Override double calCost(ArrayList<CallRecord> callRecords) { long nm=1000*60; double money=0; long min = 0; for (CallRecord callRecord:callRecords){ //SimpleDateFormat sdt=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); long time=callRecord.getEndTime().getTime()-callRecord.getStartTime().getTime(); if(time%6000>0){ min=1+time/nm; } else{ min=time/nm; } money=money+min*0.6; } return money; } } class AnPhoneOutPrinceRule extends CallChargeRule{ @Override double calCost(ArrayList<CallRecord> callRecords) { long nm=1000*60; double money=0; long min = 0; for (CallRecord callRecord:callRecords){ //SimpleDateFormat sdt=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); long time=callRecord.getEndTime().getTime()-callRecord.getStartTime().getTime(); if(time%6000>0){ min=1+time/nm; } else{ min=time/nm; } money=money+min*0.3; } return money; } } 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> callingCityToPrinceRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>(); private ArrayList<MessageRecord> sendMessageRecords =new ArrayList<MessageRecord>(); private ArrayList<MessageRecord> receiveMessageRecords =new ArrayList<MessageRecord>(); public void addCallingInCityRecords (CallRecord callRecord){ callingInCityRecords.add(callRecord); } public void addCallingCityToPrinceRecords (CallRecord callRecord){ callingCityToPrinceRecords.add(callRecord); } public void addCallingInProvinceRecords (CallRecord callRecord){ callingInProvinceRecords.add(callRecord); } public void addCallingInLandRecords (CallRecord callRecord){ callingInLandRecords.add(callRecord); } public void addAnswerInCityRecords (CallRecord answerRecord){ answerInCityRecords.add(answerRecord); } public void addAnswerInProvinceRecords (CallRecord answerRecord){ answerInProvinceRecords.add(answerRecord); } public void addAnswerInLandRecords (CallRecord answerRecord){ answerInLandRecords.add(answerRecord); } public void addSendMessageRecords (MessageRecord sendMessageRecord){ sendMessageRecords.add(sendMessageRecord); } public void addReceiveMessageRecords (MessageRecord receiveMessageRecord){ receiveMessageRecords.add(receiveMessageRecord); } public ArrayList<CallRecord> getAnswerInCityRecords() { return answerInCityRecords; } public ArrayList<CallRecord> getAnswerInLandRecords() { return answerInLandRecords; } public ArrayList<CallRecord> getAnswerInProvinceRecords() { return answerInProvinceRecords; } public ArrayList<CallRecord> getCallingInCityRecords() { return callingInCityRecords; } public ArrayList<CallRecord> getCallingInLandRecords() { return callingInLandRecords; } public ArrayList<CallRecord> getCallingInProvinceRecords() { return callingInProvinceRecords; } public ArrayList<MessageRecord> getReceiveMessageRecords() { return receiveMessageRecords; } public ArrayList<MessageRecord> getSendMessageRecords() { return sendMessageRecords; } public void setAnswerInCityRecords(ArrayList<CallRecord> answerInCityRecords) { this.answerInCityRecords = answerInCityRecords; } public void setAnswerInLandRecords(ArrayList<CallRecord> answerInLandRecords) { this.answerInLandRecords = answerInLandRecords; } public void setAnswerInProvinceRecords(ArrayList<CallRecord> answerInProvinceRecords) { this.answerInProvinceRecords = answerInProvinceRecords; } public void setCallingInCityRecords(ArrayList<CallRecord> callingInCityRecords) { this.callingInCityRecords = callingInCityRecords; } public void setCallingInLandRecords(ArrayList<CallRecord> callingInLandRecords) { this.callingInLandRecords = callingInLandRecords; } public void setCallingInProvinceRecords(ArrayList<CallRecord> callingInProvinceRecords) { this.callingInProvinceRecords = callingInProvinceRecords; } public void setReceiveMessageRecords(ArrayList<MessageRecord> receiveMessageRecords) { this.receiveMessageRecords = receiveMessageRecords; } public void setSendMessageRecords(ArrayList<MessageRecord> sendMessageRecords) { this.sendMessageRecords = sendMessageRecords; } public ArrayList<CallRecord> getCallingCityToPrinceRecords() { return callingCityToPrinceRecords; } public void setCallingCityToPrinceRecords(ArrayList<CallRecord> callingCityToPrinceRecords) { this.callingCityToPrinceRecords = callingCityToPrinceRecords; } } abstract class ChargeMode{ private ArrayList<ChargeRule> chargeRules=new ArrayList<>(); public ArrayList<ChargeRule> getChargeRules() { return chargeRules; } public void setChargeRules(ArrayList<ChargeRule> chargeRules) { this.chargeRules = chargeRules; } abstract public double calCost(UserRecords userRecords); abstract public double getMonthlyRent(); } class LandlinePhoneCharging extends ChargeMode{ private double monthlyRent=20; @Override public double calCost(UserRecords userRecords) { LandPhoneInCityRule landPhoneInCityRule = new LandPhoneInCityRule(); LandPhoneInlandRule landPhoneInlandRule = new LandPhoneInlandRule(); LandPhoneInProvinceRule landPhoneInProvinceRule = new LandPhoneInProvinceRule(); PhoneCityToPrinceRule phoneCityToPrinceRule=new PhoneCityToPrinceRule(); return landPhoneInCityRule.calCost(userRecords.getCallingInCityRecords())+landPhoneInlandRule.calCost(userRecords.getCallingInLandRecords())+landPhoneInProvinceRule.calCost(userRecords.getCallingInProvinceRecords())+phoneCityToPrinceRule.calCost(userRecords.getCallingCityToPrinceRecords()); } @Override public double getMonthlyRent() { return monthlyRent; } public void setMonthlyRent(double monthlyRent) { this.monthlyRent = monthlyRent; } } class User implements Comparable<User>{ private UserRecords userRecords=new UserRecords(); private double balance=100; private ChargeMode chargeMode; private String number; public User(String number){ this.number=number; } public UserRecords getUserRecords() { return userRecords; } public void setUserRecords(UserRecords userRecords) { this.userRecords = userRecords; } public double getBalance() { chargeMode=new LandlinePhoneCharging(); if(number.substring(0,4).equals("0791")){ balance=balance-chargeMode.getMonthlyRent()-chargeMode.calCost(userRecords);} else { balance=balance-chargeMode.getMonthlyRent()-chargeMode.calCost(userRecords)+5; } return balance; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public ChargeMode getChargeMode() { return chargeMode; } public void setChargeMode(ChargeMode chargeMode) { this.chargeMode = chargeMode; } public double calBalance(){ chargeMode=new LandlinePhoneCharging(); return chargeMode.calCost(userRecords); } public int compareTo(User o){ return Integer.getInteger(this.getNumber().substring(10,12))-Integer.getInteger(o.getNumber().substring(10,12)); } @Override public String toString() { return number+" "+calBalance()+" "+getBalance(); } }
PTA-电信计费系列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。
本题只考虑短信计费,不考虑通信费用以及月租费。
import java.math.RoundingMode; import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String dateS; String dateE; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); ArrayList<User> users = new ArrayList<>(); String x = in.nextLine(); while (!MessagePh(x)) { if(x.charAt(0)!='m'){ if (x.charAt(0) == 'u') { String t = x.substring(2); String[] ak = t.split(" +"); if((Ph(x))&&!repeat(users,ak[0])){ users.add(new User(ak[0])); } x = in.nextLine(); continue; } x = in.nextLine(); } } while (!x.equals("end")){ if (!x.equals("")) { if(x.charAt(0)=='m'){ if(MessagePh(x)){ for (User user:users){ String[] ak=x.split(" +"); String s=ak[0].substring(2); if(user.getNumber().equals(s)){ int num=ak[0].length()+ak[1].length()+2; String message = x.substring(num); int mes=0; mes=message.length()/10; if(message.length()%10>0){ mes+=1; } user.setMessageNum(user.getMessageNum()+mes); } }} } x = in.nextLine(); continue; } x=in.nextLine(); } String []all=new String[users.size()]; for (int i=0;i<users.size();i++){ all[i]=users.get(i).getNumber()+" "+tran(users.get(i).calBalance())+" "+tran(users.get(i).getBalance()); } Arrays.sort(all); if(users.size()!=0){ for (int i=0;i<users.size();i++){ System.out.println(all[i]); } } } public static String format1(double x){ return String.format("%.1f",x); } public static boolean judgePr(String x,String y){ return x.substring(0, 3).equals(y.substring(0, 3)); } public static boolean judgeCity(String x,String y){ return x.substring(0, 4).equals(y.substring(0, 4)); } public static String tran(double x) { String str_d = String.valueOf(x); str_d = str_d.substring(str_d.indexOf(".") + 1); int len = str_d.length(); len = 1; String out = String.format("%." + len + "f", x); return out; } public static boolean repeat(ArrayList<User> users,String x){ int i=0; for (User user1:users){ if(user1.getNumber().equals(x)){ i++; } } return i!=0; } public static boolean Ph(String x) { String w; w = "[u][-][1][0-9][0-9]{9}\\s[3]"; if(x.matches(w)) return true; else return false; } public static boolean MessagePh(String x){ String message="[a-zA-Z0-9.,\\s]*$"; String w= "^[m]-1[0-9]{10}\\s1[0-9]{10}\\s"+message; if(x.matches(w)) return true; else return false; } } abstract class CommunicationRecord{ String callingNumber; String answerNumber; public String getCallingNumber(){ return callingNumber; } public String getAnswerNumber() { return answerNumber; } public void setAnswerNumber(String answerNumber) { this.answerNumber = answerNumber; } public void setCallingNumber(String callingNumber) { this.callingNumber = callingNumber; } } class CallRecord extends CommunicationRecord{ private Date startTime=new Date(); private Date endTime=new Date(); private String callingAddressAreaCode; private String answerAddressAreaCode; public Date getStartTime() { return startTime; } public void setStartTime(Date startTime) { this.startTime = startTime; } public Date getEndTime() { return endTime; } public void setEndTime(Date endTime) { this.endTime = endTime; } public String getCallingAddressAreaCode() { return callingAddressAreaCode; } public void setCallingAddressAreaCode(String callingAddressAreaCode) { this.callingAddressAreaCode = callingAddressAreaCode; } public String getAnswerAddressAreaCode() { return answerAddressAreaCode; } public void setAnswerAddressAreaCode(String answerAddressAreaCode) { this.answerAddressAreaCode = answerAddressAreaCode; } public long timeToTime(){ return endTime.getTime()-startTime.getTime(); } } class MessageRecord extends CommunicationRecord{ private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } abstract class ChargeRule{ } abstract class CallChargeRule{ abstract double calCost(ArrayList<CallRecord> callRecords); } 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> callingCityToPrinceRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>(); private ArrayList<MessageRecord> sendMessageRecords =new ArrayList<MessageRecord>(); private ArrayList<MessageRecord> receiveMessageRecords =new ArrayList<MessageRecord>(); public void addCallingInCityRecords (CallRecord callRecord){ callingInCityRecords.add(callRecord); } public void addCallingCityToPrinceRecords (CallRecord callRecord){ callingCityToPrinceRecords.add(callRecord); } public void addCallingInProvinceRecords (CallRecord callRecord){ callingInProvinceRecords.add(callRecord); } public void addCallingInLandRecords (CallRecord callRecord){ callingInLandRecords.add(callRecord); } public void addAnswerInCityRecords (CallRecord answerRecord){ answerInCityRecords.add(answerRecord); } public void addAnswerInProvinceRecords (CallRecord answerRecord){ answerInProvinceRecords.add(answerRecord); } public void addAnswerInLandRecords (CallRecord answerRecord){ answerInLandRecords.add(answerRecord); } public void addSendMessageRecords (MessageRecord sendMessageRecord){ sendMessageRecords.add(sendMessageRecord); } public void addReceiveMessageRecords (MessageRecord receiveMessageRecord){ receiveMessageRecords.add(receiveMessageRecord); } public ArrayList<CallRecord> getAnswerInCityRecords() { return answerInCityRecords; } public ArrayList<CallRecord> getAnswerInLandRecords() { return answerInLandRecords; } public ArrayList<CallRecord> getAnswerInProvinceRecords() { return answerInProvinceRecords; } public ArrayList<CallRecord> getCallingInCityRecords() { return callingInCityRecords; } public ArrayList<CallRecord> getCallingInLandRecords() { return callingInLandRecords; } public ArrayList<CallRecord> getCallingInProvinceRecords() { return callingInProvinceRecords; } public ArrayList<MessageRecord> getReceiveMessageRecords() { return receiveMessageRecords; } public ArrayList<MessageRecord> getSendMessageRecords() { return sendMessageRecords; } public void setAnswerInCityRecords(ArrayList<CallRecord> answerInCityRecords) { this.answerInCityRecords = answerInCityRecords; } public void setAnswerInLandRecords(ArrayList<CallRecord> answerInLandRecords) { this.answerInLandRecords = answerInLandRecords; } public void setAnswerInProvinceRecords(ArrayList<CallRecord> answerInProvinceRecords) { this.answerInProvinceRecords = answerInProvinceRecords; } public void setCallingInCityRecords(ArrayList<CallRecord> callingInCityRecords) { this.callingInCityRecords = callingInCityRecords; } public void setCallingInLandRecords(ArrayList<CallRecord> callingInLandRecords) { this.callingInLandRecords = callingInLandRecords; } public void setCallingInProvinceRecords(ArrayList<CallRecord> callingInProvinceRecords) { this.callingInProvinceRecords = callingInProvinceRecords; } public void setReceiveMessageRecords(ArrayList<MessageRecord> receiveMessageRecords) { this.receiveMessageRecords = receiveMessageRecords; } public void setSendMessageRecords(ArrayList<MessageRecord> sendMessageRecords) { this.sendMessageRecords = sendMessageRecords; } public ArrayList<CallRecord> getCallingCityToPrinceRecords() { return callingCityToPrinceRecords; } public void setCallingCityToPrinceRecords(ArrayList<CallRecord> callingCityToPrinceRecords) { this.callingCityToPrinceRecords = callingCityToPrinceRecords; } } abstract class ChargeMode{ private ArrayList<ChargeRule> chargeRules=new ArrayList<>(); public ArrayList<ChargeRule> getChargeRules() { return chargeRules; } public void setChargeRules(ArrayList<ChargeRule> chargeRules) { this.chargeRules = chargeRules; } abstract public double calCost(UserRecords userRecords); abstract public double getMonthlyRent(); } class LandlinePhoneCharging extends ChargeMode{ private double monthlyRent=20; @Override public double calCost(UserRecords userRecords) { return 0; } @Override public double getMonthlyRent() { return monthlyRent; } public void setMonthlyRent(double monthlyRent) { this.monthlyRent = monthlyRent; } } class User implements Comparable<User>{ private double messageFee=0; private double messageNum=0; private UserRecords userRecords=new UserRecords(); private double balance=100; private ChargeMode chargeMode; private String number; public User(String number){ this.number=number; } public UserRecords getUserRecords() { return userRecords; } public void setUserRecords(UserRecords userRecords) { this.userRecords = userRecords; } public double getBalance() { chargeMode=new LandlinePhoneCharging(); balance=balance-calBalance(); return balance; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public ChargeMode getChargeMode() { return chargeMode; } public void setChargeMode(ChargeMode chargeMode) { this.chargeMode = chargeMode; } public double calBalance(){ return getMessageFee(); } public int compareTo(User o){ return Integer.getInteger(this.getNumber().substring(10,12))-Integer.getInteger(o.getNumber().substring(10,12)); } @Override public String toString() { return number+" "+calBalance()+" "+getBalance(); } public double getMessageNum() { return messageNum; } public void setMessageNum(double messageNum) { this.messageNum = messageNum; } public double getMessageFee() { if(messageNum<=3){ messageFee=messageNum*0.1; } else if(messageNum>3&&messageNum<=5){ messageFee=0.3+(messageNum-3)*0.2; } else if(messageNum>5){ messageFee=0.7+(messageNum-5)*0.3; } return messageFee; } public void setMessageFee(double messageFee) { this.messageFee = messageFee; } }
3.踩坑心得
用户重复问题报错容易忽略。
遇到这类问题要用正则表达式解决。
输入错误的判定与进入下一个信息录入函数的顺序不能乱。
4.改进建议
这类题型的类建立一定要完善,从第一次题型就应该建立好完整的架构,建立好架构下次做进阶题型更加便利。
5.总结
正则表达式很重要。