首页 > 编程语言 >Java题目集6~8总结Blog

Java题目集6~8总结Blog

时间:2022-12-07 19:01:28浏览次数:44  
标签:题目 String double ArrayList Blog Java new return public

一、前言

题目集六:

知识点:Java基本语法,对类的继承、抽象以及多态和容器的应用。

题量:较少。

难度:难。

题目集七:

知识点:Java基本语法,类的设计、继承和容器及其容器中排序;迭代器的有关用法。

题量:适中。

难度:较难。

题目集八:

知识点:类的继承以及容器的相关用法;内部类的使用;多态的使用。

题量:适中。

难度:较难。

二、设计与分析

题目集六:

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,可根据理解自行调整:

image.png

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

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

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

image.png

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

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

image.png

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

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

后续扩展说明:
后续题目集将增加手机用户,手机用户的计费方式中除了与座机计费类似的主叫通话费之外,还包含市外接听电话的漫游费以及发短信的费用。在本题的设计时可统一考虑。
通话记录中,手机需要额外记录拨打/接听的地点的区号,比如:
座机打手机: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

 代码思路:首先根据老师给的类图创建相关的类,以及引用容器。再添加相关的方法完成此次作业。

主要代码:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;

import java.util.Date;

abstract class ChargeMode {
    private 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 CommunicationRecord {
    protected String callingNumber;
    protected String answerNumber;

    public String getCallingNumber() {
        return callingNumber;
    }

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

    public String getAnswerNumber() {
        return answerNumber;
    }

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

class CallRecord extends CommunicationRecord{
    private Date startTime;
    private Date endTime;
    private String callingAddressAreaCode;
    private String 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 ChargeRule {
}
abstract class CallChargeRule extends ChargeRule{

    public abstract double calCost(ArrayList<CallRecord> callRecords);
}


class LandPhoneInLandRule extends CallChargeRule{
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        long interval;//通话时长
        double cost = 0;
        for(int i = 0; i < callRecords.size(); i++){
            interval = callRecords.get(i).getEndTime().getTime() - callRecords.get(i).getStartTime().getTime();//得出的时间是毫秒
            if(interval % 60000 == 0){
                interval = interval / 60000;
            } else {
                interval = interval / 60000 +1;
            }
            cost = cost + interval*0.6;
        }
        return cost;
    }
}



class LandPhoneInCityRule extends CallChargeRule{
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        long interval;
        double cost = 0;
        for(int i = 0; i < callRecords.size(); i++){
            interval = callRecords.get(i).getEndTime().getTime() - callRecords.get(i).getStartTime().getTime();
            if(interval % 60000 == 0){
                interval = interval / 60000;
            } else {
                interval = interval / 60000 +1;
            }
            cost = cost + interval*0.1;
        }
        return cost;
    }
}
class LandPhoneInProvinceRule extends CallChargeRule{
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        long interval;
        double cost = 0;
        for(int i = 0; i < callRecords.size(); i++){
            interval = callRecords.get(i).getEndTime().getTime() - callRecords.get(i).getStartTime().getTime();
            if(interval % 60000 == 0){
                interval = interval / 60000;
            } else {
                interval = interval / 60000 +1;
            }
            cost = cost + interval*0.3;
        }
        return cost;
    }
}
//固定电话缴费
class LandlinePhoneCharging extends ChargeMode{
    double monthlyRent = 20;

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

    @Override
    public double calCost(UserRecords userRecords) {
        double cost = 0;
        cost = cost + ((LandPhoneInCityRule)getChargeRules().get(0)).calCost(userRecords.getCallingInCityRecords());
        cost = cost + ((LandPhoneInProvinceRule)getChargeRules().get(1)).calCost(userRecords.getCallingInProvinceRecords());
        cost = cost + ((LandPhoneInLandRule)getChargeRules().get(2)).calCost(userRecords.getCallingInLandRecords());
        return cost;
    }

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


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> sendMessageRecord = new ArrayList<>();
    private ArrayList<MessageRecord> receiveMessageRecord = new ArrayList<>();

