短信计费PTA题目集8-1
本题是整个短信计费实现的基础,并且这道题目实现的要求如下实现一个简单的电信计费程序:
假设南昌市电信分公司针对市内座机用户采用的计费方式:
月租20元,接电话免费,市内拨打电话0.1元/分钟,省内长途0.3元/分钟,国内长途拨打0.6元/分钟。不足一分钟按一分钟计。
南昌市的区号:0791,江西省内各地市区号包括:0790~0799以及0701。
跟据题目的意思这只是整个短信计费的基础,根据之前的经验和教训,我们要合理搭建好目前的框架和设计要求吗,以实现他的复用性和延展性,这样在后续的题目要求添加新的的功能时我们可以更加的方便便捷,在设计这道题目时由于老师提前给了整个的代码的类与类之间的设计结构以及类与类之间的设计关系和调用关系,这样也就方便了我们,所以根据这道题目,我想要主要来分析一下老师在设计这道题目的在整个的设计思路
根据老师所给的类图,由于他并不是将所有的类都集中在一个图中,所以我想要对每张类图都做说明,根据上面的类图,我们发现,我们将程序的主要区分为使用者,然而根据分析,我们需要思考,电话使用者需要具备的属性和行为是什么,根据日常的使用,我们知道使用者所具备的属性一定要有电话号码这个属性,并且我们可想而知,后续的对用户的操作,我们都是根据电话号码来判断操作的对象,并且,我们也可以知道对于电话号码,我们分为座机号码和移动的手机号码,并且后续的题目我们可以发现,我们要添加短信的计费方式,所以可想而知,我们便能够知道前期的代码设计的重要性,我们需要知道提高代码的可复用性,和延展性,以便于对后续的程序的功能的增加,并且我们也可以知道用户应该也要话费余额以及用户的使用记录,分别是打电话和发信息的使用记录
接下来对于这道题目,我们可以进一步的实现,例如对于userrecord,我们可以思考他的作用以及属性行为,于是我们可以知道每次都用户无论是接电话和打电话都要在对应的号码进行操作,所以根据这个,我们可以知道,userrecord分别分为callrecord和answerrecord,所以进一步我们可以知道callrecord应该具备的属性是,callnumber和answernumber并且它也应该要有通话时间,而不同的通话时间也对应不同的计费方式,并且第一道题目是是也要考虑通话区域,考虑是不是跨省跨市等等,并且不同的的地方,也对应不同的计费方式,由于这道题目是整个的短信计费的基础所以,对于不同区域的计费方式,这道题目的要求并不是很严格,而且也没有对不同区域的漫游计费做要求
对于后面的代码我们则要考虑类与类之间的继承关系,这样能够减少代码的重复性,并且要能够了解代码的子类与父类之间的具体的关系,这样才能更好的了解继承,对于不同的record我们需要将它非几种,分别是callrecord和messagerecord,很明显二者但是record,所以都是有一些共同的属性,所以便建了应该communcationrecord的抽象类,这样就更方便进行减少代码的重复性,让整体的代码都更加的简洁.
根据题目的要求我们可以知道不同的区域对应着不同的计费方式,所以我们在对通话记录进行保存时存储时就要进行区分将对应的区域保存到对应的计费规则,这样就便于后续对结果进行计算,以及在后面如果是要加入不同的计费规则时就可以直接进行添加了例如后续的,手机通话的计费规则,省内省外的漫游计费,这样就可以直接继承chargerule,这样降低的耦合性十分方便后续功能的添加和改进,使得代码更加完善
import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input=new Scanner(System.in); ArrayList<User> userArrayList=new ArrayList<>(); String number="0"; String regStr = "[u]-0791[0-9]{7,8}\\s[0]"; String regStr2 = "[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])"; WhatTypeofCall whatTypeofCall=new WhatTypeofCall(); //InputData inputData=new InputData(); while (true) { number = input.nextLine(); if (number.equals("end")) break; number = number.replace("\n", ""); if(!number.matches(regStr)&&!number.matches(regStr2)) continue; User user = new User(); // inputData.setString(number); InputData inputData = new InputData(number); if (!inputData.checkData()) continue; if (inputData.getChoice().equals("u")) { inputData.split1(); user.setNumber(inputData.getCallNumber1()); userArrayList.add(user); } if (inputData.getChoice().equals("t")) { if(inputData.checksplit()) continue; inputData.split2(); // if(inputData.checksplit()) continue; // if(inputData.getCallNumber2().equals(inputData.getAnswerNumber())) continue; for (User value : userArrayList) { // if (inputData.getCallNumber2().equals(value.getNumber())) { CallRecord callRecord = new CallRecord(); if (whatTypeofCall.whatTypeofcall(inputData.getCallnumberCode(), inputData.getAnswernumberCode()).equals("InCity")) { callRecord.setAnswerAddressAreaCode(inputData.getAnswernumberCode()); callRecord.setCallingAddressAreaCode(inputData.getCallnumberCode()); callRecord.setCallingnumber(inputData.getCallNumber2()); callRecord.setAnswernumber(inputData.getAnswerNumber()); callRecord.setStarttime(inputData.getData1()); callRecord.setEndtime(inputData.getData2()); value.getUserRecords().addCallinginCityRecords(callRecord); value.getUserRecords().addAnswerinCityRecords(callRecord); } if (whatTypeofCall.whatTypeofcall(inputData.getCallnumberCode(), inputData.getAnswernumberCode()).equals("InPrivance")) { callRecord.setAnswerAddressAreaCode(inputData.getAnswernumberCode()); callRecord.setCallingAddressAreaCode(inputData.getCallnumberCode()); callRecord.setCallingnumber(inputData.getCallNumber2()); callRecord.setAnswernumber(inputData.getAnswerNumber()); callRecord.setStarttime(inputData.getData1()); callRecord.setEndtime(inputData.getData2()); value.getUserRecords().addCallinginPrivanceRecords(callRecord); value.getUserRecords().addAnswerinPrivanceRecords(callRecord); } if (whatTypeofCall.whatTypeofcall(inputData.getCallnumberCode(), inputData.getAnswernumberCode()).equals("InLand")) { callRecord.setAnswerAddressAreaCode(inputData.getAnswernumberCode()); callRecord.setCallingAddressAreaCode(inputData.getCallnumberCode()); callRecord.setCallingnumber(inputData.getCallNumber2()); callRecord.setAnswernumber(inputData.getAnswerNumber()); callRecord.setStarttime(inputData.getData1()); callRecord.setEndtime(inputData.getData2()); value.getUserRecords().addCallinginLandRecords(callRecord); value.getUserRecords().addAnswerinLandRecords(callRecord); } } } for (int i = 0; i < userArrayList.size(); i++) { for (int j = 0; j < userArrayList.size(); j++) { if (Double.parseDouble(userArrayList.get(i).getNumber()) < Double.parseDouble(userArrayList.get(j).getNumber())) { String s = userArrayList.get(i).getNumber(); userArrayList.get(i).setNumber(userArrayList.get(j).getNumber()); userArrayList.get(j).setNumber(s); ChargeMode chargeMode = new ChargeMode(); chargeMode = userArrayList.get(i).getChargeMode(); userArrayList.get(i).setChargeMode(userArrayList.get(j).getChargeMode()); userArrayList.get(j).setChargeMode(chargeMode); UserRecords userRecords; userRecords = userArrayList.get(i).getUserRecords(); userArrayList.get(i).setUserRecords(userArrayList.get(j).getUserRecords()); userArrayList.get(j).setUserRecords(userRecords); } } } } } for (int i = 0; i < userArrayList.size(); i++) { for (int j = i+1; j < userArrayList.size(); j++) { if(userArrayList.get(i).getNumber().equals(userArrayList.get(j).getNumber())){ userArrayList.remove(j); break; } } } // CheckData checkData=new CheckData(userArrayList); // checkData.getUserRecordsToCheck(); userArrayList=checkData.getUserArrayList(); for (int i=0;i<userArrayList.size();i++) { userArrayList.get(i).calCost(); } for(int i=0;i<userArrayList.size();i++){ double m=userArrayList.get(i).calCost(); System.out.printf("%s %.1f %.1f\n",userArrayList.get(i).getNumber(),(m-20),(100.0-m)); } } } abstract class CallChargeRule { public abstract double calCost(ArrayList<CallRecord> callRecords); } class CallRecord extends CommunicationRecord { private String starttime; private String endtime; private String callingAddressAreaCode; private String AnswerAddressAreaCode; public String getAnswerAddressAreaCode() { return AnswerAddressAreaCode; } @Override public void setAnswernumber(String answernumber) { super.setAnswernumber(answernumber); } @Override public void setCallingnumber(String callingnumber) { super.setCallingnumber(callingnumber); } @Override public String getAnswernumber() { return super.getAnswernumber(); } @Override public String getCallingnumber() { return super.getCallingnumber(); } public String getCallingAddressAreaCode() { return callingAddressAreaCode; } public String getEndtime() { return endtime; } public String getStarttime() { return starttime; } public void setAnswerAddressAreaCode(String answerAddressAreaCode) { AnswerAddressAreaCode = answerAddressAreaCode; } public void setCallingAddressAreaCode(String callingAddressAreaCode) { this.callingAddressAreaCode = callingAddressAreaCode; } public void setEndtime(String endtime) { this.endtime = endtime; } public void setStarttime(String starttime) { this.starttime = starttime; } } class ChargeMode extends LandLinePhoneCharge { private ArrayList<ChargeRule> chargeRules=new ArrayList<>(); public double calCost(UserRecords userRecords){ double sum=0; if(!userRecords.getCallinginPrivanceRecords().isEmpty()){ LandPhoneInPrivanceRule landPhoneInPrivanceRule=new LandPhoneInPrivanceRule(); sum=sum+landPhoneInPrivanceRule.calCost(userRecords.getCallinginPrivanceRecords()); } if(!userRecords.getCallinginCityRecords().isEmpty()){ LandPhoneInCityRule landPhoneInCityRule=new LandPhoneInCityRule(); sum=sum+ landPhoneInCityRule.calCost(userRecords.getCallinginCityRecords()); } if(!userRecords.getCallinginLandRecords().isEmpty()){ LandPhoneInLandRule landPhoneInLandRule=new LandPhoneInLandRule(); sum=sum+landPhoneInLandRule.calCost(userRecords.getCallinginLandRecords()); } return sum+getMonthlyRent(); } public ArrayList<ChargeRule> getChargeRules() { return chargeRules; } public void setChargeRules(ArrayList<ChargeRule> chargeRules) { this.chargeRules = chargeRules; } @Override public double getMonthlyRent() { return super.getMonthlyRent(); } } abstract class ChargeRule { } class CheckData { ArrayList<User> userArrayList=new ArrayList<>(); String s; public CheckData(ArrayList<User> userArrayList){ this.userArrayList=userArrayList; } public CheckData(String s){ this.s=s; } public void getUserRecordsToCheck(){ for(int i=0;i< userArrayList.size();i++){ if( ! checkdate(userArrayList.get(i).getUserRecords())){ userArrayList.remove(i); } } } public ArrayList<User> getUserArrayList() { return userArrayList; } public boolean checkdate(UserRecords userRecords){ SimpleDate simpleDate=new SimpleDate(); if(!userRecords.getCallinginPrivanceRecords().isEmpty()){ LandPhoneInPrivanceRule landPhoneInPrivanceRule=new LandPhoneInPrivanceRule(); for(int i=0;i<userRecords.getCallinginPrivanceRecords().size();i++){ if(simpleDate.simpledate(userRecords.getCallinginPrivanceRecords().get(i).getStarttime(),userRecords.getCallinginPrivanceRecords().get(i).getEndtime())==0){ return false; } } } if(!userRecords.getCallinginCityRecords().isEmpty()){ LandPhoneInCityRule landPhoneInCityRule=new LandPhoneInCityRule(); for (int i=0;i<userRecords.getCallinginCityRecords().size();i++){ if(simpleDate.simpledate(userRecords.getCallinginCityRecords().get(i).getStarttime(),userRecords.getCallinginCityRecords().get(i).getEndtime())==0){ return false; } } } if(!userRecords.getCallinginLandRecords().isEmpty()){ LandPhoneInLandRule landPhoneInLandRule=new LandPhoneInLandRule(); for(int i=0;i<userRecords.getCallinginLandRecords().size();i++){ if(simpleDate.simpledate(userRecords.getCallinginLandRecords().get(i).getStarttime(),userRecords.getCallinginLandRecords().get(i).getEndtime())==0){ return false; } } } return true; } } abstract class CommunicationRecord extends ChargeRule { String callingnumber; String answernumber; public String getAnswernumber() { return answernumber; } public String getCallingnumber() { return callingnumber; } public void setAnswernumber(String answernumber) { this.answernumber = answernumber; } public void setCallingnumber(String callingnumber) { this.callingnumber = callingnumber; } } class InputData { private String string; private String data1; private String data2; private String chargeChoice; private String number; private String callNumber1; private String callNumber2; private String CallnumberCode; private String AnswernumberCode; private String answerNumber; private String[] num=new String[10]; private String[] data=new String[10]; public InputData (String s){ string=s; } public InputData (){ } public boolean checksplit(){ String[] nu; nu=string.split("\\s"); //if(data[3]==null||data[4]!=null) return true; return nu.length>6; } public boolean checkData(){ if( string.length()<10) return false; return true; } public boolean checkdate(){ SimpleDate simpleDate=new SimpleDate(); if(simpleDate.simpledate(getData1(),getData2())==1){return false;} return true; } public String getAnswernumberCode() { return AnswernumberCode; } public String getCallnumberCode() { return CallnumberCode; } public String getAnswerNumber() { return answerNumber; } public String getCallNumber1() { return callNumber1; } public String getCallNumber2() { return callNumber2; } public String getData1() { return data1; } public String getData2() { return data2; } public String getChargeChoice() { return chargeChoice; } public void split1(){ num=string.split("\\s",2); callNumber1=num[0].substring(2); chargeChoice=num[1]; } public void split2(){ num=string.split("\\s",3); callNumber2=num[0].substring(2); CallnumberCode=callNumber2.substring(0,4); answerNumber=num[1]; AnswernumberCode=answerNumber.substring(0,4); data=num[2].split("\\s"); data1=data[0]+" "+data[1]; data2=data[2]+" "+data[3]; } public String getString() { return string; } public void setString(String string) { this.string = string; } public String getChoice(){ return string.substring(0,1); } } abstract class LandLinePhoneCharge { double monthlyRent=20; public abstract double calCost(UserRecords userRecords); public double getMonthlyRent() { return monthlyRent; } } class LandPhoneInCityRule extends CallChargeRule { @Override public double calCost(ArrayList<CallRecord> callRecords) { SimpleDate simpleDate=new SimpleDate(); double sum=0; long time=0; for(int i=0;i<callRecords.size();i++){ time=0; time=time+simpleDate.simpledate(callRecords.get(i).getStarttime(),callRecords.get(i).getEndtime()); sum=sum+0.1*time; } return sum; } } class LandPhoneInLandRule extends CallChargeRule{ @Override public double calCost(ArrayList<CallRecord> callRecords) { SimpleDate simpleDate=new SimpleDate(); double sum=0; long time=0; for(int i=0;i<callRecords.size();i++){ time=0; time=time+simpleDate.simpledate(callRecords.get(i).getStarttime(),callRecords.get(i).getEndtime()); sum=sum+0.6*time; } return sum; } } class LandPhoneInPrivanceRule extends CallChargeRule{ @Override public double calCost(ArrayList<CallRecord> callRecords) { SimpleDate simpleDate=new SimpleDate(); double sum=0; long time; for(int i=0;i<callRecords.size();i++){ time=0; time=time+simpleDate.simpledate(callRecords.get(i).getStarttime(),callRecords.get(i).getEndtime()); sum=sum+0.3*time; } return sum; } } class MessageRecord extends CommunicationRecord{ String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } class SimpleDate { public long simpledate(String date1,String date2){ try { SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); Date now = df.parse(date2);//当前时间 Date date = df.parse(date1);//过去 long l = (now.getTime() - date.getTime())/1000; if(l%60==0) { return( l/60);} return (l/60)+1; } catch (Exception e) { return 1; } } } class User { private UserRecords userRecords=new UserRecords(); private double balance=100; private ChargeMode chargeMode=new ChargeMode(); private String number; public double calBalance(){ return balance; } public double calCost(){ double m=chargeMode.calCost(userRecords); return m; } public ChargeMode getChargeMode() { return chargeMode; } public double getBalance() { return balance; } public String getNumber() { return number; } public UserRecords getUserRecords() { return userRecords; } public void setBalance(double balance) { this.balance = balance; } public void setChargeMode(ChargeMode chargeMode) { this.chargeMode = chargeMode; } public void setNumber(String number) { this.number = number; } public void setUserRecords(UserRecords userRecords) { this.userRecords = userRecords; } } class UserRecords { private ArrayList<CallRecord> callinginCityRecords=new ArrayList<>(); private ArrayList<CallRecord> callinginPrivanceRecords=new ArrayList<>(); private ArrayList<CallRecord> callinginLandRecords=new ArrayList<>(); private ArrayList<CallRecord> answerinCityRecords=new ArrayList<>(); private ArrayList<CallRecord> answerinPrivanceRecords=new ArrayList<>(); private ArrayList<CallRecord> answerinLandRecords=new ArrayList<>(); private ArrayList<MessageRecord> sendMessageRecords=new ArrayList<>(); private ArrayList<MessageRecord> receiveMessageRecords=new ArrayList<>(); public void addCallinginCityRecords(CallRecord callRecord){ callinginCityRecords.add(callRecord); } public void addCallinginPrivanceRecords(CallRecord callRecord){ callinginPrivanceRecords.add(callRecord); } public void addCallinginLandRecords(CallRecord callRecord){ callinginLandRecords.add(callRecord); } public void addAnswerinCityRecords(CallRecord answerRecord){ answerinCityRecords.add(answerRecord); } public void addAnswerinPrivanceRecords(CallRecord answerRecord){ answerinPrivanceRecords.add(answerRecord); } public void addAnswerinLandRecords(CallRecord answerRecord){ answerinLandRecords.add(answerRecord); } public void sendMessageRecords(MessageRecord sendMessageRecord){ sendMessageRecords.add(sendMessageRecord); } public void receiveMessageRecords(MessageRecord receiveMessageRecord){ receiveMessageRecords.add(receiveMessageRecord); } public ArrayList<CallRecord> getAnswerinCityRecords() { return answerinCityRecords; } public ArrayList<CallRecord> getAnswerinLandRecords() { return answerinLandRecords; } public ArrayList<CallRecord> getAnswerinPrivanceRecords() { return answerinPrivanceRecords; } public ArrayList<CallRecord> getCallinginCityRecords() { return callinginCityRecords; } public ArrayList<CallRecord> getCallinginLandRecords() { return callinginLandRecords; } public ArrayList<CallRecord> getCallinginPrivanceRecords() { return callinginPrivanceRecords; } 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 setAnswerinPrivanceRecords(ArrayList<CallRecord> answerinPrivanceRecords) { this.answerinPrivanceRecords = answerinPrivanceRecords; } public void setCallinginCityRecords(ArrayList<CallRecord> callinginCityRecords) { this.callinginCityRecords = callinginCityRecords; } public void setCallinginLandRecords(ArrayList<CallRecord> callinginLandRecords) { this.callinginLandRecords = callinginLandRecords; } public void setCallinginPrivanceRecords(ArrayList<CallRecord> callinginPrivanceRecords) { this.callinginPrivanceRecords = callinginPrivanceRecords; } public void setReceiveMessageRecords(ArrayList<MessageRecord> receiveMessageRecords) { this.receiveMessageRecords = receiveMessageRecords; } public void setSendMessageRecords(ArrayList<MessageRecord> sendMessageRecords) { this.sendMessageRecords = sendMessageRecords; } } class WhatTypeofCall { public String whatTypeofcall(String s1,String s2){ if(s1.equals(s2)){ return "InCity"; }else { String s="([0][7][9][0-9]|[0][7][0][1])"; if(s2.matches(s)){ return "InPrivance"; } } return "InLand"; } }
短信计费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也要被计算省外接听漫游费:
这道题目是根据上一道题目进行的进一步的完善,添加的手机计费规则相较与之前的功能更加的完善和复杂,手机计费不仅要考虑打电话的计费规则,也要考虑接电话的计费规则,并且上一道题目只是针对座机的规则,所以在打电话的时候主要考虑座机打座机,而现在却添加了手机,所以目前的打电话的对象目前就有三种,还要针对不同身份的打电话,并且我们还应该考虑的是不同的通话时长
由题意,我们可以知道,对于新添加的功能,我们只需要添加一些新的类;来实现上面的功能来对相关的数据进行存储和计算,在进行计算的时后,我们要在存储的数据中进行查找我们目前所需要的数据,以此来实现功能,所以在进行数据的存储时我们就要格外注意,这样才能便于数据的存储在进行计算的时候我们要注意,那些在省外进行接电话的,这种则需要进行对接电话进行计费,但是如果时座机,就不要考虑接听电话的费用,不管是省内省外都是这样,但是如果手机的话,那么它就对应着不同计费规则,在进行对手机的接电话打电话计费时,我们要考虑是省内还是省外,而判断的依据是打电话的当时和接电话的当时所处的地方决定,而不是根据电话号码办理的地点决定
import java.util.ArrayList; import java.util.Scanner; import java.text.SimpleDateFormat; import java.util.Date; package pta11; import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); ArrayList<User> userArrayList = new ArrayList<>(); String number; RightFormat rightFormat = new RightFormat(); WhatTypeofCall whatTypeofCall = new WhatTypeofCall(); while (true) { number = input.nextLine(); if (number.equals("end")) break; number = number.replace("\n", ""); User user = new User(); InputData inputData = new InputData(number); if (inputData.getChoice().equals("u")) { if (inputData.getLoadChoice().equals("0")) { user.setNumber(inputData.getPhonenumber()); user.setChargeMode(new LandLinePhoneCharge()); userArrayList.add(user); user.setLoadChoice(0); } if (inputData.getLoadChoice().equals("1")) { user.setNumber(inputData.getPhonenumber()); user.setChargeMode(new MobilephoneCharge()); userArrayList.add(user); user.setLoadChoice(1); } if (inputData.getLoadChoice().equals("3")) { user.setNumber(inputData.getPhonenumber()); user.setChargeMode(new MessageCharge()); userArrayList.add(user); user.setLoadChoice(3); } } if (inputData.getChoice().equals("t")) { inputData.split_t(); for (User value : userArrayList) { if (inputData.getCallnumber().equals(value.getNumber())) { CallRecord callRecord = new CallRecord(); if (whatTypeofCall.whatTypeofcall(inputData.getCallnumberCode(), inputData.getAnswernumberCode()).equals("InCity")) { callRecord.setAnswerAddressAreaCode(inputData.getAnswernumberCode()); callRecord.setCallingAddressAreaCode(inputData.getCallnumberCode()); callRecord.setCallingnumber(inputData.getCallnumber()); callRecord.setAnswernumber(inputData.getAnswernumber()); callRecord.setStarttime(inputData.getStarttime()); callRecord.setEndtime(inputData.getEndtime()); value.getUserRecords().addCallinginCityRecords(callRecord); } if (whatTypeofCall.whatTypeofcall(inputData.getCallnumberCode(), inputData.getAnswernumberCode()).equals("InPrivance")) { callRecord.setAnswerAddressAreaCode(inputData.getAnswernumberCode()); callRecord.setCallingAddressAreaCode(inputData.getCallnumberCode()); callRecord.setCallingnumber(inputData.getCallnumber()); callRecord.setAnswernumber(inputData.getAnswernumber()); callRecord.setStarttime(inputData.getStarttime()); callRecord.setEndtime(inputData.getEndtime()); value.getUserRecords().addCallinginPrivanceRecords(callRecord); } if (whatTypeofCall.whatTypeofcall(inputData.getCallnumberCode(), inputData.getAnswernumberCode()).equals("InLand")) { callRecord.setAnswerAddressAreaCode(inputData.getAnswernumberCode()); callRecord.setCallingAddressAreaCode(inputData.getCallnumberCode()); callRecord.setCallingnumber(inputData.getCallnumber()); callRecord.setAnswernumber(inputData.getAnswernumber()); callRecord.setStarttime(inputData.getStarttime()); callRecord.setEndtime(inputData.getEndtime()); value.getUserRecords().addCallinginLandRecords(callRecord); for (User item : userArrayList) { if ((item.getLoadChoice() == 1) && item.getNumber().equals(inputData.getAnswernumber())) { item.getUserRecords().addAnswerinLandRecords(callRecord); break; } } } } else { if (whatTypeofCall.whatTypeofcall(inputData.getCallnumberCode(), inputData.getAnswernumberCode()).equals("InLand")) { String s = "[0][7][9][0-9]"; String ss = "0701"; CallRecord callRecord = new CallRecord(); callRecord.setAnswerAddressAreaCode(inputData.getAnswernumberCode()); callRecord.setCallingAddressAreaCode(inputData.getCallnumberCode()); callRecord.setCallingnumber(inputData.getCallnumber()); callRecord.setAnswernumber(inputData.getAnswernumber()); callRecord.setStarttime(inputData.getStarttime()); callRecord.setEndtime(inputData.getEndtime()); for (User item : userArrayList) { if (item.getLoadChoice() == 1 && item.getNumber().equals(inputData.getAnswernumber())) { item.getUserRecords().addAnswerinLandRecords(callRecord); break; } } } } } } if (inputData.getChoice().equals("m")) { inputData.split_m(); for (User value : userArrayList) { if (value.getNumber().equals(inputData.getSendMessagenumber())) { MessageRecord messageRecord = new MessageRecord(); messageRecord.setMessage(inputData.getMessage()); value.getUserRecords().addsendMessageRecords(messageRecord); } } } } for (int i = 0; i < userArrayList.size(); i++) { for (int j = 0; j < userArrayList.size(); j++) { if (Double.parseDouble(userArrayList.get(i).getNumber()) < Double.parseDouble(userArrayList.get(j).getNumber())) { String s = userArrayList.get(i).getNumber(); userArrayList.get(i).setNumber(userArrayList.get(j).getNumber()); userArrayList.get(j).setNumber(s); ChargeMode chargeMode; chargeMode = userArrayList.get(i).getChargeMode(); userArrayList.get(i).setChargeMode(userArrayList.get(j).getChargeMode()); userArrayList.get(j).setChargeMode(chargeMode); UserRecords userRecords; userRecords = userArrayList.get(i).getUserRecords(); userArrayList.get(i).setUserRecords(userArrayList.get(j).getUserRecords()); userArrayList.get(j).setUserRecords(userRecords); int c; c = userArrayList.get(i).getLoadChoice(); userArrayList.get(i).setLoadChoice(userArrayList.get(j).getLoadChoice()); userArrayList.get(j).setLoadChoice(c); } } } // for (int i = 0; i < userArrayList.size(); i++) { for (int j = i + 1; j < userArrayList.size(); j++) { if (userArrayList.get(i).getNumber().equals(userArrayList.get(j).getNumber())) { userArrayList.remove(j); break; } } } for (int i = 0; i < userArrayList.size(); i++) { userArrayList.get(i).calCost(); } for (int i = 0; i < userArrayList.size(); i++) { double m = userArrayList.get(i).calCost(); if (userArrayList.get(i).getLoadChoice() == 3) { System.out.printf("%s %.1f %.1f\n", userArrayList.get(i).getNumber(), userArrayList.get(i).calCost() - userArrayList.get(i).getChargeMode().getMonthlyRent(), (100.0 - m)); } } } } abstract class AnswerChargeRule extends ChargeRule{ public abstract double calCost(ArrayList<CallRecord> callRecords); } abstract class CallChargeRule extends ChargeRule { public abstract double calCost(ArrayList<CallRecord> callRecords); } class CallRecord extends CommunicationRecord { private String starttime; private String endtime; private String callingAddressAreaCode; private String AnswerAddressAreaCode; public String getAnswerAddressAreaCode() { return AnswerAddressAreaCode; } @Override public void setAnswernumber(String answernumber) { super.setAnswernumber(answernumber); } @Override public void setCallingnumber(String callingnumber) { super.setCallingnumber(callingnumber); } @Override public String getAnswernumber() { return super.getAnswernumber(); } @Override public String getCallingnumber() { return super.getCallingnumber(); } public String getCallingAddressAreaCode() { return callingAddressAreaCode; } public String getEndtime() { return endtime; } public String getStarttime() { return starttime; } public void setAnswerAddressAreaCode(String answerAddressAreaCode) { AnswerAddressAreaCode = answerAddressAreaCode; } public void setCallingAddressAreaCode(String callingAddressAreaCode) { this.callingAddressAreaCode = callingAddressAreaCode; } public void setEndtime(String endtime) { this.endtime = endtime; } public void setStarttime(String starttime) { this.starttime = starttime; } } abstract class ChargeMode { private ArrayList<ChargeRule> chargeRules=new ArrayList<>(); public void setChargeRules(ArrayList<ChargeRule> chargeRules) { this.chargeRules = chargeRules; } public ArrayList<ChargeRule> getChargeRules() { return chargeRules; } public abstract double calCost(UserRecords userRecords); public abstract double getMonthlyRent(); } abstract class ChargeRule { } abstract class CommunicationRecord extends ChargeRule { String callingnumber; String answernumber; public String getAnswernumber() { return answernumber; } public String getCallingnumber() { return callingnumber; } public void setAnswernumber(String answernumber) { this.answernumber = answernumber; } public void setCallingnumber(String callingnumber) { this.callingnumber = callingnumber; } } class InputData { private String number; private String Callnumber; private String Phonenumber; private String Answernumber; private String Starttime; private String Endtime; private String CallnumberCode; private String AnswernumberCode; private String Message; private String SendMessagenumber; private String ReceiveMessagenumber; private String []num=new String[10]; public InputData(String number) { this.number=number; } public void split_m(){ num= number.split("\\s"); SendMessagenumber=num[0].substring(2); ReceiveMessagenumber=num[1]; Message=num[2]; } public String getMessage() { return Message; } public String getReceiveMessagenumber() { return ReceiveMessagenumber; } public String getSendMessagenumber() { return SendMessagenumber; } public String getCallnumber() { return Callnumber; } public String getCallnumberCode() { return CallnumberCode; } public String getAnswernumber() { return Answernumber; } public String getAnswernumberCode() { return AnswernumberCode; } public String getChoice(){ return number.substring(0,1); } public String getLoadChoice(){ return number.substring(number.length()-1); } public String getPhonenumber(){ num=number.split("\\s",2); Phonenumber=num[0].substring(2); return Phonenumber; } public void split_t(){ num= number.split("\\s"); Endtime=num[num.length-2]+" "+num[num.length-1]; Starttime=num[num.length-4]+" "+num[num.length-3]; if(num.length==8){ Callnumber=num[0].substring(2); CallnumberCode=num[1]; Answernumber=num[2]; AnswernumberCode=num[3]; } if(num.length==6){ Callnumber=num[0].substring(2); CallnumberCode=num[0].substring(2,6); Answernumber=num[1]; AnswernumberCode=num[1].substring(0,4); } if(num.length==7){ if(num[1].length()<11){ Callnumber=num[0].substring(2); CallnumberCode=num[1]; Answernumber=num[2]; AnswernumberCode=num[2].substring(0,4); }else{ Callnumber=num[0].substring(2); CallnumberCode=num[0].substring(2,6); Answernumber=num[1]; AnswernumberCode=num[2]; } } //System.out.println(AnswernumberCode+" "+Answernumber+" "+CallnumberCode+" "+Callnumber); } public String getStarttime() { return Starttime; } public String getEndtime() { return Endtime; } } class LandLinePhoneCharge extends ChargeMode{ double monthlyRent=20; public double calCost(UserRecords userRecords){ double sum=0; if(!userRecords.getCallinginPrivanceRecords().isEmpty()){ LandPhoneInPrivanceRule landPhoneInPrivanceRule=new LandPhoneInPrivanceRule(); sum=sum+landPhoneInPrivanceRule.calCost(userRecords.getCallinginPrivanceRecords()); } if(!userRecords.getCallinginCityRecords().isEmpty()){ LandPhoneInCityRule landPhoneInCityRule=new LandPhoneInCityRule(); sum=sum+ landPhoneInCityRule.calCost(userRecords.getCallinginCityRecords()); } if(!userRecords.getCallinginLandRecords().isEmpty()){ LandPhoneInLandRule landPhoneInLandRule=new LandPhoneInLandRule(); sum=sum+landPhoneInLandRule.calCost(userRecords.getCallinginLandRecords()); } return sum+getMonthlyRent(); } public double getMonthlyRent() { return monthlyRent; } } class LandPhoneInCityRule extends CallChargeRule { public double calCost(ArrayList<CallRecord> callRecords) { SimpleDate simpleDate=new SimpleDate(); double sum=0; long time=0; for(int i=0;i<callRecords.size();i++){ time=0; time=time+simpleDate.simpledate(callRecords.get(i).getStarttime(),callRecords.get(i).getEndtime()); sum=sum+0.1*time; } return sum; } } class LandPhoneInLandRule extends CallChargeRule { public double calCost(ArrayList<CallRecord> callRecords) { SimpleDate simpleDate=new SimpleDate(); double sum=0; long time=0; for(int i=0;i<callRecords.size();i++){ time=0; time=time+simpleDate.simpledate(callRecords.get(i).getStarttime(),callRecords.get(i).getEndtime()); sum=sum+0.6*time; } return sum; } } class LandPhoneInPrivanceRule extends CallChargeRule { public double calCost(ArrayList<CallRecord> callRecords) { SimpleDate simpleDate=new SimpleDate(); double sum=0; long time; for(int i=0;i<callRecords.size();i++){ time=0; time=time+simpleDate.simpledate(callRecords.get(i).getStarttime(),callRecords.get(i).getEndtime()); sum=sum+0.3*time; } return sum; } } class MessageRecord extends CommunicationRecord { String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } class MobilephoneCharge extends ChargeMode{ double monthlyRent=15; public double calCost(UserRecords userRecords){ double sum=0; if(!userRecords.getCallinginPrivanceRecords().isEmpty()){ MobilePhoneInPrivenceCallCharge mobilePhoneInPrivenceCallCharge=new MobilePhoneInPrivenceCallCharge(); sum=sum+mobilePhoneInPrivenceCallCharge.calCost(userRecords.getCallinginPrivanceRecords()); } if(!userRecords.getCallinginCityRecords().isEmpty()){ MobllePhoneInCityCallCharge mobllePhoneInCityCallCharge=new MobllePhoneInCityCallCharge(); sum=sum+mobllePhoneInCityCallCharge.calCost(userRecords.getCallinginCityRecords()); } if(!userRecords.getCallinginLandRecords().isEmpty()){ MobilePhoneInLandCallCharge mobilePhoneInLandCallCharge=new MobilePhoneInLandCallCharge(); sum=sum+mobilePhoneInLandCallCharge.calCost(userRecords.getCallinginLandRecords()); } if(!userRecords.getAnswerinLandRecords().isEmpty()){ MobilePhoneInLandAnswerCharge mobilePhoneInLandAnswerCharge=new MobilePhoneInLandAnswerCharge(); sum=sum+mobilePhoneInLandAnswerCharge.calcost(userRecords.getAnswerinLandRecords()); } return sum+getMonthlyRent(); } public double getMonthlyRent() { return monthlyRent; } } class MobilePhoneInLandAnswerCharge { public double calcost(ArrayList<CallRecord> AnswerRecords){ SimpleDate simpleDate=new SimpleDate(); double sum=0; long time=0; for(int i=0;i<AnswerRecords.size();i++){ time=0; time=time+simpleDate.simpledate(AnswerRecords.get(i).getStarttime(),AnswerRecords.get(i).getEndtime()); sum=sum+0.3*time; } return sum; } } class MobilePhoneInLandCallCharge extends CallChargeRule{ @Override public double calCost(ArrayList<CallRecord> callRecords) { SimpleDate simpleDate=new SimpleDate(); double sum=0; long time=0; for(int i=0;i<callRecords.size();i++){ time=0; time=time+simpleDate.simpledate(callRecords.get(i).getStarttime(),callRecords.get(i).getEndtime()); if(callRecords.get(i).getCallingAddressAreaCode().equals("0791")){ sum=sum+0.3*time; }else { String s="[0][7][9][0-9]"; String ss="0701"; if(s.matches(callRecords.get(i).getCallingAddressAreaCode())||ss.equals(callRecords.get(i).getCallingAddressAreaCode())){ sum=sum+0.3*time; }else sum=sum+0.6*time; } } return sum; } } class MobilePhoneInPrivenceCallCharge extends CallChargeRule { @Override public double calCost(ArrayList<CallRecord> callRecords) { SimpleDate simpleDate=new SimpleDate(); double sum=0; long time=0; for(int i=0;i<callRecords.size();i++){ time=0; time=time+simpleDate.simpledate(callRecords.get(i).getStarttime(),callRecords.get(i).getEndtime()); if(callRecords.get(i).getCallingAddressAreaCode().equals("0791")){ sum=sum+0.2*time; }else { sum=sum+0.3*time; } } return sum; } } class MobllePhoneInCityCallCharge extends CallChargeRule{ @Override public double calCost(ArrayList<CallRecord> callRecords) { SimpleDate simpleDate=new SimpleDate(); double sum=0; long time=0; for(int i=0;i<callRecords.size();i++){ time=0; time=time+simpleDate.simpledate(callRecords.get(i).getStarttime(),callRecords.get(i).getEndtime()); if(callRecords.get(i).getCallingAddressAreaCode().equals("0791")){ sum=sum+0.1*time; }else { sum=sum+0.3*time; } } return sum; } } class RightFormat { private String date = "(((([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]))"; private String areaCode = "([0-9]{3,4})"; private String landPhoneNumber = "(0[0-9]{9,11})"; private String cellPhoneNumber = "(" + "(1[0-9]{10})" + "\\s" + areaCode + ")"; private String phoneNumber = "(" + landPhoneNumber + "|" + cellPhoneNumber + ")"; public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getAreaCode() { return areaCode; } public void setAreaCode(String areaCode) { this.areaCode = areaCode; } public String getLandPhoneNumber() { return landPhoneNumber; } public void setLandPhoneNumber(String landPhoneNumber) { this.landPhoneNumber = landPhoneNumber; } public String getCellPhoneNumber() { return cellPhoneNumber; } public void setCellPhoneNumber(String cellPhoneNumber) { this.cellPhoneNumber = cellPhoneNumber; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public boolean isImformation(String s) { if (s.matches("[t]-" + phoneNumber + "\\s" + phoneNumber + "\\s" + date + "\\s" + date)) return true; return false; } public boolean isAccountOpening(String s) { if (s.matches("[u]\\-" + "((0[0-9]{9,11})|([1][0-9]{10}))" + "\\s" + "[0-2]")) return true; return false; } } class SimpleDate { String date1; String date2; public SimpleDate(){ } public SimpleDate(String date1, String date2){ this.date1=date1; this.date2=date2; } public long simpledate(String date1,String date2){ try { SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); Date now = df.parse(date2);//当前时间 Date date = df.parse(date1);//过去 long l = (now.getTime() - date.getTime())/1000; if(l%60==0) { return( l/60);} return (l/60)+1; } catch (Exception e) { return 1; } } } class User { private UserRecords userRecords=new UserRecords(); private double balance=100; private ChargeMode chargeMode; private String number; private int loadChoice=1; public double calBalance(){ return balance-calCost(); } public double calCost(){ double m=chargeMode.calCost(userRecords); return m; } public int getLoadChoice() { return loadChoice; } public void setLoadChoice(int loadChoice) { this.loadChoice = loadChoice; } public ChargeMode getChargeMode() { return chargeMode; } public double getBalance() { return balance; } public String getNumber() { return number; } public UserRecords getUserRecords() { return userRecords; } public void setBalance(double balance) { this.balance = balance; } public void setChargeMode(ChargeMode chargeMode) { this.chargeMode = chargeMode; } public void setNumber(String number) { this.number = number; } public void setUserRecords(UserRecords userRecords) { this.userRecords = userRecords; } } class UserRecords { private ArrayList<CallRecord> callinginCityRecords=new ArrayList<>(); private ArrayList<CallRecord> callinginPrivanceRecords=new ArrayList<>(); private ArrayList<CallRecord> callinginLandRecords=new ArrayList<>(); private ArrayList<CallRecord> answerinCityRecords=new ArrayList<>(); private ArrayList<CallRecord> answerinPrivanceRecords=new ArrayList<>(); private ArrayList<CallRecord> answerinLandRecords=new ArrayList<>(); private ArrayList<MessageRecord> sendMessageRecords=new ArrayList<>(); private ArrayList<MessageRecord> receiveMessageRecords=new ArrayList<>(); private ArrayList<CallRecord> AllCallRecords=new ArrayList<>(); public void addCallinginCityRecords(CallRecord callRecord){ callinginCityRecords.add(callRecord); AllCallRecords.add(callRecord); } public void addsendMessageRecords(MessageRecord messageRecord){ sendMessageRecords.add(messageRecord); } public void addCallinginPrivanceRecords(CallRecord callRecord){ callinginPrivanceRecords.add(callRecord); AllCallRecords.add(callRecord); } public void addCallinginLandRecords(CallRecord callRecord){ callinginLandRecords.add(callRecord); AllCallRecords.add(callRecord); } public void addAnswerinCityRecords(CallRecord answerRecord){ answerinCityRecords.add(answerRecord); } public void addAnswerinPrivanceRecords(CallRecord answerRecord){ answerinPrivanceRecords.add(answerRecord); } public void addAnswerinLandRecords(CallRecord answerRecord){ answerinLandRecords.add(answerRecord); } public void sendMessageRecords(MessageRecord sendMessageRecord){ sendMessageRecords.add(sendMessageRecord); } public void receiveMessageRecords(MessageRecord receiveMessageRecord){ receiveMessageRecords.add(receiveMessageRecord); } public ArrayList<CallRecord> getAnswerinCityRecords() { return answerinCityRecords; } public ArrayList<CallRecord> getAnswerinLandRecords() { return answerinLandRecords; } public ArrayList<CallRecord> getAnswerinPrivanceRecords() { return answerinPrivanceRecords; } public ArrayList<CallRecord> getCallinginCityRecords() { return callinginCityRecords; } public ArrayList<CallRecord> getCallinginLandRecords() { return callinginLandRecords; } public ArrayList<CallRecord> getCallinginPrivanceRecords() { return callinginPrivanceRecords; } 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 setAnswerinPrivanceRecords(ArrayList<CallRecord> answerinPrivanceRecords) { this.answerinPrivanceRecords = answerinPrivanceRecords; } public void setCallinginCityRecords(ArrayList<CallRecord> callinginCityRecords) { this.callinginCityRecords = callinginCityRecords; } public void setCallinginLandRecords(ArrayList<CallRecord> callinginLandRecords) { this.callinginLandRecords = callinginLandRecords; } public void setCallinginPrivanceRecords(ArrayList<CallRecord> callinginPrivanceRecords) { this.callinginPrivanceRecords = callinginPrivanceRecords; } public void setReceiveMessageRecords(ArrayList<MessageRecord> receiveMessageRecords) { this.receiveMessageRecords = receiveMessageRecords; } public void setSendMessageRecords(ArrayList<MessageRecord> sendMessageRecords) { this.sendMessageRecords = sendMessageRecords; } } class WhatTypeofCall { public String whatTypeofcall(String s1,String s2){ // System.out.println(s1+" "+s2); String s="[0][7][9][0-9]"; String ss="0701"; if(s1.equals(s2)){ return "InCity"; }else { if((s1.matches(s)||s1.equals(ss))&&(s2.matches(s)||s2.equals(ss))){ return "InPrivance"; } } return "InLand"; } }
短信计费规则-添加短信计费
实现一个简单的电信计费程序,针对手机的短信采用如下计费方式:
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.
短信的计费规则相比较与前两个而言是更简单的,唯一不同的就是计费规则,短信的计费的方式是根据它的发送短信的条数,以及根据它的每条短信的发送的字符数,如果一条短信的的字符数大于十个,就要额外当做另外一条信息进行计算,整体的代码比较简单,并没有太多的要求所以
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); String s1="[u]\-[1][0-9][10]\\s[3]"; String s2="[m]\\-[1][0-9][10]\\s[1][0-9][10]\\s[a-z|A-Z|\\s\\,\\.]"; ArrayList<User> userArrayList = new ArrayList<>(); String number; WhatTypeofCall whatTypeofCall = new WhatTypeofCall(); while (true) { number = input.nextLine(); if (number.equals("end")) break; if(!s1.matches(number)&&!s2.matches(number)) continue; number = number.replace("\n", ""); User user = new User(); InputData inputData = new InputData(number); if (inputData.getChoice().equals("u")) { if(inputData.getPhonenumber().length()!=11) continue; if (inputData.getLoadChoice().equals("3")) { user.setNumber(inputData.getPhonenumber()); user.setChargeMode(new MessageCharge()); userArrayList.add(user); user.setLoadChoice(3); } } if (inputData.getChoice().equals("m")) { if(number.length()<20) continue; inputData.split_m(); for (User value : userArrayList) { if (value.getNumber().equals(inputData.getSendMessagenumber())) { MessageRecord messageRecord = new MessageRecord(); messageRecord.setMessage(inputData.getMessage()); value.getUserRecords().addsendMessageRecords(messageRecord); } } } } for (int i = 0; i < userArrayList.size(); i++) { for (int j = 0; j < userArrayList.size(); j++) { if (Double.parseDouble(userArrayList.get(i).getNumber()) < Double.parseDouble(userArrayList.get(j).getNumber())) { String s = userArrayList.get(i).getNumber(); userArrayList.get(i).setNumber(userArrayList.get(j).getNumber()); userArrayList.get(j).setNumber(s); ChargeMode chargeMode; chargeMode = userArrayList.get(i).getChargeMode(); userArrayList.get(i).setChargeMode(userArrayList.get(j).getChargeMode()); userArrayList.get(j).setChargeMode(chargeMode); UserRecords userRecords; userRecords = userArrayList.get(i).getUserRecords(); userArrayList.get(i).setUserRecords(userArrayList.get(j).getUserRecords()); userArrayList.get(j).setUserRecords(userRecords); int c; c = userArrayList.get(i).getLoadChoice(); userArrayList.get(i).setLoadChoice(userArrayList.get(j).getLoadChoice()); userArrayList.get(j).setLoadChoice(c); } } } for (int i = 0; i < userArrayList.size(); i++) { for (int j = i + 1; j < userArrayList.size(); j++) { if (userArrayList.get(i).getNumber().equals(userArrayList.get(j).getNumber())) { userArrayList.remove(j); break; } } } for (int i = 0; i < userArrayList.size(); i++) { userArrayList.get(i).calCost(); } for (int i = 0; i < userArrayList.size(); i++) { double m = userArrayList.get(i).calCost(); if (userArrayList.get(i).getLoadChoice() == 3) { System.out.printf("%s %.1f %.1f\n", userArrayList.get(i).getNumber(), userArrayList.get(i).calCost() - userArrayList.get(i).getChargeMode().getMonthlyRent(), (100.0 - m)); } } } } class CallRecord extends CommunicationRecord { private String starttime; private String endtime; private String callingAddressAreaCode; private String AnswerAddressAreaCode; public String getAnswerAddressAreaCode() { return AnswerAddressAreaCode; } @Override public void setAnswernumber(String answernumber) { super.setAnswernumber(answernumber); } @Override public void setCallingnumber(String callingnumber) { super.setCallingnumber(callingnumber); } @Override public String getAnswernumber() { return super.getAnswernumber(); } @Override public String getCallingnumber() { return super.getCallingnumber(); } public String getCallingAddressAreaCode() { return callingAddressAreaCode; } public String getEndtime() { return endtime; } public String getStarttime() { return starttime; } public void setAnswerAddressAreaCode(String answerAddressAreaCode) { AnswerAddressAreaCode = answerAddressAreaCode; } public void setCallingAddressAreaCode(String callingAddressAreaCode) { this.callingAddressAreaCode = callingAddressAreaCode; } public void setEndtime(String endtime) { this.endtime = endtime; } public void setStarttime(String starttime) { this.starttime = starttime; } } abstract class ChargeMode { private ArrayList<ChargeRule> chargeRules=new ArrayList<>(); public void setChargeRules(ArrayList<ChargeRule> chargeRules) { this.chargeRules = chargeRules; } public ArrayList<ChargeRule> getChargeRules() { return chargeRules; } public abstract double calCost(UserRecords userRecords); public abstract double getMonthlyRent(); } abstract class ChargeRule { } abstract class CommunicationRecord extends ChargeRule { String callingnumber; String answernumber; public String getAnswernumber() { return answernumber; } public String getCallingnumber() { return callingnumber; } public void setAnswernumber(String answernumber) { this.answernumber = answernumber; } public void setCallingnumber(String callingnumber) { this.callingnumber = callingnumber; } } class InputData { private String number; private String Callnumber; private String Phonenumber; private String Answernumber; private String Starttime; private String Endtime; private String CallnumberCode; private String AnswernumberCode; private String Message; private String SendMessagenumber; private String ReceiveMessagenumber; private String []num=new String[10]; public InputData(String number) { this.number=number; } public void split_m(){ num= number.split("\\s",3); SendMessagenumber=num[0].substring(2); ReceiveMessagenumber=num[1]; Message=num[2]; } public String getMessage() { return Message; } public String getReceiveMessagenumber() { return ReceiveMessagenumber; } public String getSendMessagenumber() { return SendMessagenumber; } public String getCallnumber() { return Callnumber; } public String getCallnumberCode() { return CallnumberCode; } public String getAnswernumber() { return Answernumber; } public String getAnswernumberCode() { return AnswernumberCode; } public String getChoice(){ return number.substring(0,1); } public String getLoadChoice(){ return number.substring(number.length()-1); } public String getPhonenumber(){ num=number.split("\\s",2); Phonenumber=num[0].substring(2); return Phonenumber; } public void split_t(){ num= number.split("\\s"); Endtime=num[num.length-2]+" "+num[num.length-1]; Starttime=num[num.length-4]+" "+num[num.length-3]; if(num.length==8){ Callnumber=num[0].substring(2); CallnumberCode=num[1]; Answernumber=num[2]; AnswernumberCode=num[3]; } if(num.length==6){ Callnumber=num[0].substring(2); CallnumberCode=num[0].substring(2,6); Answernumber=num[1]; AnswernumberCode=num[1].substring(0,4); } if(num.length==7){ if(num[1].length()<11){ Callnumber=num[0].substring(2); CallnumberCode=num[1]; Answernumber=num[2]; AnswernumberCode=num[2].substring(0,4); }else{ Callnumber=num[0].substring(2); CallnumberCode=num[0].substring(2,6); Answernumber=num[1]; AnswernumberCode=num[2]; } } //System.out.println(AnswernumberCode+" "+Answernumber+" "+CallnumberCode+" "+Callnumber); } public String getStarttime() { return Starttime; } public String getEndtime() { return Endtime; } } class MessageCharge extends ChargeMode{ @Override public double calCost(UserRecords userRecords) { double sum=0; int len; double sum1=0; if(!userRecords.getSendMessageRecords().isEmpty()){ for(int i=0;i<userRecords.getSendMessageRecords().size();i++){ if(userRecords.getSendMessageRecords().get(i).message.length()%10==0){ len=userRecords.getSendMessageRecords().get(i).message.length()/10; }else { len=userRecords.getSendMessageRecords().get(i).message.length()/10+1; } sum=sum+len; } } if(sum<=3) sum1=sum1+0.1*(sum); if(sum<=5&&sum>3) sum1=sum1+0.3+0.2*(sum-3); if(sum>5) sum1=sum1+0.7+0.3*(sum-5); return sum1; } @Override public double getMonthlyRent() { return 0; } } class MessageRecord extends CommunicationRecord { String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } class User { private UserRecords userRecords=new UserRecords(); private double balance=100; private ChargeMode chargeMode; private String number; private int loadChoice=1; public double calBalance(){ return balance-calCost(); } public double calCost(){ double m=chargeMode.calCost(userRecords); return m; } public int getLoadChoice() { return loadChoice; } public void setLoadChoice(int loadChoice) { this.loadChoice = loadChoice; } public ChargeMode getChargeMode() { return chargeMode; } public double getBalance() { return balance; } public String getNumber() { return number; } public UserRecords getUserRecords() { return userRecords; } public void setBalance(double balance) { this.balance = balance; } public void setChargeMode(ChargeMode chargeMode) { this.chargeMode = chargeMode; } public void setNumber(String number) { this.number = number; } public void setUserRecords(UserRecords userRecords) { this.userRecords = userRecords; } } class UserRecords { private ArrayList<MessageRecord> sendMessageRecords=new ArrayList<>(); private ArrayList<MessageRecord> receiveMessageRecords=new ArrayList<>(); public void addsendMessageRecords(MessageRecord messageRecord){ sendMessageRecords.add(messageRecord); } public ArrayList<MessageRecord> getSendMessageRecords() { return sendMessageRecords; } } class WhatTypeofCall { public String whatTypeofcall(String s1,String s2){ // System.out.println(s1+" "+s2); String s="[0][7][9][0-9]"; String ss="0701"; if(s1.equals(s2)){ return "InCity"; }else { if((s1.matches(s)||s1.equals(ss))&&(s2.matches(s)||s2.equals(ss))){ return "InPrivance"; } } return "InLand"; } }
踩坑心得
在短信计费1的那里我们要注意使用时间函数以及注意使用正则表达式来排除格式要求
在进行计算是其实并不考虑时间交叉的这种情况
在进行短信计费二时要注意重复的用户的删除,以及它可以在原来没有开户的手机号依然可以拨打电话