Blog3

2022/6/18 23:20:45

本文主要是介绍Blog3,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

前言 10-16周java学习总结

近几周PTA的作业以电信收费为主,考试内容涉及正则表达式,字符串的比较.输入.分割代码的复用性.面向对象设计,ArrayList的使用等等。题目量和难度都适中。

期末考试主要考察了对课本知识的运用,例如多态,继承,抽象类,抽象类ArrayList的用法等等.难度题量都适中。

同时实验五考察了javafx的应用。

设计与分析

 

7-1 电信计费系列2-手机+座机计费

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

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

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

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

输入样例:

在这里给出一组输入。例如:

u-13811111111 1
t-13811111111 0791 13811111110 020 2022.1.3 08:00:00 2022.1.3 08:09:20
end

输出样例:

在这里给出相应的输出。例如:

13811111111 3.0 82.0
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.Comparator;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Scanner in = new Scanner(System.in);
        ArrayList<User> x = new ArrayList<User>();
//        UserRecords y = new UserRecords();
        ArrayList<CallRecord> z = new ArrayList<CallRecord>();
        LandlinePhoneCharging cost1 = new LandlinePhoneCharging();
        MobilelinePhoneCharging cost2 = new MobilelinePhoneCharging();
        while(true) {
            String a = in.nextLine();
            if(a.equals("end")) {
                break;
            }
            if(jug(a)==3) {
                User b = new User();
                String[] strarray=a.split(" ");
                b.setNumber(strarray[0].substring(2));
                b.setChargeMode(strarray[1]);
                x.add(b);
                for (int i = 0; i < x.size(); i++) {
                    for (int j = 0; j < x.size(); j++) {
                        if (i != j && x.get(i).number.equals(x.get(j).number)) {
                            x.remove(x.get(j));
                        }
                    }
                }
            }

            else if(jug(a)==2){
                User b = new User();
                String[] strarray=a.split(" ");
                b.setNumber(strarray[0].substring(2));
                b.setChargeMode(strarray[1]);
                x.add(b);
                for (int i = 0; i < x.size(); i++) {
                    for (int j = 0; j < x.size(); j++) {
                        if (i != j && x.get(i).number.equals(x.get(j).number)) {
                            x.remove(x.get(j));
                        }
                    }
                }
            }

            else if(jug(a)==1) {
                CallRecord b = new CallRecord();
                String[] strarray=a.split(" ",6);
                b.setCallingAddressAreaCode(strarray[0].substring(2, 6));
                b.setAnswerAddressAreaCode(strarray[1].substring(0, 4));
                b.setCallingNumber(strarray[0].substring(2));
                b.setAnswerNumber(strarray[1]);
                b.setStartTime(strarray[2].concat(" ").concat(strarray[3]));
                b.setEndTime(strarray[4].concat(" ").concat(strarray[5]));
                z.add(b);
            }
            else if(jug(a)==4) {
                CallRecord b = new CallRecord();
                String[] strarray=a.split(" ",7);
                b.setCallingAddressAreaCode(strarray[0].substring(2, 6));
                b.setAnswerAddressAreaCode(strarray[2]);
                b.setCallingNumber(strarray[0].substring(2));
                b.setAnswerNumber(strarray[1]);
                b.setStartTime(strarray[3].concat(" ").concat(strarray[4]));
                b.setEndTime(strarray[5].concat(" ").concat(strarray[6]));
                z.add(b);
            }else if(jug(a)==5) {
                CallRecord b = new CallRecord();
                String[] strarray=a.split(" ",8);
                b.setCallingAddressAreaCode(strarray[1]);
                b.setAnswerAddressAreaCode(strarray[3]);
                b.setCallingNumber(strarray[0].substring(2));
                b.setAnswerNumber(strarray[2]);
                b.setStartTime(strarray[4].concat(" ").concat(strarray[5]));
                b.setEndTime(strarray[6].concat(" ").concat(strarray[7]));
                z.add(b);
            }else if(jug(a)==6) {
                CallRecord b = new CallRecord();
                String[] strarray=a.split(" ",7);
                b.setCallingAddressAreaCode(strarray[1]);
                b.setAnswerAddressAreaCode(strarray[2].substring(0, 4));
                b.setCallingNumber(strarray[0].substring(2));
                b.setAnswerNumber(strarray[2]);
                b.setStartTime(strarray[3].concat(" ").concat(strarray[4]));
                b.setEndTime(strarray[5].concat(" ").concat(strarray[6]));
                z.add(b);
            }
        }
        
        
        for(int i=0;i<x.size();i++) {
            for(int j =0;j<z.size();j++) {
                if(x.get(i).ChargeMode.equals("0")) {
                if(z.get(j).answerAddressAreaCode.equals("NC")) {
                     if(x.get(i).number.equals(z.get(j).callingNumber))
                    x.get(i).userRecords.addCallingInCityRecords(z.get(j));

                }else if(z.get(j).answerAddressAreaCode.equals("JX")) {
                    if(x.get(i).number.equals(z.get(j).callingNumber))
                    x.get(i).userRecords.addCallingInProvinceRecords(z.get(j));

                }else if(z.get(j).answerAddressAreaCode.equals("ZG")){
                    if(x.get(i).number.equals(z.get(j).callingNumber))
                    x.get(i).userRecords.addCallingInLandRecords(z.get(j));

                    }
                }else {
                    if(z.get(j).answerAddressAreaCode.equals("NC")) {
                         if(x.get(i).number.equals(z.get(j).callingNumber)) {
                             if(z.get(j).callingAddressAreaCode.equals("NC")) {
                                 x.get(i).userRecords.addPhonecallingInCityRecords(z.get(j));
                             }else if(z.get(j).callingAddressAreaCode.equals("JX")){
                                 x.get(i).userRecords.addPhonecallingInProvinceRecords(z.get(j));
                             }else if(z.get(j).callingAddressAreaCode.equals("ZG")){
                                 x.get(i).userRecords.addPhonecallingInLandRecords(z.get(j));
                             }
                         }
                         if(x.get(i).number.equals(z.get(j).answerNumber)) {
                             if(z.get(j).answerAddressAreaCode.equals("NC")) {
                                 x.get(i).userRecords.addAnswerInInCityRecords(z.get(j));
                             }else if(z.get(j).answerAddressAreaCode.equals("JX")){
                                 x.get(i).userRecords.addAnswerInInProvinceRecords(z.get(j));
                             }else if(z.get(j).answerAddressAreaCode.equals("ZG")){
                                 x.get(i).userRecords.addAnswerInInLandRecords(z.get(j));
                             }
                         }

                    }else if(z.get(j).answerAddressAreaCode.equals("JX")) {
                        if(x.get(i).number.equals(z.get(j).callingNumber)) {
                             if(z.get(j).callingAddressAreaCode.equals("NC")) {
                                 x.get(i).userRecords.addPhonecallingInCityRecords(z.get(j));
                             }else if(z.get(j).callingAddressAreaCode.equals("JX")){
                                 x.get(i).userRecords.addPhonecallingInProvinceRecords(z.get(j));
                             }else if(z.get(j).callingAddressAreaCode.equals("ZG")){
                                 x.get(i).userRecords.addPhonecallingInLandRecords(z.get(j));
                             }
                        }
                         if(x.get(i).number.equals(z.get(j).answerNumber)) {
                             if(z.get(j).answerAddressAreaCode.equals("NC")) {
                                 x.get(i).userRecords.addAnswerInInCityRecords(z.get(j));
                             }else if(z.get(j).answerAddressAreaCode.equals("JX")){
                                 x.get(i).userRecords.addAnswerInInProvinceRecords(z.get(j));
                             }else if(z.get(j).answerAddressAreaCode.equals("ZG")){
                                 x.get(i).userRecords.addAnswerInInLandRecords(z.get(j));
                             }
                         }

                    }else if(z.get(j).answerAddressAreaCode.equals("ZG")){
                        if(x.get(i).number.equals(z.get(j).callingNumber)) {
                             if(z.get(j).callingAddressAreaCode.equals("NC")) {
                                 x.get(i).userRecords.addPhonecallingInCityRecords(z.get(j));
                             }else if(z.get(j).callingAddressAreaCode.equals("JX")){
                                 x.get(i).userRecords.addPhonecallingInProvinceRecords(z.get(j));
                             }else if(z.get(j).callingAddressAreaCode.equals("ZG")){
                                 x.get(i).userRecords.addPhonecallingInLandRecords(z.get(j));
                             }
                        }
                         if(x.get(i).number.equals(z.get(j).answerNumber)) {
                             if(z.get(j).answerAddressAreaCode.equals("NC")) {
                                 x.get(i).userRecords.addAnswerInInCityRecords(z.get(j));
                             }else if(z.get(j).answerAddressAreaCode.equals("JX")){
                                 x.get(i).userRecords.addAnswerInInProvinceRecords(z.get(j));
                             }else if(z.get(j).answerAddressAreaCode.equals("ZG")){
                                 x.get(i).userRecords.addAnswerInInLandRecords(z.get(j));
                             }
                         }

                        }
                }
            }
        }
        
        Collections.sort(x, new MyComparatorToUser());
        
        for(int i =0;i<x.size();i++) {
            if(x.get(i).ChargeMode.equals("0")) {
            x.get(i).balance = 100- cost1.calCost(x.get(i).userRecords);
            double q = 80-x.get(i).balance;
            if(i==x.size()-1) {
                System.out.print(x.get(i).number+" "+String.format("%.1f", q)+" "+String.format("%.1f", x.get(i).balance));
            }
            else {
                System.out.println(x.get(i).number+" "+String.format("%.1f", q)+" "+String.format("%.1f", x.get(i).balance));
            }
            }
            else {
                x.get(i).balance = 100- cost2.calCost(x.get(i).userRecords);
                double q = 85-x.get(i).balance;
                if(i==x.size()-1) {
                    System.out.print(x.get(i).number+" "+String.format("%.1f", q)+" "+String.format("%.1f", x.get(i).balance));
                }
                else {
                    System.out.println(x.get(i).number+" "+String.format("%.1f", q)+" "+String.format("%.1f", x.get(i).balance));
                }
            }
        }
        
        in.close();
    }
    

    static class MyComparatorToUser implements Comparator<User>{
        @Override
        public int compare(User u1, User u2) {
            double p = Double.parseDouble(u1.number);
            double q = Double.parseDouble(u2.number);
            if(u1.number.charAt(0)=='0'&&u2.number.charAt(0)=='0') {
                return (int)(p-q);
            }
            if(u1.number.charAt(0)=='0') {
                p=0;
            }
            if(u2.number.charAt(0)=='0') {
                q=0;
            }
            return (int)(p-q);
        }
    }


    public static int jug(String a) {

        String parrten1 = "t-0791[0-9]{7,8} [0-9]{3,4}[0-9]{7,8} [1-9]\\d{3}.([1-9]|1[0-2]).([1-9]|[1-2][0-9]|3[0-1])\\s(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d [1-9]\\d{3}.([1-9]|1[0-2]).([1-9]|[1-2][0-9]|3[0-1])\\s(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d";
        String parrten2 = "u-0791[0-9]{7,8}[ ]0";//开户
        String parrten3 = "u-(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8} 1";//开户
        String parrten4 = "t-[0-9]{3,4}[0-9]{7,8} (13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8} [0-9]{3,4} [1-9]\\d{3}.([1-9]|1[0-2]).([1-9]|[1-2][0-9]|3[0-1])\\s(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d [1-9]\\d{3}.([1-9]|1[0-2]).([1-9]|[1-2][0-9]|3[0-1])\\s(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d";
        String parrten5 = "t-(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8} [0-9]{3,4} (13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8} [0-9]{3,4} [1-9]\\d{3}.([1-9]|1[0-2]).([1-9]|[1-2][0-9]|3[0-1])\\s(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d [1-9]\\d{3}.([1-9]|1[0-2]).([1-9]|[1-2][0-9]|3[0-1])\\s(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d";
        String parrten6 = "t-(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8} [0-9]{3,4} [0-9]{3,4}[0-9]{7,8} [1-9]\\d{3}.([1-9]|1[0-2]).([1-9]|[1-2][0-9]|3[0-1])\\s(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d [1-9]\\d{3}.([1-9]|1[0-2]).([1-9]|[1-2][0-9]|3[0-1])\\s(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d";
        
        if(a.matches(parrten1)) {
            return 1;
        }
        else if(a.matches(parrten2)) {
            return 2;
        }else if(a.matches(parrten3)) {
            return 3;
        }else if(a.matches(parrten4)) {
            return 4;
        }else if(a.matches(parrten5)) {
            return 5;
        }else if(a.matches(parrten6)) {
            return 6;
        }
        return 0;
    }
}


