1. 程式人生 > 其它 >第三次Blog

第三次Blog

一、前言

  在最後三次作業主要是圍繞電信計費系統的作業。從一開始的座機計費,再到手機+座機計費,最後到簡訊計費。至於其它的題目,C~K的班級、閱讀程式,按照題目需求修改程式、商品內部類購物券、動物發聲模擬器這幾道題目難度都不大,只要認真讀題自己編碼即可解決。這三次作業針對手機、座機的簡訊計費等三個方式,環環遞進,相較於前面的多邊形計算來說,難度沒有那麼大,其中難點在於對於正則表示式的運用判斷,計費過程中的計算,以及輸出排序的問題。

二、設計與分析

1、電信計費系列1-座機計費

(1)題目:

實現一個簡單的電信計費程式:
假設南昌市電信分公司針對市內座機使用者採用的計費方式:
月租20元,接電話免費,市內撥打電話0.1元/分鐘,省內長途0.3元/分鐘,國內長途撥打0.6元/分鐘。不足一分鐘按一分鐘計。


南昌市的區號:0791,江西省內各地市區號包括:0790~0799以及0701。

輸入格式:

輸入資訊包括兩種型別
1、逐行輸入南昌市使用者開戶的資訊,每行一個使用者,
格式:u-號碼 計費型別 (計費型別包括:0-座機 1-手機實時計費 2-手機A套餐)
例如:u-079186300001 0
座機號碼除區號外由是7-8位數字組成。
本題只考慮計費型別0-座機計費,電信系列2、3題會逐步增加計費型別。
2、逐行輸入本月某些使用者的通訊資訊,通訊資訊格式:
座機呼叫座機:t-主叫號碼 接聽號碼 起始時間 結束時間
t-079186330022 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:11
以上四項內容之間以一個英文空格分隔,


時間必須符合"yyyy.MM.dd HH:mm:ss"格式。提示:使用SimpleDateFormat類。
以上兩類資訊,先輸入所有開戶資訊,再輸入所有通訊資訊,最後一行以“end”結束。
注意:
本題非法輸入只做格式非法的判斷,不做內容是否合理的判斷(時間除外,否則無法計算),比如:
1、輸入的所有通訊資訊均認為是同一個月的通訊資訊,不做日期是否在同一個月還是多個月的判定,直接將通訊費用累加,因此月租只計算一次。
2、記錄中如果同一電話號碼的多條通話記錄時間出現重合,這種情況也不做判斷,直接 計算每條記錄的費用並累加。
3、使用者區號不為南昌市的區號也作為正常使用者處理。

輸出格式:

根據輸入的詳細通訊資訊,計算所有已開戶的使用者

的當月費用(精確到小數點後2位,
單位元)。假設每個使用者初始餘額是100元。
每條通訊資訊單獨計費後累加,不是將所有時間累計後統一計費。
格式:號碼+英文空格符+總的話費+英文空格符+餘額
每個使用者一行,使用者之間按號碼字元從小到大排序。

錯誤處理:
輸入資料中出現的不符合格式要求的行一律忽略。

建議類圖:
參見圖1、2、3,可根據理解自行調整:

                                    圖1
圖1中User是使用者類,包括屬性:
userRecords (使用者記錄)、balance(餘額)、chargeMode(計費方式)、number(號碼)。

ChargeMode是計費方式的抽象類:
chargeRules是計費方式所包含的各種計費規則的集合,ChargeRule類的定義見圖3。
getMonthlyRent()方法用於返回月租(monthlyRent)。

UserRecords是使用者記錄類,儲存使用者各種通話、簡訊的記錄,    
各種計費規則將使用其中的部分或者全部記錄。
其屬性從上到下依次是:
市內撥打電話、省內(不含市內)撥打電話、省外撥打電話、
市內接聽電話、省內(不含市內)接聽電話、省外接聽電話的記錄
以及傳送簡訊、接收簡訊的記錄。
   
                                     圖2
圖2中CommunicationRecord是抽象的通訊記錄類:
包含callingNumber撥打號碼、answerNumber接聽號碼兩個屬性。
CallRecord(通話記錄)、MessageRecord(簡訊記錄)是它的子類。

CallRecord(通話記錄類)包含屬性:
通話的起始、結束時間以及
撥號地點的區號(callingAddressAreaCode)、接聽地點的區號(answerAddressAreaCode)。
區號用於記錄在哪個地點撥打和接聽的電話。座機無法移動,就是本機區號,如果是手機號,則會有差異。
   
                                        圖3
圖3是計費規則的相關類,這些類的核心方法是:
calCost(ArrayList<CallRecord> callRecords)。
該方法針根據輸入引數callRecords中的所有記錄計算某使用者的某一項費用;如市話費。
輸入引數callRecords的約束條件:必須是某一個使用者的符合計費規則要求的所有記錄。

LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三個類分別是
座機撥打市內、省內、省外電話的計費規則類,用於實現這三種情況的費用計算。    
(提示:可以從UserRecords類中獲取各種型別的callRecords)。
 

(2)測試樣例:

輸入樣例:

在這裡給出一組輸入。例如:

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

輸出樣例:

在這裡給出相應的輸出。例如:

079186300001 3.0 77.0

(3)我的程式碼:

電信計費系列1-座機計費
 import java.util.*;
import java.text.*;
public class Main {
    public static void main(String[] args) {
        Dealdata p = new Dealdata();
        p.deal();
    }
}
abstract class CallChargeRule extends ChargeRule {
    public abstract  double calCost(ArrayList<CallRecord> callrecords);
}
class CallRecord extends CommunicationRecord {
    private Date  stateTime;
    private Date  endTime;
    private String callingAddressAreaCode;
    private String answerAddressAreaCode;
    public CallRecord( String callingNumber,String answerNumber,Date stateTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) {
        super();
        setCallingNumber(callingNumber);
        setAnswerNumber(answerNumber);
        this.stateTime = stateTime;
        this.endTime = endTime;
        this.callingAddressAreaCode = callingAddressAreaCode;
        this.answerAddressAreaCode = answerAddressAreaCode;
    }
    public Date getStateTime() {
        return stateTime;
    }
    public void setStateTime(Date stateTime) {
        this.stateTime = stateTime;
    }
    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 ChargeMode {
    private ArrayList<ChargeRule> chargeRules = new ArrayList<ChargeRule>();
    public ArrayList<ChargeRule> getChargeRules() {
        return chargeRules;
    }
    public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
        this.chargeRules = chargeRules;
    }
    public abstract double calCost(UserRecords useRecords);
    public abstract double getMonthlyRent();
}
abstract class ChargeRule {
    public  abstract double calCost(ArrayList<CallRecord> callrecords);
}
abstract class CommunicationRecord {
    protected String callingNumber;
    protected String answerNumber;
    public String getCallingNumber() {
        return callingNumber;
    }
    public void setCallingNumber(String callingNumber) {
        this.callingNumber = callingNumber;
    }
    public String getAnswerNumber() {
        return answerNumber;
    }
    public void setAnswerNumber(String answerNumber) {
        this.answerNumber = answerNumber;
    }
}
class LandlinePhoneCharging extends ChargeMode {
    private double monthlyRent = 20;
    public LandlinePhoneCharging() {
        super();
        getChargeRules().add(new LandPhoneInCityRule());
        getChargeRules().add(new LandPhoneInProvinceRule());
        getChargeRules().add(new LandPhoneInLandRule());
    }
    @Override
    public double calCost(UserRecords useRecords) {
        double cost= 0;
        cost+= getChargeRules().get(0).calCost(useRecords.getCallingInCityRecords());
        cost+= getChargeRules().get(1).calCost(useRecords.getCallingInProvinceRecords());
        cost+= getChargeRules().get(2).calCost(useRecords.getCallingInLandRecords());
        return cost;
    }
    @Override
    public double getMonthlyRent() {
        return this.monthlyRent;
    }
}
class LandPhoneInCityRule extends CallChargeRule {
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double cost = 0;
        for(CallRecord c : callRecords) {
            cost+=Math.ceil((c.getEndTime().getTime()-c.getStateTime().getTime())/1000.0/60.0)*0.1;
        }
        return cost;
    }
}
class LandPhoneInLandRule extends CallChargeRule {
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double cost = 0;
        for(CallRecord c : callRecords) {
            cost+=Math.ceil((c.getEndTime().getTime()-c.getStateTime().getTime())/1000.0/60.0)*0.6;
        }
        return cost;
    }
}
class LandPhoneInProvinceRule extends CallChargeRule {
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        // TODO 自動生成的方法存根
        double cost = 0;
        for(CallRecord c : callRecords) {
            cost+=Math.ceil((c.getEndTime().getTime()-c.getStateTime().getTime())/1000.0/60.0)*0.3;
        }
        return cost;
    }
}
class MessageRecord extends CommunicationRecord {
    private String message;
    public MessageRecord(String callingNumber, String answerNumber, String message) {
        super();
        setCallingNumber(callingNumber);
        setAnswerNumber(answerNumber);
        this.message = message;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}
class Dealdata {
    public Dealdata() {
        super();
    }
    public  void deal() {
        TreeMap<String, User> users = new TreeMap<>();
        Scanner input = new Scanner(System.in);
        String regex1 ="[u]-[0-9]{11,12}\\s[0]";
        String regex2 ="t-(\\d){11,12}\\s(\\d){10,12}\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d";
        String s = input.nextLine();
        while(!s.equals("end")) {
            if(s.matches(regex1)) {
                String[] data = s.substring(2).split(" ");
                String number = data[0].substring(4);
                ChargeMode changeMode = new LandlinePhoneCharging();
                User user = new User(changeMode,number);
                users.put(number, user);
            }
            else if(s.matches(regex2)) {
                String[] data = s.substring(2).split(" ");
                String callingAddressAreaCode = data[0].substring(0,4);
                String answerAddressAreaCode = data[1].substring(0,4);
                String callingNumber = data[0].substring(4);
                String answerNumber = data[1].substring(4);
                String stateDate = data[2];
                String stateTime = data[3];
                String endDate  = data[4];
                String endTime = data[5];
                String state = stateDate+" "+stateTime;
                String end = endDate + " " + endTime;
                Date statedate = new Date();
                Date enddate = new Date();
                String strDateFormat = "yyyy.MM.dd HH:mm:ss";
                SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
                try {
                    statedate = sdf.parse(state);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                try {
                    enddate = sdf.parse(end);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                CallRecord callRecord = new CallRecord(callingNumber, answerNumber, statedate, enddate, callingAddressAreaCode, answerAddressAreaCode);
                long address = Long.parseLong(callRecord.getAnswerAddressAreaCode());
                if(users.get(callingNumber) != null){
                    if(address==791) {
                        users.get(callingNumber).getUserRecords().addCallingInCityRecords(callRecord);
                    }
                    else if(address==701||(address>= 790 &&address<=799&&address!= 791)) {
                        users.get(callingNumber).getUserRecords().addCallingInProvinceRecords(callRecord);
                    }
                    else {
                        users.get(callingNumber).getUserRecords().addCallingInLandRecords(callRecord);
                    }
                }
            }
            s = input.nextLine();
        }
        for (User user: users.values()) {
            System.out.println("0791"+user.getNumber()+" "+Math.round(user.calCost()*100)/100.0+" "+Math.round(user.calBanlance()*100)/100.0);
        }
    }
}
class User {
    private UserRecords userRecords = new UserRecords();// 使用者記錄
    private double banlance = 100; // 餘額
    private ChargeMode changeMode; // 計費方式
    private String number; // 電話號碼
    public User(ChargeMode changeMode, String number) {
        super();
        this.changeMode = changeMode;
        this.number = number;
    }
    public UserRecords getUserRecords() {
        return userRecords;
    }
    public void setUserRecords(UserRecords userRecords) {
        this.userRecords = userRecords;
    }
    public double getBanlance() {
        return banlance;
    }
    public void setBanlance(double banlance) {
        this.banlance = banlance;
    }
    public ChargeMode getChangeMode() {
        return changeMode;
    }
    public void setChangeMode(ChargeMode changeMode) {
        this.changeMode = changeMode;
    }
    public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
        this.number = number;
    }
    public double calBanlance() {
        return this.banlance-changeMode.calCost(userRecords)-changeMode.getMonthlyRent();
    }
    public double calCost() {
        return changeMode.calCost(userRecords);
    }
}
class UserRecords {
    public UserRecords() {
        super();
    }
    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 ArrayList<CallRecord> getCallingInCityRecords() {
        return callingInCityRecords;
    }
    public ArrayList<CallRecord> getCallingInProvinceRecords() {
        return callingInProvinceRecords;
    }
    public ArrayList<CallRecord> getCallingInLandRecords() {
        return callingInLandRecords;
    }
    public ArrayList<CallRecord> getAnswerInCityRecords() {
        return answerInCityRecords;
    }
    public ArrayList<CallRecord> getAnswerInProvinceRecords() {
        return answerInProvinceRecords;
    }
    public ArrayList<CallRecord> getAnswerInLandRecords() {
        return answerInLandRecords;
    }
    public ArrayList<MessageRecord> getSendMessageRecords() {
        return sendMessageRecords;
    }
    public ArrayList<MessageRecord> getReceiveMessageRecords() {
        return receiveMessageRecords;
    }
    public void addCallingInCityRecords(CallRecord callRecord) {
        callingInCityRecords.add(callRecord);
    }
    public void addCallingInLandRecords(CallRecord callRecord) {
        callingInLandRecords.add(callRecord);
    }
    public void addCallingInProvinceRecords(CallRecord callRecord) {
        callingInProvinceRecords.add(callRecord);
    }
    public void addAnswerInCityRecords(CallRecord callRecord) {
        answerInCityRecords.add(callRecord);
    }
    public void addAnswerInProvinceRecords(CallRecord callRecord) {
        answerInProvinceRecords.add(callRecord);
    }
    public void addAnswerInLandRecords(CallRecord callRecord) {
        answerInLandRecords.add(callRecord);
    }
    public void addSendMessageRecords(MessageRecord messageRecord) {
        sendMessageRecords.add(messageRecord);
    }
    public void addReceiveMessageRecords(MessageRecord messageRecord) {
        receiveMessageRecords.add(messageRecord);
    }
}

(4)類圖及SourceMonitor分析

(5)我的分析:

①分析類圖和SourceMonitor圖:

