随笔 - 3,  文章 - 0,  评论 - 2,  阅读 - 159

PTA后三次作业总结

1. 前言:

本次总结的是PTA6-PTA8的所有题目,主要涉及同一个系列,电信计费系列,由座机收费到手机收费再到短信收费,同一系列的题目,更考验代码的复用性。不过由于大体框架,类图老师都已经给出,设计上基本大体参照老师的设计方案即可,总体上来说这三次作业难度相较于前几次大作业难度并不高,同时由于其他小题都比较简单,故下面仅分析电信计费系列的问题。

2. 设计与分析:

第六次作业

7-1 电信计费系列1-座机计费

题目: 实现一个简单的电信计费程序:
假设南昌市电信分公司针对市内座机用户采用的计费方式:
月租20元,接电话免费,市内拨打电话0.1元/分钟,省内长途0.3元/分钟,国内长途拨打0.6元/分钟。不足一分钟按一分钟计。
南昌市的区号:0791,江西省内各地市区号包括:0790~0799以及0701。

输入格式:
输入信息包括两种类型
1、逐行输入南昌市用户开户的信息,每行一个用户,
格式:u-号码 计费类型 (计费类型包括:0-座机 1-手机实时计费 2-手机A套餐)
例如:u-079186300001 0
座机号码除区号外由是7-8位数字组成。
本题只考虑计费类型0-座机计费,电信系列2、3题会逐步增加计费类型。
2、逐行输入本月某些用户的通讯信息,通讯信息格式:
座机呼叫座机:t-主叫号码 接听号码 起始时间 结束时间
t-079186330022 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:11
以上四项内容之间以一个英文空格分隔,
时间必须符合"yyyy.MM.dd HH:mm:ss"格式。提示:使用SimpleDateFormat类。
以上两类信息,先输入所有开户信息,再输入所有通讯信息,最后一行以“end”结束。
注意:
本题非法输入只做格式非法的判断,不做内容是否合理的判断(时间除外,否则无法计算),比如:
1、输入的所有通讯信息均认为是同一个月的通讯信息,不做日期是否在同一个月还是多个月的判定,直接将通讯费用累加,因此月租只计算一次。
2、记录中如果同一电话号码的多条通话记录时间出现重合,这种情况也不做判断,直接 计算每条记录的费用并累加。
3、用户区号不为南昌市的区号也作为正常用户处理。

输出格式:
根据输入的详细通讯信息,计算所有已开户的用户的当月费用(精确到小数点后2位,
单位元)。假设每个用户初始余额是100元。
每条通讯信息单独计费后累加,不是将所有时间累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。

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

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

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

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

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

img

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

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

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

LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三个类分别是
座机拨打市内、省内、省外电话的计费规则类,用于实现这三种情况的费用计算。
(提示:可以从UserRecords类中获取各种类型的callRecords)。
后续扩展说明:
后续题目集将增加手机用户,手机用户的计费方式中除了与座机计费类似的主叫通话费之外,还包含市外接听电话的漫游费以及发短信的费用。在本题的设计时可统一考虑。
通话记录中,手机需要额外记录拨打/接听的地点的区号,比如:
座机打手机: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
短信的格式:m-主叫号码,接收号码,短信内容
m-18907910010 13305862264 welcome to jiangxi
m-13305862264 18907910010 thank you

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

u-079186300001 0
t-079186300001 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:25
end
输出样例:
在这里给出相应的输出。例如:

079186300001 3.0 77.0
其余参考样例详见附件,未尽事宜以附件样例为准:

试题分析: 首先通过题目中给出的类图以及部分类的描述,我们可以知道代码的实现主要分为三个部分,第一个部分便是对用户信息描述的一系列类,如用户信息类,用户记录类等;第二部分是实现通话记录的一系列类;而第三部分是整个计费的核心,也就是具体的计费方式的一系列类。整体思路主要就是实现给出的类图的架构,然后安装题意对第三部分中添加收费的细节即可。除此之外,还需要实现对输入数据的格式的判断以及读取,但由于整体架构已经给出,实现难度不高,故不过多分析,代码如下。

源代码展示:

查看代码
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.SimpleFormatter;

class User {
    private UserRecords userRecords;//用户记录
    private double balance = 100;//余额
    private ChargeMode chargeMode;//计费方式
    private String number;//号码

    public double calBalance(){
        return balance - (chargeMode.getMonthlyRent() + chargeMode.calCost(userRecords));
    }
    public double calCost(){
        return chargeMode.calCost(userRecords);
    }

    public UserRecords getUserRecords() {
        return userRecords;
    }

    public void setUserRecords(UserRecords userRecords) {
        this.userRecords = userRecords;
    }

    public double getBalance() {
        return balance;
    }

    public ChargeMode getChargeMode() {
        return chargeMode;
    }

    public void setChargeMode(ChargeMode chargeMode) {
        this.chargeMode = chargeMode;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }
}


abstract class ChargeMode {
    protected List<ChargeRule> chargeRules = new ArrayList<>();

    public List<ChargeRule> getChargeRules() {
        return chargeRules;
    }

    public void setChargeRules(List<ChargeRule> chargeRules) {
        this.chargeRules = chargeRules;
    }

    abstract public double calCost(UserRecords userRecords);

    abstract public double getMonthlyRent();

}

class UserRecords {
    List<CallRecord> callingInCityRecords = new ArrayList<>();
    List<CallRecord> callingInProvinceRecords = new ArrayList<>();
    List<CallRecord> callingInLandRecords = new ArrayList<>();
    List<CallRecord> answerInCityRecords = new ArrayList<>();
    List<CallRecord> answerInProvinceRecords = new ArrayList<>();
    List<CallRecord> answerInLandRecords = new ArrayList<>();
    List<CallRecord> sendMessageRecords = new ArrayList<>();
    List<CallRecord> receiveMessageRecords = new ArrayList<>();

    public void addCallingInCityRecords(CallRecord callRecord) {
        callingInCityRecords.add(callRecord);
    }

    public void addCallingInProvinceRecords(CallRecord callRecord) {
        callingInProvinceRecords.add(callRecord);
    }

    public void addCallingInLandRecords(CallRecord callRecord) {
        callingInLandRecords.add(callRecord);
    }

    public void addAnswerInCityRecords(CallRecord callRecord) {
        answerInCityRecords.add(callRecord);
    }

    public void addAnswerInProvinceRecords(CallRecord callRecord) {
        answerInProvinceRecords.add(callRecord);
    }

    public void addAnswerInLandRecords(CallRecord callRecord) {
        answerInLandRecords.add(callRecord);
    }

    public void addSendMessageRecords(CallRecord callRecord) {
        sendMessageRecords.add(callRecord);
    }

    public void addReceiveMessageRecords(CallRecord callRecord) {
        receiveMessageRecords.add(callRecord);
    }

    public List<CallRecord> getCallingInCityRecords() {
        return callingInCityRecords;
    }

    public List<CallRecord> getCallingInLandRecords() {
        return callingInLandRecords;
    }

    public List<CallRecord> getCallingInProvinceRecords() {
        return callingInProvinceRecords;
    }