class UserRecords {
    ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();
    ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();
    ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();
    ArrayList<CallRecord> phonecallingInCityRecords = new ArrayList<CallRecord>();
    ArrayList<CallRecord> phonecallingInProvinceRecords = new ArrayList<CallRecord>();
    ArrayList<CallRecord> phonecallingInLandRecords = new ArrayList<CallRecord>();
    ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();
    ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();
    ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();
    
    ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();
    ArrayList<MessageRecord> reciveMessageRecords = 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 addAnswerInInCityRecords(CallRecord answerRecord) {
        answerInCityRecords.add(answerRecord);
    }
    
    public void addAnswerInInProvinceRecords(CallRecord answerRecord) {
        answerInProvinceRecords.add(answerRecord);
    }
    
    public void addAnswerInInLandRecords(CallRecord answerRecord) {
        answerInLandRecords.add(answerRecord);
    }
    public void addSendMessageRecords(MessageRecord sendMessageRecord) {
        sendMessageRecords.add(sendMessageRecord);
    }
    
    public void addReciveMessageRecords(MessageRecord reciveMessageRecord) {
        reciveMessageRecords.add(reciveMessageRecord);
    }

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

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

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

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

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

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

    public ArrayList<CallRecord> getPhonecallingInCityRecords() {
        return phonecallingInCityRecords;
    }

