首页 > 其他分享 >blog_03

blog_03

时间:2022-12-04 15:33:47浏览次数:58  
标签:03 return String inputs double ArrayList blog public

第三次博客作业

目录

1.前言

 (1).题目集6的7-1是座机计费,主要考查了抽象类,继承与多态等知识,题目中已经给出了相应的类图,写代码时可以参考给出的类图,根据要求进一步补充内容。本次题目集的题量和难度都适中,主要还是得理清楚各个类之间的关系。
 (2).题目集7的7-1是手机+座机计费,相对于题目集6中的座机计费新增加了手机计费的功能,需要考虑四种情况,座机打座机,座机打手机,手机打座机,手机打手机,计费方式也新增了漫游,因此需要增加一些新的类,类与类之间的关系变复杂了,写代码的时候有点绕不过来了,难度有所提高。
 (3).题目集8的7-1是短信计费,本次题目相对于前两次的题目量减少了不少,因为本题不用考虑前两次题目中的通话计费,只需要考虑短信的费用,因此类之间的关系简单了不少,难度有所降低。

2.设计与分析

(1)题目集6 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<CallRecord> 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

试题分析:

User是用户类,包括属性:userRecords (用户记录)、balance(余额)、chargeMode(计费方式)、number(号码)。
ChargeMode是计费方式的抽象类:
chargeRules是计费方式所包含的各种计费规则的集合,ChargeRule类的定义见图3。
getMonthlyRent()方法用于返回月租(monthlyRent)。
UserRecords是用户记录类,保存用户各种通话、短信的记录,各种计费规则将使用其中的部分或者全部记录。
CommunicationRecord是抽象的通讯记录类: 包含callingNumber拨打号码、answerNumber接听号码两个属性。CallRecord(通话记录)、MessageRecord(短信记录)是它的子类。
CallRecord(通话记录类)包含属性: 通话的起始、结束时间以及拨号地点的区号(callingAddressAreaCode)、接听地点的区号(answerAddressAreaCode)。 区号用于记录在哪个地点拨打和接听的电话。座机无法移动,就是本机区号,如果是手机号,则会有差异。
计费规则的相关类,这些类的核心方法是:calCost(ArrayList callRecords), 该方法针根据输入参数callRecords中的所有记录计算某用户的某一项费用;如市话费。 输入参数callRecords的约束条件:必须是某一个用户的符合计费规则要求的所有记录。
LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三个类分别是 座机拨打市内、省内、省外电话的计费规则类,用于实现这三种情况的费用计算。

源代码展示:


import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws ParseException {
        Start start = new Start();
        start.Start();
    }
}

class Start {
    public void Start() throws ParseException {
        ArrayList<User> users = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        int tCount = 0;
        while (!s.equals("end")) {
            if (s.matches("u-07(9[0-9]|01)[0-9]{7,8} [0-3]")
                    || s.matches("t-0\\d{9,11} 0\\d{9,11}(( \\d{4}\\.([1-9]|(1[0-2]))" +
                    "\\.([1-9]|([1-2][0-9])|3[0-1]) (([0-1][0-9])" +
                    "|(2[0-3])):([0-5][0-9]):([0-5][0-9])){2})")) {
                if (s.startsWith("u-")) {
                    s = s.substring(2);
                    String[] strings = s.split(" ");
                    String num = strings[0];
                    String mod = strings[1];
                    if (Double.parseDouble(mod) == 0) {
                        User u = new User();
                        u.setNumber(num);
                        u.setChargeMode(new LandlinePhoneCharging());
                        users.add(u);
                        users = DeleteSameUser(users);
                    }
                } else if (s.startsWith("t-")) {
                    tCount++;
                    s = s.substring(2);
                    String[] strings = s.split(" ");
                    String num1 = strings[0];
                    String num2 = strings[1];
                    String date1 = strings[2] + " " + strings[3];
                    String date2 = strings[4] + " " + strings[5];
                    String[] info = {num1, num2, date1, date2};
                    User callUser = null, answerUser = null;
                    CallRecord callrecord = new CallRecord();
                    callrecord.CallRecord(info);

                    for (User i : users) {
                        if (i.getNumber().equals(info[0])) {
                            callUser = i;
                        }
                        if (i.getNumber().equals(info[1])) {
                            answerUser = i;
                        }
                        if (callUser != null && answerUser != null) {
                            break;
                        }
                    }
                    if (callUser != null) {
                        if (callrecord.getCallType() == 1) {
                            callUser.getUserRecords().setCallingInCityRecords(callrecord);
                        } else if (callrecord.getCallType() == 2) {
                            callUser.getUserRecords().setCallingInProvinceRecords(callrecord);
                        } else {
                            callUser.getUserRecords().setCallingInLandRecords(callrecord);
                        }
                    }

                    if (answerUser != null) {
                        if (callrecord.getCallType() == 1) {
                            answerUser.getUserRecords().setAnswerInCityRecords(callrecord);
                        } else if (callrecord.getCallType() == 2) {
                            answerUser.getUserRecords().setAnswerInProvinceRecords(callrecord);
                        } else {
                            answerUser.getUserRecords().setAnswerInLandRecords(callrecord);
                        }
                    }
                }
            }
            s = sc.nextLine();
        }
        users.sort(new Comparator<User>() {
            @Override
            public int compare(User u1, User u2) {
                if (Double.parseDouble(u1.getNumber()) > Double.parseDouble(u2.getNumber())) {
                    return 1;
                }
                return -1;
            }
        });
        for (User i : users) {
            double out = 0;
            LandPhoneInlandRule landPhoneInlandRule = new LandPhoneInlandRule();
            LandPhoneInCityRule landPhoneInCityRule = new LandPhoneInCityRule();
            LandPhoneInProvinceRule landPhoneInProvinceRule = new LandPhoneInProvinceRule();
            if (i.getUserRecords().getCallingInLandRecords().size() >= 1) {
                out += outPut(landPhoneInlandRule.calCost(i.getUserRecords()));
            }
            if (i.getUserRecords().getCallingInCityRecords().size() >= 1) {
                out += outPut(landPhoneInCityRule.calCost(i.getUserRecords()));
            }
            if (i.getUserRecords().getCallingInProvinceRecords().size() >= 1) {
                out += outPut(landPhoneInProvinceRule.calCost(i.getUserRecords()));
            }
            System.out.print(i.getNumber() + " " + out + " " + outPut(i.calBalance()) + "\n");
        }
    }