    public List<CallRecord> getAnswerInCityRecords() {
        return answerInCityRecords;
    }

    public List<CallRecord> getAnswerInLandRecords() {
        return answerInLandRecords;
    }

    public List<CallRecord> getAnswerInProvinceRecords() {
        return answerInProvinceRecords;
    }

    public List<CallRecord> getReceiveMessageRecords() {
        return receiveMessageRecords;
    }

    public List<CallRecord> getSendMessageRecords() {
        return sendMessageRecords;
    }
}

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 Sum = 0;
        //System.out.println("?");
        //System.out.println(chargeRules.size());
        for (ChargeRule chargeRule : chargeRules) {
            Sum += chargeRule.calCost(userRecords);
            //System.out.println("sum = "+Sum);
        }
        return Sum;
    }

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

}

abstract class CommunicationRecord {

    protected String callingNumber;
    protected String answerNumber;

    public String getAnswerNumber() {
        return answerNumber;
    }

    public void setAnswerNumber(String answerNumber) {
        this.answerNumber = answerNumber;
    }

    public String getCallingNumber() {
        return callingNumber;
    }

    public void setCallingNumber(String callingNumber) {
        this.callingNumber = callingNumber;
    }
}

class CallRecord extends CommunicationRecord {
    private Date startTime;
    private Date endTime;
    private String callingAddressAreaCode;
    private String answerAddressAreaCode;

    public int getCallType() {
        if (callingAddressAreaCode.equals(answerAddressAreaCode)) {
            return 1;
        }
        if (callingAddressAreaCode.matches("079[0-9]") || callingAddressAreaCode.equals("0701")) {
            if (answerAddressAreaCode.matches("079[0-9]") || answerAddressAreaCode.equals("0701")) {
                return 2;
            }
        }
        return 3;
    }

    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 getAnswerAddressAreaCode() {
        return answerAddressAreaCode;
    }

    public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
        this.answerAddressAreaCode = answerAddressAreaCode;
    }

    public String getCallingAddressAreaCode() {
        return callingAddressAreaCode;
    }

    public void setCallingAddressAreaCode(String callingAddressAreaCode) {
        this.callingAddressAreaCode = callingAddressAreaCode;
    }
}

class MessageRecord extends CommunicationRecord {
    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

abstract class CallChargeRule extends ChargeRule {
    //abstract public double calCost(List<CallRecord> callRecords);
}


abstract class ChargeRule{
    abstract public double calCost(UserRecords userRecords);
}

class LandPhoneInCityRule extends CallChargeRule {

    @Override
    public double calCost(UserRecords userRecords) {
        double Sum = 0;
        for (CallRecord call : userRecords.getCallingInCityRecords()) {
            Sum += tools.getTime(call.getEndTime(),call.getStartTime())* 0.1;
        }
        return Sum;
    }

}

class LandPhoneInProvinceRule extends CallChargeRule {
    @Override
    public double calCost(UserRecords userRecords) {
        double Sum = 0;
        for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
            Sum += tools.getTime(call.getEndTime(),call.getStartTime())* 0.3;
        }
        return Sum;
    }
}

class LandPhoneInlandRule extends CallChargeRule {
    @Override
    public double calCost(UserRecords userRecords) {
        double Sum = 0;
        for (CallRecord call : userRecords.getCallingInLandRecords()) {
            Sum += tools.getTime(call.getEndTime(),call.getStartTime())* 0.6;
        }
        return Sum;
    }
}

class tools{

    public static int checkFormat(String str) {
        String[] str1 = str.split(" ");
        if (str1.length == 2) {
            if (str1[0].matches("u-[0-9]{11,12}")) {
                if (Integer.parseInt(str1[1]) == 0 || Integer.parseInt((str1[1])) == 1 || Integer.parseInt(str1[1]) == 2) {
                    return 1;
                }
            }
        }
        else if (str1.length == 6) {
            if (str1[2].matches("[1-9][0-9]{3}.([1-9]|1[0-2]).([1-9]|[1-2][0-9]|3[0-1])"))
                if (str1[4].matches("[1-9][0-9]{3}.([1-9]|1[0-2]).([1-9]|[1-2][0-9]|3[0-1])"))
                    if (str1[3].matches("([0-1][0-9]|2[0-3])\\:([0-5][0-9])\\:([0-5][0-9])"))
                        if (str1[5].matches("([0-1][0-9]|2[0-3])\\:([0-5][0-9])\\:([0-5][0-9])"))
                            if (str1[0].matches("[t]-0791[0-9]{7,8}")) {
                                if (str1[1].matches(".[0-9]{9,11}"))
                                    return 2;
                            }
        }
        return -1;
    }

    public static long getTime(Date endTime,Date startTime){
        long t = (endTime.getTime()-startTime.getTime()) ;
        long time = t / 60000;
        if (t % 60000 != 0) {
            time += 1;
        }
        return time;
    }


}

class UsersLibrary {
    List<User> userList = new ArrayList<>();

    public void addNewUser(String s){
        User newUser = new User();
        String[] str = s.split(" ");
        String telNumber = str[0].substring(2);
        if(userList!=null){
            for (User user : userList) {
                if (user.getNumber().equals(telNumber)) {
                    return;
                }
            }
        }
        int type = Integer.parseInt(str[1]);
        newUser.setNumber(telNumber);
        if(type == 0){
            newUser.setChargeMode(new LandlinePhoneCharging());
        }
        UserRecords userRecords = new UserRecords();
        newUser.setUserRecords(userRecords);
        userList.add(newUser);
    }

    public void addNewRecord(String s){
        String[] str1 = s.split(" ");
        str1[0] = str1[0].substring(2);
        User callUser = null, answerUser = null;
        CallRecord callRecord = new CallRecord();
        if (str1[0].length() == 10) {
            callRecord.setCallingAddressAreaCode(str1[0].substring(0, 3));
        }
        else {
            callRecord.setCallingAddressAreaCode(str1[0].substring(0, 4));
        }
        if (str1[1].length() == 10) {
            callRecord.setAnswerAddressAreaCode(str1[1].substring(0, 3));
        }
        else {
            callRecord.setAnswerAddressAreaCode(str1[1].substring(0, 4));;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        try {
            callRecord.setStartTime(simpleDateFormat.parse(str1[2] + " " + str1[3]));
            callRecord.setEndTime(simpleDateFormat.parse(str1[4] + " " + str1[5]));
        } catch (ParseException e) {
        }

        for (User u :  userList) {
            if (u.getNumber().equals(str1[0])) {
                callUser = u;
            }
            if (u.getNumber().equals(str1[1])) {
                answerUser = u;
            }
            if (callUser != null && answerUser != null) {
                break;
            }
        }
        if (callUser != null) {
            if (callRecord.getCallType() == 1) {
                callUser.getUserRecords().addCallingInCityRecords(callRecord);
            } else if (callRecord.getCallType() == 2) {
                callUser.getUserRecords().addCallingInProvinceRecords(callRecord);
            } else {
                callUser.getUserRecords().addCallingInLandRecords(callRecord);
            }
        }

        if (answerUser != null) {
            if (callRecord.getCallType() == 1) {
                answerUser.getUserRecords().addAnswerInCityRecords(callRecord);
            } else if (callRecord.getCallType() == 2) {
                answerUser.getUserRecords().addAnswerInProvinceRecords(callRecord);
            } else {
                answerUser.getUserRecords().addAnswerInLandRecords(callRecord);
            }
        }
    }


}



public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        UsersLibrary usersLibrary = new UsersLibrary();
        String str = in.nextLine();
        while(!str.equals("end")){
            int mode = tools.checkFormat(str);
            if(mode == 1)
                usersLibrary.addNewUser(str);
            else if(mode == 2)
                usersLibrary.addNewRecord(str);
            str = in.nextLine();
        }
        usersLibrary.userList.sort(new Comparator<User>() {

            @Override
            public int compare(User u1, User u2) {
                if (Double.parseDouble(u1.getNumber()) > Double.parseDouble(u2.getNumber())) {
                    return 1;
                } else {
                    return -1;
                }
            }
        });