  我的結構基本依據題目提供的參考類圖進行設計,用User類來儲存使用者資訊,UserRecords儲存通話訊息的記錄。我自己寫了inputDeal類來處理輸入的資料,用以將輸入的資訊分類儲存在處理時間中,我們需要使用SimpleDateFormat類,先把日期化成標準型性,再用其固定的方法來計算時間間隔。這次程式碼主要是根據老師所給的類圖來進行填充和編寫,總的來說程式碼量並不是很大,而且複用程度較高,總體的圈複雜度也較為客觀,整個的程式碼質量還算是可以的。

②小結:

  本題作為電信計費系列的開端,僅加入了座機開戶和座機互通,計費方式單一也不是很難,主要難度在於結構的建立,要理清每個類的關係,分清父類子類,建立結構容器,按照類圖將類與類連結起來。最重要的第一步就是要弄清楚如何去處理輸入的資料,區分輸入的資料型別,以及要判斷,輸入的資料是否符合格式。

2、電信計費系列2-手機+座機計費

(1)題目:

實現南昌市電信分公司的計費程式,假設該公司針對手機和座機使用者分別採取了兩種計費方案,分別如下:
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元。
每條通訊、簡訊資訊均單獨計費後累加,不是將所有資訊累計後統一計費。
格式:號碼+英文空格符+總的話費+英文空格符+餘額
每個使用者一行,使用者之間按號碼字元從小到大排序。
錯誤處理:
輸入資料中出現的不符合格式要求的行一律忽略。

(2)測試樣例:

輸入樣例:

在這裡給出一組輸入。例如:

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

(3)我的程式碼:

電信計費系列2-手機+座機計費
import java.util.*;
import java.text.*;

public class Main {