    public static double outPut(double out) {
        BigDecimal bigDecimal = new BigDecimal(out);
        out = bigDecimal.setScale(2, RoundingMode.HALF_UP).doubleValue();
        return out;
    }

    public static ArrayList<User> DeleteSameUser(ArrayList<User> users) {
        if (users.size() < 2) return users;
        for (int i = 0; i < users.size() - 1; i++) {
            for (int j = i + 1; j < users.size(); j++) {
                if (users.get(i).getNumber().equals(users.get(j).getNumber())) {
                    users.remove(j);
                }
            }
        }
        return users;
    }
}

class MessageRecord extends CommunicationRecord {
    private String message;

    public String getMessage() {
        return message;
    }

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

class User {
    private UserRecords userRecords = new UserRecords();
    private double balance = 100;
    private ChargeMode chargeMode;
    private String number;

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

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

class UserRecords {
    //市内呼叫
    private ArrayList<CallRecord> callingInCityRecords = new ArrayList<>();
    //省内呼叫
    private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<>();
    //大陆内呼叫
    private ArrayList<CallRecord> callingInLandRecords = new ArrayList<>();
    //市内接收
    private ArrayList<CallRecord> answerInCityRecords = new ArrayList<>();
    //省内接收
    private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<>();
    //大陆内接收
    private ArrayList<CallRecord> answerInLandRecords = new ArrayList<>();
    //发消息
    private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<>();
    //收消息
    private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<>();

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

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

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

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

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

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


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

    public void setCallingInCityRecords(ArrayList<CallRecord> callingInCityRecords) {
        this.callingInCityRecords = callingInCityRecords;
    }

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

    public void setCallingInProvinceRecords(ArrayList<CallRecord> callingInProvinceRecords) {
        this.callingInProvinceRecords = callingInProvinceRecords;
    }

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

    public void setCallingInLandRecords(ArrayList<CallRecord> callingInLandRecords) {

        this.callingInLandRecords = callingInLandRecords;
    }

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

    public void setAnswerInCityRecords(ArrayList<CallRecord> answerInCityRecords) {
        this.answerInCityRecords = answerInCityRecords;
    }

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

    public void setAnswerInProvinceRecords(ArrayList<CallRecord> answerInProvinceRecords) {
        this.answerInProvinceRecords = answerInProvinceRecords;
    }

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

    public void setAnswerInLandRecords(ArrayList<CallRecord> answerInLandRecords) {
        this.answerInLandRecords = answerInLandRecords;
    }

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

    public void setSendMessageRecords(ArrayList<MessageRecord> sendMessageRecords) {
        this.sendMessageRecords = sendMessageRecords;
    }

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

    public void setReceiveMessageRecords(ArrayList<MessageRecord> receiveMessageRecords) {
        this.receiveMessageRecords = receiveMessageRecords;
    }
}

abstract class CallChargeRule extends ChargeRule {
    public long GetMin(CallRecord callRecord) {
        long startDate = callRecord.getStartTime().getTime();
        long endDate = callRecord.getEndTime().getTime();
        long diff = endDate - startDate;
        long day = diff / (24 * 60 * 60 * 1000);
        long hour = diff / (60 * 60 * 1000) - day * 24;
        long min = diff / (60 * 1000) - day * 24 * 60 - hour * 60;
        long sec = diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60;
        if (sec != 0) {
            min += 1;
        }
        return day * 24 * 60 + hour * 60 + min;
    }
}

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

    public int getCallType() {
        int sameCity = 1, sameProvince = 2, diff = 3;
        if (callingAddressAreaCode.equals(answerAddressAreaCode)) {
            return sameCity;
        }
        if (callingAddressAreaCode.matches("^079[0-9]$") || callingAddressAreaCode.equals("0701")) {
            if (answerAddressAreaCode.matches("^079[0-9]$") || answerAddressAreaCode.equals("0701")) {
                return sameProvince;
            }
        }
        return diff;
    }

    public void CallRecord(String[] s) throws ParseException {
        super.setCallingNumber(s[0]);
        super.setAnswerNumber(s[1]);
        if (s[0].length() == 10) {
            callingAddressAreaCode = s[0].substring(0, 3);
        } else {
            callingAddressAreaCode = s[0].substring(0, 4);
        }
        if (s[1].length() == 10) {
            answerAddressAreaCode = s[1].substring(0, 3);
        } else {
            answerAddressAreaCode = s[1].substring(0, 4);
        }
        startTime = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").parse(s[2]);
        endTime = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").parse(s[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 getCallingAddressAreaCode() {
        return callingAddressAreaCode;
    }

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

    public String getAnswerAddressAreaCode() {
        return answerAddressAreaCode;
    }

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


    public String getCallingNumber() {
        return super.getCallingNumber();
    }

    public String getAnswerNumber() {
        return super.getAnswerNumber();
    }
}

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

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

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

    public abstract double calCost(UserRecords userRecords);

    public abstract double getMonthlyRent();
}

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

abstract class CommunicationRecord {
    private String callingNumber;
    private String answerNumber;

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

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

    public String getCallingNumber() {
        return callingNumber;
    }

    public String getAnswerNumber() {
        return answerNumber;
    }
}

class LandlinePhoneCharging extends ChargeMode {
    private final double monthlyRent = 20;

    public LandlinePhoneCharging() {
        super();
        chargeRules.add(new LandPhoneInCityRule());
        chargeRules.add(new LandPhoneInProvinceRule());
        chargeRules.add(new LandPhoneInlandRule());
    }

    public ArrayList<ChargeRule> getChargeRules() {
        return super.getChargeRules();
    }

