首页 > 编程语言 >面向对象程序设计(BLOG-3)

面向对象程序设计(BLOG-3)

时间:2022-12-10 19:13:05浏览次数:57  
标签:return double ArrayList CallRecord BLOG 面向对象 str 程序设计 public

面向对象程序设计(BLOG-3)   前言:本次博客的题目大都以多态、容器和继承的方式进行相关内容的考察,难度比之前的点线系列题目相对增加。   设计与分析   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,可根据理解自行调整:

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

输入样例:

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

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

输出样例:

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

079186300001 3.0 77.0
 

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

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

代码长度限制 40 KB 时间限制 400 ms 内存限制 64 MB
  代码如下:
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main {
    public static void main(String args[]) throws ParseException {
        Scanner input = new Scanner(System.in);
        User[] user = new User[20];
        int x = -1,g = 0;
        String str3 = "([1-2]\\d{3}[.]([1-9]|1[0,1,2])[.]([1-9]|[1-2]\\d|30|31))\\s((20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d)";
        while (true) {
            String arr = input.nextLine();
            if (arr.equals("end")) {
                break;
            }
            String[] str = arr.split(" ");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
            if ((str[0].matches("u-0791\\d{7,8}") && str[1].equals("0"))||
                    (str[0].matches("u-1\\d{10}")&&str[1].equals("1"))) {
                if(user[0]==null){
                    x++;
                    String[] arr1 = str[0].split("-");
                    user[x] = new User();
                    user[x].setNumber(arr1[1]);
                }
                else {
                    g=0;
                    for(int i = 0;user[i]!=null;i++) {
                        String[] arr1 = str[0].split("-");
                        if(user[i].number.equals(arr1[1])) {g++;}
                    }
                    if(g==0){
                        x++;
                        String[] arr1 = str[0].split("-");
                        user[x] = new User();
                        user[x].setNumber(arr1[1]);
                    }
                }
            }
            else {
                for (int k = 0; k<=x; k++) {
                    if (str[0].matches("t-0791\\d{7,8}")&&str[1].matches("0\\d{10,11}")&&
                            str[2].concat(" ").concat(str[3]).matches(str3)&&
                            str[4].concat(" ").concat(str[5]).matches(str3)&&
                            str.length==6) {
                        CallRecord CallRecord = new CallRecord();
                        CallRecord.setCallingAddressAreaCode(str[0].substring(2,6));
                        CallRecord.setAnswerAddressAreaCode(str[1].substring(0,4));
                        CallRecord.setStartTime(sdf.parse(str[2].concat(" ").concat(str[3])));
                        CallRecord.setEndTime(sdf.parse(str[4].concat(" ").concat(str[5])));
                        CallRecord.setCallingNumber(str[0].substring(2));
                        CallRecord.setAnswerNumber(str[1]);
                        if(CallRecord.endTime.getTime()>CallRecord.startTime.getTime()){
                            if (str[0].substring(2).equals(user[k].number)) {user[k].userRecords.addCallingInCityRecords(CallRecord);}
                        }
                    }
                    if(str[0].matches("t-0791\\d{7,8}")&&str[1].matches("1\\d{10}")&&
                    str[2].matches("0(([0-6]\\d{1,2})|(7[0-8]\\d)|([8-9](\\d)?))")&&str[3].concat(" ").concat(str[4]).matches(str3)&&
                            str[5].concat(" ").concat(str[6]).matches(str3)&&
                    str.length==7){
                        CallRecord CallRecord = new CallRecord();
                        CallRecord.setCallingAddressAreaCode(str[0].substring(2,6));
                        CallRecord.setAnswerAddressAreaCode(str[2]);
                        CallRecord.setStartTime(sdf.parse(str[3].concat(" ").concat(str[4])));
                        CallRecord.setEndTime(sdf.parse(str[5].concat(" ").concat(str[6])));
                        CallRecord.setCallingNumber(str[0].substring(2));
                        CallRecord.setAnswerNumber(str[1]);
                        if(CallRecord.endTime.getTime()>CallRecord.startTime.getTime()){
                            if (str[0].substring(2).equals(user[k].number)) {user[k].userRecords.addCallingInCityRecords(CallRecord);}
                            if (str[1].equals((user[k].number))) {
                                if (str[2].matches("0791")) {
                                    CallRecord.setCallingAddressAreaCode(" ");
                                    CallRecord.setCallingNumber(" ");
                                    user[k].userRecords.addAnswerInCityRecords(CallRecord);
                                }
                                else if(str[2].matches("(079[0,2,3,4,5,6,7,8,9])|(0701)")){
                                    CallRecord.setCallingAddressAreaCode(" ");
                                    CallRecord.setCallingNumber(" ");
                                    user[k].userRecords.addAnswerInProvinceRecords(CallRecord);
                                }
                                else if(str[2].matches("0(([0-6]\\d{1,2})|(7[0-8]\\d)|([8-9](\\d)?))")){
                                    CallRecord.setCallingAddressAreaCode(" ");
                                    CallRecord.setCallingNumber(" ");
                                    user[k].userRecords.addAnswerInLandRecords(CallRecord);
                                }
                            }
                        }
                    }
                    if(str[0].matches("t-1\\d{10}")&&str[1].matches("0\\d{2,3}")&&
                            str[2].matches("0\\d{10,11}")&&
                            str[3].concat(" ").concat(str[4]).matches(str3)&&
                            str[5].concat(" ").concat(str[6]).matches(str3)&&
                            str.length==7){
                        CallRecord CallRecord = new CallRecord();
                        CallRecord.setCallingAddressAreaCode(str[1]);
                        CallRecord.setAnswerAddressAreaCode(str[2].substring(0,4));
                        CallRecord.setStartTime(sdf.parse(str[3].concat(" ").concat(str[4])));
                        CallRecord.setEndTime(sdf.parse(str[5].concat(" ").concat(str[6])));
                        CallRecord.setCallingNumber(str[0].substring(2));
                        CallRecord.setAnswerNumber(str[2]);
                        if(CallRecord.endTime.getTime()>CallRecord.startTime.getTime()){
                            if (str[0].substring(2).equals(user[k].number)&&str[1].matches("0791")) {
                                user[k].userRecords.addCallingInCityRecords(CallRecord);
                            }
                            else if(str[0].substring(2).equals(user[k].number)&&str[1].matches("(079[0,,2,3,4,5,6,7,8,9])|(0701)")){
                                user[k].userRecords.addCallingInProvinceRecords(CallRecord);
                            }
                            else if(str[0].substring(2).equals(user[k].number)&&str[1].matches("0(([0-6]\\d{1,2})|(7[0-8]\\d)|([8-9](\\d)?))")){
                                user[k].userRecords.addCallingInLandRecords(CallRecord);
                            }
                        }
                    }
                    if(str[0].matches("t-1\\d{10}")&&str[1].matches("0\\d{2,3}")&&
                            str[2].matches("1\\d{10}")&&str[3].matches("0\\d{2,3}")&&
                            str[4].concat(" ").concat(str[5]).matches(str3)&&
                            str[6].concat(" ").concat(str[7]).matches(str3)&&
                            str.length==8){
                        CallRecord CallRecord = new CallRecord();
                        CallRecord.setCallingAddressAreaCode(str[1]);
                        CallRecord.setAnswerAddressAreaCode(str[3]);
                        CallRecord.setStartTime(sdf.parse(str[4].concat(" ").concat(str[5])));
                        CallRecord.setEndTime(sdf.parse(str[6].concat(" ").concat(str[7])));
                        CallRecord.setCallingNumber(str[0].substring(2));
                        CallRecord.setAnswerNumber(str[2]);
                        if(CallRecord.endTime.getTime()>CallRecord.startTime.getTime()){
                            if (str[0].substring(2).equals(user[k].number)&&str[1].matches("0791")) {
                                user[k].userRecords.addCallingInCityRecords(CallRecord);
                            }
                            else if(str[0].substring(2).equals(user[k].number)&&str[1].matches("(079[0,,2,3,4,5,6,7,8,9])|(0701)")){
                                user[k].userRecords.addCallingInProvinceRecords(CallRecord);
                            }
                            else if(str[0].substring(2).equals(user[k].number)&&str[1].matches("0(([0-6]\\d{1,2})|(7[0-8]\\d)|([8-9](\\d)?))")){
                                user[k].userRecords.addCallingInLandRecords(CallRecord);
                            }
                            if (str[2].equals(user[k].number)&&str[3].matches("0791")) {
                                user[k].userRecords.addAnswerInCityRecords(CallRecord);
                            }
                            else if(str[2].equals(user[k].number)&&str[3].matches("(079[0,2,3,4,5,6,7,8,9])|(0701)")){
                                user[k].userRecords.addAnswerInProvinceRecords(CallRecord);
                            }
                            else if(str[2].equals(user[k].number)&&str[3].matches("0(([0-6]\\d{1,2})|(7[0-8]\\d)|([8-9](\\d)?))")){
                                user[k].userRecords.addAnswerInLandRecords(CallRecord);
                            }
                        }
                    }
                }
            }
        }
        String[] arr3 = new String [x+1];
        for (int k = 0;k<=x;k++){
            arr3[k] = user[k].number;
        }
        Arrays.sort(arr3, String::compareToIgnoreCase);
        for(int k = 0;k<=x;k++){
            for(int j = 0;j<=x;j++){
                if(user[j].number.equals(arr3[k])){
                    User az;
                    az = user[k];
                    user[k] = user[j];
                    user[j] = az;
                }
            }
        }
        for(int k = 0;k<=x;k++){
            for(int j = 0;j<=user[k].userRecords.callingInCityRecords.size()-1;j++){
                for(int i = j+1;i<=user[k].userRecords.callingInCityRecords.size()-1;i++){
                    if(i==j){break;}
                    if(user[k].userRecords.callingInCityRecords.get(i).answerNumber.equals(user[k].userRecords.callingInCityRecords.get(j).answerNumber)&&
                            user[k].userRecords.callingInCityRecords.get(i).startTime.getTime()==user[k].userRecords.callingInCityRecords.get(j).startTime.getTime()&&
                            user[k].userRecords.callingInCityRecords.get(i).endTime.getTime()==user[k].userRecords.callingInCityRecords.get(j).endTime.getTime()){
                        user[k].userRecords.callingInCityRecords.remove(j);
                    }
                }
            }
        }
        LandlinePhoneCharging LandlinePhoneCharging = new LandlinePhoneCharging();
        MobilePhoneCharges MobilePhoneCharges = new MobilePhoneCharges();
        DecimalFormat df = new DecimalFormat("0.0");
        for(int k = 0;k<=x;k++){
            if(user[k].getNumber().matches("0\\d{10,11}")){
                System.out.println(user[k].number+" "+Double.parseDouble(df.format(LandlinePhoneCharging.calCost(user[k].userRecords)))+" "+Double.parseDouble(df.format(LandlinePhoneCharging.getMonthlyRent())));
            }
            else if (user[k].getNumber().matches("1\\d{10}")) {
                System.out.println(user[k].number + " " + Double.parseDouble(df.format(MobilePhoneCharges.calCost(user[k].userRecords))) + " " + Double.parseDouble(df.format(MobilePhoneCharges.getMonthlyRent())));
            }
        }
    }
}
class User{
    UserRecords userRecords = new UserRecords();
    double balance = 100;
    ChargeMode chargeMode = new ChargeMode();
    String number;
    public double calBalance(){
        return 0;
    }
    public double calCost(){
        return 0;
    }
    public UserRecords getUserRecords() {
        return userRecords;
    }

