ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

BLOG-3

2022-06-16 17:04:03  阅读:158  来源: 互联网

标签:String double ArrayList BLOG 计费 new public


一、前言:

  近三次大作业主要写了电信计费系统的主要功能,第一次大作业是座机计费功能,第二次大作业是手机计费功能,第三次大作业是短信计费的功能。这三次大作业主要考察了:类的设计,正则表达式,以及Data 类的使用等知识点。总的来说这三次PTA难度不大,题量适中。\

 

二、设计与分析:

  

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元。
每条通讯信息单独计费后累加,不是将所有时间累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。

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

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Objects;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;

public class Main {
    public static void main(String[] args) throws ParseException {
        Scanner input = new Scanner(System.in);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        simpleDateFormat.setLenient(false);
        String str = input.nextLine();
        String[] string = str.split(" ");
        String[] strings = string[0].split("-");
        String r1 = "u-0791[0-9]{7,8} 0";
        String r2 = "t-0791[0-9]{7,8} [0-9]{10,12} [0-9]{4}\\.([1-9]|10|11|12)\\.([1-9]|[1-9][0-9]) ([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]) [0-9]{4}\\.([1-9]|10|11|12)\\.([1-9]|[1-9][0-9]) ([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])";
        String callnumber, anwsernumber, startTime, endTime, place;
        CallRecord callRecord;
        Date startDate, endDate;
        ChargeMode chargeMode = new LandLinePoneCharge();
        ArrayList<User> users = new ArrayList<User>();
        int i;
        while(!str.equals("end")) {
            if(!str.equals("")) {
                ParsePosition pos1 = new ParsePosition(0);
                ParsePosition pos2 = new ParsePosition(0);
                if(str.matches(r1)) {
                    callnumber = strings[1];
                    User user = new User(callnumber);
                    if(!users.contains(user)) {
                        user.setChargeMode(chargeMode);
                        users.add(user);
                    }
                }
                else if(str.matches(r2)){
                    callnumber = strings[1];
                    anwsernumber = string[1];
                    startTime = string[2] + " " + string[3];
                    endTime = string[4] + " " + string[5];
                    place = anwsernumber.substring(0, 4);
                    startDate = simpleDateFormat.parse(startTime, pos1);
                    endDate = simpleDateFormat.parse(endTime, pos2);
                    callRecord = new CallRecord(callnumber, anwsernumber, startDate, endDate);
                    
                    if(startDate!=null&&endDate!=null) {
                        for(i=0;i<users.size();i++) {
                            if(users.get(i).getNumber().equals(callnumber)) {
                                if(place.equals("0791")) {
                                    users.get(i).getUserRecords().addAnswerInCityRecords(callRecord);
                                }
                                else if(place.equals("0790")||place.equals("0792")||place.equals("0793")||place.equals("0794")||place.equals("0795")
                                        ||place.equals("0796")||place.equals("0797")||place.equals("0798")||place.equals("0799")||place.equals("0701")) {
                                    users.get(i).getUserRecords().addAnswerInProvinceRecords(callRecord);
                                }
                                else {
                                    users.get(i).getUserRecords().addAnswerInLandRecords(callRecord);
                                }
                                break;
                            }
                        }
                    }
                }
            }
            str = input.nextLine();
            string = str.split("\\s+");
            strings = string[0].split("-");
        }
        Collections.sort(users, new Comparator<User>() {
            @Override
            public int compare(User user1, User user2) {
                return user1.getNumber().compareTo(user2.getNumber());
            }
        });
        for(int j=0;j<users.size();j++) {
            System.out.println(users.get(j).getNumber() + " " + String.format("%.1f", users.get(j).calCost()) + " " + String.format("%.1f", users.get(j).calBalance()));
        }
    }
}
class User {
    private UserRecords userRecords = new UserRecords();
    private double balance = 100;
    private ChargeMode chargeMode;
    private String number;

    public User() {
        
        super();
    }

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

    public UserRecords getUserRecords() {
        return userRecords;
    }

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

    public double getBalance() {
        return balance;
    }
    
    public void setBalance(double balance) {
        this.balance = 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;
    }
    
    public double calBalance() {
        return getBalance() - calCost() - chargeMode.getMonthlyRent();
    }
    
    public double calCost() {
        return chargeMode.calCost(userRecords);
    }
    
    @Override
    public boolean equals(Object o) {
        if (o == null) {
          return false;
        }
        if (o instanceof User) {
          User user = (User) o;
          return  number.equals(user.getNumber());
        }
        return false;
      }
}
abstract class ChargeMode {
    private ArrayList<CallChargeRule> callChargeRules;
    
    public ArrayList<CallChargeRule> getCallChargeRules() {
        return callChargeRules;
    }

    public void setCallChargeRules(ArrayList<CallChargeRule> callChargeRules) {
        this.callChargeRules = callChargeRules;
    }

    public abstract double calCost(UserRecords userRecords);

