第三次BOLG总结

前言

1.这次的大作业相对于点线型类的题目来说还是简单了许多的,没有点线型设计的那么复杂。

2.主要运用的知识有:

(1)类的设计

(2)多态

(3)容器

设计与分析

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 import java.util.ArrayList;
 2 import java.util.Scanner;
 3 public class Main{
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         Scanner in = new Scanner(System.in);
 8         ArrayList<String> str = new ArrayList<String>();
 9         int balance = 80;
10         String s = "";
11         while(!s.equals("end")) {
12             s = in.nextLine();
13             str.add(s);            
14         }
15         String[] s1 = str.get(0).split("u-| ");
16         String[] s2 = str.get(1).split("t-| ");
17         String[] s3 = s2[4].split(":");
18         String[] s4 = s2[6].split(":");
19         int[] n = new int[3];
20         int[] n1 = new int[3];
21         for(int i = 0; i < n.length; i++) {
22             n[i] = Integer.parseInt(s3[i]);
23             n1[i] = Integer.parseInt(s4[i]);
24         }
25         int t;
26         if(n1[2] - n[2] > 0) {
27             t = 60*(n1[0] - n[0]) + n1[1] -n[1] + 1;
28         }
29         else {
30             t = 60*(n1[0] - n[0]) + n1[1] -n[1];
31         }
32         
33          if(s2[2].matches("0791\\d{8,9}")) {
34             System.out.println(s1[1] + " " + String.format("%.1f",0.1*t) + " " + (80 - 0.1*t));
35 //             System.out.println(s2[2] + " " + "0.0" + " " + "80.0");
36         }
37         else if(s2[2].matches("079[234567890]\\d{8,9}"))
38         {
39             System.out.println(s1[1] + " " + String.format("%.1f",0.3*t) + " " + (80 - 0.3*t));
40         }
41         else
42         {
43             System.out.println(s1[1] + " " + String.format("%.1f",0.6*t) + " " + (80 - 0.6*t));
44 
45         }
46     }
47 
48 }
49 class User{
50     double balance = 100;
51     String number;
52 }
53 
54 class ChargeMode{
55     
56 }
57 
58 class UserRecord{
59     
60 }

这道题并不难,主要是类的设计,加深了我对类的理解,类与int、double、String这种的相似。

在第四题的第一题中需要用到动态数组加上正则表达式,把每一行的数字全部提取出来

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元。
每条通讯、短信信息均单独计费后累加,不是将所有信息累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。
错误处理:
输入数据中出现的不符合格式要求的行一律忽略。

 