    public String getNumber() {
        return number;
    }

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

    public ChargeMode getChargeMode() {
        return chargeMode;
    }

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

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public void setUserRecords(UserRecords userRecords) {this.userRecords = userRecords; }
}
class ChargeMode{
    ArrayList<ChargeRule> chargeRule = new ArrayList<>();
    public ArrayList<ChargeRule> getChargeRule() {
        return chargeRule;
    }
    public void setChargeRule(ArrayList<ChargeRule> chargeRule) {this.chargeRule = chargeRule;}
    public double calCost(UserRecords userRecords) {return 0;}
    public double getMonthlyRent() {return 0;}
}
class UserRecords{
    ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();
    ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();
    ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();
    ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();
    ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();
    ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();
    ArrayList<MessageRecords> sendMessageRecords = new ArrayList<MessageRecords>();
    ArrayList<MessageRecords> receiveMessageRecords = new ArrayList<MessageRecords>();
    public void addCallingInCityRecords(CallRecord callRecord) {
        callingInCityRecords.add(callRecord);
    }
    public void addCallingInProvinceRecords(CallRecord callRecord) {
        callingInProvinceRecords.add(callRecord);
    }
    public void addCallingInLandRecords(CallRecord callRecord) {
        callingInLandRecords.add(callRecord);
    }
    public void addAnswerInCityRecords(CallRecord callRecord) {
        answerInCityRecords.add(callRecord);
    }
    public void addAnswerInProvinceRecords(CallRecord callRecord) {
        answerInProvinceRecords.add(callRecord);
    }
    public void addAnswerInLandRecords(CallRecord callRecord) {
        answerInLandRecords.add(callRecord);
    }
    public void addSendMessageRecords(MessageRecords MessageRecords) {
        sendMessageRecords.add(MessageRecords);
    }
    public void addReceiveMessageRecords(MessageRecords MessageRecords) {
        receiveMessageRecords.add(MessageRecords);
    }
    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<MessageRecords> getSendMessageRecords() {
        return sendMessageRecords;
    }
    public ArrayList<MessageRecords> getReceiveMessageRecords() {
        return receiveMessageRecords;
    }
}
class LandlinePhoneCharging extends ChargeMode{
    double monthlyRent = 20;
    double cost,cost1,cost2,cost3;
    public double calCost(UserRecords userRecords) {
        cost = 0;
        cost1 = 0;
        cost2 = 0;
        cost3 = 0;
        LandPhoneInCityRule landPhoneInCityRule = new LandPhoneInCityRule();
        LandPhoneInProvinceRule landPhoneInProvinceRule = new LandPhoneInProvinceRule();
        LandPhoneInLandRule landPhoneInLandRule = new LandPhoneInLandRule();
        for(CallRecord CallRecord : userRecords.callingInCityRecords){
            if(CallRecord.answerAddressAreaCode.matches("0791")){
                cost1 = landPhoneInCityRule.calCost(userRecords.callingInCityRecords);
            } else if (CallRecord.answerAddressAreaCode.matches("(079[0,2,3,4,5,6,7,8,9])|(0701)")) {
                cost2 = landPhoneInProvinceRule.calCost(userRecords.callingInCityRecords);
            } else if (CallRecord.answerAddressAreaCode.matches("0(([0-6]\\d{1,2})|(7[0-8]\\d)|([8-9](\\d)?))")) {
                cost3 = landPhoneInLandRule.calCost(userRecords.callingInCityRecords);
            }
        }
        cost += cost1+cost2+cost3;
        return cost;
    }
    public double getMonthlyRent() {
        return 100-monthlyRent-cost;
    }
}
class MobilePhoneCharges{
    double monthlyRent = 15;
    double cost;
    public double calCost(UserRecords userRecords) {
        cost = 0;
        LandPhoneInCityRule landPhoneInCityRule = new LandPhoneInCityRule();
        LandPhoneInProvinceRule landPhoneInProvinceRule = new LandPhoneInProvinceRule();
        LandPhoneInLandRule landPhoneInLandRule = new LandPhoneInLandRule();
        cost += landPhoneInCityRule.calCost(userRecords.callingInCityRecords)+landPhoneInCityRule.calCost(userRecords.answerInCityRecords)+
                landPhoneInCityRule.calCost(userRecords.callingInProvinceRecords)+landPhoneInCityRule.calCost(userRecords.answerInProvinceRecords)+
                landPhoneInCityRule.calCost(userRecords.callingInLandRecords)+landPhoneInCityRule.calCost(userRecords.answerInLandRecords)+
                landPhoneInProvinceRule.calCost(userRecords.callingInCityRecords)+landPhoneInProvinceRule.calCost(userRecords.answerInCityRecords)+
                landPhoneInProvinceRule.calCost(userRecords.callingInProvinceRecords)+landPhoneInProvinceRule.calCost(userRecords.answerInProvinceRecords)+
                landPhoneInProvinceRule.calCost(userRecords.callingInLandRecords)+landPhoneInProvinceRule.calCost(userRecords.answerInLandRecords)+
                landPhoneInLandRule.calCost(userRecords.callingInCityRecords)+landPhoneInLandRule.calCost(userRecords.answerInCityRecords)+
                landPhoneInLandRule.calCost(userRecords.callingInProvinceRecords)+landPhoneInLandRule.calCost(userRecords.answerInProvinceRecords)+
                landPhoneInLandRule.calCost(userRecords.callingInLandRecords)+landPhoneInLandRule.calCost(userRecords.answerInLandRecords);
        return cost;
    }
    public double getMonthlyRent() {
        return 100-monthlyRent-cost;
    }
}
class CallRecord extends CommunicationRecord{
    Date startTime;
    Date endTime;
    String callingAddressAreaCode;
    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;
    }
}
class MessageRecords extends CommunicationRecord{
    String message;
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}
abstract class ChargeRule{
}
abstract class CommunicationRecord{
    String callingNumber;
    String answerNumber;
    public String getCallingNumber() {
        return callingNumber;
    }
    public void setCallingNumber(String callingNumber) {
        this.callingNumber = callingNumber;
    }
    public String getAnswerNumber() {
        return answerNumber;
    }
    public void setAnswerNumber(String answerNumber) {this.answerNumber = answerNumber;}
}
abstract class CallChargeRule extends ChargeRule{
    public double calCost(ArrayList<CallRecord> callRecords) {
        return 0;
    }
}
class LandPhoneInCityRule extends CallChargeRule{
    double cost;
    public double calCost(ArrayList<CallRecord> callRecords) {
        cost = 0;
        for (CallRecord callRecord : callRecords) {
            if(callRecord.getCallingNumber().matches("0\\d{10,11}")&&callRecord.getAnswerAddressAreaCode().matches("0791")){
                double x = callRecord.endTime.getTime() - callRecord.startTime.getTime();
                double second = (x / 1000);
                double minute = second / 60;
                if (second % 60 == 0) {
                    cost += minute * 0.1;
                } else {
                    cost += ((int) minute * 0.1 + 0.1);
                }
            } else if(callRecord.getCallingNumber().matches("1\\d{10}")&&callRecord.getAnswerAddressAreaCode().matches("0791")&&callRecord.getCallingAddressAreaCode().matches("0791")){
                double x = callRecord.endTime.getTime() - callRecord.startTime.getTime();
                double second = (x / 1000);
                double minute = second / 60;
                if (second % 60 == 0) {
                    cost += minute * 0.1;
                } else {
                    cost += ((int) minute * 0.1 + 0.1);
                }
            } else if(callRecord.getCallingNumber().matches("1\\d{10}")&&callRecord.getAnswerAddressAreaCode().matches("(079[0,2,3,4,5,6,7,8,9])|(0701)")&&callRecord.getCallingAddressAreaCode().matches("0791")){
                double x = callRecord.endTime.getTime() - callRecord.startTime.getTime();
                double second = (x / 1000);
                double minute = second / 60;
                if (second % 60 == 0) {
                    cost += minute * 0.2;
                } else {
                    cost += ((int) minute * 0.2 + 0.2);
                }
            } else if (callRecord.getCallingNumber().matches("1\\d{10}")&&callRecord.getAnswerAddressAreaCode().matches("0(([0-6]\\d{1,2})|(7[0-8]\\d)|([8-9](\\d)?))")&&callRecord.getCallingAddressAreaCode().matches("0791")) {
                double x = callRecord.endTime.getTime() - callRecord.startTime.getTime();
                double second = (x / 1000);
                double minute = second / 60;
                if (second % 60 == 0) {
                    cost += minute * 0.3;
                } else {
                    cost += ((int) minute * 0.3 + 0.3);
                }
            }
        }
        return cost;
    }
}
class LandPhoneInProvinceRule extends CallChargeRule{
    double cost;
    public double calCost(ArrayList<CallRecord> callRecords) {
        cost = 0;
        for (CallRecord callRecord : callRecords) {
            if(callRecord.getCallingNumber().matches("0\\d{10,11}")&&callRecord.getAnswerAddressAreaCode().matches("(079[0,2,3,4,5,6,7,8,9])|(0701)")){
                double x = callRecord.endTime.getTime() - callRecord.startTime.getTime();
                double second = (x / 1000);
                double minute = second / 60;
                if (second % 60 == 0) {
                    cost += minute * 0.3;
                } else {
                    cost += ((int) minute * 0.3 + 0.3);
                }
            } else if (callRecord.getCallingNumber().matches("1\\d{10}")&&callRecord.getCallingAddressAreaCode().matches("(079[0,2,3,4,5,6,7,8,9])|(0701)")) {
                double x = callRecord.endTime.getTime() - callRecord.startTime.getTime();
                double second = (x / 1000);
                double minute = second / 60;
                if (second % 60 == 0) {
                    cost += minute * 0.3;
                } else {
                    cost += ((int) minute * 0.3 + 0.3);
                }
            }
        }
        return cost;
    }
}
class LandPhoneInLandRule extends CallChargeRule{
    double cost;
    public double calCost(ArrayList<CallRecord> callRecords) {
        cost = 0;
        for (CallRecord callRecord : callRecords) {
            if(callRecord.getCallingNumber().matches("0\\d{10,11}")&&callRecord.getAnswerAddressAreaCode().matches("0(([0-6]\\d{1,2})|(7[0-8]\\d)|([8-9](\\d)?))")){
                double x = callRecord.endTime.getTime() - callRecord.startTime.getTime();
                double second = (x / 1000);
                double minute = second / 60;
                if (second % 60 == 0) {
                    cost += minute * 0.6;
                } else {
                    cost += ((int) minute * 0.6 + 0.6);
                }
            } else if (callRecord.getCallingNumber().matches("1\\d{10}")&&callRecord.getCallingAddressAreaCode().matches("0(([0-6]\\d{1,2})|(7[0-8]\\d)|([8-9](\\d)?))")) {
                double x = callRecord.endTime.getTime() - callRecord.startTime.getTime();
                double second = (x / 1000);
                double minute = second / 60;
                if (second % 60 == 0) {
                    cost += minute * 0.6;
                } else {
                    cost += ((int) minute * 0.6 + 0.6);
                }
            } else if (callRecord.getAnswerNumber().matches("1\\d{10}")&&callRecord.getAnswerAddressAreaCode().matches("0(([0-6]\\d{1,2})|(7[0-8]\\d)|([8-9](\\d)?))")) {
                double x = callRecord.endTime.getTime() - callRecord.startTime.getTime();
                double second = (x / 1000);
                double minute = second / 60;
                if (second % 60 == 0) {
                    cost += minute * 0.3;
                } else {
                    cost += ((int) minute * 0.3 + 0.3);
                }
            }
        }
        return cost;
    }
}
View Code

 