    public void addCallingCityRecords(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 addReceiveMessageRecords(MessageRecord messageRecord){
        receiveMessageRecord.add(messageRecord);
    }

    public void addSendMessageRecords(MessageRecord messageRecord){
        sendMessageRecord.add(messageRecord);
    }


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

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

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

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

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

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

    public ArrayList<MessageRecord> getSendMessageRecord() {
        return sendMessageRecord;
    }

    public ArrayList<MessageRecord> getReceiveMessageRecord() {
        return receiveMessageRecord;
    }
}


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 void calBalance() {
        balance = balance - calCost() - chargeMode.getMonthlyRent();
    }

    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 Run {
    ArrayList<User> users = new ArrayList<>();

    public void play() {
        Scanner sc = new Scanner(System.in);
        String s=null;
        String[] a;
        User user= new User();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        int pUserCoincide = 0;

        while (true) {
            s = sc.nextLine();
            if (s.equals("end")) break;
            //if(s.length() < 5) continue;//判断拨号是否符合格式
//            if(s.charAt(1) != '-'){
//                continue;
//            }

            if (s.charAt(0) == 'u') {//用户增加
                if(!s.matches("^u-(1[0-9]{10} 1|0[0-9]{9,11} 0)$")) continue;
                a = s.substring(2).split(" ");
            //  判断号码位数
                if(a[0].length() != 11 && a[0].length() != 12) continue;
                //判断拨号用户是否为同一个
                for(int i = 0; i < users.size(); i++){
                    if(a[0].equals(users.get(i).getNumber())){
                        pUserCoincide = 1;
                        continue;
                    }
                }
                if(pUserCoincide == 1){
                    pUserCoincide = 0 ;
                    continue;
                }
                user.setNumber(a[0]);//设置号码
                //设置收费模式
                if (a[1].equals("0")) {
                    user.setChargeMode(new LandlinePhoneCharging());
                }

                users.add(user);
            } else if (s.charAt(0) == 't') {
                //String regex5 = "^t-0[0-9]{9,11} 0[0-9]{9,11} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2}$";//座机打座机
               if(!s.matches("^t-0[0-9]{9,11} 0[0-9]{9,11} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4}.[0-9]{1,2}.[0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2}$"))  continue;
                a = s.substring(2).split(" ");
                if(a.length != 6){
                    continue;
                }
                if((a[1].length() != 11 && a[1].length() != 12 && a[1].length() != 10) ||( a[0].length() != 11 && a[0].length() != 12 && a[0].length() != 10)){
                    continue;
                }

                CallRecord callRecord = new CallRecord();
                String tempS1 = a[2].concat(" ").concat(a[3]);
                String tempS2 = a[4].concat(" ").concat(a[5]);

                if(!isWrongFormat(tempS1) || !isWrongFormat(tempS2)){
                    continue;
                }

                try {
                    callRecord.setStartTime(simpleDateFormat.parse(tempS1));
                    callRecord.setEndTime(simpleDateFormat.parse(tempS2));
                } catch (ParseException e) {
                    continue;
                }
                callRecord.setCallingAddressAreaCode(a[0].substring(0, 4));
                callRecord.setAnswerAddressAreaCode(a[1].substring(0, 4));
                callRecord.setCallingNumber(a[0]);
                callRecord.setAnswerNumber(a[1]);

                if(callRecord.getStartTime().compareTo(callRecord.getEndTime()) >= 0){
                    continue;
                }

                for (int i = 0; i < users.size(); i++) {
                    if (callRecord.callingNumber.equals(users.get(i).getNumber())) {
                        if (callRecord.getAnswerAddressAreaCode().equals(callRecord.getCallingNumber().substring(0,4))) {
                            users.get(i).getUserRecords().addCallingCityRecords(callRecord);//区内
                        } else if (callRecord.getAnswerAddressAreaCode().matches("(079\\d)|(0701)")) {
                            users.get(i).getUserRecords().addCallingInProvinceRecords(callRecord);//省内
                        } else {
                            users.get(i).getUserRecords().addCallingInLandRecords(callRecord);//内地
                        }
                    }
                    if (callRecord.answerNumber.equals(users.get(i).getNumber())) {
                        if (callRecord.getAnswerAddressAreaCode().equals("0791")) {
                            users.get(i).getUserRecords().addAnswerInCityRecords(callRecord);
                        } else if (callRecord.getAnswerAddressAreaCode().matches("(079\\d)|(0701)")) {
                            users.get(i).getUserRecords().addAnswerInProvinceRecords(callRecord);
                        } else {
                            users.get(i).getUserRecords().addAnswerInLandRecords(callRecord);
                        }
                    }
                }
            }
        }
        users.sort(new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                return o1.getNumber().compareTo(o2.getNumber());
            }
        });
        for(int i = 0; i < users.size(); i++){
            users.get(i).calBalance();
            System.out.printf("%s %.1f %.1f\n",users.get(i).getNumber(),users.get(i).calCost(),users.get(i).getBalance());
        }

    }

    public boolean isWrongFormat(String s){
        return s.matches("((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[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])");
    }
}

