首页 > 其他分享 >第三次Blog

第三次Blog

时间:2022-12-09 16:11:06浏览次数:42  
标签:return String double ArrayList Blog new public 第三次

一、前言

  在最后三次作业主要是围绕电信计费系统的作业。从一开始的座机计费,再到手机+座机计费,最后到短信计费。至于其它的题目,C~K的班级、阅读程序,按照题目需求修改程序、商品内部类购物券、动物发声模拟器这几道题目难度都不大,只要认真读题自己编码即可解决。这三次作业针对手机、座机的短信计费等三个方式,环环递进,相较于前面的多边形计算来说,难度没有那么大,其中难点在于对于正则表达式的运用判断,计费过程中的计算,以及输出排序的问题。

二、设计与分析

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元。
每条通讯信息单独计费后累加,不是将所有时间累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。

错误处理:
输入数据中出现的不符合格式要求的行一律忽略。

建议类图:
参见图1、2、3,可根据理解自行调整:

image.png

                                    图1
图1中User是用户类,包括属性:
userRecords (用户记录)、balance(余额)、chargeMode(计费方式)、number(号码)。

ChargeMode是计费方式的抽象类:
chargeRules是计费方式所包含的各种计费规则的集合,ChargeRule类的定义见图3。
getMonthlyRent()方法用于返回月租(monthlyRent)。

UserRecords是用户记录类,保存用户各种通话、短信的记录,    
各种计费规则将使用其中的部分或者全部记录。
其属性从上到下依次是:
市内拨打电话、省内(不含市内)拨打电话、省外拨打电话、
市内接听电话、省内(不含市内)接听电话、省外接听电话的记录
以及发送短信、接收短信的记录。
   

image.png

                                     图2
图2中CommunicationRecord是抽象的通讯记录类:
包含callingNumber拨打号码、answerNumber接听号码两个属性。
CallRecord(通话记录)、MessageRecord(短信记录)是它的子类。

CallRecord(通话记录类)包含属性:
通话的起始、结束时间以及
拨号地点的区号(callingAddressAreaCode)、接听地点的区号(answerAddressAreaCode)。
区号用于记录在哪个地点拨打和接听的电话。座机无法移动,就是本机区号,如果是手机号,则会有差异。
   

image.png

                                        图3
图3是计费规则的相关类,这些类的核心方法是:
calCost(ArrayList<CallRecord> callRecords)。
该方法针根据输入参数callRecords中的所有记录计算某用户的某一项费用;如市话费。
输入参数callRecords的约束条件:必须是某一个用户的符合计费规则要求的所有记录。

LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三个类分别是
座机拨打市内、省内、省外电话的计费规则类,用于实现这三种情况的费用计算。    
(提示:可以从UserRecords类中获取各种类型的callRecords)。
 

(2)测试样例:

输入样例:

在这里给出一组输入。例如:

u-079186300001 0
t-079186300001 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:25
end

输出样例:

在这里给出相应的输出。例如:

079186300001 3.0 77.0

(3)我的代码:

电信计费系列1-座机计费
 import java.util.*;