本题目依照老师给的类图就可以写出来,难度不是很大,但是我case9和case12没有运行,这我当时没想明白。   SourceMonitor的生成报表内容如下

 

 

    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 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)。
注意:以上图中所定义的类不是限定要求,根据实际需要自行补充或修改。

输入样例:

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

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

输出样例:

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

13811111111 3.0 82.0
 

更多内容详见附件:

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

代码长度限制 40 KB 时间限制 400 ms 内存限制 64 MB   代码如下:
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();
 
        }
 
    }
 
}
 
abstract class ChargeMode {
    protected ArrayList<ChargeRule> chargeRules = new ArrayList<>();
 
    public abstract double calCost(UserRecords userRecords);
 
    public abstract double getMonthlyRent();
 
    public ArrayList<ChargeRule> getChargeRules() {
        return chargeRules;
    }
 
    public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
        this.chargeRules = chargeRules;
    }
}
 
class UserRecords {
 
    private ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();
    private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();
    private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();
 
    public void addCallingInCityRecords(CallRecord callRecord) {
        callingInCityRecords.add(callRecord);
    }
 
    public void addCallingInProvinceRecords(CallRecord callRecord) {
        callingInProvinceRecords.add(callRecord);
    }
 
    public void addCallingInLandRecords(CallRecord callRecord) {
        callingInLandRecords.add(callRecord);
    }
 
