第三次Blog总结

目录

(1)前言。

(2)设计与分析。

(3)踩坑新得。

(4)改进建议。

(5)总结。

1.前言

      在最近的几次pta作业中难度开始有了显著的提升,已经开始让我们做一些有技术要求的小项目。代码要求也比较复杂,用上了链表,多态,等知识点。

2.设计与分析

        题目一

         实现一个简单的电信计费程序: 假设南昌市电信分公司针对市内座机用户采用的计费方式: 月租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类。

代码 

import java.util.ArrayList;
import java.util.Date;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;
public class Main {
    public static void main(String[] args) throws ParseException {
        Scanner input = new Scanner(System.in);
        ArrayList<User> uselist = new ArrayList<User>();
        String shuru = "[u][-]0791[0-9]{7,8}[ ][0-2]";
        String shuru1 = "[t][-]0791[0-9]{7,8}[ ][0-9]{10,12}[ ]([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9])[\\\\.]([1-9]|[1-9][0-9])[\\\\.]([1-9]|[1-9][0-9])[ ]([0-1][0-9]|2[0-3])[:][0-5][0-9][:][0-5][0-9][ ]([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9])[\\\\.]([1-9]|[1-9][0-9])[\\\\.]([1-9]|[1-9][0-9])[ ]([0-1][0-9]|2[0-3])[:][0-5][0-9][:][0-5][0-9]";
        while (true) {
            String str = input.nextLine();
            String[] str1 = str.split("t|u");
            if (str.equals("end"))
                break;
            if (u(str)) {
                String[] a1 = str.split(" ");
                String[] a2 = a1[0].split("-");
                int l = 0;
                for (int i = 0; i < uselist.size(); i++) {
                    if (Objects.equals(a2[1], uselist.get(i).getNumber()))
                        l = 1;
                }
                if (l == 0) {
                    User b = new User(a2[1]);
                    uselist.add(b);
                }

            }

            if (t(str)) {
                String[] a1 = str.split(" ");
                String[] a2 = a1[0].split("-");
                int p = -1;
                for (int i = 0; i < uselist.size(); i++) {
                    if (a2[1].equals(uselist.get(i).getNumber()))
                        p = i;
                }
                if (p >= 0) {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
                    simpleDateFormat.setLenient(false);
                    String time1 = a1[2] + " " + a1[3];
                    String time2 = a1[4] + " " + a1[5];
                    ParsePosition parsePosition1 = new ParsePosition(0);
                    ParsePosition parsePosition2 = new ParsePosition(0);
                    CallRecord call = new CallRecord();

                    if (simpleDateFormat.parse(time1, parsePosition1) != null && simpleDateFormat.parse(time2, parsePosition2) != null) {
                        Date StartTime = simpleDateFormat.parse(time1);
                        Date EndTime = simpleDateFormat.parse(time2);
                        call.setStartTime(StartTime);
                        call.setEndTime(EndTime);
                        if (a1[1].matches("0791[0-9]{7,8}")) {
                            uselist.get(p).getUserRecords().addCallinglnCityRecords(call);
                        } else if (a1[1].matches("079[0-9][0-9]{7,8}") || a1[1].matches("0701[0-9]{7,8}")) {
                            uselist.get(p).getUserRecords().addCallinglnProvinceRecords(call);
                        } else {
                            uselist.get(p).getUserRecords().addCallinglnLandRecords(call);
                        }
                    }

                }
            }
        }
        for(int i=1;i<=uselist.size()-1;i++){
            for(int j=0;j<=uselist.size()-1-i;j++){
                if(Double.parseDouble(uselist.get(j).getNumber())>Double.parseDouble(uselist.get(j+1).getNumber())){
                    Collections.swap(uselist,j,j+1);
                }
            }
        }
        for(int i=0;i<uselist.size();i++){
            System.out.println(uselist.get(i).getNumber()+" "+String.format("%.1f",uselist.get(i).getChargeMode().calCost(uselist.get(i).getUserRecords()))
                    +" "+String.format("%.1f",uselist.get(i).calBalance()));
        }
    }

    public static boolean u(String u){
        return u.matches("[u][-]0791[0-9]{7,8}[ ][0-2]");
    }
    public static boolean t(String t){
        return t.matches("[t][-]0791[0-9]{7,8}[ ][0-9]{10,12}[ ]([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9])[\\\\.]([1-9]|[1-9][0-9])[\\\\.]([1-9]|[1-9][0-9])[ ]([0-1][0-9]|2[0-3])[:][0-5][0-9][:][0-5][0-9][ ]([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9])[\\\\.]([1-9]|[1-9][0-9])[\\\\.]([1-9]|[1-9][0-9])[ ]([0-1][0-9]|2[0-3])[:][0-5][0-9][:][0-5][0-9]");
    }
}
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 double time() {
        double minute = (getEndTime().getTime() - getStartTime().getTime()) / 60000 ;
        double s = (getEndTime().getTime() - getStartTime().getTime()) / 1000;
        if(s %60 != 0)
            minute ++;
        return minute;
    }
    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) {
        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> getChargeRule(){
        return chargeRules;
    }

    public abstract double calCost(UserRecords userRecords);

    public abstract double getMonthlyRent();
    public void setChargeRule(ArrayList<ChargeRule> chargeRule) {
        this.chargeRules = chargeRule;
    }

}
abstract class ChargeRule {

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

    public String getCallingNumber() {
        return callingNumber;
    }

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

    public String getAnswerNumber() {
        return answerNumber;
    }