    public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
        super.setChargeRules(chargeRules);
    }

    @Override
    public double calCost(UserRecords userRecords) {
        double sum = 0;
        for (ChargeRule i : chargeRules) {
            sum += i.calCost(userRecords);
        }
        return sum;
    }

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

class LandPhoneInCityRule extends CallChargeRule {

    @Override
    public double calCost(UserRecords userRecords) {
        double sum = 0;
        long min;
        for (CallRecord i : userRecords.getCallingInCityRecords()) {
            min = GetMin(i);
            sum += min * 0.1;
        }
        return sum;
    }
}

class LandPhoneInlandRule extends CallChargeRule {

    @Override
    public double calCost(UserRecords userRecords) {
        double sum = 0;
        long min;
        for (CallRecord i : userRecords.getCallingInLandRecords()) {
            min = GetMin(i);
            sum += min * 0.6;
        }
        return sum;
    }
}

class LandPhoneInProvinceRule extends CallChargeRule {

    @Override
    public double calCost(UserRecords userRecords) {
        double sum = 0;
        long min;
        for (CallRecord i : userRecords.getCallingInProvinceRecords()) {
            min = GetMin(i);
            sum += min * 0.3;
        }
        return sum;
    }
}

SourceMonitor生成的报表内容:
img

代码分析总结:
  从上述的SourceMonitor生成的报表内容来看,我本题代码的平均复杂度、平均深度、最大深度以及方法和类的比值均达到了圈复杂度的期望,但是最大复杂度和语句与方法的比值仍然有不足之处,还需要改进。从柱状图可以看出我本题代码的块深度大多数在1和2,但是最大的块深度到达了7,有点偏大,应该在类的构建和方法调用方面还有待改进。

(2)题目集7 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<CallRecord> callRecords)。
该方法针根据输入参数callRecords中的所有记录计算某用户的某一项费用;如市话费。
输入参数callRecords的约束条件:必须是某一个用户的符合计费规则要求的所有记录。
SendMessageRule是发送短信的计费规则类,用于计算发送短信的费用。
LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三个类分别是座机拨打市内、省内、省外电话的计费规则类,用于实现这三种情况的费用计算。

试题分析:
User是用户类,包括属性:userRecords (用户记录)、balance(余额)、chargeMode(计费方式)、number(号码)。
ChargeMode是计费方式的抽象类:chargeRules是计费方式所包含的各种计费规则的集合。
getMonthlyRent()方法用于返回月租(monthlyRent)。
UserRecords是用户记录类,保存用户各种通话、短信的记录,各种计费规则将使用其中的部分或者全部记录。其属性从上到下依次是:市内拨打电话、省内(不含市内)拨打电话、省外拨打电话、市内接听电话、省内(不含市内)接听电话、省外接听电话的记录以及发送短信、接收短信的记录。
CommunicationRecord是抽象的通讯记录类:包含callingNumber拨打号码、answerNumber接听号码两个属性。
CallRecord(通话记录)、MessageRecord(短信记录)是它的子类。CallRecord(通话记录类)包含属性:通话的起始、结束时间以及拨号地点的区号(callingAddressAreaCode)、接听地点的区号(answerAddressAreaCode)。区号用于记录在哪个地点拨打和接听的电话。座机无法移动,就是本机区号,如果是手机号,则会有差异。
calCost(ArrayList callRecords)。该方法针根据输入参数callRecords中的所有记录计算某用户的某一项费用;如市话费。
输入参数callRecords的约束条件:必须是某一个用户的符合计费规则要求的所有记录。
SendMessageRule是发送短信的计费规则类,用于计算发送短信的费用。LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三个类分别是座机拨打市内、省内、省外电话的计费规则类,用于实现这三种情况的费用计算。

源代码展示:

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


public class Main {