    public static void main(String[] args) {
        ArrayList<User> users = new ArrayList<User>();
        InputDeal inputDeal = new InputDeal();

        Scanner input = new Scanner(System.in);

        String s = input.nextLine();
        while(!s.equals("end")) {
            if(inputDeal.checkType(s)==1) {
                inputDeal.addUser(users, s);
            }
            if(inputDeal.checkType(s)==2) {
                inputDeal.addRecord(users, s);
            }
            s = input.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 user: users) {
            System.out.println(user.getNumber()+" "+Math.round(user.calCost()*100)/100.0+" "+Math.round(user.calBanlance()*100)/100.0);
        }
        input.close();
    }
}
abstract class CallChargeRule extends ChargeRule {

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

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

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

        char type = s[0].charAt(0);
        s[0] = s[0].substring(2);
        String sd = null, st = null, ed = null, et = null;

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

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

    public 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 callingNumber,String answerNumber,Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) {
        super();
        setCallingNumber(callingNumber);
        setAnswerNumber(answerNumber);
        this.startTime = startTime;
        this.endTime = endTime;
        this.callingAddressAreaCode = callingAddressAreaCode;
        this.answerAddressAreaCode = answerAddressAreaCode;
    }
    public Date getStartTime() {
        return startTime;
    }
    public void setStartTime(Date stateTime) {
        this.startTime = stateTime;
    }
    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 ChargeMode {
    private ArrayList<ChargeRule> chargeRules = new ArrayList<ChargeRule>();
    public ArrayList<ChargeRule> getChargeRules() {
        return chargeRules;
    }
    public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
        this.chargeRules = chargeRules;
    }
    public abstract double calCost(UserRecords useRecords);
    public abstract double getMonthlyRent();
}
abstract class ChargeRule {
    public  abstract double calCost(UserRecords userRecords);
}
abstract class CommunicationRecord {
    protected String callingNumber;
    protected String answerNumber;
    public String getCallingNumber() {
        return callingNumber;
    }
    public void setCallingNumber(String callingNumber) {
        this.callingNumber = callingNumber;
    }
    public String getAnswerNumber() {
        return answerNumber;
    }
    public void setAnswerNumber(String answerNumber) {
        this.answerNumber = answerNumber;
    }
}
class InputDeal {