import java.text.*;
public class Main {
    public static void main(String[] args) {
        Dealdata p = new Dealdata();
        p.deal();
    }
}
abstract class CallChargeRule extends ChargeRule {
    public abstract  double calCost(ArrayList<CallRecord> callrecords);
}
class CallRecord extends CommunicationRecord {
    private Date  stateTime;
    private Date  endTime;
    private String callingAddressAreaCode;
    private String answerAddressAreaCode;
    public CallRecord( String callingNumber,String answerNumber,Date stateTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) {
        super();
        setCallingNumber(callingNumber);
        setAnswerNumber(answerNumber);
        this.stateTime = stateTime;
        this.endTime = endTime;
        this.callingAddressAreaCode = callingAddressAreaCode;
        this.answerAddressAreaCode = answerAddressAreaCode;
    }
    public Date getStateTime() {
        return stateTime;
    }
    public void setStateTime(Date stateTime) {
        this.stateTime = stateTime;
    }
    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<ChargeRule>();
    public ArrayList<ChargeRule> getChargeRules() {
        return chargeRules;
    }
    public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
        this.chargeRules = chargeRules;
    }
    public abstract double calCost(UserRecords useRecords);
    public abstract double getMonthlyRent();
}
abstract class ChargeRule {
    public  abstract double calCost(ArrayList<CallRecord> callrecords);
}
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 LandlinePhoneCharging extends ChargeMode {
    private double monthlyRent = 20;
    public LandlinePhoneCharging() {
        super();
        getChargeRules().add(new LandPhoneInCityRule());
        getChargeRules().add(new LandPhoneInProvinceRule());
        getChargeRules().add(new LandPhoneInLandRule());
    }
    @Override
    public double calCost(UserRecords useRecords) {
        double cost= 0;
        cost+= getChargeRules().get(0).calCost(useRecords.getCallingInCityRecords());
        cost+= getChargeRules().get(1).calCost(useRecords.getCallingInProvinceRecords());
        cost+= getChargeRules().get(2).calCost(useRecords.getCallingInLandRecords());
        return cost;
    }
    @Override
    public double getMonthlyRent() {
        return this.monthlyRent;
    }
}
class LandPhoneInCityRule extends CallChargeRule {
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double cost = 0;
        for(CallRecord c : callRecords) {
            cost+=Math.ceil((c.getEndTime().getTime()-c.getStateTime().getTime())/1000.0/60.0)*0.1;
        }
        return cost;
    }
}
class LandPhoneInLandRule extends CallChargeRule {
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double cost = 0;
        for(CallRecord c : callRecords) {
            cost+=Math.ceil((c.getEndTime().getTime()-c.getStateTime().getTime())/1000.0/60.0)*0.6;
        }
        return cost;
    }
}
class LandPhoneInProvinceRule extends CallChargeRule {
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        // TODO 自动生成的方法存根
        double cost = 0;
        for(CallRecord c : callRecords) {
            cost+=Math.ceil((c.getEndTime().getTime()-c.getStateTime().getTime())/1000.0/60.0)*0.3;
        }
        return cost;
    }
}
class MessageRecord extends CommunicationRecord {
    private String message;
    public MessageRecord(String callingNumber, String answerNumber, String message) {
        super();
        setCallingNumber(callingNumber);
        setAnswerNumber(answerNumber);
        this.message = message;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}
class Dealdata {
    public Dealdata() {
        super();
    }
    public  void deal() {
        TreeMap<String, User> users = new TreeMap<>();
        Scanner input = new Scanner(System.in);
        String regex1 ="[u]-[0-9]{11,12}\\s[0]";
        String regex2 ="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";
        String s = input.nextLine();
        while(!s.equals("end")) {
            if(s.matches(regex1)) {
                String[] data = s.substring(2).split(" ");
                String number = data[0].substring(4);
                ChargeMode changeMode = new LandlinePhoneCharging();
                User user = new User(changeMode,number);
                users.put(number, user);
            }
            else if(s.matches(regex2)) {
                String[] data = s.substring(2).split(" ");
                String callingAddressAreaCode = data[0].substring(0,4);
                String answerAddressAreaCode = data[1].substring(0,4);
                String callingNumber = data[0].substring(4);
                String answerNumber = data[1].substring(4);
                String stateDate = data[2];
                String stateTime = data[3];
                String endDate  = data[4];
                String endTime = data[5];
                String state = stateDate+" "+stateTime;
                String end = endDate + " " + endTime;
                Date statedate = new Date();
                Date enddate = new Date();
                String strDateFormat = "yyyy.MM.dd HH:mm:ss";
                SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
                try {
                    statedate = sdf.parse(state);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                try {
                    enddate = sdf.parse(end);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                CallRecord callRecord = new CallRecord(callingNumber, answerNumber, statedate, enddate, callingAddressAreaCode, answerAddressAreaCode);
                long address = Long.parseLong(callRecord.getAnswerAddressAreaCode());
                if(users.get(callingNumber) != null){
                    if(address==791) {
                        users.get(callingNumber).getUserRecords().addCallingInCityRecords(callRecord);
                    }
                    else if(address==701||(address>= 790 &&address<=799&&address!= 791)) {
                        users.get(callingNumber).getUserRecords().addCallingInProvinceRecords(callRecord);
                    }
                    else {
                        users.get(callingNumber).getUserRecords().addCallingInLandRecords(callRecord);
                    }
                }
            }
            s = input.nextLine();
        }
        for (User user: users.values()) {
            System.out.println("0791"+user.getNumber()+" "+Math.round(user.calCost()*100)/100.0+" "+Math.round(user.calBanlance()*100)/100.0);
        }
    }
}
class User {
    private UserRecords userRecords = new UserRecords();// 用户记录
    private double banlance = 100; // 余额
    private ChargeMode changeMode; // 计费方式
    private String number; // 电话号码
    public User(ChargeMode changeMode, String number) {
        super();
        this.changeMode = changeMode;
        this.number = number;
    }
    public UserRecords getUserRecords() {
        return userRecords;
    }
    public void setUserRecords(UserRecords userRecords) {
        this.userRecords = userRecords;
    }
    public double getBanlance() {
        return banlance;
    }
    public void setBanlance(double banlance) {
        this.banlance = banlance;
    }
    public ChargeMode getChangeMode() {
        return changeMode;
    }
    public void setChangeMode(ChargeMode changeMode) {
        this.changeMode = changeMode;
    }
    public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
        this.number = number;
    }
    public double calBanlance() {
        return this.banlance-changeMode.calCost(userRecords)-changeMode.getMonthlyRent();
    }
    public double calCost() {
        return changeMode.calCost(userRecords);
    }
}
class UserRecords {
    public UserRecords() {
        super();
    }
    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 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 addCallingInCityRecords(CallRecord callRecord) {
        callingInCityRecords.add(callRecord);
    }
    public void addCallingInLandRecords(CallRecord callRecord) {
        callingInLandRecords.add(callRecord);
    }
    public void addCallingInProvinceRecords(CallRecord callRecord) {
        callingInProvinceRecords.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 addSendMessageRecords(MessageRecord messageRecord) {
        sendMessageRecords.add(messageRecord);
    }
    public void addReceiveMessageRecords(MessageRecord messageRecord) {
        receiveMessageRecords.add(messageRecord);
    }
}

(4)类图及SourceMonitor分析

(5)我的分析:

①分析类图和SourceMonitor图:

  我的结构基本依据题目提供的参考类图进行设计,用User类来存储用户信息,UserRecords存储通话消息的记录。我自己写了inputDeal类来处理输入的数据,用以将输入的信息分类存储在处理时间中,我们需要使用SimpleDateFormat类,先把日期化成标准型性,再用其固定的方法来计算时间间隔。这次代码主要是根据老师所给的类图来进行填充和编写,总的来说代码量并不是很大,而且复用程度较高,总体的圈复杂度也较为客观,整个的代码质量还算是可以的。

②小结:

  本题作为电信计费系列的开端,仅加入了座机开户和座机互通,计费方式单一也不是很难,主要难度在于结构的创建,要理清每个类的关系,分清父类子类,创建结构容器,按照类图将类与类链接起来。最重要的第一步就是要弄清楚如何去处理输入的数据,区分输入的数据类型,以及要判断,输入的数据是否符合格式。

2、电信计费系列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)测试样例:

输入样例:

在这里给出一组输入。例如:

u-13811111111 1
t-13811111111 0791 13811111110 020 2022.1.3 08:00:00 2022.1.3 08:09:20
end

输出样例:

在这里给出相应的输出。例如:

13811111111 3.0 82.0

(3)我的代码:

电信计费系列2-手机+座机计费
import java.util.*;
import java.text.*;

public class Main {