    public static void main(String[] args) {

        OutPut outPut = new OutPut();

        InputProcessing inputProcessing = new InputProcessing();

        ArrayList<User> users = new ArrayList<>();

        Scanner in = new Scanner(System.in);

        String input = in.nextLine();

        while (!input.equals("end")) {
            if (1 == inputProcessing.checkInput(input)) {
                inputProcessing.writeUser(users, input);
            } else if (2 == inputProcessing.checkInput(input)) {
                inputProcessing.setRecord(users, input);
            }
            input = in.nextLine();
        }

        users.sort(new Comparator<User>() {

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

        for (User u : users) {
            System.out.print(u.getNumber() + " ");
            outPut.output(u.calCost());
            System.out.print(" ");
            outPut.output(u.calBalance());
            System.out.println();

        }

    }

}

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

    public abstract double calCost(UserRecords userRecords);

    public abstract double getMonthlyRent();

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

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

class UserRecords {

    private ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();
    private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();
    private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();

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

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

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

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

    public void 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 ArrayList<CallRecord> getCallingInCityRecords() {
        return callingInCityRecords;
    }

    public void setCallingInCityRecords(ArrayList<CallRecord> callingInCityRecords) {
        this.callingInCityRecords = callingInCityRecords;
    }

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

    public void setCallingInProvinceRecords(ArrayList<CallRecord> callingInProvinceRecords) {
        this.callingInProvinceRecords = callingInProvinceRecords;
    }

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

    public void setCallingInLandRecords(ArrayList<CallRecord> callingInLandRecords) {
        this.callingInLandRecords = callingInLandRecords;
    }

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

    public void setAnswerInCityRecords(ArrayList<CallRecord> answerInCityRecords) {
        this.answerInCityRecords = answerInCityRecords;
    }

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

    public void setAnswerInProvinceRecords(ArrayList<CallRecord> answerInProvinceRecords) {
        this.answerInProvinceRecords = answerInProvinceRecords;
    }

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

    public void setAnswerInLandRecords(ArrayList<CallRecord> answerInLandRecords) {
        this.answerInLandRecords = answerInLandRecords;
    }

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

    public void setSendMessageRecords(ArrayList<MessageRecord> sendMessageRecords) {
        this.sendMessageRecords = sendMessageRecords;
    }

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

    public void setReceiveMessageRecords(ArrayList<MessageRecord> receiveMessageRecords) {
        this.receiveMessageRecords = receiveMessageRecords;
    }

}

class LandlinePhoneCharging extends ChargeMode {

    private double monthlyRent = 20;

    public LandlinePhoneCharging() {
        super();
        chargeRules.add(new LandPhoneInCityRule());
        chargeRules.add(new LandPhoneInProvinceRule());
        chargeRules.add(new LandPhoneInlandRule());
    }

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

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

}

class MobilePhoneCharging extends ChargeMode {

    private double monthlyRent = 15;

    public MobilePhoneCharging() {
        super();
        chargeRules.add(new MobilePhoneInCityRule());
        chargeRules.add(new MobilePhoneInProvinceRule());
        chargeRules.add(new MobilePhoneInlandRule());
    }

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

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

}

class InputProcessing {

    public int checkInput(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;
    }

    /*
        private boolean validated(String string) {
            if (!string.matches("^([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$")) {
                return false;
            }
            return true;
        }
    
        public static boolean validate(String dateString) {
            // 使用正则表达式 测试 字符 符合 dddd.dd.dd 的格式(d表示数字)
            Pattern p = Pattern.compile("\\d{4}+[\\.]\\d{1,2}+[\\.]\\d{1,2}+");
            Matcher m = p.matcher(dateString);
            if (!m.matches()) {
                return false;
            }
    
            // 得到年月日
            String[] array = dateString.split("\\.");
            int year = Integer.valueOf(array[0]);
            int month = Integer.valueOf(array[1]);
            int day = Integer.valueOf(array[2]);
    
            if (month < 1 || month > 12) {
                return false;
            }
            int[] monthLengths = new int[] { 0, 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
            if (isLeapYear(year)) {
                monthLengths[2] = 29;
            } else {
                monthLengths[2] = 28;
            }
            int monthLength = monthLengths[month];
            if (day < 1 || day > monthLength) {
                return false;
            }
            return true;
        }
    
        // 是否是闰年 
        private static boolean isLeapYear(int year) {
            return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
        }
    */
    public void writeUser(ArrayList<User> users, String input) {
        User newUser = new User();
        String[] inputs = input.split(" ");
        String num = inputs[0].substring(2);
        for (User i : users) {
            if (i.getNumber().equals(num)) {
                return;
            }
        }
        newUser.setNumber(num);
        int mode = Integer.parseInt(inputs[1]);
        if (mode == 0) {
            newUser.setChargeMode(new LandlinePhoneCharging());
        } else if (mode == 1) {
            newUser.setChargeMode(new MobilePhoneCharging());
        }
        users.add(newUser);
    }

    public void setRecord(ArrayList<User> users, String input) {
        String[] inputs = input.split(" ");

        User callUser = null, answerUser = 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)) {
                    callUser = i;
                }
                if (i.getNumber().equals(in)) {
                    answerUser = i;
                }
                if (callUser != null && answerUser != null) {
                    break;
                }
            }

            if (callUser != null) {
                if (callrecord.getCallType().matches("^1[1-3]$")) {
                    callUser.getUserRecords().addCallingInCityRecords(callrecord);
                } else if (callrecord.getCallType().matches("^2[1-3]$")) {
                    callUser.getUserRecords().addCallingInProvinceRecords(callrecord);
                } else {
                    callUser.getUserRecords().addCallingInLandRecords(callrecord);
                }
            }

            if (answerUser != null) {
                if (callrecord.getCallType().matches("^[1-3]1$")) {
                    answerUser.getUserRecords().addAnswerInCityRecords(callrecord);
                } else if (callrecord.getCallType().matches("^[1-3]2$")) {
                    answerUser.getUserRecords().addAnswerInProvinceRecords(callrecord);
                } else {
                    answerUser.getUserRecords().addAnswerInLandRecords(callrecord);
                }
            }
        } else if (input.charAt(0) == 'm') {

        }

    }

}

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

}

abstract class ChargeRule {

    abstract public double calCost(UserRecords userRecords);

}

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

    public String getCallType() {
        String type = "";
        if (callingAddressAreaCode.equals("0791")) {
            type = type.concat("1");
        } else if (callingAddressAreaCode.matches("^079[023456789]$") || callingAddressAreaCode.equals("0701")) {
            type = type.concat("2");
        } else {
            type = type.concat("3");
        }

        if (answerAddressAreaCode.equals("0791")) {
            type = type.concat("1");
        } else if (answerAddressAreaCode.matches("^079[023456789]$") || answerAddressAreaCode.equals("0701")) {
            type = type.concat("2");
        } else {
            type = type.concat("3");
        }

        return type;
    }

    public CallRecord(String[] inputs) {
        super();

        char type = inputs[0].charAt(0);
        inputs[0] = inputs[0].substring(2);

        String sd = null, st = null, ed = null, et = null;

        if (type == 't') {
            if (inputs.length == 6) {
                sd = inputs[2];
                st = inputs[3];
                ed = inputs[4];
                et = inputs[5];
                callingAddressAreaCode = inputs[0].substring(0, 4);
                answerAddressAreaCode = inputs[1].substring(0, 4);
            } else if (inputs.length == 7) {
                sd = inputs[3];
                st = inputs[4];
                ed = inputs[5];
                et = inputs[6];
                if (inputs[0].charAt(0) != '0') {
                    if (inputs[2].length() == 10) {
                        answerAddressAreaCode = inputs[2].substring(0, 3);
                    } else {
                        answerAddressAreaCode = inputs[2].substring(0, 4);
                    }
                    callingAddressAreaCode = inputs[1];
                } else {
                    if (inputs[0].length() == 10) {
                        callingAddressAreaCode = inputs[0].substring(0, 3);
                    } else {
                        callingAddressAreaCode = inputs[0].substring(0, 4);
                    }
                    answerAddressAreaCode = inputs[2];
                }
            } else if (inputs.length == 8) {
                sd = inputs[4];
                st = inputs[5];
                ed = inputs[6];
                et = inputs[7];
                callingAddressAreaCode = inputs[1];
                answerAddressAreaCode = inputs[3];
            }
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault());
        try {
            startTime = simpleDateFormat.parse(sd + " " + st);
            endTime = simpleDateFormat.parse(ed + " " + et);
        } catch (ParseException e) {
        }

    }

    public CallRecord(Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) {
        super();
        this.startTime = startTime;
        this.endTime = endTime;
        this.callingAddressAreaCode = callingAddressAreaCode;
        this.answerAddressAreaCode = answerAddressAreaCode;
    }

    public Date getStartTime() {
        return startTime;
    }

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

    public Date getEndTime() {
        return endTime;
    }

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

    public String getCallingAddressAreaCode() {
        return callingAddressAreaCode;
    }

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

    public String getAnswerAddressAreaCode() {
        return answerAddressAreaCode;
    }

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