    public abstract double getMonthlyRent();
}
class LandLinePoneCharge extends ChargeMode {
    private double monthlyRent = 20;
    private CallChargeRule landPhoneInCityRule = new LandPhoneInCityRule();
    private CallChargeRule landPhoneInProvinceRule = new LandPhoneInProvinceRule();
    private CallChargeRule landPhoneInLandRule = new LandPhoneInLandRule();
    
    public LandLinePoneCharge() {
        super();
    }

    @Override
    public double calCost(UserRecords userRecords) {
        double cost = 0;
        cost += landPhoneInCityRule.calCost(userRecords.getAnswerInCityRecords())
                + landPhoneInProvinceRule.calCost(userRecords.getAnswerInProvinceRecords())
                + landPhoneInLandRule.calCost(userRecords.getAnswerInLandRecords());
        return cost;
    }

    @Override
    public double getMonthlyRent() {
        return monthlyRent;
    }
    
}
class UserRecords {
    private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();
    
    public UserRecords() {
        super();
    }

    public UserRecords(ArrayList<CallRecord> answerInCityRecords,ArrayList<CallRecord> answerInProvinceRecords,
            ArrayList<CallRecord> answerInLandRecords) {
        super();
        this.answerInCityRecords = answerInCityRecords;
        this.answerInProvinceRecords = answerInProvinceRecords;
        this.answerInLandRecords = answerInLandRecords;
    }

    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 void addAnswerInCityRecords(CallRecord callRecord) {
        getAnswerInCityRecords().add(callRecord);
    }
    
    public void addAnswerInProvinceRecords(CallRecord callRecord) {
        getAnswerInProvinceRecords().add(callRecord);
    }
    
    public void addAnswerInLandRecords(CallRecord callRecord) {
        getAnswerInLandRecords().add(callRecord);
    }
}
abstract class CommunicationRecord {
    private String callingNumber;
    private String answerNumber;
    
    public CommunicationRecord(String callingNumber, String answerNumber) {
        super();
        this.callingNumber = callingNumber;
        this.answerNumber = 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;

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

    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 double getTime() {
        long time;
        time = (endTime.getTime() - startTime.getTime()) / 1000;
        if(time%60==0) {
            time /= 60;
        }
        else {
            time = time / 60 + 1;
        }
        return (double)time;
    }

}
abstract class CallChargeRule {
    
    public abstract double calCost(ArrayList<CallRecord> callRecords);
    
}
class LandPhoneInCityRule extends CallChargeRule {

    public LandPhoneInCityRule() {
        super();
    }

    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double city = 0;
        for(int i=0;i<callRecords.size();i++) {
            city += callRecords.get(i).getTime() * 0.1;
        }
        return city;
    }
    
}
class LandPhoneInProvinceRule extends CallChargeRule {

    public LandPhoneInProvinceRule() {
        super();
    }

    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double province = 0;
        for(int i=0;i<callRecords.size();i++) {
            province += callRecords.get(i).getTime() * 0.3;
        }
        return province;
    }
    
}
class LandPhoneInLandRule extends CallChargeRule {

    public LandPhoneInLandRule() {
        super();
    }

    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double land = 0;
        for(int i=0;i<callRecords.size();i++) {
            land += callRecords.get(i).getTime() * 0.6;
        }
        return land;
    }
    
}

 

总结:该题首先要建立User类,里面包含了建户的基本信息,号码,计费类型等属性。接着还需要一个类可以接收信息,处理信息,并将其所读取到的信息进行分别存储到两个容器,判断输入格式是否正确用正则表达式。然后将分类好的信息对应到每个用户之中,用UserRecord类来记录。通过对比输入信息中的号码与用户的开户号码,将通讯信息对应到每个用户下面,之后通过一系列计算将用户的电信费用展示出来。其中还需要我们创建新类,计算电话的时间差,以此来计算总费用,还要将ArrayList里的重复元素去掉。

 

类图:

  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元。
每条通讯、短信信息均单独计费后累加,不是将所有信息累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。
错误处理:
输入数据中出现的不符合格式要求的行一律忽略。

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;