    public static void main(String[] args) {
        ArrayList<User> users = new ArrayList<User>();
        InputDeal inputDeal = new InputDeal();

        Scanner input = new Scanner(System.in);

        String s = input.nextLine();
        while(!s.equals("end")) {
            if(inputDeal.checkType(s)==1) {
                inputDeal.addUser(users, s);
            }
            if(inputDeal.checkType(s)==2) {
                inputDeal.addRecord(users, s);
            }
            s = input.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 user: users) {
            System.out.println(user.getNumber()+" "+Math.round(user.calCost()*100)/100.0+" "+Math.round(user.calBanlance()*100)/100.0);
        }
        input.close();
    }
}
abstract class CallChargeRule extends ChargeRule {

}
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 CallRecord(String[] s) {
        super();

        char type = s[0].charAt(0);
        s[0] = s[0].substring(2);
        String sd = null, st = null, ed = null, et = null;

        if (type == 't') {
            if (s.length == 6) {
                sd = s[2];
                st = s[3];
                ed = s[4];
                et = s[5];
                callingAddressAreaCode = s[0].substring(0, 4);
                answerAddressAreaCode = s[1].substring(0, 4);
            } else if (s.length == 7) {
                sd = s[3];
                st = s[4];
                ed = s[5];
                et = s[6];
                if (s[0].charAt(0) != '0') {
                    if (s[2].length() == 10) {
                        answerAddressAreaCode = s[2].substring(0, 3);
                    } else {
                        answerAddressAreaCode = s[2].substring(0, 4);
                    }
                    callingAddressAreaCode = s[1];
                } else {
                    if (s[0].length() == 10) {
                        callingAddressAreaCode = s[0].substring(0, 3);
                    } else {
                        callingAddressAreaCode = s[0].substring(0, 4);
                    }
                    answerAddressAreaCode = s[2];
                }
            } else if (s.length == 8) {
                sd = s[4];
                st = s[5];
                ed = s[6];
                et = s[7];
                callingAddressAreaCode = s[1];
                answerAddressAreaCode = s[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 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 callingNumber,String answerNumber,Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) {
        super();
        setCallingNumber(callingNumber);
        setAnswerNumber(answerNumber);
        this.startTime = startTime;
        this.endTime = endTime;
        this.callingAddressAreaCode = callingAddressAreaCode;
        this.answerAddressAreaCode = answerAddressAreaCode;
    }
    public Date getStartTime() {
        return startTime;
    }
    public void setStartTime(Date stateTime) {
        this.startTime = stateTime;
    }
    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<ChargeRule>();
    public ArrayList<ChargeRule> getChargeRules() {
        return chargeRules;
    }
    public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
        this.chargeRules = chargeRules;
    }
    public abstract double calCost(UserRecords useRecords);
    public abstract double getMonthlyRent();
}
abstract class ChargeRule {
    public  abstract double calCost(UserRecords userRecords);
}
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 InputDeal {

    //用户:1  通话记录:2 基本格式错误:0
    public int checkType(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.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;
    }
    public void addUser(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 addRecord(ArrayList<User> users, String input) {
        String[] inputs = input.split(" ");
        User call = null, answer = 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)) {
                    call = i;
                }
                if (i.getNumber().equals(in)) {
                    answer = i;
                }
                if (call != null && answer != null) {
                    break;
                }
            }
            if (call != null) {
                if (callrecord.getCallType().matches("^1[1-3]$")) {
                    call.getUserRecords().addCallingInCityRecords(callrecord);
                }
                else if (callrecord.getCallType().matches("^2[1-3]$")) {
                    call.getUserRecords().addCallingInProvinceRecords(callrecord);
                }
                else {
                    call.getUserRecords().addCallingInLandRecords(callrecord);
                }
            }
            if (answer != null) {
                if (callrecord.getCallType().matches("^[1-3]1$")) {
                    answer.getUserRecords().addAnswerInCityRecords(callrecord);
                }
                else if (callrecord.getCallType().matches("^[1-3]2$")) {
                    answer.getUserRecords().addAnswerInProvinceRecords(callrecord);
                }
                else {
                    answer.getUserRecords().addAnswerInLandRecords(callrecord);
                }
            }
        }
        else if (input.charAt(0) == 'm') {

        }
    }
}
class LandlinePhoneCharging extends ChargeMode {

    private double monthlyRent = 20;

    public LandlinePhoneCharging() {
        super();
        getChargeRules().add(new LandPhoneInCityRule());
        getChargeRules().add(new LandPhoneInProvinceRule());
        getChargeRules().add(new LandPhoneInLandRule());
    }

    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (ChargeRule rule : getChargeRules()) {
            sumCost += rule.calCost(userRecords);
        }
        return sumCost;
    }

    @Override
    public double getMonthlyRent() {
        return monthlyRent;
    }
}
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 MessageRecord extends CommunicationRecord {
    private String message;
    public MessageRecord(String callingNumber, String answerNumber, String message) {
        super();
        setCallingNumber(callingNumber);
        setAnswerNumber(answerNumber);
        this.message = message;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}
class MobilePhoneCharging extends ChargeMode{
    private double monthlyRent = 15;

    public MobilePhoneCharging() {
        super();
        getChargeRules().add(new MobilePhoneInCityRule());
        getChargeRules().add(new MobilePhoneInProvinceRule());
        getChargeRules().add(new MobilePhoneInLandRule());
    }

    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (ChargeRule rule : getChargeRules()) {
            sumCost += rule.calCost(userRecords);
        }
        return sumCost;
    }

    @Override
    public double getMonthlyRent() {
        return monthlyRent;
    }
}
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 User {
    private UserRecords userRecords = new UserRecords();// 用户记录
    private double banlance = 100; // 余额
    private ChargeMode changeMode; // 计费方式
    private String number; // 电话号码
    public User(ChargeMode changeMode, String number) {
        super();
        this.changeMode = changeMode;
        this.number = number;
    }
    public User() {
        // TODO Auto-generated constructor stub
    }
    public User(String number){
        this.number = number;
    }

    public UserRecords getUserRecords() {
        return userRecords;
    }
    public void setUserRecords(UserRecords userRecords) {
        this.userRecords = userRecords;
    }
    public double getBanlance() {
        return banlance;
    }
    public void setBanlance(double banlance) {
        this.banlance = banlance;
    }
    public ChargeMode getChargeMode() {
        return changeMode;
    }
    public void setChargeMode(ChargeMode changeMode) {
        this.changeMode = changeMode;
    }
    public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
        this.number = number;
    }
    public double calBanlance() {
        return this.banlance-changeMode.calCost(userRecords)-changeMode.getMonthlyRent();
    }
    public double calCost() {
        return changeMode.calCost(userRecords);
    }
}
class UserRecords {