    public void addAnswerInCityRecords(CallRecord callRecord) {
        answerInCityRecords.add(callRecord);
    }
 
    public void 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;
    }
 
}
 
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;
    }
 
}
 
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') {
 
        }
 
    }
 
}
 
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;
    }
 
}
 
abstract class ChargeRule {
 
    abstract public double calCost(UserRecords userRecords);
 
}
 
class CallRecord extends CommunicationRecord {
    private Date startTime;
    private Date endTime;
    private String callingAddressAreaCode;
    private String answerAddressAreaCode;
 
    public String getCallType() {
        String type = "";
        if (callingAddressAreaCode.equals("0791")) {
            type = type.concat("1");
        } else if (callingAddressAreaCode.matches("^079[023456789]$") || callingAddressAreaCode.equals("0701")) {
            type = type.concat("2");
        } else {
            type = type.concat("3");
        }
 
        if (answerAddressAreaCode.equals("0791")) {
            type = type.concat("1");
        } else if (answerAddressAreaCode.matches("^079[023456789]$") || answerAddressAreaCode.equals("0701")) {
            type = type.concat("2");
        } else {
            type = type.concat("3");
        }
 
        return type;
    }
 
    public CallRecord(String[] inputs) {
        super();
 
        char type = inputs[0].charAt(0);
        inputs[0] = inputs[0].substring(2);
 
        String sd = null, st = null, ed = null, et = null;
 
        if (type == 't') {
            if (inputs.length == 6) {
                sd = inputs[2];
                st = inputs[3];
                ed = inputs[4];
                et = inputs[5];
                callingAddressAreaCode = inputs[0].substring(0, 4);
                answerAddressAreaCode = inputs[1].substring(0, 4);
            } else if (inputs.length == 7) {
                sd = inputs[3];
                st = inputs[4];
                ed = inputs[5];
                et = inputs[6];
                if (inputs[0].charAt(0) != '0') {
                    if (inputs[2].length() == 10) {
                        answerAddressAreaCode = inputs[2].substring(0, 3);
                    } else {
                        answerAddressAreaCode = inputs[2].substring(0, 4);
                    }
                    callingAddressAreaCode = inputs[1];
                } else {
                    if (inputs[0].length() == 10) {
                        callingAddressAreaCode = inputs[0].substring(0, 3);
                    } else {
                        callingAddressAreaCode = inputs[0].substring(0, 4);
                    }
                    answerAddressAreaCode = inputs[2];
                }
            } else if (inputs.length == 8) {
                sd = inputs[4];
                st = inputs[5];
                ed = inputs[6];
                et = inputs[7];
                callingAddressAreaCode = inputs[1];
                answerAddressAreaCode = inputs[3];
            }
        } else if (type == 'm') {
 
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault());
        
 
    }
 