    public void setAnswerNumber(String answerNumber) {
        this.answerNumber = answerNumber;
    }
}
class LandlinePhoneCharging extends ChargeMode{
    private double monthlyRent=20;
    LandPhonelnCityRule landPhoneInCityRule = new LandPhonelnCityRule();
    LandPhonelnProvinceRule landPhoneInProvinceRule = new LandPhonelnProvinceRule();
    LandPhonelnLandRule landPhoneInLandRule = new LandPhonelnLandRule();

    @Override
    public double calCost(UserRecords userRecords) {
        return landPhoneInCityRule.calCost(userRecords.getCallinglnCityRecords()) +
                landPhoneInProvinceRule.calCost(userRecords.getCallinglnProvinceRecords()) +
                landPhoneInLandRule.calCost(userRecords.getCallinglnLandRecords());
    }
    @Override
    public double getMonthlyRent(){
        return monthlyRent;
    }
}
class LandPhonelnCityRule extends CallChargeRule{
    @Override
    public double calCost(ArrayList<CallRecord>callRecords) {
        double cost = 0;
        for (int i = 0; i < callRecords.size(); i++) {
            cost = callRecords.get(i).time() * 0.1 + cost;
        }
        return cost;
    }
}
class LandPhonelnLandRule extends CallChargeRule{
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double cost = 0;
        for (int i = 0; i < callRecords.size(); i++) {
            cost = callRecords.get(i).time() * 0.6 + cost;
        }
        return cost;
    }
}
class LandPhonelnProvinceRule extends CallChargeRule{
    @Override
    public double calCost(ArrayList<CallRecord> callRecords){
        double cost = 0;
        for (int i = 0; i < callRecords.size(); i++) {
            cost = callRecords.get(i).time() * 0.3 + cost;
        }
        return cost;
    }
}
class MessageRecord extends CommunicationRecord{
    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
class User {
    private double balance=100;
    private String number;
    UserRecords userRecords=new UserRecords();
    private LandlinePhoneCharging chargeMode = new LandlinePhoneCharging();

    public User(String number) {
        this.number=number;
    }
    public double calBalance(){
        return getBalance()-calCost()-chargeMode.getMonthlyRent();}


    public double calCost(){
        return chargeMode.calCost(getUserRecords()) ;
    }
    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(LandlinePhoneCharging chargeMode){
        this.chargeMode=  chargeMode;
    }
    public String getNumber(){
        return number;
    }
    public void setNumber(String number){
       this.number=number;
    }
}
class UserRecords {
    private ArrayList<CallRecord> callinglnCityRecords=new ArrayList<CallRecord>();
    private ArrayList<CallRecord> callinglnProvinceRecords=new ArrayList<CallRecord>();
    private ArrayList<CallRecord> callinglnLandRecords=new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerlnCityRecords=new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerlnProvinceRecords=new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerlnLandRecords=new ArrayList<CallRecord>();
    private ArrayList<MessageRecord> sendMessageRecords=new ArrayList<MessageRecord>();
    private ArrayList<MessageRecord> receiveMessageRecords=new ArrayList<MessageRecord>();
    public void addCallinglnCityRecords(CallRecord callRecord){
        callinglnCityRecords.add(callRecord);
    }
    public void addCallinglnProvinceRecords(CallRecord callRecord){
        callinglnProvinceRecords.add(callRecord);
    }
    public void addCallinglnLandRecords(CallRecord callRecord){
        callinglnLandRecords.add(callRecord);
    }
    public void addAnswerlnCityRecords(CallRecord answerRecord){
        answerlnCityRecords.add(answerRecord);
    }
    public void addAnswerlnProvinceRecords(CallRecord answerRecord){
        answerlnProvinceRecords.add(answerRecord);
    }
    public void addAnswerlnLandRecords(CallRecord answerRecord){
        answerlnLandRecords.add(answerRecord);
    }
    public void addSendMessageRecords(MessageRecord sendMessageRecord){
        sendMessageRecords.add(sendMessageRecord);
    }
    public void addReceiveMessageRecord(MessageRecord receiveMessageRecord){
       receiveMessageRecords.add(receiveMessageRecord);
    }

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

    public ArrayList<MessageRecord> getSendMessageRecords() {
        return sendMessageRecords;
    }
    public ArrayList<CallRecord> getCallinglnCityRecords(){
        return callinglnCityRecords;
    }
    public ArrayList<CallRecord> getCallinglnProvinceRecords(){
        return callinglnProvinceRecords;
    }
    public ArrayList<CallRecord> getCallinglnLandRecords(){
        return callinglnLandRecords;
    }
    public ArrayList<CallRecord> getAnswerlnCityRecords(){
        return answerlnCityRecords;
    }
    public ArrayList<CallRecord> getAnswerlnProvinceRecords(){
        return answerlnProvinceRecords;
    }
    public ArrayList<CallRecord> getAnswerlnLandRecords(){
        return answerlnLandRecords;
    }
}

类图 

 

 

 

 

