·前言:第10-16周的学习已然结束,我们或多或少的学到了不同的知识和技能,更加熟练的掌握如何运用Java来进行编写与运行,在其中虽然遇到了各类大大小小的问题,但通过不懈的努力最终都化解了、也学会了。这一阶段的题目是:电信计费系列,对此在对这类题目上对其进行知识点、题量、难度进行总结,并发表自己的观点和看法。
题目详细描述:
(7-1 电信计费系列1-座机计费): 实现一个简单的电信计费程序: 假设南昌市电信分公司针对市内座机用户采用的计费方式: 月租20元,接电话免费,市内拨打电话0.1元/分钟,省内长途0.3元/分钟,国内长途 拨打0.6元/分钟。不足一分钟按一分钟计。 假设本市的区号:0791,江西省内各地市区号包括:0790~0799以及0701。输入: 输入信息包括两种类型
1、逐行输入南昌市用户开户的信息,每行一个用户,含手机和座机用户 格式:u-号码 计费类型 (计费类型包括:0-座机 1-手机实时计费 2-手机A套餐) 例如:u-079186300001 0 座机号码由区号和电话号码拼接而成,电话号码包含7-8位数字。 本题只考虑计费类型0-座机计费,系列2、3会逐步增加计费类型。
2、逐行输入本月某些用户的通讯信息,通讯信息格式: 座机呼叫座机:t-主叫号码 接听号码 起始时间 结束时间 t-079186330022 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:11 以上四项内容之间以一个英文空格分隔, 时间必须符合"yyyy.MM.dd HH:mm:ss"格式。提示:使用SimpleDateFormat类。 以上两类信息,先输入所有开户信息,再输入所有通讯信息,最后一行以“end”结束。 注意: 本题非法输入只做格式非法的判断,不做内容是否合理的判断(时间除外,否则无法计 算),比如: 1、输入的所有通讯信息均认为是同一个月的通讯信息,不做日期是否在同一个月还是多 个月的判定,直接将通讯费用累加,因此月租只计算一次。 2、记录中如果同一电话号码的多条通话记录时间出现重合,这种情况也不做判断,直接 计算每条记录的费用并累加。
3、用户区号不为南昌市的区号也作为正常用户处理。 输出: 根据输入的详细通讯信息,计算所有已开户的用户的当月费用(精确到小数点后2位, 单位元)。假设每个用户初始余额是100元。 每条通讯信息单独计费后累加,不是将所有时间累计后统一计费。 格式:号码+英文空格符+总的话费+英文空格符+余额 每个用户一行,用户之间按号码字符从小到大排序。 错误处理: 输入数据中出现的不符合格式要求的行一律忽略。
(7-1 电信计费系列2-手机+座机计费): 实现南昌市电信分公司的计费程序,假设该公司针对手机和座机用户分别采取了两种计费方案,分别如下:
1、针对市内座机用户采用的计费方式(与电信计费系列1内容相同):
月租20元,接电话免费,市内拨打电话0.1元/分钟,省内长途0.3元/分钟,国内长途拨打0.6元/分钟。不足一分钟按一分钟计。
假设本市的区号:0791,江西省内各地市区号包括:0790~0799以及0701。
2、针对手机用户采用实时计费方式:
月租15元,市内省内接电话均免费,市内拨打市内电话0.1元/分钟,市内拨打省内电话0.2元/分钟,市内拨打省外电话0.3元/分钟,省内漫游打电话0.3元/分钟,省外漫游接听0.3元/分钟,省外漫游拨打0.6元/分钟;
注:被叫电话属于市内、省内还是国内由被叫电话的接听地点区号决定,比如以下案例中,南昌市手机用户13307912264在区号为020的广州接听了电话,主叫号码应被计算为拨打了一个省外长途,同时,手机用户13307912264也要被计算省外接听漫游费:
u-13307912264 1
t-079186330022 13307912264 020 2022.1.3 10:00:25 2022.1.3 10:05:11
(7-1 电信计费系列3-短信计费): 实现一个简单的电信计费程序,针对手机的短信采用如下计费方式:
1、接收短信免费,发送短信0.1元/条,超过3条0.2元/条,超过5条0.3元/条。
2、如果一次发送短信的字符数量超过10个,按每10个字符一条短信进行计算。
输入:
输入信息包括两种类型
1、逐行输入南昌市手机用户开户的信息,每行一个用户。
格式:u-号码 计费类型 (计费类型包括:0-座机 1-手机实时计费 2-手机A套餐 3-手机短信计费)
例如:u-13305862264 3
座机号码由区号和电话号码拼接而成,电话号码包含7-8位数字,区号最高位是0。
手机号码由11位数字构成,最高位是1。
本题只针对类型3-手机短信计费。
2、逐行输入本月某些用户的短信信息,短信的格式:
m-主叫号码,接收号码,短信内容 (短信内容只能由数字、字母、空格、英文逗号、英文句号组成)
m-18907910010 13305862264 welcome to jiangxi.
m-13305862264 18907910010 thank you.
相关类图展示:



电信计费系列共有3道题,题量较少;难度适中,重点在于用户与用户之间的关系,以及个人拨打电话的记录;我个人认为难点在于精准找到对应的用户后对其拨打记录进行计费,算出用户的余额。刚开始写的很慢是因为没有很好的理解之间的联系,左改改、右改改,花费了很多时间,当梳理清什么函数怎么用、用来干什么后写起来却很容易。还有需要注意的是用正则表达式来进行语句的规范,筛掉那些不符合基本格式的数据。
·各题目集题目 设计与分析 采坑心得 改进建议
(1)PTA大作业七 7-1 电信计费系列2-手机+座机计费