public class Main {
    public static void main(String[] args) {
        Run run = new Run();
        run.play();
    }
}
View Code

圈复杂度分析:

分析:

    1.根据题目的类图,自己创建类的时候有些轻松,创建了很多的类,减少了代码的冗余;

    2.像相关的ArrayList容器类的使用,通过查找资料知道其用法。

    3.在具体的实现方法中没有想仔细,导致出现注册同一用户的时候出现混乱;

    4.在年份的考虑上没有考虑闰年的计算,导致测试点没通过。

题目集七:

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:

image.png
图1

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

image.png

图2

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

image.png
图3

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

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

代码思路:这次和上次的差不多,就是增加了移动手机的相关操作,还是根据类图写出相关的类,自己也写了一些自己的类,更方便自己写代码;也更改了同一用户注册的问题。

CallChargeRule类:

abstract class CallChargeRule extends ChargeRule {

}
CallChargeRule

 CallRecord类:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

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

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

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

        return type;
    }

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

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

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

        if (type == 't') {
            if (inputs.length == 6) {
                sd = inputs[2];
                st = inputs[3];
                ed = inputs[4];
                et = inputs[5];
                callingAddressAreaCode = inputs[0].substring(0, 4);
                answerAddressAreaCode = inputs[1].substring(0, 4);
            } else if (inputs.length == 7) {
                sd = inputs[3];
                st = inputs[4];
                ed = inputs[5];
                et = inputs[6];
                if (inputs[0].charAt(0) != '0') {
                    if (inputs[2].length() == 10) {
                        answerAddressAreaCode = inputs[2].substring(0, 3);
                    } else {
                        answerAddressAreaCode = inputs[2].substring(0, 4);
                    }
                    callingAddressAreaCode = inputs[1];
                } else {
                    if (inputs[0].length() == 10) {
                        callingAddressAreaCode = inputs[0].substring(0, 3);
                    } else {
                        callingAddressAreaCode = inputs[0].substring(0, 4);
                    }
                    answerAddressAreaCode = inputs[2];
                }
            } else if (inputs.length == 8) {
                sd = inputs[4];
                st = inputs[5];
                ed = inputs[6];
                et = inputs[7];
                callingAddressAreaCode = inputs[1];
                answerAddressAreaCode = inputs[3];
            }
        } else if (type == 'm') {

        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault());
        try {
            startTime = simpleDateFormat.parse(sd + " " + st);
            endTime = simpleDateFormat.parse(ed + " " + et);
        } catch (ParseException e) {
        }

    }

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

    public Date getStartTime() {
        return startTime;
    }

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

    public Date getEndTime() {
        return endTime;
    }

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

    public String getCallingAddressAreaCode() {
        return callingAddressAreaCode;
    }

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

    public String getAnswerAddressAreaCode() {
        return answerAddressAreaCode;
    }

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

圈复杂度分析:

chargeMode类:

import java.util.ArrayList;

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

圈复杂度分析:

ChargeRule类:

abstract class ChargeRule {

    abstract public double calCost(UserRecords userRecords);

}
ChargeRule

CommunicationRecord类:

abstract class CommunicationRecord {
    protected String callingNumber;
    protected String answerNumbe;

    public String getCallingNumber() {
        return callingNumber;
    }

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

    public String getAnswerNumbe() {
        return answerNumbe;
    }