    //使用者:1  通話記錄:2 基本格式錯誤:0
    public int checkType(String input) {
        if (input.matches("[u]-0791[0-9]{7,8}\\s[0]") || input.matches("[u]-1[0-9]{10}\\s[1]")) {
            return 1;
        }
        else if (input.matches("(([t]-0791[0-9]{7,8}\\s" + "0[0-9]{9,11}\\s)|"
                + "([t]-0791[0-9]{7,8}\\s" + "1[0-9]{10}\\s" + "0[0-9]{2,3}\\s)|"
                + "([t]-1[0-9]{10}\\s" + "0[0-9]{2,3}\\s" + "0[0-9]{9,11}\\s)|"
                + "([t]-1[0-9]{10}\\s" + "0[0-9]{2,3}\\s" + "1[0-9]{10}\\s" + "0[0-9]{2,3}\\s))"
                + "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.(((0?[13578]|1[02])\\.(0?"
                + "[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((("
                + "[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))"
                + "\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\\s"
                + "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.("
                + "[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((("
                + "[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))"
                + "\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])")) {
            return 2;
        }
        return 0;
    }
    public void addUser(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 addRecord(ArrayList<User> users, String input) {
        String[] inputs = input.split(" ");
        User call = null, answer = 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)) {
                    call = i;
                }
                if (i.getNumber().equals(in)) {
                    answer = i;
                }
                if (call != null && answer != null) {
                    break;
                }
            }
            if (call != null) {
                if (callrecord.getCallType().matches("^1[1-3]$")) {
                    call.getUserRecords().addCallingInCityRecords(callrecord);
                }
                else if (callrecord.getCallType().matches("^2[1-3]$")) {
                    call.getUserRecords().addCallingInProvinceRecords(callrecord);
                }
                else {
                    call.getUserRecords().addCallingInLandRecords(callrecord);
                }
            }
            if (answer != null) {
                if (callrecord.getCallType().matches("^[1-3]1$")) {
                    answer.getUserRecords().addAnswerInCityRecords(callrecord);
                }
                else if (callrecord.getCallType().matches("^[1-3]2$")) {
                    answer.getUserRecords().addAnswerInProvinceRecords(callrecord);
                }
                else {
                    answer.getUserRecords().addAnswerInLandRecords(callrecord);
                }
            }
        }
        else if (input.charAt(0) == 'm') {

        }
    }
}
class LandlinePhoneCharging extends ChargeMode {

    private double monthlyRent = 20;

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

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

    @Override
    public double getMonthlyRent() {
        return monthlyRent;
    }
}
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 MessageRecord extends CommunicationRecord {
    private String message;
    public MessageRecord(String callingNumber, String answerNumber, String message) {
        super();
        setCallingNumber(callingNumber);
        setAnswerNumber(answerNumber);
        this.message = message;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}
class MobilePhoneCharging extends ChargeMode{
    private double monthlyRent = 15;

    public MobilePhoneCharging() {
        super();
        getChargeRules().add(new MobilePhoneInCityRule());
        getChargeRules().add(new MobilePhoneInProvinceRule());
        getChargeRules().add(new MobilePhoneInLandRule());
    }

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

    @Override
    public double getMonthlyRent() {
        return monthlyRent;
    }
}
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 User {
    private UserRecords userRecords = new UserRecords();// 使用者記錄
    private double banlance = 100; // 餘額
    private ChargeMode changeMode; // 計費方式
    private String number; // 電話號碼
    public User(ChargeMode changeMode, String number) {
        super();
        this.changeMode = changeMode;
        this.number = number;
    }
    public User() {
        // TODO Auto-generated constructor stub
    }
    public User(String number){
        this.number = number;
    }

    public UserRecords getUserRecords() {
        return userRecords;
    }
    public void setUserRecords(UserRecords userRecords) {
        this.userRecords = userRecords;
    }
    public double getBanlance() {
        return banlance;
    }
    public void setBanlance(double banlance) {
        this.banlance = banlance;
    }
    public ChargeMode getChargeMode() {
        return changeMode;
    }
    public void setChargeMode(ChargeMode changeMode) {
        this.changeMode = changeMode;
    }
    public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
        this.number = number;
    }
    public double calBanlance() {
        return this.banlance-changeMode.calCost(userRecords)-changeMode.getMonthlyRent();
    }
    public double calCost() {
        return changeMode.calCost(userRecords);
    }
}
class UserRecords {

    public UserRecords() {
        super();
    }

    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 ArrayList<CallRecord> getCallingInCityRecords() {
        return callingInCityRecords;
    }
    public ArrayList<CallRecord> getCallingInProvinceRecords() {
        return callingInProvinceRecords;
    }
    public ArrayList<CallRecord> getCallingInLandRecords() {
        return callingInLandRecords;
    }
    public ArrayList<CallRecord> getAnswerInCityRecords() {
        return answerInCityRecords;
    }
    public ArrayList<CallRecord> getAnswerInProvinceRecords() {
        return answerInProvinceRecords;
    }
    public ArrayList<CallRecord> getAnswerInLandRecords() {
        return answerInLandRecords;
    }
    public ArrayList<MessageRecord> getSendMessageRecords() {
        return sendMessageRecords;
    }
    public ArrayList<MessageRecord> getReceiveMessageRecords() {
        return receiveMessageRecords;
    }
    public void addCallingInCityRecords(CallRecord callRecord) {
        callingInCityRecords.add(callRecord);
    }
    public void addCallingInLandRecords(CallRecord callRecord) {
        callingInLandRecords.add(callRecord);
    }
    public void addCallingInProvinceRecords(CallRecord callRecord) {
        callingInProvinceRecords.add(callRecord);
    }
    public void addAnswerInCityRecords(CallRecord callRecord) {
        answerInCityRecords.add(callRecord);
    }
    public void addAnswerInProvinceRecords(CallRecord callRecord) {
        answerInProvinceRecords.add(callRecord);
    }
    public void addAnswerInLandRecords(CallRecord callRecord) {
        answerInLandRecords.add(callRecord);
    }
    public void addSendMessageRecords(MessageRecord messageRecord) {
        sendMessageRecords.add(messageRecord);
    }
    public void addReceiveMessageRecords(MessageRecord messageRecord) {
        receiveMessageRecords.add(messageRecord);
    }
}