abstract class CallChargeRule extends ChargeRule {}

class LandPhoneInCityRule extends CallChargeRule {

    @Override
    public double calCost(UserRecords userRecords) {
        double totalCost = 0;
        for (CallRecord call : userRecords.getCallingInCityRecords()) {
            double distanceSec = (call.getEndTime().getTime() - call.getStartTime().getTime()) / 1000;
            if (distanceSec < 0) {
                continue;
            }
            double distanceMin = (int) distanceSec / 60;
            if (distanceSec % 60 != 0) {
                distanceMin += 1;
            }
            if (call.getCallType().equals("11")) {
                totalCost += distanceMin * 0.1;
            } else if (call.getCallType().equals("12")) {
                totalCost += distanceMin * 0.3;
            } else if (call.getCallType().equals("13")) {
                totalCost += distanceMin * 0.6;
            }
        }
        return totalCost;
    }

}

class LandPhoneInlandRule extends CallChargeRule {

    @Override
    public double calCost(UserRecords userRecords) {
        double totalCost = 0;
        for (CallRecord call : userRecords.getCallingInLandRecords()) {
            double distanceSec = (call.getEndTime().getTime() - call.getStartTime().getTime()) / 1000;
            if (distanceSec < 0) {
                continue;
            }
            double distanceMin = (int) distanceSec / 60;
            if (distanceSec % 60 != 0) {
                distanceMin += 1;
            }
            totalCost += distanceMin * 0.6;
        }
        return totalCost;
    }

}

class LandPhoneInProvinceRule extends CallChargeRule {

    @Override
    public double calCost(UserRecords userRecords) {
        double totalCost = 0;
        for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
            double distanceSec = (call.getEndTime().getTime() - call.getStartTime().getTime()) / 1000;
            if (distanceSec < 0) {
                continue;
            }
            double distanceMin = (int) distanceSec / 60;
            if (distanceSec % 60 != 0) {
                distanceMin += 1;
            }
            totalCost += distanceMin * 0.3;
        }
        return totalCost;
    }

}

class MobilePhoneInCityRule extends CallChargeRule {

    @Override
    public double calCost(UserRecords userRecords) {
        double totalCost = 0;
        for (CallRecord call : userRecords.getCallingInCityRecords()) {
            double distanceSec = (call.getEndTime().getTime() - call.getStartTime().getTime()) / 1000;
            if (distanceSec < 0) {
                continue;
            }
            double distanceMin = (int) distanceSec / 60;
            if (distanceSec % 60 != 0) {
                distanceMin += 1;
            }
            if (call.getCallType().equals("11")) {
                totalCost += distanceMin * 0.1;
            } else if (call.getCallType().equals("12")) {
                totalCost += distanceMin * 0.2;
            } else if (call.getCallType().equals("13")) {
                totalCost += distanceMin * 0.3;
            }

        }
        return totalCost;
    }

}

class MobilePhoneInlandRule extends CallChargeRule {
    @Override
    public double calCost(UserRecords userRecords) {
        double totalCost = 0;
        for (CallRecord call : userRecords.getCallingInLandRecords()) {
            double distanceSec = (call.getEndTime().getTime() - call.getStartTime().getTime()) / 1000;
            if (distanceSec < 0) {
                continue;
            }
            double distanceMin = (int) distanceSec / 60;
            if (distanceSec % 60 != 0) {
                distanceMin += 1;
            }
            totalCost += distanceMin * 0.6;
        }
        for (CallRecord call : userRecords.getAnswerInLandRecords()) {
            double distanceSec = (call.getEndTime().getTime() - call.getStartTime().getTime()) / 1000;
            if (distanceSec < 0) {
                continue;
            }
            double distanceMin = (int) distanceSec / 60;
            if (distanceSec % 60 != 0) {
                distanceMin += 1;
            }
            totalCost += distanceMin * 0.3;
        }
        return totalCost;
    }

}

class MobilePhoneInProvinceRule extends CallChargeRule {
    @Override
    public double calCost(UserRecords userRecords) {
        double totalCost = 0;
        for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
            double distanceSec = (call.getEndTime().getTime() - call.getStartTime().getTime()) / 1000;
            if (distanceSec < 0) {
                continue;
            }
            double distanceMin = (int) distanceSec / 60;
            if (distanceSec % 60 != 0) {
                distanceMin += 1;
            }
            if (call.getCallType().equals("21")) {
                totalCost += distanceMin * 0.3;
            } else if (call.getCallType().equals("22")) {
                totalCost += distanceMin * 0.3;
            } else if (call.getCallType().equals("23")) {
                totalCost += distanceMin * 0.3;
            }
        }
        return totalCost;
    }

}

class MessageRecord extends CommunicationRecord {
    private String message;

    public String getMessage() {
        return message;
    }

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

class User {
    private UserRecords userRecords = new UserRecords();
    private double balance = 100;
    private ChargeMode chargeMode;
    private String number;

    public double calCost() {
        return chargeMode.calCost(userRecords);
    }

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

    public UserRecords getUserRecords() {
        return userRecords;
    }

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

    public ChargeMode getChargeMode() {
        return chargeMode;
    }

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