    public void setAnswerNumbe(String answerNumbe) {
        this.answerNumbe = answerNumbe;
    }

}
CommunicationRecord

 圈复杂度分析:

Inputdeal类:

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class Inputdeal {

    public int check(String input) {
        if (input.matches("[u]-0791[0-9]{7,8}\\s[0]") || input.matches("[u]-1[0-9]{10}\\s[1]")) {
            return 1;
//        } else if (input.charAt(0) == 'm') {
//            return 2;
        } else if (input.matches("(([t]-0791[0-9]{7,8}\\s" + "0[0-9]{9,11}\\s)|"
                + "([t]-0791[0-9]{7,8}\\s" + "1[0-9]{10}\\s" + "0[0-9]{2,3}\\s)|"
                + "([t]-1[0-9]{10}\\s" + "0[0-9]{2,3}\\s" + "0[0-9]{9,11}\\s)|"
                + "([t]-1[0-9]{10}\\s" + "0[0-9]{2,3}\\s" + "1[0-9]{10}\\s" + "0[0-9]{2,3}\\s))"

                + "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.(((0?[13578]|1[02])\\.(0?"
                + "[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((("
                + "[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))"
                + "\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\\s"
                + "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.("
                + "[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((("
                + "[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))"
                + "\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])")) {
            return 2;
        }
        return 0;
    }

    @SuppressWarnings("unused")
    private boolean validatet(String string) {
        if (!string.matches("^([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$")) {
            return false;
        }
        return true;
    }

    public static boolean validate(String dateString) {
        // 使用正则表达式 测试 字符 符合 dddd.dd.dd 的格式(d表示数字)
        Pattern p = Pattern.compile("\\d{4}+[\\.]\\d{1,2}+[\\.]\\d{1,2}+");
        Matcher m = p.matcher(dateString);
        if (!m.matches()) {
            return false;
        }

        // 得到年月日
        String[] array = dateString.split("\\.");
        int year = Integer.valueOf(array[0]);
        int month = Integer.valueOf(array[1]);
        int day = Integer.valueOf(array[2]);

        if (month < 1 || month > 12) {
            return false;
        }
        int[] monthLengths = new int[] { 0, 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        if (isLeapYear(year)) {
            monthLengths[2] = 29;
        } else {
            monthLengths[2] = 28;
        }
        int monthLength = monthLengths[month];
        if (day < 1 || day > monthLength) {
            return false;
        }
        return true;
    }

    /** 是否是闰年 */
    private static boolean isLeapYear(int year) {
        return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
    }

    public boolean judge(String input) {

        return false;
    }

    public void writeUser(ArrayList<User> users, String input) {
        User usernew = new User();
        String[] inputs = input.split(" ");
        String num = inputs[0].substring(2);
        for (User i : users) {
            if (i.getNumber().equals(num)) {
                return;
            }
        }
        usernew.setNumber(num);
        int mode = Integer.parseInt(inputs[1]);
        if (mode == 0) {
            usernew.setChargeMode(new LandlinePhoneCharging());
        } else if (mode == 1) {
            usernew.setChargeMode(new MobilePhoneCharging());
        }
        users.add(usernew);
    }

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

        User callu = null, answeru = null;
        CallRecord callrecord = new CallRecord(inputs);

        if (input.charAt(0) == 't') {
            String out = inputs[0];
            String in = "";
            if (inputs.length == 6) {
                in = inputs[1];
            } else if (inputs.length == 7) {
                in = inputs[1];
            } else if (inputs.length == 8) {
                in = inputs[2];
            }

            for (User i : users) {
                if (i.getNumber().equals(out)) {
                    callu = i;
                }
                if (i.getNumber().equals(in)) {
                    answeru = i;
                }
                if (callu != null && answeru != null) {
                    break;
                }
            }

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

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

        }

    }

}
Inputdeal

 圈复杂度分析:

LandlinePhoneCharging类:

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

}
LandlinePhoneCharging

圈复杂度分析:

LandPhoneInCityRule类:

class LandPhoneInCityRule extends CallChargeRule {

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

}
LandPhoneInCityRule

圈复杂度分析:

LandPhoneInlandRule类:

class LandPhoneInlandRule extends CallChargeRule {

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

}
LandPhoneInlandRule