    public void addPhonecallingInCityRecords(CallRecord callRecord) {
        phonecallingInCityRecords.add(callRecord);
    }

    public ArrayList<CallRecord> getPhonecallingInProvinceRecords() {
        return phonecallingInProvinceRecords;
    }

    public void addPhonecallingInProvinceRecords(CallRecord callRecord) {
        phonecallingInProvinceRecords.add(callRecord);
    }

    public ArrayList<CallRecord> getPhonecallingInLandRecords() {
        return phonecallingInLandRecords;
    }

    public void addPhonecallingInLandRecords(CallRecord callRecord) {
        phonecallingInLandRecords.add(callRecord);
    }

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

    public ArrayList<MessageRecord> getReciveMessageRecords() {
        return reciveMessageRecords;
    }
}
class User {
    UserRecords userRecords = new UserRecords();
    double balance = 100;
    String ChargeMode;
    String number;
    
    public User() {
    }
    
    public double calBalance() {
        return 100-calCost();
    }
    
    public double calCost() {
        return 0;
    }
    
    public  UserRecords  getUserRecords() {
        return userRecords;
    }
    
    public  void  setUserRecords(UserRecords userRecords) {
        this.userRecords = userRecords;
    }

    public double getBalance() {
        return balance;
    }
    public void setChargeMode(String chargeMode) {
    ChargeMode = chargeMode;
}

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }
}
class MobilePhoneInProvinceCallRule extends CallChargeRule{
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double cost=0;
        if(callRecords.size()==0) {
            return 0;
        }
        for(int i=0;i<callRecords.size();i++) {
            long  a = (callRecords.get(i).endTime.getTime()-callRecords.get(i).startTime.getTime())/1000;
            long b = a%60;
            
                if(b==0&&
                        (callRecords.get(i).endTime.getTime()-callRecords.get(i).startTime.getTime())>0) {
                cost = cost + (a/60)*0.1;
                }else {
                    cost = cost + ((a-b)/60+1)*0.3;
                }
        }
        return cost;
    }
}
class MobilePhoneInProvinceAnswerRule extends CallChargeRule{
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double cost=0;
        return cost;
    }
}
class MobilePhoneInlandCallRule extends CallChargeRule{
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double cost=0;
        if(callRecords.size()==0) {
            return 0;
        }
        for(int i=0;i<callRecords.size();i++) {
            long  a = (callRecords.get(i).endTime.getTime()-callRecords.get(i).startTime.getTime())/1000;
            long b = a%60;
            
                if(b==0&&
                        (callRecords.get(i).endTime.getTime()-callRecords.get(i).startTime.getTime())>0) {
                cost = cost + (a/60)*0.1;
                }else {
                    cost = cost + ((a-b)/60+1)*0.6;
                }
        }
        return cost;
    }
}
class MobilePhoneInlandAnswerRule extends CallChargeRule{

