一、前言
1、题目集6-8
难度适中,电信计费给出了相应类图,根据类图构建好大致框架后只需填充一些方法、完善类的功能就行,其他题目难度不大,但需要注意题目要求。这三次题目题量不大,主要工作量集中于电信计费。涉及的知识点有,抽象类的使用、子类对父类方法的复用实现(继承多态)、内部类的使用方法、正则表达式验证格式、Arraylist数组的使用、字符串的处理的相关库函数、迭代器的使用等等。
二、题目具体分析
1、题目集6/题目7-1:电信计费系列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元。
每条通讯信息单独计费后累加,不是将所有时间累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。
错误处理:
输入数据中出现的不符合格式要求的行一律忽略。
(2)设计与分析
1、根据题目给出的类图进行基础框架编程:
上图中User是用户类,包括属性:
userRecords (用户记录)、balance(余额)、chargeMode(计费方式)、number(号码)。
ChargeMode是计费方式的抽象类:
chargeRules是计费方式所包含的各种计费规则的集合,ChargeRule类的定义见图3。
getMonthlyRent()方法用于返回月租(monthlyRent)。
UserRecords是用户记录类,保存用户各种通话、短信的记录,
各种计费规则将使用其中的部分或者全部记录。
其属性从上到下依次是:
市内拨打电话、省内(不含市内)拨打电话、省外拨打电话、
市内接听电话、省内(不含市内)接听电话、省外接听电话的记录
以及发送短信、接收短信的记录。
上图中CommunicationRecord是抽象的通讯记录类:
包含callingNumber拨打号码、answerNumber接听号码两个属性。
CallRecord(通话记录)、MessageRecord(短信记录)是它的子类。
CallRecord(通话记录类)包含属性:
通话的起始、结束时间以及
拨号地点的区号(callingAddressAreaCode)、接听地点的区号(answerAddressAreaCode)。
区号用于记录在哪个地点拨打和接听的电话。座机无法移动,就是本机区号,如果是手机号,则会有差异。
上图是计费规则的相关类,这些类的核心方法是:
calCost(ArrayList<CallRecord> callRecords)。
该方法针根据输入参数callRecords中的所有记录计算某用户的某一项费用;如市话费。
输入参数callRecords的约束条件:必须是某一个用户的符合计费规则要求的所有记录。
LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三个类分别是
座机拨打市内、省内、省外电话的计费规则类,用于实现这三种情况的费用计算。
(提示:可以从UserRecords类中获取各种类型的callRecords)。
2、SourceMonitor生成图
根据下图可知,该代码的结构处于一个较为合理的状态,相对而言最大复杂度和平均深度有些许出线
可知最大复杂度和最大深度均出现在主函数中:
把主函数代码单独拎出来看,可以发现原因是主函数中的4个循环以及循环中的判断,可以通过将输入输出单列为Input类和Output类来减小主函数的圈复杂度:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class Main{ public static void main(String[] args){ Scanner input = new Scanner(System.in); ArrayList<User> list = new ArrayList<>();//用户列表 String s = input.nextLine(); GeshiCheck check = new GeshiCheck(); writeMessage writein = new writeMessage(); while(!s.equals("end")){//输入未结束 int flag = check.inputSelect(s);//0为用户信息,1为用户记录,2为不合法格式(直接忽略) if(flag==0){ writein.writeUsers(list, s); } else if(flag==1){ writein.writeUserRecords(list, s); } s = input.nextLine();//继续读入下一行 }//信息录入完毕 //计算余额 for(User u : list){//对每个用户进行计算 u.calCost(); u.calBalance(); } for(int i=0;i<list.size();i++){ //对用户进行按号码排序 for(int j=0;j<list.size()-1;j++){ if(list.get(j).getNumber().compareTo(list.get(j+1).getNumber()) > 0){ Collections.swap(list, j, j+1); } } } //输出信息 for(User u : list){ System.out.println(u.getNumber()+" "+String.format("%.1f",u.calCost())+" "+String.format("%.1f",u.getBalance())); } }//main }//Main主函数
(3)踩坑心得
1、写完码后一运行发现单个用户的没有问题,然后多用户的全错,不能理解!通过几次测试后突然发现 是排序出的问题,在主函数排序时只有一层循环,误解了collection的用法,加上里边的循环就过了。
2、对正则表达式的测试花了挺多时间的,一开始通话记录匹配那里写的是t-[0-9]{10,11}结果有几个点过不去,然后就改成了{10,12},没发现问题出在哪......
用户匹配:s.matches("u-079[0-9][0-9]{7,8}\\s0") || s.matches("u-0701[0-9]{7,8}\\s0")
通话记录匹配:s.matches("t-[0-9]{10,12}\\s[0-9]{10,12}\\s[0-9]{4}.([1-9]||1[0-2]).([1-9]||[1-2][0-9]||3[01])\\s[0-9]{2}.[0-9]{2}.[0-9]{2}\\s[0-9]{4}.([1-9]||1[0-2]).([1-9]||[1-2][0-9]||3[01])\\s[0-9]{2}.[0-9]{2}.[0-9]{2}")
(4)改进建议
主函数内容过于复杂,应另创几个类分担一下Main类的工作。
(5)实现代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.util.Scanner; import java.util.ArrayList; import java.util.Date; import java.text.SimpleDateFormat; import java.text.ParseException; import java.util.Collections; class GeshiCheck{//输入格式检查 public int inputSelect(String s){ if(s.matches("u-079[0-9][0-9]{7,8}\\s0") || s.matches("u-0701[0-9]{7,8}\\s0")){//用户,0 return 0; } else if(s.matches("t-[0-9]{10,12}\\s[0-9]{10,12}\\s[0-9]{4}.([1-9]||1[0-2]).([1-9]||[1-2][0-9]||3[01])\\s[0-9]{2}.[0-9]{2}.[0-9]{2}\\s[0-9]{4}.([1-9]||1[0-2]).([1-9]||[1-2][0-9]||3[01])\\s[0-9]{2}.[0-9]{2}.[0-9]{2}")){//用户记录,1 return 1; } return 2;//不合法,跳过 } }//GeshiCheck class writeMessage{//信息写入 public void writeUsers(ArrayList<User> list, String s){//用户 User newuser = new User();//新用户对象 String tokens[] = s.split(" "); newuser.setNumber(tokens[0].substring(2));//截取下标2以后的字符串 //确保该用户之前不存在 int count = 0; for(User u : list){ if(u.getNumber().equals(newuser.getNumber())){ count++; break; } } if(count==0){//添加新用户 list.add(newuser); } } public void writeUserRecords(ArrayList<User> list, String s){//用户记录 String tokens[] = s.split(" "); String startTime = tokens[2]+" "+tokens[3]; String endTime = tokens[4]+" "+tokens[5]; RiQiCheck check1 = new RiQiCheck(); Date startTime1 = new Date(); Date endTime1 = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); sdf.setLenient(false);//强校验格式 try{ startTime1 = sdf.parse(startTime); endTime1 = sdf.parse(endTime); }catch(Exception e){} if(check1.riQiCheck(startTime) && check1.riQiCheck(endTime)){//时间合法 for(User u : list){ if(u.getNumber().equals(tokens[0].substring(2))){//打电话的用户存在 if(tokens[1].matches("0791[0-9]{7,8}")){//市内 CallRecord callRecord = new CallRecord(startTime1, endTime1, tokens[0].substring(2), tokens[1]); u.getUserRecords().addCallingInCityRecords(callRecord); } else if(tokens[1].matches("0701[0-9]{7,8}") || tokens[1].matches("079[0-9][0-9]{7,8}")){//省内 CallRecord callRecord = new CallRecord(startTime1, endTime1, tokens[0].substring(2), tokens[1]); u.getUserRecords().addCallingInProvinceRecords(callRecord); } else{//省外 CallRecord callRecord = new CallRecord(startTime1, endTime1, tokens[0].substring(2), tokens[1]); u.getUserRecords().addCallingInLandRecords(callRecord); } } } } } }//writeMessage class RiQiCheck{//日期合法检查 public boolean riQiCheck(String str){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");//"HH":24小时制 sdf.setLenient(false);//强校验格式 try{ sdf.parse(str); }catch(Exception e){ return false; } return true; } }//RiQiCheck public class Main{ public static void main(String[] args){ Scanner input = new Scanner(System.in); ArrayList<User> list = new ArrayList<>();//用户列表 String s = input.nextLine(); GeshiCheck check = new GeshiCheck(); writeMessage writein = new writeMessage(); while(!s.equals("end")){//输入未结束 int flag = check.inputSelect(s);//0为用户信息,1为用户记录,2为不合法格式(直接忽略) if(flag==0){ writein.writeUsers(list, s); } else if(flag==1){ writein.writeUserRecords(list, s); } s = input.nextLine();//继续读入下一行 }//信息录入完毕 //计算余额 for(User u : list){//对每个用户进行计算 u.calCost(); u.calBalance(); } for(int i=0;i<list.size();i++){ //对用户进行按号码排序 for(int j=0;j<list.size()-1;j++){ if(list.get(j).getNumber().compareTo(list.get(j+1).getNumber()) > 0){ Collections.swap(list, j, j+1); } } } //输出信息 for(User u : list){ System.out.println(u.getNumber()+" "+String.format("%.1f",u.calCost())+" "+String.format("%.1f",u.getBalance())); } }//main }//Main class User{ private UserRecords userRecords = new UserRecords();//用户记录 private double balance = 100;//余额 private ChargeMode chargeMode;//计费方式 private String number;//号码 public User(){ this.chargeMode = new LandlinePhoneCharging(); } public double calCost(){//打电话花费金额 return getChargeMode().calCost(getUserRecords()); } public double calBalance(){//打完电话剩下金额 balance = getBalance() - calCost() - getChargeMode().getMonthlyRent(); return getBalance() - getChargeMode().getMonthlyRent() - calCost(); } public double getBalance(){ return balance; } public UserRecords getUserRecords(){ return userRecords; } public void setUserRecords(UserRecords userRecords){ this.userRecords = userRecords; } 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; } }//User 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 void addCallingInCityRecords(CallRecord callRecord){//市内,拨打 callingInCityRecords.add(callRecord); } public ArrayList<CallRecord> getCallingInCityRecords(){ return callingInCityRecords; } public void addCallingInProvinceRecords(CallRecord callRecord){//省内,拨打 callingInProvinceRecords.add(callRecord); } public ArrayList<CallRecord> getCallingInProvinceRecords(){ return callingInProvinceRecords; } public void addCallingInLandRecords(CallRecord callRecord){//省外,拨打 callingInLandRecords.add(callRecord); } public ArrayList<CallRecord> getCallingInLandRecords(){ return callingInLandRecords; } public void addAnswerInCityRecords(CallRecord answerRecord){//市内,接 } public ArrayList<CallRecord> getAnswerInCityRecords(){ return answerInCityRecords; } public void addAnswerInProvinceRecords(CallRecord answerRecord){//省内,接 } public ArrayList<CallRecord> getAnswerInProvinceRecords(){ return answerInProvinceRecords; } public void addAnswerInLandRecords(CallRecord answerRecord){//省外,接 } public ArrayList<CallRecord> getAnswerInLandRecords(){ return answerInLandRecords; } public void addSendMessageRecords(MessageRecord sendMessageRecord){//发送信息 } public ArrayList<MessageRecord> getSendMessageRecords(){ return sendMessageRecords; } public void addReceiveMessageRecords(MessageRecord receiveMessageRecord){//接收信息 } public ArrayList<MessageRecord> getReceiveMessageRecords(){ return receiveMessageRecords; } }//UserRecords abstract class ChargeMode{//计费方式的抽象类 private ArrayList<ChargeRule> chargeRules = new ArrayList<>(); public ArrayList<ChargeRule> getChargeRules(){ return chargeRules; } public void setChargeRules(ArrayList<ChargeRule> chargeRules){ } public double calCost(UserRecords userRecords){ return 0; } public double getMonthlyRent(){ return 0; } }//ChargeMode abstract 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; } }//CommunicationRecord class CallRecord extends CommunicationRecord{//通话记录 private Date startTime;//通话起始时间 private Date endTime;//通话结束时间 private String callingAddressAreaCode;//拨号地点的区号 private String answerAddressAreaCode;//接听地点的区号 public CallRecord(Date startTime, Date endTime, String s1, String s2){//构造方法 this.startTime = startTime; this.endTime = endTime; this.callingAddressAreaCode = s1.substring(0,4); this.answerAddressAreaCode = s2.substring(0,4); this.callingNumber = s1.substring(4); this.answerNumber = s2.substring(4); } 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; } }//CallRecord class MessageRecord extends CommunicationRecord{//短信记录 private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }//MessageRecord abstract class ChargeRule{} abstract class CallChargeRule extends ChargeRule{ public double calCost (ArrayList<CallRecord> callRecords){ return 0; } }//CallChargeRule class LandPhoneInCityRule extends CallChargeRule{ @Override public double calCost (ArrayList<CallRecord> callRecords){ double sum = 0; for(CallRecord c : callRecords){ //c.getEndTime().getTime()-c.getStartTime().getTime()可得到以毫秒为单位的时间差 sum += 0.1 * ( (((c.getEndTime().getTime()-c.getStartTime().getTime())/1000)+59) / 60); } return sum; } }//LandPhoneInCityRule class LandPhoneInProvinceRule extends CallChargeRule{ @Override public double calCost (ArrayList<CallRecord> callRecords){ double sum = 0; for(CallRecord c : callRecords){ //c.getEndTime().getTime()-c.getStartTime().getTime()可得到以毫秒为单位的时间差 sum += 0.3 * ( (((c.getEndTime().getTime()-c.getStartTime().getTime())/1000)+59) / 60); } return sum; } }//LandPhoneInProvinceRule class LandPhoneInlandRule extends CallChargeRule{ @Override public double calCost (ArrayList<CallRecord> callRecords){ double sum = 0; for(CallRecord c : callRecords){ //c.getEndTime().getTime()-c.getStartTime().getTime()可得到以毫秒为单位的时间差 sum += 0.6 * ( (((c.getEndTime().getTime()-c.getStartTime().getTime())/1000)+59) / 60); } return sum; } }//LandPhoneInlandRule class LandlinePhoneCharging extends ChargeMode{ private double monthlyRent = 20; @Override public double getMonthlyRent(){ return monthlyRent; } @Override public double calCost(UserRecords userRecords) { return new LandPhoneInCityRule().calCost(userRecords.getCallingInCityRecords()) + new LandPhoneInProvinceRule().calCost(userRecords.getCallingInProvinceRecords()) + new LandPhoneInlandRule().calCost(userRecords.getCallingInLandRecords()); } }//LandlinePhoneChargingView Code
2、题目集6/题目7-2:多态测试
(1)题目详情
定义容器Container接口。模拟实现一个容器类层次结构,并进行接口的实现、抽象方法重写和多态机制测试。各容器类实现求表面积、体积的方法。
- 定义接口Container:
属性:
public static final double pi=3.1415926;
抽象方法:
public abstract double area();
public abstract double volume();
static double sumofArea(Container c[]);
static double sumofVolume(Container c[]);
其中两个静态方法分别计算返回容器数组中所有对象的面积之和、周长之和; - 定义Cube类、Cylinder类均实现自Container接口。
Cube类(属性:边长double类型)、Cylinder类(属性:底圆半径、高,double类型)。
输入格式:
第一行n表示对象个数,对象类型用cube、cylinder区分,cube表示立方体对象,后面输入边长,输入cylinder表示圆柱体对象,后面是底圆半径、高。
输出格式:
分别输出所有容器对象的表面积之和、体积之和,结果保留小数点后2位。
(2)设计与分析
1、类图
类图较为简单清晰,主要在于对接口的理解和使用以及对抽象方法的重写以实现接口下的多态。
2、SourceMonitor生成图
本题比较简单,从生成图可以看出本题的简单程度。
(3)踩坑心得
虽说题目简单,但还是在一些地方出了差错,读入数据时出现了问题,将nextInt()和nextLine()混用了,会发现读取的数据变少了,解决办法是将nextLine()改为next()。nextLine()的使用还是很需要注意的,nextLine()会自动读取了前面被nextInt()去掉的Enter作为它的结束符,nextDouble() ,nextFloat() ,nextInt() 等与nextLine()连用时都存在这个问题,如果需要读取带空格的一整行的话,可以考虑在nextDouble() ,nextFloat() ,nextInt()后先用一个nextLine()吃掉enter符,在用nextLine()读取下一行。很明显本题不需读取带空格的一整行,所以直接用next()替换就好。
(4)改进建议
没有,题目简单。
(5)实现代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.util.Scanner; interface Container{//接口 public static final double pi=3.1415926; //抽象方法 public abstract double area();//面积 public abstract double volume();//体积 static double sumofArea(Container c[]){ double sum = 0; for(int i=0;i<c.length;i++){ sum += c[i].area(); } return sum; } static double sumofVolume(Container c[]){ double sum = 0; for(int i=0;i<c.length;i++){ sum += c[i].volume(); } return sum; } } class Cylinder implements Container{//圆柱体 private double radius; private double hight; public Cylinder(double a, double b){//构造方法 this.radius = a; this.hight = b; } @Override public double area(){//表面积 return 2 * pi * radius * hight + 2 * pi * radius * radius; } @Override public double volume(){//体积 return pi * radius * radius * hight; } } class Cube implements Container{//立方体 private double side; public Cube(double a){//构造方法 this.side = a; } @Override public double area(){//表面积 return side * side * 6; } @Override public double volume(){//体积 return side * side * side; } } public class Main{ public static void main(String[] args){ Scanner input = new Scanner(System.in); int n = input.nextInt();//容器数量 Object[] container = new Object[n]; for(int i=0;i<n;i++){ String s = input.next(); if(s.equals("cube")){//立方体 container[i] = new Cube(input.nextDouble()); } if(s.equals("cylinder")){//圆柱体 container[i] = new Cylinder(input.nextDouble(), input.nextDouble()); } } double sum1 = 0, sum2 = 0; for(int i=0;i<n;i++){ if(container[i] instanceof Container){ sum1 += (((Container)container[i]).area()); sum2 += (((Container)container[i]).volume()); } } System.out.println(String.format("%.2f",sum1)); System.out.print(String.format("%.2f",sum2)); }//main }//MainView Code
3、题目集7/题目7-1:电信计费系列2-手机+座机计费
(1)题目详情
实现南昌市电信分公司的计费程序,假设该公司针对手机和座机用户分别采取了两种计费方案,分别如下:
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元。
每条通讯、短信信息均单独计费后累加,不是将所有信息累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。
错误处理:
输入数据中出现的不符合格式要求的行一律忽略。
(2)设计与分析
1、类图
前两个类图于与电信1相同,第三个有点变化
上图是计费规则的相关类,这些类的核心方法是:
calCost(ArrayList<CallRecord> callRecords)。
该方法针根据输入参数callRecords中的所有记录计算某用户的某一项费用;如市话费。
输入参数callRecords的约束条件:必须是某一个用户的符合计费规则要求的所有记录。
SendMessageRule是发送短信的计费规则类,用于计算发送短信的费用。
LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三个类分别是座机拨打市内、省内、省外电话的计费规则类,用于实现这三种情况的费用计算。
2、SourceMonitor生成图
由SourceMonitor图可知最大复杂度过大了,超出了正常范围太多:
复杂度这么高,原因是用户记录的格式校验和用户记录的登记放在了一起,虽然另创了一个输入处理类,但分工还不够明确,校验还是得用单独一个类来实现,因为首先正则的情况很多,其次是时间上的校验较为复杂。
(3)改进建议
类的设计还需要得到改进,任务的分工不够明确,导致一个类显得格外庞大,校验类还是需要保留,不应该合并到输入类中。
(4)实现代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.util.ArrayList; import java.util.Comparator; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.text.ParseException; public class Main { public static void main(String[] args) { Outputtool outputtool = new Outputtool(); Inputdeal inputdeal = new Inputdeal(); ArrayList<User> users = new ArrayList<>(); Scanner in = new Scanner(System.in); String input = in.nextLine(); while (!input.equals("end")) { if (1 == inputdeal.check(input)) { inputdeal.writeUser(users, input); } else if (2 == inputdeal.check(input)) { inputdeal.writeRecord(users, input); } input = in.nextLine(); } users.sort(new Comparator<User>() { @Override public int compare(User u1, User u2) { if (u1.getNumber().charAt(0) == '0' && u2.getNumber().charAt(0) != '0') { return -1; } else if (u1.getNumber().charAt(0) != '0' && u2.getNumber().charAt(0) == '0') { return 1; } if (Double.parseDouble(u1.getNumber()) > Double.parseDouble(u2.getNumber())) { return 1; } else { return -1; } } }); for (User u : users) { System.out.print(u.getNumber() + " "); outputtool.output(u.calCost()); System.out.print(" "); outputtool.output(u.calBalance()); System.out.println(); } } } abstract class ChargeMode { protected ArrayList<ChargeRule> chargeRules = new ArrayList<>(); public abstract double calCost(UserRecords userRecords); public abstract double getMonthlyRent(); public ArrayList<ChargeRule> getChargeRules() { return chargeRules; } public void setChargeRules(ArrayList<ChargeRule> chargeRules) { this.chargeRules = chargeRules; } } class UserRecords { private ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>(); private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>(); private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>(); private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>(); public void addCallingInCityRecords(CallRecord callRecord) { callingInCityRecords.add(callRecord); } public void addCallingInProvinceRecords(CallRecord callRecord) { callingInProvinceRecords.add(callRecord); } public void addCallingInLandRecords(CallRecord callRecord) { callingInLandRecords.add(callRecord); } public void addAnswerInCityRecords(CallRecord callRecord) { answerInCityRecords.add(callRecord); } public void aaddAnswerInProvinceRecords(CallRecord callRecord) { answerInProvinceRecords.add(callRecord); } public void addAnswerInLandRecords(CallRecord callRecord) { answerInLandRecords.add(callRecord); } public void addSendMessageRecords(MessageRecord callRecord) { sendMessageRecords.add(callRecord); } public void addReceiveMessageRecords(MessageRecord callRecord) { receiveMessageRecords.add(callRecord); } public ArrayList<CallRecord> getCallingInCityRecords() { return callingInCityRecords; } public void setCallingInCityRecords(ArrayList<CallRecord> callingInCityRecords) { this.callingInCityRecords = callingInCityRecords; } public ArrayList<CallRecord> getCallingInProvinceRecords() { return callingInProvinceRecords; } public void setCallingInProvinceRecords(ArrayList<CallRecord> callingInProvinceRecords) { this.callingInProvinceRecords = callingInProvinceRecords; } public ArrayList<CallRecord> getCallingInLandRecords() { return callingInLandRecords; } public void setCallingInLandRecords(ArrayList<CallRecord> callingInLandRecords) { this.callingInLandRecords = callingInLandRecords; } public ArrayList<CallRecord> getAnswerInCityRecords() { return answerInCityRecords; } public void setAnswerInCityRecords(ArrayList<CallRecord> answerInCityRecords) { this.answerInCityRecords = answerInCityRecords; } public ArrayList<CallRecord> getAnswerInProvinceRecords() { return answerInProvinceRecords; } public void setAnswerInProvinceRecords(ArrayList<CallRecord> answerInProvinceRecords) { this.answerInProvinceRecords = answerInProvinceRecords; } public ArrayList<CallRecord> getAnswerInLandRecords() { return answerInLandRecords; } public void setAnswerInLandRecords(ArrayList<CallRecord> answerInLandRecords) { this.answerInLandRecords = answerInLandRecords; } public ArrayList<MessageRecord> getSendMessageRecords() { return sendMessageRecords; } public void setSendMessageRecords(ArrayList<MessageRecord> sendMessageRecords) { this.sendMessageRecords = sendMessageRecords; } public ArrayList<MessageRecord> getReceiveMessageRecords() { return receiveMessageRecords; } public void setReceiveMessageRecords(ArrayList<MessageRecord> receiveMessageRecords) { this.receiveMessageRecords = receiveMessageRecords; } } class LandlinePhoneCharging extends ChargeMode { private double monthlyRent = 20; public LandlinePhoneCharging() { super(); chargeRules.add(new LandPhoneInCityRule()); chargeRules.add(new LandPhoneInProvinceRule()); chargeRules.add(new LandPhoneInlandRule()); } @Override public double calCost(UserRecords userRecords) { double sumCost = 0; for (ChargeRule rule : chargeRules) { sumCost += rule.calCost(userRecords); } return sumCost; } @Override public double getMonthlyRent() { return monthlyRent; } } class MobilePhoneCharging extends ChargeMode { private double monthlyRent = 15; public MobilePhoneCharging() { super(); chargeRules.add(new MobilePhoneInCityRule()); chargeRules.add(new MobilePhoneInProvinceRule()); chargeRules.add(new MobilePhoneInlandRule()); } @Override public double calCost(UserRecords userRecords) { double sumCost = 0; for (ChargeRule rule : chargeRules) { sumCost += rule.calCost(userRecords); } return sumCost; } @Override public double getMonthlyRent() { return monthlyRent; } } class Inputdeal { public int check(String input) { if (input.matches("[u]-0791[0-9]{7,8}\\s[0]") || input.matches("[u]-1[0-9]{10}\\s[1]")) { return 1; // } else if (input.charAt(0) == 'm') { // return 2; } else if (input.matches("(([t]-0791[0-9]{7,8}\\s" + "0[0-9]{9,11}\\s)|" + "([t]-0791[0-9]{7,8}\\s" + "1[0-9]{10}\\s" + "0[0-9]{2,3}\\s)|" + "([t]-1[0-9]{10}\\s" + "0[0-9]{2,3}\\s" + "0[0-9]{9,11}\\s)|" + "([t]-1[0-9]{10}\\s" + "0[0-9]{2,3}\\s" + "1[0-9]{10}\\s" + "0[0-9]{2,3}\\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])")) { return 2; } return 0; } @SuppressWarnings("unused") private boolean validatet(String string) { if (!string.matches("^([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$")) { return false; } return true; } public static boolean validate(String dateString) { // 使用正则表达式 测试 字符 符合 dddd.dd.dd 的格式(d表示数字) Pattern p = Pattern.compile("\\d{4}+[\\.]\\d{1,2}+[\\.]\\d{1,2}+"); Matcher m = p.matcher(dateString); if (!m.matches()) { return false; } // 得到年月日 String[] array = dateString.split("\\."); int year = Integer.valueOf(array[0]); int month = Integer.valueOf(array[1]); int day = Integer.valueOf(array[2]); if (month < 1 || month > 12) { return false; } int[] monthLengths = new int[] { 0, 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (isLeapYear(year)) { monthLengths[2] = 29; } else { monthLengths[2] = 28; } int monthLength = monthLengths[month]; if (day < 1 || day > monthLength) { return false; } return true; } /** 是否是闰年 */ private static boolean isLeapYear(int year) { return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0); } public boolean judge(String input) { return false; } public void writeUser(ArrayList<User> users, String input) { User usernew = new User(); String[] inputs = input.split(" "); String num = inputs[0].substring(2); for (User i : users) { if (i.getNumber().equals(num)) { return; } } usernew.setNumber(num); int mode = Integer.parseInt(inputs[1]); if (mode == 0) { usernew.setChargeMode(new LandlinePhoneCharging()); } else if (mode == 1) { usernew.setChargeMode(new MobilePhoneCharging()); } users.add(usernew); } public void writeRecord(ArrayList<User> users, String input) { String[] inputs = input.split(" "); User callu = null, answeru = null; CallRecord callrecord = new CallRecord(inputs); if (input.charAt(0) == 't') { String out = inputs[0]; String in = ""; if (inputs.length == 6) { in = inputs[1]; } else if (inputs.length == 7) { in = inputs[1]; } else if (inputs.length == 8) { in = inputs[2]; } for (User i : users) { if (i.getNumber().equals(out)) { callu = i; } if (i.getNumber().equals(in)) { answeru = i; } if (callu != null && answeru != null) { break; } } if (callu != null) { if (callrecord.getCallType().matches("^1[1-3]$")) { callu.getUserRecords().addCallingInCityRecords(callrecord); } else if (callrecord.getCallType().matches("^2[1-3]$")) { callu.getUserRecords().addCallingInProvinceRecords(callrecord); } else { callu.getUserRecords().addCallingInLandRecords(callrecord); } } if (answeru != null) { if (callrecord.getCallType().matches("^[1-3]1$")) { answeru.getUserRecords().addAnswerInCityRecords(callrecord); } else if (callrecord.getCallType().matches("^[1-3]2$")) { answeru.getUserRecords().aaddAnswerInProvinceRecords(callrecord); } else { answeru.getUserRecords().addAnswerInLandRecords(callrecord); } } } else if (input.charAt(0) == 'm') { } } } abstract class CommunicationRecord { protected String callingNumber; protected String answerNumbe; public String getCallingNumber() { return callingNumber; } public void setCallingNumber(String callingNumber) { this.callingNumber = callingNumber; } public String getAnswerNumbe() { return answerNumbe; } public void setAnswerNumbe(String answerNumbe) { this.answerNumbe = answerNumbe; } } abstract class ChargeRule { abstract public double calCost(UserRecords userRecords); } class CallRecord extends CommunicationRecord { private Date startTime; private Date endTime; private String callingAddressAreaCode; private String answerAddressAreaCode; public String getCallType() { String type = ""; if (callingAddressAreaCode.equals("0791")) { type = type.concat("1"); } else if (callingAddressAreaCode.matches("^079[023456789]$") || callingAddressAreaCode.equals("0701")) { type = type.concat("2"); } else { type = type.concat("3"); } if (answerAddressAreaCode.equals("0791")) { type = type.concat("1"); } else if (answerAddressAreaCode.matches("^079[023456789]$") || answerAddressAreaCode.equals("0701")) { type = type.concat("2"); } else { type = type.concat("3"); } return type; } public CallRecord(String[] inputs) { super(); char type = inputs[0].charAt(0); inputs[0] = inputs[0].substring(2); String sd = null, st = null, ed = null, et = null; if (type == 't') { if (inputs.length == 6) { sd = inputs[2]; st = inputs[3]; ed = inputs[4]; et = inputs[5]; callingAddressAreaCode = inputs[0].substring(0, 4); answerAddressAreaCode = inputs[1].substring(0, 4); } else if (inputs.length == 7) { sd = inputs[3]; st = inputs[4]; ed = inputs[5]; et = inputs[6]; if (inputs[0].charAt(0) != '0') { if (inputs[2].length() == 10) { answerAddressAreaCode = inputs[2].substring(0, 3); } else { answerAddressAreaCode = inputs[2].substring(0, 4); } callingAddressAreaCode = inputs[1]; } else { if (inputs[0].length() == 10) { callingAddressAreaCode = inputs[0].substring(0, 3); } else { callingAddressAreaCode = inputs[0].substring(0, 4); } answerAddressAreaCode = inputs[2]; } } else if (inputs.length == 8) { sd = inputs[4]; st = inputs[5]; ed = inputs[6]; et = inputs[7]; callingAddressAreaCode = inputs[1]; answerAddressAreaCode = inputs[3]; } } else if (type == 'm') { } SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault()); try { startTime = simpleDateFormat.parse(sd + " " + st); endTime = simpleDateFormat.parse(ed + " " + et); } catch (ParseException e) { } } public CallRecord(Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) { super(); this.startTime = startTime; this.endTime = endTime; this.callingAddressAreaCode = callingAddressAreaCode; this.answerAddressAreaCode = answerAddressAreaCode; } public Date getStartTime() { return startTime; } public void setStartTime(Date startTime) { this.startTime = startTime; } public Date getEndTime() { return endTime; } public void setEndTime(Date endTime) { this.endTime = endTime; } public String getCallingAddressAreaCode() { return callingAddressAreaCode; } public void setCallingAddressAreaCode(String callingAddressAreaCode) { this.callingAddressAreaCode = callingAddressAreaCode; } public String getAnswerAddressAreaCode() { return answerAddressAreaCode; } public void setAnswerAddressAreaCode(String answerAddressAreaCode) { this.answerAddressAreaCode = answerAddressAreaCode; } } abstract class CallChargeRule extends ChargeRule { } class LandPhoneInCityRule extends CallChargeRule { @Override public double calCost(UserRecords userRecords) { double sumCost = 0; for (CallRecord call : userRecords.getCallingInCityRecords()) { double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000; if (distanceS < 0) { continue; } double distanceM = (int) distanceS / 60; if (distanceS % 60 != 0) { distanceM += 1; } if (call.getCallType().equals("11")) { sumCost += distanceM * 0.1; } else if (call.getCallType().equals("12")) { sumCost += distanceM * 0.3; } else if (call.getCallType().equals("13")) { sumCost += distanceM * 0.6; } } return sumCost; } } class LandPhoneInlandRule extends CallChargeRule { @Override public double calCost(UserRecords userRecords) { double sumCost = 0; for (CallRecord call : userRecords.getCallingInLandRecords()) { double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000; if (distanceS < 0) { continue; } double distanceM = (int) distanceS / 60; if (distanceS % 60 != 0) { distanceM += 1; } sumCost += distanceM * 0.6; } return sumCost; } } class LandPhoneInProvinceRule extends CallChargeRule { @Override public double calCost(UserRecords userRecords) { double sumCost = 0; for (CallRecord call : userRecords.getCallingInProvinceRecords()) { double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000; if (distanceS < 0) { continue; } double distanceM = (int) distanceS / 60; if (distanceS % 60 != 0) { distanceM += 1; } sumCost += distanceM * 0.3; } return sumCost; } } class MobilePhoneInCityRule extends CallChargeRule { @Override public double calCost(UserRecords userRecords) { double sumCost = 0; for (CallRecord call : userRecords.getCallingInCityRecords()) { double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000; if (distanceS < 0) { continue; } double distanceM = (int) distanceS / 60; if (distanceS % 60 != 0) { distanceM += 1; } if (call.getCallType().equals("11")) { sumCost += distanceM * 0.1; } else if (call.getCallType().equals("12")) { sumCost += distanceM * 0.2; } else if (call.getCallType().equals("13")) { sumCost += distanceM * 0.3; } } return sumCost; } } class MobilePhoneInlandRule extends CallChargeRule { @Override public double calCost(UserRecords userRecords) { double sumCost = 0; for (CallRecord call : userRecords.getCallingInLandRecords()) { double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000; if (distanceS < 0) { continue; } double distanceM = (int) distanceS / 60; if (distanceS % 60 != 0) { distanceM += 1; } sumCost += distanceM * 0.6; } for (CallRecord call : userRecords.getAnswerInLandRecords()) { double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000; if (distanceS < 0) { continue; } double distanceM = (int) distanceS / 60; if (distanceS % 60 != 0) { distanceM += 1; } sumCost += distanceM * 0.3; } return sumCost; } } class MobilePhoneInProvinceRule extends CallChargeRule { @Override public double calCost(UserRecords userRecords) { double sumCost = 0; for (CallRecord call : userRecords.getCallingInProvinceRecords()) { double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000; if (distanceS < 0) { continue; } double distanceM = (int) distanceS / 60; if (distanceS % 60 != 0) { distanceM += 1; } if (call.getCallType().equals("21")) { sumCost += distanceM * 0.3; } else if (call.getCallType().equals("22")) { sumCost += distanceM * 0.3; } else if (call.getCallType().equals("23")) { sumCost += distanceM * 0.3; } } return sumCost; } } class MessageRecord extends CommunicationRecord { private 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; public double calCost() { return chargeMode.calCost(userRecords); } public double calBalance() { return balance - chargeMode.getMonthlyRent() - chargeMode.calCost(userRecords); } public UserRecords getUserRecords() { return userRecords; } public void setUserRecords(UserRecords userRecords) { this.userRecords = userRecords; } 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; } } class Outputtool { @SuppressWarnings("deprecation") public void output(double out) { // java.text.DecimalFormat df=new java.text.DecimalFormat("#.##"); // String a=df.format(out); // System.out.print(a); BigDecimal numb = new BigDecimal(out); out = numb.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); System.out.print(out); } }View Code
4、题目集7/题目7-1:
(1)题目详情
经过不懈的努力,C~K终于当上了班主任。
现在他要统计班里学生的名单,但是C~K在教务系统中导出班级名单时出了问题,发现会有同学的信息重复,现在他想把重复的同学信息删掉,只保留一个,
但是工作量太大了,所以找到了会编程的你,你能帮他解决这个问题吗?
输入格式:
第一行输入一个N,代表C~K导出的名单共有N行(N<100000).
接下来的N行,每一行包括一个同学的信息,学号 姓名 年龄 性别。
输出格式:
第一行输出一个n,代表删除重复名字后C~K的班级共有几人。
接下来的n行,输出每一个同学的信息,输出按照学号从小到大的顺序。
(2)设计与分析
1、类图
类图很简单,只有一个主函数
2、SourceMonitor生成图
由SourceMonitor图可知平均复杂度过大了,原因出在值只使用了一个Main类,然后读取、排序、输出都是循环,可以将排序单列(主要是里面还存在判断)
(3)踩坑心得
一开始自己写了个排序代码,然后发现样例能过,但就是没分,整个人懵住了,查了好几遍感觉没问题,如果有那位看出来了,请指正一下,最后借鉴了一下Comparator和字符串sort排序的使用通过了。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
for(i=0;i<n-1;i++) { String str1 = str.get(i); int a = Integer.parseInt(str1.substring(0,4)); for(j=i+1;j<n;j++){ String str2 = str.get(j); int b = Integer.parseInt(str2.substring(0,4)); if(a >= b){ str.set(i, str2); str.set(j, str1); } } }View Code
(4)实现代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.util.Scanner; import java.util.ArrayList; import java.util.Comparator; public class Main { public static void main(String []args) { Scanner input = new Scanner(System.in); int n = Integer.parseInt(input.nextLine()); ArrayList<String> str = new ArrayList<String>(); for(int i=0;i<n;i++) { String s = input.nextLine(); if(!str.contains(s)){ str.add(s); } } n = str.size(); str.sort(new Comparator<String>() { @Override public int compare(String s1, String s2) { return Integer.parseInt(s1.substring(0, 4))-Integer.parseInt(s2.substring(0, 4)); } }); System.out.println(n); for(int i=0;i<n;i++) { System.out.println(str.get(i)); } } } /* import java.util.Scanner; import java.util.ArrayList; public class Main{ public static void main(String[] args){ Scanner input = new Scanner(System.in); int n = Integer.parseInt(input.nextLine()); ArrayList<String> str = new ArrayList<>(); String s; int i = 0, j = 0; for(i=0;i<n;i++) { s = input.nextLine(); if(!str.contains(s)){ str.add(s); } } n = str.size(); for(i=0;i<n-1;i++) { String str1 = str.get(i); int a = Integer.parseInt(str1.substring(0,4)); for(j=i+1;j<n;j++){ String str2 = str.get(j); int b = Integer.parseInt(str2.substring(0,4)); if(a >= b){ str.set(i, str2); str.set(j, str1); } } } System.out.println(n); for(i=0;i<n;i++) { System.out.println(str.get(i)); } } } */View Code
5、题目集8/题目7-1:
(1)题目详情
实现一个简单的电信计费程序,针对手机的短信采用如下计费方式:
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。
本题只考虑短信计费,不考虑通信费用以及月租费。
(2)设计与分析
1、类图
题目给的参考类图和电信2相同,但在题目的要求下有一些类是不需要的,下面是去除一些没必要的之后的类图:
2、SourceMonitor生成图
由SourceMonitor图可知平均复杂度过大了
可知最大复杂度和最大深度均出现在主函数中:
(4)改进建议
类还需要进行一下重构,主函数的简化不到位。
(5)代码实现
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) throws ParseException { ArrayList<User> User1List = new ArrayList<>(); ArrayList<MessageChargeRule> messageChargeRules = new ArrayList<>(); SendMessageRule sendMessageRule = new SendMessageRule(); MessageCharging messageCharging = new MessageCharging(); messageCharging.getMessageChargeRules().add(sendMessageRule); Scanner in = new Scanner(System.in); String m = in.nextLine(); int flag; while (!m.equals("end")) { Judge judge = new Judge(); if (judge.isUser1Information(m)) { String regex = "[1][\\d]{10}"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(m); while (matcher.find()) { flag = 0; for (User user : User1List ) { if (user.getNumber().equals(matcher.group(0))) { flag = 1; break; } } if (flag == 0) { User1List.add(new User(matcher.group(0))); } } } if (judge.isMessageInformation(m)){ for (User user:User1List ) { if (user.getNumber().equals(m.substring(2,13))){ user.getUserRecords().addSendMessageRecords(new MessageRecord(m.substring(26))); } } } m = in.nextLine(); } for (User user:User1List ) { user.setChargeMode(messageCharging); } for (int i = 0; i < User1List.size(); i++) { int index = -1; Double min = Double.parseDouble(User1List.get(i).getNumber()); for (int j = i + 1; j < User1List.size(); j++) { if (Double.parseDouble(User1List.get(j).getNumber()) < min) { min = Double.parseDouble(User1List.get(j).getNumber()); index = j; } } if (min != Double.parseDouble(User1List.get(i).getNumber())) { User user = User1List.get(i); User1List.set(i, User1List.get(index)); User1List.set(index, user); } } for (User user:User1List ) { System.out.printf("%s %.1f %.1f\n",user.getNumber(),user.getChargeMode().calCost(user.getUserRecords()),user.getMessageBalance()); } } } class MessageCharging extends ChargeMode{ public double calCost(UserRecords userRecords){ return getMessageChargeRules().get(0).calCost(userRecords.getSendMessageRecords()); } } abstract class CallChargeRule extends ChargeRule{ public double calCost(ArrayList<CallRecord> callRecords){ return 0; } } abstract class MessageChargeRule extends ChargeRule{ public double calCost(ArrayList<MessageRecord> messageRecords){ return 0; } } class SendMessageRule extends MessageChargeRule{ public double calCost(ArrayList<MessageRecord> messageRecords){ int count = 0; double sum; for (MessageRecord messageRecord:messageRecords ) { if (messageRecord.getMessage().length()%10!=0){ count+=messageRecord.getMessage().length()/10+1; } else { count+=messageRecord.getMessage().length()/10; } } if (count<=3){ sum=count*0.1; } else if (count>3&&count<=5){ sum=0.3+(count-3)*0.2; } else { sum=0.3+0.4+(count-5)*0.3; } return sum; } } class CallRecord extends CommunicationRecord{ private Date startTime; private Date endTime; private String callingAddressAreaCode; private String answerAddressAreaCode; public CallRecord(Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) { this.startTime = startTime; this.endTime = endTime; this.callingAddressAreaCode = callingAddressAreaCode; this.answerAddressAreaCode = answerAddressAreaCode; } public Date getStartTime() { return startTime; } public void setStartTime(Date startTime) { this.startTime = startTime; } public Date getEndTime() { return endTime; } public void setEndTime(Date endTime) { this.endTime = endTime; } public String getCallingAddressAreaCode() { return callingAddressAreaCode; } public void setCallingAddressAreaCode(String callingAddressAreaCode) { this.callingAddressAreaCode = callingAddressAreaCode; } public String getAnswerAddressAreaCode() { return answerAddressAreaCode; } public void setAnswerAddressAreaCode(String answerAddressAreaCode) { this.answerAddressAreaCode = answerAddressAreaCode; } } abstract class ChargeMode { private ArrayList<ChargeRule> chargeRules = new ArrayList<>(); private ArrayList<ChargeRule> chargeRules1 = new ArrayList<>(); private ArrayList<MessageChargeRule> messageChargeRules = new ArrayList<>(); public void setChargeRules1(ArrayList<ChargeRule> chargeRules1) { this.chargeRules1 = chargeRules1; } public ArrayList<MessageChargeRule> getMessageChargeRules() { return messageChargeRules; } public void setMessageChargeRules(ArrayList<MessageChargeRule> messageChargeRules) { this.messageChargeRules = messageChargeRules; } public ArrayList<ChargeRule> getChargeRules1() { return chargeRules1; } public ArrayList<ChargeRule> getChargeRules() { return chargeRules; } public void setChargeRules(ArrayList<ChargeRule> chargeRules) { this.chargeRules = chargeRules; } public double calCost(UserRecords userRecords){ return 0; } public double getMonthlyRent(){ return 0; } } abstract class ChargeRule {} abstract 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 Judge { public boolean isMessageInformation(String s){ String pattern = "(m-)1[\\d]{10}\\s1[\\d]{10}\\s[A-Za-z0-9\\s,.]+"; return s.matches(pattern); } public boolean isUser1Information(String s){ String pattern = "(u-)[1][\\d]{10}[\\s]3"; return s.matches(pattern); } } class MessageRecord extends CommunicationRecord{ private String message; public MessageRecord(String message) { this.message = 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; public User(String number) { this.number = number; } public double getMessageBalance(){ return balance-chargeMode.calCost(userRecords); } public double getBalance() { return balance-chargeMode.calCost(userRecords)-chargeMode.getMonthlyRent(); } public UserRecords getUserRecords() { return userRecords; } public void setUserRecords(UserRecords userRecords) { this.userRecords = userRecords; } 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; } } 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<CallRecord> manYouInProvinceRecords = new ArrayList<>(); private ArrayList<CallRecord> manYouOutProvinceRecords = new ArrayList<>(); private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<>(); private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<>(); public void addManYouInProvinceRecords(CallRecord callRecord){ manYouInProvinceRecords.add(callRecord); } public ArrayList<CallRecord> getManYouInProvinceRecords() { return manYouInProvinceRecords; } public ArrayList<CallRecord> getManYouOutProvinceRecords() { return manYouOutProvinceRecords; } public void addManYouOutProvinceRecords(CallRecord callRecord){ manYouOutProvinceRecords.add(callRecord); } 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 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; } public ArrayList<MessageRecord> getSendMessageRecords() { return sendMessageRecords; } public ArrayList<MessageRecord> getReceiveMessageRecords() { return receiveMessageRecords; } public void addSendMessageRecords(MessageRecord messageRecord){ sendMessageRecords.add(messageRecord); } }View Code
6、题目集8/题目7-2:
(1)题目详情
编写一个类Shop
(商店),该类中有一个成员内部类InnerCoupons
(内部购物券),可以用于购买该商店的牛奶(假设每箱牛奶售价为50元)。
(2)设计与分析
1、类图
2、SourceMonitor生成图
题目简单,所以该SourceMonitor图没啥参考意义
(3)踩坑心得
主要在于内部类的使用,我使用的是非静态的成员内部类,成员内部类可以直接使用外部类的所有成员,包括私有的数据,所以在这个角度看它和方法有点像,但不同于方法,其可以被外部类当成一种属性的类别,即外部类可以创建内部类的对象作为其属性,这一点和其他类是一样的。
(4)代码实现
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import java.util.Scanner; class Shop{ private int milkCount; InnerCoupons coupons50 = new InnerCoupons(50); InnerCoupons coupons100 = new InnerCoupons(100); public void setMilkCount(int milkCount){ this.milkCount = milkCount; } public int getMilkCount(){ return milkCount; } public class InnerCoupons{ public int value; public InnerCoupons(int value){ this.value = value; } public void buy(){ System.out.println("使用了面值为"+this.value+"的购物券进行支付"); milkCount = milkCount - (this.value)/50; } } } public class Main{ public static void main(String[] args){ Shop myshop = new Shop(); Scanner input = new Scanner(System.in); myshop.setMilkCount(input.nextInt()); myshop.coupons50.buy(); System.out.println("牛奶还剩"+myshop.getMilkCount()+"箱"); myshop.coupons100.buy(); System.out.print("牛奶还剩"+myshop.getMilkCount()+"箱"); } }View Code
三、总结
通过这个阶段的学习,我对于继承多态的使用进一步得到了加强,其中很重要的应用就是对父类方法的重写,然后提升至抽象类,刚开始用抽象类从单个代码来看,这不是在把代码复杂化嘛,能直接写干嘛要用抽象类,后来理解了代码搬来搬去的时候抽象类的作用才有点体现出来;正则表达式运用的能力得到进一步的提升,让我体会到了正则的重要性,如果没有正则,对格式的判断光是想想就很复杂;ArrayList数组的应用使得我对于这个数据类型有了更深的理解;pta还进行了一些知识扩展,比如说内部类、迭代器,不得不说,确实起到了“迫使”我们学习新东西的作用!
标签:return,String,double,ArrayList,PTA6,new,题目,public From: https://www.cnblogs.com/jjbokey/p/16972138.html