 第二题

实现南昌市电信分公司的计费程序,假设该公司针对手机和座机用户分别采取了两种计费方案,分别如下:
1、针对市内座机用户采用的计费方式(与电信计费系列1内容相同):
月租20元,接电话免费,市内拨打电话0.1元/分钟,省内长途0.3元/分钟,国内长途拨打0.6元/分钟。不足一分钟按一分钟计。
假设本市的区号:0791,江西省内各地市区号包括:0790~0799以及0701。
2、针对手机用户采用实时计费方式:
月租15元,市内省内接电话均免费,市内拨打市内电话0.1元/分钟,市内拨打省内电话0.2元/分钟,市内拨打省外电话0.3元/分钟,省内漫游打电话0.3元/分钟,省外漫游接听0.3元/分钟,省外漫游拨打0.6元/分钟;
注:被叫电话属于市内、省内还是国内由被叫电话的接听地点区号决定,比如以下案例中,南昌市手机用户13307912264在区号为020的广州接听了电话,主叫号码应被计算为拨打了一个省外长途,同时,手机用户13307912264也要被计算省外接听漫游费:
u-13307912264 1
t-079186330022 13307912264 020 2022.1.3 10:00:25 2022.1.3 10:05:11

输入:
输入信息包括两种类型
1、逐行输入南昌市用户开户的信息,每行一个用户,含手机和座机用户
格式:u-号码 计费类型 (计费类型包括:0-座机 1-手机实时计费 2-手机A套餐)
例如:u-079186300001 0
座机号码由区号和电话号码拼接而成,电话号码包含7-8位数字,区号最高位是0。
手机号码由11位数字构成,最高位是1。
本题在电信计费系列1基础上增加类型1-手机实时计费。
手机设置0或者座机设置成1,此种错误可不做判断。
2、逐行输入本月某些用户的通讯信息,通讯信息格式:
座机呼叫座机:t-主叫号码 接听号码 起始时间 结束时间
t-079186330022 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:11
以上四项内容之间以一个英文空格分隔,
时间必须符合"yyyy.MM.dd HH:mm:ss"格式。提示:使用SimpleDateFormat类。
输入格式增加手机接打电话以及收发短信的格式,手机接打电话的信息除了号码之外需要额外记录拨打/接听的地点的区号,比如:
座机打手机:
t-主叫号码 接听号码 接听地点区号 起始时间 结束时间
t-079186330022 13305862264 020 2022.1.3 10:00:25 2022.1.3 10:05:11
手机互打:
t-主叫号码 拨号地点 接听号码 接听地点区号 起始时间 结束时间
t-18907910010 0791 13305862264 0371 2022.1.3 10:00:25 2022.1.3 10:05:11

注意:以上两类信息,先输入所有开户信息,再输入所有通讯信息,最后一行以“end”结束。

输出:
根据输入的详细通讯信息,计算所有已开户的用户的当月费用(精确到小数点后2位,单位元)。假设每个用户初始余额是100元。
每条通讯、短信信息均单独计费后累加,不是将所有信息累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。

代码

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.ArrayList;
public class Main {
    public static void main(String[] args) throws ParseException {
        Scanner input = new Scanner(System.in);
        ArrayList<User> uselist = new ArrayList<User>();
        String shuru = "[u][-]0791[0-9]{7,8}[ ][0-2]";
        String shuru1 = "[t][-]0791[0-9]{7,8}[ ][0-9]{10,12}[ ]([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9])[\\\\.]([1-9]|[1-9][0-9])[\\\\.]([1-9]|[1-9][0-9])[ ]([0-1][0-9]|2[0-3])[:][0-5][0-9][:][0-5][0-9][ ]([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9])[\\\\.]([1-9]|[1-9][0-9])[\\\\.]([1-9]|[1-9][0-9])[ ]([0-1][0-9]|2[0-3])[:][0-5][0-9][:][0-5][0-9]";
        while (true) {
            String str = input.nextLine();
            String[] str1 = str.split("t|u");
            if (str.equals("end"))
                break;
            if(!u(str)&&!u1(str)&&!t(str)&&!t1(str)&&!t2(str)&&!t3(str)){
                continue;
            }
            if (u(str)) {
                String[] a1 = str.split(" ");
                String[] a2 = a1[0].split("-");
                int l = 0;
                for (int i = 0; i < uselist.size(); i++) {
                    if (Objects.equals(a2[1], uselist.get(i).getNumber()))
                        l = 1;
                }
                if (l == 0) {
                    User b = new User(a2[1],0);
                    uselist.add(b);
                }

            }
            else if(u1(str))
            {
                String[] a1=str.split(" ");
                String[] a2=a1[0].split("-");
                int l=0;
                for(int i=0;i<uselist.size();i++)
                {
                    if(Objects.equals(a2[1], uselist.get(i).getNumber()))
                        l=1;
                }
                if(l==0) {
                    User b = new User(a2[1],1);
                    uselist.add(b);
                }
            }
            else if (t(str)) {
                String[] a1 = str.split(" ");
                String[] a2 = a1[0].split("-");
                int p = -1;
                for (int i = 0; i < uselist.size(); i++) {
                    if (a2[1].equals(uselist.get(i).getNumber()))
                        p = i;
                }
                if (p >= 0) {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
                    simpleDateFormat.setLenient(false);
                    String time1 = a1[2] + " " + a1[3];
                    String time2 = a1[4] + " " + a1[5];
                    ParsePosition parsePosition1 = new ParsePosition(0);
                    ParsePosition parsePosition2 = new ParsePosition(0);
                    CallRecord call = new CallRecord();

                    if (simpleDateFormat.parse(time1, parsePosition1) != null && simpleDateFormat.parse(time2, parsePosition2) != null) {
                        Date StartTime = simpleDateFormat.parse(time1);
                        Date EndTime = simpleDateFormat.parse(time2);
                        call.setStartTime(StartTime);
                        call.setEndTime(EndTime);
                        if (a1[1].matches("0791[0-9]{7,8}")) {
                            uselist.get(p).getUserRecords().addCallingInCityRecords(call);
                        } else if (a1[1].matches("079[0-9][0-9]{7,8}") || a1[1].matches("0701[0-9]{7,8}")) {
                            uselist.get(p).getUserRecords().addCallingInProvinceRecords(call);
                        } else {
                            uselist.get(p).getUserRecords().addCallingInLandRecords(call);
                        }
                    }

                }
            }
            else if(t1(str)){

                String[] a1=str.split(" ");
                String[] a2=a1[0].split("-");
                int p=-1;
                int o=-1;
                for(int i=0;i<uselist.size();i++){
                    if(Objects.equals(a2[1], uselist.get(i).getNumber()))
                        p=i;
                }
                for(int i=0;i<uselist.size();i++){
                    if(Objects.equals(a1[1], uselist.get(i).getNumber()))
                        o=i;
                }
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
                simpleDateFormat.setLenient(false);
                String time1=a1[3]+" "+a1[4];
                String time2=a1[5]+" "+a1[6];
                ParsePosition parsePosition1 = new ParsePosition(0);
                ParsePosition parsePosition2 = new ParsePosition(0);
                CallRecord call=new CallRecord();

                if(simpleDateFormat.parse(time1,parsePosition1) != null && simpleDateFormat.parse(time2,parsePosition2) != null ) {
                    Date StartTime = simpleDateFormat.parse(time1);
                    Date EndTime = simpleDateFormat.parse(time2);
                    call.setStartTime(StartTime);
                    call.setEndTime(EndTime);
                    if(p>=0){
                        if (a1[2].matches("0791"))
                        {
                            uselist.get(p).getUserRecords().addCallingInCityRecords(call);
                        }
                        else if(a1[2].matches("079[0-9]")||a1[2].matches("0701"))
                        {
                            uselist.get(p).getUserRecords().addCallingInProvinceRecords(call);
                        }
                        else{
                            uselist.get(p).getUserRecords().addCallingInLandRecords(call);
                        }
                    }
                    if(o>=0){
                        if(!a1[2].matches("079[0-9]")&&!a1[2].matches("0701")){
                            uselist.get(o).getUserRecords().add5(call);
                        }
                    }
                }
            }
            else if(t2(str)){

                String[] a1=str.split(" ");
                String[] a2=a1[0].split("-");
                int p=-1;
                for(int i=0;i<uselist.size();i++){
                    if(Objects.equals(a2[1], uselist.get(i).getNumber()))
                        p=i;
                }
                if(p>=0){
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
                    simpleDateFormat.setLenient(false);
                    String time1=a1[3]+" "+a1[4];
                    String time2=a1[5]+" "+a1[6];
                    ParsePosition parsePosition1 = new ParsePosition(0);
                    ParsePosition parsePosition2 = new ParsePosition(0);
                    CallRecord call=new CallRecord();
                    if(simpleDateFormat.parse(time1,parsePosition1) != null && simpleDateFormat.parse(time2,parsePosition2) != null ) {
                        Date StartTime = simpleDateFormat.parse(time1);
                        Date EndTime = simpleDateFormat.parse(time2);
                        call.setStartTime(StartTime);
                        call.setEndTime(EndTime);
                        if (a1[1].matches("0791"))
                        {
                            if(a1[2].matches("0791[0-9]{7,8}"))
                                uselist.get(p).getUserRecords().add1(call);
                            else if(a1[2].matches("079[0-9][0-9]{7,8}"))
                                uselist.get(p).getUserRecords().add2(call);
                            else
                                uselist.get(p).getUserRecords().add3(call);
                        }
                        else if(a1[1].matches("079[0-9]")||a1[1].matches("0701"))
                        {
                            uselist.get(p).getUserRecords().add4(call);
                        }
                        else{
                            uselist.get(p).getUserRecords().add6(call);
                        }
                    }
                }
            }
            else if(t3(str)){

                String[] a1=str.split(" ");
                String[] a2=a1[0].split("-");
                int p=-1;
                int o=-1;
                for(int i=0;i<uselist.size();i++){
                    if(Objects.equals(a2[1], uselist.get(i).getNumber()))
                        p=i;
                }
                for(int i=0;i<uselist.size();i++){
                    if(Objects.equals(a1[2], uselist.get(i).getNumber()))
                        o=i;
                }
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
                simpleDateFormat.setLenient(false);
                String time1=a1[4]+" "+a1[5];
                String time2=a1[6]+" "+a1[7];
                ParsePosition parsePosition1 = new ParsePosition(0);
                ParsePosition parsePosition2 = new ParsePosition(0);
                CallRecord call=new CallRecord();

                if(simpleDateFormat.parse(time1,parsePosition1) != null && simpleDateFormat.parse(time2,parsePosition2) != null ) {
                    Date StartTime = simpleDateFormat.parse(time1);
                    Date EndTime = simpleDateFormat.parse(time2);
                    call.setStartTime(StartTime);
                    call.setEndTime(EndTime);
                    if(p>=0){
                        if (a1[1].matches("0791"))
                        {
                            if(a1[3].matches("0791"))
                                uselist.get(p).getUserRecords().add1(call);
                            else if(a1[3].matches("079[0-9]")||a1[3].matches("0701"))
                                uselist.get(p).getUserRecords().add2(call);
                            else
                                uselist.get(p).getUserRecords().add3(call);
                        }
                        else if(a1[1].matches("079[0-9]")||a1[1].matches("0701"))
                        {
                            uselist.get(p).getUserRecords().add4(call);
                        }
                        else {
                            uselist.get(p).getUserRecords().add6(call);
                        }

                    }

                }
                if(o>=0){
                    if(!a1[3].matches("079[0-9]")&&!a1[3].matches("0701")){
                        uselist.get(o).getUserRecords().add5(call);
                    }
                }
            }

        }
        for(int i=1;i<=uselist.size()-1;i++){
            for(int j=0;j<=uselist.size()-1-i;j++){
                if(uselist.get(j).getNumber().compareTo(uselist.get(j+1).getNumber())>0){
                    Collections.swap(uselist,j,j+1);
                }
            }
        }
        for(int i=0;i<uselist.size();i++){
            if(uselist.get(i).gettype()==0) {
                System.out.println(uselist.get(i).getNumber() + " " + String.format("%.1f", uselist.get(i).getChargeMode().calCost(uselist.get(i).getUserRecords()))
                        + " " + String.format("%.1f", uselist.get(i).calBalance()));
            }
            else {
                System.out.println(uselist.get(i).getNumber() + " " + String.format("%.1f", uselist.get(i).getChargeMode().calCost(uselist.get(i).getUserRecords()))
                        + " " + String.format("%.1f", uselist.get(i).calBalance1()));
            }
        }


    }

