1. 程式人生 > 其它 >pta第三次部落格

pta第三次部落格

目錄

pta第三次部落格

1.前言

本次部落格主要分析電信計費的三個作業,相較於之前的多邊形,這三次作業相對簡單一點,主要的難點還是理解老師給出的幾張類圖,分析起來很麻煩,要花費的時間比真正寫程式碼的時間還要長,因為裡面的繼承關係很多很雜,經常寫到一半又忘了這個類的繼承物件。其次就是正則表示式,因為輸入的規範要求很嚴格,就導致正則經常滿足了這一條又不滿足另一條,而且改起來很麻煩。

2.設計與分析

第6次作業第一題

1.題目:
7-1 電信計費系列1-座機計費
實現一個簡單的電信計費程式:
假設南昌市電信分公司針對市內座機使用者採用的計費方式:
月租20元,接電話免費,市內撥打電話0.1元/分鐘,省內長途0.3元/分鐘,國內長途撥打0.6元/分鐘。不足一分鐘按一分鐘計。
南昌市的區號:0791,江西省內各地市區號包括:0790~0799以及0701。

輸入格式:
輸入資訊包括兩種型別
1、逐行輸入南昌市使用者開戶的資訊,每行一個使用者,
格式:u-號碼 計費型別 (計費型別包括:0-座機 1-手機實時計費 2-手機A套餐)
例如:u-079186300001 0
座機號碼除區號外由是7-8位數字組成。
本題只考慮計費型別0-座機計費,電信系列2、3題會逐步增加計費型別。
2、逐行輸入本月某些使用者的通訊資訊,通訊資訊格式:
座機呼叫座機:t-主叫號碼 接聽號碼 起始時間 結束時間
t-079186330022 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:11
以上四項內容之間以一個英文空格分隔,
時間必須符合"yyyy.MM.dd HH:mm:ss"格式。提示:使用SimpleDateFormat類。
以上兩類資訊,先輸入所有開戶資訊,再輸入所有通訊資訊,最後一行以“end”結束。
注意:
本題非法輸入只做格式非法的判斷,不做內容是否合理的判斷(時間除外,否則無法計算),比如:
1、輸入的所有通訊資訊均認為是同一個月的通訊資訊,不做日期是否在同一個月還是多個月的判定,直接將通訊費用累加,因此月租只計算一次。
2、記錄中如果同一電話號碼的多條通話記錄時間出現重合,這種情況也不做判斷,直接 計算每條記錄的費用並累加。
3、使用者區號不為南昌市的區號也作為正常使用者處理。

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

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

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

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

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

UserRecords是使用者記錄類,儲存使用者各種通話、簡訊的記錄,
各種計費規則將使用其中的部分或者全部記錄。
其屬性從上到下依次是:
市內撥打電話、省內(不含市內)撥打電話、省外撥打電話、
市內接聽電話、省內(不含市內)接聽電話、省外接聽電話的記錄
以及傳送簡訊、接收簡訊的記錄。

圖2中CommunicationRecord是抽象的通訊記錄類:
包含callingNumber撥打號碼、answerNumber接聽號碼兩個屬性。
CallRecord(通話記錄)、MessageRecord(簡訊記錄)是它的子類。

CallRecord(通話記錄類)包含屬性:
通話的起始、結束時間以及
撥號地點的區號(callingAddressAreaCode)、接聽地點的區號(answerAddressAreaCode)。
區號用於記錄在哪個地點撥打和接聽的電話。座機無法移動,就是本機區號,如果是手機號,則會有差異。

圖3是計費規則的相關類,這些類的核心方法是:
calCost(ArrayList callRecords)。
該方法針根據輸入引數callRecords中的所有記錄計算某使用者的某一項費用;如市話費。
輸入引數callRecords的約束條件:必須是某一個使用者的符合計費規則要求的所有記錄。

LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三個類分別是
座機撥打市內、省內、省外電話的計費規則類,用於實現這三種情況的費用計算。
(提示:可以從UserRecords類中獲取各種型別的callRecords)。
後續擴充套件說明:
後續題目集將增加手機使用者,手機使用者的計費方式中除了與座機計費類似的主叫通話費之外,還包含市外接聽電話的漫遊費以及發簡訊的費用。在本題的設計時可統一考慮。
通話記錄中,手機需要額外記錄撥打/接聽的地點的區號,比如:
座機打手機:t-主叫號碼 接聽號碼 接聽地點區號 起始時間 結束時間
t-079186330022 13305862264 020 2022.1.3 10:00:25 2022.1.3 10:05:11
手機互打:t-主叫號碼 撥號地點 接聽號碼 接聽地點區號 起始時間 結束時間
t-18907910010 0791 13305862264 0371 2022.1.3 10:00:25 2022.1.3 10:05:11
簡訊的格式:m-主叫號碼,接收號碼,簡訊內容
m-18907910010 13305862264 welcome to jiangxi
m-13305862264 18907910010 thank you
試題分析
1.本題需要根據給出的類圖來確定各個主要的類的屬性以及各個類之間的繼承關係
2.需要通過正則表示式來判斷輸入的開戶資訊和通話記錄是否符合格式要求
3.本題只要考慮南昌的座機打完市內,省內和省外的座機的通話記錄來計費和月租來計算總費用
原始碼展示:

檢視程式碼
import java.util.Comparator;
import java.util.ArrayList;
import java.util.Scanner;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.text.ParseException;