    public double calCost(ArrayList<CallRecord> callRecords) {
        double cost=0;
        if(callRecords.size()==0) {
            return 0;
        }
        for(int i=0;i<callRecords.size();i++) {
            if(callRecords.get(i).answerAddressAreaCode.equals("ZG")) {
            long  a = (callRecords.get(i).endTime.getTime()-callRecords.get(i).startTime.getTime())/1000;
            long b = a%60;
            
                if(b==0&&
                        (callRecords.get(i).endTime.getTime()-callRecords.get(i).startTime.getTime())>0) {
                cost = cost + (a/60)*0.1;
                }else {
                    cost = cost + ((a-b)/60+1)*0.3;
                }
            }
        }
        return cost;
    }
    
}
class MobilePhoneInCityCallRule extends CallChargeRule{
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double cost=0;
        if(callRecords.size()==0) {
            return 0;
        }
        for(int i=0;i<callRecords.size();i++) {
            long  a = (callRecords.get(i).endTime.getTime()-callRecords.get(i).startTime.getTime())/1000;
            long b = a%60;
            
                if(b==0&&
                        (callRecords.get(i).endTime.getTime()-callRecords.get(i).startTime.getTime())>0) {
                cost = cost + (a/60)*0.1;
                }else {
                    if(callRecords.get(i).answerAddressAreaCode.equals("NC"))
                    cost = cost + ((a-b)/60+1)*0.1;
                    else if(callRecords.get(i).answerAddressAreaCode.equals("JX"))
                    cost = cost + ((a-b)/60+1)*0.2;
                    else 
                    cost = cost + ((a-b)/60+1)*0.3;
                }
        }
        return cost;
    }
}
class MobilePhoneInCityAnswerRule extends CallChargeRule{
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double cost=0;
        return cost;
    }
}
class MobilelinePhoneCharging extends ChargeMode{
    double monthlyRent = 15;
    