import java.text.ParseException;
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> users = new ArrayList<User>();
        String string = input.nextLine();
        String datePatten = "((([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])\\.(" +
                "[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])";
        String patten1 = "[u][-][0][7][9][1][0-9]{7,8}\\s[0]";
        String patten2 = "[u][-][1][3][0-9]{9}\\s[1]";

        String patten3 = "[t]-0791[0-9]{7,8}\\s" + "0[0-9]{9,11}\\s"+datePatten + "\\s" + datePatten;//座机打座机
        String patten4 = "[t]-0791[0-9]{7,8}\\s" +"1[0-9]{10}\\s"+"0[0-9]{2,3}\\s"+datePatten + "\\s" + datePatten;//座机打手机
        String patten5 = "[t]-1[0-9]{10}\\s" +"0[0-9]{2,3}\\s"+"0[0-9]{10,11}\\s"+datePatten + "\\s" + datePatten;//手机打座机
        String patten6 = "[t]-1[0-9]{10}\\s" +"0[0-9]{2,3}\\s"+"1[0-9]{10}\\s"+"0[0-9]{2,3}\\s"+datePatten + "\\s" + datePatten;//手机打手机

        while (!string.equals("end")) {
            String str[] = string.split(" ");
            if (string.matches(patten1)) {
                String number = str[0].substring(2);
                if (!isUser(number,users)) {
                    User user = new User(number);
                    LandlinePhoneCharging landlinePhoneCharging = new LandlinePhoneCharging();
                    user.setChargeMode(landlinePhoneCharging);
                    users.add(user);
                }
            }else if (string.matches(patten2)) {
                String number = str[0].substring(2);
                if (!isUser(number,users)) {
                    User user = new User(number);
                    MobilelinePhoneCharing mobilePhoneCharging = new  MobilelinePhoneCharing();
                    user.setChargeMode(mobilePhoneCharging);
                    users.add(user);
                }
            }else if (string.matches(patten3)) {
                Date startTime = new Date();
                Date endTime = new Date();
                String callNum = str[0].substring(2);
                String answerNum = str[1];
                SimpleDateFormat content = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
                startTime = content.parse(str[2] + " " + str[3]);
                endTime = content.parse(str[4] + " "+ str[5]);
                CallRecord callRecord = new CallRecord(startTime,endTime,callNum,answerNum);
                setLinePhoneCallRecord(users,callNum,answerNum,callRecord);

            }else if (string.matches(patten4)) { //座机打手机
                Date startTime = new Date();
                Date endTime = new Date();
                String callNum = str[0].substring(2);
                String answerNum = str[1];
                SimpleDateFormat content = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
                startTime = content.parse(str[3] + " " + str[4]);
                endTime = content.parse(str[5] + " " + str[6]);
                CallRecord callRecord = new CallRecord(startTime,endTime,callNum,answerNum);
                if (isUser(callNum,users)) {
                    int n = findUserNum(callNum,users);
                    int check = checkLinePhone(str[2]);
                    if (check == 1) {
                        users.get(n).getUserRecords().addCallingInCityRecords(callRecord);
                    }else if (check == 2) {
                        users.get(n).getUserRecords().addCallingInProvinceRecords(callRecord);
                    }else if (check == 3) {
                        users.get(n).getUserRecords().addCallingInLandRecords(callRecord);
                    }
                }
                if (isUser(answerNum,users)){
                    int n = findUserNum(answerNum,users);
                    int check = checkLineToMobile(callNum,str[2]);
                    if (!callNum.substring(0,4).matches("[0][7]([9][0-9]|[0][1])") || !str[2].matches("[0][7]([9][0-9]|[0][1])")) {
                        users.get(n).getUserRecords().addAnswerOutProvinceRecords(callRecord);
                    }
                }
            }else if (string.matches(patten5)) { //手机打座机
                Date startTime = new Date();
                Date endTime = new Date();
                String callNum = str[0].substring(2);
                String answerNum = str[2];
                SimpleDateFormat content = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
                startTime = content.parse(str[3] + " " + str[4]);
                endTime = content.parse(str[5] + " " + str[6]);
                CallRecord callRecord = new CallRecord(startTime,endTime,callNum,answerNum);
                if (isUser(callNum,users)) {
                    int n = findUserNum(callNum,users);
                    int check = checkMobileToLine(str[1],answerNum.substring(0,4));
                    switch (check) {
                        case 1:
                            users.get(n).getUserRecords().addCallingInCityRecords(callRecord);break;
                        case 2:
                            users.get(n).getUserRecords().addCallingInProvinceRecords(callRecord);break;
                        case 3:
                            users.get(n).getUserRecords().addCallingInLandRecords(callRecord);break;
                        case 4:
                            users.get(n).getUserRecords().addCallingRecordsInProvince(callRecord);break;
                        case 5:
                            users.get(n).getUserRecords().addCallingOutProvinceRecords(callRecord);break;
                    }
                }
            }else if (string.matches(patten6)) { //手机互打
                Date startTime = new Date();
                Date endTime = new Date();
                String callNum = str[0].substring(2);
                String answerNum = str[2];
                SimpleDateFormat content = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
                startTime = content.parse(str[4] + " " + str[5]);
                endTime = content.parse(str[6] + " " + str[7]);
                CallRecord callRecord = new CallRecord(startTime,endTime,callNum,answerNum);
                setMobilePhoneCallRecord(users,callNum,answerNum,callRecord,str[1],str[3]);
            }
            string = input.nextLine();
        }
        Collections.sort(users, new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                return o1.getNumber().compareTo(o2.getNumber());
            }
        });
        for (int i = 0;i < users.size();i++) {
            System.out.println(String.format("%s %.1f %.1f",users.get(i).getNumber(),users.get(i).calCost(),
                    users.get(i).calBalance()));
        }
    }

    public static long calDiffFormTwoDate(Date date1,Date date2) {
        long startTime = date1.getTime();
        long endTime = date2.getTime();
        long minutes = (int)Math.ceil((endTime - startTime) / 1000.0 / 60);
        return minutes;
    }

    public static boolean isUser(String number,ArrayList<User> users) {
        for (int i = 0;i < users.size();i++) {
            if (number.equals(users.get(i).getNumber())) {
                return true;
            }
        }
        return false;
    }
    public static int findUserNum(String number,ArrayList<User> users) {
        for (int i = 0;i < users.size();i++) {
            if (number.equals(users.get(i).getNumber())) {
                return i;
            }
        }
        return 0;
    }
    public static int checkLinePhone(String number) {
        if (number.matches("[0][7][9][1]")) {
            return 1;
        }else if (number.matches("[0][7]([9][0|2-9]|[0][1])")) {
            return 2;
        }else {
            return 3;
        }
    }
    public static int checkLineToMobile(String s1,String s2) {
        if (s1.substring(0,5).matches(s2)) {
            return 1;
        }else if (s2.equals("079")) {
            return 2;
        }else {
            return 3;
        }
    }
    public static void setLinePhoneCallRecord(ArrayList<User> users,String number,String answer,CallRecord callRecord) {
        if (isUser(number,users)) {
            int n = findUserNum(number,users);
            int check = checkLinePhone(answer.substring(0,4));
            if (check == 1) {
                users.get(n).getUserRecords().addCallingInCityRecords(callRecord);
            }else if (check == 2) {
                users.get(n).getUserRecords().addCallingInProvinceRecords(callRecord);
            }else if (check == 3) {
                users.get(n).getUserRecords().addCallingInLandRecords(callRecord);
            }
        }
    }
    public static int checkMobileToLine(String s1,String s2) {
        if (s1.equals("0791")) {
           if (s2.equals("0791")) {
               return 1;
           }else if (s2.matches("[0][7]([9][0|2-9]|[0][1])")) {
               return 2;
           }else {
               return 3;
           }
        }else {
            if (s1.matches("[0][7]([9][0|2-9]|[0][1])")) {
                return 4;
            }else {
                return 5;
            }
        }
    }
    public static void setMobilePhoneCallRecord(ArrayList<User> users,String call,String answer,CallRecord callRecord,String s1,String s2) {
        if (isUser(call,users)) {     //主叫记录存储                  //手机互打时的记录存储
            int n = findUserNum(call,users);
            int check = checkMobileToLine(s1,s2);
            switch (check) {
                case 1:
                    users.get(n).getUserRecords().addCallingInCityRecords(callRecord);break;
                case 2:
                    users.get(n).getUserRecords().addCallingInProvinceRecords(callRecord);break;
                case 3:
                    users.get(n).getUserRecords().addCallingInLandRecords(callRecord);break;
                case 4:
                    users.get(n).getUserRecords().addCallingRecordsInProvince(callRecord);break;
                case 5:
                    users.get(n).getUserRecords().addCallingOutProvinceRecords(callRecord);break;
            }
        }
        if (isUser(answer,users)) { //接听记录存储,只存省外漫游
            int n  = findUserNum(answer,users);
            int check = checkMobileToLine(s2,s1);
            if (check == 5){
                users.get(n).getUserRecords().addAnswerOutProvinceRecords(callRecord);
            }
        }
    }

}




