最新要闻

广告

手机

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

家电

天天短讯!BLOG-3总结

来源:博客园

一、前言

通过最近的学习,java大致的内容都有一定的了解与练习。这几次的作业难度较大,不过按照老师的类图,经行分类,写起来的难度降低了不少。


(资料图片)

题目集六:

①、座机计费题目,通过老师给的类图,可以更好的理解题目,不过,对于“单个用户通话计算,含无效输入”部分,还是有一点问题,没有全对,考虑可能是和算法有关系。

②、多态测试,整体难度不高,只要按照题目要求来写就可以了,主要需要掌握接口写法。

题目集七:

①、手机+座机计费题目,在题目集6的基础上,增加手机类,通过老师给的类图和题目要求经行编写即可。

②、sdut-Collection-sort--C~K的班级,没有什么难度,按照题目要求经行编写就可以了。

③、阅读题目改错,这道题有几个写法错误,不过还是出现了一个小问题,需要把命名语句提出来就可以解决了。

题目集八:

①、短信计费,这道题目在上两次作业的练习下,已经没有那么困难了,按照题目要求和类图编写即可。

②、编写一个类Shop(商店)、内部类InnerCoupons(内部购物券),这道题不复杂,按照题目要求编写就可以了。

③、动物发声模拟器(多态),按照给出的部分代码经行编写就可以了。

二、设计与分析

7-1 电信计费系列1-座机计费分数80作者蔡轲单位南昌航空大学

实现一个简单的电信计费程序:假设南昌市电信分公司针对市内座机用户采用的计费方式:月租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,可根据理解自行调整:

图1图1中User是用户类,包括属性:userRecords (用户记录)、balance(余额)、chargeMode(计费方式)、number(号码)。ChargeMode是计费方式的抽象类:chargeRules是计费方式所包含的各种计费规则的集合,ChargeRule类的定义见图3。getMonthlyRent()方法用于返回月租(monthlyRent)。UserRecords是用户记录类,保存用户各种通话、短信的记录,    各种计费规则将使用其中的部分或者全部记录。其属性从上到下依次是:市内拨打电话、省内(不含市内)拨打电话、省外拨打电话、市内接听电话、省内(不含市内)接听电话、省外接听电话的记录以及发送短信、接收短信的记录。
图2图2中CommunicationRecord是抽象的通讯记录类:包含callingNumber拨打号码、answerNumber接听号码两个属性。CallRecord(通话记录)、MessageRecord(短信记录)是它的子类。CallRecord(通话记录类)包含属性:通话的起始、结束时间以及拨号地点的区号(callingAddressAreaCode)、接听地点的区号(answerAddressAreaCode)。区号用于记录在哪个地点拨打和接听的电话。座机无法移动,就是本机区号,如果是手机号,则会有差异。
图3图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 jiangxim-13305862264 18907910010 thank you

输入样例:

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

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

输出样例:

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

079186300001 3.0 77.0

部分代码:

abstract class ChargeMode {    protected ArrayList chargeRules = new ArrayList<>();    public abstract double calCost(UserRecords userRecords);    public abstract double getMonthlyRent();    public ArrayList getChargeRules() {        return chargeRules;    }    public void setChargeRules(ArrayList chargeRules) {        this.chargeRules = chargeRules;    }}class UserRecords {    private ArrayList callingInCityRecords = new ArrayList();    private ArrayList callingInProvinceRecords = new ArrayList();    private ArrayList callingInLandRecords = new ArrayList();    private ArrayList answerInCityRecords = new ArrayList();    private ArrayList answerInProvinceRecords = new ArrayList();    private ArrayList answerInLandRecords = new ArrayList();    private ArrayList sendMessageRecords = new ArrayList();    private ArrayList 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 aaddAnswerInProvinceRecords(CallRecord callRecord) {        answerInProvinceRecords.add(callRecord);    }    public void addAnswerInLandRecords(CallRecord callRecord) {        answerInLandRecords.add(callRecord);    }    public void addSendMessageRecords(MessageRecord callRecord) {        sendMessageRecords.add(callRecord);    }    public void addReceiveMessageRecords(MessageRecord callRecord) {        receiveMessageRecords.add(callRecord);    }    public ArrayList getCallingInCityRecords() {        return callingInCityRecords;    }    public void setCallingInCityRecords(ArrayList callingInCityRecords) {        this.callingInCityRecords = callingInCityRecords;    }    public ArrayList getCallingInProvinceRecords() {        return callingInProvinceRecords;    }    public void setCallingInProvinceRecords(ArrayList callingInProvinceRecords) {        this.callingInProvinceRecords = callingInProvinceRecords;    }    public ArrayList getCallingInLandRecords() {        return callingInLandRecords;    }    public void setCallingInLandRecords(ArrayList callingInLandRecords) {        this.callingInLandRecords = callingInLandRecords;    }    public ArrayList getAnswerInCityRecords() {        return answerInCityRecords;    }    public void setAnswerInCityRecords(ArrayList answerInCityRecords) {        this.answerInCityRecords = answerInCityRecords;    }    public ArrayList getAnswerInProvinceRecords() {        return answerInProvinceRecords;    }    public void setAnswerInProvinceRecords(ArrayList answerInProvinceRecords) {        this.answerInProvinceRecords = answerInProvinceRecords;    }    public ArrayList getAnswerInLandRecords() {        return answerInLandRecords;    }    public void setAnswerInLandRecords(ArrayList answerInLandRecords) {        this.answerInLandRecords = answerInLandRecords;    }    public ArrayList getSendMessageRecords() {        return sendMessageRecords;    }    public void setSendMessageRecords(ArrayList sendMessageRecords) {        this.sendMessageRecords = sendMessageRecords;    }    public ArrayList getReceiveMessageRecords() {        return receiveMessageRecords;    }    public void setReceiveMessageRecords(ArrayList receiveMessageRecords) {        this.receiveMessageRecords = receiveMessageRecords;    }}class LandlinePhoneCharging extends ChargeMode {    private double monthlyRent = 20;    public LandlinePhoneCharging() {        super();        chargeRules.add(new LandPhoneInCityRule());        chargeRules.add(new LandPhoneInProvinceRule());        chargeRules.add(new LandPhoneInlandRule());    }    @Override    public double calCost(UserRecords userRecords) {        double sumCost = 0;        for (ChargeRule rule : chargeRules) {            sumCost += rule.calCost(userRecords);        }        return sumCost;    }    @Override    public double getMonthlyRent() {        return monthlyRent;    }}class Inputdeal {    public int check(String input) {        String[] inputs = input.split(" ");        if (inputs.length == 2) {            if (inputs[0].matches("^u-[0-9]{11,13}$")) {                if (Integer.parseInt(inputs[1]) >= 0) {                    if (Integer.parseInt(inputs[1]) <= 2) {                        return 1;                    }                }            }        } else if (inputs.length == 6) {//            if(input.matches("[t]-0791[0-9]{7,8}\\s" + "0[0-9]{9,11}\\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;//            }            if (validate(inputs[2]))                if (validate(inputs[4]))                    if (validatet(inputs[3]))                        if (validatet(inputs[5]))//                    if (inputs[0].matches("^t\\-[0-9]{10,12}$")) {                            if (inputs[0].matches("[t]-0791[0-9]{7,8}")) {                                if (inputs[1].matches(".[0-9]{9,11}"))                                    return 2;                            }        }        return 0;    }    private boolean validatet(String string) {        String[] split = string.split(":");        if (!string.matches("^([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$")) {            return false;        }        return true;    }    public static boolean validate(String dateString) {        // 使用正则表达式 测试 字符 符合 dddd.dd.dd 的格式(d表示数字)        Pattern p = Pattern.compile("\\d{4}+[\\.]\\d{1,2}+[\\.]\\d{1,2}+");        Matcher m = p.matcher(dateString);        if (!m.matches()) {            return false;        }        // 得到年月日        String[] array = dateString.split("\\.");        int year = Integer.valueOf(array[0]);        int month = Integer.valueOf(array[1]);        int day = Integer.valueOf(array[2]);        if (month < 1 || month > 12) {            return false;        }        int[] monthLengths = new int[] { 0, 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };        if (isLeapYear(year)) {            monthLengths[2] = 29;        } else {            monthLengths[2] = 28;        }        int monthLength = monthLengths[month];        if (day < 1 || day > monthLength) {            return false;        }        return true;    }    /** 是否是闰年 */    private static boolean isLeapYear(int year) {        return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);    }    public boolean judge(String input) {        return false;    }    public void writeUser(ArrayList users, String input) {        User user = new User();        String[] inputs = input.split(" ");        String num = inputs[0].substring(2);        for (User i : users) {            if (i.getNumber().equals(num)) {                return;            }        }        user.setNumber(num);        int mode = Integer.parseInt(inputs[1]);        if (mode == 0) {            user.setChargeMode(new LandlinePhoneCharging());        }        users.add(user);    }    public void writeRecord(ArrayList users, String input) {        String[] inputs = input.split(" ");        inputs[0] = inputs[0].replace("t-", "");        User callu = null, answeru = null;        CallRecord callrecord = new CallRecord(inputs);        for (User i : users) {            if (i.getNumber().equals(inputs[0])) {                callu = i;            }            if (i.getNumber().equals(inputs[1])) {                answeru = i;            }            if (callu != null && answeru != null) {                break;            }        }        if (callu != null) {            if (callrecord.getCallType() == 1) {                callu.getUserRecords().addCallingInCityRecords(callrecord);            } else if (callrecord.getCallType() == 2) {                callu.getUserRecords().addCallingInProvinceRecords(callrecord);            } else {                callu.getUserRecords().addCallingInLandRecords(callrecord);            }        }        if (answeru != null) {            if (callrecord.getCallType() == 1) {                answeru.getUserRecords().addAnswerInCityRecords(callrecord);            } else if (callrecord.getCallType() == 2) {                answeru.getUserRecords().aaddAnswerInProvinceRecords(callrecord);            } else {                answeru.getUserRecords().addAnswerInLandRecords(callrecord);            }        }    }}abstract class CommunicationRecord {    protected String callingNumber;    protected String answerNumber;    public String getCallingNumber() {        return callingNumber;    }    public void setCallingNumber(String callingNumber) {        this.callingNumber = callingNumber;    }    public String getAnswerNumbe() {        return answerNumber;    }    public void setAnswerNumbe(String answerNumbe) {        this.answerNumber = answerNumbe;    }}abstract class ChargeRule {    abstract public double calCost(UserRecords userRecords);}
abstract class ChargeMode {    protected ArrayList chargeRules = new ArrayList<>();    public abstract double calCost(UserRecords userRecords);    public abstract double getMonthlyRent();    public ArrayList getChargeRules() {        return chargeRules;    }    public void setChargeRules(ArrayList chargeRules) {        this.chargeRules = chargeRules;    }}class UserRecords {    private ArrayList callingInCityRecords = new ArrayList();    private ArrayList callingInProvinceRecords = new ArrayList();    private ArrayList callingInLandRecords = new ArrayList();    private ArrayList answerInCityRecords = new ArrayList();    private ArrayList answerInProvinceRecords = new ArrayList();    private ArrayList answerInLandRecords = new ArrayList();    private ArrayList sendMessageRecords = new ArrayList();    private ArrayList 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 aaddAnswerInProvinceRecords(CallRecord callRecord) {        answerInProvinceRecords.add(callRecord);    }    public void addAnswerInLandRecords(CallRecord callRecord) {        answerInLandRecords.add(callRecord);    }    public void addSendMessageRecords(MessageRecord callRecord) {        sendMessageRecords.add(callRecord);    }    public void addReceiveMessageRecords(MessageRecord callRecord) {        receiveMessageRecords.add(callRecord);    }    public ArrayList getCallingInCityRecords() {        return callingInCityRecords;    }    public void setCallingInCityRecords(ArrayList callingInCityRecords) {        this.callingInCityRecords = callingInCityRecords;    }    public ArrayList getCallingInProvinceRecords() {        return callingInProvinceRecords;    }    public void setCallingInProvinceRecords(ArrayList callingInProvinceRecords) {        this.callingInProvinceRecords = callingInProvinceRecords;    }    public ArrayList getCallingInLandRecords() {        return callingInLandRecords;    }    public void setCallingInLandRecords(ArrayList callingInLandRecords) {        this.callingInLandRecords = callingInLandRecords;    }    public ArrayList getAnswerInCityRecords() {        return answerInCityRecords;    }    public void setAnswerInCityRecords(ArrayList answerInCityRecords) {        this.answerInCityRecords = answerInCityRecords;    }    public ArrayList getAnswerInProvinceRecords() {        return answerInProvinceRecords;    }    public void setAnswerInProvinceRecords(ArrayList answerInProvinceRecords) {        this.answerInProvinceRecords = answerInProvinceRecords;    }    public ArrayList getAnswerInLandRecords() {        return answerInLandRecords;    }    public void setAnswerInLandRecords(ArrayList answerInLandRecords) {        this.answerInLandRecords = answerInLandRecords;    }    public ArrayList getSendMessageRecords() {        return sendMessageRecords;    }    public void setSendMessageRecords(ArrayList sendMessageRecords) {        this.sendMessageRecords = sendMessageRecords;    }    public ArrayList getReceiveMessageRecords() {        return receiveMessageRecords;    }    public void setReceiveMessageRecords(ArrayList receiveMessageRecords) {        this.receiveMessageRecords = receiveMessageRecords;    }}class LandlinePhoneCharging extends ChargeMode {    private double monthlyRent = 20;    public LandlinePhoneCharging() {        super();        chargeRules.add(new LandPhoneInCityRule());        chargeRules.add(new LandPhoneInProvinceRule());        chargeRules.add(new LandPhoneInlandRule());    }    @Override    public double calCost(UserRecords userRecords) {        double sumCost = 0;        for (ChargeRule rule : chargeRules) {            sumCost += rule.calCost(userRecords);        }        return sumCost;    }    @Override    public double getMonthlyRent() {        return monthlyRent;    }}class Inputdeal {    public int check(String input) {        String[] inputs = input.split(" ");        if (inputs.length == 2) {            if (inputs[0].matches("^u-[0-9]{11,13}$")) {                if (Integer.parseInt(inputs[1]) >= 0) {                    if (Integer.parseInt(inputs[1]) <= 2) {                        return 1;                    }                }            }        } else if (inputs.length == 6) {//            if(input.matches("[t]-0791[0-9]{7,8}\\s" + "0[0-9]{9,11}\\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;//            }            if (validate(inputs[2]))                if (validate(inputs[4]))                    if (validatet(inputs[3]))                        if (validatet(inputs[5]))//                    if (inputs[0].matches("^t\\-[0-9]{10,12}$")) {                            if (inputs[0].matches("[t]-0791[0-9]{7,8}")) {                                if (inputs[1].matches(".[0-9]{9,11}"))                                    return 2;                            }        }        return 0;    }    private boolean validatet(String string) {        String[] split = string.split(":");        if (!string.matches("^([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$")) {            return false;        }        return true;    }    public static boolean validate(String dateString) {        // 使用正则表达式 测试 字符 符合 dddd.dd.dd 的格式(d表示数字)        Pattern p = Pattern.compile("\\d{4}+[\\.]\\d{1,2}+[\\.]\\d{1,2}+");        Matcher m = p.matcher(dateString);        if (!m.matches()) {            return false;        }        // 得到年月日        String[] array = dateString.split("\\.");        int year = Integer.valueOf(array[0]);        int month = Integer.valueOf(array[1]);        int day = Integer.valueOf(array[2]);        if (month < 1 || month > 12) {            return false;        }        int[] monthLengths = new int[] { 0, 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };        if (isLeapYear(year)) {            monthLengths[2] = 29;        } else {            monthLengths[2] = 28;        }        int monthLength = monthLengths[month];        if (day < 1 || day > monthLength) {            return false;        }        return true;    }    /** 是否是闰年 */    private static boolean isLeapYear(int year) {        return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);    }    public boolean judge(String input) {        return false;    }    public void writeUser(ArrayList users, String input) {        User user = new User();        String[] inputs = input.split(" ");        String num = inputs[0].substring(2);        for (User i : users) {            if (i.getNumber().equals(num)) {                return;            }        }        user.setNumber(num);        int mode = Integer.parseInt(inputs[1]);        if (mode == 0) {            user.setChargeMode(new LandlinePhoneCharging());        }        users.add(user);    }    public void writeRecord(ArrayList users, String input) {        String[] inputs = input.split(" ");        inputs[0] = inputs[0].replace("t-", "");        User callu = null, answeru = null;        CallRecord callrecord = new CallRecord(inputs);        for (User i : users) {            if (i.getNumber().equals(inputs[0])) {                callu = i;            }            if (i.getNumber().equals(inputs[1])) {                answeru = i;            }            if (callu != null && answeru != null) {                break;            }        }        if (callu != null) {            if (callrecord.getCallType() == 1) {                callu.getUserRecords().addCallingInCityRecords(callrecord);            } else if (callrecord.getCallType() == 2) {                callu.getUserRecords().addCallingInProvinceRecords(callrecord);            } else {                callu.getUserRecords().addCallingInLandRecords(callrecord);            }        }        if (answeru != null) {            if (callrecord.getCallType() == 1) {                answeru.getUserRecords().addAnswerInCityRecords(callrecord);            } else if (callrecord.getCallType() == 2) {                answeru.getUserRecords().aaddAnswerInProvinceRecords(callrecord);            } else {                answeru.getUserRecords().addAnswerInLandRecords(callrecord);            }        }    }}abstract class CommunicationRecord {    protected String callingNumber;    protected String answerNumber;    public String getCallingNumber() {        return callingNumber;    }    public void setCallingNumber(String callingNumber) {        this.callingNumber = callingNumber;    }    public String getAnswerNumbe() {        return answerNumber;    }    public void setAnswerNumbe(String answerNumbe) {        this.answerNumber = answerNumbe;    }}abstract class ChargeRule {    abstract public double calCost(UserRecords userRecords);}

圈复杂度:

踩坑心得:

  1. 圈复杂度处于合适范围,整体代码较好,这也和老师给的类图有关。
  2. 自己的格式类有些笨拙,后面根据老师的类图经行了修改。
7-1 电信计费系列2-手机+座机计费分数80作者蔡轲单位南昌航空大学

实现南昌市电信分公司的计费程序,假设该公司针对手机和座机用户分别采取了两种计费方案,分别如下: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 1t-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:

图1

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

图2

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

图3

图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:20end

输出样例:

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

13811111111 3.0 82.0

部分代码:

public class Main {     public static void main(String[] args) {         Outputtool outputtool = new Outputtool();         Inputdeal inputdeal = new Inputdeal();         ArrayList users = new ArrayList<>();         Scanner in = new Scanner(System.in);         String input = in.nextLine();         while (!input.equals("end")) {            if (1 == inputdeal.check(input)) {                inputdeal.writeUser(users, input);            } else if (2 == inputdeal.check(input)) {                inputdeal.writeRecord(users, input);            }            input = in.nextLine();        }         users.sort(new Comparator() {             @Override            public int compare(User u1, User u2) {                if (u1.getNumber().charAt(0) == "0" && u2.getNumber().charAt(0) != "0") {                    return -1;                } else if (u1.getNumber().charAt(0) != "0" && u2.getNumber().charAt(0) == "0") {                    return 1;                }                if (Double.parseDouble(u1.getNumber()) > Double.parseDouble(u2.getNumber())) {                    return 1;                } else {                    return -1;                }            }        });         for (User u : users) {            System.out.print(u.getNumber() + " ");            outputtool.output(u.calCost());            System.out.print(" ");            outputtool.output(u.calBalance());            System.out.println();         }     } } abstract class ChargeMode {    protected ArrayList chargeRules = new ArrayList<>();     public abstract double calCost(UserRecords userRecords);     public abstract double getMonthlyRent();     public ArrayList getChargeRules() {        return chargeRules;    }     public void setChargeRules(ArrayList chargeRules) {        this.chargeRules = chargeRules;    }} class UserRecords {     private ArrayList callingInCityRecords = new ArrayList();    private ArrayList callingInProvinceRecords = new ArrayList();    private ArrayList callingInLandRecords = new ArrayList();    private ArrayList answerInCityRecords = new ArrayList();    private ArrayList answerInProvinceRecords = new ArrayList();    private ArrayList answerInLandRecords = new ArrayList();    private ArrayList sendMessageRecords = new ArrayList();    private ArrayList 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 aaddAnswerInProvinceRecords(CallRecord callRecord) {        answerInProvinceRecords.add(callRecord);    }     public void addAnswerInLandRecords(CallRecord callRecord) {        answerInLandRecords.add(callRecord);    }     public void addSendMessageRecords(MessageRecord callRecord) {        sendMessageRecords.add(callRecord);    }     public void addReceiveMessageRecords(MessageRecord callRecord) {        receiveMessageRecords.add(callRecord);    }     public ArrayList getCallingInCityRecords() {        return callingInCityRecords;    }     public void setCallingInCityRecords(ArrayList callingInCityRecords) {        this.callingInCityRecords = callingInCityRecords;    }     public ArrayList getCallingInProvinceRecords() {        return callingInProvinceRecords;    }     public void setCallingInProvinceRecords(ArrayList callingInProvinceRecords) {        this.callingInProvinceRecords = callingInProvinceRecords;    }     public ArrayList getCallingInLandRecords() {        return callingInLandRecords;    }     public void setCallingInLandRecords(ArrayList callingInLandRecords) {        this.callingInLandRecords = callingInLandRecords;    }     public ArrayList getAnswerInCityRecords() {        return answerInCityRecords;    }     public void setAnswerInCityRecords(ArrayList answerInCityRecords) {        this.answerInCityRecords = answerInCityRecords;    }     public ArrayList getAnswerInProvinceRecords() {        return answerInProvinceRecords;    }     public void setAnswerInProvinceRecords(ArrayList answerInProvinceRecords) {        this.answerInProvinceRecords = answerInProvinceRecords;    }     public ArrayList getAnswerInLandRecords() {        return answerInLandRecords;    }     public void setAnswerInLandRecords(ArrayList answerInLandRecords) {        this.answerInLandRecords = answerInLandRecords;    }     public ArrayList getSendMessageRecords() {        return sendMessageRecords;    }     public void setSendMessageRecords(ArrayList sendMessageRecords) {        this.sendMessageRecords = sendMessageRecords;    }     public ArrayList getReceiveMessageRecords() {        return receiveMessageRecords;    }     public void setReceiveMessageRecords(ArrayList receiveMessageRecords) {        this.receiveMessageRecords = receiveMessageRecords;    } }

圈复杂度:

踩坑心得:

圈复杂度不是很理想,判断条件还是多了,在后续写码中需要更加注意。

改进建议:

使用接口,继承等来减少重复等,降低圈复杂度。

7-1 电信计费系列3-短信计费分数50作者蔡轲单位南昌航空大学

实现一个简单的电信计费程序,针对手机的短信采用如下计费方式: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:

图1

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

图2

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

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

(提示:可以从UserRecords类中获取各种类型的callRecords)。

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

输入样例:

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

u-18907910010 3m-18907910010 13305862264 aaaaaaaaaaaaaaaaaaaaaaaend

输出样例:

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

18907910010 0.3 99.7
### 输入样例1:

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

u-18907910010 3m-18907910010 13305862264 aaaaaaaaaaaam-18907910010 13305862264 aaaaaaa.m-18907910010 13305862264 bb,bbbbend

输出样例1:

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

18907910010 0.5 99.5
部分代码:
public class Main {     public static void main(String[] args) {         Outputtool outputtool = new Outputtool();         Inputdeal inputdeal = new Inputdeal();         ArrayList users = new ArrayList<>();         Scanner in = new Scanner(System.in);         String input = in.nextLine();         while (!input.equals("end")) {            if (1 == inputdeal.check(input)) {                inputdeal.writeUser(users, input);            } else if (2 == inputdeal.check(input)) {                inputdeal.writeRecord(users, input);            }            input = in.nextLine();        }         users.sort(new Comparator() {             @Override            public int compare(User u1, User u2) {                if (u1.getNumber().charAt(0) == "0" && u2.getNumber().charAt(0) != "0") {                    return -1;                } else if (u1.getNumber().charAt(0) != "0" && u2.getNumber().charAt(0) == "0") {                    return 1;                }                if (Double.parseDouble(u1.getNumber()) > Double.parseDouble(u2.getNumber())) {                    return 1;                } else {                    return -1;                }            }        });         for (User u : users) {            System.out.print(u.getNumber() + " ");            outputtool.output(u.calCost());            System.out.print(" ");            outputtool.output(u.calBalance());            System.out.println();         }     } } abstract class ChargeMode {    protected ArrayList chargeRules = new ArrayList<>();     public abstract double calCost(UserRecords userRecords);     public abstract double getMonthlyRent();     public ArrayList getChargeRules() {        return chargeRules;    }     public void setChargeRules(ArrayList chargeRules) {        this.chargeRules = chargeRules;    }} class UserRecords {     private ArrayList callingInCityRecords = new ArrayList();    private ArrayList callingInProvinceRecords = new ArrayList();    private ArrayList callingInLandRecords = new ArrayList();    private ArrayList answerInCityRecords = new ArrayList();    private ArrayList answerInProvinceRecords = new ArrayList();    private ArrayList answerInLandRecords = new ArrayList();    private ArrayList sendMessageRecords = new ArrayList();    private ArrayList 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 aaddAnswerInProvinceRecords(CallRecord callRecord) {        answerInProvinceRecords.add(callRecord);    }     public void addAnswerInLandRecords(CallRecord callRecord) {        answerInLandRecords.add(callRecord);    }     public void addSendMessageRecords(MessageRecord callRecord) {        sendMessageRecords.add(callRecord);    }     public void addReceiveMessageRecords(MessageRecord callRecord) {        receiveMessageRecords.add(callRecord);    }     public ArrayList getCallingInCityRecords() {        return callingInCityRecords;    }     public void setCallingInCityRecords(ArrayList callingInCityRecords) {        this.callingInCityRecords = callingInCityRecords;    }     public ArrayList getCallingInProvinceRecords() {        return callingInProvinceRecords;    }     public void setCallingInProvinceRecords(ArrayList callingInProvinceRecords) {        this.callingInProvinceRecords = callingInProvinceRecords;    }     public ArrayList getCallingInLandRecords() {        return callingInLandRecords;    }     public void setCallingInLandRecords(ArrayList callingInLandRecords) {        this.callingInLandRecords = callingInLandRecords;    }     public ArrayList getAnswerInCityRecords() {        return answerInCityRecords;    }     public void setAnswerInCityRecords(ArrayList answerInCityRecords) {        this.answerInCityRecords = answerInCityRecords;    }     public ArrayList getAnswerInProvinceRecords() {        return answerInProvinceRecords;    }     public void setAnswerInProvinceRecords(ArrayList answerInProvinceRecords) {        this.answerInProvinceRecords = answerInProvinceRecords;    }     public ArrayList getAnswerInLandRecords() {        return answerInLandRecords;    }     public void setAnswerInLandRecords(ArrayList answerInLandRecords) {        this.answerInLandRecords = answerInLandRecords;    }     public ArrayList getSendMessageRecords() {        return sendMessageRecords;    }     public void setSendMessageRecords(ArrayList sendMessageRecords) {        this.sendMessageRecords = sendMessageRecords;    }     public ArrayList getReceiveMessageRecords() {        return receiveMessageRecords;    }     public void setReceiveMessageRecords(ArrayList receiveMessageRecords) {        this.receiveMessageRecords = receiveMessageRecords;    } } class LandlinePhoneCharging extends ChargeMode {     private double monthlyRent = 20;     public LandlinePhoneCharging() {        super();        chargeRules.add(new LandPhoneInCityRule());        chargeRules.add(new LandPhoneInProvinceRule());        chargeRules.add(new LandPhoneInlandRule());    }     @Override    public double calCost(UserRecords userRecords) {        double sumCost = 0;        for (ChargeRule rule : chargeRules) {            sumCost += rule.calCost(userRecords);        }        return sumCost;    }     @Override    public double getMonthlyRent() {        return monthlyRent;    } } class MobilePhoneCharging extends ChargeMode {     private double monthlyRent = 15;     public MobilePhoneCharging() {        super();        chargeRules.add(new MobilePhoneInCityRule());        chargeRules.add(new MobilePhoneInProvinceRule());        chargeRules.add(new MobilePhoneInlandRule());    }     @Override    public double calCost(UserRecords userRecords) {        double sumCost = 0;        for (ChargeRule rule : chargeRules) {            sumCost += rule.calCost(userRecords);        }        return sumCost;    }     @Override    public double getMonthlyRent() {        return monthlyRent;    } }

圈复杂度:

踩坑心得:

圈复杂度不是很理想,判断条件还是多了,在后续写码中需要更加注意。

改进建议:

使用接口,继承等来减少重复等,降低圈复杂度。

总结:

通过这几次的实验,对于java的了解更加深入了,对于接口的掌握也更加牢固了,虽然这学期的学习就要结束了,但是我相信,在未来我也慧继续学习并更好的使用java,java学习会一直伴随着我,也感谢老师孜孜不倦的教诲,让我由衷的喜欢上了这门课。

                 

关键词: 计费方式 结束时间 手机用户