public class Main {
    public static void main(String[] args) {
        ArrayList<User> users = new ArrayList<>();
        Input inputdeal = new Input();
        Reduce e=new Reduce();
        Scanner in = new Scanner(System.in);
        String input= in.nextLine();
        while(!input.equals("end")){
            if(input.matches("[u]-[0][7]([9][0-9]|[0][1])[0-9]{7,8} [0-3]") || input.matches("^t-0\\d{9,11}\\s0\\d{9,11}((\\s\\d{4}\\.([1-9]|([1]{1}[0-2]{1}))\\.([1-9]|([1-2]{1}[0-9]{1})|3[0-1])\\s(([0-1][0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])){2})")){
                String[] line=input.split(" ");
                if(line.length==2){
                    inputdeal.writeUser(users, input);
                }
                if(line.length==6){
                    inputdeal.writeRecord(users, input);
                }
            }
            input= in.nextLine();
        }
        users.sort(new Comparator<User>() {
            @Override
            public int compare(User u1, User u2) {
                if (Double.parseDouble(u1.getNumber()) > Double.parseDouble(u2.getNumber())) {
                    return 1;
                } else {
                    return -1;
                }
            }
        });

        for (User u : users) {
            System.out.println(u.getNumber() + " "+e.exchange(u.calCost())+ " "+e.exchange(u.calBalance()));
        }
    }
}
class Input {
    public void writeUser(ArrayList<User> users, String input) {
        User newuser = new User();
        String[] inputs = input.split(" ");
        String num = inputs[0].substring(2);
        for (int i=0;i<users.size();i++) {
            User user=users.get(i);
            if (user.getNumber().equals(num)) {
                return;
            }
        }
        newuser.setNumber(num);
        int mode = Integer.parseInt(inputs[1]);
        if (mode == 0) {
            newuser.setChargeMode(new LandlinePhoneCharging());
        }
        users.add(newuser);
    }

    public void writeRecord(ArrayList<User> users, String input) {
        String[] inputs = input.split(" ");
        inputs[0] = inputs[0].replace("t-", "");
        User call = null, answer = null;
        CallRecord callrecord = new CallRecord(inputs);

        for (int i=0;i<users.size();i++) {
            User user=users.get(i);
            if (user.getNumber().equals(inputs[0])) {
                call = user;
            }
            if (user.getNumber().equals(inputs[1])) {
                answer= user;
            }
            if (call != null && answer != null) {
                break;
            }
        }

        if (call != null) {
            if (callrecord.getCallType() == 1) {
                call.getUserRecords().addCallingInCityRecords(callrecord);
            } else if (callrecord.getCallType() == 2) {
                call.getUserRecords().addCallingInProvinceRecords(callrecord);
            } else {
                call.getUserRecords().addCallingInLandRecords(callrecord);
            }
        }

        if (answer != null) {
            if (callrecord.getCallType() == 1) {
                answer.getUserRecords().addAnswerInCityRecords(callrecord);
            } else if (callrecord.getCallType() == 2) {
                answer.getUserRecords().addAnswerInProvinceRecords(callrecord);
            } else {
                answer.getUserRecords().addAnswerInLandRecords(callrecord);
            }
        }

    }

}
class User {

    private UserRecords userRecords = new UserRecords();
    private double balance = 100;
    private ChargeMode chargeMode;
    private String number;

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

    public double calBalance() {
        return balance - chargeMode.getMonthlyRent() - chargeMode.calCost(userRecords);
    }

    public UserRecords getUserRecords() {
        return userRecords;
    }

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

    public ChargeMode getChargeMode() {
        return chargeMode;
    }

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

    public String getNumber() {
        return number;
    }

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

}
class UserRecords {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    public String getCallingNumber() {return callingNumber;}

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

    public String getAnswerNumbe() {return answerNumbe;}

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

class MessageRecord extends CommunicationRecord {

    private String message;

    public String getMessage() {return message;}

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

abstract class ChargeMode {
    protected ArrayList<ChargeRule> chargeRules = new ArrayList<>();

    public abstract double calCost(UserRecords userRecords);

    public abstract double getMonthlyRent();

    public ArrayList<ChargeRule> getChargeRules() {return chargeRules;}

    public void setChargeRules(ArrayList<ChargeRule> chargeRules) {this.chargeRules = chargeRules;}
}

abstract class CallChargeRule extends ChargeRule {

}

abstract class ChargeRule {
    abstract public double calCost(UserRecords userRecords);
}

class LandlinePhoneCharging extends ChargeMode {
    private double monthlyRent = 20;

    LandlinePhoneCharging() {
        chargeRules.add(new LandPhoneInCityRule());
        chargeRules.add(new LandPhoneInProvinceRule());
        chargeRules.add(new LandPhoneInlandRule());
    }

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

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

}
class LandPhoneInCityRule extends CallChargeRule {

    @Override
    public double calCost(UserRecords userRecords) {
        double sum=0;
        for (CallRecord call : userRecords.getCallingInCityRecords()) {
            double S = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (S < 0) {
                continue;
            }
            double M = (int) S / 60;
            if (S % 60 != 0) {
                M += 1;
            }
            sum += M * 0.1;
        }
        return sum;
    }
}
class LandPhoneInProvinceRule extends CallChargeRule {

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

class LandPhoneInlandRule extends CallChargeRule {
    @Override
    public double calCost(UserRecords userRecords) {
        double sum=0,S,M;
        for (CallRecord call : userRecords.getCallingInLandRecords()) {
            S = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (S < 0) {
                continue;
            }
            M = (int) S / 60;
            if (S % 60 != 0) {
                M += 1;
            }
            sum += M * 0.6;
        }
        return sum;
    }
}

class CallRecord extends CommunicationRecord {
    private Date startTime;
    private Date endTime;
    private String callingAddressAreaCode;
    private String answerAddressAreaCode;
    public int getCallType() {
        if (callingAddressAreaCode.equals(answerAddressAreaCode)) {
            return 1;
        }
        if (callingAddressAreaCode.matches("^079[0-9]$") || callingAddressAreaCode.equals("0701")) {
            if (answerAddressAreaCode.matches("^079[0-9]$") || answerAddressAreaCode.equals("0701")) {
                return 2;
            }
        }
        return 3;
    }
    public CallRecord(String[] input){
        if(input[0].length()==10){
            callingAddressAreaCode = input[0].substring(0, 3);
        } else {
            callingAddressAreaCode = input[0].substring(0, 4);
        }
        if (input[1].length() == 10) {
            answerAddressAreaCode = input[1].substring(0, 3);
        } else {
            answerAddressAreaCode = input[1].substring(0, 4);
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault());
        try {
            startTime = simpleDateFormat.parse(input[2] + " " + input[3]);
            endTime = simpleDateFormat.parse(input[4] + " " + input[5]);
        } catch (ParseException e) {
        }
    }

    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 Reduce{
    public double exchange(double a){
        String c=String.format("%."+2+"f",a);a= Double.parseDouble(c);
        return a;
    }
}

SourceMonitor生成的報表內容:

類圖

程式碼分析總結:
總體來說,本題只要先寫出正則表示式,再按照題目給出的類圖寫好大部分的類,再將三種計費型別的計費計算演算法寫好,大致就結束了。

第6次作業第二題

題目:
7-2 多型測試
定義容器Container介面。模擬實現一個容器類層次結構,並進行介面的實現、抽象方法重寫和多型機制測試。各容器類實現求表面積、體積的方法。

定義介面Container:
屬性:
public static final double pi=3.1415926;
抽象方法:
public abstract double area();
public abstract double volume();
static double sumofArea(Container c[]);
static double sumofVolume(Container c[]);
其中兩個靜態方法分別計算返回容器陣列中所有物件的面積之和、周長之和;
定義Cube類、Cylinder類均實現自Container介面。
Cube類(屬性:邊長double型別)、Cylinder類(屬性:底圓半徑、高,double型別)。

輸入格式:

第一行n表示物件個數,物件型別用cube、cylinder區分,cube表示立方體物件,後面輸入邊長,輸入cylinder表示圓柱體物件,後面是底圓半徑、高。
輸出格式:
分別輸出所有容器物件的表面積之和、體積之和,結果保留小數點後2位。
試題分析
本題考查對多型的理解,根據題目要求寫好類和繼承關係就行
原始碼展示:

檢視程式碼
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int i,n=in.nextInt();
        Container[] sum=new Container[n];
        for(i=0;i<n;i++){
            String input = in.next();
            if(input.equals("cube")){
                double a=in.nextDouble();
                sum[i]=new Cube(a);
            }
            if(input.equals("cylinder")){
                double r=in.nextDouble();
                double h=in.nextDouble();
                sum[i]=new Cylinder(r,h);
            }
        }