        for (User u : usersLibrary.userList) {
            System.out.println(u.getNumber() +" "+ Double.parseDouble(String.format("%.1f",u.calCost())) +" " +
                    Double.parseDouble( String.format("%.1f",u.calBalance()) )  );
        }

    }

}

类图:
img
SourceMonitor生成的报表内容:
img

代码分析总结: 本次作业主要是对我们继承,组合等关系的考察,理解了这些关系,在给出架构的情况下实现还是较为简单,难点可能就是正则表达式,代码已在代码中给出,不多赘述。

第七次作业

7-1 电信计费系列2-手机+座机计费

题目: 实现南昌市电信分公司的计费程序,假设该公司针对手机和座机用户分别采取了两种计费方案,分别如下:
1、针对市内座机用户采用的计费方式(与电信计费系列1内容相同):
月租20元,接电话免费,市内拨打电话0.1元/分钟,省内长途0.3元/分钟,国内长途拨打0.6元/分钟。不足一分钟按一分钟计。
假设本市的区号:0791,江西省内各地市区号包括:0790~0799以及0701。
2、针对手机用户采用实时计费方式:
月租15元,市内省内接电话均免费,市内拨打市内电话0.1元/分钟,市内拨打省内电话0.2元/分钟,市内拨打省外电话0.3元/分钟,省内漫游打电话0.3元/分钟,省外漫游接听0.3元/分钟,省外漫游拨打0.6元/分钟;
注:被叫电话属于市内、省内还是国内由被叫电话的接听地点区号决定,比如以下案例中,南昌市手机用户13307912264在区号为020的广州接听了电话,主叫号码应被计算为拨打了一个省外长途,同时,手机用户13307912264也要被计算省外接听漫游费:
u-13307912264 1
t-079186330022 13307912264 020 2022.1.3 10:00:25 2022.1.3 10:05:11

输入:
输入信息包括两种类型
1、逐行输入南昌市用户开户的信息,每行一个用户,含手机和座机用户
格式:u-号码 计费类型 (计费类型包括:0-座机 1-手机实时计费 2-手机A套餐)
例如:u-079186300001 0
座机号码由区号和电话号码拼接而成,电话号码包含7-8位数字,区号最高位是0。
手机号码由11位数字构成,最高位是1。
本题在电信计费系列1基础上增加类型1-手机实时计费。
手机设置0或者座机设置成1,此种错误可不做判断。
2、逐行输入本月某些用户的通讯信息,通讯信息格式:
座机呼叫座机:t-主叫号码 接听号码 起始时间 结束时间
t-079186330022 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:11
以上四项内容之间以一个英文空格分隔,
时间必须符合"yyyy.MM.dd HH:mm:ss"格式。提示:使用SimpleDateFormat类。
输入格式增加手机接打电话以及收发短信的格式,手机接打电话的信息除了号码之外需要额外记录拨打/接听的地点的区号,比如:
座机打手机:
t-主叫号码 接听号码 接听地点区号 起始时间 结束时间
t-079186330022 13305862264 020 2022.1.3 10:00:25 2022.1.3 10:05:11
手机互打:
t-主叫号码 拨号地点 接听号码 接听地点区号 起始时间 结束时间
t-18907910010 0791 13305862264 0371 2022.1.3 10:00:25 2022.1.3 10:05:11

注意:以上两类信息,先输入所有开户信息,再输入所有通讯信息,最后一行以“end”结束。

输出:
根据输入的详细通讯信息,计算所有已开户的用户的当月费用(精确到小数点后2位,单位元)。假设每个用户初始余额是100元。
每条通讯、短信信息均单独计费后累加,不是将所有信息累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。
错误处理:
输入数据中出现的不符合格式要求的行一律忽略。

本题只做格式的错误判断,无需做内容上不合理的判断,比如同一个电话两条通讯记录的时间有重合、开户号码非南昌市的号码等,此类情况都当成正确的输入计算。但时间的输入必须符合要求,比如不能输入2022.13.61 28:72:65。
建议类图:
参见图1、2、3:

img
图1中User是用户类,包括属性:
userRecords (用户记录)、balance(余额)、chargeMode(计费方式)、number(号码)。
ChargeMode是计费方式的抽象类:
chargeRules是计费方式所包含的各种计费规则的集合,ChargeRule类的定义见图3。
getMonthlyRent()方法用于返回月租(monthlyRent)。
UserRecords是用户记录类,保存用户各种通话、短信的记录,
各种计费规则将使用其中的部分或者全部记录。
其属性从上到下依次是:
市内拨打电话、省内(不含市内)拨打电话、省外拨打电话、
市内接听电话、省内(不含市内)接听电话、省外接听电话的记录
以及发送短信、接收短信的记录。

img
图2中CommunicationRecord是抽象的通讯记录类:
包含callingNumber拨打号码、answerNumber接听号码两个属性。
CallRecord(通话记录)、MessageRecord(短信记录)是它的子类。CallRecord(通话记录类)包含属性:
通话的起始、结束时间以及
拨号地点的区号(callingAddressAreaCode)、接听地点的区号(answerAddressAreaCode)。
区号用于记录在哪个地点拨打和接听的电话。座机无法移动,就是本机区号,如果是手机号,则会有差异。

img

图3是计费规则的相关类,这些类的核心方法是:
calCost(ArrayList callRecords)。
该方法针根据输入参数callRecords中的所有记录计算某用户的某一项费用;如市话费。
输入参数callRecords的约束条件:必须是某一个用户的符合计费规则要求的所有记录。
SendMessageRule是发送短信的计费规则类,用于计算发送短信的费用。
LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三个类分别是座机拨打市内、省内、省外电话的计费规则类,用于实现这三种情况的费用计算。
(提示:可以从UserRecords类中获取各种类型的callRecords)。
注意:以上图中所定义的类不是限定要求,根据实际需要自行补充或修改。

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

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
更多内容详见附件:

电信计费简化版-座机计费说明.pdf