    public UserRecords() {
        super();
    }

    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 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 addCallingInCityRecords(CallRecord callRecord) {
        callingInCityRecords.add(callRecord);
    }
    public void addCallingInLandRecords(CallRecord callRecord) {
        callingInLandRecords.add(callRecord);
    }
    public void addCallingInProvinceRecords(CallRecord callRecord) {
        callingInProvinceRecords.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 addSendMessageRecords(MessageRecord messageRecord) {
        sendMessageRecords.add(messageRecord);
    }
    public void addReceiveMessageRecords(MessageRecord messageRecord) {
        receiveMessageRecords.add(messageRecord);
    }
}

(4)类图及SourceMonitor分析

 

(5)我的分析:

①分析类图和SourceMonitor图:

  这次的题目是在第一次电话计费的基础上加了手机通信,主要是在第一次的框架基础上加入了MobilePhoneChargingMobilePhoneInCityRuleMobilePhoneInLandRuleMobilePhoneInProvinceRule四个类计算不同地区的手机通信。除此之外,因为加了手机通信,手机-手机,手机-电话两种通信手段,省内省外国内三种计费方式,共计6种组合方式,增加了计费的难度,而且对于通信记录的输入处理的正则表达式也更为是复杂了,与座机不同的是,手机计费增加了漫游记费规则,这都是需要仔细考虑的点。看SourceMonitor图分析,最大复杂度偏大,最大深度以及平均深度都在超出预期的边缘,说明有一部分的代码并不理想,存在优化修改的空间。

②小结:

  在第一题的基础上加入了手机的开户,并增加了手机计费,手机互相通话,手机与座机通话,由于本题在上一题已经完成的结构上继续进行,作业主要难度在于计费方法的运用。计费的情况有许多种,容易混乱掉,需要耐心分析,将每种情况都考虑在内细细分析。

3、电信计费系列3-短信计费

(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)测试样例:

输入样例:

在这里给出一组输入。例如:

u-18907910010 3
m-18907910010 13305862264 aaaaaaaaaaaaaaaaaaaaaaa
end

输出样例:

在这里给出相应的输出。例如:

18907910010 0.3 99.7

(3)我的代码:

电信计费系列3-短信计费
 import java.util.*;
import java.text.*;
public class Main {