        System.out.printf("%.2f\n%.2f",Container.sumofArea(sum),Container.sumofVolume(sum));
    }
}

abstract class Container {
    public static final double pi=3.1415926;
    public abstract double area();
    public abstract double volume();

    static double sumofArea(Container[] c) {
        int i;double S=0;
        for(i=0;i<c.length;i++){
            S+=c[i].area();
        }
        return S;
    }

    static double sumofVolume(Container[] c) {
        int i;double V=0;
        for(i=0;i<c.length;i++){
            V+=c[i].volume();
        }
        return V;
    }
}
class Cube extends Container{
    double a;
    Cube(double a){
        this.a=a;
    }
    @Override
    public double volume() {
        return Math.pow(this.a,3);
    }

    @Override
    public double area() {
        return 6*Math.pow(this.a,2);
    }
}
class Cylinder extends Container{
    double r,h;
Cylinder(double r,double h){
        this.r=r;
        this.h=h;
    }
    @Override
    public double volume() {
        return Math.pow(this.r,2)* this.pi* this.h;
    }

    @Override
    public double area() {
        return 2* this.pi*Math.pow(this.r,2)+2* this.pi* this.r* this.h;
    }
}



SourceMonitor生成的報表內容:

類圖

程式碼分析總結:
本題難度不高,確定好繼承關係就行

第七次作業第一題

題目:
電信計費系列2-手機+座機計費
實現南昌市電信分公司的計費程式,假設該公司針對手機和座機使用者分別採取了兩種計費方案,分別如下:
1、針對市內座機使用者採用的計費方式(與電信計費系列1內容相同):
月租20元,接電話免費,市內撥打電話0.1元/分鐘,省內長途0.3元/分鐘,國內長途撥打0.6元/分鐘。不足一分鐘按一分鐘計。
假設本市的區號:0791,江西省內各地市區號包括:0790~0799以及0701。
2、針對手機使用者採用實時計費方式:
月租15元,市內省內接電話均免費,市內撥打市內電話0.1元/分鐘,市內撥打省內電話0.2元/分鐘,市內撥打省外電話0.3元/分鐘,省內漫遊打電話0.3元/分鐘,省外漫遊接聽0.3元/分鐘,省外漫遊撥打0.6元/分鐘;
注:被叫電話屬於市內、省內還是國內由被叫電話的接聽地點區號決定,比如以下案例中,南昌市手機使用者13307912264在區號為020的廣州接聽了電話,主叫號碼應被計算為撥打了一個省外長途,同時,手機使用者13307912264也要被計算省外接聽漫遊費:
u-13307912264 1
t-079186330022 13307912264 020 2022.1.3 10:00:25 2022.1.3 10:05:11
輸入格式:
輸入資訊包括兩種型別
1、逐行輸入南昌市使用者開戶的資訊,每行一個使用者,含手機和座機使用者
格式:u-號碼 計費型別 (計費型別包括:0-座機 1-手機實時計費 2-手機A套餐)
例如:u-079186300001 0
座機號碼由區號和電話號碼拼接而成,電話號碼包含7-8位數字,區號最高位是0。
手機號碼由11位數字構成,最高位是1。
本題在電信計費系列1基礎上增加型別1-手機實時計費。
手機設定0或者座機設定成1,此種錯誤可不做判斷。
2、逐行輸入本月某些使用者的通訊資訊,通訊資訊格式:
座機呼叫座機:t-主叫號碼 接聽號碼 起始時間 結束時間
t-079186330022 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:11
以上四項內容之間以一個英文空格分隔,
時間必須符合"yyyy.MM.dd HH:mm:ss"格式。提示:使用SimpleDateFormat類。
輸入格式增加手機接打電話以及收發簡訊的格式,手機接打電話的資訊除了號碼之外需要額外記錄撥打/接聽的地點的區號,比如:
座機打手機:
t-主叫號碼 接聽號碼 接聽地點區號 起始時間 結束時間
t-079186330022 13305862264 020 2022.1.3 10:00:25 2022.1.3 10:05:11
手機互打:
t-主叫號碼 撥號地點 接聽號碼 接聽地點區號 起始時間 結束時間
t-18907910010 0791 13305862264 0371 2022.1.3 10:00:25 2022.1.3 10:05:11

注意:以上兩類資訊,先輸入所有開戶資訊,再輸入所有通訊資訊,最後一行以“end”結束。

輸出格式:
根據輸入的詳細通訊資訊,計算所有已開戶的使用者的當月費用(精確到小數點後2位,單位元)。假設每個使用者初始餘額是100元。
每條通訊、簡訊資訊均單獨計費後累加,不是將所有資訊累計後統一計費。
格式:號碼+英文空格符+總的話費+英文空格符+餘額
每個使用者一行,使用者之間按號碼字元從小到大排序。
錯誤處理:
輸入資料中出現的不符合格式要求的行一律忽略。

本題只做格式的錯誤判斷,無需做內容上不合理的判斷,比如同一個電話兩條通訊記錄的時間有重合、開戶號碼非南昌市的號碼等,此類情況都當成正確的輸入計算。但時間的輸入必須符合要求,比如不能輸入2022.13.61 28:72:65。
建議類圖:
參見圖1、2、3:

圖1中User是使用者類,包括屬性:
userRecords (使用者記錄)、balance(餘額)、chargeMode(計費方式)、number(號碼)。
ChargeMode是計費方式的抽象類:
chargeRules是計費方式所包含的各種計費規則的集合,ChargeRule類的定義見圖3。
getMonthlyRent()方法用於返回月租(monthlyRent)。
UserRecords是使用者記錄類,儲存使用者各種通話、簡訊的記錄,
各種計費規則將使用其中的部分或者全部記錄。
其屬性從上到下依次是:
市內撥打電話、省內(不含市內)撥打電話、省外撥打電話、
市內接聽電話、省內(不含市內)接聽電話、省外接聽電話的記錄
以及傳送簡訊、接收簡訊的記錄。

圖2中CommunicationRecord是抽象的通訊記錄類:
包含callingNumber撥打號碼、answerNumber接聽號碼兩個屬性。
CallRecord(通話記錄)、MessageRecord(簡訊記錄)是它的子類。CallRecord(通話記錄類)包含屬性:
通話的起始、結束時間以及
撥號地點的區號(callingAddressAreaCode)、接聽地點的區號(answerAddressAreaCode)。
區號用於記錄在哪個地點撥打和接聽的電話。座機無法移動,就是本機區號,如果是手機號,則會有差異。

圖3是計費規則的相關類,這些類的核心方法是:
calCost(ArrayList callRecords)。
該方法針根據輸入引數callRecords中的所有記錄計算某使用者的某一項費用;如市話費。
輸入引數callRecords的約束條件:必須是某一個使用者的符合計費規則要求的所有記錄。
SendMessageRule是傳送簡訊的計費規則類,用於計算髮送簡訊的費用。
LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三個類分別是座機撥打市內、省內、省外電話的計費規則類,用於實現這三種情況的費用計算。
(提示:可以從UserRecords類中獲取各種型別的callRecords)。
注意:以上圖中所定義的類不是限定要求,根據實際需要自行補充或修改。
試題分析
本題是在上一題的基礎上加入了行動電話,所以需要在上一題的程式碼的基礎上加入行動電話與座機,行動電話與行動電話以及更多的通話型別如省內的戶主在省外向省內打電話之類等更復雜的情況。
原始碼展示:

檢視程式碼

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


public class Main {
    public static void main(String[] args) {
        ArrayList<User> users = new ArrayList<>();
        Input inputdeal = new Input();
        Reduce e=new Reduce();
        Scanner in = new Scanner(System.in);
        String input= in.nextLine();
        while(!input.equals("end")){
            if(input.matches("[u]-([0][7]([9][0-9]|[0][1])[0-9]{7,8}) [0-3]")
                    ||input.matches("[u]-[1][0-9]{10} [0-3]")
                    ||input.matches("(([t]-0791[0-9]{7,8}\\s0[0-9]{9,11})|([t]-0791[0-9]{7,8}\\s1[0-9]{10}\\s0[0-9]{2,3})|([t]-1[0-9]{10}\\s0[0-9]{2,3}\\s0[0-9]{9,11})|([t]-1[0-9]{10}\\s0[0-9]{2,3}\\s1[0-9]{10}\\s0[0-9]{2,3}))(\\s\\d{4}\\.([1-9]|([1]{1}[0-2]{1}))\\.([1-9]|([1-2]{1}[0-9]{1})|3[0-1])\\s(([0-1][0-9])|(2[0-3]))\\:([0-5][0-9])\\:([0-5][0-9])){2}")==true)

            {String[] line=input.split(" ");
                if(line.length==2){
                    inputdeal.writeUser(users, input);
                }
                if(line.length==6||line.length==7||line.length==8){
                    inputdeal.writeRecord(users, input);
                }
            }
            input= in.nextLine();
        }
        users.sort(new Comparator<User>() {

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

        for (User u : users) {
            //System.out.println(u.getUserRecords().getCallingInCityRecords().size());
            System.out.println(u.getNumber() + " "+e.exchange(u.calCost())+ " "+e.exchange(u.calBalance()));
        }
    }
}
class Input {
    public void writeUser(ArrayList<User> users, String input) {
        User newuser = new User();
        String[] inputs = input.split(" ");
        String num = inputs[0].substring(2);
        for (int i=0;i<users.size();i++) {
            User user=users.get(i);
            if (user.getNumber().equals(num)) {
                return;
            }
        }
        newuser.setNumber(num);
        int mode = Integer.parseInt(inputs[1]);
        if (mode == 0) {
            newuser.setChargeMode(new LandlinePhoneCharging());
        } else if (mode ==1) {
            newuser.setChargeMode(new MobilePhoneCharging());
        }
        users.add(newuser);
    }

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

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

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

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

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

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

        }

    }

}

class User {

