1. 程式人生 > 其它 >OO第三次java學習blog

OO第三次java學習blog

第三次java學習blog

前言

涉及知識點:

  • 1.正則表示式的運用
  • 2.字串的操作
  • 3.SimpleDateFormat類的使用
  • 4.Date類的使用
  • 5.泛型的使用
  • 6.HashSet類的使用
  • 7.多型的使用
  • 8.繼承的使用
  • 9.介面的使用
  • 10.異常的使用

題量與難度

這三次題目集的題量都不大,之後的作業都是在之前的程式碼上進行迭代,所以,只要第一次的類設計的好,之後的作業就會比較容易。

設計與分析

電信計費1

原始碼:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        Scanner input = new Scanner(System.in);
        java.text.DecimalFormat df = new java.text.DecimalFormat("0.0#");
        HashSet<String> init = new HashSet<>();// 儲存使用者號碼,代表User
        ArrayList<String> operate = new ArrayList<String>();// 對User進行的操作
        ArrayList<User> users = new ArrayList<User>();
        String reg = "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3}).((([13578]|1[02]).([1-9]|[12][0-9]|3[01]))|(([469]|11).([1-9]|[12][0-9]|30))|(2-([1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))-2-29))\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])";
        while (true) {
            String t = input.nextLine();
            if (t.equals("end")) {
                break;
            } else {
                if(t.length()<5){
                    continue;
                }
                if (t.charAt(0) == 'u') {
                    init.add(t);
                } else if (t.charAt(0) == 't') {
                    operate.add(t);
                }
            }
        }
        for (String it : init) {
            String nums[] = it.split("[- ]");
            if (nums[1].length() >= 11 && nums[1].length() <= 12) {
                if (nums[2].equals("0") && nums.length == 3) {
                    users.add(new User(new LandlinePhoneCharging(), nums[1]));// init
                }
            }
        }
        for (String it : operate) {
            String ops[] = it.split("[- ]");
            String startString = ops[3] + " " + ops[4];
            String endString = ops[5] + " " + ops[6];
            String callnum = ops[1];
            String recenum = ops[2];
            if (ops[1].length() >= 11 && ops[1].length() <= 12 && ops[2].length() <= 12 && ops[2].length() >= 10 && ops.length == 7 && startString.matches(reg) && endString.matches(reg)) {
                try {
                    Date startDate = simpleDateFormat.parse(startString);
                    Date endDate = simpleDateFormat.parse(endString);
                    if (callnum.substring(0, 4).equals(recenum.substring(0, 4))) {
                        for (User user : users) {// city
                            if (user.getNumber().equals(callnum)) {
                                CallRecord callRecord = new CallRecord(startDate, endDate, callnum, recenum);
                                user.getUserRecords().getCallingInCityRecords().add(callRecord);
                            }
                        }
                    } else if (recenum.substring(0,3).equals("079")||recenum.substring(0,4).equals("0701")) {
                        for (User user : users) {// city
                            if (user.getNumber().equals(callnum)) {
                                CallRecord callRecord = new CallRecord(startDate, endDate, callnum, recenum);
                                user.getUserRecords().getCallingInProvinceRecords().add(callRecord);
                            }
                        }
                    } else {
                        for (User user : users) {// city
                            if (user.getNumber().equals(callnum)) {
                                CallRecord callRecord = new CallRecord(startDate, endDate, callnum, recenum);
                                user.getUserRecords().getCallingInLandRecords().add(callRecord);
                            }
                        }
                    }
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    continue;
                }
            }
        }

        for (User user : users) {
            user.calCost();
            user.calBalance();
        }
        for (int i = 0; i < users.size(); i++) {
            int min = i;
            for (int j = i; j < users.size(); j++) {
                if (users.get(min).getNumber().compareTo(users.get(j).getNumber()) > 0) {
                    min = j;
                }
            }
            Collections.swap(users, i, min);
        }
        for (User user : users) {
            System.out.println(user.getNumber() + " " + df.format(user.calCost()) + " " + df.format(user.getBalance()));
        }
    }
}


class Judge{

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

class CallRecord extends CommunicationRecord {//通話記錄 繼承自抽象類
    private Date startTime;//開始時間
    private Date endTime;//結束時間
    private String callingAddressAreaCode;//地區,存區號
    private String answerAddressAreaCode;//地區,存區號

    public CallRecord() {
    }

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

    public Date getStartTime() {
        return startTime;
    }

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

    public Date getEndTime() {
        return endTime;
    }

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