试题分析: 第七次作业相较于上一次作业,实在其基础上增添了手机的计费,对于座机部分修改不大,基本沿用上次的代码,需要添加的部分主要是手机计费规则。
img
如图所示,主要的增添部分就是这四个函数,分别是手机计费规则以及它的三个子类,市内,省内,省外计费规则,结构同座机计费,只是其中细节有所不同,根据题意实现即可。除了增加这四个类,还需要对输入判断进行一些修改,增添对手机输入的判断,同时修改记录通话记录的函数,加入对手机记录的判断,这部分我认为是本次作业的一大难点,情况较多需要仔细思考,具体代码如下。

源代码展示:

查看代码
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

class User {
    private UserRecords userRecords;//用户记录
    private double balance = 100;//余额
    private ChargeMode chargeMode;//计费方式
    private String number;//号码

    public double calBalance(){
        return balance - (chargeMode.getMonthlyRent() + chargeMode.calCost(userRecords));
    }
    public double calCost(){
        return chargeMode.calCost(userRecords);
    }

    public UserRecords getUserRecords() {
        return userRecords;
    }

    public void setUserRecords(UserRecords userRecords) {
        this.userRecords = userRecords;
    }

    public double getBalance() {
        return balance;
    }

    public ChargeMode getChargeMode() {
        return chargeMode;
    }

    public void setChargeMode(ChargeMode chargeMode) {
        this.chargeMode = chargeMode;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }
}


abstract class ChargeMode {
    protected List<ChargeRule> chargeRules = new ArrayList<>();

    public List<ChargeRule> getChargeRules() {
        return chargeRules;
    }

    public void setChargeRules(List<ChargeRule> chargeRules) {
        this.chargeRules = chargeRules;
    }

    abstract public double calCost(UserRecords userRecords);

    abstract public double getMonthlyRent();

}

class UserRecords {
    List<CallRecord> callingInCityRecords = new ArrayList<>();
    List<CallRecord> callingInProvinceRecords = new ArrayList<>();
    List<CallRecord> callingInLandRecords = new ArrayList<>();
    List<CallRecord> answerInCityRecords = new ArrayList<>();
    List<CallRecord> answerInProvinceRecords = new ArrayList<>();
    List<CallRecord> answerInLandRecords = new ArrayList<>();
    List<CallRecord> sendMessageRecords = new ArrayList<>();
    List<CallRecord> receiveMessageRecords = new ArrayList<>();

    public void addCallingInCityRecords(CallRecord callRecord) {
        callingInCityRecords.add(callRecord);
    }

    public void addCallingInProvinceRecords(CallRecord callRecord) {
        callingInProvinceRecords.add(callRecord);
    }

    public void addCallingInLandRecords(CallRecord callRecord) {
        callingInLandRecords.add(callRecord);
    }

    public void addAnswerInCityRecords(CallRecord callRecord) {
        answerInCityRecords.add(callRecord);
    }

    public void addAnswerInProvinceRecords(CallRecord callRecord) {
        answerInProvinceRecords.add(callRecord);
    }

    public void addAnswerInLandRecords(CallRecord callRecord) {
        answerInLandRecords.add(callRecord);
    }

    public void addSendMessageRecords(CallRecord callRecord) {
        sendMessageRecords.add(callRecord);
    }

    public void addReceiveMessageRecords(CallRecord callRecord) {
        receiveMessageRecords.add(callRecord);
    }

    public List<CallRecord> getCallingInCityRecords() {
        return callingInCityRecords;
    }

    public List<CallRecord> getCallingInLandRecords() {
        return callingInLandRecords;
    }

    public List<CallRecord> getCallingInProvinceRecords() {
        return callingInProvinceRecords;
    }

    public List<CallRecord> getAnswerInCityRecords() {
        return answerInCityRecords;
    }

    public List<CallRecord> getAnswerInLandRecords() {
        return answerInLandRecords;
    }

    public List<CallRecord> getAnswerInProvinceRecords() {
        return answerInProvinceRecords;
    }

    public List<CallRecord> getReceiveMessageRecords() {
        return receiveMessageRecords;
    }

    public List<CallRecord> getSendMessageRecords() {
        return sendMessageRecords;
    }
}

class telPhoneCharging extends ChargeMode {

    private double monthlyRent = 15;

    public telPhoneCharging() {
        super();
        chargeRules.add(new telPhoneInCityRule());
        chargeRules.add(new telPhoneInProvinceRule());
        chargeRules.add(new telPhoneInlandRule());
    }
    @Override
    public double calCost(UserRecords userRecords){
        double Sum = 0;
        //System.out.println("?");
        //System.out.println(chargeRules.size());
        for (ChargeRule chargeRule : chargeRules) {
            Sum += chargeRule.calCost(userRecords);
           // System.out.println("sum = "+Sum);
        }
        return Sum;
    }

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


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 Sum = 0;
        //System.out.println("?");
        //System.out.println(chargeRules.size());
        for (ChargeRule chargeRule : chargeRules) {
            Sum += chargeRule.calCost(userRecords);
            //System.out.println("sum = "+Sum);
        }
        return Sum;
    }

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

}

abstract class CommunicationRecord {

    protected String callingNumber;
    protected String answerNumber;

    public String getAnswerNumber() {
        return answerNumber;
    }

    public void setAnswerNumber(String answerNumber) {
        this.answerNumber = answerNumber;
    }

    public String getCallingNumber() {
        return callingNumber;
    }

    public void setCallingNumber(String callingNumber) {
        this.callingNumber = callingNumber;
    }
}

class CallRecord extends CommunicationRecord {
    private Date startTime;
    private Date endTime;
    private String callingAddressAreaCode;
    private String answerAddressAreaCode;

    public int getCallType() {
        int type = 0;
        if (callingAddressAreaCode.equals("0791")) {
            type +=10;
        } else if (callingAddressAreaCode.matches("^079[023456789]$") || callingAddressAreaCode.equals("0701")) {
            type +=20;
        } else {
            type +=30;
        }

        if (answerAddressAreaCode.equals("0791")) {
            type +=1;
        } else if (answerAddressAreaCode.matches("^079[023456789]$") || answerAddressAreaCode.equals("0701")) {
            type +=2;
        } else {
            type +=3;
        }
        //System.out.println(type/10);
        return type;

    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public String getAnswerAddressAreaCode() {
        return answerAddressAreaCode;
    }

    public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
        this.answerAddressAreaCode = answerAddressAreaCode;
    }

    public String getCallingAddressAreaCode() {
        return callingAddressAreaCode;
    }

    public void setCallingAddressAreaCode(String callingAddressAreaCode) {
        this.callingAddressAreaCode = callingAddressAreaCode;
    }
}

class MessageRecord extends CommunicationRecord {
    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

abstract class CallChargeRule extends ChargeRule {
    //abstract public double calCost(List<CallRecord> callRecords);
}


abstract class ChargeRule{
    abstract public double calCost(UserRecords userRecords);
}

class LandPhoneInCityRule extends CallChargeRule {