    private UserRecords userRecords = new UserRecords();
    private double balance = 100;
    private ChargeMode chargeMode;
    private String number;

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

    public double calBalance() {
        return balance - chargeMode.getMonthlyRent() - chargeMode.calCost(userRecords);
    }

    public UserRecords getUserRecords() {
        return userRecords;
    }

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

    public ChargeMode getChargeMode() {
        return chargeMode;
    }

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

    public String getNumber() {
        return number;
    }

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

}
class UserRecords {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    public String getCallingNumber() {return callingNumber;}

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

    public String getAnswerNumbe() {return answerNumbe;}

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

class MessageRecord extends CommunicationRecord {

    private String message;

    public String getMessage() {return message;}

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

abstract class ChargeMode {
    protected ArrayList<ChargeRule> chargeRules = new ArrayList<>();

    public abstract double calCost(UserRecords userRecords);

    public abstract double getMonthlyRent();

    public ArrayList<ChargeRule> getChargeRules() {return chargeRules;}

    public void setChargeRules(ArrayList<ChargeRule> chargeRules) {this.chargeRules = chargeRules;}
}

abstract class CallChargeRule extends ChargeRule {

}

abstract class ChargeRule {
    abstract public double calCost(UserRecords userRecords);
}

class LandlinePhoneCharging extends ChargeMode {
    private double monthlyRent = 20;