    public String getNumber() {
        return number;
    }

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

}

class OutPut {
    public void output(double out) {
        BigDecimal numb = new BigDecimal(out);
        out = numb.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.print(out);
    }
}

SourceMonitor生成的报表内容:

img

代码分析总结:
 从上述的SourceMonitor生成的报表内容来看,我本题代码的平均复杂度、平均深度和最大深度达到了圈复杂度的期望值,平均语句与方法的比值和方法与类的比值很接近圈复杂度的期望值,但是最大复杂度仍然不满足圈复杂度的期望,还有不足之处,有待改进。从柱状图可以看出我本题代码的块深度大多数在1到3,但是最大的块深度到达了6,有点偏大,应该在类的构建和方法调用方面还有待改进。

(3)题目集8 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)。

注意:以上图中所定义的类不是限定要求,根据实际需要自行补充或修改。

试题分析:
User是用户类,包括属性:userRecords (用户记录)、balance(余额)、chargeMode(计费方式)、number(号码)。
ChargeMode是计费方式的抽象类:chargeRules是计费方式所包含的各种计费规则的集合,ChargeRule类的定义见图3。
getMonthlyRent()方法用于返回月租(monthlyRent)。
UserRecords是用户记录类,保存用户各种通话、短信的记录,各种计费规则将使用其中的部分或者全部记录。其属性从上到下依次是:市内拨打电话、省内(不含市内)拨打电话、省外拨打电话、市内接听电话、省内(不含市内)接听电话、省外接听电话的记录以及发送短信、接收短信的记录。
CommunicationRecord是抽象的通讯记录类:包含callingNumber拨打号码、answerNumber接听号码两个属性。CallRecord(通话记录)、MessageRecord(短信记录)是它的子类。
这些类的核心方法是:calCost(ArrayList callRecords)。该方法针根据输入参数callRecords中的所有记录计算某用户的某一项费用,输入参数callRecords的约束条件:必须是某一个用户的符合计费规则要求的所有记录。
SendMessageRule是发送短信的计费规则类,用于计算发送短信的费用。
LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三个类分别是座机拨打市内、省内、省外电话的计费规则类,用于实现这三种情况的费用计算。

源代码展示:

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        OutPut outPut = new OutPut();
        InputProcessing inputProcessing = new InputProcessing();
        ArrayList<User> users = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        while (!input.equals("end")) {
            if (1 == inputProcessing.checkInput(input)) {
                inputProcessing.writeUser(users, input);
            } else if (2 == inputProcessing.checkInput(input)) {
                inputProcessing.setRecord(users, input);
            }
            input = sc.nextLine();
        }
        users.sort((u1, u2) -> {
            if (u1.getNumber().charAt(0) == '0' && u2.getNumber().charAt(0) != '0') {
                return -1;
            } else if (u1.getNumber().charAt(0) != '0' && u2.getNumber().charAt(0) == '0') {
                return 1;
            }
            if (Double.parseDouble(u1.getNumber()) > Double.parseDouble(u2.getNumber())) {
                return 1;
            } else {
                return -1;
            }
        });
        for (User u : users) {
            System.out.print(u.getNumber() + " ");
            outPut.output(u.calCost());
            System.out.print(" ");
            outPut.output(u.calBalance());
            System.out.println();
        }
    }
}

abstract class ChargeMode {
    protected ArrayList<ChargeRule> chargeRules = new ArrayList<>();
    public abstract double calCost(UserRecords userRecords);
    public abstract double getMonthlyRent();
}

class UserRecords {
    private ArrayList<CallRecord> callingInCityRecords = new ArrayList<>();
    private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<>();
    private ArrayList<CallRecord> callingInLandRecords = new ArrayList<>();
    private ArrayList<CallRecord> answerInCityRecords = new ArrayList<>();
    private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<>();
    private ArrayList<CallRecord> answerInLandRecords = new ArrayList<>();
    private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<>();
    private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<>();
    public void 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 ArrayList<CallRecord> getCallingInCityRecords() {
        return callingInCityRecords;
    }
    public ArrayList<CallRecord> getCallingInProvinceRecords() {
        return callingInProvinceRecords;
    }
    public ArrayList<CallRecord> getCallingInLandRecords() {
        return callingInLandRecords;
    }
    public ArrayList<CallRecord> getAnswerInLandRecords() {
        return answerInLandRecords;
    }
    public ArrayList<MessageRecord> getSendMessageRecords() {
        return sendMessageRecords;
    }
}

class LandlinePhoneCharging extends ChargeMode {
    private final 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 totalCost = 0;
        for (ChargeRule chargeRule : chargeRules) {
            totalCost += chargeRule.calCost(userRecords);
        }
        return totalCost;
    }
    @Override
    public double getMonthlyRent() {
        return monthlyRent;
    }
}

class MobilePhoneCharging extends ChargeMode {
    private final double monthlyRent = 15;
    public MobilePhoneCharging() {
        super();
        chargeRules.add(new MobilePhoneInCityRule());
        chargeRules.add(new MobilePhoneInProvinceRule());
        chargeRules.add(new MobilePhoneInlandRule());
    }
    @Override
    public double calCost(UserRecords userRecords) {
        double totalCost = 0;
        for (ChargeRule rule : chargeRules) {
            totalCost += rule.calCost(userRecords);
        }
        return totalCost;
    }
    @Override
    public double getMonthlyRent() {
        return monthlyRent;
    }
}