    @Override
    public double calCost(UserRecords userRecords) {
        double Sum = 0;
        for (CallRecord call : userRecords.getCallingInCityRecords()) {
            if(call.getCallType() == 11)
                Sum += tools.getTime(call.getEndTime(),call.getStartTime())* 0.1;
            else if(call.getCallType() == 12)
                Sum += tools.getTime(call.getEndTime(),call.getStartTime())* 0.3;
            else if(call.getCallType() == 13)
                Sum += tools.getTime(call.getEndTime(),call.getStartTime())* 0.6;
        }
        return Sum;
    }

}

class LandPhoneInProvinceRule extends CallChargeRule {
    @Override
    public double calCost(UserRecords userRecords) {
        double Sum = 0;
        for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
            Sum += tools.getTime(call.getEndTime(),call.getStartTime())* 0.3;
        }
        return Sum;
    }
}

class LandPhoneInlandRule extends CallChargeRule {
    @Override
    public double calCost(UserRecords userRecords) {
        double Sum = 0;
        for (CallRecord call : userRecords.getCallingInLandRecords()) {
            Sum += tools.getTime(call.getEndTime(),call.getStartTime())* 0.6;
        }
        return Sum;
    }
}

class telPhoneInCityRule extends CallChargeRule {

    @Override
    public double calCost(UserRecords userRecords) {
        double Sum = 0;
        for (CallRecord call : userRecords.getCallingInCityRecords()) {
            if(call.getCallType() == 11)
                Sum += tools.getTime(call.getEndTime(),call.getStartTime())* 0.1;
            else if(call.getCallType() == 12)
                Sum += tools.getTime(call.getEndTime(),call.getStartTime())* 0.2;
            else if(call.getCallType() == 13)
                Sum += tools.getTime(call.getEndTime(),call.getStartTime())* 0.3;
        }
        return Sum;
    }

}

class telPhoneInProvinceRule extends CallChargeRule {
    @Override
    public double calCost(UserRecords userRecords) {
        double Sum = 0;
        for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
            Sum += tools.getTime(call.getEndTime(),call.getStartTime())* 0.3;
        }
        return Sum;
    }
}

class telPhoneInlandRule extends CallChargeRule {
    @Override
    public double calCost(UserRecords userRecords) {
        double Sum = 0;
        for (CallRecord call : userRecords.getCallingInLandRecords()) {
            Sum += tools.getTime(call.getEndTime(),call.getStartTime())* 0.6;
        }
        for (CallRecord call : userRecords.getAnswerInLandRecords()) {
            Sum += tools.getTime(call.getEndTime(),call.getStartTime())* 0.3;

        }
        return Sum;
    }
}


class tools{

    public static int checkFormat(String str) {
        /*String[] str1 = str.split(" ");
        if (str1.length == 2) {
            if (str1[0].matches("u-[0-9]{11,12}")) {
                if (Integer.parseInt(str1[1]) == 0 || Integer.parseInt((str1[1])) == 1 || Integer.parseInt(str1[1]) == 2) {
                    return 1;
                }
            }
        }
        else if (str1.length == 6) {
            if (str1[2].matches("[1-9][0-9]{3}.([1-9]|1[0-2]).([1-9]|[1-2][0-9]|3[0-1])"))
                if (str1[4].matches("[1-9][0-9]{3}.([1-9]|1[0-2]).([1-9]|[1-2][0-9]|3[0-1])"))
                    if (str1[3].matches("([0-1][0-9]|2[0-3])\\:([0-5][0-9])\\:([0-5][0-9])"))
                        if (str1[5].matches("([0-1][0-9]|2[0-3])\\:([0-5][0-9])\\:([0-5][0-9])"))
                            if (str1[0].matches("[t]-0791[0-9]{7,8}")) {
                                if (str1[1].matches(".[0-9]{9,11}"))
                                    return 2;
                            }
        }*/
        if (str.matches("[u]-0791[0-9]{7,8}\\s[0]") || str.matches("[u]-1[0-9]{10}\\s[1]"))
                return 1;
        else if (str.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 -1;
    }

    public static long getTime(Date endTime,Date startTime){
        long t = (endTime.getTime()-startTime.getTime()) ;
        long time = t / 60000;
        if (t % 60000 != 0) {
            time += 1;
        }
        return time;
    }

}

class UsersLibrary {
    List<User> userList = new ArrayList<>();

    public void addNewUser(String s){
        User newUser = new User();
        String[] str = s.split(" ");
        String telNumber = str[0].substring(2);
        if(userList!=null){
            for (User user : userList) {
                if (user.getNumber().equals(telNumber)) {
                    return;
                }
            }
        }
        int type = Integer.parseInt(str[1]);
        newUser.setNumber(telNumber);
        if(type == 0){
            newUser.setChargeMode(new LandlinePhoneCharging());
        }
        else if(type == 1){
            newUser.setChargeMode(new telPhoneCharging());
        }
        UserRecords userRecords = new UserRecords();
        newUser.setUserRecords(userRecords);
        userList.add(newUser);
    }

    public void addNewRecord(String s){
        String[] inputs = s.split(" ");
        User callUser = null, answerUser = null;
        CallRecord callRecord = new CallRecord();
        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];
                callRecord.setCallingAddressAreaCode(inputs[0].substring(0, 4));
                callRecord.setAnswerAddressAreaCode(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) {
                        callRecord.setAnswerAddressAreaCode(inputs[2].substring(0, 3)) ;
                    } else {
                        callRecord.setAnswerAddressAreaCode(inputs[2].substring(0, 4)) ;
                    }
                    callRecord.setCallingAddressAreaCode(inputs[1])  ;
                } else {
                    if (inputs[0].length() == 10) {
                        callRecord.setCallingAddressAreaCode(inputs[0].substring(0, 3)) ;
                    } else {
                        callRecord.setCallingAddressAreaCode(inputs[0].substring(0, 4)) ;
                    }
                    callRecord.setAnswerAddressAreaCode(inputs[2]) ;
                }
            } else if (inputs.length == 8) {
                sd = inputs[4];
                st = inputs[5];
                ed = inputs[6];
                et = inputs[7];
                callRecord.setCallingAddressAreaCode(inputs[1]) ;
                callRecord.setAnswerAddressAreaCode(inputs[3])  ;
            }
        } else if (type == 'm') {

        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault());
        try {
            callRecord.setStartTime(simpleDateFormat.parse(sd + " " + st));
            callRecord.setEndTime(simpleDateFormat.parse(ed + " " + et));
        } catch (ParseException e) {
        }
        String[] str1 = s.split(" ");
        str1[0] = str1[0].substring(2);
        for (User u :  userList) {
            if(str1.length == 6){
                if (u.getNumber().equals(str1[0]))
                    callUser = u;
                if (u.getNumber().equals(str1[1]))
                    answerUser = u;
                if (callUser != null && answerUser != null)
                    break;
            }
            else if(str1.length == 7){
                if (str1[0].charAt(0) != '0'){
                    if (u.getNumber().equals(str1[0]))
                        callUser = u;
                    if (u.getNumber().equals(str1[2]))
                        answerUser = u;
                    if (callUser != null && answerUser != null)
                        break;
                }
                else {
                    if (u.getNumber().equals(str1[0]))
                        callUser = u;
                    if (u.getNumber().equals(str1[1]))
                        answerUser = u;
                    if (callUser != null && answerUser != null)
                        break;
                }
            }
            else if(str1.length == 8){
                if (u.getNumber().equals(str1[0]))
                    callUser = u;
                if (u.getNumber().equals(str1[2]))
                    answerUser = u;
                if (callUser != null && answerUser != null)
                    break;


            }
        }
        if (callUser != null) {
            //System.out.println("call");
            if (callRecord.getCallType()/10 == 1) {
                callUser.getUserRecords().addCallingInCityRecords(callRecord);
            } else if (callRecord.getCallType()/10  == 2) {
                callUser.getUserRecords().addCallingInProvinceRecords(callRecord);
            } else {
                callUser.getUserRecords().addCallingInLandRecords(callRecord);
            }
        }

        if (answerUser != null) {
            //System.out.println("ans");
            if (callRecord.getCallType()%10  == 1) {

                answerUser.getUserRecords().addAnswerInCityRecords(callRecord);
            } else if (callRecord.getCallType()%10  == 2) {
                answerUser.getUserRecords().addAnswerInProvinceRecords(callRecord);
            } else {
                answerUser.getUserRecords().addAnswerInLandRecords(callRecord);
            }
        }
    }
}