    LandlinePhoneCharging() {
        chargeRules.add(new LandPhoneInCityRule());
        chargeRules.add(new LandPhoneInProvinceRule());
        chargeRules.add(new LandPhoneInlandRule());
    }

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

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

}
class LandPhoneInCityRule extends CallChargeRule {

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

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

class LandPhoneInlandRule extends CallChargeRule {
    @Override
    public double calCost(UserRecords userRecords) {
        double sum=0,S,M;
        for (CallRecord call : userRecords.getCallingInLandRecords()) {
            S = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (S < 0) {
                continue;
            }
            M = (int) S / 60;
            if (S % 60 != 0) {
                M += 1;
            }
            sum += M * 0.6;
        }
        return sum;
    }
}


class MobilePhoneCharging extends ChargeMode {

    private double monthlyRent = 15;

    public MobilePhoneCharging() {
        super();
        chargeRules.add(new MobilePhoneInCityRule());
        chargeRules.add(new MobilePhoneInProvinceRule());
        chargeRules.add(new MobilePhoneInlandRule());
    }

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

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

}

class MobilePhoneInCityRule extends CallChargeRule {

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

class MobilePhoneInProvinceRule extends CallChargeRule {

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

        return sum;
    }
}

class MobilePhoneInlandRule extends CallChargeRule {
    @Override
    public double calCost(UserRecords userRecords) {
        double sum=0,S,M;
        for (CallRecord call : userRecords.getCallingInLandRecords()) {
            S = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (S < 0) {
                continue;
            }
            M = (int) S / 60;
            if (S % 60 != 0) {
                M += 1;
            }
            sum += M * 0.6;
        }
        for (CallRecord call : userRecords.getAnswerInLandRecords()) {
            S = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (S < 0) {
                continue;
            }
            M = (int) S / 60;
            if (S % 60 != 0) {
                M += 1;
            }
            sum += M * 0.3;
        }
        //System.out.println("C "+sum);
        return sum;
    }
}

class CallRecord extends CommunicationRecord {
    private Date startTime;
    private Date endTime;
    private String callingAddressAreaCode;
    private String answerAddressAreaCode;
    public String getCallType() {
        String type = "";
        if (callingAddressAreaCode.equals("0791")) {
            type = type.concat("1");
        } else if (callingAddressAreaCode.matches("^079[023456789]$") || callingAddressAreaCode.equals("0701")) {
            type = type.concat("2");
        } else {
            type = type.concat("3");
        }

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

        return type;
    }
    public CallRecord(String[] inputs) {
        super();
        char type = inputs[0].charAt(0);
        inputs[0] = inputs[0].substring(2);
        String sd = null, st = null, ed = null, et = null;

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

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

    }

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

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

    public Date getEndTime() {
        return endTime;
    }

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

    public String getCallingAddressAreaCode() {
        return callingAddressAreaCode;
    }

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

    public String getAnswerAddressAreaCode() {
        return answerAddressAreaCode;
    }

    public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
        this.answerAddressAreaCode = answerAddressAreaCode;
    }
}
class Reduce{
    public double exchange(double a){
        String c=String.format("%."+2+"f",a);a= Double.parseDouble(c);
        return a;
    }
}


SourceMonitor生成的報表內容:

類圖

程式碼分析總結:
本題只要理清撥號使用者與接聽使用者的位置,同時不僅要計算撥號使用者的計費,還要計算漫遊接聽使用者的費用,如果上一次的程式碼夠全面的話,這次只要加上多出的幾種計費演算法即可。

第七次作業第二題

題目:
sdut-Collection-sort--C~K的班級(II)
經過不懈的努力,C~K終於當上了班主任。

現在他要統計班裡學生的名單,但是C~K在教務系統中匯出班級名單時出了問題,發現會有同學的資訊重複,現在他想把重複的同學資訊刪掉,只保留一個,
但是工作量太大了,所以找到了會程式設計的你,你能幫他解決這個問題嗎?

輸入格式:
第一行輸入一個N,代表C~K匯出的名單共有N行(N<100000).

接下來的N行,每一行包括一個同學的資訊,學號 姓名 年齡 性別。

輸出格式:
第一行輸出一個n,代表刪除重複名字後C~K的班級共有幾人。

接下來的n行,輸出每一個同學的資訊,輸出按照學號從小到大的順序。
試題分析
本題需要定義一個學生類,再將學生類存入一個list裡,再去除list中重複的學生資訊,然後排序輸出即可
原始碼展示:

檢視程式碼
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        ArrayList<String> stu=new ArrayList<>();
        Scanner in = new Scanner(System.in);
        int n=in.nextInt();
        in.nextLine();
        stu.add(in.nextLine());
        for(int i=0;i<n-1;i++){
            String s=in.nextLine();
            for(int j=0;j<stu.size();j++) {
                if(s.equals(stu.get(j)))
                    break;
                else if(j==stu.size()-1){
                    stu.add(s);
                }
            }
        }
        ArrayList<Student> student=new ArrayList<>();
        for(int j=0;j<stu.size();j++) {
            String s[];
            s=stu.get(j).split(" ");
            //System.out.println(s[0]+s[1]+s[2]+s[3]);
            student.add(new Student(s[0],s[1],s[2],s[3])) ;
        }
        Collections.sort(student,new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getSno().compareTo(o2.getSno());
            }
        });
        System.out.println(student.size());
        for(int j=0;j<student.size();j++) {
            System.out.println(student.get(j).getSno()+" "+student.get(j).Sna+" "+student.get(j).age+" "+student.get(j).sex);
        }
    }
}
class Student {
    