圈复杂度分析:

LandPhoneInProvinceRule类:

class LandPhoneInProvinceRule extends CallChargeRule {

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

}
LandPhoneInProvinceRule

 圈复杂度分析:

Main 类:

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.text.ParseException;

public class Main {

    public static void main(String[] args) {

        Outputtool outputtool = new Outputtool();

        Inputdeal inputdeal = new Inputdeal();

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

        Scanner in = new Scanner(System.in);

        String input = in.nextLine();

        while (!input.equals("end")) {
            if (1 == inputdeal.check(input)) {
                inputdeal.writeUser(users, input);
            } else if (2 == inputdeal.check(input)) {
                inputdeal.writeRecord(users, input);
            }
            input = in.nextLine();
        }

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

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

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

        }

    }

}
Main

圈复杂度分析:

MessageRecord类:

class MessageRecord extends CommunicationRecord {

    private String message;

    public String getMessage() {
        return message;
    }

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

圈复杂度分析:

MobilePhoneCharging类:

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

}
MobilePhoneCharging

MobilePhoneInCityRule类:

class MobilePhoneInCityRule extends CallChargeRule {

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

        }
        return sumCost;
    }

}
MobilePhoneInCityRule

圈复杂度分析:

MobilePhoneInlandRule类:

class MobilePhoneInlandRule extends CallChargeRule {

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

}
MobilePhoneInlandRule

圈复杂度分析:

MobilePhoneInProvinceRule类:

class MobilePhoneInProvinceRule extends CallChargeRule {

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

}
MobilePhoneInProvinceRule

圈复杂度分析:

Outputtool类:

import java.math.BigDecimal;

class Outputtool {

    @SuppressWarnings("deprecation")
    public void output(double out) {
        BigDecimal numb = new BigDecimal(out);
        out = numb.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.print(out);
    }
}
Outputtool

圈复杂度分析:

User类:

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

}
User

圈复杂度分析:

UserRecords类:

import java.util.ArrayList;

class UserRecords {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

}
UserRecords

 圈复杂度分析:

 

分析:

    1.这次的作业是在上次的基础上增加了针对手机用户的实时计费,增加了一些方法,难度也不大,按照之前的思路写;

    2.在具体的实现方法中改了之前代码中一些错误于不足,像争对同一用户的注册时解决策略,这次也要考虑手机用户在不同的地区是可以用的,所以要创造不同的方法;

    3.在年份上添加了闰年的计算,顺利完成此次代码。

7-2 sdut-Collection-sort--C~K的班级(II)

经过不懈的努力,C~K终于当上了班主任。

现在他要统计班里学生的名单,但是C~K在教务系统中导出班级名单时出了问题,发现会有同学的信息重复,现在他想把重复的同学信息删掉,只保留一个,
但是工作量太大了,所以找到了会编程的你,你能帮他解决这个问题吗?

输入格式:

第一行输入一个N,代表C~K导出的名单共有N行(N<100000).

接下来的N行,每一行包括一个同学的信息,学号 姓名 年龄 性别。

输出格式:

第一行输出一个n,代表删除重复名字后C~K的班级共有几人。

接下来的n行,输出每一个同学的信息,输出按照学号从小到大的顺序。

主要思路:理解题意,使用ArrayList容器类,进行数据的存储,还要用到排序的算法。

主要代码:

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Comparator;

public class Main {
    public static void main(String []args) {
        Scanner in = new Scanner(System.in);
        int n = Integer.parseInt(in.nextLine());
        ArrayList<String> a = new ArrayList<String>();
        while(n!=0){
            String s = in.nextLine();
            if(!a.contains(s)){
                a.add(s);
            }
            n--;
        }
        a.sort(new Comparator<String>() {

            @Override
            public int compare(String s1, String s2) {
                return Integer.parseInt(s1.substring(0, 4))-Integer.parseInt(s2.substring(0, 4));
            }
        });

        System.out.println(a.size());

        for(int i = 0; i < a.size(); i++) {
         System.out.println(a.get(i));
        }
    }
}
View Code