    public static boolean u(String u){
        return u.matches("[u][-]0791[0-9]{7,8}[ ][0-2]");
    }
    public static boolean u1(String u){
        return u.matches("u-1[0-9]{10} 1");
    }
    public static boolean t(String t){
        return t.matches("[t][-]0791[0-9]{7,8}[ ][0-9]{10,12}[ ]([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9])[\\\\.]([1-9]|[1-9][0-9])[\\\\.]([1-9]|[1-9][0-9])[ ]([0-1][0-9]|2[0-3])[:][0-5][0-9][:][0-5][0-9][ ]([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9])[\\\\.]([1-9]|[1-9][0-9])[\\\\.]([1-9]|[1-9][0-9])[ ]([0-1][0-9]|2[0-3])[:][0-5][0-9][:][0-5][0-9]");
    }
    public static boolean t1(String t){
        return t.matches("t-0791[0-9]{7,8} 1[0-9]{10} 0[0-9]{2,3} [0-9]{4}.([1-9]||1[0-2]).([1-9]||[1-3][0-9]) [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4}.([1-9]||1[0-2]).([1-9]||[1-3][0-9]) [0-9]{2}:[0-9]{2}:[0-9]{2}");
    }
    public static boolean t2(String t){
        return t.matches("t-1[0-9]{10} 0[0-9]{2,3} 0[0-9]{9,11} [0-9]{4}.([1-9]||1[0-2]).([1-9]||[1-3][0-9]) [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4}.([1-9]||1[0-2]).([1-9]||[1-3][0-9]) [0-9]{2}:[0-9]{2}:[0-9]{2}");
    }
    public static boolean t3(String t){
        return t.matches("t-1[0-9]{10} 0[0-9]{2,3} 1[0-9]{10} 0[0-9]{2,3} ((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.(((0?[13578]|1[02])\\.(0?[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\\s((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.([1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])");
    }
}
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 double time() {
        double minute = (getEndTime().getTime() - getStartTime().getTime()) / 60000 ;
        double s = (getEndTime().getTime() - getStartTime().getTime()) / 1000;
        if(s %60 != 0)
            minute ++;
        return minute;
    }
    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) {
        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> getChargeRule(){
        return chargeRules;
    }

    public abstract double calCost(UserRecords userRecords);

    public abstract double getMonthlyRent();
    public void setChargeRule(ArrayList<ChargeRule> chargeRule) {
        this.chargeRules = chargeRule;
    }

}
abstract class ChargeRule {

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