    public String getCallingAddressAreaCode() {
        return callingAddressAreaCode;
    }

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

    public String getAnswerAddressAreaCode() {
        return answerAddressAreaCode;
    }

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

}
abstract class 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 userRecords);

    public abstract double getMonthlyRent();
}
abstract class ChargeRule {

}
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 LandlinePhoneCharging extends ChargeMode {

    final double monthlyRent = 20;

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

    @Override
    public double calCost(UserRecords userRecords) {
        // TODO Auto-generated method stub
        double all = 0;
        all += ((LandPhoneInCityRule) getChargeRules().get(0)).calCost(userRecords.getCallingInCityRecords());
        all += ((LandPhoneInProvinceRule) getChargeRules().get(1)).calCost(userRecords.getCallingInProvinceRecords());
        all += ((LandPhoneInlandRule) getChargeRules().get(2)).calCost(userRecords.getCallingInLandRecords());
        return all;
    }

    @Override
    public double getMonthlyRent() {
        // TODO Auto-generated method stub
        return monthlyRent;
    }

}

class LandPhoneInCityRule extends CallChargeRule {

    public LandPhoneInCityRule() {
    }

    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        // TODO Auto-generated method stub
        double ans = 0;
        for (CallRecord callRecord : callRecords) {
            long a = callRecord.getStartTime().getTime();
            long b = callRecord.getEndTime().getTime();
            int c = (int) ((b - a) / 1000);
            if (c % 60 != 0) {
                ans += (c / 60 + 1) * 0.1;
            } else {
                ans += (c / 60) * 0.1;
            }
        }
        return ans;
    }

}

class LandPhoneInlandRule extends CallChargeRule {

    
    public LandPhoneInlandRule() {
    }

    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        // TODO Auto-generated method stub
        double ans = 0;
        for (CallRecord callRecord : callRecords) {
            long a = callRecord.getStartTime().getTime();
            long b = callRecord.getEndTime().getTime();
            int c = (int) ((b - a) / 1000);
            if (c % 60 != 0) {
                ans += (c / 60 + 1) * 0.6;
            } else {
                ans += (c / 60) * 0.6;
            }
        }
        return ans;
    }
    
}

class LandPhoneInProvinceRule extends CallChargeRule {

    public LandPhoneInProvinceRule() {
    }

    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        // TODO Auto-generated method stub
        double ans = 0;
        for (CallRecord callRecord : callRecords) {
            long a = callRecord.getStartTime().getTime();
            long b = callRecord.getEndTime().getTime();
            int c = (int) ((b - a) / 1000);
            if (c % 60 != 0) {
                ans += (c / 60 + 1) * 0.3;
            } else {
                ans += (c / 60) * 0.3;
            }
        }
        return ans;
    }

}

class MessageRecord extends CommunicationRecord {
    private String message;

    public MessageRecord() {
    }

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

    public String getMessage() {
        return message;
    }

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

}
class User {
    private UserRecords userRecords = new UserRecords();
    private double balance = 100;
    private ChargeMode chargeMode;
    private String number;

    public User() {
    }

    public User(ChargeMode chargeMode, String number) {
        this.chargeMode = chargeMode;
        this.number = number;
    }

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

    public double calCost() {
        return chargeMode.calCost(userRecords);
    }

    public UserRecords getUserRecords() {
        return userRecords;
    }

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

    public double getBalance() {
        return balance;
    }

    public ChargeMode getChargeMode() {
        return chargeMode;
    }

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

    public String getNumber() {
        return number;
    }

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

}

class 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> answerInCitRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInProvincRecords = 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 UserRecords() {
    }

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

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

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

    public ArrayList<CallRecord> getAnswerInCitRecords() {
        return answerInCitRecords;
    }

    public ArrayList<CallRecord> getAnswerInProvincRecords() {
        return answerInProvincRecords;
    }

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

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

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

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

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

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

    public void addAnswerInCitRecords(CallRecord answerInCitRecords) {
        this.answerInCitRecords.add(answerInCitRecords);
    }

    public void addAnswerInProvincRecords(CallRecord answerInProvincRecords) {
        this.answerInProvincRecords.add(answerInProvincRecords);
    }

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

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

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