(4)類圖及SourceMonitor分析

 

(5)我的分析:

①分析類圖和SourceMonitor圖:

  這次的題目是在第一次電話計費的基礎上加了手機通訊,主要是在第一次的框架基礎上加入了MobilePhoneChargingMobilePhoneInCityRuleMobilePhoneInLandRuleMobilePhoneInProvinceRule四個類計算不同地區的手機通訊。除此之外,因為加了手機通訊,手機-手機,手機-電話兩種通訊手段,省內省外國內三種計費方式,共計6種組合方式,增加了計費的難度,而且對於通訊記錄的輸入處理的正則表示式也更為是複雜了,與座機不同的是,手機計費增加了漫遊記費規則,這都是需要仔細考慮的點。看SourceMonitor圖分析,最大複雜度偏大,最大深度以及平均深度都在超出預期的邊緣,說明有一部分的程式碼並不理想,存在優化修改的空間。

②小結:

  在第一題的基礎上加入了手機的開戶,並增加了手機計費,手機互相通話,手機與座機通話,由於本題在上一題已經完成的結構上繼續進行,作業主要難度在於計費方法的運用。計費的情況有許多種,容易混亂掉,需要耐心分析,將每種情況都考慮在內細細分析。

3、電信計費系列3-簡訊計費

(1)題目:

實現一個簡單的電信計費程式,針對手機的簡訊採用如下計費方式
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。

本題只考慮簡訊計費,不考慮通訊費用以及月租費。

(2)測試樣例:

輸入樣例:

在這裡給出一組輸入。例如:

u-18907910010 3
m-18907910010 13305862264 aaaaaaaaaaaaaaaaaaaaaaa
end

輸出樣例:

在這裡給出相應的輸出。例如:

18907910010 0.3 99.7

(3)我的程式碼:

電信計費系列3-簡訊計費
 import java.util.*;
import java.text.*;
public class Main {

    public static void main(String[] args) {
        ArrayList<User> users = new ArrayList<User>();
        InputDeal inputDeal = new InputDeal();

        Scanner input = new Scanner(System.in);

        String s = input.nextLine();
        while(!s.equals("end")) {
            if(inputDeal.checkType(s)==1) {
                inputDeal.addUser(users, s);
            }
            if(inputDeal.checkType(s)==2) {
                inputDeal.addRecord(users, s);
            }
            s = input.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 user: users) {
            System.out.println(user.getNumber()+" "+Math.round(user.calCost()*100)/100.0+" "+Math.round(user.calBanlance()*100)/100.0);
        }
        input.close();
    }
}
abstract class CallChargeRule extends ChargeRule {

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

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

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

        char type = s[0].charAt(0);
        s[0] = s[0].substring(2);
        String sd = null, st = null, ed = null, et = null;

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

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

    public 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 callingNumber,String answerNumber,Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) {
        super();
        setCallingNumber(callingNumber);
        setAnswerNumber(answerNumber);
        this.startTime = startTime;
        this.endTime = endTime;
        this.callingAddressAreaCode = callingAddressAreaCode;
        this.answerAddressAreaCode = answerAddressAreaCode;
    }
    public Date getStartTime() {
        return startTime;
    }
    public void setStartTime(Date stateTime) {
        this.startTime = stateTime;
    }
    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 ChargeMode {
    private ArrayList<ChargeRule> chargeRules = new ArrayList<ChargeRule>();
    public ArrayList<ChargeRule> getChargeRules() {
        return chargeRules;
    }
    public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
        this.chargeRules = chargeRules;
    }
    public abstract double calCost(UserRecords useRecords);
    public abstract double getMonthlyRent();
}
abstract class ChargeRule {
    public  abstract double calCost(UserRecords userRecords);
}
abstract class CommunicationRecord {
    protected String callingNumber;
    protected String answerNumber;
    public String getCallingNumber() {
        return callingNumber;
    }
    public void setCallingNumber(String callingNumber) {
        this.callingNumber = callingNumber;
    }
    public String getAnswerNumber() {
        return answerNumber;
    }
    public void setAnswerNumber(String answerNumber) {
        this.answerNumber = answerNumber;
    }
}
class InputDeal {