    public static void main(String[] args) {
        ArrayList<User> users = new ArrayList<User>();
        InputDeal inputDeal = new InputDeal();

        Scanner input = new Scanner(System.in);

        String s = input.nextLine();
        while(!s.equals("end")) {
            if(inputDeal.checkType(s)==1) {
                inputDeal.addUser(users, s);
            }
            if(inputDeal.checkType(s)==2) {
                inputDeal.addRecord(users, s);
            }
            s = input.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 user: users) {
            System.out.println(user.getNumber()+" "+Math.round(user.calCost()*100)/100.0+" "+Math.round(user.calBanlance()*100)/100.0);
        }
        input.close();
    }
}
abstract class CallChargeRule extends ChargeRule {

}
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 CallRecord(String[] s) {
        super();

        char type = s[0].charAt(0);
        s[0] = s[0].substring(2);
        String sd = null, st = null, ed = null, et = null;

        if (type == 't') {
            if (s.length == 6) {
                sd = s[2];
                st = s[3];
                ed = s[4];
                et = s[5];
                callingAddressAreaCode = s[0].substring(0, 4);
                answerAddressAreaCode = s[1].substring(0, 4);
            } else if (s.length == 7) {
                sd = s[3];
                st = s[4];
                ed = s[5];
                et = s[6];
                if (s[0].charAt(0) != '0') {
                    if (s[2].length() == 10) {
                        answerAddressAreaCode = s[2].substring(0, 3);
                    } else {
                        answerAddressAreaCode = s[2].substring(0, 4);
                    }
                    callingAddressAreaCode = s[1];
                } else {
                    if (s[0].length() == 10) {
                        callingAddressAreaCode = s[0].substring(0, 3);
                    } else {
                        callingAddressAreaCode = s[0].substring(0, 4);
                    }
                    answerAddressAreaCode = s[2];
                }
            } else if (s.length == 8) {
                sd = s[4];
                st = s[5];
                ed = s[6];
                et = s[7];
                callingAddressAreaCode = s[1];
                answerAddressAreaCode = s[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 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 callingNumber,String answerNumber,Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) {
        super();
        setCallingNumber(callingNumber);
        setAnswerNumber(answerNumber);
        this.startTime = startTime;
        this.endTime = endTime;
        this.callingAddressAreaCode = callingAddressAreaCode;
        this.answerAddressAreaCode = answerAddressAreaCode;
    }
    public Date getStartTime() {
        return startTime;
    }
    public void setStartTime(Date stateTime) {
        this.startTime = stateTime;
    }
    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<ChargeRule>();
    public ArrayList<ChargeRule> getChargeRules() {
        return chargeRules;
    }
    public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
        this.chargeRules = chargeRules;
    }
    public abstract double calCost(UserRecords useRecords);
    public abstract double getMonthlyRent();
}
abstract class ChargeRule {
    public  abstract double calCost(UserRecords userRecords);
}
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 InputDeal {

    //用户:1  通话记录:2 基本格式错误:0
    public int checkType(String input) {
        if (input.matches("[u]-0791[0-9]{7,8}\\s[0]") || input.matches("[u]-1[0-9]{10}\\s[13]")) {
            return 1;
        } else if (input.matches("[m]-1[0-9]{10}\\s" + "1[0-9]{10}\\s" + "[0-9a-zA-Z\\s\\.,]+")) {
            return 2;
        }
        return 0;
    }
    public void addUser(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());
        } else if (mode == 3) {
            usernew.setChargeMode(new MobilePhoneMassageCharging());
        }
        users.add(usernew);
    }
    public void addRecord(ArrayList<User> users, String input) {
        String[] inputs = input.split(" ");
        inputs[0] = inputs[0].substring(2);

        User callu = null, answeru = null;

        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];
        } else {
            in = inputs[1];
        }

        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 (input.charAt(0) == 'm') {
            MessageRecord messageRecord = new MessageRecord(input);
            if (callu != null) {
                callu.getUserRecords().addSendMessageRecords(messageRecord);
                ;
            }
            if (answeru != null) {
                callu.getUserRecords().addReceiveMessageRecords(messageRecord);
            }
        }
    }
}
class LandlinePhoneCharging extends ChargeMode {

    private double monthlyRent = 20;

    public LandlinePhoneCharging() {
        super();
        getChargeRules().add(new LandPhoneInCityRule());
        getChargeRules().add(new LandPhoneInProvinceRule());
        getChargeRules().add(new LandPhoneInLandRule());
    }

    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (ChargeRule rule : getChargeRules()) {
            sumCost += rule.calCost(userRecords);
        }
        return sumCost;
    }

    @Override
    public double getMonthlyRent() {
        return monthlyRent;
    }
}
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 MessageRecord extends CommunicationRecord {
    private String message;

    public MessageRecord(String input) {
        super();
        this.message = input.substring(26);
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
class MobilePhoneCharging extends ChargeMode{
    private double monthlyRent = 15;

    public MobilePhoneCharging() {
        super();
        getChargeRules().add(new MobilePhoneInCityRule());
        getChargeRules().add(new MobilePhoneInProvinceRule());
        getChargeRules().add(new MobilePhoneInLandRule());
    }

    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (ChargeRule rule : getChargeRules()) {
            sumCost += rule.calCost(userRecords);
        }
        return sumCost;
    }

    @Override
    public double getMonthlyRent() {
        return monthlyRent;
    }
}


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 MobilePhoneMassageCharging extends ChargeMode {

    private double monthlyRent = 0;

    public MobilePhoneMassageCharging() {
        super();
        getChargeRules().add(new MobilePhoneMessageRule());
    }

    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (ChargeRule rule : getChargeRules()) {
            sumCost += rule.calCost(userRecords);
        }
        return sumCost;
    }

    @Override
    public double getMonthlyRent() {
        return monthlyRent;
    }

}
class MobilePhoneMessageRule extends CallChargeRule {
    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        int number = 0;
        for (MessageRecord m : userRecords.getSendMessageRecords()) {
            int length = m.getMessage().length();
            if (length <= 10) {
                number++;
            }
            else {
                number += length / 10;
                if (length % 10 != 0) {
                    number++;
                }
            }
        }
        if (number <= 3) {
            sumCost = number * 0.1;
        }
        else if (number <= 5) {
            sumCost = 0.3 + 0.2 * (number - 3);
        }
        else {
            sumCost = 0.7 + 0.3 * (number - 5);
        }
        return sumCost;
    }
}
class User {
    private UserRecords userRecords = new UserRecords();// 用户记录
    private double banlance = 100; // 余额
    private ChargeMode changeMode; // 计费方式
    private String number; // 电话号码
    public User(ChargeMode changeMode, String number) {
        super();
        this.changeMode = changeMode;
        this.number = number;
    }
    public User() {
        // TODO Auto-generated constructor stub
    }
    public User(String number){
        this.number = number;
    }