    public String getCallingNumber() {
        return callingNumber;
    }

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

    public String getAnswerNumber() {
        return answerNumber;
    }

    public void setAnswerNumber(String answerNumber) {
        this.answerNumber = answerNumber;
    }
}
 class LandlinePhoneCharging extends ChargeMode{
    private double monthlyRent=20;
    LandPhonelnCityRule landPhoneInCityRule = new LandPhonelnCityRule();
    LandPhonelnProvinceRule landPhoneInProvinceRule = new LandPhonelnProvinceRule();
    LandPhonelnLandRule landPhoneInLandRule = new LandPhonelnLandRule();
    talk1 t1 = new talk1();
    talk2 t2 = new talk2();
    talk3 t3 = new talk3();
    talk4 t4 = new talk4();
    talk5 t5 = new talk5();
    talk6 t6 = new talk6();
    @Override
    public double calCost(UserRecords userRecords) {
        return landPhoneInCityRule.calCost(userRecords.getCallingInCityRecords()) +
                landPhoneInProvinceRule.calCost(userRecords.getCallingInProvinceRecords()) +
                landPhoneInLandRule.calCost(userRecords.getCallingInLandRecords())+
                t1.calCost(userRecords.gettalk1())+t2.calCost(userRecords.gettalk2())+
                t3.calCost(userRecords.gettalk3())+t4.calCost(userRecords.gettalk4())+
                t5.calCost(userRecords.gettalk5());
    }
    @Override
    public double getMonthlyRent(){
        return monthlyRent;
    }
}
class LandPhonelnCityRule extends CallChargeRule{
    @Override
    public double calCost(ArrayList<CallRecord>callRecords) {
        double cost = 0;
        for (int i = 0; i < callRecords.size(); i++) {
            cost = callRecords.get(i).time() * 0.1 + cost;
        }
        return cost;
    }
}
class LandPhonelnLandRule extends CallChargeRule{
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double cost = 0;
        for (int i = 0; i < callRecords.size(); i++) {
            cost = callRecords.get(i).time() * 0.6 + cost;
        }
        return cost;
    }
}
class LandPhonelnProvinceRule extends CallChargeRule{
    @Override
    public double calCost(ArrayList<CallRecord> callRecords){
        double cost = 0;
        for (int i = 0; i < callRecords.size(); i++) {
            cost = callRecords.get(i).time() * 0.3 + cost;
        }
        return cost;
    }
}
class MessageRecord extends CommunicationRecord{
    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
class talk1 extends CallChargeRule{
        @Override
        public double calCost(ArrayList<CallRecord> callRecords) {
                double cost = 0;
                for(int i = 0;i < callRecords.size();i ++) {
                        cost = callRecords.get(i).time() * 0.1 + cost;
                }
                return cost;
        }
}
class talk2 extends CallChargeRule{
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double cost = 0;
        for(int i = 0;i < callRecords.size();i ++) {
            cost = callRecords.get(i).time() * 0.2 + cost;
        }
        return cost;
    }
}
class talk3 extends CallChargeRule{
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double cost = 0;
        for(int i = 0;i < callRecords.size();i ++) {
            cost = callRecords.get(i).time() * 0.3 + cost;
        }
        return cost;
    }
}
class talk4 extends CallChargeRule{
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double cost = 0;
        for(int i = 0;i < callRecords.size();i ++) {
            cost = callRecords.get(i).time() * 0.3 + cost;
        }
        return cost;
    }
}
class talk5 extends CallChargeRule{
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double cost = 0;
        for(int i = 0;i < callRecords.size();i ++) {
            cost = callRecords.get(i).time() * 0.3 + cost;
        }
        return cost;
    }
}
class talk6{
    
}
class User {
    private UserRecords userRecords = new UserRecords();
    private double balance=100;
    private String number;
    private int type;
    private LandlinePhoneCharging chargeMode = new LandlinePhoneCharging();