類圖:

  • 在上面的類圖中,我們可以看到,User類中有一個UserRecords型別的屬性,這個屬性是一個UserRecords型別的物件,這個物件中包含了一些CallRecord型別的屬性,這些屬性是一個集合,這些集合中包含了一些CallRecord型別的物件,這些物件中包含了一些屬性,這些屬性是一些字串型別的屬性,這些屬性是使用者的通話記錄,傳送的簡訊記錄,接收的簡訊記錄。
  • 利用了抽象計費類來表示計費模式,讓後續的程式碼可以進行迭代,不需要修改。
  • 利用了抽象、繼承和多型進行程式設計,讓程式碼的可維護性質更強,並且增強了程式碼的可讀性和可維護性。
  • 關於類的繼承,可以看到,User類繼承了CommunicationRecord類,而CommunicationRecord類繼承了CallRecord類,而CallRecord類繼承了CommunicationRecord類。

電信計費2

原始碼:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        Scanner input = new Scanner(System.in);
        java.text.DecimalFormat df = new java.text.DecimalFormat("0.0#");
        HashSet<String> init = new HashSet<String>();// 儲存使用者號碼,代表User
        ArrayList<String> operate = new ArrayList<String>();// 對User進行的操作
        ArrayList<User> users = new ArrayList<User>();
        String initCellphonenum = "[u][-]1[3-9]\\d{9}[ ][1]";
        String initLandPhonenum = "[u][-]0791[0-9]{7,8}[ ][0]";
        while (true) {// 將資訊錄入
            String t = input.nextLine();
            if (t.equals("end")) {
                break;
            } else {
                if (t.length() < 13) {
                    continue;
                }
                if (t.charAt(0) == 'u') {// 首字母為u則為註冊
                    init.add(t);
                } else if (t.charAt(0) == 't') {// 首字母為t則為操作
                    operate.add(t);
                }
            }
        }

        for (String it : init) {
            if (it.matches(initCellphonenum) || it.matches(initLandPhonenum)) {
                String nums[] = it.split("[- ]");

                if (nums[2].equals("1")) {
                    users.add(new User(new CellphoneCharging(), nums[1]));
                } else if (nums[2].equals("0")) {
                    users.add(new User(new LandlinePhoneCharging(), nums[1]));
                } else if (nums[2].equals("3")) {

                }
            }
        } // TODO 使用者的初始化已完成
          // TODO 判斷打電話操作時間,先利用字串切割,然後判斷切割後的字串個數,通過個數以及手機號碼來判斷區號,
        for (String it : operate) {
            if (land_call_landjudge(it)) {
                String arrs[] = it.split("[- ]");
                String callNumber = arrs[1];
                String answerNumber = arrs[2];
                String startTime = arrs[3] + " " + arrs[4];
                String endTime = arrs[5] + " " + arrs[6];
                String Call_Code = callNumber.substring(0, 4);
                String Answer_Code = answerNumber.substring(0, 4);
                if (callNumber.equals(answerNumber)) {
                    continue;
                }
                if (Datejudge(startTime) && Datejudge(endTime)) {
                    Date startDate = simpleDateFormat.parse(startTime);
                    Date endDate = simpleDateFormat.parse(endTime);
                    for (User user : users) {
                        if (user.getNumber().equals(callNumber)) {
                            if (Answer_Code.equals("0791")) {
                                // TODO 判斷是否為市內電話
                                user.getUserRecords().addCallingInCityRecords(
                                        new CallRecord(startDate, endDate, Call_Code, Answer_Code));
                                break;
                            } else if (Answer_Code.substring(0, 3).equals("079") || Answer_Code.equals("0701")) {
                                // TODO 判斷是否為市外電話
                                user.getUserRecords().addCallingInProvinceRecords(
                                        new CallRecord(startDate, endDate, Call_Code, Answer_Code));
                                break;
                            } else {
                                // TODO 判斷是否為國內電話
                                user.getUserRecords().addCallingInLandRecords(
                                        new CallRecord(startDate, endDate, Call_Code, Answer_Code));
                                break;
                            }
                        }
                    }
                }
            } else if (land_call_cellJudge(it)) {
                String arrs[] = it.split("[- ]");
                String callNumber = arrs[1];
                String answerNumber = arrs[2];
                String startTime = arrs[4] + " " + arrs[5];
                String endTime = arrs[6] + " " + arrs[7];
                String Call_Code = callNumber.substring(0, 4);// 可能為省外電話,此時則不一定為前四位
                String Answer_Code = arrs[3];
                if (Datejudge(startTime) && Datejudge(endTime)) {
                    Date startDate = simpleDateFormat.parse(startTime);
                    Date endDate = simpleDateFormat.parse(endTime);
                    for (User user : users) {
                        if (user.getNumber().equals(callNumber)) {
                            if (Call_Code.equals(Answer_Code)) {
                                user.getUserRecords().addCallingInCityRecords(
                                        new CallRecord(startDate, endDate, Call_Code, Answer_Code));
                            } else if (Call_Code.substring(0, 3).equals(Answer_Code.substring(0, 3))) {
                                user.getUserRecords().addCallingInProvinceRecords(
                                        new CallRecord(startDate, endDate, Call_Code, Answer_Code));
                            } else {
                                user.getUserRecords().addCallingInLandRecords(
                                        new CallRecord(startDate, endDate, Call_Code, Answer_Code));
                            }
                        }
                    }
                    if (!Answer_Code.equals("0701") && !Answer_Code.substring(0, 3).equals("079")) {// 不是江西省內區號
                        for (User user : users) {
                            if (user.getNumber().equals(answerNumber)) {
                                user.getUserRecords().addAnswerInLandRecords(
                                        new CallRecord(startDate, endDate, Call_Code, Answer_Code));
                                break;
                            }
                        }
                    }
                }
            } else if (cell_call_landJudge(it)) {
                String arrs[] = it.split("[- ]");
                String callNumber = arrs[1];
                String answerNumber = arrs[3];
                String startTime = arrs[4] + " " + arrs[5];
                String endTime = arrs[6] + " " + arrs[7];
                String Call_Code = arrs[2];
                String Answer_Code = answerNumber.substring(0, 4);
                if (Datejudge(startTime) && Datejudge(endTime)) {
                    Date startDate = simpleDateFormat.parse(startTime);
                    Date endDate = simpleDateFormat.parse(endTime);
                    for (User user : users) {
                        if (user.getNumber().equals(callNumber)) {
                            if (Call_Code.equals("0791")) {
                                user.getUserRecords().addCallingInCityRecords(
                                        new CallRecord(startDate, endDate, Call_Code, Answer_Code));
                            } else if (Call_Code.substring(0, 3).equals("079") || Call_Code.equals("0701")) {
                                user.getUserRecords().addCallingInProvinceRecords(
                                        new CallRecord(startDate, endDate, Call_Code, Answer_Code));
                            } else {
                                user.getUserRecords().addCallingInLandRecords(
                                        new CallRecord(startDate, endDate, Call_Code, Answer_Code));
                            }
                        }
                    }
                    for (User user : users) {// TODO 座機可能出問題
                        if (user.getNumber().equals(answerNumber)) {
                            user.getUserRecords().addAnswerInLandRecords(
                                    new CallRecord(startDate, endDate, Call_Code, Answer_Code));
                            break;
                        }
                    }
                }
            } else if (cell_call_cellJudge(it)) {
                String arrs[] = it.split("[- ]");
                String callNumber = arrs[1];
                String answerNumber = arrs[3];
                String startTime = arrs[5] + " " + arrs[6];
                String endTime = arrs[7] + " " + arrs[8];
                String Call_Code = arrs[2];
                String Answer_Code = arrs[4];
                if (Datejudge(startTime) && Datejudge(endTime)) {
                    Date startDate = simpleDateFormat.parse(startTime);
                    Date endDate = simpleDateFormat.parse(endTime);
                    for (User user : users) {
                        if (user.getNumber().equals(callNumber)) {
                            if (Call_Code.equals("0791")) {
                                user.getUserRecords().addCallingInCityRecords(
                                        new CallRecord(startDate, endDate, Call_Code, Answer_Code));
                            } else if (Call_Code.substring(0, 3).equals("079") || Call_Code.equals("0701")) {// 省內漫遊通話
                                user.getUserRecords().addCallingInProvinceRecords(
                                        new CallRecord(startDate, endDate, Call_Code, Answer_Code));
                            } else {
                                user.getUserRecords().addCallingInLandRecords(
                                        new CallRecord(startDate, endDate, Call_Code, Answer_Code));
                            }
                        }
                    }
                    if (!Answer_Code.equals("0701") && !Answer_Code.substring(0, 3).equals("079")) {
                        for (User user2 : users) {
                            if (user2.getNumber().equals(answerNumber)) {
                                user2.getUserRecords().addAnswerInLandRecords(
                                        new CallRecord(startDate, endDate, Call_Code, Answer_Code));
                                break;
                            }
                        }
                    }
                }
            } else {
                continue;
            }

        }
        for (User user : users) {
            user.calCost();
            user.calBalance();
        }
        for (int i = 0; i < users.size(); i++) {
            int min = i;
            for (int j = i; j < users.size(); j++) {
                if (users.get(min).getNumber().compareTo(users.get(j).getNumber()) > 0) {
                    min = j;
                }
            }
            Collections.swap(users, i, min);
        }
        for (User user : users) {
            System.out.println(user.getNumber() + " " + df.format(user.calCost()) + " " +
                    df.format(user.getBalance()));
        }
    }

    public static boolean Datejudge(String datestring) {
        boolean flag = true;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        simpleDateFormat.setLenient(false);
        try {
            simpleDateFormat.parse(datestring);
        } catch (Exception e) {
            // TODO: handle exception
            flag = false;
        }
        return flag;
    }

    public static boolean land_call_landjudge(String arr) {
        String landcityincity = "t-0791\\d{7,8} 0\\d{9,11} \\d{4}.\\d{1,2}.\\d{1,2} \\d{2}:\\d{2}:\\d{2} \\d{4}.\\d{1,2}.\\d{1,2} \\d{2}:\\d{2}:\\d{2}";
        if (arr.matches(landcityincity)) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean land_call_cellJudge(String arr) {
        String land_call_cell = "t-0\\d{9,11} 1\\d{10} 0\\d{2,3} \\d{4}.\\d{1,2}.\\d{1,2} \\d{2}:\\d{2}:\\d{2} \\d{4}.\\d{1,2}.\\d{1,2} \\d{2}:\\d{2}:\\d{2}";

        if (arr.matches(land_call_cell)) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean cell_call_landJudge(String arr) {
        String cell_call_land = "t-1\\d{10} 0\\d{2,3} 0\\d{9,11} \\d{4}.\\d{1,2}.\\d{1,2} \\d{2}:\\d{2}:\\d{2} \\d{4}.\\d{1,2}.\\d{1,2} \\d{2}:\\d{2}:\\d{2}";

        if (arr.matches(cell_call_land)) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean cell_call_cellJudge(String arr) {
        String cell_call_cell = "t-1\\d{10} 0\\d{2,3} 1\\d{10} 0\\d{2,3} \\d{4}.\\d{1,2}.\\d{1,2} \\d{2}:\\d{2}:\\d{2} \\d{4}.\\d{1,2}.\\d{1,2} \\d{2}:\\d{2}:\\d{2}";

        if (arr.matches(cell_call_cell)) {
            return true;
        } else {
            return false;
        }
    }
}



abstract class CallChargeRule extends ChargeRule{
    public abstract double calCost(ArrayList<CallRecord> callRecords);
}
class CallRecord extends CommunicationRecord {//通話記錄 繼承自抽象類
    private Date startTime;//開始時間
    private Date endTime;//結束時間
    private String callingAddressAreaCode;//地區,存區號
    private String answerAddressAreaCode;//地區,存區號

    public CallRecord() {
    }

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

    public Date getStartTime() {
        return startTime;
    }

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

    public Date getEndTime() {
        return endTime;
    }

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

    public String getCallingAddressAreaCode() {
        return callingAddressAreaCode;
    }

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

    public String getAnswerAddressAreaCode() {
        return answerAddressAreaCode;
    }

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

}
 class CellphoneCharging extends ChargeMode {
    final double monthlyRent = 15;

    public CellphoneCharging() {
        getChargeRules().add(new CellphoneInCityRule());
        getChargeRules().add(new CellphoneInProvinceRule());
        getChargeRules().add(new CellphoneInlandRule());
        getChargeRules().add(new CellphoneInlandAnsRule());
    }

    @Override
    public double calCost(UserRecords userRecords) {
        // TODO Auto-generated method stub
        double all = 0;
        all += ((CellphoneInCityRule) getChargeRules().get(0)).calCost(userRecords.getCallingInCityRecords());
        all += ((CellphoneInProvinceRule) getChargeRules().get(1)).calCost(userRecords.getCallingInProvinceRecords());
        all += ((CellphoneInlandRule) getChargeRules().get(2)).calCost(userRecords.getCallingInLandRecords());
        all += ((CellphoneInlandAnsRule) getChargeRules().get(3)).calCost(userRecords.getAnswerInLandRecords());
        return all;
    }

    @Override
    public double getMonthlyRent() {
        // TODO Auto-generated method stub
        return monthlyRent;
    }

}
class CellphoneInCityRule extends CallChargeRule {

    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        // TODO Auto-generated method stub
        double ans = 0;
        for (CallRecord callRecord : callRecords) {
            long a = callRecord.getStartTime().getTime();
            long b = callRecord.getEndTime().getTime();
            int c = (int) ((b - a) / 1000);
            if (callRecord.getAnswerAddressAreaCode().equals("0791")) {
                if (c % 60 != 0) {
                    ans += (c / 60 + 1) * 0.1;
                } else {
                    ans += (c / 60) * 0.1;
                }
            } else if (callRecord.getAnswerAddressAreaCode().substring(0, 3).equals("079")||callRecord.getAnswerAddressAreaCode().equals("0701")) {
                if (c % 60 != 0) {
                    ans += (c / 60 + 1) * 0.2;
                } else {
                    ans += (c / 60) * 0.2;
                }
            } else {
                if (c % 60 != 0) {
                    ans += (c / 60 + 1) * 0.3;
                } else {
                    ans += (c / 60) * 0.3;
                }
            }
        }
        return ans;
    }

}
class CellphoneInlandAnsRule extends CallChargeRule {
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double cost = 0;
        for (CallRecord callRecord : callRecords) {
            long a = callRecord.getStartTime().getTime();
            long b = callRecord.getEndTime().getTime();
            int c = (int) ((b - a) / 1000);
            if (c % 60 != 0) {
                cost += (c / 60 + 1) * 0.3;
            } else {
                cost += (c / 60) * 0.3;
            }
        }
        return cost;
    }
}
class CellphoneInlandRule extends CallChargeRule {

    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        // TODO Auto-generated method stub
        double ans = 0;
        for (CallRecord callRecord : callRecords) {
            long a = callRecord.getStartTime().getTime();
            long b = callRecord.getEndTime().getTime();
            int c = (int) ((b - a) / 1000);
            if (c % 60 != 0) {
                ans += (c / 60 + 1) * 0.6;
            } else {
                ans += (c / 60) * 0.6;
            }
        }
        return ans;
    }

}
class CellphoneInProvinceRule extends CallChargeRule{

    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        // TODO Auto-generated method stub
        double ans = 0;
        for (CallRecord callRecord : callRecords) {
            long a = callRecord.getStartTime().getTime();
            long b = callRecord.getEndTime().getTime();
            int c = (int) ((b - a) / 1000);
            if (c % 60 != 0) {
                ans += (c / 60 + 1) * 0.3;
            } else {
                ans += (c / 60) * 0.3;
            }
        }
        return ans;
    }
    
}
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 userRecords);

    public abstract double getMonthlyRent();
}
abstract class ChargeRule {

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

}