分析:仔细阅读题目,通过所给的类图搞清楚用户、通讯记录、拨打记录、收费模式之间的关系,写之前先进行大体的架构,理清楚脉络,防止写中途忘了相关函数或相关类的功能以及使用方法。写注释是一个让你写时间长了忘了内容快速找回的办法,在写完一个函数后要写上相应的注释。
采坑心得
刚开始我使用的是数组装输入的数据,但发现用数组时,长度固定,于是使用动态数组来存储不固定的数据,如:ArrayList<String> title=new ArrayList<>();并用其title.add(input.nextLine())来添加数据;用title.get(++i)来获取数据;写本题时需要考虑好用户的重复输入、用户的非法输入、是用户打座机还是座机打用户、用户是拨打电话方还是接听电话方、用户拨打电话时的位置(市内、省内、省外)、用户在何时打电话以及何时打完电话等等一系类情况,否则会导致输出结果错误。
改进建议
通过不同的正则表达式可以简化代码长度,使其更好的维护和使用;可以通过写一个函数来实现公共的功能,减小冗杂程度,使其得到优化。
代码展示(直接复制便可用):
1 import java.util.Date; 2 import java.util.ArrayList; 3 import java.util.Scanner; 4 import java.text.DateFormat; 5 import java.text.ParseException; 6 import java.text.SimpleDateFormat; 7 import java.util.Collections; 8 import java.util.logging.SimpleFormatter; 9 import java.util.regex.Pattern; 10 public class Main { 11 public static void main(String[] args) throws ParseException { 12 Scanner input = new Scanner(System.in); 13 ArrayList<String> informationString=new ArrayList<>();//字符串 14 ArrayList<char[]> informationChar=new ArrayList<>();//字符串转为字符 15 ArrayList<User> user=new ArrayList<>(); //用户列表 16 String str="end"; 17 String regex1 = "^u-(1[0-9]{10} 1|0[0-9]{9,11} 0)$"; 18 String regex2 = "^t-1[0-9]{10} 0[0-9]{2,3} 1[0-9]{10} 0[0-9]{2,3} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2}$";//手机打手机 19 String regex3 = "^t-0[0-9]{9,11} 1[0-9]{10} 0[0-9]{2,3} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2}$";//座机打手机 20 String regex4 = "^t-1[0-9]{10} 0[0-9]{2,3} 0[0-9]{9,11} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2}$";//手机打座机 21 String regex5 = "^t-0[0-9]{9,11} 0[0-9]{9,11} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2}$";//座机打座机 22 String regex6 = "^[1-9]\\d{3}.([1-9]|1[0-2]).([1-9]|[1-2][0-9]|3[0-1])\\s+(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d$"; 23 int i=-1; 24 while(true) { //输入信息并获取 25 String cin=input.nextLine(); 26 if(cin.equals(str)) { //判断输入的是否为end结束符 27 break; 28 } 29 informationString.add(cin); 30 } 31 int isSecondRow1 = 0; //在类型1下记录用户个数 32 boolean isSame1 = false; //判断是否用户相同 33 boolean isDifferent = true; //在比大小中如果找到了比它大的号码时改为false,后面的号码不需要在进行比较 34 int isSecondRow2 = 0; //在类型2下记录用户个数 35 for(int j=0;j<informationString.size();j++) { //将信息进行分割 36 if(informationString.get(j).toCharArray().length!=0) { 37 informationChar.add(informationString.get(j).toCharArray());//将字符串变为字符数组 38 boolean flag1 = informationString.get(j).matches(regex1); 39 boolean flag2 = informationString.get(j).matches(regex2);//手机打手机 40 boolean flag3 = informationString.get(j).matches(regex3);//座机打手机 41 boolean flag4 = informationString.get(j).matches(regex4);//手机打座机 42 boolean flag5 = informationString.get(j).matches(regex5);//座机打座机 43 //输入类型1,以u开头进行用户开户 44 //if(informationChar.get(j)[0]=='u') { 45 if(flag1==true) { 46 int number=-1; 47 isSecondRow1++; 48 String []thirdString = informationString.get(j).split("[- ]");//将第一种信息分割成三个要第二个 49 if(isSecondRow1>=2) { //从第二个用户开始进行判断是否重复与比大小 50 isSame1 = false; 51 int recordPlace =0; //记录插入时列表的位置 52 for(int k=0;k<user.size();k++) {//在用户列表从开始到结束进行判断 53 if(thirdString[1].equals(user.get(k).getNumber())) {//判断第二个用户是否与用户列表中的用户相同 54 isSame1 = true; //相同直接跳出查询 55 break; 56 } 57 if(thirdString[1].charAt(0)==user.get(k).getNumber().charAt(0)) { 58 if(thirdString[1].compareTo(user.get(k).getNumber())<0 ) { 59 //System.out.println("here"); 60 recordPlace=k; 61 } 62 else { 63 recordPlace++; //使用这个目的为了不让程序继续无意义的比下去而浪费时间,在之后没有在用到 64 } 65 } 66 else { 67 if( thirdString[1].charAt(0)>user.get(k).getNumber().charAt(0) ) { 68 recordPlace++; 69 } 70 } 71 } 72 if(!isSame1) { //用户不同,需要添加到用户列表里 73 user.add(recordPlace,new User());//给用户开户并按照大小顺序进行插入 74 user.get(recordPlace).setNumber(thirdString[1]); 75 if( thirdString[2].charAt(0)=='0' ) user.get(recordPlace).selectCharge='0'; 76 if( thirdString[2].charAt(0)=='1' ) user.get(recordPlace).selectCharge='1'; 77 } 78 } 79 else { //第一个用户,直接放到用户列表中;从第二个开始进行判断 80 user.add(new User());//给用户开户 81 user.get(++number).setNumber(thirdString[1]); 82 if( thirdString[2].charAt(0)=='0' ) user.get(number).selectCharge='0'; 83 if( thirdString[2].charAt(0)=='1' ) user.get(number).selectCharge='1'; 84 } 85 } 86 87 else { 88 String []fiveString = informationString.get(j).split("[- ]");//将第二种信息分割成七个 89 if(flag5) { //座机打座机 90 for(int k=0;k<user.size();k++) { //在用户列表从开始到结束进行判断 91 if(fiveString[1].equals(user.get(k).getNumber())) { //用户的通讯信息与号码进行匹配 92 CallRecord callRecord1 = new CallRecord(); //通话记录 93 callRecord1.setCallingNumber(fiveString[1]);//拨打电话号 94 callRecord1.setAnswerNumber(fiveString[2]);//就听电话号 95 callRecord1.setCallingAddressAreaCode(fiveString[1].substring(0,4));//拨打电话的区号 96 callRecord1.setAnswerAddressAreaCode(fiveString[2].substring(0,4));//接收电话的区号 97 String startTime1 =fiveString[3]+" "+fiveString[4]; //传入开始时间 98 String endTime =fiveString[5]+" "+fiveString[6]; //传入结束时间 99 boolean flag6=startTime1.matches(regex6); 100 boolean flag7=endTime.matches(regex6); 101 if(flag6==true&&flag7==true) { 102 SimpleDateFormat totalUse1 = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");//固定格式 103 Date ss1 = totalUse1.parse(startTime1); 104 callRecord1.setStartTime(ss1); 105 Date ss2 = totalUse1.parse(endTime); 106 callRecord1.setEndTime(ss2); 107 if( (judgeQuHao(fiveString[1],0)==judgeQuHao(fiveString[2],0)&&(judgeQuHao(fiveString[1],0)==1)||judgeQuHao(fiveString[1],0)==2) ) { //市内1、省内2、国内3 108 user.get(k).getUserRecords().addCallingInCityRecords(callRecord1); 109 } 110 else if( (judgeQuHao(fiveString[1],0)!=judgeQuHao(fiveString[2],0))&&( judgeQuHao(fiveString[1],0)==1||judgeQuHao(fiveString[1],0)==2 )&&( judgeQuHao(fiveString[2],0)==1||judgeQuHao(fiveString[2],0)==2 ) ) { 111 user.get(k).getUserRecords().addCallingInProvinceRecords(callRecord1); 112 } 113 else { 114 user.get(k).getUserRecords().addCallingInLandRecords(callRecord1); 115 } 116 } 117 } 118 } 119 } 120 // if(regex3==true) {//座机与手机互打 121 if(flag3) {//座机在前面 122 boolean judge1=true; 123 for(int k=0;k<user.size();k++) { //在用户列表从开始到结束进行判断 124 if(fiveString[1].equals(user.get(k).getNumber())) { //用户的通讯信息与号码进行匹配 125 judge1=false; //当打电话的人没开户时 126 CallRecord callRecord1 = new CallRecord(); //创建通话记录 127 callRecord1.setCallingNumber(fiveString[1]);//拨打电话号 128 callRecord1.setAnswerNumber(fiveString[2]);//接听电话号 129 callRecord1.setCallingAddressAreaCode(fiveString[1].substring(0,4));//拨打电话的区号 130 callRecord1.setAnswerAddressAreaCode(fiveString[3]);//接收电话的区号 131 String startTime1 =fiveString[4]+" "+fiveString[5]; //传入开始时间 132 String endTime =fiveString[6]+" "+fiveString[7]; //传入结束时间 133 boolean flag6=startTime1.matches(regex6); 134 boolean flag7=endTime.matches(regex6); 135 if(flag6==true&&flag7==true) { 136 SimpleDateFormat totalUse1 = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");//固定格式 137 Date ss1 = totalUse1.parse(startTime1); 138 callRecord1.setStartTime(ss1); 139 Date ss2 = totalUse1.parse(endTime); 140 callRecord1.setEndTime(ss2); 141 //判断接听区号 142 if( judgeQuHao(fiveString[1],0)==1 ){ 143 //判断接电话的人在哪里 144 //接电话的人也在市内,则将打电话人的用户记录添加到市内拨打记录列表里 145 if( judgeQuHao(fiveString[3],1)==1 ) { 146 user.get(k).getUserRecords().addCallingInCityRecords(callRecord1); 147 } 148 //接电话的人在省内,则将打电话人的用户记录添加到省内拨打记录列表里 149 else if( judgeQuHao(fiveString[3],1)==2 ) { 150 user.get(k).getUserRecords().addCallingInProvinceRecords(callRecord1); 151 } 152 //接电话的人在省外,则将打电话人的用户记录添加到省外拨打记录列表里,并将接电话的人记录添加到省外 153 else if( judgeQuHao(fiveString[3],1)==3 ) { 154 user.get(k).getUserRecords().addCallingInLandRecords(callRecord1); 155 int q; 156 for(q=0;q<user.size();q++) { 157 if( fiveString[2].equals(user.get(q).getNumber()) ) break; 158 } 159 if(q!=user.size()) { 160 user.get(q).getUserRecords().addAnswerLandRecords(callRecord1); 161 } 162 } 163 } 164 } 165 } 166 } 167 if(judge1==true) { 168 CallRecord callRecord1 = new CallRecord(); //创建通话记录 169 callRecord1.setCallingNumber(fiveString[1]);//拨打电话号 170 callRecord1.setAnswerNumber(fiveString[2]);//接听电话号 171 callRecord1.setCallingAddressAreaCode(fiveString[1].substring(0,4));//拨打电话的区号 172 callRecord1.setAnswerAddressAreaCode(fiveString[3]);//接收电话的区号 173 String startTime1 =fiveString[4]+" "+fiveString[5]; //传入开始时间 174 String endTime =fiveString[6]+" "+fiveString[7]; //传入结束时间 175 boolean flag6=startTime1.matches(regex6); 176 boolean flag7=endTime.matches(regex6); 177 if(flag6==true&&flag7==true) { 178 SimpleDateFormat totalUse1 = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");//固定格式 179 Date ss1 = totalUse1.parse(startTime1); 180 callRecord1.setStartTime(ss1); 181 Date ss2 = totalUse1.parse(endTime); 182 callRecord1.setEndTime(ss2); 183 //打电话的人在省外,走省外漫游, 184 if( judgeQuHao(fiveString[3],1)==3 ) { 185 int q; 186 for(q=0;q<user.size();q++) { 187 if( fiveString[2].equals(user.get(q).getNumber()) ) break; 188 } 189 if(q!=user.size()) user.get(q).getUserRecords().addAnswerLandRecords(callRecord1); 190 } 191 } 192 } 193 } 194 //手机打座机 195 if(flag4==true) { 196 for(int k=0;k<user.size();k++) { //在用户列表从开始到结束进行判断 197 if(fiveString[1].equals(user.get(k).getNumber())) { //用户的通讯信息与号码进行匹配 198 CallRecord callRecord1 = new CallRecord(); //创建通话记录 199 callRecord1.setCallingNumber(fiveString[1]);//拨打电话号 200 callRecord1.setAnswerNumber(fiveString[3]);//接听电话号 201 callRecord1.setCallingAddressAreaCode(fiveString[2]);//拨打电话的区号 202 callRecord1.setAnswerAddressAreaCode(fiveString[3].substring(0,4));//接收电话的区号 203 String startTime1 =fiveString[4]+" "+fiveString[5]; //传入开始时间 204 String endTime =fiveString[6]+" "+fiveString[7]; //传入结束时间 205 boolean flag6=startTime1.matches(regex6); 206 boolean flag7=endTime.matches(regex6); 207 if(flag6==true&&flag7==true) { 208 SimpleDateFormat totalUse1 = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");//固定格式 209 Date ss1 = totalUse1.parse(startTime1); 210 callRecord1.setStartTime(ss1); 211 Date ss2 = totalUse1.parse(endTime); 212 callRecord1.setEndTime(ss2); 213 //判断接听区号 214 if( judgeQuHao(fiveString[2],1)==1 ){ 215 user.get(k).getUserRecords().addNewCallingInCityRecords(callRecord1); //改了 216 } 217 else if( judgeQuHao(fiveString[2],1)==2 ) { 218 user.get(k).getUserRecords().addCallingInProvinceRecords(callRecord1); 219 } 220 else if(judgeQuHao(fiveString[2],1)==3) { 221 user.get(k).getUserRecords().addCallingInLandRecords(callRecord1); 222 } 223 } 224 } 225 } 226 } 227 // } 228 if(flag2) {//手机打手机 229 boolean judge1=true; 230 for(int k=0;k<user.size();k++) { //在用户列表从开始到结束进行判断 231 if(fiveString[1].equals(user.get(k).getNumber())) { //用户的通讯信息与号码进行匹配 232 judge1=false; //当打电话的人没开户时 233 CallRecord callRecord1 = new CallRecord(); //创建通话记录 234 callRecord1.setCallingNumber(fiveString[1]);//拨打电话号 235 callRecord1.setAnswerNumber(fiveString[3]);//接听电话号 236 callRecord1.setCallingAddressAreaCode(fiveString[2]);//拨打电话的区号 237 callRecord1.setAnswerAddressAreaCode(fiveString[4]);//接收电话的区号 238 String startTime1 =fiveString[5]+" "+fiveString[6]; //传入开始时间 239 String endTime =fiveString[7]+" "+fiveString[8]; //传入结束时间 240 boolean flag6=startTime1.matches(regex6); 241 boolean flag7=endTime.matches(regex6); 242 if(flag6==true&&flag7==true) { 243 SimpleDateFormat totalUse1 = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");//固定格式 244 Date ss1 = totalUse1.parse(startTime1); 245 callRecord1.setStartTime(ss1); 246 Date ss2 = totalUse1.parse(endTime); 247 callRecord1.setEndTime(ss2); 248 //分别判断两个号的区号,在长度为9中,都是手机号,没有座机号, 249 //打电话的人在本市内打 250 if( judgeQuHao(fiveString[2],1)==1 ){ 251 //判断接电话的人在哪里 252 //接电话的人也在市内,则将打电话人的用户记录添加到市内拨打记录列表里 253 if( judgeQuHao(fiveString[4],1)==1 ) { 254 user.get(k).getUserRecords().addNewCallingInCityRecords(callRecord1); 255 } 256 //接电话的人在省内,则将打电话人的用户记录添加到省内拨打记录列表里 257 else if( judgeQuHao(fiveString[4],1)==2 ) { 258 user.get(k).getUserRecords().addNewCallingInProvinceRecords(callRecord1); 259 } 260 //接电话的人在省外,则将打电话人的用户记录添加到省外拨打记录列表里,并将接电话的人记录添加到省外 261 else if( judgeQuHao(fiveString[4],1)==3 ) { 262 user.get(k).getUserRecords().addNewCallingInLandRecords(callRecord1); 263 int q; 264 for(q=0;q<user.size();q++) { 265 if( fiveString[3].equals(user.get(q).getNumber()) ) break; 266 } 267 if(q!=user.size()) { 268 user.get(q).getUserRecords().addAnswerLandRecords(callRecord1); 269 } 270 } 271 } 272 //打电话的人在省内,走省内漫游, 273 else if( judgeQuHao(fiveString[2],1)==2 ) { 274 //判断接电话人的位置 275 //先判断接电话的人在省外, 276 if( judgeQuHao(fiveString[4],1)==3 ) { 277 int q; 278 for(q=0;q<user.size();q++) { 279 if( fiveString[3].equals(user.get(q).getNumber()) ) break; 280 } 281 if(q!=user.size()) user.get(q).getUserRecords().addAnswerLandRecords(callRecord1); 282 } 283 user.get(k).getUserRecords().addCallingInProvinceRecords(callRecord1); 284 } 285 //打电话的人在省外,走省外漫游, 286 else if( judgeQuHao(fiveString[2],1)==3 ) { 287 //判断接电话人的位置 288 //先判断接电话的人在省外, 289 if( judgeQuHao(fiveString[4],1)==3 ) { 290 int q; 291 for(q=0;q<user.size();q++) { 292 if( fiveString[3].equals(user.get(q).getNumber()) ) break; 293 } 294 if(q!=user.size()) user.get(q).getUserRecords().addAnswerLandRecords(callRecord1); 295 } 296 user.get(k).getUserRecords().addCallingInLandRecords(callRecord1); 297 } 298 } 299 } 300 } 301 if(judge1==true) { 302 CallRecord callRecord1 = new CallRecord(); //创建通话记录 303 callRecord1.setCallingNumber(fiveString[1]);//拨打电话号 304 callRecord1.setAnswerNumber(fiveString[3]);//接听电话号 305 callRecord1.setCallingAddressAreaCode(fiveString[2]);//拨打电话的区号 306 callRecord1.setAnswerAddressAreaCode(fiveString[4]);//接收电话的区号 307 String startTime1 =fiveString[5]+" "+fiveString[6]; //传入开始时间 308 String endTime =fiveString[7]+" "+fiveString[8]; //传入结束时间 309 boolean flag6=startTime1.matches(regex6); 310 boolean flag7=endTime.matches(regex6); 311 if(flag6==true&&flag7==true) { 312 SimpleDateFormat totalUse1 = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");//固定格式 313 Date ss1 = totalUse1.parse(startTime1); 314 callRecord1.setStartTime(ss1); 315 Date ss2 = totalUse1.parse(endTime); 316 callRecord1.setEndTime(ss2); 317 //接电话的人只判断是否在省外, 318 if( judgeQuHao(fiveString[4],1)==3 ) { 319 int q; 320 for(q=0;q<user.size();q++) { 321 if( fiveString[3].equals(user.get(q).getNumber()) ) break; 322 } 323 if(q!=user.size()) user.get(q).getUserRecords().addAnswerLandRecords(callRecord1); 324 } 325 } 326 } 327 } 328 } 329 } 330 } 331 for(int j=0;j<user.size();j++) { 332 LandlinePhoneCharging landlinePhoneCharging =new LandlinePhoneCharging(); 333 user.get(j).setChargeMode(landlinePhoneCharging); 334 user.get(j).calBalance(); 335 if(j==user.size()-1){ 336 System.out.print(user.get(j).getNumber()+" "+user.get(j).calCost()+" "+user.get(j).getBalance()); 337 } 338 else 339 System.out.println(user.get(j).getNumber()+" "+user.get(j).calCost()+" "+user.get(j).getBalance()); 340 } 341 } 342 public static int judgeQuHao(String s,int select) { //判断是市内、省内、国内 343 String s1 = new String(); 344 if(select==0) s1 = s.substring(0,4); //将前四位截取出来 345 else if(select==1) s1 = s; 346 int s2 = Integer.parseInt(s1); //将其转为数字 347 if(s2==791) return 1; //判断市内 348 else { 349 if( (s2>=790&&s2<=799) || s2==701 ) return 2; //判断省内 350 return 3; //判断国内 351 } 352 } 353 354 } 355 356 class User{ 357 public char selectCharge ='0'; 358 private UserRecords userRecords = new UserRecords(); //用户记录 359 private double balance = 100; //余额 360 private ChargeMode chargeMode; //计费方式 361 private String number; //号码 362 public double calBalance() //拨打余额 363 { 364 double bal=this.balance; 365 double a1=chargeMode.calCost(userRecords); 366 double b2=chargeMode.getMonthlyRent(selectCharge); //返回月租 367 this.balance=bal-a1-b2; 368 return Double.parseDouble(String.format("%.1f",balance)); 369 } 370 public double calCost() //拨打费用 371 { 372 double a2=chargeMode.calCost(userRecords); 373 return Double.parseDouble(String.format("%.1f",a2)); 374 } 375 public UserRecords getUserRecords() 376 { 377 return userRecords; 378 } 379 public void setUserRecords(UserRecords userRecords) //设置该用户记录 380 { 381 this.userRecords = userRecords; 382 } 383 public double getBalance() //返回余额 384 { 385 return balance; 386 } 387 public ChargeMode getChargeMode() 388 { 389 return chargeMode; 390 } 391 public void setChargeMode(ChargeMode chargeMode) 392 { 393 this.chargeMode = chargeMode; 394 } 395 public String getNumber() //返回此用户号码判断是否为同一用户 396 { 397 return number; 398 } 399 public void setNumber(String number) //创建用户时设置号码 400 { 401 this.number=number; 402 } 403 } 404 405 abstract class ChargeMode{ //计费方式的抽象类 406 public abstract double calCost(UserRecords userRecords) ; 407 public abstract double getMonthlyRent(char selectCharge);//返回月租 408 } 409 410 class LandlinePhoneCharging extends ChargeMode{//固定电话缴费 411 private double monthlyRent = 20; //以前月租 412 private double monthlyRent1 = 15;//新模式月租 413 @Override 414 public double calCost(UserRecords userRecords) 415 { 416 ArrayList<ChargeRule> chargeRules1=new ArrayList<ChargeRule>(); 417 LandPhoneInCityRule landPhoneInCityRule=new LandPhoneInCityRule(); 418 LandPhoneInProvinceRule landPhoneInProvinceRule=new LandPhoneInProvinceRule(); 419 LandPhoneInLandRule landPhoneInLandRule=new LandPhoneInLandRule(); 420 newLandPhoneInCityRule newlandPhoneInCityRule=new newLandPhoneInCityRule(); 421 newLandPhoneInProvinceRule newlandPhoneInProvinceRule=new newLandPhoneInProvinceRule(); 422 newLandPhoneInLandRule newlandPhoneInLandRule=new newLandPhoneInLandRule(); 423 AnswerInLandRule answerInLandRecords = new AnswerInLandRule(); 424 chargeRules1.add(landPhoneInCityRule); 425 chargeRules1.add(landPhoneInProvinceRule); 426 chargeRules1.add(landPhoneInLandRule); 427 chargeRules1.add(newlandPhoneInCityRule); 428 chargeRules1.add(newlandPhoneInProvinceRule); 429 chargeRules1.add(newlandPhoneInLandRule); 430 chargeRules1.add(answerInLandRecords); 431 double a=chargeRules1.get(0).calCost(userRecords.getCallingInCityRecords()); 432 double b=chargeRules1.get(1).calCost(userRecords.getCallingInProvinceRecords()); 433 double c=chargeRules1.get(2).calCost(userRecords.getCallingInLandRecords()); 434 double d=chargeRules1.get(3).calCost(userRecords.getNewCallingInCityRecords()); 435 double e=chargeRules1.get(4).calCost(userRecords.getNewCallingInProvinceRecords()); 436 double f=chargeRules1.get(5).calCost(userRecords.getNewCallingInLandRecords()); 437 double g=chargeRules1.get(6).calCost(userRecords.getAnswerInLandRecords()); 438 // System.out.println(a); 439 // System.out.println(b); 440 // System.out.println(c); 441 // System.out.println(d); 442 // System.out.println(e); 443 // System.out.println(f); 444 // System.out.println(g); 445 return a+b+c+d+e+f+g; 446 } 447 @Override 448 public double getMonthlyRent(char selectCharge){ 449 if(selectCharge=='0') return monthlyRent; 450 else return monthlyRent1; 451 } 452 } 453 454 class UserRecords { //用户记录类,保存用户各种通话、短信的记录 455 private ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>(); //市内拨打电话记录列表 456 private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>(); //省内拨打电话记录列表 457 private ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>(); //国内拨打电话记录列表 458 459 private ArrayList<CallRecord> newCallingInCityRecords = new ArrayList<CallRecord>(); //新款市内拨打电话记录列表 460 private ArrayList<CallRecord> newCallingInProvinceRecords = new ArrayList<CallRecord>(); //新款省内拨打电话记录列表 461 private ArrayList<CallRecord> newCallingInLandRecords = new ArrayList<CallRecord>(); //新款国内拨打电话记录列表 462 463 private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>(); //市内接听电话记录列表 464 private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();//省内接听电话记录列表 465 private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>(); //国内接听电话记录列表 466 private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>(); 467 private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>(); 468 public void addCallingInCityRecords(CallRecord callRecord) { 469 callingInCityRecords.add(callRecord); 470 } 471 public void addCallingInProvinceRecords(CallRecord callRecord) { 472 callingInProvinceRecords.add(callRecord); 473 } 474 public void addCallingInLandRecords(CallRecord callRecord) { 475 callingInLandRecords.add(callRecord); 476 } 477 public void addNewCallingInCityRecords(CallRecord callRecord) { //新的 478 newCallingInCityRecords.add(callRecord); 479 } 480 public void addNewCallingInProvinceRecords(CallRecord callRecord) {//新的 481 newCallingInProvinceRecords.add(callRecord); 482 } 483 public void addNewCallingInLandRecords(CallRecord callRecord) {//新的 484 newCallingInLandRecords.add(callRecord); 485 } 486 public void addAnswerlnCityRecords(CallRecord answerRecord) { 487 answerInCityRecords.add(answerRecord); 488 } 489 public void addAnswerlnProvinceRecords(CallRecord answerRecord) { 490 answerInProvinceRecords.add(answerRecord); 491 } 492 public void addAnswerLandRecords(CallRecord answerRecord) { 493 answerInLandRecords.add(answerRecord); 494 } 495 public void addSendMessageRecords(MessageRecord sendMessageRecord){ 496 sendMessageRecords.add(sendMessageRecord); 497 } 498 public void addReceiveMessageRecords(MessageRecord receiveMessageRecord){ 499 receiveMessageRecords.add(receiveMessageRecord); 500 } 501 public ArrayList<MessageRecord> getSendMessageRecords(){ 502 return sendMessageRecords; 503 } 504 public ArrayList<MessageRecord> getReceiveMessageRecords(){ 505 return receiveMessageRecords; 506 } 507 public ArrayList<CallRecord> getCallingInCityRecords(){ //返回市内拨打记录 508 return callingInCityRecords; 509 } 510 public ArrayList<CallRecord> getCallingInProvinceRecords(){ 511 return callingInProvinceRecords; 512 } 513 public ArrayList<CallRecord> getCallingInLandRecords(){ 514 return callingInLandRecords; 515 } 516 public ArrayList<CallRecord> getNewCallingInCityRecords(){ //返回新款市内拨打记录 517 return newCallingInCityRecords; 518 } 519 public ArrayList<CallRecord> getNewCallingInProvinceRecords(){ 520 return newCallingInProvinceRecords; 521 } 522 public ArrayList<CallRecord> getNewCallingInLandRecords(){ 523 return newCallingInLandRecords; 524 } 525 public ArrayList<CallRecord> getAnswerInCityRecords(){ 526 return answerInCityRecords; 527 } 528 public ArrayList<CallRecord> getAnswerInProvinceRecords(){ 529 return answerInProvinceRecords; 530 } 531 public ArrayList<CallRecord> getAnswerInLandRecords(){ 532 return answerInLandRecords; 533 } 534 } 535 536 abstract class CommunicationRecord { //抽象的通讯记录类 537 protected String callingNumber; //拨打号码 538 protected String answerNumber; //接听号码 539 540 public String getCallingNumber() { 541 return callingNumber; 542 } 543 public void setCallingNumber(String callingNumber) { 544 this.callingNumber = callingNumber; 545 } 546 547 public String getAnswerNumber() { 548 return answerNumber; 549 } 550 public void setAnswerNumber(String answerNumber) { 551 this.answerNumber = answerNumber; 552 } 553 } 554 555 class CallRecord extends CommunicationRecord{ //通话记录 556 private Date startTime; //通话的起始时间 557 private Date endTime; //通话的结束时间 558 private String callingAddressAreaCode; //拨号地点的区号 559 private String answerAddressAreaCode; //接听地点的区号 560 public Date getStartTime() { //返回起始时间 561 return startTime; 562 } 563 public void setStartTime(Date startTime) { //设置起始时间 564 this.startTime = startTime; 565 } 566 public Date getEndTime() { //返回结束时间 567 return endTime; 568 } 569 public void setEndTime(Date endTime) { //设置结束时间 570 this.endTime = endTime; 571 } 572 public String getCallingAddressAreaCode() { 573 return callingAddressAreaCode; 574 } 575 public void setCallingAddressAreaCode(String callingAddressAreaCode) { 576 this.callingAddressAreaCode = callingAddressAreaCode; 577 } 578 public String getAnswerAddressAreaCode() { 579 return answerAddressAreaCode; 580 } 581 public void setAnswerAddressAreaCode(String answerAddressAreaCode) { 582 this.answerAddressAreaCode = answerAddressAreaCode; 583 } 584 585 } 586 587 class MessageRecord extends CommunicationRecord{ //短信记录 588 private String message; 589 590 public String getMessage() { 591 return message; 592 } 593 594 public void setMessage(String message) { 595 this.message = message; 596 } 597 598 } 599 600 abstract class ChargeRule { 601 public double calCost(ArrayList<CallRecord> callingInCityRecords){//计费规则 602 return 1; 603 } 604 } 605 abstract class CallChargeRule extends ChargeRule{ 606 public double calCost(ArrayList<CallRecord> callingInCityRecords){//计费规则 607 return 1; 608 } 609 } 610 class LandPhoneInCityRule extends CallChargeRule{ //座机拨打市内电话的计费规则类 611 public double calCost(ArrayList<CallRecord> callRecords) { 612 double cost = 0; 613 for(int i=0;i<callRecords.size();i++) { 614 long totoalTime = callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime(); 615 if(totoalTime%60000>1) { 616 totoalTime = totoalTime/60000+1; 617 } 618 else totoalTime = totoalTime/60000; 619 cost+=totoalTime*0.1; 620 } 621 return cost; 622 } 623 } 624 class LandPhoneInProvinceRule extends CallChargeRule{//座机拨打省内电话的计费规则类 625 public double calCost(ArrayList<CallRecord> callRecords) { 626 double cost = 0; 627 for(int i=0;i<callRecords.size();i++) { 628 long totoalTime = callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime(); 629 if(totoalTime%60000>1) { 630 totoalTime = totoalTime/60000+1; 631 } 632 else totoalTime = totoalTime/60000; 633 cost+=totoalTime*0.3; 634 } 635 return cost; 636 } 637 } 638 class LandPhoneInLandRule extends CallChargeRule{//座机拨打省外电话的计费规则类 639 public double calCost(ArrayList<CallRecord> callRecords) { 640 double cost = 0; 641 for(int i=0;i<callRecords.size();i++) { 642 long totoalTime = callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime(); 643 if(totoalTime%60000>1) { 644 totoalTime = totoalTime/60000+1; 645 } 646 else totoalTime = totoalTime/60000; 647 cost+=totoalTime*0.6; 648 } 649 return cost; 650 } 651 } 652 class newLandPhoneInCityRule extends CallChargeRule{ //新座机拨打市内电话的计费规则类 653 public double calCost(ArrayList<CallRecord> callRecords) { 654 double cost = 0; 655 for(int i=0;i<callRecords.size();i++) { 656 long totoalTime = callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime(); 657 if(totoalTime%60000>1) { 658 totoalTime = totoalTime/60000+1; 659 } 660 else totoalTime = totoalTime/60000; 661 cost+=totoalTime*0.1; 662 } 663 return cost; 664 } 665 } 666 class newLandPhoneInProvinceRule extends CallChargeRule{//新座机拨打省内电话的计费规则类 667 public double calCost(ArrayList<CallRecord> callRecords) { 668 double cost = 0; 669 for(int i=0;i<callRecords.size();i++) { 670 long totoalTime = callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime(); 671 if(totoalTime%60000>1) { 672 totoalTime = totoalTime/60000+1; 673 } 674 else totoalTime = totoalTime/60000; 675 cost+=totoalTime*0.2; 676 } 677 return cost; 678 } 679 } 680 class newLandPhoneInLandRule extends CallChargeRule{//新座机拨打省外电话的计费规则类 681 public double calCost(ArrayList<CallRecord> callRecords) { 682 double cost = 0; 683 for(int i=0;i<callRecords.size();i++) { 684 long totoalTime = callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime(); 685 if(totoalTime%60000>1) { 686 totoalTime = totoalTime/60000+1; 687 } 688 else totoalTime = totoalTime/60000; 689 cost+=totoalTime*0.3; 690 } 691 return cost; 692 } 693 } 694 class AnswerInLandRule extends CallChargeRule{//座机拨打省外电话的计费规则类 695 public double calCost(ArrayList<CallRecord> callRecords) { 696 double cost = 0; 697 for(int i=0;i<callRecords.size();i++) { 698 long totoalTime = callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime(); 699 if(totoalTime%60000>1) { 700 totoalTime = totoalTime/60000+1; 701 } 702 else totoalTime = totoalTime/60000; 703 cost+=totoalTime*0.3; 704 } 705 return cost; 706 } 707 } 708 abstract class MessageChargeRule{ 709 public abstract double calCost(ArrayList<MessageRecord> messageRecords); 710 } 711 class SendMessageRule extends MessageChargeRule{ 712 @Override 713 public double calCost(ArrayList<MessageRecord> messageRecords) { 714 return 1; 715 } 716 }
(2)PTA大作业八 7-1 电信计费系列3-短信计费

分析:用户的开户以及多用户的关系与前面一致,不同在于前面是电话记录,而这题是在此基础上加入短信记录;之前的代码不用动,只要添加新的即可。
采坑心得
短信对于电话来说相对的简单一些,需要判断的情况也没有电话那么复杂,需要注意的就是短信条数的统计,短信计费是通过发送短信的条数进行的,短信条数也包括一条短信内容中每8个字算一条短信,不足8个算成8个来计算。
本次代码只是针对于短信计费的相关的问题,删去了电话计费;如想看电话计费可以浏览上面的代码:
1 import java.util.Date; 2 import java.util.ArrayList; 3 import java.util.Scanner; 4 import java.text.DateFormat; 5 import java.text.ParseException; 6 import java.text.SimpleDateFormat; 7 import java.util.Collections; 8 import java.util.logging.SimpleFormatter; 9 import java.util.regex.Pattern; 10 public class Main { 11 public static void main(String[] args) throws ParseException { 12 Scanner input = new Scanner(System.in); 13 ArrayList<String> informationString=new ArrayList<>();//字符串 14 ArrayList<char[]> informationChar=new ArrayList<>();//字符串转为字符 15 ArrayList<User> user=new ArrayList<>(); //用户列表 16 String str="end"; 17 String regex1 = "^u-(1[0-9]{10} 1|0[0-9]{9,11} 0|1[0-9]{10} 3)$"; 18 String regex2 = "^t-1[0-9]{10} 0[0-9]{2,3} 1[0-9]{10} 0[0-9]{2,3} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2}$";//手机打手机 19 String regex3 = "^t-0[0-9]{9,11} 1[0-9]{10} 0[0-9]{2,3} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2}$";//座机打手机 20 String regex4 = "^t-1[0-9]{10} 0[0-9]{2,3} 0[0-9]{9,11} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2}$";//手机打座机 21 String regex5 = "^t-0[0-9]{9,11} 0[0-9]{9,11} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2}$";//座机打座机 22 String regex6 = "^[1-9]\\d{3}.([1-9]|1[0-2]).([1-9]|[1-2][0-9]|3[0-1])\\s+(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d$"; 23 String regex7 = "^m-1[0-9]{10} 1[0-9]{10} [ 0-9a-z,.A-Z]+$"; 24 int i=-1; 25 while(true) { //输入信息并获取 26 String cin=input.nextLine(); 27 if(cin.equals(str)) { //判断输入的是否为end结束符 28 break; 29 } 30 informationString.add(cin); 31 } 32 boolean flag1=false; 33 boolean flag2=false; 34 boolean flag3=false; 35 boolean flag4=false; 36 boolean flag5=false; 37 boolean flagmessage=false; 38 boolean isFirst = true; 39 int isSecondRow1 = 0; //在类型1下记录用户个数 40 boolean isSame1 = false; //判断是否用户相同 41 boolean isDifferent = true; //在比大小中如果找到了比它大的号码时改为false,后面的号码不需要在进行比较 42 int isSecondRow2 = 0; //在类型2下记录用户个数 43 for(int j=0;j<informationString.size();j++) { //将信息进行分割 44 if(informationString.get(j).toCharArray().length!=0) { 45 informationChar.add(informationString.get(j).toCharArray());//将字符串变为字符数组 46 flag1 = informationString.get(j).matches(regex1); 47 flag2 = informationString.get(j).matches(regex2);//手机打手机 48 flag3 = informationString.get(j).matches(regex3);//座机打手机 49 flag4 = informationString.get(j).matches(regex4);//手机打座机 50 flag5 = informationString.get(j).matches(regex5);//座机打座机 51 flagmessage = informationString.get(j).matches(regex7);//短信 52 //输入类型1,以u开头进行用户开户 53 //if(informationChar.get(j)[0]=='u') { 54 if(flag1==true&&isFirst==true) { 55 int number=-1; 56 isSecondRow1++; 57 String []thirdString = informationString.get(j).split("[- ]");//将第一种信息分割成三个要第二个 58 if(isSecondRow1>=2) { //从第二个用户开始进行判断是否重复与比大小 59 isSame1 = false; 60 int recordPlace =0; //记录插入时列表的位置 61 for(int k=0;k<user.size();k++) {//在用户列表从开始到结束进行判断 62 if(thirdString[1].equals(user.get(k).getNumber())) {//判断第二个用户是否与用户列表中的用户相同 63 isSame1 = true; //相同直接跳出查询 64 break; 65 } 66 if(thirdString[1].charAt(0)==user.get(k).getNumber().charAt(0)) { 67 if(thirdString[1].compareTo(user.get(k).getNumber())<0 ) { 68 //System.out.println("here"); 69 recordPlace=k; 70 break; 71 } 72 else { 73 recordPlace++; //使用这个目的为了不让程序继续无意义的比下去而浪费时间,在之后没有在用到 74 } 75 } 76 else { 77 if( thirdString[1].charAt(0)>user.get(k).getNumber().charAt(0) ) { 78 recordPlace++; 79 } 80 } 81 } 82 if(!isSame1) { //用户不同,需要添加到用户列表里 83 user.add(recordPlace,new User());//给用户开户并按照大小顺序进行插入 84 user.get(recordPlace).setNumber(thirdString[1]); 85 if( thirdString[2].charAt(0)=='0' ) user.get(recordPlace).selectCharge='0'; 86 if( thirdString[2].charAt(0)=='1' ) user.get(recordPlace).selectCharge='1'; 87 } 88 } 89 else { //第一个用户,直接放到用户列表中;从第二个开始进行判断 90 user.add(new User());//给用户开户 91 user.get(++number).setNumber(thirdString[1]); 92 if( thirdString[2].charAt(0)=='0' ) user.get(number).selectCharge='0'; 93 if( thirdString[2].charAt(0)=='1' ) user.get(number).selectCharge='1'; 94 if( thirdString[2].charAt(0)=='3' ) user.get(number).selectCharge='3'; 95 } 96 } 97 98 else { 99 isFirst=false; 100 if(isFirst==false) { 101 String str1 = informationString.get(j).substring(2); 102 String s[] = str1.split("[ ]"); 103 if(flagmessage) {//短信 104 for(int k=0;k<user.size();k++) { 105 if(s[0].equals(user.get(k).getNumber())) { 106 MessageRecord messageRecord = new MessageRecord (); //短信记录 107 messageRecord.setCallingNumber(s[0]);//拨打电话号 108 messageRecord.setMessage(str1.substring(24)); 109 user.get(k).getUserRecords().addSendMessageRecords(messageRecord); 110 } 111 } 112 } 113 } 114 } 115 } 116 } 117 if(flag2==true||flag3==true||flag4==true||flag5==true) { 118 for(int j=0;j<user.size();j++) { 119 LandlinePhoneCharging landlinePhoneCharging =new LandlinePhoneCharging(); 120 user.get(j).setChargeMode(landlinePhoneCharging); 121 user.get(j).calBalance(1); 122 if(j==user.size()-1){ 123 System.out.print(user.get(j).getNumber()+" "+user.get(j).calCost(1)+" "+user.get(j).getBalance()); 124 } 125 else 126 System.out.println(user.get(j).getNumber()+" "+user.get(j).calCost(1)+" "+user.get(j).getBalance()); 127 } 128 } 129 for(int j=0;j<user.size();j++) { 130 LineMessageCharging lineMessageCharging = new LineMessageCharging(); 131 user.get(j).setLineMessageCharging(lineMessageCharging); 132 user.get(j).calBalance(2); 133 if(j==user.size()-1) { 134 System.out.print(user.get(j).getNumber()+" "+user.get(j).calCost(2)+" "+user.get(j).getBalance()); 135 } 136 else System.out.println(user.get(j).getNumber()+" "+user.get(j).calCost(2)+" "+user.get(j).getBalance()); 137 } 138 } 139 public static int judgeQuHao(String s,int select) { //判断是市内、省内、国内 140 String s1 = new String(); 141 if(select==0) s1 = s.substring(0,4); //将前四位截取出来 142 else if(select==1) s1 = s; 143 int s2 = Integer.parseInt(s1); //将其转为数字 144 if(s2==791) return 1; //判断市内 145 else { 146 if( (s2>=790&&s2<=799) || s2==701 ) return 2; //判断省内 147 return 3; //判断国内 148 } 149 } 150 151 } 152 153 class User{ 154 public char selectCharge ='0'; 155 private UserRecords userRecords = new UserRecords(); //用户记录 156 private double balance = 100; //余额 157 private ChargeMode chargeMode; //计费方式 158 private LineMessageCharging lineMessageCharging=new LineMessageCharging(); 159 private String number; //号码 160 public double calBalance(int select) //拨打余额 161 { 162 double bal=this.balance; 163 if(select==1) { 164 double a1=chargeMode.calCost(userRecords); 165 double b2=chargeMode.getMonthlyRent(selectCharge); //返回月租 166 this.balance=bal-a1-b2; 167 } 168 else { //返回短信余额 169 double a1=lineMessageCharging.calCost(userRecords); 170 this.balance=bal-a1; 171 } 172 return Double.parseDouble(String.format("%.1f",balance)); 173 } 174 public double calCost(int select) //拨打费用 175 { 176 double a2=0; 177 if(select==1) { 178 a2=chargeMode.calCost(userRecords); 179 } 180 else { 181 a2=lineMessageCharging.calCost(userRecords); 182 } 183 return Double.parseDouble(String.format("%.1f",a2)); 184 } 185 public UserRecords getUserRecords() 186 { 187 return userRecords; 188 } 189 public void setUserRecords(UserRecords userRecords) //设置该用户记录 190 { 191 this.userRecords = userRecords; 192 } 193 public double getBalance() //返回余额 194 { 195 return balance; 196 } 197 public ChargeMode getChargeMode() 198 { 199 return chargeMode; 200 } 201 public void setChargeMode(ChargeMode chargeMode) 202 { 203 this.chargeMode = chargeMode; 204 } 205 public String getNumber() //返回此用户号码判断是否为同一用户 206 { 207 return number; 208 } 209 public void setNumber(String number) //创建用户时设置号码 210 { 211 this.number=number; 212 } 213 public void setLineMessageCharging(LineMessageCharging lineMessageCharging) { 214 this.lineMessageCharging=lineMessageCharging; 215 } 216 public LineMessageCharging getLineMessageCharging() { 217 return lineMessageCharging; 218 } 219 } 220 221 class LineMessageCharging{//短信收费 222 private double sum=0; 223 public double calCost(UserRecords userRecords) { 224 SendMessageRule sendMessageRule =new SendMessageRule (); 225 double sum=sendMessageRule.calCost(userRecords.getSendMessageRecords()); 226 return sum; 227 } 228 } 229 abstract class ChargeMode{ //计费方式的抽象类 230 public abstract double calCost(UserRecords userRecords) ; 231 public abstract double getMonthlyRent(char selectCharge);//返回月租 232 } 233 234 class LandlinePhoneCharging extends ChargeMode{//固定电话缴费 235 private double monthlyRent = 20; //以前月租 236 private double monthlyRent1 = 15;//新模式月租 237 @Override 238 public double calCost(UserRecords userRecords) 239 { 240 ArrayList<ChargeRule> chargeRules1=new ArrayList<ChargeRule>(); 241 LandPhoneInCityRule landPhoneInCityRule=new LandPhoneInCityRule(); 242 LandPhoneInProvinceRule landPhoneInProvinceRule=new LandPhoneInProvinceRule(); 243 LandPhoneInLandRule landPhoneInLandRule=new LandPhoneInLandRule(); 244 newLandPhoneInCityRule newlandPhoneInCityRule=new newLandPhoneInCityRule(); 245 newLandPhoneInProvinceRule newlandPhoneInProvinceRule=new newLandPhoneInProvinceRule(); 246 newLandPhoneInLandRule newlandPhoneInLandRule=new newLandPhoneInLandRule(); 247 AnswerInLandRule answerInLandRecords = new AnswerInLandRule(); 248 chargeRules1.add(landPhoneInCityRule); 249 chargeRules1.add(landPhoneInProvinceRule); 250 chargeRules1.add(landPhoneInLandRule); 251 chargeRules1.add(newlandPhoneInCityRule); 252 chargeRules1.add(newlandPhoneInProvinceRule); 253 chargeRules1.add(newlandPhoneInLandRule); 254 chargeRules1.add(answerInLandRecords); 255 double a=chargeRules1.get(0).calCost(userRecords.getCallingInCityRecords()); 256 double b=chargeRules1.get(1).calCost(userRecords.getCallingInProvinceRecords()); 257 double c=chargeRules1.get(2).calCost(userRecords.getCallingInLandRecords()); 258 double d=chargeRules1.get(3).calCost(userRecords.getNewCallingInCityRecords()); 259 double e=chargeRules1.get(4).calCost(userRecords.getNewCallingInProvinceRecords()); 260 double f=chargeRules1.get(5).calCost(userRecords.getNewCallingInLandRecords()); 261 double g=chargeRules1.get(6).calCost(userRecords.getAnswerInLandRecords()); 262 // System.out.println(a); 263 // System.out.println(b); 264 // System.out.println(c); 265 // System.out.println(d); 266 // System.out.println(e); 267 // System.out.println(f); 268 // System.out.println(g); 269 return a+b+c+d+e+f+g; 270 } 271 @Override 272 public double getMonthlyRent(char selectCharge){ 273 if(selectCharge=='0') return monthlyRent; 274 else return monthlyRent1; 275 } 276 } 277 278 class UserRecords { //用户记录类,保存用户各种通话、短信的记录 279 private ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>(); //市内拨打电话记录列表 280 private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>(); //省内拨打电话记录列表 281 private ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>(); //国内拨打电话记录列表 282 283 private ArrayList<CallRecord> newCallingInCityRecords = new ArrayList<CallRecord>(); //新款市内拨打电话记录列表 284 private ArrayList<CallRecord> newCallingInProvinceRecords = new ArrayList<CallRecord>(); //新款省内拨打电话记录列表 285 private ArrayList<CallRecord> newCallingInLandRecords = new ArrayList<CallRecord>(); //新款国内拨打电话记录列表 286 287 private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>(); //市内接听电话记录列表 288 private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();//省内接听电话记录列表 289 private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>(); //国内接听电话记录列表 290 private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>(); 291 private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>(); 292 public void addCallingInCityRecords(CallRecord callRecord) { 293 callingInCityRecords.add(callRecord); 294 } 295 public void addCallingInProvinceRecords(CallRecord callRecord) { 296 callingInProvinceRecords.add(callRecord); 297 } 298 public void addCallingInLandRecords(CallRecord callRecord) { 299 callingInLandRecords.add(callRecord); 300 } 301 public void addNewCallingInCityRecords(CallRecord callRecord) { //新的 302 newCallingInCityRecords.add(callRecord); 303 } 304 public void addNewCallingInProvinceRecords(CallRecord callRecord) {//新的 305 newCallingInProvinceRecords.add(callRecord); 306 } 307 public void addNewCallingInLandRecords(CallRecord callRecord) {//新的 308 newCallingInLandRecords.add(callRecord); 309 } 310 public void addAnswerlnCityRecords(CallRecord answerRecord) { 311 answerInCityRecords.add(answerRecord); 312 } 313 public void addAnswerlnProvinceRecords(CallRecord answerRecord) { 314 answerInProvinceRecords.add(answerRecord); 315 } 316 public void addAnswerLandRecords(CallRecord answerRecord) { 317 answerInLandRecords.add(answerRecord); 318 } 319 public void addSendMessageRecords(MessageRecord sendMessageRecord){ 320 sendMessageRecords.add(sendMessageRecord); 321 } 322 public void addReceiveMessageRecords(MessageRecord receiveMessageRecord){ 323 receiveMessageRecords.add(receiveMessageRecord); 324 } 325 public ArrayList<MessageRecord> getSendMessageRecords(){ 326 return sendMessageRecords; 327 } 328 public ArrayList<MessageRecord> getReceiveMessageRecords(){ 329 return receiveMessageRecords; 330 } 331 public ArrayList<CallRecord> getCallingInCityRecords(){ //返回市内拨打记录 332 return callingInCityRecords; 333 } 334 public ArrayList<CallRecord> getCallingInProvinceRecords(){ 335 return callingInProvinceRecords; 336 } 337 public ArrayList<CallRecord> getCallingInLandRecords(){ 338 return callingInLandRecords; 339 } 340 public ArrayList<CallRecord> getNewCallingInCityRecords(){ //返回新款市内拨打记录 341 return newCallingInCityRecords; 342 } 343 public ArrayList<CallRecord> getNewCallingInProvinceRecords(){ 344 return newCallingInProvinceRecords; 345 } 346 public ArrayList<CallRecord> getNewCallingInLandRecords(){ 347 return newCallingInLandRecords; 348 } 349 public ArrayList<CallRecord> getAnswerInCityRecords(){ 350 return answerInCityRecords; 351 } 352 public ArrayList<CallRecord> getAnswerInProvinceRecords(){ 353 return answerInProvinceRecords; 354 } 355 public ArrayList<CallRecord> getAnswerInLandRecords(){ 356 return answerInLandRecords; 357 } 358 } 359 360 abstract class CommunicationRecord { //抽象的通讯记录类 361 protected String callingNumber; //拨打号码 362 protected String answerNumber; //接听号码 363 364 public String getCallingNumber() { 365 return callingNumber; 366 } 367 public void setCallingNumber(String callingNumber) { 368 this.callingNumber = callingNumber; 369 } 370 371 public String getAnswerNumber() { 372 return answerNumber; 373 } 374 public void setAnswerNumber(String answerNumber) { 375 this.answerNumber = answerNumber; 376 } 377 } 378 379 class CallRecord extends CommunicationRecord{ //通话记录 380 private Date startTime; //通话的起始时间 381 private Date endTime; //通话的结束时间 382 private String callingAddressAreaCode; //拨号地点的区号 383 private String answerAddressAreaCode; //接听地点的区号 384 public Date getStartTime() { //返回起始时间 385 return startTime; 386 } 387 public void setStartTime(Date startTime) { //设置起始时间 388 this.startTime = startTime; 389 } 390 public Date getEndTime() { //返回结束时间 391 return endTime; 392 } 393 public void setEndTime(Date endTime) { //设置结束时间 394 this.endTime = endTime; 395 } 396 public String getCallingAddressAreaCode() { 397 return callingAddressAreaCode; 398 } 399 public void setCallingAddressAreaCode(String callingAddressAreaCode) { 400 this.callingAddressAreaCode = callingAddressAreaCode; 401 } 402 public String getAnswerAddressAreaCode() { 403 return answerAddressAreaCode; 404 } 405 public void setAnswerAddressAreaCode(String answerAddressAreaCode) { 406 this.answerAddressAreaCode = answerAddressAreaCode; 407 } 408 409 } 410 411 class MessageRecord extends CommunicationRecord{ //短信记录 412 private String message; 413 414 public String getMessage() { 415 return message; 416 } 417 418 public void setMessage(String message) { 419 this.message = message; 420 } 421 422 } 423 424 abstract class ChargeRule { 425 public double calCost(ArrayList<CallRecord> callingInCityRecords){//计费规则 426 return 1; 427 } 428 } 429 abstract class CallChargeRule extends ChargeRule{ 430 public double calCost(ArrayList<CallRecord> callingInCityRecords){//计费规则 431 return 1; 432 } 433 } 434 class LandPhoneInCityRule extends CallChargeRule{ //座机拨打市内电话的计费规则类 435 public double calCost(ArrayList<CallRecord> callRecords) { 436 double cost = 0; 437 for(int i=0;i<callRecords.size();i++) { 438 long totoalTime = callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime(); 439 if(totoalTime%60000>1) { 440 totoalTime = totoalTime/60000+1; 441 } 442 else totoalTime = totoalTime/60000; 443 cost+=totoalTime*0.1; 444 } 445 return cost; 446 } 447 } 448 class LandPhoneInProvinceRule extends CallChargeRule{//座机拨打省内电话的计费规则类 449 public double calCost(ArrayList<CallRecord> callRecords) { 450 double cost = 0; 451 for(int i=0;i<callRecords.size();i++) { 452 long totoalTime = callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime(); 453 if(totoalTime%60000>1) { 454 totoalTime = totoalTime/60000+1; 455 } 456 else totoalTime = totoalTime/60000; 457 cost+=totoalTime*0.3; 458 } 459 return cost; 460 } 461 } 462 class LandPhoneInLandRule extends CallChargeRule{//座机拨打省外电话的计费规则类 463 public double calCost(ArrayList<CallRecord> callRecords) { 464 double cost = 0; 465 for(int i=0;i<callRecords.size();i++) { 466 long totoalTime = callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime(); 467 if(totoalTime%60000>1) { 468 totoalTime = totoalTime/60000+1; 469 } 470 else totoalTime = totoalTime/60000; 471 cost+=totoalTime*0.6; 472 } 473 return cost; 474 } 475 } 476 class newLandPhoneInCityRule extends CallChargeRule{ //新座机拨打市内电话的计费规则类 477 public double calCost(ArrayList<CallRecord> callRecords) { 478 double cost = 0; 479 for(int i=0;i<callRecords.size();i++) { 480 long totoalTime = callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime(); 481 if(totoalTime%60000>1) { 482 totoalTime = totoalTime/60000+1; 483 } 484 else totoalTime = totoalTime/60000; 485 cost+=totoalTime*0.1; 486 } 487 return cost; 488 } 489 } 490 class newLandPhoneInProvinceRule extends CallChargeRule{//新座机拨打省内电话的计费规则类 491 public double calCost(ArrayList<CallRecord> callRecords) { 492 double cost = 0; 493 for(int i=0;i<callRecords.size();i++) { 494 long totoalTime = callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime(); 495 if(totoalTime%60000>1) { 496 totoalTime = totoalTime/60000+1; 497 } 498 else totoalTime = totoalTime/60000; 499 cost+=totoalTime*0.2; 500 } 501 return cost; 502 } 503 } 504 class newLandPhoneInLandRule extends CallChargeRule{//新座机拨打省外电话的计费规则类 505 public double calCost(ArrayList<CallRecord> callRecords) { 506 double cost = 0; 507 for(int i=0;i<callRecords.size();i++) { 508 long totoalTime = callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime(); 509 if(totoalTime%60000>1) { 510 totoalTime = totoalTime/60000+1; 511 } 512 else totoalTime = totoalTime/60000; 513 cost+=totoalTime*0.3; 514 } 515 return cost; 516 } 517 } 518 class AnswerInLandRule extends CallChargeRule{//座机拨打省外电话的计费规则类 519 public double calCost(ArrayList<CallRecord> callRecords) { 520 double cost = 0; 521 for(int i=0;i<callRecords.size();i++) { 522 long totoalTime = callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime(); 523 if(totoalTime%60000>1) { 524 totoalTime = totoalTime/60000+1; 525 } 526 else totoalTime = totoalTime/60000; 527 cost+=totoalTime*0.3; 528 } 529 return cost; 530 } 531 } 532 abstract class MessageChargeRule{ 533 public abstract double calCost(ArrayList<MessageRecord> messageRecords); 534 } 535 class SendMessageRule extends MessageChargeRule{ 536 double sum=0; 537 @Override 538 public double calCost(ArrayList<MessageRecord> messageRecords) { 539 int number=0; 540 int a=0; 541 for(int i=0;i<messageRecords.size();i++) { 542 int leng=messageRecords.get(i).getMessage().length(); 543 if(leng%10==0) { 544 number=leng/10; 545 a+=number; 546 } 547 else { 548 number=leng/10+1; 549 a+=number; 550 } 551 } 552 while(a>0) { 553 if(a<=3) { 554 sum+=0.1*a; 555 a-=a; 556 } 557 else if(a<=5) { 558 sum=sum+(a-3)*0.2; 559 a=a-(a-3); 560 //sum+=0.2*number; 561 } 562 else { 563 sum=sum+(a-5)*0.3; 564 a=a-(a-5); 565 //sum+=0.3*number; 566 } 567 } 568 return sum; 569 } 570 }
总结: 在这段的学习当中,我学习了很多,也犯过很多错误,有大的BUG也有细微的错误,在这当中我对于类以及对象有了更深的了解,并在基础的语法下熟练使用继承、多态、抽象类、包装类进行编写代码,对JAVA有了进一步的理解;面向对象的程序设计的重点在于如何创建对象,以及使用对象来做一些事,这使得理解类与对象是尤为的重要;对于题目的了解也显得尤为重要,不要毫无头绪进行编写,要有方向,这要才有更高的效率,通过编写题目要搞清楚自己的缺点在哪里、不足在哪里,要对其进行及时的弥补,多进行总结,多进行思考,通过本次的学习我发现了自身不足有以下几点:
1.当类和对象非常多时,导致我对之间的关系有些混乱,这说明我还需要多加练习,应用需要更加熟练。
2.对包装类中的方法不够清楚,导致使用时常常报错,在之后需要多次使用其中的方法来增加印象。
3.数学计算能力较差,导致解决问题时不能立刻想出解决问题的办法,解决方案是在其他软件上进行刷题,可以增加自己的解决问题的能力以及对知识点的巩固,需要多编写一些算法题,让自己更加熟练用何种方法进行相应计算。
感谢OOP课程老师认真的指导与作业的监督,,老师的每节课都是干货满满,使我们能在困难中提升自己编程能力,提升自己对问题的解决能力。
浙公网安备 33010602011771号