    public User(String number,int type) {
        this.number = number;
        this.type = type;
    }

    public double calBalance() {return getBalance() - calCost() - chargeMode.getMonthlyRent();}
    public double calBalance1() {return getBalance() - calCost() - 15;}
    public double calCost() {return chargeMode.calCost(getUserRecords()) ;}
    public ChargeMode getChargeMode() {
        return chargeMode;
    }
    public void setChargeMode(LandlinePhoneCharging chargeMode) {
        this.chargeMode = chargeMode;
    }
    public UserRecords getUserRecords() {
        return userRecords;
    }
    public void setUserRecords(UserRecords userRecords) {
        this.userRecords = userRecords;
    }
    public double getBalance() {
        return balance;
    }
    public String getNumber() {
        return number;
    }
    public int gettype() {
        return type;
    }
    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<CallRecord> talk1 = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> talk2 = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> talk3 = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> talk4 = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> talk5 = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> talk6 = 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 add1(CallRecord callRecord) {
        talk1.add(callRecord);
    }
    public void add2(CallRecord callRecord) {
        talk2.add(callRecord);
    }
    public void add3(CallRecord callRecord) { talk3.add(callRecord); }
    public void add4(CallRecord callRecord) {
        talk4.add(callRecord);
    }
    public void add5(CallRecord callRecord) {
        talk5.add(callRecord);
    }
    public void add6(CallRecord callRecord) {
        talk6.add(callRecord);
    }
    public void addAnswerInCityRecords(CallRecord callRecord) {answerInCityRecords.add(callRecord);}
    public void addAnswerInProvinceRecords(CallRecord callRecord) {
        answerInProvinceRecords.add(callRecord);
    }
    public void addAnswerInLandRecords(CallRecord callRecord) {
        answerInLandRecords.add(callRecord);
    }
    public void addSendMessageRecords(MessageRecord messageRecord) {
        sendMessageRecords.add(messageRecord);
}
    public void addReceiveMessageRecords(MessageRecord messageRecord) {
        receiveMessageRecords.add(messageRecord);
    }
    public ArrayList<CallRecord> getCallingInCityRecords() {return callingInCityRecords;}
    public ArrayList<CallRecord> getCallingInProvinceRecords() {
        return callingInProvinceRecords;
    }
    public ArrayList<CallRecord> getCallingInLandRecords() {
        return callingInLandRecords;
    }
    public ArrayList<CallRecord> gettalk1() {return talk1;}
    public ArrayList<CallRecord> gettalk2() {
        return talk2;
    }
    public ArrayList<CallRecord> gettalk3() {
        return talk3;
    }
    public ArrayList<CallRecord> gettalk4() {return talk4;}
    public ArrayList<CallRecord> gettalk5() {
        return talk5;
    }
    public ArrayList<CallRecord> gettalk6() {
        return talk6;
    }
    public ArrayList<CallRecord> getAnswerInCityRecords() {
        return answerInCityRecords;
    }
    public ArrayList<CallRecord> getAnswerInProvinceRecords() {
        return answerInProvinceRecords;
    }
    public ArrayList<CallRecord> getAnswerInLandRecords() {
        return answerInLandRecords;
    }
    public ArrayList<MessageRecord> getSendMessageRecords() {
        return sendMessageRecords;
    }
    public ArrayList<MessageRecord> getReceiveMessageRecords() {
        return receiveMessageRecords;
    }
}

类图

 

 

 

 第三题

实现一个简单的电信计费程序,针对手机的短信采用如下计费方式:
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”结束。

代码

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