    public UserRecords getUserRecords() {
        return userRecords;
    }
    public void setUserRecords(UserRecords userRecords) {
        this.userRecords = userRecords;
    }
    public double getBanlance() {
        return banlance;
    }
    public void setBanlance(double banlance) {
        this.banlance = banlance;
    }
    public ChargeMode getChargeMode() {
        return changeMode;
    }
    public void setChargeMode(ChargeMode changeMode) {
        this.changeMode = changeMode;
    }
    public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
        this.number = number;
    }
    public double calBanlance() {
        return this.banlance-changeMode.calCost(userRecords)-changeMode.getMonthlyRent();
    }
    public double calCost() {
        return changeMode.calCost(userRecords);
    }
}
class UserRecords {

    public UserRecords() {
        super();
    }

    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 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 addCallingInCityRecords(CallRecord callRecord) {
        callingInCityRecords.add(callRecord);
    }
    public void addCallingInLandRecords(CallRecord callRecord) {
        callingInLandRecords.add(callRecord);
    }
    public void addCallingInProvinceRecords(CallRecord callRecord) {
        callingInProvinceRecords.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 addSendMessageRecords(MessageRecord messageRecord) {
        sendMessageRecords.add(messageRecord);
    }
    public void addReceiveMessageRecords(MessageRecord messageRecord) {
        receiveMessageRecords.add(messageRecord);
    }
}

(4)类图及SourceMonitor分析

(5)我的分析:

①分析类图和SourceMonitor图:

  这次作业我只是在第二次的基础上增加了信息计费的规则以及修改了对于输入信息的处理,看SourceMonitor图分析,最大复杂度偏大,最大深度以及平均深度都在超出预期的边缘,说明有一部分的代码并不理想,存在优化修改的空间。

②小结:

  短信收费很简单,没有复杂的收费规则,只有一个简单的收费方法。不过有一点需要注意:如果短信长度大于十就按照两条短信发送,并且要注意发送的次数多少,每一次发送短信的价格也会随着次数的增加而增高。不需要考虑地区的情况,也不需要新的计费规则。

三、踩坑心得

(1)对于排序问题,可以使用Java的一些类和方法,比如说:sort()方法和Comparable接口,三次作业的输出都要求每个用户一行,用户之间按号码字符从小到大排序。这就需要先对User中Number属性进行排序,然后输出。

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;
        }
    }
});

(2)关于正则表达式编写,对于数据合法性的判断以及存储,我这里是专门写了一个InputDeal的类,里边有deal()方法用以处理字符串,addUser()用以增加存储用户,addRecord()用以增加存存储通信记录。checkType()用以检查是哪一种输入。其中使用"[u]-0791[0-9]{7,8}\\s[0]"来检测座机类型用户输入,"[u]-1[0-9]{10}\\s[13]"来检测手机类型用户输入,用"[m]-1[0-9]{10}\\s" + "1[0-9]{10}\\s" + "[0-9a-zA-Z\\s\\.,]+"检测信息类型的数据的输入。

(3)还有就是对于通话时长的计算,查阅了网上的资料才了解到date类型的数据有getTime()获取时间然后就可以与同类型数据进行加减运算了。对于时间类型数据格式的规范我是采用了题目建议"yyyy.MM.dd HH:mm:ss"格式,使用SimpleDateFormat类

四、改进建议