public class Main {
    public static void main(String[] args) throws ParseException {
        Scanner input = new Scanner(System.in);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        simpleDateFormat.setLenient(false);
        String str = input.nextLine();
        String[] string = str.split(" ");
        String[] strings = string[0].split("-");
        String r1 = "u-0791[0-9]{7,8} 0";
        String r2 = "u-1[0-9]{10} 1";
        String r3 = "t-0791[0-9]{7,8} [0-9]{10,12} [0-9]{4}\\.([1-9]|10|11|12)\\.([1-9]|[1-9][0-9]) ([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]) [0-9]{4}\\.([1-9]|10|11|12)\\.([1-9]|[1-9][0-9]) ([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])";
        String r4 = "t-0791[0-9]{7,8} 1[0-9]{10} [0-9]{3,4} [0-9]{4}\\.([1-9]|10|11|12)\\.([1-9]|[1-9][0-9]) ([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]) [0-9]{4}\\.([1-9]|10|11|12)\\.([1-9]|[1-9][0-9]) ([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])";
        String r5 = "t-1[0-9]{10} [0-9]{3,4} 1[0-9]{10} [0-9]{3,4} [0-9]{4}\\.([1-9]|10|11|12)\\.([1-9]|[1-9][0-9]) ([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]) [0-9]{4}\\.([1-9]|10|11|12)\\.([1-9]|[1-9][0-9]) ([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])";
        String r6 = "t-1[0-9]{10} [0-9]{3,4} [0-9]{10,12} [0-9]{4}\\.([1-9]|10|11|12)\\.([1-9]|[1-9][0-9]) ([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]) [0-9]{4}\\.([1-9]|10|11|12)\\.([1-9]|[1-9][0-9]) ([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])";
        String callnumber, anwsernumber, startTime, endTime, place, callPlace, anwserPlace;
        CallRecord callRecord;
        Date startDate, endDate;
        ArrayList<User> users = new ArrayList<User>();
        int flag;
        
        while(!str.equals("end")) {
            flag = 0;
            if(!str.equals("")) {
                ParsePosition pos1 = new ParsePosition(0);
                ParsePosition pos2 = new ParsePosition(0);
                if(str.matches(r1)||str.matches(r2)) {
                    callnumber = strings[1];
                    User user = new User(callnumber);
                    if(!users.contains(user)) {
                        users.add(user);
                    }
                }
                else if(str.matches(r3)) {
                    callnumber = strings[1];
                    anwsernumber = string[1];
                    startTime = string[2] + " " + string[3];
                    endTime = string[4] + " " + string[5];
                    place = anwsernumber.substring(0, 4);
                    startDate = simpleDateFormat.parse(startTime, pos1);
                    endDate = simpleDateFormat.parse(endTime, pos2);
                    callRecord = new CallRecord(callnumber, anwsernumber, startDate, endDate);
                    
                    if(startDate!=null&&endDate!=null) {
                        for(int i=0;i<users.size();i++) {
                            if(users.get(i).getNumber().equals(callnumber)) {
                                if(place.equals("0791")) {
                                    users.get(i).getUserRecords().addcallingInCityRecords(callRecord);
                                }
                                else if(place.equals("0790")||place.equals("0792")||place.equals("0793")||place.equals("0794")||place.equals("0795")
                                        ||place.equals("0796")||place.equals("0797")||place.equals("0798")||place.equals("0799")||place.equals("0701")) {
                                    users.get(i).getUserRecords().addcallingInProvinceRecords(callRecord);
                                }
                                else {
                                    users.get(i).getUserRecords().addcallingInLandRecords(callRecord);
                                }
                                break;
                            }
                        }
                    }
                }
                else if(str.matches(r4)) {
                    callnumber = strings[1];
                    anwsernumber = string[1];
                    anwserPlace = string[2];
                    startTime = string[3] + " " + string[4];
                    endTime = string[5] + " " + string[6];
                    startDate = simpleDateFormat.parse(startTime, pos1);
                    endDate = simpleDateFormat.parse(endTime, pos2);
                    callRecord = new CallRecord(callnumber, anwsernumber, startDate, endDate);
                    
                    if(startDate!=null&&endDate!=null) {
                        for(int i=0;i<users.size();i++) {
                            if(users.get(i).getNumber().equals(callnumber)) {
                                if(anwserPlace.equals("0791")) {
                                    users.get(i).getUserRecords().addcallingInCityRecords(callRecord);
                                }
                                else if(anwserPlace.equals("0790")||anwserPlace.equals("0792")||anwserPlace.equals("0793")||anwserPlace.equals("0794")||anwserPlace.equals("0795")
                                        ||anwsernumber.equals("0796")||anwserPlace.equals("0797")||anwserPlace.equals("0798")||anwserPlace.equals("0799")||anwserPlace.equals("0701")) {
                                    users.get(i).getUserRecords().addcallingInProvinceRecords(callRecord);
                                }
                                else {
                                    users.get(i).getUserRecords().addcallingInLandRecords(callRecord);
                                    for(int j=0;j<users.size();j++) {
                                        if(users.get(j).getNumber().equals(anwsernumber)) {
                                            users.get(j).getUserRecords().addPhoneProvinceAnwseringInLandRecords(callRecord);
                                        }
                                    }
                                }
                                flag = 1;
                                break;
                            }
                        }
                        if(flag==0) {
                            for(int j=0;j<users.size();j++) {
                                if(users.get(j).getNumber().equals(anwsernumber)) {
                                    if(anwserPlace.equals("0791")||anwserPlace.equals("0790")||anwserPlace.equals("0792")||anwserPlace.equals("0793")||anwserPlace.equals("0794")||anwserPlace.equals("0795")
                                                ||anwsernumber.equals("0796")||anwserPlace.equals("0797")||anwserPlace.equals("0798")||anwserPlace.equals("0799")||anwserPlace.equals("0701")) {
                                        break;
                                    }
                                    else {
                                        users.get(j).getUserRecords().addPhoneProvinceAnwseringInLandRecords(callRecord);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                else if(str.matches(r5)) {
                    callnumber = strings[1];
                    callPlace = string[1];
                    anwsernumber = string[2];
                    anwserPlace = string[3];
                    startTime = string[4] + " " + string[5];
                    endTime = string[6] + " " + string[7];
                    startDate = simpleDateFormat.parse(startTime, pos1);
                    endDate = simpleDateFormat.parse(endTime, pos2);
                    callRecord = new CallRecord(callnumber, anwsernumber, startDate, endDate);
                    
                    if(startDate!=null&&endDate!=null) {
                        for(int i=0;i<users.size();i++) {
                            if(users.get(i).getNumber().equals(callnumber)) {
                                if(callPlace.equals("0791")&&anwserPlace.equals("0791")) {
                                    users.get(i).getUserRecords().addPhoneCityCallingInCityRecords(callRecord);
                                }
                                else if(callPlace.equals("0791")&&(anwserPlace.equals("0790")||anwserPlace.equals("0792")||anwserPlace.equals("0793")||anwserPlace.equals("0794")||anwserPlace.equals("0795")
                                        ||anwserPlace.equals("0796")||anwserPlace.equals("0797")||anwserPlace.equals("0798")||anwserPlace.equals("0799")||anwserPlace.equals("0701"))) {
                                    users.get(i).getUserRecords().addPhoneCityCallingInProvinceRecords(callRecord);
                                }
                                else if(callPlace.equals("0791")) {
                                    users.get(i).getUserRecords().addPhoneCityCallingInLandRecords(callRecord);
                                    for(int j=0;j<users.size();j++) {
                                        if(users.get(j).getNumber().equals(anwsernumber)) {
                                            users.get(j).getUserRecords().addPhoneProvinceAnwseringInLandRecords(callRecord);
                                            break;
                                        }
                                    }
                                }
                                else if((callPlace.equals("0790")||callPlace.equals("0792")||callPlace.equals("0793")||callPlace.equals("0794")||callPlace.equals("0795")
                                                ||callPlace.equals("0796")||callPlace.equals("0797")||callPlace.equals("0798")||callPlace.equals("0799")||callPlace.equals("0701"))
                                        &&(anwserPlace.equals("0791")||anwserPlace.equals("0790")||anwserPlace.equals("0792")||anwserPlace.equals("0793")||anwserPlace.equals("0794")||anwserPlace.equals("0795")
                                                ||anwserPlace.equals("0796")||anwserPlace.equals("0797")||anwserPlace.equals("0798")||anwserPlace.equals("0799")||anwserPlace.equals("0701"))) {
                                    users.get(i).getUserRecords().addPhoneProvinceCallingInLandRecords(callRecord);
                                }
                                else if(callPlace.equals("0790")||callPlace.equals("0792")||callPlace.equals("0793")||callPlace.equals("0794")||callPlace.equals("0795")
                                                ||callPlace.equals("0796")||callPlace.equals("0797")||callPlace.equals("0798")||callPlace.equals("0799")||callPlace.equals("0701")) {
                                    users.get(i).getUserRecords().addPhoneProvinceCallingInLandRecords(callRecord);
                                    for(int j=0;j<users.size();j++) {
                                        if(users.get(j).getNumber().equals(anwsernumber)) {
                                            users.get(j).getUserRecords().addPhoneProvinceAnwseringInLandRecords(callRecord);
                                            break;
                                        }
                                    }
                                }
                                else if(anwserPlace.equals("0791")||anwserPlace.equals("0790")||anwserPlace.equals("0792")||anwserPlace.equals("0793")||anwserPlace.equals("0794")||anwserPlace.equals("0795")
                                                ||anwserPlace.equals("0796")||anwserPlace.equals("0797")||anwserPlace.equals("0798")||anwserPlace.equals("0799")||anwserPlace.equals("0701")) {
                                    users.get(i).getUserRecords().addPhoneLandCallingInLandRecords(callRecord);
                                }
                                else {
                                    users.get(i).getUserRecords().addPhoneLandCallingInLandRecords(callRecord);
                                    for(int j=0;j<users.size();j++) {
                                        if(users.get(j).getNumber().equals(anwsernumber)) {
                                            users.get(j).getUserRecords().addPhoneProvinceAnwseringInLandRecords(callRecord);
                                            break;
                                        }
                                    }
                                }
                                flag = 1;
                                break;
                            }
                        }
                        if(flag==0) {
                            for(int j=0;j<users.size();j++) {
                                if(users.get(j).getNumber().equals(anwsernumber)) {
                                    if(anwserPlace.equals("0791")||anwserPlace.equals("0790")||anwserPlace.equals("0792")||anwserPlace.equals("0793")||anwserPlace.equals("0794")||anwserPlace.equals("0795")
                                                ||anwserPlace.equals("0796")||anwserPlace.equals("0797")||anwserPlace.equals("0798")||anwserPlace.equals("0799")||anwserPlace.equals("0701")) {
                                        break;
                                    }
                                    else {
                                        users.get(j).getUserRecords().addPhoneProvinceAnwseringInLandRecords(callRecord);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                else if(str.matches(r6)) {
                    callnumber = strings[1];
                    callPlace = string[1];
                    anwsernumber = string[2];
                    anwserPlace = anwsernumber.substring(0, 4);
                    startTime = string[3] + " " + string[4];
                    endTime = string[5] + " " + string[6];
                    startDate = simpleDateFormat.parse(startTime, pos1);
                    endDate = simpleDateFormat.parse(endTime, pos2);
                    callRecord = new CallRecord(callnumber, anwsernumber, startDate, endDate);
                    
                    if(startDate!=null&&endDate!=null) {
                        for(int i=0;i<users.size();i++) {
                            if(users.get(i).getNumber().equals(callnumber)) {
                                if(callPlace.equals("0791")) {
                                    if(anwserPlace.equals("0791")) {
                                        users.get(i).getUserRecords().addPhoneCityCallingInCityRecords(callRecord);
                                    }
                                    else if(anwserPlace.equals("0790")||anwserPlace.equals("0792")||anwserPlace.equals("0793")||anwserPlace.equals("0794")||anwserPlace.equals("0795")
                                        ||anwserPlace.equals("0796")||anwserPlace.equals("0797")||anwserPlace.equals("0798")||anwserPlace.equals("0799")||anwserPlace.equals("0701")) {
                                        users.get(i).getUserRecords().addPhoneCityCallingInProvinceRecords(callRecord);
                                    }
                                    else {
                                        users.get(i).getUserRecords().addPhoneCityCallingInLandRecords(callRecord);
                                    }
                                }
                                else if(callPlace.equals("0790")||callPlace.equals("0792")||callPlace.equals("0793")||callPlace.equals("0794")||callPlace.equals("0795")
                                        ||callPlace.equals("0796")||callPlace.equals("0797")||callPlace.equals("0798")||callPlace.equals("0799")||callPlace.equals("0701")) {
                                    users.get(i).getUserRecords().addPhoneProvinceCallingInLandRecords(callRecord);
                                }
                                else {
                                    users.get(i).getUserRecords().addPhoneLandCallingInLandRecords(callRecord);
                                }
                                break;
                            }
                        }
                    }
                }
            }
            str = input.nextLine();
            string = str.split(" ");
            strings = string[0].split("-");
        }
        Collections.sort(users, new Comparator<User>() {
            @Override
            public int compare(User user1, User user2) {
                return user1.getNumber().compareTo(user2.getNumber());
            }
        });
        for(int j=0;j<users.size();j++) {
            if(users.get(j).getNumber().charAt(0)=='0') {
                System.out.println(users.get(j).getNumber() + " " + String.format("%.1f", users.get(j).calCost()) + " " + String.format("%.1f", users.get(j).calBalance()));
            }
            else {
                System.out.println(users.get(j).getNumber() + " " + String.format("%.1f", users.get(j).phoneCost()) + " " + String.format("%.1f", users.get(j).phoneBalance()));
            }
        }
    }
}
class User {
    private UserRecords userRecords = new UserRecords();
    private double balance = 100;
    private ChargeMode landLinePhonechargeMode = new LandLinePoneCharge();
    private ChargeMode phoneChargeMode = new PhoneCharge();
    private String number;

    public User() {
        
        super();
    }

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

    public UserRecords getUserRecords() {
        return userRecords;
    }

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

    public double getBalance() {
        return balance;
    }
    
    public void setBalance(double balance) {
        this.balance = balance;
    }
    
    public ChargeMode getLandLinePhonechargeMode() {
        return landLinePhonechargeMode;
    }

    public void setLandLinePhonechargeMode(ChargeMode landLinePhonechargeMode) {
        this.landLinePhonechargeMode = landLinePhonechargeMode;
    }

    public ChargeMode getPhoneChargeMode() {
        return phoneChargeMode;
    }

    public void setPhoneChargeMode(ChargeMode phoneChargeMode) {
        this.phoneChargeMode = phoneChargeMode;
    }

    public String getNumber() {
        return number;
    }
    
    public void setNumber(String number) {
        this.number = number;
    }
    
    public double calBalance() {
        return getBalance() - calCost() - landLinePhonechargeMode.getMonthlyRent();
    }
    
    public double calCost() {
        return landLinePhonechargeMode.calCost(userRecords);
    }
    
    public double phoneBalance() {
        return getBalance() - phoneCost() - phoneChargeMode.getMonthlyRent();
    }
    
    public double phoneCost() {
        return phoneChargeMode.calCost(userRecords);
    }
    
    @Override
    public boolean equals(Object o) {
        if (o == null) {
          return false;
        }
        if (o instanceof User) {
          User user = (User) o;
          return  number.equals(user.getNumber());
        }
        return false;
      }
}
abstract class ChargeMode {
    private ArrayList<CallChargeRule> callChargeRules;
    
    public ArrayList<CallChargeRule> getCallChargeRules() {
        return callChargeRules;
    }

    public void setCallChargeRules(ArrayList<CallChargeRule> callChargeRules) {
        this.callChargeRules = callChargeRules;
    }

    public abstract double calCost(UserRecords userRecords);

    public abstract double getMonthlyRent();
}
class LandLinePoneCharge extends ChargeMode {
    private double monthlyRent = 20;
    private CallChargeRule landPhoneInCityRule = new LandPhoneInCityRule();
    private CallChargeRule landPhoneInProvinceRule = new LandPhoneInProvinceRule();
    private CallChargeRule landPhoneInLandRule = new LandPhoneInLandRule();
    
    public LandLinePoneCharge() {
        super();
    }

    @Override
    public double calCost(UserRecords userRecords) {
        double cost = 0;
        cost += landPhoneInCityRule.calCost(userRecords.getCallingInCityRecords())
                + landPhoneInProvinceRule.calCost(userRecords.getCallingInProvinceRecords())
                + landPhoneInLandRule.calCost(userRecords.getCallingInLandRecords());
        return cost;
    }

    @Override
    public double getMonthlyRent() {
        return monthlyRent;
    }
    
}
class PhoneCharge extends ChargeMode {
    private double monthlyRent = 15;
    private CallChargeRule phoneCityCallingInCityRule = new PhoneCityCallingInCityRule();
    private CallChargeRule phoneCityCallingInProvinceRule = new PhoneCityCallingInProvinceRule();
    private CallChargeRule phoneCityCallingInLandRule = new PhoneCityCallingInLandRule();
    private CallChargeRule phoneProvinceCallingInLandRule = new PhoneProvinceCallingInLandRule();
    private CallChargeRule phoneProvinceAnwseringInLandRule = new PhoneProvinceAnwseringInLandRule();
    private CallChargeRule phoneLandCallingInLandRule = new PhoneLandCallingInLandRule();
    
    public PhoneCharge() {
        super();
    }

    @Override
    public double calCost(UserRecords userRecords) {
        return phoneCityCallingInCityRule.calCost(userRecords.getPhoneCityCallingInCityRecords()) + phoneCityCallingInProvinceRule.calCost(userRecords.getPhoneCityCallingInProvinceRecords())
         + phoneCityCallingInLandRule.calCost(userRecords.getPhoneCityCallingInLandRecords()) + phoneProvinceCallingInLandRule.calCost(userRecords.getPhoneProvinceCallingInLandRecords())
          + phoneProvinceAnwseringInLandRule.calCost(userRecords.getPhoneProvinceAnwseringInLandRecords()) + phoneLandCallingInLandRule.calCost(userRecords.getPhoneLandCallingInLandRecords());
    }

    @Override
    public double getMonthlyRent() {
        return monthlyRent;
    }
    
    
}
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> phoneCityCallingInCityRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> phoneCityCallingInProvinceRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> phoneCityCallingInLandRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> phoneProvinceCallingInLandRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> phoneProvinceAnwseringInLandRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> phoneLandCallingInLandRecords = new ArrayList<CallRecord>();
    
    public UserRecords() {
        super();
    }

    public ArrayList<CallRecord> getPhoneCityCallingInCityRecords() {
        return phoneCityCallingInCityRecords;
    }

    public ArrayList<CallRecord> getPhoneCityCallingInProvinceRecords() {
        return phoneCityCallingInProvinceRecords;
    }

    public ArrayList<CallRecord> getPhoneCityCallingInLandRecords() {
        return phoneCityCallingInLandRecords;
    }

    public ArrayList<CallRecord> getPhoneProvinceCallingInLandRecords() {
        return phoneProvinceCallingInLandRecords;
    }

    public ArrayList<CallRecord> getPhoneProvinceAnwseringInLandRecords() {
        return phoneProvinceAnwseringInLandRecords;
    }

    public ArrayList<CallRecord> getPhoneLandCallingInLandRecords() {
        return phoneLandCallingInLandRecords;
    }

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

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

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

    public void addcallingInCityRecords(CallRecord callRecord) {
        getCallingInCityRecords().add(callRecord);
    }
    
    public void addcallingInProvinceRecords(CallRecord callRecord) {
        getCallingInProvinceRecords().add(callRecord);
    }
    
    public void addcallingInLandRecords(CallRecord callRecord) {
        getCallingInLandRecords().add(callRecord);
    }
    
    public void addPhoneCityCallingInCityRecords(CallRecord callRecord) {
        getPhoneCityCallingInCityRecords().add(callRecord);
    }
    
    public void addPhoneCityCallingInProvinceRecords(CallRecord callRecord) {
        getPhoneCityCallingInProvinceRecords().add(callRecord);
    }
    
    public void addPhoneCityCallingInLandRecords(CallRecord callRecord) {
        getPhoneCityCallingInLandRecords().add(callRecord);
    }
    
    public void addPhoneProvinceCallingInLandRecords(CallRecord callRecord) {
        getPhoneProvinceCallingInLandRecords().add(callRecord);
    }
    
    public void addPhoneProvinceAnwseringInLandRecords(CallRecord callRecord) {
        getPhoneProvinceAnwseringInLandRecords().add(callRecord);
    }
    
    public void addPhoneLandCallingInLandRecords(CallRecord callRecord) {
        getPhoneLandCallingInLandRecords().add(callRecord);
    }
}
abstract class CommunicationRecord {
    private String callingNumber;
    private String answerNumber;
    
    public CommunicationRecord(String callingNumber, String answerNumber) {
        super();
        this.callingNumber = callingNumber;
        this.answerNumber = 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;

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

    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 double getTime() {
        long time;
        time = (endTime.getTime() - startTime.getTime()) / 1000;
        if(time%60==0) {
            time /= 60;
        }
        else {
            time = time / 60 + 1;
        }
        return (double)time;
    }

}
abstract class ChargeRule{
    
}
abstract class CallChargeRule extends ChargeRule {
    
    public abstract double calCost(ArrayList<CallRecord> callRecords);
    
}
class LandPhoneInCityRule extends CallChargeRule {    //座机市内拨打电话

    public LandPhoneInCityRule() {
        super();
    }

    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double city = 0;
        for(int i=0;i<callRecords.size();i++) {
            city += callRecords.get(i).getTime() * 0.1;
        }
        return city;
    }
    
}
class LandPhoneInProvinceRule extends CallChargeRule {    //座机省内长途

    public LandPhoneInProvinceRule() {
        super();
    }

    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double province = 0;
        for(int i=0;i<callRecords.size();i++) {
            province += callRecords.get(i).getTime() * 0.3;
        }
        return province;
    }
    
}
class LandPhoneInLandRule extends CallChargeRule {    //座机国内长途

    public LandPhoneInLandRule() {
        super();
    }

    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double land = 0;
        for(int i=0;i<callRecords.size();i++) {
            land += callRecords.get(i).getTime() * 0.6;
        }
        return land;
    }
    
}
class PhoneCityCallingInCityRule extends CallChargeRule {    //手机市内拨打市内电话

    public PhoneCityCallingInCityRule() {
        super();
    }

    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double cost = 0;
        for(int i=0;i<callRecords.size();i++) {
            cost += callRecords.get(i).getTime() * 0.1;
        }
        return cost;
    }
    
}
class PhoneCityCallingInProvinceRule extends CallChargeRule {    //手机市内拨打省内电话

    public PhoneCityCallingInProvinceRule() {
        super();
    }

    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double cost = 0;
        for(int i=0;i<callRecords.size();i++) {
            cost += callRecords.get(i).getTime() * 0.2;
        }
        return cost;
    }
    
}
class PhoneCityCallingInLandRule extends CallChargeRule {    //手机市内拨打省外电话

    public PhoneCityCallingInLandRule() {
        super();
    }

    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double cost = 0;
        for(int i=0;i<callRecords.size();i++) {
            cost += callRecords.get(i).getTime() * 0.3;
        }
        return cost;
    }
    
}
class PhoneProvinceCallingInLandRule extends CallChargeRule {    //手机省内漫游打电话

    public PhoneProvinceCallingInLandRule() {
        super();
    }

    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double cost = 0;
        for(int i=0;i<callRecords.size();i++) {
            cost += callRecords.get(i).getTime() * 0.3;
        }
        return cost;
    }
    
}
class PhoneProvinceAnwseringInLandRule extends CallChargeRule {    //手机省外漫游接听

    public PhoneProvinceAnwseringInLandRule() {
        super();
    }

    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double cost = 0;
        for(int i=0;i<callRecords.size();i++) {
            cost += callRecords.get(i).getTime() * 0.3;
        }
        return cost;
    }
    
}
class PhoneLandCallingInLandRule extends CallChargeRule {    //手机省外漫游拨打

    public PhoneLandCallingInLandRule() {
        super();
    }

    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double cost = 0;
        for(int i=0;i<callRecords.size();i++) {
            cost += callRecords.get(i).getTime() * 0.6;
        }
        return cost;
    }
    
}

 

总结:这次电信计费相对于上次来讲增加了手机用户计费方式,代码中需要增加chargeMode的子类PhoneMode用来手机计费,同时需要增加CallChargeMode的子类用来代表手机的计费,增加与手机用户有关的正则表达式,其他的地方都与上次的代码差不多。

 

类图:

 

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。

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

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;

public class Main {
    public static void main(String[] args) throws ParseException {
        Scanner input = new Scanner(System.in);
        String str = input.nextLine();
        String[] string = str.split(" ");
        String[] strings = string[0].split("-");
        String r7 = "u-1[3-9]\\d{9} 3";
        String r8 = "m-1[3-9]\\d{9} 1[3-9]\\d{9} [a-z|0-9| |,|.]++";
        String callnumber, anwsernumber, startTime, endTime, place, callPlace, anwserPlace, message;
        Date startDate, endDate;
        ArrayList<User> users = new ArrayList<User>();
        int flag;
        
        while(!str.equals("end")) {
            flag = 0;
            if(!str.equals("")) {
                if(str.matches(r7)) {
                    callnumber = strings[1];
                    User user = new User(callnumber);
                    if(!users.contains(user)) {
                        users.add(user);
                    }
                }
                else if(str.matches(r8)) {
                    callnumber = strings[1];
                    anwsernumber = string[1];
                    message = str.substring(26);
                    MessageRecord messageRecord = new MessageRecord(message);
                    for(int i=0;i<users.size();i++) {
                        if(users.get(i).getNumber().equals(callnumber)) {
                            users.get(i).getUserRecords().addReceiveMessageRecords(messageRecord);
                            break;
                        }
                    }
                }
            }
            str = input.nextLine();
            string = str.split(" ");
            strings = string[0].split("-");
        }
        Collections.sort(users, new Comparator<User>() {
            @Override
            public int compare(User user1, User user2) {
                return user1.getNumber().compareTo(user2.getNumber());
            }
        });
        for(int j=0;j<users.size();j++) {
            System.out.println(users.get(j).getNumber() + " " + String.format("%.1f", users.get(j).phoneCost()) + " " + String.format("%.1f", users.get(j).phoneBalance()));
        }
    }
}
class User {
    private UserRecords userRecords = new UserRecords();
    private double balance = 100;
    private ChargeMode phoneChargeMode = new PhoneCharge();
    private String number;

    public User() {
        
        super();
    }

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

    public UserRecords getUserRecords() {
        return userRecords;
    }

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

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

    public ChargeMode getPhoneChargeMode() {
        return phoneChargeMode;
    }

    public void setPhoneChargeMode(ChargeMode phoneChargeMode) {
        this.phoneChargeMode = phoneChargeMode;
    }

    public String getNumber() {
        return number;
    }
    
    public void setNumber(String number) {
        this.number = number;
    }
    
    public double phoneBalance() {
        return getBalance() - phoneCost() - phoneChargeMode.getMonthlyRent();
    }
    
    public double phoneCost() {
        return phoneChargeMode.calCost(userRecords);
    }
    
    @Override
    public boolean equals(Object o) {
        if (o == null) {
          return false;
        }
        if (o instanceof User) {
          User user = (User) o;
          return  number.equals(user.getNumber());
        }
        return false;
      }
}
abstract class ChargeMode {

    public abstract double calCost(UserRecords userRecords);

    public abstract double getMonthlyRent();
}
class PhoneCharge extends ChargeMode {
    private double monthlyRent = 0;
    private MessageChargaRule messageChargaRule = new SendMessageRule();
    
    public PhoneCharge() {
        super();
    }

    @Override
    public double calCost(UserRecords userRecords) {
        return messageChargaRule.calCost(userRecords.getReceivemessageRecords());
    }

    @Override
    public double getMonthlyRent() {
        return monthlyRent;
    }
    
    
}
class UserRecords {
    private ArrayList<MessageRecord> receivemessageRecords = new ArrayList<MessageRecord>();
    
    public UserRecords() {
        super();
    }

    public ArrayList<MessageRecord> getReceivemessageRecords() {
        return receivemessageRecords;
    }
    
    public void addReceiveMessageRecords(MessageRecord messageRecord) {
        getReceivemessageRecords().add(messageRecord);
    }
}
abstract class CommunicationRecord {
    private String callingNumber;
    private 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 MessageRecord extends CommunicationRecord {
    String message;

    public String getMessage() {
        return message;
    }

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

    public void setMessage(String message) {
        this.message = message;
    }
    
}
abstract class ChargeRule{
    
}
abstract class MessageChargaRule extends ChargeRule {
    public abstract double calCost(ArrayList<MessageRecord> messageRecords);
}
class SendMessageRule extends MessageChargaRule{

    public SendMessageRule() {
        super();
    }

    @Override
    public double calCost(ArrayList<MessageRecord> messageRecords) {
        double cost = 0;
        double length = 0;
        for(int i=0;i<messageRecords.size();i++) {
            if(messageRecords.get(i).getMessage().length()%10==0) {
                length += messageRecords.get(i).getMessage().length() / 10;
            }
            else {
                length += messageRecords.get(i).getMessage().length() / 10 + 1;
            }
        }
        for(int i=1;i<=length;i++) {
            if(i<=3) {
                cost += 0.1;
            }
            else if(i<=5) {
                cost += 0.2;
            }
            else {
                cost += 0.3;
            }
        }
        return cost;
    }
    
}

 

总结:这次迭代在原来的基础上又增加了手机用户的短信计费功能,在原来代码的基础上再增加MessageRecord类用来记录用户发送短信的条数,增加MessageChargeRule用来实现短信计费,增加有关短信输入的正则表达式。

 

类图:

 

 

 

 

 

三、采坑心得:

  1.正则表达式输入错误。

  2.数据进行处理时,当代码的复杂度过高时,每次新增功能时都要花很长的时间进行修改读取和判断格式错误上。

四、改进建议:

  1.多分几个函数,以此提高代码复用率,减少代码的复杂性。

  2.尝试使用父类,抽象类,对代码进行忧化。

  3.多用继承降低类与类之间的耦合性,提高代码的课扩展性。

五、总结:

  1.类和类之间具有共同特征,将这些共同特征提取出来,形成的就是抽象类。抽象类无法创建对象即无法实例化。

  2.抽象类是无法实例化的,无法创建对象的,所以抽象类是用来被子类继承的。

  3.抽象类中不一定有抽象方法,抽象方法必须出现在抽象类中。

标签:String,double,ArrayList,BLOG,计费,new,public
来源: https://www.cnblogs.com/xiarenzhuxin/p/16382597.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有