public class Main {
    public static void main(String[] args) throws ParseException {
        Scanner input = new Scanner(System.in);
        ArrayList<User> uselist = new ArrayList<User>();
        String shuru = "[u][-]0791[0-9]{7,8}[ ][0-2]";
        String shuru1 = "[t][-]0791[0-9]{7,8}[ ][0-9]{10,12}[ ]([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9])[\\\\.]([1-9]|[1-9][0-9])[\\\\.]([1-9]|[1-9][0-9])[ ]([0-1][0-9]|2[0-3])[:][0-5][0-9][:][0-5][0-9][ ]([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9])[\\\\.]([1-9]|[1-9][0-9])[\\\\.]([1-9]|[1-9][0-9])[ ]([0-1][0-9]|2[0-3])[:][0-5][0-9][:][0-5][0-9]";
        while (true) {
            String str = input.nextLine();
            String[] str1 = str.split("t|u");
            if (str.equals("end"))
                break;
           /* if(!u(str)&&!u1(str)&&!t(str)&&!t1(str)&&!t2(str)&&!t3(str)){
                continue;
            }*/
            if (u(str)) {
                String[] a1 = str.split(" ");
                String[] a2 = a1[0].split("-");
                int l = 0;
                for (int i = 0; i < uselist.size(); i++) {
                    if (Objects.equals(a2[1], uselist.get(i).getNumber()))
                        l = 1;
                }
                if (l == 0) {
                    User b = new User(a2[1], 0);
                    uselist.add(b);
                }

            } else if (t3(str)) {

                String[] a1 = str.split(" ");
                String[] a2 = a1[0].split("-");
                int p = -1;

                for (int i = 0; i < uselist.size(); i++) {
                    if (Objects.equals(a2[1], uselist.get(i).getNumber()))
                        p = i;
                }
                MessageRecord message=new MessageRecord();
                String k = str.substring(26);
                message.setMessage(k);
                if(p>=0){
                    uselist.get(p).getUserRecords().addSendMessageRecords(message);
                }
            }

        }
        for(int i=1;i<=uselist.size()-1;i++) {
            for (int j = 0; j <= uselist.size() - 1 - i; j++) {
                if (uselist.get(j).getNumber().compareTo(uselist.get(j + 1).getNumber()) > 0) {
                    Collections.swap(uselist, j, j + 1);
                }
            }
        }
        for(int i=0;i<uselist.size();i++){

            System.out.println(uselist.get(i).getNumber() + " " + String.format("%.1f", uselist.get(i).getChargeMode().calCost(uselist.get(i).getUserRecords()))
                    + " " + String.format("%.1f", uselist.get(i).calBalance1()));

        }
    }

    public static boolean u(String u){
        return u.matches("u-1[0-9]{10} 3");
    }
    public static boolean t3(String t){
        return t.matches("m-1[0-9]{10} 1[0-9]{10} [0-9|a-z|A-Z|.|,| ]+");
    }
}
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 double time() {
        double minute = (getEndTime().getTime() - getStartTime().getTime()) / 60000 ;
        double s = (getEndTime().getTime() - getStartTime().getTime()) / 1000;
        if(s %60 != 0)
            minute ++;
        return minute;
    }
    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) {
        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> getChargeRule(){
        return chargeRules;
    }

    public abstract double calCost(UserRecords userRecords);

    public abstract double getMonthlyRent();
    public void setChargeRule(ArrayList<ChargeRule> chargeRule) {
        this.chargeRules = chargeRule;
    }

}
abstract class ChargeRule {

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

    public String getCallingNumber() {
        return callingNumber;
    }

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

    public String getAnswerNumber() {
        return answerNumber;
    }

    public void setAnswerNumber(String answerNumber) {
        this.answerNumber = answerNumber;
    }
}
class LandlinePhoneCharging extends ChargeMode{
    private double monthlyRent=20;
    LandPhonelnCityRule landPhoneInCityRule = new LandPhonelnCityRule();
    LandPhonelnProvinceRule landPhoneInProvinceRule = new LandPhonelnProvinceRule();
    LandPhonelnLandRule landPhoneInLandRule = new LandPhonelnLandRule();
   /* talk1 t1 = new talk1();
    talk2 t2 = new talk2();
    talk3 t3 = new talk3();
    talk4 t4 = new talk4();
    talk5 t5 = new talk5();
    talk6 t6 = new talk6();*/
    message mm=new message();
    @Override
    public double calCost(UserRecords userRecords) {
        return message.calCost(userRecords.getSendMessageRecords());
    }
    @Override
    public double getMonthlyRent(){
        return monthlyRent;
    }
}
class LandPhonelnCityRule extends CallChargeRule{
    @Override
    public double calCost(ArrayList<CallRecord>callRecords) {
        double cost = 0;
        for (int i = 0; i < callRecords.size(); i++) {
            cost = callRecords.get(i).time() * 0.1 + cost;
        }
        return cost;
    }
}
class LandPhonelnLandRule extends CallChargeRule{
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double cost = 0;
        for (int i = 0; i < callRecords.size(); i++) {
            cost = callRecords.get(i).time() * 0.6 + cost;
        }
        return cost;
    }
}
class LandPhonelnProvinceRule extends CallChargeRule{
    @Override
    public double calCost(ArrayList<CallRecord> callRecords){
        double cost = 0;
        for (int i = 0; i < callRecords.size(); i++) {
            cost = callRecords.get(i).time() * 0.3 + cost;
        }
        return cost;
    }
}
class message {
    public static double calCost(ArrayList<MessageRecord> sendMessageRecords) {
        double cost = 0;
        int m=0;
        for(int i=0;i<sendMessageRecords.size();i++){
            m+=sendMessageRecords.get(i).getMessage().length()/10;
            if(sendMessageRecords.get(i).getMessage().length()%10!=0)
                m++;
        }
        if(m<=3)
        {
            cost=0.1*m;
        }
        else if(m<=5)
        {
            cost=0.3+0.2*(m-3);
        }
        else{
            cost=0.7+0.3*(m-5);
        }
        return cost;
    }


}
class MessageRecord extends CommunicationRecord{
    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
class User {
    private UserRecords userRecords = new UserRecords();
    private double balance=100;
    private String number;
    private int type;
    private LandlinePhoneCharging chargeMode = new LandlinePhoneCharging();

    public User(String number,int type) {
        this.number = number;
        this.type = type;
    }