    //使用者:1  通話記錄:2 基本格式錯誤:0
    public int checkType(String input) {
        if (input.matches("[u]-0791[0-9]{7,8}\\s[0]") || input.matches("[u]-1[0-9]{10}\\s[13]")) {
            return 1;
        } else if (input.matches("[m]-1[0-9]{10}\\s" + "1[0-9]{10}\\s" + "[0-9a-zA-Z\\s\\.,]+")) {
            return 2;
        }
        return 0;
    }
    public void addUser(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());
        } else if (mode == 3) {
            usernew.setChargeMode(new MobilePhoneMassageCharging());
        }
        users.add(usernew);
    }
    public void addRecord(ArrayList<User> users, String input) {
        String[] inputs = input.split(" ");
        inputs[0] = inputs[0].substring(2);

        User callu = null, answeru = null;

        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];
        } else {
            in = inputs[1];
        }

        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 (input.charAt(0) == 'm') {
            MessageRecord messageRecord = new MessageRecord(input);
            if (callu != null) {
                callu.getUserRecords().addSendMessageRecords(messageRecord);
                ;
            }
            if (answeru != null) {
                callu.getUserRecords().addReceiveMessageRecords(messageRecord);
            }
        }
    }
}
class LandlinePhoneCharging extends ChargeMode {

    private double monthlyRent = 20;

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

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

    @Override
    public double getMonthlyRent() {
        return monthlyRent;
    }
}
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 MessageRecord extends CommunicationRecord {
    private String message;

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

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
class MobilePhoneCharging extends ChargeMode{
    private double monthlyRent = 15;

    public MobilePhoneCharging() {
        super();
        getChargeRules().add(new MobilePhoneInCityRule());
        getChargeRules().add(new MobilePhoneInProvinceRule());
        getChargeRules().add(new MobilePhoneInLandRule());
    }

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

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


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 MobilePhoneMassageCharging extends ChargeMode {

    private double monthlyRent = 0;

    public MobilePhoneMassageCharging() {
        super();
        getChargeRules().add(new MobilePhoneMessageRule());
    }

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

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

}
class MobilePhoneMessageRule extends CallChargeRule {
    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        int number = 0;
        for (MessageRecord m : userRecords.getSendMessageRecords()) {
            int length = m.getMessage().length();
            if (length <= 10) {
                number++;
            }
            else {
                number += length / 10;
                if (length % 10 != 0) {
                    number++;
                }
            }
        }
        if (number <= 3) {
            sumCost = number * 0.1;
        }
        else if (number <= 5) {
            sumCost = 0.3 + 0.2 * (number - 3);
        }
        else {
            sumCost = 0.7 + 0.3 * (number - 5);
        }
        return sumCost;
    }
}
class User {
    private UserRecords userRecords = new UserRecords();// 使用者記錄
    private double banlance = 100; // 餘額
    private ChargeMode changeMode; // 計費方式
    private String number; // 電話號碼
    public User(ChargeMode changeMode, String number) {
        super();
        this.changeMode = changeMode;
        this.number = number;
    }
    public User() {
        // TODO Auto-generated constructor stub
    }
    public User(String number){
        this.number = number;
    }

    public UserRecords getUserRecords() {
        return userRecords;
    }
    public void setUserRecords(UserRecords userRecords) {
        this.userRecords = userRecords;
    }
    public double getBanlance() {
        return banlance;
    }
    public void setBanlance(double banlance) {
        this.banlance = banlance;
    }
    public ChargeMode getChargeMode() {
        return changeMode;
    }
    public void setChargeMode(ChargeMode changeMode) {
        this.changeMode = changeMode;
    }
    public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
        this.number = number;
    }
    public double calBanlance() {
        return this.banlance-changeMode.calCost(userRecords)-changeMode.getMonthlyRent();
    }
    public double calCost() {
        return changeMode.calCost(userRecords);
    }
}
class UserRecords {

    public UserRecords() {
        super();
    }

    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 ArrayList<CallRecord> getCallingInCityRecords() {
        return callingInCityRecords;
    }
    public ArrayList<CallRecord> getCallingInProvinceRecords() {
        return callingInProvinceRecords;
    }
    public ArrayList<CallRecord> getCallingInLandRecords() {
        return callingInLandRecords;
    }
    public ArrayList<CallRecord> getAnswerInCityRecords() {
        return answerInCityRecords;
    }
    public ArrayList<CallRecord> getAnswerInProvinceRecords() {
        return answerInProvinceRecords;
    }
    public ArrayList<CallRecord> getAnswerInLandRecords() {
        return answerInLandRecords;
    }
    public ArrayList<MessageRecord> getSendMessageRecords() {
        return sendMessageRecords;
    }
    public ArrayList<MessageRecord> getReceiveMessageRecords() {
        return receiveMessageRecords;
    }
    public void addCallingInCityRecords(CallRecord callRecord) {
        callingInCityRecords.add(callRecord);
    }
    public void addCallingInLandRecords(CallRecord callRecord) {
        callingInLandRecords.add(callRecord);
    }
    public void addCallingInProvinceRecords(CallRecord callRecord) {
        callingInProvinceRecords.add(callRecord);
    }
    public void addAnswerInCityRecords(CallRecord callRecord) {
        answerInCityRecords.add(callRecord);
    }
    public void addAnswerInProvinceRecords(CallRecord callRecord) {
        answerInProvinceRecords.add(callRecord);
    }
    public void addAnswerInLandRecords(CallRecord callRecord) {
        answerInLandRecords.add(callRecord);
    }
    public void addSendMessageRecords(MessageRecord messageRecord) {
        sendMessageRecords.add(messageRecord);
    }
    public void addReceiveMessageRecords(MessageRecord messageRecord) {
        receiveMessageRecords.add(messageRecord);
    }
}