class MobilePhoneMassageCharging extends ChargeMode {
    private double monthlyRent = 0;
    public MobilePhoneMassageCharging() {
        super();
        chargeRules.add(new MobilePhoneMessageRule());
    }
    @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 InputProcessing {
    public int checkInput(String input) {
        if (input.matches("u-0791[0-9]{7,8}\\s0") || 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 writeUser(ArrayList<User> users, String input) {
        User newUser = new User();
        String[] inputs = input.split(" ");
        String num = inputs[0].substring(2);
        for (User i : users) {
            if (i.getNumber().equals(num)) {
                return;
            }
        }
        newUser.setNumber(num);
        int mode = Integer.parseInt(inputs[1]);
        if (mode == 0) {
            newUser.setChargeMode(new LandlinePhoneCharging());
        } else if (mode == 1) {
            newUser.setChargeMode(new MobilePhoneCharging());
        }else if (mode == 3) {
            newUser.setChargeMode(new MobilePhoneMassageCharging());
        }
        users.add(newUser);
    }
    public void setRecord(ArrayList<User> users, String input) {
        String[] inputs = input.split(" ");
        User callUser = null, answerUser = null;
        String out = inputs[0].substring(2);
        String get;
        if (inputs.length == 6) {
            get = inputs[1];
        } else if (inputs.length == 7) {
            get = inputs[1];
        } else if (inputs.length == 8) {
            get = inputs[2];
        }else{
            get = inputs[1];
        }
        for (User i : users) {
            if (i.getNumber().equals(out)) {
                callUser = i;
            }
            if (i.getNumber().equals(get)) {
                answerUser = i;
            }
            if (callUser != null && answerUser != null) {
                break;
            }
        }
        if (input.charAt(0) == 'm') {
            MessageRecord messageRecord = new MessageRecord(input);
            if (callUser != null) {
                callUser.getUserRecords().addSendMessageRecords(messageRecord);
            }
            if (answerUser != null) {
                callUser.getUserRecords().addReceiveMessageRecords(messageRecord);
            }
        }
    }
}

abstract class CommunicationRecord {
}

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

class CallRecord extends CommunicationRecord {
    private Date start_Time;
    private Date end_Time;
    private String callingAddressAreaCode;
    private String answerAddressAreaCode;
    public String getCallType() {
        String type = "";
        if (callingAddressAreaCode.equals("0791")) {
            type = type.concat("1");
        } else if (callingAddressAreaCode.matches("^079[023456789]$") || callingAddressAreaCode.equals("0701")) {
            type = type.concat("2");
        } else {
            type = type.concat("3");
        }
        if (answerAddressAreaCode.equals("0791")) {
            type = type.concat("1");
        } else if (answerAddressAreaCode.matches("^079[023456789]$") || answerAddressAreaCode.equals("0701")) {
            type = type.concat("2");
        } else {
            type = type.concat("3");
        }
        return type;
    }
    public CallRecord(String[] inputs) {
        super();
        char type = inputs[0].charAt(0);
        inputs[0] = inputs[0].substring(2);
        String startDate = null, startTime = null, endDate = null, endTime = null;
        if (type == 't') {
            if (inputs.length == 6) {
                startDate = inputs[2];
                startTime = inputs[3];
                endDate = inputs[4];
                endTime = inputs[5];
                callingAddressAreaCode = inputs[0].substring(0, 4);
                answerAddressAreaCode = inputs[1].substring(0, 4);
            } else if (inputs.length == 7) {
                startDate = inputs[3];
                startTime = inputs[4];
                endDate = inputs[5];
                endTime = inputs[6];
                if (inputs[0].charAt(0) != '0') {
                    if (inputs[2].length() == 10) {
                        answerAddressAreaCode = inputs[2].substring(0, 3);
                    } else {
                        answerAddressAreaCode = inputs[2].substring(0, 4);
                    }
                    callingAddressAreaCode = inputs[1];
                } else {
                    if (inputs[0].length() == 10) {
                        callingAddressAreaCode = inputs[0].substring(0, 3);
                    } else {
                        callingAddressAreaCode = inputs[0].substring(0, 4);
                    }
                    answerAddressAreaCode = inputs[2];
                }
            } else if (inputs.length == 8) {
                startDate = inputs[4];
                startTime = inputs[5];
                endDate = inputs[6];
                endTime = inputs[7];
                callingAddressAreaCode = inputs[1];
                answerAddressAreaCode = inputs[3];
            }
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault());
        try {
            start_Time = simpleDateFormat.parse(startDate + " " + startTime);
            end_Time = simpleDateFormat.parse(endDate + " " + endTime);
        } catch (ParseException e) {
        }
    }
    public Date getStartTime() {
        return start_Time;
    }
    public Date getEndTime() {
        return end_Time;
    }
}

abstract class CallChargeRule extends ChargeRule {
}

class LandPhoneInCityRule extends CallChargeRule {
    @Override
    public double calCost(UserRecords userRecords) {
        double totalCost = 0;
        for (CallRecord call : userRecords.getCallingInCityRecords()) {
            double distanceSec = (call.getEndTime().getTime() - call.getStartTime().getTime()) / 1000.0;
            if (distanceSec < 0) {
                continue;
            }
            double distanceMin = (int) distanceSec / 60.0;
            if (distanceSec % 60 != 0) {
                distanceMin += 1;
            }
            if (call.getCallType().equals("11")) {
                totalCost += distanceMin * 0.1;
            } else if (call.getCallType().equals("12")) {
                totalCost += distanceMin * 0.3;
            } else if (call.getCallType().equals("13")) {
                totalCost += distanceMin * 0.6;
            }
        }
        return totalCost;
    }
}

class LandPhoneInlandRule extends CallChargeRule {
    @Override
    public double calCost(UserRecords userRecords) {
        double totalCost = 0;
        for (CallRecord call : userRecords.getCallingInLandRecords()) {
            double distanceSec = (call.getEndTime().getTime() - call.getStartTime().getTime()) / 1000.0;
            if (distanceSec < 0) {
                continue;
            }
            double distanceMin = (int) distanceSec / 60.0;
            if (distanceSec % 60 != 0) {
                distanceMin += 1;
            }
            totalCost += distanceMin * 0.6;
        }
        return totalCost;
    }
}

class LandPhoneInProvinceRule extends CallChargeRule {
    @Override
    public double calCost(UserRecords userRecords) {
        double totalCost = 0;
        for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
            double distanceSec = (call.getEndTime().getTime() - call.getStartTime().getTime()) / 1000.0;
            if (distanceSec < 0) {
                continue;
            }
            double distanceMin = (int) distanceSec / 60.0;
            if (distanceSec % 60 != 0) {
                distanceMin += 1;
            }
            totalCost += distanceMin * 0.3;
        }
        return totalCost;
    }
}

class MobilePhoneInCityRule extends CallChargeRule {
    @Override
    public double calCost(UserRecords userRecords) {
        double totalCost = 0;
        for (CallRecord call : userRecords.getCallingInCityRecords()) {
            double distanceSec = (call.getEndTime().getTime() - call.getStartTime().getTime()) / 1000.0;
            if (distanceSec < 0) {
                continue;
            }
            double distanceMin = (int) distanceSec / 60.0;
            if (distanceSec % 60 != 0) {
                distanceMin += 1;
            }
            if (call.getCallType().equals("11")) {
                totalCost += distanceMin * 0.1;
            } else if (call.getCallType().equals("12")) {
                totalCost += distanceMin * 0.2;
            } else if (call.getCallType().equals("13")) {
                totalCost += distanceMin * 0.3;
            }
        }
        return totalCost;
    }
}

class MobilePhoneInlandRule extends CallChargeRule {
    @Override
    public double calCost(UserRecords userRecords) {
        double totalCost = 0;
        for (CallRecord call : userRecords.getCallingInLandRecords()) {
            double distanceSec = (call.getEndTime().getTime() - call.getStartTime().getTime()) / 1000.0;
            if (distanceSec < 0) {
                continue;
            }
            double distanceMin = (int) distanceSec / 60.0;
            if (distanceSec % 60 != 0) {
                distanceMin += 1;
            }
            totalCost += distanceMin * 0.6;
        }
        for (CallRecord call : userRecords.getAnswerInLandRecords()) {
            double distanceSec = (call.getEndTime().getTime() - call.getStartTime().getTime()) / 1000.0;
            if (distanceSec < 0) {
                continue;
            }
            double distanceMin = (int) distanceSec / 60.0;
            if (distanceSec % 60 != 0) {
                distanceMin += 1;
            }
            totalCost += distanceMin * 0.3;
        }
        return totalCost;
    }
}

class MobilePhoneInProvinceRule extends CallChargeRule {
    @Override
    public double calCost(UserRecords userRecords) {
        double totalCost = 0;
        for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
            double distanceSec = (call.getEndTime().getTime() - call.getStartTime().getTime()) / 1000.0;
            if (distanceSec < 0) {
                continue;
            }
            double distanceMin = (int) distanceSec / 60.0;
            if (distanceSec % 60 != 0) {
                distanceMin += 1;
            }
            if (call.getCallType().equals("21")) {
                totalCost += distanceMin * 0.3;
            } else if (call.getCallType().equals("22")) {
                totalCost += distanceMin * 0.3;
            } else if (call.getCallType().equals("23")) {
                totalCost += distanceMin * 0.3;
            }
        }
        return totalCost;
    }
}

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 MobilePhoneMessageRule extends CallChargeRule {
    @Override
    public double calCost(UserRecords userRecords) {
        double totalCost;
        int count = 0;
        for (MessageRecord messageRecord : userRecords.getSendMessageRecords()) {
            int messageLength = messageRecord.getMessage().length();
            if (messageLength <= 10) {
                count++;
            } else {
                count += messageLength / 10;
                if (messageLength % 10 != 0) {
                    count++;
                }
            }
        }
        if (count <= 3) {
            totalCost = count * 0.1;
        } else if (count <= 5) {
            totalCost = 0.3 + 0.2 * (count - 3);
        } else {
            totalCost = 0.7 + 0.3 * (count - 5);
        }
        return totalCost;
    }
}

class User {
    private UserRecords userRecords = new UserRecords();
    private final double balance = 100;
    private ChargeMode chargeMode;
    private String number;
    public double calCost() {
        return chargeMode.calCost(userRecords);
    }
    public double calBalance() {
        return balance - chargeMode.getMonthlyRent() - chargeMode.calCost(userRecords);
    }
    public UserRecords getUserRecords() {
        return userRecords;
    }
    public void setUserRecords(UserRecords userRecords) {
        this.userRecords = userRecords;
    }
    public void setChargeMode(ChargeMode chargeMode) {
        this.chargeMode = chargeMode;
    }
    public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
        this.number = number;
    }
}