    String Sno;
    String Sna;
    String age;
    String sex;
    public String getSno() {
        return Sno;
    }

    public void setSno(String sno) {
        Sno = sno;
    }
    Student(String Sno,String Sna,String age,String sex){
        this.age=age;
        this.sex=sex;
        this.Sna=Sna;
        this.Sno=Sno;
    }

}
/*
學號 姓名 年齡 性別
 */


SourceMonitor生成的報表內容:

類圖

程式碼分析總結:
本題只要稍微分析一下就行,難度不高。

第七次作業第三題

題目:
閱讀程式,按照題目需求修改程式
功能需求:
使用集合儲存3個員工的資訊(有序);
通過迭代器依次找出所有的員工。
提示:學生複製以下程式碼到程式設計區,並按需求進行除錯修改。

// 1、匯入相關包

//定義員工類
class Employee {

private String name;
private int age;

public Employee() {
	super();
}

public Employee(String name, int age) {
	super();
	this.name = name;
	this.age = age;
}

public String getName() {
	return name;
}

public void setName(String name) {
	this.name = name;
}

public int getAge() {
	return age;
}

public void setAge(int age) {
	this.age = age;
}

}

//主函式
public class Main {

public static void main(String[] args) {
			// 1、建立有序集合物件
			Collection c ;

  // 建立3個員工元素物件
	for (int i = 0; i < 3; i++) {
		Scanner sc = new Scanner(System.in);
		String employeeName = sc.nextLine();
		int employeeAge = sc.nextInt();
		
		Employee employee = new Employee(employeeName, employeeAge);
		c.add(employee);
	}			
			
			
			
			// 2、建立迭代器遍歷集合
			Iterator it;
			
			//3、遍歷
			while (it.hasnext) {
				
				//4、集合中物件未知,向下轉型
				Employee e =  it.next();
				
				System.out.println(e.getName() + "---" + e.getAge());
			}
}

}

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

zs
10
ls
20
ww
30

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

zs---10
ls---20
ww---30
試題分析
本題已經給出了程式碼,只要建立有序集合物件和建立迭代器遍歷集合就行
原始碼展示:

檢視程式碼


import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Scanner;

// 1、匯入相關包
//主函式
public class Main {

    public static void main(String[] args) {
        // 1、建立有序集合物件
        Collection<Employee> c = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        // 建立3個員工元素物件
        for (int i = 0; i < 3; i++) {
            String employeeName = sc.next().replace(" ","");
            int employeeAge = sc.nextInt();
            Employee employee = new Employee(employeeName, employeeAge);
            c.add(employee);
        }

        // 2、建立迭代器遍歷集合
        Iterator<Employee> it = c.iterator();

        //3、遍歷
        while (it.hasNext()) {
            //4、集合中物件未知,向下轉型
            Employee e =  it.next();
            System.out.println(e.getName() + "---" + e.getAge());
        }
    }

}

//定義員工類
class Employee {

    private String name;
    private int age;

    public Employee() {
        super();
    }

    public Employee(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}




SourceMonitor生成的報表內容:

類圖

程式碼分析總結:
本題基本沒有難度,不做分析

第八次作業第一題

題目:
電信計費系列3-簡訊計費
實現一個簡單的電信計費程式,針對手機的簡訊採用如下計費方式:
1、接收簡訊免費,傳送簡訊0.1元/條,超過3條0.2元/條,超過5條0.3元/條。
2、如果一次傳送簡訊的字元數量超過10個,按每10個字元一條簡訊進行計算。

輸入格式:
輸入資訊包括兩種型別
1、逐行輸入南昌市手機使用者開戶的資訊,每行一個使用者。
格式:u-號碼 計費型別 (計費型別包括:0-座機 1-手機實時計費 2-手機A套餐 3-手機簡訊計費)
例如:u-13305862264 3
座機號碼由區號和電話號碼拼接而成,電話號碼包含7-8位數字,區號最高位是0。
手機號碼由11位數字構成,最高位是1。
本題只針對型別3-手機簡訊計費。
2、逐行輸入本月某些使用者的簡訊資訊,簡訊的格式:
m-主叫號碼,接收號碼,簡訊內容 (簡訊內容只能由數字、字母、空格、英文逗號、英文句號組成)
m-18907910010 13305862264 welcome to jiangxi.
m-13305862264 18907910010 thank you.

注意:以上兩類資訊,先輸入所有開戶資訊,再輸入所有通訊資訊,最後一行以“end”結束。

輸出格式:
根據輸入的詳細簡訊資訊,計算所有已開戶的使用者的當月簡訊費用(精確到小數點後2位,單位元)。假設每個使用者初始餘額是100元。
每條簡訊資訊均單獨計費後累加,不是將所有資訊累計後統一計費。
格式:號碼+英文空格符+總的話費+英文空格符+餘額
每個使用者一行,使用者之間按號碼字元從小到大排序。
錯誤處理:
輸入資料中出現的不符合格式要求的行一律忽略。
本題只做格式的錯誤判斷,無需做內容上不合理的判斷,比如同一個電話兩條通訊記錄的時間有重合、開戶號碼非南昌市的號碼、自己給自己打電話等,此類情況都當成正確的輸入計算。但時間的輸入必須符合要求,比如不能輸入2022.13.61 28:72:65。

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

建議類圖:
參見圖1、2、3:

圖1

圖1中User是使用者類,包括屬性:
userRecords (使用者記錄)、balance(餘額)、chargeMode(計費方式)、number(號碼)。
ChargeMode是計費方式的抽象類:
chargeRules是計費方式所包含的各種計費規則的集合,ChargeRule類的定義見圖3。
getMonthlyRent()方法用於返回月租(monthlyRent)。
UserRecords是使用者記錄類,儲存使用者各種通話、簡訊的記錄,
各種計費規則將使用其中的部分或者全部記錄。
其屬性從上到下依次是:
市內撥打電話、省內(不含市內)撥打電話、省外撥打電話、
市內接聽電話、省內(不含市內)接聽電話、省外接聽電話的記錄
以及傳送簡訊、接收簡訊的記錄。

圖2

圖2中CommunicationRecord是抽象的通訊記錄類:

包含callingNumber撥打號碼、answerNumber接聽號碼兩個屬性。
CallRecord(通話記錄)、MessageRecord(簡訊記錄)是它的子類。

圖3
圖3是計費規則的相關類,這些類的核心方法是:
calCost(ArrayList callRecords)。
該方法針根據輸入引數callRecords中的所有記錄計算某使用者的某一項費用;如市話費。
輸入引數callRecords的約束條件:必須是某一個使用者的符合計費規則要求的所有記錄。
SendMessageRule是傳送簡訊的計費規則類,用於計算髮送簡訊的費用。
LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三個類分別是座機撥打市內、省內、省外電話的計費規則類,用於實現這三種情況的費用計算。

(提示:可以從UserRecords類中獲取各種型別的callRecords)。
注意:以上圖中所定義的類不是限定要求,根據實際需要自行補充或修改。
試題分析
本題沒有了之前的通話計費進行計算,只需要對發簡訊進行計費,所以只需要將第六次作業的程式碼中通訊計費換成簡訊計費,再寫好簡訊計費的程式碼就行
原始碼展示:

檢視程式碼

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

public class Main {