圈复杂度:

 

 分析:

        在这次的题目中自己想着用数组来存取这些数据,但是写的效果不理想。后来改写成用ArrayList容器类,刚开始自己没有使用Comparable接口,结果提交的时候答案总是错的,在测试样例中才发现结果没有排序,后来增加了接口Comparable,顺利通过。

 

题目集八:

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:

image.png

图1

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

image.png

图2

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

image.png

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

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

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

主要思路:这次的题目有前面的基础写起来困难还是少,这次根据题目设计了自己所想的简单类图,主要写了main类和user类就可以满足这次的题目要求,在前面所用到的类上增加了Collections类的使用实现集合元素的排序、查找替换和复制等操作。

主要代码:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class Main {

    public static ArrayList<User> users = new ArrayList<User>();
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
//        String ss = in.nextLine();
        while(true) {
            String str = in.nextLine();
            if(!str.equals("end")) {

                if(str.matches("[u]-1[0-9]{10}\\s[3]")) {
                    String[] s = str.split(" ");

                    User user = new User(s[0].substring(2,s[0].length()));
                    users.add(user);
                }
                else if(str.matches("[m]-1[0-9]{10}\\s"+"1[0-9]{10}\\s"+"[a-zA-Z-z0-9-,\\.\\ ]+$"
                )){
                    String[] s = str.split(" ",3);
                    users.get(getUser(s[0].substring(2,s[0].length()))).messageRecord.add(s[2]);
                }
            }
            else
                break;
        }
        Collections.sort(users, new Comparator<User>() {

            public int compare(User user1, User user2) {

                return user1.getNumber().compareTo(user2.getNumber());

            }

        });
        for(int i = 0;i<users.size();i++) {
            System.out.println(users.get(i).getNumber()+" "+String.format("%.1f", users.get(i).getCalCost())+" "+String.format("%.1f", users.get(i).getCalBalance()));
        }
    }

    //根据号码找到对应的用户
    public static int getUser(String s) {
        int n = 0;
        for(int i = 0;i<users.size();i++) {
            if(s.equals(users.get(i).getNumber())) {
                n=i;
            }
        }
        return n;

    }
}
class User{
    private String number;
    private double balance = 100;
    private double calBalance;
    private double calCost = 0;
    ArrayList<String> messageRecord = new ArrayList<String>();
    public String getNumber() {
        return this.number;
    }



    public User(String number) {
        super();
        this.number = number;
    }


    //计算用了多少话费
    public double getCalCost() {
        int n = 0;
        for(int i = 0;i<this.messageRecord.size();i++){

            if(Count(messageRecord.get(i))+n<=3) {

                this.calCost = this.calCost + 0.1*Count(messageRecord.get(i));
                n = n + Count(messageRecord.get(i));

            }
            else if(Count(messageRecord.get(i))+n>3&&Count(messageRecord.get(i))+n<=5) {
                if(n == 0) {
                    this.calCost = this.calCost + 0.1*3 + (Count(messageRecord.get(i))-3)*0.2;
                }
                else if(n==1) {
                    this.calCost = this.calCost + 0.1*2 + (Count(messageRecord.get(i))-2)*0.2;
                }
                else if(n==2) {
                    this.calCost = this.calCost + 0.1*1 + (Count(messageRecord.get(i))-1)*0.2;
                }
                else if(n>=3) {
                    this.calCost = this.calCost  + Count(messageRecord.get(i))*0.2;
                }
                n = n + Count(messageRecord.get(i));
            }
            else if(Count(messageRecord.get(i))+n>5) {
                if(n<=3)
                    this.calCost = this.calCost + (3-n)*0.1+2*0.2+(Count(messageRecord.get(i))-5+n)*0.3;
                else if(n==4) {
                    this.calCost = this.calCost +0.2+(Count(messageRecord.get(i))-1)*0.3;
                }
                else if(n>=5) {
                    this.calCost = this.calCost + Count(messageRecord.get(i))*0.3;
                }

                n = n + Count(messageRecord.get(i));
            }

        }
        return this.calCost;
    }


    public double getCalBalance(){
        this.calBalance = this.balance - this.calCost;
        return this.calBalance;
    }