abstract class Judge {
    public abstract boolean judge_landlinephone();
    
}
class LandlinePhoneCharging extends ChargeMode {

    final double monthlyRent = 20;

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

    @Override
    public double calCost(UserRecords userRecords) {
        // TODO Auto-generated method stub
        double all = 0;
        all += ((LandPhoneInCityRule) getChargeRules().get(0)).calCost(userRecords.getCallingInCityRecords());
        all += ((LandPhoneInProvinceRule) getChargeRules().get(1)).calCost(userRecords.getCallingInProvinceRecords());
        all += ((LandPhoneInlandRule) getChargeRules().get(2)).calCost(userRecords.getCallingInLandRecords());
        return all;
    }

    @Override
    public double getMonthlyRent() {
        // TODO Auto-generated method stub
        return monthlyRent;
    }

}
class LandPhoneInCityRule extends CallChargeRule {

    public LandPhoneInCityRule() {
    }

    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        // TODO Auto-generated method stub
        double ans = 0;
        for (CallRecord callRecord : callRecords) {
            long a = callRecord.getStartTime().getTime();
            long b = callRecord.getEndTime().getTime();
            int c = (int) ((b - a) / 1000);
            if (c % 60 != 0) {
                ans += (c / 60 + 1) * 0.1;
            } else {
                ans += (c / 60) * 0.1;
            }
        }
        return ans;
    }

}
class LandPhoneInlandRule extends CallChargeRule {

    
    public LandPhoneInlandRule() {
    }

    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        // TODO Auto-generated method stub
        double ans = 0;
        for (CallRecord callRecord : callRecords) {
            long a = callRecord.getStartTime().getTime();
            long b = callRecord.getEndTime().getTime();
            int c = (int) ((b - a) / 1000);
            if (c % 60 != 0) {
                ans += (c / 60 + 1) * 0.6;
            } else {
                ans += (c / 60) * 0.6;
            }
        }
        return ans;
    }
    
}
class LandPhoneInProvinceRule extends CallChargeRule {