public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        UsersLibrary usersLibrary = new UsersLibrary();
        String str = in.nextLine();
        while(!str.equals("end")){
            int mode = tools.checkFormat(str);
            if(mode == 1)
                usersLibrary.addNewUser(str);
            else if(mode == 2)
                usersLibrary.addNewRecord(str);
            str = in.nextLine();
        }
        usersLibrary.userList.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 : usersLibrary.userList) {
            //System.out.println("  " + u.getUserRecords().callingInLandRecords.size()+"  ");
            System.out.println(u.getNumber() +" "+ Double.parseDouble(String.format("%.1f",u.calCost())) +" " +
                    Double.parseDouble( String.format("%.1f",u.calBalance()) )  );
        }

    }

}

类图:
img
SourceMonitor生成的报表内容:
img

代码分析总结: 由于有了上一次作业的经验,对于手机收费规则添加应该比较顺利,但由于我代码机构问题,对于手机记录添加的复杂度比较高,If嵌套次数较多。不过本次作业总题难度不算高,还是比较好实现的。

第八次作业

7-1 电信计费系列3-短信计费

题目: 实现一个简单的电信计费程序,针对手机的短信采用如下计费方式:
1、接收短信免费,发送短信0.1元/条,超过3条0.2元/条,超过5条0.3元/条。
2、如果一次发送短信的字符数量超过10个,按每10个字符一条短信进行计算。

输入:
输入信息包括两种类型
1、逐行输入南昌市手机用户开户的信息,每行一个用户。
格式:u-号码 计费类型 (计费类型包括:0-座机 1-手机实时计费 2-手机A套餐 3-手机短信计费)
例如:u-13305862264 3
座机号码由区号和电话号码拼接而成,电话号码包含7-8位数字,区号最高位是0。
手机号码由11位数字构成,最高位是1。
本题只针对类型3-手机短信计费。
2、逐行输入本月某些用户的短信信息,短信的格式:
m-主叫号码,接收号码,短信内容 (短信内容只能由数字、字母、空格、英文逗号、英文句号组成)
m-18907910010 13305862264 welcome to jiangxi.
m-13305862264 18907910010 thank you.

注意:以上两类信息,先输入所有开户信息,再输入所有通讯信息,最后一行以“end”结束。
输出:
根据输入的详细短信信息,计算所有已开户的用户的当月短信费用(精确到小数点后2位,单位元)。假设每个用户初始余额是100元。
每条短信信息均单独计费后累加,不是将所有信息累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。
错误处理:
输入数据中出现的不符合格式要求的行一律忽略。
本题只做格式的错误判断,无需做内容上不合理的判断,比如同一个电话两条通讯记录的时间有重合、开户号码非南昌市的号码、自己给自己打电话等,此类情况都当成正确的输入计算。但时间的输入必须符合要求,比如不能输入2022.13.61 28:72:65。

本题只考虑短信计费,不考虑通信费用以及月租费。

建议类图:
参见图1、2、3:

img

图1中User是用户类,包括属性:
userRecords (用户记录)、balance(余额)、chargeMode(计费方式)、number(号码)。
ChargeMode是计费方式的抽象类:
chargeRules是计费方式所包含的各种计费规则的集合,ChargeRule类的定义见图3。
getMonthlyRent()方法用于返回月租(monthlyRent)。
UserRecords是用户记录类,保存用户各种通话、短信的记录,
各种计费规则将使用其中的部分或者全部记录。
其属性从上到下依次是:
市内拨打电话、省内(不含市内)拨打电话、省外拨打电话、
市内接听电话、省内(不含市内)接听电话、省外接听电话的记录
以及发送短信、接收短信的记录。

img

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

img
图3是计费规则的相关类,这些类的核心方法是:
calCost(ArrayList callRecords)。
该方法针根据输入参数callRecords中的所有记录计算某用户的某一项费用;如市话费。
输入参数callRecords的约束条件:必须是某一个用户的符合计费规则要求的所有记录。
SendMessageRule是发送短信的计费规则类,用于计算发送短信的费用。
LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三个类分别是座机拨打市内、省内、省外电话的计费规则类,用于实现这三种情况的费用计算。

(提示:可以从UserRecords类中获取各种类型的callRecords)。
注意:以上图中所定义的类不是限定要求,根据实际需要自行补充或修改。

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

u-18907910010 3
m-18907910010 13305862264 aaaaaaaaaaaaaaaaaaaaaaa
end
输出样例:
在这里给出相应的输出。例如:

18907910010 0.3 99.7

输入样例1:

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

u-18907910010 3
m-18907910010 13305862264 aaaaaaaaaaaa
m-18907910010 13305862264 aaaaaaa.
m-18907910010 13305862264 bb,bbbb
end
输出样例1:
在这里给出相应的输出。例如:

18907910010 0.5 99.5

试题分析: 本次作业时电信计费系列的最后一次作业,要求实现短信计费,可以说这次作业时这个系列中最简单的一次作业,由于短信计费与前两次作业并不冲突且本次仅需实现短信计费(不必考虑前两次作业的情况),无论时对正则表达式的修改还是记录短信记录的修改都比较简单,添加计费规则也同上一次作业添加手机计费规则相似,且更简单(无地区不同计费不同的规则),故不作具体分析,代码如下:

源代码展示:

查看代码

import java.util.*;

class User {
    private UserRecords userRecords;//用户记录
    private double balance = 100;//余额
    private ChargeMode chargeMode;//计费方式
    private String number;//号码

    public double calBalance(){
        return balance - (chargeMode.getMonthlyRent() + chargeMode.calCost(userRecords));
    }
    public double calCost(){
        return chargeMode.calCost(userRecords);
    }