    //计算一次话费条数
    public int Count(String string) {
        int n = 0;
        if(string.length()%10==0) {
            n = string.length()/10;
        }
        else
            n = string.length()/10 + 1;
        return n;
    }
}
View Code

圈复杂度分析:

 

  分析:

         这次的题目在新用户和旧的用户的比较上用Collections类来实现,再在号码的比较上用了接口Comparable来实现,根据号码找到用户,计算所花费的费用。

7-3 动物发声模拟器(多态)

设计一个动物发生模拟器,用于模拟不同动物的叫声。比如狮吼、虎啸、狗旺旺、猫喵喵……。
定义抽象类Animal,包含两个抽象方法:获取动物类别getAnimalClass()、动物叫shout();
然后基于抽象类Animal定义狗类Dog、猫类Cat和山羊Goat,用getAnimalClass()方法返回不同的动物类别(比如猫,狗,山羊),用shout()方法分别输出不同的叫声(比如喵喵、汪汪、咩咩)。
最后编写AnimalShoutTest类测试,输出:
猫的叫声:喵喵
狗的叫声:汪汪
山羊的叫声:咩咩

其中,在AnimalShoutTestMain类中,用speak(Animal animal){}方法输出动物animal的叫声,在main()方法中调用speak()方法,分别输出猫、狗和山羊对象的叫声。

请在下面的【】处添加代码。

 
//动物发生模拟器.  请在下面的【】处添加代码。
public class AnimalShoutTest2 {
    public static void main(String[] args) {        
         Cat cat = new Cat();
         Dog dog = new Dog();        
        Goat goat = new Goat();
         speak(cat);
         speak(dog);
         speak(goat);
    }
    //定义静态方法speak()
    【】

}

//定义抽象类Animal
【】class Animal{
    【】
}
//基于Animal类,定义猫类Cat,并重写两个抽象方法
class Cat 【】{
    【】    
    【】
}
//基于Animal类,定义狗类Dog,并重写两个抽象方法
class Dog 【】{
    【】
    【】
}
//基于Animal类,定义山羊类Goat,并重写两个抽象方法
class Goat 【】{
    【】
    【】
}

 主要代码:

public class Main {
    public static void main(String[] args) {
        Cat cat = new Cat();
        Dog dog = new Dog();
        Goat goat = new Goat();
        speak(cat);
        speak(dog);
        speak(goat);
    }
    // 定义静态方法speak()
    private static void speak(Animal animal) {
        System.out.println(animal.getAnimalClass() + "的叫声:" + animal.shout());

    }
}
abstract class Animal {
    public abstract String getAnimalClass();
    abstract public String shout();
}
class Cat extends Animal{
    @Override
    public String getAnimalClass() {
        return "猫";
    }

    @Override
    public String shout() {
        return "喵喵";

    }

}
class Dog extends Animal {
    @Override
    public String getAnimalClass() {
        return "狗";
    }

    @Override
    public String shout() {
        return "汪汪";

    }

}
class Goat extends Animal{

    @Override
    public String getAnimalClass() {
        return "山羊";
    }

    @Override
    public String shout() {
        return "咩咩";
    }
}
View Code

圈复杂度分析:

 

 分析:

       这道题目比较简单,但是对我来说也是比较重要,以前的掌握多态的技术在做这道题目时尽然做错了,我还没有掌握真正的精髓,我当时写动物的叫声及其名字的方法时,写在了抽象类,大失误,要写在各个子类中去实现,让我牢固的记住了多态的用法和更多的知识。

三、踩坑心得:

1.在题目集六中的电信系列的计算中,也由于是第一次写这类题目。对日期这类的时间不是很理解怎么处理和判断,时间必须符合"yyyy.MM.dd HH:mm:ss"格式,使用SimpleDateFormat类中的方法会自动检查。还有Data类中计算不熟悉,第一次直接调用了方法,但是结果不对,最后查找资料getTime()函数得到的是毫秒数,转化为秒的话需要除以1000。还有在开户方面需要考虑到是否已经存在,当时没有考虑导致测试点没有通过。还有在年份时间的考虑上,我没有考虑闰年,测试点没有通过,一定要增加闰年的情况。

2.这几次的题目集中还有一个自己经常错误的一个点就是排序,要熟悉.Comparator接口和ArrayList容器类在排序方面的联系和使用。也可以直接用Collections去调用sort方法。