    public LandPhoneInProvinceRule() {
    }

    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        // TODO Auto-generated method stub
        double ans = 0;
        for (CallRecord callRecord : callRecords) {
            long a = callRecord.getStartTime().getTime();
            long b = callRecord.getEndTime().getTime();
            int c = (int) ((b - a) / 1000);
            if (c % 60 != 0) {
                ans += (c / 60 + 1) * 0.3;
            } else {
                ans += (c / 60) * 0.3;
            }
        }
        return ans;
    }

}
class MessageRecord extends CommunicationRecord {
    private String message;

    public MessageRecord() {
    }

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

    public String getMessage() {
        return message;
    }

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

}
class User {
    private UserRecords userRecords = new UserRecords();
    private double balance = 100;
    private ChargeMode chargeMode;
    private String number;

    public User() {
    }

    public User(ChargeMode chargeMode, String number) {
        this.chargeMode = chargeMode;
        this.number = number;
    }

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

    public double calCost() {
        return chargeMode.calCost(userRecords);
    }

    public UserRecords getUserRecords() {
        return userRecords;
    }

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

    public double getBalance() {
        return balance;
    }

    public ChargeMode getChargeMode() {
        return chargeMode;
    }

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

    public String getNumber() {
        return number;
    }

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

}
class 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> answerInCitRecords = new ArrayList<CallRecord>();//市內接通電話
    private ArrayList<CallRecord> answerInProvincRecords = 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 UserRecords() {
    }

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

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

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

    public ArrayList<CallRecord> getAnswerInCitRecords() {
        return answerInCitRecords;
    }

    public ArrayList<CallRecord> getAnswerInProvincRecords() {
        return answerInProvincRecords;
    }

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

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

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

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

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

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

    public void addAnswerInCitRecords(CallRecord answerInCitRecords) {
        this.answerInCitRecords.add(answerInCitRecords);
    }

    public void addAnswerInProvincRecords(CallRecord answerInProvincRecords) {
        this.answerInProvincRecords.add(answerInProvincRecords);
    }

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

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

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