    public UserRecords getUserRecords() {
        return userRecords;
    }

    public void setUserRecords(UserRecords userRecords) {
        this.userRecords = userRecords;
    }

    public double getBalance() {
        return balance;
    }

    public ChargeMode getChargeMode() {
        return chargeMode;
    }

    public void setChargeMode(ChargeMode chargeMode) {
        this.chargeMode = chargeMode;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }
}


abstract class ChargeMode {
    protected List<ChargeRule> chargeRules = new ArrayList<>();

    public List<ChargeRule> getChargeRules() {
        return chargeRules;
    }

    public void setChargeRules(List<ChargeRule> chargeRules) {
        this.chargeRules = chargeRules;
    }

    abstract public double calCost(UserRecords userRecords);

    abstract public double getMonthlyRent();

}

class UserRecords {
    List<CallRecord> callingInCityRecords = new ArrayList<>();
    List<CallRecord> callingInProvinceRecords = new ArrayList<>();
    List<CallRecord> callingInLandRecords = new ArrayList<>();
    List<CallRecord> answerInCityRecords = new ArrayList<>();
    List<CallRecord> answerInProvinceRecords = new ArrayList<>();
    List<CallRecord> answerInLandRecords = new ArrayList<>();
    List<MessageRecord> sendMessageRecords = new ArrayList<>();
    List<MessageRecord> receiveMessageRecords = new ArrayList<>();

    public void addCallingInCityRecords(CallRecord callRecord) {
        callingInCityRecords.add(callRecord);
    }

    public void addCallingInProvinceRecords(CallRecord callRecord) {
        callingInProvinceRecords.add(callRecord);
    }

    public void addCallingInLandRecords(CallRecord callRecord) {
        callingInLandRecords.add(callRecord);
    }

    public void addAnswerInCityRecords(CallRecord callRecord) {
        answerInCityRecords.add(callRecord);
    }

    public void addAnswerInProvinceRecords(CallRecord callRecord) {
        answerInProvinceRecords.add(callRecord);
    }

    public void addAnswerInLandRecords(CallRecord callRecord) {
        answerInLandRecords.add(callRecord);
    }

    public void addSendMessageRecords(MessageRecord callRecord) {
        sendMessageRecords.add(callRecord);
    }

    public void addReceiveMessageRecords(MessageRecord callRecord) {
        receiveMessageRecords.add(callRecord);
    }

    public List<CallRecord> getCallingInCityRecords() {
        return callingInCityRecords;
    }

    public List<CallRecord> getCallingInLandRecords() {
        return callingInLandRecords;
    }

    public List<CallRecord> getCallingInProvinceRecords() {
        return callingInProvinceRecords;
    }

    public List<CallRecord> getAnswerInCityRecords() {
        return answerInCityRecords;
    }

    public List<CallRecord> getAnswerInLandRecords() {
        return answerInLandRecords;
    }

    public List<CallRecord> getAnswerInProvinceRecords() {
        return answerInProvinceRecords;
    }

    public List<MessageRecord> getReceiveMessageRecords() {
        return receiveMessageRecords;
    }

    public List<MessageRecord> getSendMessageRecords() {
        return sendMessageRecords;
    }
}



class telPhoneMessageRule 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 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;
    }
}



abstract class CommunicationRecord {

    protected String callingNumber;
    protected String answerNumber;

    public String getAnswerNumber() {
        return answerNumber;
    }

    public void setAnswerNumber(String answerNumber) {
        this.answerNumber = answerNumber;
    }

    public String getCallingNumber() {
        return callingNumber;
    }

    public void setCallingNumber(String callingNumber) {
        this.callingNumber = callingNumber;
    }
}

class CallRecord extends CommunicationRecord {
    private Date startTime;
    private Date endTime;
    private String callingAddressAreaCode;
    private String answerAddressAreaCode;

    public int getCallType() {
        int type = 0;
        if (callingAddressAreaCode.equals("0791")) {
            type +=10;
        } else if (callingAddressAreaCode.matches("^079[023456789]$") || callingAddressAreaCode.equals("0701")) {
            type +=20;
        } else {
            type +=30;
        }

        if (answerAddressAreaCode.equals("0791")) {
            type +=1;
        } else if (answerAddressAreaCode.matches("^079[023456789]$") || answerAddressAreaCode.equals("0701")) {
            type +=2;
        } else {
            type +=3;
        }
        //System.out.println(type/10);
        return type;

    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public String getAnswerAddressAreaCode() {
        return answerAddressAreaCode;
    }

    public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
        this.answerAddressAreaCode = answerAddressAreaCode;
    }

    public String getCallingAddressAreaCode() {
        return callingAddressAreaCode;
    }

    public void setCallingAddressAreaCode(String callingAddressAreaCode) {
        this.callingAddressAreaCode = callingAddressAreaCode;
    }
}

class telPhoneMessageCharging extends ChargeMode{

    private double monthlyRent = 0;

    public telPhoneMessageCharging() {
        super();
        chargeRules.add(new telPhoneMessageRule());
    }

    @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 telPhoneCharging extends ChargeMode {

    private double monthlyRent = 15;

    public telPhoneCharging() {
        super();
        chargeRules.add(new telPhoneInCityRule());
        chargeRules.add(new telPhoneInProvinceRule());
        chargeRules.add(new telPhoneInlandRule());
    }
    @Override
    public double calCost(UserRecords userRecords){
        double Sum = 0;
        //System.out.println("?");
        //System.out.println(chargeRules.size());
        for (ChargeRule chargeRule : chargeRules) {
            Sum += chargeRule.calCost(userRecords);
            // System.out.println("sum = "+Sum);
        }
        return Sum;
    }

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


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 Sum = 0;
        //System.out.println("?");
        //System.out.println(chargeRules.size());
        for (ChargeRule chargeRule : chargeRules) {
            Sum += chargeRule.calCost(userRecords);
            //System.out.println("sum = "+Sum);
        }
        return Sum;
    }

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

}

abstract class CallChargeRule extends ChargeRule {
    //abstract public double calCost(List<CallRecord> callRecords);
}


abstract class ChargeRule{
    abstract public double calCost(UserRecords userRecords);
}

class LandPhoneInCityRule extends CallChargeRule {

    @Override
    public double calCost(UserRecords userRecords) {
        double Sum = 0;
        for (CallRecord call : userRecords.getCallingInCityRecords()) {
            if(call.getCallType() == 11)
                Sum += tools.getTime(call.getEndTime(),call.getStartTime())* 0.1;
            else if(call.getCallType() == 12)
                Sum += tools.getTime(call.getEndTime(),call.getStartTime())* 0.3;
            else if(call.getCallType() == 13)
                Sum += tools.getTime(call.getEndTime(),call.getStartTime())* 0.6;
        }
        return Sum;
    }

}

class LandPhoneInProvinceRule extends CallChargeRule {
    @Override
    public double calCost(UserRecords userRecords) {
        double Sum = 0;
        for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
            Sum += tools.getTime(call.getEndTime(),call.getStartTime())* 0.3;
        }
        return Sum;
    }
}