(1)这一系列的题目总的来说,自己设计的部分较少,基本都是在老师所提供的参考类图的基础上填写方法补充内容,我自己设计也就只有Main类以及对输入信息的处理。我觉得对于这方面的处理我并没有做好,首先是正则表达式的编写,比如第二次手机+电话通信,我的正则表达式就很复杂,建议在正则表达式的书写更精简一些,比如表达数字[0-9]可以换成\\d的一系列操作,这样更容易看清楚以及时找出错误。在编译代码的时候不用过于着急,应当顾其全面,尽量减少遗漏。

(2)我没有做到将三次的输入有机的结合在一起,只是简单的就题论题,对三次作业不同的输入进行不同的处理,这是我觉得可以结合在一起,构成一个统一的系统的。

五、总结

  通过对作业的解决,学会了算法,面向对象的三大技术特性,作业的解决过程中,渐渐的懂得了三大技术特性之间的关系,封装,承载,继承,多态,都是新学会的名词,重载是多态的一部分,也是代码中不可或缺的部分,通过对简单和复杂题目的解答,从最初学会的最基本的System.out.println到后来对子类父类的调用,使用多态中的方法实现子类覆盖父类,从最开始的获取字符到后面的单一开闭原则,每次作业的过程就是积累的过程,每次解题的过程就是成长的过程。

  这几个月的面向对象课程的Java学习和作业练习,从初步了解到Java的基本算法和简单的Java思想,到慢慢的了解更加复杂的Java问题,碰到难题时,解决的过程既锻炼了自己的耐心,也学会了以前不了解的知识,不过解题过程中也有发现自己拖延懒惰的问题,遇到困难总是想拖,但是下次写又忘记了这次的思路,这是需要加紧改正的不足。

标签:return,String,double,ArrayList,Blog,new,public,第三次
From: https://www.cnblogs.com/RushForward/p/16912902.html

相关文章

  • BLOG3
    一、前言1、知识点 类和对象:  类(class)和对象(object)是两种以计算机为载体的计算机语言的合称。对象是对客观事物的抽象,类是对对象的抽象。类是一种抽象......
  • 收集下阿里集团下的技术BLOG
    众所周知,阿里集团下的淘宝,阿里巴巴,支付宝等都是著名的技术公司,现在收集下他们公开的BLOG,有相当精彩的内容呢。1阿里中文站交互设计技术BLOG(http......
  • CVE-2015-4852 Weblogic T3 反序列化分析
    0x01前言看到很多师傅的面经里面都有提到Weblogic这一个漏洞,最近正好有一些闲暇时间,可以看一看。因为环境上总是有一些小问题,所以会在本地和云服务器切换着调试。0x0......
  • weblogic开启debug模式启动时出现ERROR: Cannot load this JVM TI agent twice
     分析错误:weblogic开启debug模式启动时出现下面的错误:ERROR:CannotloadthisJVMTIagenttwice,checkyourjavacommandlineforduplicatejdwpoptions.Erroroc......
  • weblogic开启开发者模式
    这几天在weblogic下的开发有一点小心得,分享下。 我们这边由于每次重写部署项目要加载缓存数据,非常耗时,最后在网上找了一个自动重写发布编译好的java文件的方法。 每次修改......
  • Java题目集6~8总结Blog
    一、前言题目集六:知识点:Java基本语法,对类的继承、抽象以及多态和容器的应用。题量:较少。难度:难。题目集七:知识点:Java基本语法,类的设计、继承和容器及其容器中排序;迭......
  • BLOG-3
    一、前言1、pta题目集6总结本次题目集主要是根据类图,写电信计费的座机计费,难度并不是特别大,但是因为是电信计费的基础,为了后续方便需要对本次作业有较深的了解,要......
  • BLOG-3
    1.前言:本次博客主要是对pta-6,pta-7,pta-8大作业的总结与收获。pta6-8考的是电信计费系列,第一次只考虑座机的情况且只考虑打电话的情况。主要运用的ArrayList的使用,以及考察......
  • BLOG-3
    前言:在上一个月的课程学习中,我吸收掌握了很多具有拓展性、延申性的知识,其中包含类与对象的构造、组成,以及在封装、继承、多态三原则优化后的代码结构基础上,进行抽象类......
  • PTA-oop第三次博客2022.12.4
    一.前言 题目集六:本次大作业是第一次写电信计费,难度较前几次的多边形有了明显的下降,题目难点不再是算法的设计,而是类与类之间关系的设计,同样也是因为第一次写电信计......