類圖:

  • 該次題目新增了一個手機類,並且增加了三種計費方式,由於之前的類沒有設計好。所以這次的的設計和之前的設計有很大的不同之處,這次的設計對比上次的設計更加有結構性,並且其類之間的關係也更加明確;
    對於格式錯誤的判斷,這次將每種情況的正則表示式寫出來,每個表示式封裝成一個函式,這樣就可以更加簡單的判斷了;並且每個函式對應一種情況,四個函式將所有的情況都一一對應,極大減少了格式的判斷;

電信計費3

原始碼:

import java.text.ParseException;
import java.util.*;

public class Main {
    public static void main(String[] args) throws ParseException {

        try (Scanner input = new Scanner(System.in)) {
            java.text.DecimalFormat df = new java.text.DecimalFormat("0.0#");

            HashSet<String> init = new HashSet<String>();// 儲存使用者號碼,代表User
            ArrayList<String> operate = new ArrayList<String>();// 對User進行的操作
            ArrayList<User> users = new ArrayList<User>();
            while (true) {// 將資訊錄入
                String t = input.nextLine();
                if (t.equals("end")) {
                    break;
                } else {
                    if (t.length() < 13) {
                        continue;
                    }
                    if (t.charAt(0) == 'u') {// 首字母為u則為註冊
                        init.add(t);
                    } else if (t.charAt(0) == 'm') {// 首字母為t則為操作
                        operate.add(t);
                    }
                }
            }

            for (String it : init) {
                if (NumberJudge(it)) {
                    String nums[] = it.split("[- ]");
                    users.add(new User(new MessageCharging(), nums[1]));
                }
            }

            for (String it : operate) {
                if (MessageJudge(it)) {
                    String nums[] = it.split("[- ]");
                    String sendMessageNum = nums[1];
                    String message = it.substring(26);
                    for (User user : users) {
                        if (user.getNumber().equals(sendMessageNum)) {
                            user.getUserRecords().getSendMessageRecords().add(new MessageRecord(message));
                        }
                    }
                }
            }
            for (User user : users) {
                user.calCost();
                user.calBalance();
            }
            for (int i = 0; i < users.size(); i++) {
                int min = i;
                for (int j = i; j < users.size(); j++) {
                    if (users.get(min).getNumber().compareTo(users.get(j).getNumber()) > 0) {
                        min = j;
                    }
                }
                Collections.swap(users, i, min);
            }
            for (User user : users) {
                System.out.println(user.getNumber() + " " + df.format(user.calCost()) + " " +
                        df.format(user.getBalance()));
            }
        }
    }
    