class OutPut {
    public void output(double out) {
        BigDecimal numb = new BigDecimal(out);
        out = numb.setScale(2, RoundingMode.HALF_UP).doubleValue();
        System.out.print(out);
    }
}

SourceMonitor生成的报表内容:

img

代码分析总结:
 从上述的SourceMonitor生成的报表内容来看,我本题代码的观测值基本上都接近圈复杂度的期望值,但是最大复杂度仍然不满足圈复杂度的期望,还有不足之处,有待改进。从柱状图可以看出我本题代码的块深度大多数在1到3,但是最大的块深度到达了6,有点偏大,应该在类的构建和方法调用方面还有待改进。

3.采坑心得

 1.抽象方法必须写在抽象类中,不能实例化抽象类,只能靠它的子类去实现它,抽象类是受约束的;
 2.题目中的区号可能是三位,可能是四位,这两种情况均要考虑;
 3.list表中的用户需要排序,用sort方法更方便一些。

4.改进建议

 我在写代码的时候总是会有一堆冗余的代码段,我认为我的代码应该尽可能多地将多次用到的相同的功能写成一个类,需要此功能的时候直接调用就行了,能提高自己写的代码的复用性。这也需要我多加练习,在写代码前多加思考,如何去构思每一种方法、每一个类。

5.总结

通过这三次题目集的练习:

  1. 让我明白了写Java的代码时,不应该只将目光放在实现结果上,而是应该关注代码的总体结构,结构清晰分工明确,实现代码也就又快又准;
  2. 封装能够在实现类的功能时保证类的独立性类在独立时,能够在任何情况下被快速引用,所以写代码应该尽可能地使代码模块化,避免大量重复的代码段,让自己的代码有可观的复用性;
  3. 我掌握了正则表达式的基本用法,并且让我明白了正则表达式对于字符处理类的题目的便利;
  4. 我掌握了类的相关设计和继承与多态的运用,能够理解类与类之间的联系,对象与对象之间的联系。





最后附上Source Monitor的使用说明链接:https://blog.csdn.net/u012075442/article/details/115653071

标签:03,return,String,inputs,double,ArrayList,blog,public
From: https://www.cnblogs.com/nchu202071/p/16948903.html

相关文章