    public CallRecord(Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) {
        super();
        this.startTime = startTime;
        this.endTime = endTime;
        this.callingAddressAreaCode = callingAddressAreaCode;
        this.answerAddressAreaCode = answerAddressAreaCode;
    }
 
    public Date getStartTime() {
        return startTime;
    }
 
    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }
 
    public Date getEndTime() {
        return endTime;
    }
 
    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }
 
    public String getCallingAddressAreaCode() {
        return callingAddressAreaCode;
    }
 
    public void setCallingAddressAreaCode(String callingAddressAreaCode) {
        this.callingAddressAreaCode = callingAddressAreaCode;
    }
 
    public String getAnswerAddressAreaCode() {
        return answerAddressAreaCode;
    }
 
    public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
        this.answerAddressAreaCode = answerAddressAreaCode;
    }
}
 
abstract class CallChargeRule extends ChargeRule {
 
}
 
class LandPhoneInCityRule extends CallChargeRule {
 
    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (CallRecord call : userRecords.getCallingInCityRecords()) {
            double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (distanceS < 0) {
                continue;
            }
            double distanceM = (int) distanceS / 60;
            if (distanceS % 60 != 0) {
                distanceM += 1;
            }
            if (call.getCallType().equals("11")) {
                sumCost += distanceM * 0.1;
            } else if (call.getCallType().equals("12")) {
                sumCost += distanceM * 0.3;
            } else if (call.getCallType().equals("13")) {
                sumCost += distanceM * 0.6;
            }
        }
        return sumCost;
    }
 
}
 