class LandPhoneInlandRule extends CallChargeRule {
    @Override
    public double calCost(UserRecords userRecords) {
        double Sum = 0;
        for (CallRecord call : userRecords.getCallingInLandRecords()) {
            Sum += tools.getTime(call.getEndTime(),call.getStartTime())* 0.6;
        }
        return Sum;
    }
}

class telPhoneInCityRule extends CallChargeRule {

    @Override
    public double calCost(UserRecords userRecords) {
        double Sum = 0;
        for (CallRecord call : userRecords.getCallingInCityRecords()) {
            if(call.getCallType() == 11)
                Sum += tools.getTime(call.getEndTime(),call.getStartTime())* 0.1;
            else if(call.getCallType() == 12)
                Sum += tools.getTime(call.getEndTime(),call.getStartTime())* 0.2;
            else if(call.getCallType() == 13)
                Sum += tools.getTime(call.getEndTime(),call.getStartTime())* 0.3;
        }
        return Sum;
    }

}

class telPhoneInProvinceRule extends CallChargeRule {
    @Override
    public double calCost(UserRecords userRecords) {
        double Sum = 0;
        for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
            Sum += tools.getTime(call.getEndTime(),call.getStartTime())* 0.3;
        }
        return Sum;
    }
}

class telPhoneInlandRule extends CallChargeRule {
    @Override
    public double calCost(UserRecords userRecords) {
        double Sum = 0;
        for (CallRecord call : userRecords.getCallingInLandRecords()) {
            Sum += tools.getTime(call.getEndTime(),call.getStartTime())* 0.6;
        }
        for (CallRecord call : userRecords.getAnswerInLandRecords()) {
            Sum += tools.getTime(call.getEndTime(),call.getStartTime())* 0.3;

        }
        return Sum;
    }
}


class tools{

    public static int checkFormat(String str) {
        /*String[] str1 = str.split(" ");
        if (str1.length == 2) {
            if (str1[0].matches("u-[0-9]{11,12}")) {
                if (Integer.parseInt(str1[1]) == 0 || Integer.parseInt((str1[1])) == 1 || Integer.parseInt(str1[1]) == 2) {
                    return 1;
                }
            }
        }
        else if (str1.length == 6) {
            if (str1[2].matches("[1-9][0-9]{3}.([1-9]|1[0-2]).([1-9]|[1-2][0-9]|3[0-1])"))
                if (str1[4].matches("[1-9][0-9]{3}.([1-9]|1[0-2]).([1-9]|[1-2][0-9]|3[0-1])"))
                    if (str1[3].matches("([0-1][0-9]|2[0-3])\\:([0-5][0-9])\\:([0-5][0-9])"))
                        if (str1[5].matches("([0-1][0-9]|2[0-3])\\:([0-5][0-9])\\:([0-5][0-9])"))
                            if (str1[0].matches("[t]-0791[0-9]{7,8}")) {
                                if (str1[1].matches(".[0-9]{9,11}"))
                                    return 2;
                            }
        }*/
        if (str.matches("[u]-0791[0-9]{7,8}\\s[0]") || str.matches("[u]-1[0-9]{10}\\s[1]"))
            return 1;
        else if (str.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 -1;
    }


    public static int checkMessage(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 static long getTime(Date endTime,Date startTime){
        long t = (endTime.getTime()-startTime.getTime()) ;
        long time = t / 60000;
        if (t % 60000 != 0) {
            time += 1;
        }
        return time;
    }

}

class UsersLibrary {
    List<User> userList = new ArrayList<>();

    public void addNewUser(String input){
        User user = new User();
        String[] inputs = input.split(" ");
        String tel = inputs[0].substring(2);
        for (User u : userList) {
            if (u.getNumber().equals(tel)) {
                return;
            }
        }
        user.setNumber(tel);
        int mode = Integer.parseInt(inputs[1]);
        if (mode == 0) {
            user.setChargeMode(new LandlinePhoneCharging());
        } else if (mode == 1) {
            user.setChargeMode(new telPhoneCharging());
        } else if (mode == 3) {
            user.setChargeMode(new telPhoneMessageCharging());
        }
        UserRecords userRecords = new UserRecords();
        user.setUserRecords(userRecords);
        userList.add(user);


    }

    public void addNewRecord(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 u : userList) {
            if (u.getNumber().equals(out)) {
                callu = u;
            }
            if (u.getNumber().equals(in)) {
                answeru = u;
            }
            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);
            }
        }

    }



}



public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        UsersLibrary usersLibrary = new UsersLibrary();
        String str = in.nextLine();
        while(!str.equals("end")){
            //int mode = tools.checkFormat(str);
            int mode = tools.checkMessage(str);
            if(mode == 1)
                usersLibrary.addNewUser(str);
            else if(mode == 2)
                usersLibrary.addNewRecord(str);
            str = in.nextLine();
        }
        usersLibrary.userList.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 : usersLibrary.userList) {
            //System.out.println("  " + u.getUserRecords().callingInLandRecords.size()+"  ");
            System.out.println(u.getNumber() +" "+ Double.parseDouble(String.format("%.1f",u.calCost())) +" " +
                    Double.parseDouble( String.format("%.1f",u.calBalance()) )  );
        }

    }

}


类图: img

SourceMonitor生成的报表内容:
img
代码分析总结: 题目比较简单,有了前两次作业的经验,根据类图实现即可,计费规则也很简单只有一条,格式判断相较于上次也很简单,注意细节即可。

3. 采坑心得:

第一次和第二次作业的正则表达式可能有些难度,需要仔细思考下怎么考虑所有情况,也需要考虑是一次判断所有还是不同情况分开判断。第二次作业的通话记录方面要注意手机通话有三种情况,手机打手机,手机打座机,座机打手机,这其中二三种情况记录长度是一样的,需要更细致的判断,不然容易被卡住。第三次作业注意短信不能用sqlit分组,因为短信里是可以发空格的,别的地方还是比较简单,实现类图即可。

4. 改进心得:

由于本次是实现给出的架构,总体设计方面还是比较好的,复用性很高,不足的地方就是对信息输入部分没有想到一个较好的方式实现较高的复用性,这部分内容我认为也是比较重要的,可以拿出来专门思考,看是否能通过算法,用一个较好的结构实现保存输入信息。

5. 总结:

这次大作业,让我明白了结构的重要性,解决问题要先考虑解决思路,设计比实现更重要,更应该是我们重视的地方,老师也多次指出,培养设计思路才更具有竞争力,毕竟拿着设计好的框架实现确实简单,所以我们更应该注重设计,而不是只会编码。除此之外还认识到了解决问题可以采用分而治之的方式,将问题化小,用不同的类解决,而类与类之间又可以总体进行统筹规划,实现大体不变而内部可以千变万化,认识到了java的核心竞争力。

posted on   昌航小迷弟五号  阅读(44)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 提示词工程——AI应用必不可少的技术
· 地球OL攻略 —— 某应届生求职总结
· 字符编码:从基础到乱码解决
· SpringCloud带你走进微服务的世界
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

Document
点击右上角即可分享
微信分享提示