(4)類圖及SourceMonitor分析

(5)我的分析:

①分析類圖和SourceMonitor圖:

  這次作業我只是在第二次的基礎上增加了資訊計費的規則以及修改了對於輸入資訊的處理,看SourceMonitor圖分析,最大複雜度偏大,最大深度以及平均深度都在超出預期的邊緣,說明有一部分的程式碼並不理想,存在優化修改的空間。

②小結:

  簡訊收費很簡單,沒有複雜的收費規則,只有一個簡單的收費方法。不過有一點需要注意:如果簡訊長度大於十就按照兩條簡訊傳送,並且要注意傳送的次數多少,每一次傳送簡訊的價格也會隨著次數的增加而增高。不需要考慮地區的情況,也不需要新的計費規則。

三、踩坑心得

(1)對於排序問題,可以使用Java的一些類和方法,比如說:sort()方法和Comparable介面,三次作業的輸出都要求每個使用者一行,使用者之間按號碼字元從小到大排序。這就需要先對User中Number屬性進行排序,然後輸出。

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

(2)關於正則表示式編寫,對於資料合法性的判斷以及儲存,我這裡是專門寫了一個InputDeal的類,裡邊有deal()方法用以處理字串,addUser()用以增加儲存使用者,addRecord()用以增加存儲存通訊記錄。checkType()用以檢查是哪一種輸入。其中使用"[u]-0791[0-9]{7,8}\\s[0]"來檢測座機型別使用者輸入,"[u]-1[0-9]{10}\\s[13]"來檢測手機型別使用者輸入,用"[m]-1[0-9]{10}\\s" + "1[0-9]{10}\\s" + "[0-9a-zA-Z\\s\\.,]+"檢測資訊型別的資料的輸入。

(3)還有就是對於通話時長的計算,查閱了網上的資料才瞭解到date型別的資料有getTime()獲取時間然後就可以與同類型資料進行加減運算了。對於時間型別資料格式的規範我是採用了題目建議"yyyy.MM.dd HH:mm:ss"格式,使用SimpleDateFormat類

四、改進建議

(1)這一系列的題目總的來說,自己設計的部分較少,基本都是在老師所提供的參考類圖的基礎上填寫方法補充內容,我自己設計也就只有Main類以及對輸入資訊的處理。我覺得對於這方面的處理我並沒有做好,首先是正則表示式的編寫,比如第二次手機+電話通訊,我的正則表示式就很複雜,建議在正則表示式的書寫更精簡一些,比如表達數字[0-9]可以換成\\d的一系列操作,這樣更容易看清楚以及時找出錯誤。在編譯程式碼的時候不用過於著急,應當顧其全面,儘量減少遺漏。

(2)我沒有做到將三次的輸入有機的結合在一起,只是簡單的就題論題,對三次作業不同的輸入進行不同的處理,這是我覺得可以結合在一起,構成一個統一的系統的。

五、總結

  通過對作業的解決,學會了演算法,面向物件的三大技術特性,作業的解決過程中,漸漸的懂得了三大技術特性之間的關係,封裝,承載,繼承,多型,都是新學會的名詞,過載是多型的一部分,也是程式碼中不可或缺的部分,通過對簡單和複雜題目的解答,從最初學會的最基本的System.out.println到後來對子類父類的呼叫,使用多型中的方法實現子類覆蓋父類,從最開始的獲取字元到後面的單一開閉原則,每次作業的過程就是積累的過程,每次解題的過程就是成長的過程。

  這幾個月的面向物件課程的Java學習和作業練習,從初步瞭解到Java的基本演算法和簡單的Java思想,到慢慢的瞭解更加複雜的Java問題,碰到難題時,解決的過程既鍛鍊了自己的耐心,也學會了以前不瞭解的知識,不過解題過程中也有發現自己拖延懶惰的問題,遇到困難總是想拖,但是下次寫又忘記了這次的思路,這是需要加緊改正的不足。