class LandPhoneInlandRule extends CallChargeRule {
 
    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (CallRecord call : userRecords.getCallingInLandRecords()) {
            double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (distanceS < 0) {
                continue;
            }
            double distanceM = (int) distanceS / 60;
            if (distanceS % 60 != 0) {
                distanceM += 1;
            }
            sumCost += distanceM * 0.6;
        }
        return sumCost;
    }
 
}
 
class LandPhoneInProvinceRule extends CallChargeRule {
 
    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
            double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (distanceS < 0) {
                continue;
            }
            double distanceM = (int) distanceS / 60;
            if (distanceS % 60 != 0) {
                distanceM += 1;
            }
            sumCost += distanceM * 0.3;
        }
        return sumCost;
    }
 
}
 
class MobilePhoneInCityRule extends CallChargeRule {
 
    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (CallRecord call : userRecords.getCallingInCityRecords()) {
            double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (distanceS < 0) {
                continue;
            }
            double distanceM = (int) distanceS / 60;
            if (distanceS % 60 != 0) {
                distanceM += 1;
            }
            if (call.getCallType().equals("11")) {
                sumCost += distanceM * 0.1;
            } else if (call.getCallType().equals("12")) {
                sumCost += distanceM * 0.2;
            } else if (call.getCallType().equals("13")) {
                sumCost += distanceM * 0.3;
            }
 
        }
        return sumCost;
    }
 
}
 
class MobilePhoneInlandRule extends CallChargeRule {
 
    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (CallRecord call : userRecords.getCallingInLandRecords()) {
            double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (distanceS < 0) {
                continue;
            }
            double distanceM = (int) distanceS / 60;
            if (distanceS % 60 != 0) {
                distanceM += 1;
            }
            sumCost += distanceM * 0.6;
        }
        for (CallRecord call : userRecords.getAnswerInLandRecords()) {
            double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (distanceS < 0) {
                continue;
            }
            double distanceM = (int) distanceS / 60;
            if (distanceS % 60 != 0) {
                distanceM += 1;
            }
            sumCost += distanceM * 0.3;
        }
        return sumCost;
    }
 
}
 