abstract class CallChargeRule extends ChargeRule{
    public abstract double calCost(ArrayList<CallRecord> callRecords);
}
class CallRecord extends CommunicationRecord{
    Date startTime;
    Date endTime;
    String callingAddressAreaCode;
    String answerAddressAreaCode;
    public Date getStartTime() {
        return startTime;
    }
    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }
    public Date getEndTime() {
        return endTime;
    }
    public CallRecord(Date startTime, Date endTime, String callingAddressAreaCode, String anserAddressAreaCode) {
        super();
        this.startTime = startTime;
        this.endTime = endTime;
        this.callingAddressAreaCode = callingAddressAreaCode;
        this.answerAddressAreaCode = anserAddressAreaCode;
    }
    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }
    public String getCallingAddressAreaCode() {
        return callingAddressAreaCode;
    }
    public void setCallingAddressAreaCode(String callingAddressAreaCode) {
        this.callingAddressAreaCode = callingAddressAreaCode;
    }
    public String getAnserAddressAreaCode() {
        return answerAddressAreaCode;
    }
    public void setAnserAddressAreaCode(String anserAddressAreaCode) {
        this.answerAddressAreaCode = anserAddressAreaCode;
    }
    
}
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 abstract double calCost(UserRecords userRecords);
    public abstract double getMonthlyRent();
}
abstract class ChargeRule {

}
abstract class CommunicationRecord {
    protected String callingNumber;
    protected String anserNumber;
    public String getCallingNumber() {
        return callingNumber;
    }
    public void setCallingNumber(String callingNumber) {
        this.callingNumber = callingNumber;
    }
    public String getAnserNumber() {
        return anserNumber;
    }
    public void setAnserNumber(String anserNumber) {
        this.anserNumber = anserNumber;
    }
    
    
}
class LandlinePhoneCharging extends ChargeMode {
    private double monthlyRent = 20;
    LandPhoneInCityRule landCity = new LandPhoneInCityRule();
    LandPhoneInLandRule landLand = new LandPhoneInLandRule();
    LandPhoneInProvinceRule landProvince = new LandPhoneInProvinceRule();
    @Override
    public double calCost(UserRecords userRecords) {
        // TODO Auto-generated method stub
        double cost =0;
        cost = cost+landCity.calCost(userRecords.getCallingInCityRecords())+landLand.calCost(userRecords.getCallingInLandRecords())+landProvince.calCost(userRecords.getCallingInProvinceRecords());
//        System.out.println(cost);
        return cost;
    }
    @Override
    public double getMonthlyRent() {
        // TODO Auto-generated method stub
        return monthlyRent;
    }
    
}
class LandPhoneInCityRule extends CallChargeRule{

    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double time = 0 ;
        double timereal ;
        double cost = 0;
        for(int i=0;i<callRecords.size();i++)
        {
            callRecords.get(i).getStartTime();
            callRecords.get(i).getEndTime();
            timereal = (callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime())/60000.0;
            if(timereal>(int)timereal)
            {
                time = (int)timereal+1;
                cost = cost + 0.1 * time;
            }
            else
            {
                time = (int)timereal;
                cost = cost + 0.1 * time;
            }
        }
        String cost1 = String.format("%.1f",cost);
        double cost2 = Double.parseDouble(cost1);
//        System.out.println(cost2);
        return cost2;
    }
    
}
class LandPhoneInLandRule extends CallChargeRule{

    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        // TODO Auto-generated method stub
        int time = 0 ;
        double timereal ;
        double cost = 0;
        for(int i=0;i<callRecords.size();i++)
        {
            callRecords.get(i).getStartTime();
            callRecords.get(i).getEndTime();
            timereal = (callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime())/60000.0;
            if(timereal>(int)timereal)
            {
                time = (int)timereal+1;
                cost = cost + 0.6 * time;    
            }
            else
            {
                time = (int)timereal;
                cost = cost + 0.6 * time;    
            }
        }        
        String cost1 = String.format("%.1f",cost);
        double cost2 = Double.parseDouble(cost1);
        return cost2;
    }
    

}
class LandPhoneInProvinceRule extends CallChargeRule{

    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double time = 0 ;
        double timereal ;
        double cost = 0;
        for(int i=0;i<callRecords.size();i++)
        {
            callRecords.get(i).getStartTime();
            callRecords.get(i).getEndTime();
            timereal = (callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime())/60000.0;
            if(timereal>(int)timereal)
            {
                time = (int)timereal+1;
                cost = cost + 0.3 * time;
            }
            else
            {
                time = (int)timereal;
                cost = cost + 0.3 * time;
            }
        }    
        String cost1 = String.format("%.1f",cost);
        double cost2 = Double.parseDouble(cost1);
        return cost2;
    }
    

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

    public String getMessage() {
        return message;
    }

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

}
class MobilelinePhoneCharing extends ChargeMode {
    private double monthlyRent = 15;
    MobilePhoneInCityRule mobileCity = new MobilePhoneInCityRule();
    MobilePhoneInLandRule mobileLand = new MobilePhoneInLandRule();
    MobilePhoneInProvinceRule mobileProvince = new MobilePhoneInProvinceRule();
    MobilePhoneCallInProvinceRule mobileCallInProvince = new MobilePhoneCallInProvinceRule();
    MobilePhoneCallOutProvinceRule mobileCallOutProvince = new MobilePhoneCallOutProvinceRule();
    MobilePhoneAnswerOutProvinceRule mobileAnswerOutProvince = new MobilePhoneAnswerOutProvinceRule();
    