    public static void main(String[] args) {
        Inputdeal inputdeal = new Inputdeal();
        ArrayList<User> users = new ArrayList<>();
        Reduce e=new Reduce();
        Scanner in = new Scanner(System.in);
        String input = in.nextLine();
        while (!input.equals("end")) {
            if (1 == inputdeal.check(input)) {
                inputdeal.writeUser(users, input);
            } else if (2 == inputdeal.check(input)) {
                inputdeal.writeRecord(users, input);
            }
            input = in.nextLine();
        }

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

        for (User u : users) {
            //System.out.println(u.getUserRecords().getCallingInCityRecords().size());
            System.out.println(u.getNumber() + " "+e.exchange(u.calCost())+ " "+e.exchange(u.calBalance()));
        }
    }

}

abstract class ChargeMode {
    protected ArrayList<ChargeRule> chargeRules = new ArrayList<>();

    public abstract double calCost(UserRecords userRecords);

    public abstract double getMonthlyRent();

    public ArrayList<ChargeRule> getChargeRules() {
        return chargeRules;
    }

    public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
        this.chargeRules = chargeRules;
    }
}

class UserRecords {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

}


class MobilePhoneMassageCharging extends ChargeMode {

    private double monthlyRent = 0;

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

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

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

}

class Inputdeal {

    public int check(String input) {
        if (input.matches("[u]-0791[0-9]{7,8}\\s[0]") || input.matches("[u]-1[0-9]{10}\\s[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 writeUser(ArrayList<User> users, String input) {
        User usernew = new User();
        String[] inputs = input.split(" ");
        String num = inputs[0].substring(2);
        for (User i : users) {
            if (i.getNumber().equals(num)) {
                return;
            }
        }
        usernew.setNumber(num);
        int mode = Integer.parseInt(inputs[1]);
        if (mode == 3) {
            usernew.setChargeMode(new MobilePhoneMassageCharging());
        }
        users.add(usernew);
    }

    public void writeRecord(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);
            }
        }

    }

}

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

    public String getCallingNumber() {
        return callingNumber;
    }

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

    public String getAnswerNumbe() {
        return answerNumbe;
    }

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

}

abstract class ChargeRule {

    abstract public double calCost(UserRecords userRecords);

}

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

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

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

        return type;
    }

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

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

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

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

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

    }

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

    public Date getStartTime() {
        return startTime;
    }

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

    public Date getEndTime() {
        return endTime;
    }

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

    public String getCallingAddressAreaCode() {
        return callingAddressAreaCode;
    }

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

    public String getAnswerAddressAreaCode() {
        return answerAddressAreaCode;
    }

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

abstract class CallChargeRule extends ChargeRule {

}

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 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 User {

    private UserRecords userRecords = new UserRecords();
    private double balance = 100;
    private ChargeMode chargeMode;
    private String number;

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

    public double calBalance() {
        return balance - chargeMode.getMonthlyRent() - chargeMode.calCost(userRecords);
    }

    public UserRecords getUserRecords() {
        return userRecords;
    }

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

    public ChargeMode getChargeMode() {
        return chargeMode;
    }

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

    public String getNumber() {
        return number;
    }

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

}
class Reduce{
    public double exchange(double a){
        String c=String.format("%."+2+"f",a);a= Double.parseDouble(c);
        return a;
    }
}

SourceMonitor生成的報表內容:

類圖

程式碼分析總結:
本題只要將第六次作業的電話通訊改成簡訊就行,而且只需要通過計算髮送簡訊的長度來計費,比第六次作業還要簡單。

第八次作業第二題

題目:
編寫一個類Shop(商店)、內部類InnerCoupons(內部購物券)
編寫一個類Shop(商店),該類中有一個成員內部類InnerCoupons(內部購物券),可以用於購買該商店的牛奶(假設每箱牛奶售價為50元)。要求如下:
(1)Shop類中有私有屬性milkCount(牛奶的箱數,int型別)、公有的成員方法setMilkCount( )和getMilkCount( )分別用於設定和獲取牛奶的箱數。
(2)成員內部類InnerCoupons,有公有屬性value(面值,int型別),一個帶引數的構造方法可以設定購物券的面值value,一個公有的成員方法buy( )要求輸出使用了面值為多少的購物券進行支付,同時使商店牛奶的箱數減少value/50。
(3)Shop類中還有成員變數coupons50(面值為50元的內部購物券,型別為InnerCoupons)、coupons100(面值為100元的內部購物券,型別為InnerCoupons)。
(4)在Shop類的構造方法中,呼叫內部類InnerCoupons的帶引數的構造方法分別建立上面的購物券coupons50、coupons100。

在測試類Main中,建立一個Shop類的物件myshop,從鍵盤輸入一個整數(大於或等於3),將其設定為牛奶的箱數。假定有顧客分別使用了該商店面值為50的購物券、面值為100的購物券各消費一次,分別輸出消費後商店剩下的牛奶箱數。
輸入格式:
輸入一個大於或等於3的整數。