四、改进建议:

       这几次的大作业基本都是按照老师给的类图去写,自己的思考也不是很多,主要集中在了方法的实现,通过这几次大作业,给我的感觉就是考虑的总是不够,考虑的不全面,不仔细。还有对一些类里面的方法了解的不够,使用起来捉襟见肘,总是要参考他人的,自己才会知道用法是怎么用的。还有自己的基础还是很薄弱,继承自己掌握的差不多了,但是通过做题在多态上自己掌握的不够,很简单但是自己小看了,一定要踏踏实实去理解每一个知识点。

五、总结:

        Java的最后一次博客,也意味着这学期的Java课也就结束了。通过这学期的学习,整体上看,收获还是很多,从最开始学习Java中的迷茫,到后来的不断认识,慢慢的对Java这门编程课有了见解,慢慢的对Java的编程也熟悉了,后面做题的时候比较容易。

        在后半段(10~16周)中的学习中,自己可以来说掌握了自己能掌握的,对一些软件建模,设计思路有了一定的基础,还有在设计过程中应该遵循的规则也了解到了。在这后半段也主要交了javaFX的一些基本的常识,还有编程方法,思想都有了基本的掌握,每次安排的作业自己也是按照老师的模仿能做出,理解了代码的含义。但自己还是要在各个方面都要加强,自己的短板还是明显,需要不断地,多次的去写代码来增强自己能力。

      学习是一个漫长的过程,需要持之以恒,纵然有困难,但也要学会去克服;虽然Java课结束,但是学习生涯还是继续,仍需继续努力奋斗,开拓自己的一片天地。

标签:题目,String,double,ArrayList,Blog,Java,new,return,public
From: https://www.cnblogs.com/11100927A/p/16947043.html

相关文章

  • JavaScript(三)
    ❤️‍JavaScriptjQuery查找标签jQuery节点操作jQuery事件绑定bootstrap页面框架❤️‍jQuery查找标签......
  • java面试(多线程)
    1. Callable、Future、FutureTash详解Callable与Future是在JAVA的后续版本中引入进来的,Callable类似于Runnable接口,实现Callable接口的类与实现Runnable的类都是可以被线程......
  • java面试(基础)
    0.面向对象1.1封装:利用抽象数据类型将数据和基于数据的操作封装在一起,使其构成一个不可分割的独立实体。减少耦合,提高可重用性。1.2继承:is-a Cat可以当做Animal来使......
  • java对象/谈谈Java虚拟机
    ......
  • Java学习十三
    1.异常处理能够使一个方法给它的调用者抛出一个异常。2.Java异常是派生自java.lang.Throwable的类的实例。Java提供大量预定义的异常类,例如,Error、Exception、RuntimeExc......
  • 初学Java
    Java概念三大版本JavaSE(basis)JavaME(嵌入式,手机...)JavaEE(E企业级,web端、服务器开发,)JDKJREJWMJDK:JavaDevelopmentKitJRE:JavaRuntimeEnvironmen......
  • java高频面试题(反射、对象拷贝)
    java高频面试题(反射、对象拷贝)java高频面试题(反射、对象拷贝)1.什么是反射?反射主要是指程序可以访问、检测和修改它本身状态或行为的一种能力Java反射:在Java运行时环境......
  • Java Swing的练习感悟
    感悟心得这还是我第一次使用JavaSwing写代码呢,直接就是趣味性拉满!在相关的界面代码的基础上,在必要的位置嵌入Java代码,就可以很好的实现啦!简单的嘞!(有兴趣的各位可以选......
  • java安全 反序列化(一)
    介绍序列化就是把对象转换成字节流,便于保存在内存、文件、数据库中;反序列化即逆过程,由字节流还原成对象。序列化是一种对象持久化的手段,可以将对象的状态转换为字节数组,来......
  • 【JAVA笔记】JAVA-入门基础-面向对象04
    一、OOP详解面向对象编程(Object-OrientedProgramming,OOP)面向对象编程的本质就是:以类的方式组织代码,以对象的组织(封装)数据。抽象三大特性:封装、继承、多态、1、cla......