    @Override
    public double calCost(UserRecords userRecords) {
        // TODO Auto-generated method stub
        double cost =0;
        cost = cost+mobileCity.calCost(userRecords.getCallingInCityRecords())+mobileLand.calCost(userRecords.getCallingInLandRecords())+mobileProvince.calCost(userRecords.getCallingInProvinceRecords());
//        System.out.println(cost);
        cost = cost+mobileCallInProvince.calCost(userRecords.getCallingRecordsInProvince())+mobileAnswerOutProvince.calCost(userRecords.getAnswerOutProvinceRecords())+mobileCallOutProvince.calCost(userRecords.getCallingOutProvinceRecords());
        return cost;
    }
    @Override
    public double getMonthlyRent() {
        // TODO Auto-generated method stub
        return monthlyRent;
    }
    
}
class MobilePhoneAnswerOutProvinceRule extends CallChargeRule{
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double time = 0 ;
        double timereal ;
        double cost = 0;
        for(int i=0;i<callRecords.size();i++)
        {
            callRecords.get(i).getStartTime();
            callRecords.get(i).getEndTime();
            timereal = (callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime())/60000.0;
            if(timereal>(int)timereal)
            {
                time = (int)timereal+1;
                cost = cost + 0.3 * time;
            }
            else
            {
                time = (int)timereal;
                cost = cost + 0.3 * time;
            }
        }
        String cost1 = String.format("%.1f",cost);
        double cost2 = Double.parseDouble(cost1);
//        System.out.println(cost2);
        return cost2;
    }
}
class MobilePhoneCallInProvinceRule extends CallChargeRule{
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double time = 0 ;
        double timereal ;
        double cost = 0;
        for(int i=0;i<callRecords.size();i++)
        {
            callRecords.get(i).getStartTime();
            callRecords.get(i).getEndTime();
            timereal = (callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime())/60000.0;
            if(timereal>(int)timereal)
            {
                time = (int)timereal+1;
                cost = cost + 0.3 * time;
            }
            else
            {
                time = (int)timereal;
                cost = cost + 0.3 * time;
            }
        }
        String cost1 = String.format("%.1f",cost);
        double cost2 = Double.parseDouble(cost1);
//        System.out.println(cost2);
        return cost2;
    }
}
class MobilePhoneCallOutProvinceRule extends CallChargeRule{
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double time = 0 ;
        double timereal ;
        double cost = 0;
        for(int i=0;i<callRecords.size();i++)
        {
            callRecords.get(i).getStartTime();
            callRecords.get(i).getEndTime();
            timereal = (callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime())/60000.0;
            if(timereal>(int)timereal)
            {
                time = (int)timereal+1;
                cost = cost + 0.6 * time;
            }
            else
            {
                time = (int)timereal;
                cost = cost + 0.6 * time;
            }
        }
        String cost1 = String.format("%.1f",cost);
        double cost2 = Double.parseDouble(cost1);
//        System.out.println(cost2);
        return cost2;
    }
}
class MobilePhoneInCityRule  extends CallChargeRule{

    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double time = 0 ;
        double timereal ;
        double cost = 0;
        for(int i=0;i<callRecords.size();i++)
        {
            callRecords.get(i).getStartTime();
            callRecords.get(i).getEndTime();
            timereal = (callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime())/60000.0;
            if(timereal>(int)timereal)
            {
                time = (int)timereal+1;
                cost = cost + 0.1 * time;
            }
            else
            {
                time = (int)timereal;
                cost = cost + 0.1 * time;
            }
        }
        String cost1 = String.format("%.1f",cost);
        double cost2 = Double.parseDouble(cost1);
//        System.out.println(cost2);
        return cost2;
    }
    
}
class MobilePhoneInLandRule  extends CallChargeRule{
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double time = 0 ;
        double timereal ;
        double cost = 0;
        for(int i=0;i<callRecords.size();i++)
        {
            callRecords.get(i).getStartTime();
            callRecords.get(i).getEndTime();
            timereal = (callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime())/60000.0;
            if(timereal>(int)timereal)
            {
                time = (int)timereal+1;
                cost = cost + 0.3 * time;
            }
            else
            {
                time = (int)timereal;
                cost = cost + 0.3 * time;
            }
        }
        String cost1 = String.format("%.1f",cost);
        double cost2 = Double.parseDouble(cost1);
//        System.out.println(cost2);
        return cost2;
    }
    
}
class MobilePhoneInProvinceRule  extends CallChargeRule{
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double time = 0 ;
        double timereal ;
        double cost = 0;
        for(int i=0;i<callRecords.size();i++)
        {
            callRecords.get(i).getStartTime();
            callRecords.get(i).getEndTime();
            timereal = (callRecords.get(i).getEndTime().getTime()-callRecords.get(i).getStartTime().getTime())/60000.0;
            if(timereal>(int)timereal)
            {
                time = (int)timereal+1;
                cost = cost + 0.2 * time;
            }
            else
            {
                time = (int)timereal;
                cost = cost + 0.2 * time;
            }
        }
        String cost1 = String.format("%.1f",cost);
        double cost2 = Double.parseDouble(cost1);
//        System.out.println(cost2);
        return cost2;
    }
    
}
class SendMessageRule extends MessageChargeRule{