    public MobilelinePhoneCharging() {
        super();
    }
    
    @Override
    public double calCost(UserRecords userRecords) {
        
        MobilePhoneInCityCallRule a = new MobilePhoneInCityCallRule();
        MobilePhoneInlandCallRule b = new MobilePhoneInlandCallRule();
        MobilePhoneInProvinceCallRule c = new MobilePhoneInProvinceCallRule();
        MobilePhoneInCityAnswerRule d = new MobilePhoneInCityAnswerRule();
        MobilePhoneInProvinceAnswerRule e = new MobilePhoneInProvinceAnswerRule();
        MobilePhoneInlandAnswerRule f = new MobilePhoneInlandAnswerRule();
        return a.calCost(userRecords.getPhonecallingInCityRecords())+c.calCost(userRecords.getPhonecallingInProvinceRecords())+b.calCost(userRecords.getPhonecallingInLandRecords())+
                f.calCost(userRecords.getAnswerInLandRecords())+15;
    }
    
    @Override
    public double getMonthlyRent() {
        return monthlyRent;
    }
}
class MessageRecord {
    String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
class LandPhoneInProvinceRule extends CallChargeRule{
    
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double cost=0;
        if(callRecords.size()==0) {
            return 0;
        }
        for(int i=0;i<callRecords.size();i++) {
            long  a = (callRecords.get(i).endTime.getTime()-callRecords.get(i).startTime.getTime())/1000;
            long b = a%60;
            
                if(b==0&&
                        (callRecords.get(i).endTime.getTime()-callRecords.get(i).startTime.getTime())>0) {
                cost = cost + (a/60)*0.3;
                }else {
                    cost = cost + ((a-b)/60+1)*0.3;
                }
        }
        return cost;
    }
    
}
class LandPhoneInlandRule extends CallChargeRule{
    
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double cost=0;
        if(callRecords.size()==0) {
            return 0;
        }
        for(int i=0;i<callRecords.size();i++) {
            long  a = (callRecords.get(i).endTime.getTime()-callRecords.get(i).startTime.getTime())/1000;
            long b = a%60;
            
                if(b==0&&
                        (callRecords.get(i).endTime.getTime()-callRecords.get(i).startTime.getTime())>0) {
                cost = cost + (a/60)*0.6;
                }else {
                    cost = cost + ((a-b)/60+1)*0.6;
                }
        }
        return cost;
    }
}
class LandPhoneInCityRule extends CallChargeRule{
    
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double cost=0;
        if(callRecords.size()==0) {
            return 0;
        }
        for(int i=0;i<callRecords.size();i++) {
            long  a = (callRecords.get(i).endTime.getTime()-callRecords.get(i).startTime.getTime())/1000;
            long b = a%60;
            
                if(b==0&&
                        (callRecords.get(i).endTime.getTime()-callRecords.get(i).startTime.getTime())>0) {
                cost = cost + (a/60)*0.1;
                }else {
                    cost = cost + ((a-b)/60+1)*0.1;
                }
        }
        return cost;
    }
    
}
class LandlinePhoneCharging extends ChargeMode{
    double monthlyRent = 20;
    