    public static boolean NumberJudge(String arr) {
        String numberjudge = "[u][-]1[3-9]\\d{9}[ ][3]";

        if (arr.matches(numberjudge)) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean MessageJudge(String arr) {
        String messagejudge = "m-1[3-9]\\d{9} 1[3-9]\\d{9} [a-z|A-Z|0-9| |,|.]++";

        if (arr.matches(messagejudge)) {
            return true;
        } else {
            return false;
        }
    }
}
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 userRecords);

    public abstract double getMonthlyRent();
}
abstract class ChargeRule {

}
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 MessageCharging extends ChargeMode {

    public MessageCharging() {
        getChargeRules().add(new SendMessageRule());
    }

    @Override
    public double calCost(UserRecords userRecords) {
        double cost = 0;
        cost += ((SendMessageRule) getChargeRules().get(0)).calCost(userRecords.getSendMessageRecords());
        return cost;
    }

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

abstract class MessageChargingRule extends ChargeRule{
    abstract double calCost(ArrayList<MessageRecord> messageRecords);
}
class MessageRecord extends CommunicationRecord {
    private String message;

    public MessageRecord() {
    }

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

    public String getMessage() {
        return message;
    }

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

}
class SendMessageRule extends MessageChargingRule {
    @Override
    public double calCost(ArrayList<MessageRecord> messageRecords) {
        double cost = 0;
        int cnt = 0;
        for (MessageRecord messageRecord : messageRecords) {
            if (messageRecord.getMessage().length() % 10 != 0) {
                cnt += messageRecord.getMessage().length() / 10 + 1;
            } else {
                cnt += messageRecord.getMessage().length() / 10;
            }
        }
        if (cnt > 5) {
            cost = 0.1 * 3 + 0.2 * 2 + (cnt - 5) * 0.3;
        } else if (cnt > 3) {
            cost = 0.1 * 3 + 0.2 * (cnt - 3);
        } else {
            cost = 0.1 * cnt;
        }
        return cost;
    }
}
class User {
    private UserRecords userRecords = new UserRecords();
    private double balance = 100;
    private ChargeMode chargeMode;
    private String number;

    public User() {
    }

    public User(ChargeMode chargeMode, String number) {
        this.chargeMode = chargeMode;
        this.number = number;
    }

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

    public double calCost() {
        return chargeMode.calCost(userRecords);
    }

    public UserRecords getUserRecords() {
        return userRecords;
    }

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

    public double getBalance() {
        return balance;
    }

    public ChargeMode getChargeMode() {
        return chargeMode;
    }

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

    public String getNumber() {
        return number;
    }

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

}
class UserRecords {

    private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();//傳送簡訊電話
    private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();//接受簡訊電話

    public UserRecords() {
    }


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

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

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

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

類圖:

  • 本次新增了簡訊計費類,增加一種計費方式即可
  • 該次題目的難度主要在於格式判斷,利用正則表示式進行判斷即可
  • 格式判斷完畢後進行拆分,從而解決問題

踩坑心得

  • 1.起初未用正則表示式進行判斷,導致很多格式錯誤
  • 2.計費的判斷方式沒有設計好,導致無法進行計算
  • 3.在類中很多地方的屬性沒有進行初始化,導致出現很多錯誤
  • 4.在第三題中,沒有考慮到月租費用,導致計算錯誤
  • 5.在第三題中,沒有考慮到利用字串切割函式時,會將資訊也切割,導致錯誤

改進建議

  • 1.每次在加入屬性的時候,都要進行初始化,防止出現錯誤
  • 2.在每次進行計算的時候,都要進行判斷,防止出現錯誤
  • 3.在每次對一個題目動手的時候,都要先分析題目的類設計,並且為之後的迭代做好準備

總結

  • 在本階段的學習中,我們做了一個關於客房的案例,這個案例用到了之前學習到的很多知識,並且讓我們的知識理解更加深入,更加完善。在這段時間,我們也學習了javafx,並且進行了圖形介面的開發。
  • 在我看來,我覺得我們需要更加深入的學習,現在學習到的很多知識都比較淺,並且由於這段時間的考試較多,沒有太多的時間去學習java,所以導致這段時間的知識掌握不是很牢固,應該加強對這段時間所學習到的知識的訓練
  • 對於老師這段時間的教學,我覺得這種方式確實更能提高我們學生各個方面的能力。