    @Override
    public double calCost(ArrayList<MessageRecord> messageRecords) {
        // TODO Auto-generated method stub
        return 0;
    }
    

}
class User {
    UserRecords userRecords = new UserRecords();
    double balance = 100;
    ChargeMode chargeMode;
    String number;
    public User(String number) {
        super();
        this.number = number;
    }
    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;
    }
    public double calBalance() {
        balance = balance - chargeMode.getMonthlyRent()-calCost();
        return balance;
    }
    public double calCost() {
        return chargeMode.calCost(userRecords);
    }
    public double getBalance() {
        return balance;
    }
}
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> callingRecordsInProvince = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> callingOutProvinceRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerOutProvinceRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();
    private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();
    private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();
    public ArrayList<CallRecord> getCallingInCityRecords() {
        return callingInCityRecords;
    }
    public ArrayList<CallRecord> getCallingInProvinceRecords() {
        return callingInProvinceRecords;
    }
    public ArrayList<CallRecord> getCallingInLandRecords() {
        return callingInLandRecords;
    }
    public ArrayList<CallRecord> getAnswerInCityRecords() {
        return answerInCityRecords;
    }
    public ArrayList<CallRecord> getAnswerInProvinceRecords() {
        return answerInProvinceRecords;
    }
    public ArrayList<CallRecord> getAnswerInLandRecords() {
        return answerInLandRecords;
    }
    public ArrayList<MessageRecord> getSendMessageRecords() {
        return sendMessageRecords;
    }
    public ArrayList<MessageRecord> getReceiveMessageRecords() {
        return receiveMessageRecords;
    }
    public ArrayList<CallRecord> getCallingRecordsInProvince() {
        return callingRecordsInProvince;        
    }
    public ArrayList<CallRecord> getCallingOutProvinceRecords() {
        return callingOutProvinceRecords;
    }
    public ArrayList<CallRecord> getAnswerOutProvinceRecords() {
        return answerOutProvinceRecords;
    }
    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 answerRecord)
    {
        answerInCityRecords.add(answerRecord);
    }
    public void addAnswerInProvinceRecords(CallRecord answerRecord)
    {
        answerInProvinceRecords.add(answerRecord);
    }
    public void addAnswerInLandRecords(CallRecord answerRecord)
    {
        answerInLandRecords.add(answerRecord);
    }
    public void addSendMessageRecords(MessageRecord sendMeessageRecord)
    {
        sendMessageRecords.add(sendMeessageRecord);
    }
    public void addReceiveMessageRecords(MessageRecord receiveMessageRecord)
    {
        receiveMessageRecords.add(receiveMessageRecord);
    }
    public void addCallingRecordsInProvince(CallRecord callRecord)
    {
        callingRecordsInProvince.add(callRecord);
    }
    public void addCallingOutProvinceRecords(CallRecord callRecord)
    {
        callingOutProvinceRecords.add(callRecord);
    }
    public void addAnswerOutProvinceRecords(CallRecord callRecord)
    {
        answerOutProvinceRecords.add(callRecord);
    }
}

 

 

 