輸出格式:
使用了面值為50的購物券進行支付
牛奶還剩XX箱
使用了面值為100的購物券進行支付
牛奶還剩XX箱
試題分析
本題需要按照題目要求定義shop類,計算用50元券和100元券買完牛奶後店裡剩餘的牛奶箱數。
原始碼展示:

檢視程式碼

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Shop shop = new Shop(in.nextInt());
        shop.coupons50.buy();
        System.out.println("牛奶還剩" + shop.getMilkCount() + "箱");
        shop.coupons100.buy();
        System.out.println("牛奶還剩" + shop.getMilkCount() + "箱");
    }
}

class Shop {
    private int milkCount;
    InnerCoupons coupons50 = new InnerCoupons(50);
    InnerCoupons coupons100 = new InnerCoupons(100);
    public Shop(int milkCount) {
        super();
        this.milkCount = milkCount;
    }
    class InnerCoupons {
        public int value;
        public InnerCoupons(int value) {
            this.value = value;
        }
        public void buy() {
            System.out.println("使用了面值為" + value + "的購物券進行支付");
            milkCount = milkCount - value / 50;
        }
    }
    public int getMilkCount() {
        return milkCount;
    }
    public void setMilkCount(int milkCount) {
        this.milkCount = milkCount;
    }
}

SourceMonitor生成的報表內容:

類圖

程式碼分析總結:
本題難度較低,不做分析

第八次作業第三題

題目:
動物發聲模擬器(多型)
設計一個動物發生模擬器,用於模擬不同動物的叫聲。比如獅吼、虎嘯、狗旺旺、貓喵喵……。
定義抽象類Animal,包含兩個抽象方法:獲取動物類別getAnimalClass()、動物叫shout();
然後基於抽象類Animal定義狗類Dog、貓類Cat和山羊Goat,用getAnimalClass()方法返回不同的動物類別(比如貓,狗,山羊),用shout()方法分別輸出不同的叫聲(比如喵喵、汪汪、咩咩)。
最後編寫AnimalShoutTest類測試,輸出:
貓的叫聲:喵喵
狗的叫聲:汪汪
山羊的叫聲:咩咩

其中,在AnimalShoutTestMain類中,用speak(Animal animal){}方法輸出動物animal的叫聲,在main()方法中呼叫speak()方法,分別輸出貓、狗和山羊物件的叫聲。

請在下面的【】處新增程式碼。

//動物發生模擬器. 請在下面的【】處新增程式碼。
public class AnimalShoutTest2 {
public static void main(String[] args) {
Cat cat = new Cat();
Dog dog = new Dog();
Goat goat = new Goat();
speak(cat);
speak(dog);
speak(goat);
}
//定義靜態方法speak()
【】

}

//定義抽象類Animal
【】class Animal{
【】
}
//基於Animal類,定義貓類Cat,並重寫兩個抽象方法
class Cat 【】{
【】
【】
}
//基於Animal類,定義狗類Dog,並重寫兩個抽象方法
class Dog 【】{
【】
【】
}
//基於Animal類,定義山羊類Goat,並重寫兩個抽象方法
class Goat 【】{
【】
【】
}
輸入格式:

輸出格式:
貓的叫聲:喵喵
狗的叫聲:汪汪
山羊的叫聲:咩咩
試題分析
本題只要按照題目寫出動物類和它的三個動物子類,子類中重寫動物型別和動物叫聲兩個抽象方法
原始碼展示:

檢視程式碼

//動物發生模擬器.  請在下面的【】處新增程式碼。
public class Main {
    public static void main(String[] args) {
        Cat cat = new Cat();
        Dog dog = new Dog();
        Goat goat = new Goat();
        speak(cat);
        speak(dog);
        speak(goat);
    }
    // 定義靜態方法speak()
    private static void speak(Animal animal) {
        System.out.println(animal.getAnimalClass() + "的叫聲:" + animal.shout());
    }
}
//定義抽象類Animal
abstract class Animal {
    abstract public String getAnimalClass();
    abstract public String shout();
}
//基於Animal類,定義貓類Cat,並重寫兩個抽象方法
class Cat extends Animal {
    @Override
    public String getAnimalClass() {
        return "貓";
    }
    @Override
    public String shout() {
        return "喵喵";
    }
}
//基於Animal類,定義狗類Dog,並重寫兩個抽象方法
class Dog extends Animal {
    @Override
    public String getAnimalClass() {
        return "狗";
    }
    @Override
    public String shout() {
        return "汪汪";
    }
}
//基於Animal類,定義山羊類Goat,並重寫兩個抽象方法
class Goat extends Animal {
    @Override
    public String getAnimalClass() {
        return "山羊";
    }
    @Override
    public String shout() {
        return "咩咩";
    }
}

SourceMonitor生成的報表內容:

類圖

程式碼分析總結:
本題題目給出了主體程式碼,只要重寫4個類就行,不做分析

3.踩坑心得:

寫第六次作業第一題時忘記約分了,導致一直有幾個點過不去,約分到兩位小數後才通過。

程式碼太長就導致同一個程式碼提交後有的超時而有的通過了

4.改進建議

1.考慮到全面性,所以根據老師給出的類圖寫的眾多類中有很多
函式其實並沒有用到,如果因為執行超時而導致過不去的話可以試試刪掉一些沒有用到的部分。
2.判斷使用者開戶地址時用了大量的if else,可以考慮換成switch。

5.總結

1.作業總結:電信計費的這三次作業基本上老師已經將主體放在給出的類圖裡了,我們只需要按照類圖就能寫出大部分,通過這三次作業我也學到了對類更細緻的構造方法。
2.本階段綜合總結:在這三次作業期間,我學會了javafx的基本程式設計方法和介面的程式設計方法,但真正運用時總是不怎麼熟練,介面的程式設計總是迷糊,不知道怎麼接,用javaFx寫圖形介面時對圖片的呼叫和移動時老是感覺很迷茫,還需要多加研究和查閱資料。
3.對課堂的建議:希望老師在講述Java的一些設計方面和程式設計方面時能詳細一些,有時候明明聽得很懵但還是很快的略過了,容易導致之後的內容越來越難跟上進度。