    public LandlinePhoneCharging() {
        super();
    }
    
    @Override
    public double calCost(UserRecords userRecords) {
        LandPhoneInCityRule a = new LandPhoneInCityRule();
        LandPhoneInlandRule b = new LandPhoneInlandRule();
        LandPhoneInProvinceRule c = new LandPhoneInProvinceRule();
        return a.calCost(userRecords.getCallingInCityRecords())+b.calCost(userRecords.getCallingInLandRecords())+c.calCost(userRecords.getCallingInProvinceRecords())+20;
    }
    
    @Override
    public double getMonthlyRent() {
        return monthlyRent;
    }
}
abstract class CommunicationRecord {
    String callingNumber;
    String answerNumber;
    
    public String getCallingNumber() {
        return callingNumber;
    }
    
    public void setCallingNumber(String callingNumber) {
        this.callingNumber = callingNumber;
    }
    
    public String getAnswerNumber() {
        return answerNumber;
    }
    
    public void setAnswerNumber(String answerNumber) {
        this.answerNumber = answerNumber;
    }
    
}
abstract class ChargeRule {
    
}
abstract class ChargeMode {
    ArrayList<ChargeRule> chargeRules = new ArrayList<ChargeRule>();

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

    public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
        this.chargeRules = chargeRules;
    }
    
    public  double calCost(UserRecords userRecords) {
        return 0;
    }
    
    public double getMonthlyRent() {
        return 0;
    }
}
class CallRecord extends CommunicationRecord{
    Date startTime;
    Date endTime;
    String callingAddressAreaCode;
    String answerAddressAreaCode;
    
    SimpleDateFormat a = new SimpleDateFormat ("yyyy.MM.dd HH:mm:ss");
    
    public CallRecord() {
        super();
        // TODO 自动生成的构造函数存根
    }

    public Date getStartTime() {
        return startTime;
    }
    