class MobilePhoneInProvinceRule extends CallChargeRule {
 
    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
            double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (distanceS < 0) {
                continue;
            }
            double distanceM = (int) distanceS / 60;
            if (distanceS % 60 != 0) {
                distanceM += 1;
            }
            if (call.getCallType().equals("21")) {
                sumCost += distanceM * 0.3;
            } else if (call.getCallType().equals("22")) {
                sumCost += distanceM * 0.3;
            } else if (call.getCallType().equals("23")) {
                sumCost += distanceM * 0.3;
            }
        }
        return sumCost;
    }
 
}
 
class MessageRecord extends CommunicationRecord {
 
    private String message;
 
    public String getMessage() {
        return message;
    }
 
    public void setMessage(String message) {
        this.message = message;
    }
}
 
class User {
 
    private UserRecords userRecords = new UserRecords();
    private double balance = 100;
    private ChargeMode chargeMode;
    private String number;
 
    public double calCost() {
        return chargeMode.calCost(userRecords);
    }
 
    public double calBalance() {
        return balance - chargeMode.getMonthlyRent() - chargeMode.calCost(userRecords);
    }
 
    public UserRecords getUserRecords() {
        return userRecords;
    }
 
    public void setUserRecords(UserRecords userRecords) {
        this.userRecords = userRecords;
    }
 
    public ChargeMode getChargeMode() {
        return chargeMode;
    }
 
    public void setChargeMode(ChargeMode chargeMode) {
        this.chargeMode = chargeMode;
    }
 
    public String getNumber() {
        return number;
    }
 
    public void setNumber(String number) {
        this.number = number;
    }
 
}
 
class Outputtool {
 
    @SuppressWarnings("deprecation")
    public void output(double out) {
//        java.text.DecimalFormat df=new java.text.DecimalFormat("#.##");
//        String a=df.format(out);
//        System.out.print(a);
        BigDecimal numb = new BigDecimal(out);
        out = numb.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.print(out);
    }
}
View Code

SourceMonitor的生成报表内容如下:

 

在本次题目集我学会了如何做出一个符合SOLID原则代码。同时,正则表达式与split()方法也加深了理解。哦对,还有simpleDate方法。这是一个更加完善的计算时间的方式。

有类似于正则的功能,也有计算时间之差的功能。

生成类图如下:

 

 

 

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:

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)。
 

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

输入样例:

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

u-18907910010 3
m-18907910010 13305862264 aaaaaaaaaaaaaaaaaaaaaaa
end
 

输出样例:

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

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

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

u-18907910010 3
m-18907910010 13305862264 aaaaaaaaaaaa
m-18907910010 13305862264 aaaaaaa.
m-18907910010 13305862264 bb,bbbb
end
 

输出样例1:

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

18907910010 0.5 99.5
  代码长度限制 20 KB 时间限制 400 ms 内存限制 64 MB   代码如下:
import java.util.ArrayList;
import java.util.Scanner;
import java.text.DecimalFormat;
import java.util.Comparator;

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String mq=sc.nextLine();
        ArrayList<User> Users=new ArrayList<User>();
        DecimalFormat df = new DecimalFormat("0.00");
        User user;
        Xiaoxi xiaoxi;
        String nr;
        String hm2;
        while(!mq.equals("end")) {
            
        if(mq.matches("u-1[0-9]{10} 3")) {
            user=new User();
            user.number=mq.substring(2,13);
            Users.add(user);
            }
        else if(mq.matches("m-1[0-9]{10} 1[0-9]{10} [a-z A-Z 0-9,.]+")) {
                hm2=mq.substring(2,13);
                for(User xd: Users) {
                    if(hm2.equals(xd.number)) {
                        xiaoxi=new Xiaoxi();
                        xiaoxi.number1=mq.substring(2,13);
                        xiaoxi.number2=mq.substring(14,25);
                        nr=mq.substring(26);
                        xiaoxi.isneirong=nr;
                        if(Xiaoxi.isduanxin(nr)) {
                            for(User a:Users) {
                                if(a.number.equals(xiaoxi.number1))
                                    a.All.add(xiaoxi);
                            }
                        }
                    }
             }
                
                
           }
            else
                {}
            mq=sc.nextLine();
         }
        int sum=0;
        for(User a:Users) {
            for(Xiaoxi b:a.All) {
                double n1=b.isneirong.length()/10.0;
                int n2=(int) n1;
                if(n1-n2>0)    n2++;
                    sum+=n2;
            }
            if(sum>5)
                a.buy+=0.7+(sum-5)*0.3;
            else if(sum>3)
                a.buy+=0.3+(sum-3)*0.2;
            else
                a.buy+=sum*0.1;
            a.balance-=a.buy;
            
            sum=0;
        }
        Users.sort(new Sort_length());
        for(User a:Users) {
            System.out.println(a.number+" "+Double.valueOf(df.format(a.buy))+" "+Double.valueOf(df.format(a.balance)));
        }
    }
}

class Sort_length implements Comparator<User>{
    public int compare(User a,User b) {
        return a.number.compareTo(b.number);
    }
}