定义一个动态数组:ArrayList<类> 名称 = new ArrayList<类>();

使用add.()添加get.()提取,用使用Integer.parseInt()强制转换把字符串转换为整数

7-1 电信计费系列3-短信计费

  1 import java.math.BigDecimal;
  2 import java.util.ArrayList;
  3 import java.util.Collections;
  4 import java.util.Comparator;
  5 import java.util.Scanner;
  6 
  7 public class Main {
  8 
  9     public static void main(String[] args) {
 10         // TODO Auto-generated method stub
 11         Scanner in = new Scanner(System.in);
 12         String s = in.nextLine();
 13         String sent = "u-1\\d{10}\\s3";
 14         String answer = "m-1\\d{10}\\s" + "1\\d{10}\\s" + "[(a-z)\\d\\ \\.\\,]*";
 15         ArrayList<String[]> userRecords = new ArrayList<>();
 16         ArrayList<User> users = new ArrayList<>();
 17         while(!s.equals("end")) {
 18             User user = new User();
 19             if(s.matches(sent)) {
 20                 user.setNumber(s.split("\\s")[0].substring(2));
 21                 users.add(user);
 22         //        System.out.println(s.split("\\s")[1].substring(2));
 23             }
 24             if(s.matches(answer)) {
 25                 userRecords.add(s.split("\\s",3));
 26                 
 27                 for(int i = 0; i < users.size(); i++) {
 28                     if(users.get(i).getNumber().equals(s.split(" ",3)[0].substring(2))) {
 29                         users.get(i).setMessage(users.get(i).getMessage() + messageint(s.split(" ",3)[2].length()));
 30                         users.get(i).setCost(calcost(users.get(i).getMessage()));
 31                         
 32                     }
 33                     
 34                 }
 35                 
 36             }
 37             s = in.nextLine();
 38         }
 39         Collections.sort(users, new Comparator<User>() {
 40             public int compare(User user1, User user2) {
 41                 return user1.getNumber().compareTo(user2.getNumber());
 42             }
 43         });
 44         
 45         for(int i1 = 0; i1 < users.size(); i1++) {
 46             System.out.println(users.get(i1).getNumber() + " " + users.get(i1).getCost() + " " + (users.get(i1).getBalance() - users.get(i1).getCost()));
 47         }
 48         
 49         
 50         
 51     }
 52 
 53     public static int messageint(int n) {
 54         if(n > 10) {
 55             if(n%10 == 0) {
 56                 n = n/10;
 57             }
 58             else {
 59                 n = n/10 + 1;
 60             }
 61         }
 62         else {
 63             n = 1;
 64         }
 65         
 66         return n;
 67     }
 68     public static double calcost(int n) {
 69         double cost = 0;
 70         
 71         if(n <= 3) {
 72             cost = 0.1 * n;
 73         }
 74         else if(n > 3 && n <= 5) {
 75             cost = 0.3 + 0.2 * (n - 3);
 76         }
 77         else if(n > 5) {
 78             cost = 0.7 + 0.3 * (n - 5);
 79         }
 80         double result = new BigDecimal(cost).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
 81         return result;
 82     }
 83 }
 84 class User{
 85 
 86     private String number;
 87     private double balance = 100;
 88     private int message = 0;
 89     private double cost = 0;
 90     public double getCost() {
 91         return cost;
 92     }
 93 
 94     public void setCost(double cost) {
 95         this.cost = cost;
 96     }
 97 
 98     public String getNumber() {
 99         return number;
100     }
101 
102     public void setNumber(String number) {
103         this.number = number;
104     }
105 
106     
107 
108     public double getBalance() {
109         return balance;
110     }
111 
112     public void setBalance(double d) {
113         this.balance = d;
114     }
115 
116     public int getMessage() {
117         return message;
118     }
119 
120     public void setMessage(int message) {
121         this.message = message;
122     }
123 
124     
125 
126      
127     
128 }

这道题只需要记录每个开户用户的通讯记录,再对每一个用户遍历即可

 

总结

这次大作业的类的设计功能完善需要花一点时间来构思,第三次的作业有一个特别需要注意的点就是没条短信每条短信信息均单独计费后累加,不是将所有信息累计后统一计费。

posted @ 2022-06-11 22:59  木马*  阅读(77)  评论(0)    收藏  举报