    public void setStartTime(String startTime)   {
        try {
            this.startTime = a.parse(startTime);
        } catch (ParseException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }
    
    public Date getEndTime() {
        return endTime;
    }
    
    public void setEndTime(String endTime) {
        try {
            this.endTime = a.parse(endTime);
        } catch (ParseException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }
    
    public String getCallingAddressAreaCode() {
        return callingAddressAreaCode;
    }
    
    public void setCallingAddressAreaCode(String callingAddressAreaCode) {
        if(callingAddressAreaCode.matches("0791"))
        this.callingAddressAreaCode = "NC";
        else if(callingAddressAreaCode.matches("(079[0-9]|0701)")){
            this.callingAddressAreaCode = "JX";
        }else {
            this.callingAddressAreaCode = "ZG";
        }
    }
    
    public String getAnswerAddressAreaCode() {
        return answerAddressAreaCode;
    }
    
    public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
        if(answerAddressAreaCode.matches("0791"))
        this.answerAddressAreaCode = "NC";
        else if(answerAddressAreaCode.matches("(079[0-9]|0701)")){
            this.answerAddressAreaCode = "JX";
        }else {
            this.answerAddressAreaCode = "ZG";
        }
    }
    
    public boolean isMobilePhoneNumber(String Number) {
        if(Number.matches("(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8})")) {
            return true;
        }
        return false;
    }
    
}
abstract class CallChargeRule extends ChargeRule{
    
    public double calCost(ArrayList<CallRecord> callRecords) {
        return 0;
    }
    
}

本次作业难度较大,代码写的有点乱,首先用while循环确保循环输入,直到end退出程序,输入一条语句立刻进行判断,执行开户,或者记录操作,如果输入两个相同的号码,将取消开户,对于计费,使用的是记录操作,记录下后传入ArrayLis的用户中储存起来,最后用costcal函数进行计算,再返回结果并保留两位小数,在这之前使用comperto进行排序,再将用户的信息按顺序输出。中间过程比较繁杂,细节比较多,就不再一一赘述。类图如下。

 

 

实验5

一、实验目的

1、了解JavaFX的基本原理和基本组件;

2、能够熟练应用JavaFX组件进行界面设计;

3、掌握Java的事件驱动机制、lambda表达式;

4、掌握shape形状类的使用。

一、实验目的

1、了解JavaFX的基本原理和基本组件;

2、能够熟练应用JavaFX组件进行界面设计;

3、掌握Java的事件驱动机制、lambda表达式;

4、掌握shape形状类的使用。

一、实验步骤

1、  请修改实验四农夫过河游戏的代码,将用户界面改为图形界面,界面效果自行设计。

2、  请画出修改后的类图,并说明在界面中使用了哪些技巧以达到较好地界面效果。

3、  途中遇见了图片无法加载的问题图3-1,经过寻找,发现图片路径输入有误,修改路径后,程序正常运行。

4、  类图见代码末尾。图1-1

 

途中遇见了图片无法加载的问题,经过寻找,发现图片路径输入有误,修改路径后,程序正常运行。

通过按顺序点击农夫带羊过河—农夫独自过河—农夫带狼过河—农夫带羊过河—农夫带白菜过河—农夫独自过河—农夫带羊过河后,程序执行完毕,最终得到结果如见代码末尾2-1,程序正常运行,输出结果正常,点击农夫独自过河,得到结果如2-2,程序正常运行,输出正常。

通过javafx的实验让我掌握了基本的图形界面程序,让非专业用户能够一目了然程序的操作。将算法和用户操作分开,让程序维护和修改更容易,减少以后代码的修改量。过程中遇到了图片为空,无法加载的问题,后来发现是因为路径搞错了,修改路径以后正常运行。

类图及相关运行界面如下。

 

 

 

 

 

 

踩坑心得:

实验中存在不少细节问题,例如狼先吃羊,故羊无法再吃白菜,白菜存活,故需判断当羊进食时是否活着等等问题。本题可将农夫,狼,羊,白菜的共有属性放入一个父类中,用船来执行过河的操作,降低代码的耦合度,提高代码的复用性,方便日后修改添加新类。途中遇见了图片无法加载的问题,经过寻找,发现图片路径输入有误,修改路径后,程序正常运行。

改进建议:

建议多提供输入样例,方便进行判断,修善代码,多出些和所学契合的题目。

总结:

学会了使用抽象类,抽象方法继承与多态,使用容器类,降低代码的耦合度,提高复用性。同时学会了流的运用,文件流的读入写出等等操作,学会了使用异常处理,声明,抛出,抓取异常,优雅的终止错误。对于以上学习内容,对于容器类应该进一步学习和研究熟练的使用ArrayList , 学会了使用fx中的类,例如button等。希望老师能够提供答案或者上课讲一讲这些题目。



这篇关于Blog3的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程