    public double calBalance() {return getBalance() - calCost() - chargeMode.getMonthlyRent();}
    public double calBalance1() {return getBalance() - calCost();}
    public double calCost() {return chargeMode.calCost(getUserRecords()) ;}
    public ChargeMode getChargeMode() {
        return chargeMode;
    }
    public void setChargeMode(LandlinePhoneCharging chargeMode) {
        this.chargeMode = chargeMode;
    }
    public UserRecords getUserRecords() {
        return userRecords;
    }
    public void setUserRecords(UserRecords userRecords) {
        this.userRecords = userRecords;
    }
    public double getBalance() {
        return balance;
    }
    public String getNumber() {
        return number;
    }
    public int gettype() {
        return type;
    }
    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<CallRecord> talk1 = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> talk2 = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> talk3 = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> talk4 = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> talk5 = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> talk6 = 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 add1(CallRecord callRecord) {
        talk1.add(callRecord);
    }
    public void add2(CallRecord callRecord) {
        talk2.add(callRecord);
    }
    public void add3(CallRecord callRecord) { talk3.add(callRecord); }
    public void add4(CallRecord callRecord) {
        talk4.add(callRecord);
    }
    public void add5(CallRecord callRecord) {
        talk5.add(callRecord);
    }
    public void add6(CallRecord callRecord) {
        talk6.add(callRecord);
    }
    public void addAnswerInCityRecords(CallRecord callRecord) {answerInCityRecords.add(callRecord);}
    public void addAnswerInProvinceRecords(CallRecord callRecord) {
        answerInProvinceRecords.add(callRecord);
    }
    public void addAnswerInLandRecords(CallRecord callRecord) {
        answerInLandRecords.add(callRecord);
    }
    public void addSendMessageRecords(MessageRecord messageRecord) {
        sendMessageRecords.add(messageRecord);
}
    public void addReceiveMessageRecords(MessageRecord messageRecord) {
        receiveMessageRecords.add(messageRecord);
    }
    public ArrayList<CallRecord> getCallingInCityRecords() {return callingInCityRecords;}
    public ArrayList<CallRecord> getCallingInProvinceRecords() {
        return callingInProvinceRecords;
    }
    public ArrayList<CallRecord> getCallingInLandRecords() {
        return callingInLandRecords;
    }
    public ArrayList<CallRecord> gettalk1() {return talk1;}
    public ArrayList<CallRecord> gettalk2() {
        return talk2;
    }
    public ArrayList<CallRecord> gettalk3() {
        return talk3;
    }
    public ArrayList<CallRecord> gettalk4() {return talk4;}
    public ArrayList<CallRecord> gettalk5() {
        return talk5;
    }
    public ArrayList<CallRecord> gettalk6() {
        return talk6;
    }
    public ArrayList<CallRecord> getAnswerInCityRecords() {
        return answerInCityRecords;
    }
    public ArrayList<CallRecord> getAnswerInProvinceRecords() {
        return answerInProvinceRecords;
    }
    public ArrayList<CallRecord> getAnswerInLandRecords() {
        return answerInLandRecords;
    }
    public ArrayList<MessageRecord> getSendMessageRecords() {
        return sendMessageRecords;
    }
    public ArrayList<MessageRecord> getReceiveMessageRecords() {
        return receiveMessageRecords;
    }
}

类图

 

 

三、踩坑心得:

  这三周,我们写这个pta其中修复了很多bug,比如少写了添加的信息,导致有些东西传不进去或者报错,还有时候没有建立座机类导致一直会报错,有时候没有新建类导致有的信息没传,导致我会一直报错,就算我写完了都还有梳理很长时间的思路,当然这次的pta的作业让我知道了如何面向对象去设计实验,面向对象的好处,他的思路更清晰,修改起来更简单,代码耦合度小,代码复杂度低的优点。当我掌握这种方法后可以使代码复用度更高,可以使后面需要加什么代码直接可以加上去,不用再去另外写新的代码了。

四、改进建议:

  这次的Main函数的代码太过于长了,导致很不好寻找,思路不清晰,耦合度有点高。

五、总结:

      半年的学习java也结束了对自己的表现不是很满意。

  对于自己写的程序来说bug还是有很多的,但形成的原因也是有很多的,比如类的混乱,还有随机数的产生导致有些点可以过,但随机数可能使那些点不能过。还有那些正则表达式超级长,看到就脑阔疼,今后我个人会尽量避免写出这样的代码,除非真的万不得已。

  当然在写第五次大作业的时候7-1中的第3次情况写了一大段,但是当运行的时候就不能运行,一开始以为时是单纯的进不了循环,当测试几个样例的时候发现可以进入,那个时候就快崩溃了,当时代码写的太杂了,而且都是很混乱的,导致自己根本改不好,只能无奈的全删了,去骗分。当时就后悔了,应该讲那些代码写进一个类,在类里面进行判断,这样代码既不会写错,到时候改的时候也会更清晰明了一点。不会像这样气急败坏的删除了。

  面向对象不一定比面向过程,面向结果优越,但是优秀的封装和类一定好太多了,第三次作业能写满分,我相信他的封装和代码一定比别人更清晰明了,也更容易看的懂。

  展望:

  经过这三个周的洗礼,我觉得我的代码能力有明显的提升,当然有些地方也有些不足,同时自我学习能力也会更强,因为那些正则表达式是自己在网络上找的资料学习的,当然大家的进步速度也是迅猛的,我也要加把劲。

  在后面两次作业中,能明显感受到代码量增加,期中全部加起来都没有400行,但是第四五次大作业,一个题目就会有1000多行代码,因为里面包含很多算法和方法。

   期望:

  在这三周我学到了很多东西,例如一些函数的运用,同时也吸取了教训,积累了经验。

posted @ 2022-06-16 18:10  长驱直入  阅读(30)  评论(0)    收藏  举报