一:前言
对于近期的学习:主要是进行了pta的作业的学习,第七次题目集是因为要用到之前的那些图形有关的代码,而之前的图形题目因为未完全做出,而导致了第七次pta作业的很多问题,三次作业的套路相似,主要难点集中在理解电信计费系列题目的类图,并编写正确的正则表达式完成字符串的筛选,最后将筛选出的结果存储进对应的数组中,要计算结果时直接调用事先写好的程序遍历数组即可。这次blog的主要是就是分析八九十三次题目集中的电信资费题目。2.程序设计分析
7-1 电信计费系列1-座机计费
import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Objects; import java.util.Scanner; public class Main{ public static void main(String []args) throws ParseException { Scanner input = new Scanner(System.in); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ArrayList<User> arr = new ArrayList<>(); int flag = 0; String sc = input.nextLine(); String[] a ; while (!sc.equals("end")){ if(sc.matches("[u][-](0791)[0-9]{7,8}[ ][0]")){ a = sc.substring(2,sc.length()).split("\\s"); for(int i = 0;i < arr.size();i++){ if(Objects.equals(arr.get(i).getNumber(), a[0])) { flag = 1; } } if(flag == 0) { arr.add(new User(a[0])); flag = 0; } } if(sc.matches("t-(\\d){11,12}\\s(\\d){10,12}\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]" + "|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])" + "|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))" + "|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))" + "|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d")){ a = sc.substring(2,sc.length()).split("\\s"); for(int i=0;i< arr.size();i++){ if(arr.get(i).getNumber().equals(a[0])){ if(a[1].startsWith("0791")){ arr.get(i).userRecord.addCallingInCityRecords(new CallRecord(a[2]+" "+a[3],a[4]+" "+a[5])); arr.get(i).setUserRecord(arr.get(i).userRecord); } else if(a[1].substring(0,4).matches("(079\\d|0701)")){ arr.get(i).userRecord.addCallingInProvinceRecords(new CallRecord(a[2]+" "+a[3],a[4]+" "+a[5])); arr.get(i).setUserRecord(arr.get(i).userRecord); } else{ arr.get(i).userRecord.addCallingInLandRecords(new CallRecord(a[2]+" "+a[3],a[4]+" "+a[5])); arr.get(i).setUserRecord(arr.get(i).userRecord); } } } } sc = input.nextLine(); } List<User> list = new ArrayList<>(arr); Collections.sort(list); for (User e : list){ System.out.print(e.getNumber()+" "); System.out.print(new DecimalFormat("0.0#").format(e.calCost())); System.out.print(" "); System.out.print(new DecimalFormat("0.0#").format(e.calBalance())); System.out.println(); } } } class User implements Comparable<User> { UserRecords userRecord = new UserRecords(); private double balance = 100; ChargeMode chargeMode; private String number; public User(){ } public void setBalance(double balance) { this.balance = balance; } public User(String number) { this.number = number; } public double calBalance(){ return balance - calCost() - 20; } public double calCost(){ LandPhoneInCityRule landPhoneInCityRule = new LandPhoneInCityRule(); LandPhoneInIandRule landPhoneInIandRule = new LandPhoneInIandRule(); LandPhoneInProvinceRule landPhoneInProvinceRule = new LandPhoneInProvinceRule(); return landPhoneInIandRule.calCost(userRecord.getCallingInLandRecords()) +landPhoneInCityRule.calCost(userRecord.getCallingInCityRecords()) +landPhoneInProvinceRule.calCost(userRecord.getCallingInProvinceRecords()); } public UserRecords getUserRecord(){ return userRecord; } public void setUserRecord(UserRecords userRecord){ this.userRecord = userRecord; } public double getBalance(){ return balance; } public ChargeMode getChargeMode(){ return chargeMode; } public void setChargeMode(ChargeMode chargeMode){ this.chargeMode = chargeMode; } public String getNumber(){ return number; } public void setNumber(String number){ this.number = number; } public int compareTo(User o) { return this.getNumber().compareTo(o.getNumber()); } } class UserRecords{ ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>(); ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>(); ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>(); ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>(); ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>(); ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>(); ArrayList<MessageRecord> sendMessageRecord = new ArrayList<MessageRecord>(); ArrayList<MessageRecord> receiveMessageRecord = new ArrayList<MessageRecord>(); public void addCallingInCityRecords(CallRecord callRecord){ callingInCityRecords.add(callRecord); } public void addCallingInProvinceRecords(CallRecord callRecord){ callingInProvinceRecords.add(callRecord); } public void addCallingInLandRecords(CallRecord callRecord){ callingInLandRecords.add(callRecord); } public void addAnswerInCityRecords(CallRecord callRecord){ answerInCityRecords.add(callRecord); } public void addAnswerInProvinceRecords(CallRecord callRecord){ answerInProvinceRecords.add(callRecord); } public void addAnswerInLandRecords(CallRecord callRecord){ answerInLandRecords.add(callRecord); } public void addSendMessageRecord(MessageRecord sendMessageRecord){ this.sendMessageRecord.add(sendMessageRecord); } public void addReceiveMessageRecord(MessageRecord receiveMessageRecord){ this.receiveMessageRecord.add(receiveMessageRecord); } public ArrayList<MessageRecord> getSendMessageRecord(){ return sendMessageRecord; } public ArrayList<MessageRecord> getReceiveMessageRecord(){ return receiveMessageRecord; } public ArrayList<CallRecord> getCallingInCityRecords(){ return callingInCityRecords; } public ArrayList<CallRecord> getCallingInProvinceRecords(){ return callingInProvinceRecords; } public ArrayList<CallRecord> getCallingInLandRecords(){ return callingInLandRecords; } public ArrayList<CallRecord> getAnswerInCityRecords(){ return answerInCityRecords; } public ArrayList<CallRecord> getAnswerInProvinceRecords(){ return answerInProvinceRecords; } public ArrayList<CallRecord> getAnswerInLandRecords(){ return answerInLandRecords; } } abstract class ChargeMode{ ArrayList<ChargeRule> a = new ArrayList<ChargeRule>(); public ArrayList<ChargeRule> getChargeRule(){ return a; } public void setChargeRule(ArrayList<ChargeRule> chargeRule){ this.a = a; } public double getMonthlyRent(){ return 20; } public double calCost(UserRecords userRecords){ return 0; } } class LandlinePhoneCharging extends ChargeMode{ private double monthlyRent = 20; public double calCost(UserRecords userRecords){ return monthlyRent * 1; } public double getMonthlyRent(){ return monthlyRent; } } class CommunicationRecord{ protected String callingNumber; protected String answerNumber; public String getCallingNumber(){ return callingNumber; } public void setCallingNumber(String callingNumber){ this.callingNumber = callingNumber; } public String getAnswerNumber(){ return answerNumber; } public void setAnswerNumber(String answerNumber){ this.answerNumber = answerNumber; } } class CallRecord extends ChargeRule{ private Date startTime; private Date endTime; private String callingAddressAreaCode; private String AnswerAddressAreaCode; public CallRecord(String startTime,String endTime) throws ParseException { this.startTime = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").parse(startTime); this.endTime = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").parse(endTime); } public Date getStartTime(){ return startTime; } public void setStartTime(Date startTime){ this.startTime = startTime; } public Date getEndTime(){ return endTime; } public void setEndTime(Date endTime){ this.endTime = endTime; } public String getCallingAddressAreaCode(){ return callingAddressAreaCode; } public void setCallingAddressAreaCode(String callingAddressAreaCode){ this.callingAddressAreaCode = callingAddressAreaCode; } public String getAnswerAddressAreaCode() { return AnswerAddressAreaCode; } public void setAnswerAddressAreaCode(String answerAddressAreaCode) { this.AnswerAddressAreaCode = answerAddressAreaCode; } } class MessageRecord extends ChargeRule{ private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } abstract class ChargeRule{ } abstract class CallChargeRule extends ChargeRule{ public double calCost(ArrayList<CallRecord>callRecords){ return 0; } } class LandPhoneInCityRule extends CallChargeRule{ public double calCost(ArrayList<CallRecord> callRecords){ double cost = 0; double minute; for (CallRecord e: callRecords){ minute = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime()) / 60000); cost = cost + 0.1 * minute; } return cost; } } class LandPhoneInProvinceRule extends CallChargeRule{ public double calCost(ArrayList<CallRecord> callRecords){ double cost = 0; double minute; for (CallRecord e: callRecords){ minute = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime()) / 60000); cost = cost + 0.3 * minute; } return cost; } } class LandPhoneInIandRule extends CallChargeRule{ public double calCost(ArrayList<CallRecord> callRecords){ double cost = 0; double minute; for (CallRecord e: callRecords){ minute = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime()) / 60000); cost = cost + 0.6 * minute; } return cost; } }
题目分析:
本道题目由于老师已经给出了类图,所以第一步的事情就是将所有给出的类给补全,补全之后,在去主函数中,分别利用正则表达式去判断每一段输入的字符串,之后进行容器的添加,计算容器中的花费金额。除此之外,本道题目由于采用的是多个类之间的关系,所以要将每一个类与类之间的关系理顺。
题目集七#
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
输入:
输入信息包括两种类型
1、逐行输入南昌市用户开户的信息,每行一个用户,含手机和座机用户
格式:u-号码 计费类型 (计费类型包括:0-座机 1-手机实时计费 2-手机A套餐)
例如:u-079186300001 0
座机号码由区号和电话号码拼接而成,电话号码包含7-8位数字,区号最高位是0。
手机号码由11位数字构成,最高位是1。
本题在电信计费系列1基础上增加类型1-手机实时计费。
手机设置0或者座机设置成1,此种错误可不做判断。
2、逐行输入本月某些用户的通讯信息,通讯信息格式:
座机呼叫座机:t-主叫号码 接听号码 起始时间 结束时间
t-079186330022 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:11
以上四项内容之间以一个英文空格分隔,
时间必须符合"yyyy.MM.dd HH:mm:ss"格式。提示:使用SimpleDateFormat类。
输入格式增加手机接打电话以及收发短信的格式,手机接打电话的信息除了号码之外需要额外记录拨打/接听的地点的区号,比如:
座机打手机:
t-主叫号码 接听号码 接听地点区号 起始时间 结束时间
t-079186330022 13305862264 020 2022.1.3 10:00:25 2022.1.3 10:05:11
手机互打:
t-主叫号码 拨号地点 接听号码 接听地点区号 起始时间 结束时间
t-18907910010 0791 13305862264 0371 2022.1.3 10:00:25 2022.1.3 10:05:11
注意:以上两类信息,先输入所有开户信息,再输入所有通讯信息,最后一行以“end”结束。
输出:
根据输入的详细通讯信息,计算所有已开户的用户的当月费用(精确到小数点后2位,单位元)。假设每个用户初始余额是100元。
每条通讯、短信信息均单独计费后累加,不是将所有信息累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。
错误处理:
输入数据中出现的不符合格式要求的行一律忽略。
本题只做格式的错误判断,无需做内容上不合理的判断,比如同一个电话两条通讯记录的时间有重合、开户号码非南昌市的号码等,此类情况都当成正确的输入计算。但时间的输入必须符合要求,比如不能输入2022.13.61 28:72:65。
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public class Main { public static void main(String[] args) throws ParseException { Scanner sc=new Scanner(System.in); String s; s=sc.nextLine(); //创建用户数组 List<User> users=new ArrayList<>(); List<CallRecord> communicationRecords=new ArrayList<>(); while(!s.equals("end")) { if( s.matches("[u]-([0][7]([9][0-9]|[0][1])[0-9]{7,8}) [0-3]") ||s.matches("[u]-[1][0-9]{10} [0-3]") ||s.matches("(([t]-0791[0-9]{7,8}\\s0[0-9]{9,11})|([t]-0791[0-9]{7,8}\\s1[0-9]{10}\\s0[0-9]{2,3})|([t]-1[0-9]{10}\\s0[0-9]{2,3}\\s0[0-9]{9,11})|([t]-1[0-9]{10}\\s0[0-9]{2,3}\\s1[0-9]{10}\\s0[0-9]{2,3}))(\\s\\d{4}\\.([1-9]|([1]{1}[0-2]{1}))\\.([1-9]|([1-2]{1}[0-9]{1})|3[0-1])\\s(([0-1][0-9])|(2[0-3]))\\:([0-5][0-9])\\:([0-5][0-9])){2}")==true) { //创建用户 if(s.charAt(0)=='u') { User user = LineToUser(s); int flag=0; for (User user1 : users) { if(user1.getNumber().equals(user.getNumber())) { flag=1; break; } } if(flag==0) { users.add(user); } } //创建通话记录 else if(s.charAt(0)=='t') { communicationRecords.add(LineToCallRecord(s)); } else if(s.charAt(0)=='m') { } } s=sc.nextLine(); } List<User> temp=new ArrayList<>(users); temp.sort(new Comparator<User>() { @Override public int compare(User o1, User o2) { return o1.getNumber().compareTo(o2.getNumber()); } }); for (User user : temp) { UserRecords userRecords=new UserRecords(); for (CallRecord communicationRecord : communicationRecords) { // 遍历到拨打者 if (communicationRecord.getCallingNumber().equals(user.getNumber())) { //市内 if (communicationRecord.gettype().matches("^1[1-3]$")) { userRecords.addCallingInCityRecords(communicationRecord); } else if (communicationRecord.gettype().matches("^2[1-3]$")) { userRecords.addCallingInProvinceRecords(communicationRecord); } else { userRecords.addCallingInLandRecords(communicationRecord); } } } for (CallRecord communicationRecord : communicationRecords) { //遍历到接收者 if(communicationRecord.getAnswerNumber().equals(user.getNumber())) { //市内 if(communicationRecord.gettype().matches("^[1-3]1$")) { userRecords.addAnswerInCityRecords(communicationRecord); } else if(communicationRecord.gettype().matches("^[1-3]2$")) { userRecords.addAnswerInProvinceRecords(communicationRecord); } else { userRecords.addAnswerInLandRecords(communicationRecord); } } } user.setUserRecords(userRecords); System.out.println(user.getNumber()+" "+Double.parseDouble(String.format("%.2f",user.calCost()))+" "+Double.parseDouble(String.format("%.2f",user.calBalance()))); } } private static User LineToUser(String str){ str = str.substring(2); String[] data = str.split(" "); return new User(data[0],Integer.parseInt(data[1])); } private static CallRecord LineToCallRecord(String str) throws ParseException { SimpleDateFormat format=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); str = str.substring(2); String[] s = str.split(" "); CallRecord call=new CallRecord(); if(s.length==6) { call.setCallingNumber(s[0]); call.setAnswerNumber(s[1]); String startTime=s[2]+" "+s[3]; String endTime=s[4]+" "+s[5]; Date date1=format.parse(startTime); Date date2=format.parse(endTime); call.setStartTime(date1); call.setEndTime(date2); call.setCallingAddressAreaCode(s[0].substring(0,4)); call.setAnswerAddressAreaCode(s[1].substring(0,4)); } if(s.length==7) { if(s[0].charAt(0)!='0') { call.setCallingNumber(s[0]); call.setAnswerNumber(s[2]); call.setCallingAddressAreaCode(s[1]); call.setAnswerAddressAreaCode(s[2].substring(0,4)); String startTime=s[3]+" "+s[4]; String endTime=s[5]+" "+s[6]; Date date1=format.parse(startTime); Date date2=format.parse(endTime); call.setStartTime(date1); call.setEndTime(date2); } else { call.setCallingNumber(s[0]); call.setAnswerNumber(s[1]); call.setCallingAddressAreaCode(s[0].substring(0,4)); call.setAnswerAddressAreaCode(s[2]); String startTime=s[3]+" "+s[4]; String endTime=s[5]+" "+s[6]; Date date1=format.parse(startTime); Date date2=format.parse(endTime); call.setStartTime(date1); call.setEndTime(date2); } } else if(s.length==8) { call.setCallingNumber(s[0]); call.setCallingAddressAreaCode(s[1]); call.setAnswerNumber(s[2]); call.setAnswerAddressAreaCode(s[3]); String startTime=s[4]+" "+s[5]; String endTime=s[6]+" "+s[7]; Date date1=format.parse(startTime); Date date2=format.parse(endTime); call.setStartTime(date1); call.setEndTime(date2); } return call; } } class User{ @Override public String toString() { return "User{" + "number='" + number + '\'' + ", chargeMode=" + chargeMode + ", balance=" + balance + ", userRecords=" + userRecords + '}'; } public User(String s1, int s2) { this.number=s1; if(s2==0) { this.chargeMode=new LandlinePhoneCharging(); } else if (s2 == 1) { this.chargeMode=new MobilePhoneCharging(); } } private String number; private ChargeMode chargeMode; private double balance=100; UserRecords userRecords=new UserRecords(); public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public ChargeMode getChargeMode() { return chargeMode; } public void setChargeMode(ChargeMode chargeMode) { this.chargeMode = chargeMode; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public UserRecords getUserRecords() { return userRecords; } public void setUserRecords(UserRecords userRecords) { this.userRecords = userRecords; } public double calBalance() { double monthlyRent = chargeMode.getMonthlyRent(); return this.balance-calCost()-monthlyRent; } public double calCost() { double cost = chargeMode.CalCost(getUserRecords()); return cost; } } class UserRecords{ private ArrayList<CallRecord> callingInCityRecords=new ArrayList<>(); private ArrayList<CallRecord> callingInProvinceRecords=new ArrayList<>(); private ArrayList<CallRecord> callingInLandRecords=new ArrayList<>(); private ArrayList<CallRecord> answerInCityRecords=new ArrayList<>(); private ArrayList<CallRecord> answerInProvinceRecords=new ArrayList<>(); private ArrayList<CallRecord> answerInLandRecords=new ArrayList<>(); private ArrayList<MessageRecord> sendMessageRecords=new ArrayList<>(); private ArrayList<MessageRecord> receiveMessageRecords=new ArrayList<>(); public ArrayList<CallRecord> getCallingInCityRecords() { return callingInCityRecords; } public ArrayList<CallRecord> getCallingInProvinceRecords() { return callingInProvinceRecords; } public void addCallingInCityRecords(CallRecord Records) { callingInCityRecords.add(Records); } public void addCallingInProvinceRecords(CallRecord Records) { callingInProvinceRecords.add(Records); } public void addCallingInLandRecords(CallRecord Records) { callingInLandRecords.add(Records); } public ArrayList<CallRecord> getCallingInLandRecords() { return callingInLandRecords; } public ArrayList<CallRecord> getAnswerInCityRecords() { return answerInCityRecords; } public void addAnswerInCityRecords(CallRecord Records) { this.answerInCityRecords.add(Records); } public ArrayList<CallRecord> getAnswerInProvinceRecords() { return answerInProvinceRecords; } public void addAnswerInProvinceRecords(CallRecord Records) { this.answerInProvinceRecords.add(Records); } public ArrayList<CallRecord> getAnswerInLandRecords() { return answerInLandRecords; } public void addAnswerInLandRecords(CallRecord Records) { this.answerInLandRecords.add(Records); } public ArrayList<MessageRecord> getSendMessageRecords() { return sendMessageRecords; } public void addSendMessageRecords(ArrayList<MessageRecord> sendMessageRecords) { this.sendMessageRecords = sendMessageRecords; } public ArrayList<MessageRecord> getReceiveMessageRecords() { return receiveMessageRecords; } public void addReceiveMessageRecords(ArrayList<MessageRecord> receiveMessageRecords) { this.receiveMessageRecords = receiveMessageRecords; } } abstract class ChargeMode{ private ArrayList<ChargeRule> chargeRules=new ArrayList<>(); public ArrayList<ChargeRule> getChargeRules() { return chargeRules; } public void setChargeRules(ArrayList<ChargeRule> chargeRules) { this.chargeRules = chargeRules; } public abstract double CalCost(UserRecords userRecords); public abstract double getMonthlyRent(); } class LandlinePhoneCharging extends ChargeMode{ double MonthlyRent=20; @Override public double CalCost(UserRecords userRecords) { ChargeRule InCity=new LandPhoneInCityRule(); double InCityCost = InCity.calCost(userRecords); ChargeRule InLand=new LandPhoneInlandRule(); double InLandCost = InLand.calCost(userRecords); ChargeRule InProvince=new LandPhoneInProvince(); double InProvinceCost = InProvince.calCost(userRecords); return InCityCost+InProvinceCost+InLandCost; } @Override public double getMonthlyRent() { return MonthlyRent; } } class MobilePhoneCharging extends ChargeMode{ double MonthlyRent=15; @Override public double CalCost(UserRecords userRecords) { //打出去计费+接听计费 ChargeRule InCity=new MobilePhoneInCityRule(); double InCityCost = InCity.calCost(userRecords); ChargeRule InProvince=new MobilePhoneInProvince(); double InProvinceCost = InProvince.calCost(userRecords); ChargeRule InLand=new MobilePhoneInlandRule(); double InLandCost = InLand.calCost(userRecords); return InCityCost+InProvinceCost+InLandCost; } @Override public double getMonthlyRent() { return MonthlyRent; } } abstract class CommunicationRecord{ private String callingNumber; private String answerNumber; public String getCallingNumber() { return callingNumber; } public void setCallingNumber(String callingNumber) { this.callingNumber = callingNumber; } public String getAnswerNumber() { return answerNumber; } public void setAnswerNumber(String answerNumber) { this.answerNumber = answerNumber; } } class MessageRecord extends CommunicationRecord{ private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } class CallRecord extends CommunicationRecord{ @Override public String toString() { return "CallRecord{" + "startTime=" + startTime + ", endTime=" + endTime + ", callingAddressAreaCode='" + callingAddressAreaCode + '\'' + ", answerAddressAreaCode='" + answerAddressAreaCode + '\'' + '}'; } private Date startTime; private Date endTime; private String callingAddressAreaCode; private String answerAddressAreaCode; public String gettype() { String type=""; if(callingAddressAreaCode.equals("0791")) { type=type.concat("1"); } else if ((callingAddressAreaCode.substring(0,3).equals("079")&&!callingAddressAreaCode.equals("0791"))||callingAddressAreaCode.equals("0701")) { type= type.concat("2"); }else { type= type.concat("3"); } if(answerAddressAreaCode.equals("0791")) { type=type.concat("1"); } else if ((answerAddressAreaCode.substring(0,3).equals("079")&&!answerAddressAreaCode.equals("0791"))||answerAddressAreaCode.equals("0701")) { type= type.concat("2"); }else { type=type.concat("3"); } return type; } public Date getStartTime() { return startTime; } public void setStartTime(Date startTime) { this.startTime = startTime; } public Date getEndTime() { return endTime; } public void setEndTime(Date endTime) { this.endTime = endTime; } public String getCallingAddressAreaCode() { return callingAddressAreaCode; } public void setCallingAddressAreaCode(String callingAddressAreaCode) { this.callingAddressAreaCode = callingAddressAreaCode; } public String getAnswerAddressAreaCode() { return answerAddressAreaCode; } public void setAnswerAddressAreaCode(String answerAddressAreaCode) { this.answerAddressAreaCode = answerAddressAreaCode; } } abstract class ChargeRule { public abstract double calCost(UserRecords records); } abstract class CallChargeRule extends ChargeRule{ } class LandPhoneInCityRule extends CallChargeRule{ @Override public double calCost(UserRecords records) { double sum=0; for (CallRecord callRecord :records.getCallingInCityRecords()) { Date endTime = callRecord.getEndTime(); Date startTime = callRecord.getStartTime(); double f1,f2; f2=endTime.getTime(); f1=startTime.getTime(); double res = (f2 - f1) / 1000 / 60; int res1=(int) res; if(res-res1!=0) { res1=res1+1; } if(callRecord.gettype().equals("11")) { sum+=0.1*res1; } else if (callRecord.gettype().equals("12")) { sum+=0.3*res1; } else { sum+=0.6*res1; } } return sum; } } class LandPhoneInProvince extends CallChargeRule{ @Override public double calCost(UserRecords records) { double sum=0; for (CallRecord callRecord :records.getCallingInProvinceRecords()) { Date endTime = callRecord.getEndTime(); Date startTime = callRecord.getStartTime(); double f1,f2; f2=endTime.getTime(); f1=startTime.getTime(); double res = (f2 - f1) / 1000 / 60; int res1=(int) res; if(res-res1!=0) { res1=res1+1; } sum+=0.3*res1; } return sum; } } class LandPhoneInlandRule extends CallChargeRule{ @Override public double calCost(UserRecords records) { double sum=0; for (CallRecord callRecord :records.getCallingInLandRecords()) { Date endTime = callRecord.getEndTime(); Date startTime = callRecord.getStartTime(); double f1,f2; f2=endTime.getTime(); f1=startTime.getTime(); double res = (f2 - f1) / 1000 / 60; int res1=(int) res; if(res-res1!=0) { res1=res1+1; } sum+=0.6*res1; } return sum; } } class MobilePhoneInCityRule extends CallChargeRule{ @Override public double calCost(UserRecords records) { double sum=0; for (CallRecord callRecord :records.getCallingInCityRecords()) { Date endTime = callRecord.getEndTime(); Date startTime = callRecord.getStartTime(); double f1,f2; f2=endTime.getTime(); f1=startTime.getTime(); double res = (f2 - f1) / 1000 / 60; int res1=(int) res; if(res-res1!=0) { res1=res1+1; } if(callRecord.gettype().equals("11")) { sum+=0.1*res1; } else if (callRecord.gettype().equals("12")) { sum+=0.2*res1; } else { sum+=0.3*res1; } } return sum; } } class MobilePhoneInProvince extends CallChargeRule{ @Override public double calCost(UserRecords records) { double sum=0; for (CallRecord callRecord : records.getCallingInProvinceRecords()) { Date endTime = callRecord.getEndTime(); Date startTime = callRecord.getStartTime(); double f1,f2; f2=endTime.getTime(); f1=startTime.getTime(); double res = (f2 - f1) / 1000 / 60; int res1=(int) res; if(res-res1!=0) { res1=res1+1; } sum+=0.3*res1; } return sum; } } class MobilePhoneInlandRule extends CallChargeRule{ @Override public double calCost(UserRecords records) { double sum=0; for (CallRecord callRecord : records.getCallingInLandRecords()) { Date endTime = callRecord.getEndTime(); Date startTime = callRecord.getStartTime(); double f1,f2; f2=endTime.getTime(); f1=startTime.getTime(); double res = (f2 - f1) / 1000 / 60; int res1=(int) res; if(res-res1!=0) { res1=res1+1; } sum+=0.6*res1; } for (CallRecord answer : records.getAnswerInLandRecords()) { Date endTime =answer.getEndTime(); Date startTime =answer.getStartTime(); double f1,f2; f2=endTime.getTime(); f1=startTime.getTime(); double res = (f2 - f1) / 1000 / 60; int res1=(int) res; if(res-res1!=0) { res1=res1+1; } sum+=0.3*res1; } return sum; } }
代码分析:
一、分析类图可得
1.相对于上一题只涉及固定电话,本题新增加了手机通话的模块,因此需要在原先代码的基础上增加较多模块,例如市内手机拨打电话计费模块MobilePhoneInCity类,省内手机拨打漫游计费模块MobilePhoneInProvince类,国内手机拨打漫游模块MobilePhoneInLand类来分别统计不同区域拨打手机的计费。
题目集八#
7-1 电信计费系列3-短信计费#
题目:
实现一个简单的电信计费程序,针对手机的短信采用如下计费方式:
1、接收短信免费,发送短信0.1元/条,超过3条0.2元/条,超过5条0.3元/条。
2、如果一次发送短信的字符数量超过10个,按每10个字符一条短信进行计算。
输入:
输入信息包括两种类型
1、逐行输入南昌市手机用户开户的信息,每行一个用户。
格式:u-号码 计费类型 (计费类型包括:0-座机 1-手机实时计费 2-手机A套餐 3-手机短信计费)
例如:u-13305862264 3
座机号码由区号和电话号码拼接而成,电话号码包含7-8位数字,区号最高位是0。
手机号码由11位数字构成,最高位是1。
本题只针对类型3-手机短信计费。
2、逐行输入本月某些用户的短信信息,短信的格式:
m-主叫号码,接收号码,短信内容 (短信内容只能由数字、字母、空格、英文逗号、英文句号组成)
m-18907910010 13305862264 welcome to jiangxi.
m-13305862264 18907910010 thank you.
注意:以上两类信息,先输入所有开户信息,再输入所有通讯信息,最后一行以“end”结束。
输出:
根据输入的详细短信信息,计算所有已开户的用户的当月短信费用(精确到小数点后2位,单位元)。假设每个用户初始余额是100元。
每条短信信息均单独计费后累加,不是将所有信息累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。
错误处理:
输入数据中出现的不符合格式要求的行一律忽略。
本题只做格式的错误判断,无需做内容上不合理的判断,比如同一个电话两条通讯记录的时间有重合、开户号码非南昌市的号码、自己给自己打电话等,此类情况都当成正确的输入计算。但时间的输入必须符合要求,比如不能输入2022.13.61 28:72:65。
本题只考虑短信计费,不考虑通信费用以及月租费。
mport java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) throws ParseException { Scanner input = new Scanner(System.in); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ArrayList<User> users = new ArrayList<>(); String sc = input.nextLine(); String[] a ; while (!sc.equals("end")){ int j = 0; if(sc.matches("u-1[0-9]{10} [3]")){ a = sc.substring(2).split("\\s"); if (users.size() == 0){ users.add(new User(a[0],new MessageCharging()));} else { for (int i = 0; i < users.size(); i++) { if (a[0].equals(users.get(i).getNumber())) { j = 1; break; } } if (j == 0 && users.size() != 0) users.add(new User(a[0],new MessageCharging())); } } else if(sc.matches("m-1\\d{10} 1\\d{10} (\\w|\\,|\\.| )+") || sc.matches("m-1\\d{10} 1\\d{10}")){ a = sc.substring(2).split("\\s"); for (User e:users){ if (a[0].equals(e.getNumber())) { e.getUserRecords().addSendMessageRecord(new MessageRecord(sc.substring(26,sc.length()))); } } } sc = input.nextLine(); } Collections.sort(users); for (User e:users) { System.out.print(e.getNumber()+" "); System.out.printf("%.1f",e.calCost()); System.out.print(" "); System.out.printf("%.1f",e.calBalance()); System.out.println(); } } } class User implements Comparable<User> { UserRecords userRecords = new UserRecords(); double balance = 100; ChargeMode chargeMode; ChargePhone chargePhone; String number; public User(){ } public User(String s, LandlinePhoneCharging landlinePhoneCharging) { this.number = s; } public User(String s, MobilePhoneCharging mobilePhoneCharging) { this.number = s; } public User(String s, MessageCharging messageCharging) { this.number = s; } public double calBalance(){ return balance-calCost(); } public double calCost() { MessageChargeRule messageChargeRule = new MessageChargeRule(); return messageChargeRule.calCost(userRecords.sendMessageRecord); } public UserRecords getUserRecords(){ return userRecords; } public void setUserRecords(UserRecords userRecords){ this.userRecords = userRecords; } public double getBalance(){ return balance; } public ChargeMode getChargeMode(){ return chargeMode; } public void setChargeMode(ChargeMode chargeMode){ this.chargeMode = chargeMode; } public String getNumber(){ return number; } public ChargePhone getChargePhone() { return chargePhone; } public void setNumber(String number){ this.number = number; } public int compareTo(User o) { return this.getNumber().compareTo(o.getNumber()); } } abstract class ChargeMode{ ArrayList<ChargeRule> list = new ArrayList<ChargeRule>(); public ArrayList<ChargeRule> getChargeRule(){ return list; } public void setChargeRule(ArrayList<ChargeRule> chargeRule){ this.list = list; } public double calCost(UserRecords userRecords){ return 0; } public double getMonthlyRent(){ return 0; } } abstract class ChargePhone{ ArrayList<ChargeRule> list = new ArrayList<ChargeRule>(); public ArrayList<ChargeRule> getChargeRule(){ return list; } public void setChargeRule(ArrayList<ChargeRule> chargeRule){ this.list = list; } public double calCost(UserRecords userRecords){ return 0; } public double getMonthlyRent(){ return 15; } } abstract class ChargeMessage{ ArrayList<ChargeRule> list = new ArrayList<ChargeRule>(); public ArrayList<ChargeRule> getChargeRule(){ return list; } public void setChargeRule(ArrayList<ChargeRule> chargeRule){ this.list = list; } public double calCost(UserRecords userRecords){ return 0; } public double getMonthlyRent(){ return 0; } } class UserRecords{ ArrayList<MessageRecord> sendMessageRecord = new ArrayList<MessageRecord>(); ArrayList<MessageRecord> receiveMessageRecord = new ArrayList<MessageRecord>(); public void addSendMessageRecord(MessageRecord sendMessageRecord){ this.sendMessageRecord.add(sendMessageRecord); } public void addReceiveMessageRecord(MessageRecord receiveMessageRecord){ this.receiveMessageRecord.add(receiveMessageRecord); } public ArrayList<MessageRecord> getSendMessageRecord(){ return sendMessageRecord; } public ArrayList<MessageRecord> getReceiveMessageRecord(){ return receiveMessageRecord; } } class LandlinePhoneCharging extends ChargeMode{ double monthlyRent = 20; public double calCost(UserRecords userRecords){ return 0; } public double getMonthlyRent(){ return monthlyRent; } } class MobilePhoneCharging extends ChargePhone{ double monthlyRent = 15; public double calCost(UserRecords userRecords){ return 0; } public double getMonthlyRent(){ return monthlyRent; } } class MessageCharging extends ChargeMessage{ double monthlyRent = 0; public double calCost(UserRecords userRecords){ return 0; } public double getMonthlyRent(){ return monthlyRent; } } class CommunicationRecord{ String callingNumber; String answerNumber; public String getCallingNumber(){ return callingNumber; } public void setCallingNumber(String callingNumber){ this.callingNumber = callingNumber; } public String getAnswerNumber(){ return answerNumber; } public void setAnswerNumber(String answerNumber){ this.answerNumber = answerNumber; } } class CallRecord extends CommunicationRecord{ Date startTime; Date endTime; String callingAddressAreaCode; String AnswerAddressAreaCode; public CallRecord(String startTime,String endTime) throws ParseException { this.startTime = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").parse(startTime); this.endTime = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").parse(endTime); } public Date getStartTime(){ return startTime; } public void setStartTime(Date startTime){ this.startTime = startTime; } public Date getEndTime(){ return endTime; } public void setEndTime(Date endTime){ this.endTime = endTime; } public String getCallingAddressAreaCode(){ return callingAddressAreaCode; } public void setCallingAddressAreaCode(String callingAddressAreaCode){ this.callingAddressAreaCode = callingAddressAreaCode; } public String getAnswerAddressAreaCode() { return AnswerAddressAreaCode; } public void setAnswerAddressAreaCode(String answerAddressAreaCode) { this.AnswerAddressAreaCode = answerAddressAreaCode; } } class MessageRecord extends CommunicationRecord{ String message; public MessageRecord(String message) { this.message = message; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } class SendMessageRule extends MessageChargeRule{ @Override public double calCost(ArrayList<MessageRecord> messageRecords) { return super.calCost(messageRecords); } } abstract class ChargeRule{ } abstract class CallChargeRule { public double calCost(ArrayList<CallRecord>callRecords){ return 0; } } class MessageChargeRule extends ChargeRule{ public double calCost(ArrayList<MessageRecord> messageRecords) { double cost = 0; double count = 0; for (int i = 0; i < messageRecords.size(); i++) { double a = messageRecords.get(i).message.length(); while (a > 0){ count++; a = a - 10; } } if(count <= 3){ cost = 0.1 * count; } else if(count > 3 && count <= 5){ cost = 0.3 + 0.2 * (count - 3); } else if(count > 5){ cost = 0.7 + 0.3 * ( count - 5) ; } return cost; } }
题目分析:
本道题目由于和之前收费并无关系,所以可以将之前很多的代码进行删除,修改,最后进行一个关于短信的收费计算即可,由于其类之间的关系基本没有变化,所以只需要在main函数里面进行修改即可。
采坑心得:
本道题目关于如何将短信收费的计算做好是非常重要的。
7-2 编写一个类Shop(商店)、内部类InnerCoupons(内部购物券)
5.总结
一、作业总结:
这几次作业总的来说难度适中,不过相较于之前的多边形大作业更侧重数学方面,这几次作业更加侧重的是对java特性的考察以及学生对于类图的还原能力和扩展能力,题目本身逻辑并不复杂。通过编写并调试代码,我对于java面向对象的特性又有了更进一步的认识,也体会到了“程序设计”的“设计”的重要性。有时候,一个合理的系统设计将对程序的可移植性、可复用性、易修改性产生巨大的正面影响,反之一个糟糕的设计,将对后期编程与维护产生巨大的阻碍。从期中考试开始算起到现在的这六周,我主要复习了接口的编程思想、lambda表达式的使用方法以及学习了javaFx的基本编程方法,并完后了农夫过河的图形化界面设计。之后又学习了java线程编程的原理,完成了生产者消费者这一经典线程模型的java复现,最后还学习了一些java程序设计模式的思想与理念,可以说是收获颇丰了,在学习方面,我主要对java程序设计思想的认识还不够深刻,对于设计模式的运用还不够纯熟,希望能在日后的编程中逐渐补齐这一短板。
标签:return,String,double,ArrayList,BLOG,new,public From: https://www.cnblogs.com/nchu-y/p/16971501.html