class Number{
    public static boolean isdianhua(String haoma) {
        if(haoma.matches("u-1[0-9]{10} 3")) {
            return true;
        }
        return false;
    }
    
}
class Xiaoxi{
    public String isneirong;
    String number1;
    String number2;
    
    public static boolean isduanxin(String xinxi) {
    if(xinxi.matches("[0-9 a-z A-Z,.]+")) {
        return true;
    }
    return false;
    }
}
class User{
    String number;
    double balance=100;
    double buy=0;
    ArrayList<Xiaoxi> All = new  ArrayList<Xiaoxi>();
    
}
View Code

SourceMonitor的生成报表内容如下:

生成类图如下:

 

 踩坑心得:

 

1. 正则里出错的大概有三个部分。一个是时间虽然用simpleDate,但是它不会帮你注意的是,月份前不能加0,如一月不能写成01月。一个是时间必须是两位数,如01:01:01是正确输入。还有一个是区号

可以为三位数,比如北京为010,当时我的正则是按照四位数,测试点没过。最后在通讯收费里,空格也算是输入一部分。

 

2. 计费信息里,需要额外创建一个userRecord对象,然后用set方法存入信息各种。否则将不会被录入信息。也不会被计费。

 

改进建议:

 

1.正则表达式的测试工具很难识别%d,%s等,最好用[0-9][a-zA-Z]来代替测试。

 

2. 正则和string自带方法可以灵活使用,不一定非得用某一种。

 

 

总结:

 这次作业是分三次做完的,老师给出了几个类图,按照老师给的类图可以将程序一一进行下去。

搞清楚父类之间的关系,用户记录管理记录,然后用户通过收费模式能够计算出该模式的总价,然后不同模式下有着相应的收费标准,不同套餐得到用户的记录,然后把所有规则都循环对应计费,得出总价。

其中正则表达式的应用可能会比较复杂,涉及到时间、数据切割和打包处理成记录(callRecord),还需要多加练习。

PS:经过本学期的学习,我学到的东西还是有很多的,而且老师的教学方式也很有趣,上课时候举例的好多例子对我来说印象都挺深刻,例如那个变形金刚大黄蜂遗传父类就十分有趣,除此之外还有

南昌道路关于土块掉落的市容问题的解决也很贴近生活,这样的上课方式十分轻松,在轻巧的语言交流中就能将所学知识融进去,课堂氛围感拉满了哈哈哈。而在课程实验中,老师也能给予很多的帮助,每次

上课时都会给有问题的同学排忧解难,挺好的哈哈哈,本学期终将结束,今天就记录下本学期的最后一篇博客吧,拜拜咯,愿诸君顶峰相见!)

 

 

标签:return,double,ArrayList,CallRecord,BLOG,面向对象,str,程序设计,public
From: https://www.cnblogs.com/1351322562zjy/p/16972106.html

相关文章

  • BLOG-3
    1.前言该bolg将对近三次的大作业进行总结与分析。近三次大作业主要写了电信计费系统的主要功能,第一次大作业是座机计费功能,第一次电信计费题量不多,只有两题,电信计费第一次......
  • The Last Blog
    前言本次Blog是对6-8次的PTA进行总结,电信计费系列,包含三个部分:系列1-座机计费,系列2-手机+座机计费,系列3-短信计费。难度相对于前几次pta多边形的作业小了一些,这三次大......
  • C/C++《程序设计与算法综合实践》备选题目
    C/C++《程序设计与算法综合实践》备选题目《程序设计与算法综合实践》期末大作业题目及评分标准有如下情况之一者,为不及格。(1)未能完成所选题目评分标准的最低要求。(2)......
  • BLOG-3
    一:前言对于近期的学习:主要是进行了pta的作业的学习,第七次题目集是因为要用到之前的那些图形有关的代码,而之前的图形题目因为未完全做出,而导致了第七次pta作业的很多问题,三......
  • BLOG-3
    一、前言:这几个星期接触了电信计费的问题,这几次作业题量都相对来说不大,难度相对之前的点线形系列较低。通过这几次的题目集重新巩固了正则表达式,以及继承与多态的运用......
  • Day7 - 面向对象编程进阶
    本节内容:面向对象高级语法部分经典类vs新式类静态方法、类方法、属性方法类的特殊方法反射异常处理Socket开发基础作业:开发一个支持多用户在线的FTP程序 1.经典类......
  • 19. 面向对象
    JS中的面向对象JS中的面向对象,是基于原型的面向对象。JS中的对象(Object)是依靠构造器(constructor)和原型(prototype)构造出来的。另外,在ES6中,新引入了类(Class)和继承(Exten......
  • 面向对象
    Loli.cs1publicclassLoli2{3publicstringname;4publicboolsex;5publicintage;6publicintheight;7publicintwe......
  • 前端开发系列016-基础篇之Javascript面向对象(五)
    title:'前端开发系列016-基础篇之Javascript面向对象(五)'tags:-javaScript面向对象系列categories:[]date:2017-06-0110:52:16一、严格模式说明在JavaScri......
  • BLOG-3
    一、前言6-8的大作业主要是实现电信计费系统。二、设计与分析6-7-1电信计费系列1-座机计费实现一个简单的电信计费程序:假设南昌市电信分公司针对市内座机用户采用......