首页 > 其他分享 >BLOG3

BLOG3

时间:2022-12-08 16:33:14浏览次数:56  
标签:String double BLOG3 callRecord ArrayList 计费 public

一、前言

  1、 知识点 

  • 类和对象:

    类(class)和对象(object)是两种以计算机为载体的计算机语言的合称。对象是对客观事物的抽象,类是对对象的抽象。类是一种抽象的数据类型。它们的关系是,对象是类的实例,类是对象的模板。对象是通过new classname产生的,用来调用类的方法;类的构造方法 。

  • 对象交互:

        当一个对象里有多个对象的时候,那些对象之间是如何交互的,对象和对象之间的联系是如何建立的,对象如何和其他对象交流。

  • 继承与多态:

继承:就是保持已有类的特性而构造新类的过程。继承后,子类能够利用父类中定义的变量和方法,就像它们属于子类本身一样。

        多态:(1)一个类继承自某个父类时,对父类中的方法进行改写。

                    (2)重载是两个方法的名称相同,但参数不同,重载与多态没有关系。

                    (3)抽象类。

                    (4)接口(interface)

  • 对象容器:

Java具有丰富的容器,Java的容器具有丰富的功能和良好的性能。熟悉并能充分有效地利用好容器,是现代程序设计的基本能力。

  • 设计原则:

(1)单一职责原则

(2)里氏替换原则

(3)依赖倒置原则

(4)接口隔离原则

(5)迪米特法则(最少知道原则)

(6)开闭原则

  • 抽象: 

        JAVA语言中,用abstract 关键字来修饰一个类时,这个类叫作抽象类。抽象类是它的所有子类的公共属性的集合,是包含一个或多

  个抽象方法的类。 抽象类可以看作是对类的进一步抽象。在面向对象领域,抽象类主要用来进行类型隐藏。

  • 题目数量及难度
  1. 题目集6:7-1 电信计费系列1-座机计费(难,第一次做不会)
  2. 题目集7:7-1 电信计费系列2-手机+座机计费(难,要处理的模块实在是多)
  3. 题目集8:7-1 电信计费系列3-短信计费(正常)

  2、设计与分析

  • 题目集6:7-1 电信计费系列1-座机计费

 实现南昌市电信分公司的计费程序,假设该公司针对手机和座机用户分别采取了两种计费方案,分别如下:

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

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

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

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

本题只做格式的错误判断,无需做内容上不合理的判断,比如同一个电话两条通讯记录的时间有重合、开户号码非南昌市的号码等,此类情况都当成正确的输入计算。但时间的输入必须符合要求,比如不能输入2022.13.61 28:72:65。
 

建议类图:
参见图1、2、3:

image.png
图1

图1中User是用户类,包括属性:
userRecords (用户记录)、balance(余额)、chargeMode(计费方式)、number(号码)。
ChargeMode是计费方式的抽象类:
chargeRules是计费方式所包含的各种计费规则的集合,ChargeRule类的定义见图3。
getMonthlyRent()方法用于返回月租(monthlyRent)。
UserRecords是用户记录类,保存用户各种通话、短信的记录,    
各种计费规则将使用其中的部分或者全部记录。
其属性从上到下依次是:
市内拨打电话、省内(不含市内)拨打电话、省外拨打电话、
市内接听电话、省内(不含市内)接听电话、省外接听电话的记录
以及发送短信、接收短信的记录。
 

image.png

图2

图2中CommunicationRecord是抽象的通讯记录类:
包含callingNumber拨打号码、answerNumber接听号码两个属性。
CallRecord(通话记录)、MessageRecord(短信记录)是它的子类。CallRecord(通话记录类)包含属性:
通话的起始、结束时间以及
拨号地点的区号(callingAddressAreaCode)、接听地点的区号(answerAddressAreaCode)。
区号用于记录在哪个地点拨打和接听的电话。座机无法移动,就是本机区号,如果是手机号,则会有差异。
 

image.png
图3

图3是计费规则的相关类,这些类的核心方法是:
calCost(ArrayList<CallRecord> callRecords)。
该方法针根据输入参数callRecords中的所有记录计算某用户的某一项费用;如市话费。
输入参数callRecords的约束条件:必须是某一个用户的符合计费规则要求的所有记录。
SendMessageRule是发送短信的计费规则类,用于计算发送短信的费用。
LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三个类分别是座机拨打市内、省内、省外电话的计费规则类,用于实现这三种情况的费用计算。    
 

(提示:可以从UserRecords类中获取各种类型的callRecords)。
注意:以上图中所定义的类不是限定要求,根据实际需要自行补充或修改。

 

1,类图:

 

 

 

 源码:

import java.util.*;
import java.text.DecimalFormat;
 
public class Main {
    public static void main(String[] args) {
        Scanner scanner  = new Scanner(System.in);
        String n =  scanner.nextLine();
        String[] n1 = n.split(" ");
        String[] user = n1[0].split("-");
        System.out.print(user[1]+' ');
        double balance = 100;
        double all=0;
        while(true)
        {
            double cost = 0;
            double h=0,f=0,m=0,d=0;
            double totol=0;
            double h1=0,h2=0,f1=0,f2=0,m1=0,m2=0,d1=0,d2=0;
            String information =  scanner.next();
            if(information.equals("end"))
            {
                break;
            }
            String rNumber = scanner.next();
            String sDay =  scanner.next();
            String sTime =  scanner.next();
            String eDay =  scanner.next();
            String eTime =  scanner.next();
             
            String start =information + " " +rNumber+ " " +sDay + " " + sTime + " " +eDay + " " + eTime;
            Input input = new Input();
            
            if(input.judgeInput(start))
            {
                String[] n2 = information.split("-");
                    String[] number = information.split("-");
                    String[] time1 = sTime.split(":");
                    String[] time2 = eTime.split(":");
                    h1=Double.parseDouble(time1[0]);
                    h2=Double.parseDouble(time2[0]);
                    f1=Double.parseDouble(time1[1]);
                    f2=Double.parseDouble(time2[1]);
                    m1=Double.parseDouble(time1[2]);
                    m2=Double.parseDouble(time2[2]);
                    h=(h2-h1)*60;
                    f=f2-f1;
                    m=m2-m1;
                    if(m>0)
                    {
                        f++;
                    }
                    totol=h+f+d;
                    if(sDay.equals(eDay))
                        totol=totol;
                    else
                        totol=totol+1440;
     
                    char str[] = rNumber.toCharArray();
                    if(str[0]=='0' && str[1]=='7' && str[2]=='9' && str[3]=='1')
                    {
                        cost=totol*0.1;
                    }
                    else if(str[0]=='0' && str[1]=='7' && str[2]=='9' && str[3]!='1')
                    {
                        cost=totol*0.3;
                    }
                    else if(str[0]=='0' && str[1]=='7' && str[2]=='0' && str[3]=='1')
                    {
                        cost=totol*0.3;
                    }
                    else
                        cost=totol*0.6;
                    if(rNumber.equals(user[1]))
                        cost=0;
                    balance=balance-cost;
                    all=all+cost;
            }
            else
                cost=0;
        }
        balance=balance-20;
        System.out.print(new DecimalFormat("0.0#").format(all)+" ");
        System.out.print(new DecimalFormat("0.0#").format(balance));    
    }
}

class Input{
    String source ="[t]-0791[0-9]{7,8}\\s" + "0[0-9]{9,11}\\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})\\.(((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])";
     public boolean judgeInput(String start) {
         if(start.matches(source))
             return true;
         else
             return false;
     }
}
View Code

分析:这是电信计费系列的第一个题目,是整个系列的基础,要求的功能并不复杂只需要考虑电话的计费。LandlinePhoneCharging可以作为一个总和计算,将所有类型的通话方式都放入该类进行计算并且得到结果,LandPhoneInCityRule计算座机在城市里面的话费,LandPhoneInlandRule计算座机在省内里面的话费,LandPhoneInProvinceRule是对于省外的计算座机的话费,我们也就是利用这三个类来放入LandPhoneInCityRule进行计算得到我们所最后需要的结果。User用于储存用户的,UserRecords储存各种通话或者手机通话的所有信息,方便后续提取信息。

 

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

 

本题只做格式的错误判断,无需做内容上不合理的判断,比如同一个电话两条通讯记录的时间有重合、开户号码非南昌市的号码等,此类情况都当成正确的输入计算。但时间的输入必须符合要求,比如不能输入2022.13.61 28:72:65。

 

 

 

建议类图:
参见图1、2、3:

 

image.png
图1

 

图1中User是用户类,包括属性:
userRecords (用户记录)、balance(余额)、chargeMode(计费方式)、number(号码)。
ChargeMode是计费方式的抽象类:
chargeRules是计费方式所包含的各种计费规则的集合,ChargeRule类的定义见图3。
getMonthlyRent()方法用于返回月租(monthlyRent)。
UserRecords是用户记录类,保存用户各种通话、短信的记录,    
各种计费规则将使用其中的部分或者全部记录。
其属性从上到下依次是:
市内拨打电话、省内(不含市内)拨打电话、省外拨打电话、
市内接听电话、省内(不含市内)接听电话、省外接听电话的记录
以及发送短信、接收短信的记录。

 

 

 

image.png

 

图2

 

图2中CommunicationRecord是抽象的通讯记录类:
包含callingNumber拨打号码、answerNumber接听号码两个属性。
CallRecord(通话记录)、MessageRecord(短信记录)是它的子类。CallRecord(通话记录类)包含属性:
通话的起始、结束时间以及
拨号地点的区号(callingAddressAreaCode)、接听地点的区号(answerAddressAreaCode)。
区号用于记录在哪个地点拨打和接听的电话。座机无法移动,就是本机区号,如果是手机号,则会有差异。

 

 

 

image.png
图3

 

图3是计费规则的相关类,这些类的核心方法是:
calCost(ArrayList<CallRecord> callRecords)。
该方法针根据输入参数callRecords中的所有记录计算某用户的某一项费用;如市话费。
输入参数callRecords的约束条件:必须是某一个用户的符合计费规则要求的所有记录。
SendMessageRule是发送短信的计费规则类,用于计算发送短信的费用。
LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三个类分别是座机拨打市内、省内、省外电话的计费规则类,用于实现这三种情况的费用计算。    

 

 

 

(提示:可以从UserRecords类中获取各种类型的callRecords)。
注意:以上图中所定义的类不是限定要求,根据实际需要自行补充或修改。

 

1,类图:

 

 源码:

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

public class Main {
    public static void main(String[] args) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        Scanner input = new Scanner(System.in);
        TreeMap<String,User> users = new TreeMap<>();
        String s = input.nextLine();

        String judge = "u-0791([0-9]{7,8})\\s0";


        while( !s.equals("end")){

            String judge1 = "t-0791([0-9]{7,8})\\s([0-9]{10,12})\\s\\d{4}\\.([1-9]|1[012])\\.([1-9]|([1-2][0-9])|[3][0,1])\\s((0[0-9])|(1[0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])\\s\\d{4}\\.([1-9]|1[012])\\.([1-9]|([1-2][0-9])|[3][0,1])\\s((0[0-9])|(1[0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])";
            String judge3 = "t-0791([0-9]{7,8})\\s1([0-9]{10})\\s\\d{3,4}\\s\\d{4}\\.([1-9]|1[012])\\.([1-9]|([1-2][0-9])|[3][0,1])\\s((0[0-9])|(1[0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])\\s\\d{4}\\.([1-9]|1[012])\\.([1-9]|([1-2][0-9])|[3][0,1])\\s((0[0-9])|(1[0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])";
            String judge4 = "t-1([0-9]{10})\\s\\d{3,4}\\s([0-9]{10,12})\\s\\d{4}\\.([1-9]|1[012])\\.([1-9]|([1-2][0-9])|[3][0,1])\\s((0[0-9])|(1[0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])\\s\\d{4}\\.([1-9]|1[012])\\.([1-9]|([1-2][0-9])|[3][0,1])\\s((0[0-9])|(1[0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])";
            String judge7 = "t-1([0-9]{10})\\s\\d{3,4}\\s1([0-9]{10})\\s\\d{3,4}\\s\\d{4}\\.([1-9]|1[012])\\.([1-9]|([1-2][0-9])|[3][0,1])\\s((0[0-9])|(1[0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])\\s\\d{4}\\.([1-9]|1[012])\\.([1-9]|([1-2][0-9])|[3][0,1])\\s((0[0-9])|(1[0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])";
            String judge2 = "u-1([0-9]{10})\\s1";
            String judge5 = "07((9[0-9])|(01))";
            String judge8 = "0791([0-9]{7,8})";
            if(s.matches(judge)||s.matches(judge2)) {
                String []s1 = s.split("-| ");
                users.put(s1[1], new User(new UserRecords(), 100, new LandlinePhoneCharging()));
            }
            else if (s.matches(judge1)||s.matches(judge3)||s.matches(judge4)||s.matches(judge7)) {
                String []s1 = s.split("-| ");
                if(s1.length==7) {
                    for (Map.Entry<String, User> entry : users.entrySet()) {
                        String c1 = s1[3] + ' ' + s1[4];
                        String c2 = s1[5] + ' ' + s1[6];
                        try {
                            Date date1 = dateFormat.parse(c1);
                            Date date2 = dateFormat.parse(c2);
                            if (s1[1].equals(entry.getKey())) {
                                CallRecord callRecord = new CallRecord(date1, date2);
                                if (s1[2].charAt(0) == '0' && s1[2].charAt(1) == '7' && s1[2].charAt(2) == '9' && s1[2].charAt(3) == '1') {
                                    entry.getValue().userRecords.callingInCityRecords.add(callRecord);
                                } else if ((s1[2].charAt(0) == '0' && s1[2].charAt(1) == '7' && ((s1[2].charAt(2) == '9' && s1[2].charAt(3) >= '0' && s1[2].charAt(3) <= '9') || (s1[2].charAt(2) == '0' && s1[2].charAt(3) == '1')))) {
                                    entry.getValue().userRecords.callingInProvinceRecords.add(callRecord);
                                } else {
                                    entry.getValue().userRecords.callingInLandRecords.add(callRecord);
                                }
                            }
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                }else if(s1.length == 8&&s1[3].length()<=4){
                    String c1 = s1[4] + ' ' + s1[5];
                    String c2 = s1[6] + ' ' + s1[7];
                    try {
                        Date date11 = dateFormat.parse(c1);
                        Date date22 = dateFormat.parse(c2);
                        for (Map.Entry<String, User> entry1 : users.entrySet()) {
                            if (s1[2].equals(entry1.getKey())) {
                                CallRecord callRecord = new CallRecord(date11, date22);
                                if (!s1[3].matches(judge5)) {
                                    entry1.getValue().userRecords.answerInLandRecords.add(callRecord);
                                }
                            }
                        }
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    for (Map.Entry<String, User> entry : users.entrySet()) {
                        try {
                            Date date1 = dateFormat.parse(c1);
                            Date date2 = dateFormat.parse(c2);
                            if (s1[1].equals(entry.getKey())) {
                                CallRecord callRecord = new CallRecord(date1, date2);
                                if (s1[3].equals("0791")) {
                                    entry.getValue().userRecords.callingInCityRecords.add(callRecord);
                                } else if (s1[3].matches(judge5)) {
                                    entry.getValue().userRecords.callingInProvinceRecords.add(callRecord);
                                } else {
                                    entry.getValue().userRecords.callingInLandRecords.add(callRecord);
                                }
                            }
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                }else if(s1.length ==9){
                    String c1 = s1[5] + ' ' + s1[6];
                    String c2 = s1[7] + ' ' + s1[8];
                    try {
                        Date date11 = dateFormat.parse(c1);
                        Date date22 = dateFormat.parse(c2);
                    for (Map.Entry<String, User> entry1 : users.entrySet()) {
                        if (s1[3].equals(entry1.getKey())) {
                            CallRecord callRecord = new CallRecord(date11, date22);
                            if (!s1[4].matches(judge5)) {
                                entry1.getValue().userRecords.answerInLandRecords.add(callRecord);
                            }
                        }
                    }
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    for (Map.Entry<String, User> entry : users.entrySet()) {

                        try {
                            Date date1 = dateFormat.parse(c1);
                            Date date2 = dateFormat.parse(c2);
                            if (s1[1].equals(entry.getKey())) {
                                CallRecord callRecord = new CallRecord(date1, date2);
                                if (s1[2].equals("0791")) {
                                    if (s1[4].equals("0791")) {
                                        entry.getValue().userRecords.callingInCityRecords.add(callRecord);
                                    } else if (s1[4].matches(judge5)) {
                                        entry.getValue().userRecords.callingInProvinceRecords.add(callRecord);
                                    } else {
                                        entry.getValue().userRecords.callingInLandRecords.add(callRecord);
                                    }
                                }else if(s1[2].matches(judge5)){
                                    entry.getValue().userRecords.MYCallingInProvinceRecords.add(callRecord);
                                }else{
                                    entry.getValue().userRecords.MYCallingInLandRecords.add(callRecord);
                                }
                        }} catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                }else{
                    String c1 = s1[4] + ' ' + s1[5];
                    String c2 = s1[6] + ' ' + s1[7];
                    for (Map.Entry<String, User> entry : users.entrySet()) {
                        try {
                            Date date1 = dateFormat.parse(c1);
                            Date date2 = dateFormat.parse(c2);
                            if (s1[1].equals(entry.getKey())) {
                                CallRecord callRecord = new CallRecord(date1, date2);
                                if(s1[2].equals("0791")&&s1[3].matches(judge8)){
                                    entry.getValue().userRecords.callingInCityRecords.add(callRecord);
                                }else if(s1[2].equals("0791")&&s1[3].matches(judge5)){
                                    entry.getValue().userRecords.callingInProvinceRecords.add(callRecord);
                                }else if(s1[2].equals("0791")){
                                    entry.getValue().userRecords.callingInLandRecords.add(callRecord);
                                }
                                else if(s1[2].matches(judge5)){
                                    entry.getValue().userRecords.MYCallingInProvinceRecords.add(callRecord);
                                }else{
                                    entry.getValue().userRecords.MYCallingInLandRecords.add(callRecord);
                                }
                            }} catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            s = input.nextLine();
        }
        for (Map.Entry<String, User> entry : users.entrySet()) {
            if (entry.getKey().charAt(0) == '0') {
                System.out.print(entry.getKey() + " ");
                System.out.printf("%.1f %.1f\n", (new LandlinePhoneCharging().calCost(entry.getValue().userRecords) - 20), (100 - new LandlinePhoneCharging().calCost(entry.getValue().userRecords)));
            }else{
                System.out.print(entry.getKey() + " ");
                System.out.printf("%.1f %.1f\n",new MobPCharging().calCost(entry.getValue().userRecords)-15,100-new MobPCharging().calCost(entry.getValue().userRecords));

            }
        }

    }
}

abstract class CallChargeRule extends HowCharge{
    public double calCost (ArrayList<CallRecord> callRecords){
        return 0;
    }
}
class CallRecord {
     private Date startTime;
     private Date endTime;
     private String callingAddressAreaCode;
     private String answerAddressAreaCode;

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

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

    public long Day(){
            long stateTimeLong = getStartTime().getTime();
            long endTimeLong = getEndTime().getTime();
            long second = (endTimeLong-stateTimeLong)/1000;
            return  second;
    }

    public Date getStartTime() {
        return startTime;
    }

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

    public Date getEndTime() {
        return endTime;
    }

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

    public String getCallingAddressAreaCode() {
        return callingAddressAreaCode;
    }

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

    public String getAnswerAddressAreaCode() {
        return answerAddressAreaCode;
    }

    public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
        this.answerAddressAreaCode = answerAddressAreaCode;
    }
}
abstract class ChargeMode {
    ArrayList <HowCharge> arrayList = new ArrayList<>();

    public ChargeMode() {
        this.arrayList = arrayList;
    }

    public ArrayList<HowCharge> getArrayList() {
        return arrayList;
    }

    public void setArrayList(ArrayList<HowCharge> arrayList) {
        this.arrayList = arrayList;
    }

    public double calCost (UserRecords userRecords){
       return 0;
    }

    public double getMonthlyRent (){
        return 20;
    }
}
class HowCharge {
}
abstract class CommunicationRecord {
    private String callingNumber;
    private String answerNumber;

    public String getCallingNumber() {
        return callingNumber;
    }

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

    public String getAnswerNumber() {
        return answerNumber;
    }

    public void setAnswerNumber(String answerNumber) {
        this.answerNumber = answerNumber;
    }
}
class LandlinePhoneCharging extends ChargeMode{
    double  monthlyRent = 20;
    public LandlinePhoneCharging() {
        super();
    }

    public double calCost (UserRecords userRecords){
        LandPhoneInlandRule landPhoneInlandRule = new LandPhoneInlandRule();
        LandPhoneInCityRule landPhoneInCityRule = new LandPhoneInCityRule();
        LandPhoneInProvinceRule landPhoneInProvinceRule = new LandPhoneInProvinceRule();

      return landPhoneInCityRule.calCost(userRecords.callingInCityRecords)+landPhoneInProvinceRule.calCost(userRecords.callingInProvinceRecords)
              +landPhoneInlandRule.calCost(userRecords.callingInLandRecords)+getMonthlyRent();
    }

    public double getMonthlyRent (){
          return monthlyRent;
    }
}

class LandPhoneInCityRule extends CallChargeRule{
    public double calCost (ArrayList<CallRecord> callRecords){
        double sum = 0;
        for (CallRecord callRecord : callRecords) {
            sum+=Math.ceil(callRecord.Day()/60.0);
        }
        return sum*0.1;
    }
}
class LandPhoneInlandRule extends CallChargeRule{
    public double calCost (ArrayList<CallRecord> callRecords){
        double sum = 0;
        for (CallRecord callRecord : callRecords) {
            sum+=Math.ceil(callRecord.Day()/60.0);
        }
        return sum*0.6;
    }
    public double phoneCalCost (ArrayList<CallRecord> callRecords){
        double sum = 0;
        for (CallRecord callRecord : callRecords) {
            sum+=Math.ceil(callRecord.Day()/60.0);
        }
        return sum*0.3;
    }

    public double answerInLandCalCost(ArrayList<CallRecord> callRecords){
        double sum = 0;
        for (CallRecord callRecord : callRecords) {
            sum+=Math.ceil(callRecord.Day()/60.0);
        }
        return sum*0.3;
    }

    public double manYouCalCost(ArrayList<CallRecord> callRecords){
        double sum = 0;
        for (CallRecord callRecord : callRecords) {
            sum+=Math.ceil(callRecord.Day()/60.0);
        }
        return sum*0.6;
    }
}
class LandPhoneInProvinceRule extends CallChargeRule{
    public double calCost (ArrayList<CallRecord> callRecords){
        double sum = 0;
        for (CallRecord callRecord : callRecords) {
            sum+=Math.ceil(callRecord.Day()/60.0);
        }
        return sum*0.3;
    }

    public double phoneCalCost(ArrayList<CallRecord> callRecords){
        double sum = 0;
        for (CallRecord callRecord : callRecords) {
            sum+=Math.ceil(callRecord.Day()/60.0);
        }
        return sum*0.2;
    }

    public double manYouCalCost(ArrayList<CallRecord> callRecords){
        double sum = 0;
        for (CallRecord callRecord : callRecords) {
            sum+=Math.ceil(callRecord.Day()/60.0);
        }
        return sum*0.3;
    }
}
class MobPCharging extends ChargeMode{
    double  monthlyRent = 15;
    public MobPCharging() {
        super();
    }

    public double calCost (UserRecords userRecords){
        LandPhoneInlandRule landPhoneInlandRule = new LandPhoneInlandRule();
        LandPhoneInCityRule landPhoneInCityRule = new LandPhoneInCityRule();
        LandPhoneInProvinceRule landPhoneInProvinceRule = new LandPhoneInProvinceRule();


        return landPhoneInCityRule.calCost(userRecords.callingInCityRecords)+landPhoneInProvinceRule.phoneCalCost(userRecords.callingInProvinceRecords)
                +landPhoneInlandRule.phoneCalCost(userRecords.callingInLandRecords)+landPhoneInlandRule.answerInLandCalCost(userRecords.answerInLandRecords)+
                landPhoneInProvinceRule.manYouCalCost(userRecords.MYCallingInProvinceRecords)+getMonthlyRent()+
                landPhoneInlandRule.manYouCalCost(userRecords.MYCallingInLandRecords);
    }

    public double getMonthlyRent (){
        return monthlyRent;
    }
}
class User {
    UserRecords userRecords = new UserRecords();
    private double balance = 100;
    LandlinePhoneCharging landlinePhoneCharging;
    private String number;

    public User(UserRecords userRecords, double balance, LandlinePhoneCharging landlinePhoneCharging) {
        this.userRecords = userRecords;
        this.balance = balance;
        this.landlinePhoneCharging = landlinePhoneCharging;
    }

    public User(UserRecords userRecords) {
    }

    public double calBalance(){
        return 0;
    }

    public UserRecords getUserRecords() {
        return userRecords;
    }

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

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public LandlinePhoneCharging getLandlinePhoneCharging() {
        return landlinePhoneCharging;
    }

    public void setLandlinePhoneCharging(LandlinePhoneCharging landlinePhoneCharging) {
        this.landlinePhoneCharging = landlinePhoneCharging;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }
}
class UserRecords {
    ArrayList <CallRecord> callingInCityRecords = new ArrayList<>();
    ArrayList <CallRecord> callingInProvinceRecords = new ArrayList<>();
    ArrayList <CallRecord> callingInLandRecords = new ArrayList<>();
    ArrayList <CallRecord> MYCallingInProvinceRecords = new ArrayList<>();
    ArrayList <CallRecord> MYCallingInLandRecords = new ArrayList<>();

    ArrayList <CallRecord> answerInCityRecords = new ArrayList<>();
    ArrayList <CallRecord> answerInProvinceRecords = new ArrayList<>();
    ArrayList <CallRecord> answerInLandRecords = new ArrayList<>();

    ArrayList <MessageRecord> sendMessageRecords = new ArrayList<>();
    ArrayList <MessageRecord> receiveMessageRecords = new ArrayList<>();



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

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

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

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

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

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

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

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

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


}
class MessageRecord {
    private String message;

    public String getMessage() {
        return message;
    }

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

分析:第二次题目迭代中添加了手机操作,相比于第一次单独的电信计费,增加了手机的一些功能,因为手机和座机的互通,手机与手机的互通以及不同情况的收费标准。添加了MobliePhoneCharging来计算手机的所有计费,同时在所有座机通话的不同区域的计算类里面添加手机对应的不同区域的方法来进行分步计算,达到手机计费的目的,重要的还是要考虑清楚情况(很多容易漏掉)。有一个不得不强调的点——正则表达式,不合法的输入测试点很多,这题有很多奇奇怪怪的输入正确与否的测试很容易失分。(我就是失分比较多)

  • 题目集8:7-1 电信计费系列3-短信计费

 

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

 

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

 

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

 

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

 

本题只做格式的错误判断,无需做内容上不合理的判断,比如同一个电话两条通讯记录的时间有重合、开户号码非南昌市的号码等,此类情况都当成正确的输入计算。但时间的输入必须符合要求,比如不能输入2022.13.61 28:72:65。

 

 

 

建议类图:
参见图1、2、3:

 

image.png
图1

 

图1中User是用户类,包括属性:
userRecords (用户记录)、balance(余额)、chargeMode(计费方式)、number(号码)。
ChargeMode是计费方式的抽象类:
chargeRules是计费方式所包含的各种计费规则的集合,ChargeRule类的定义见图3。
getMonthlyRent()方法用于返回月租(monthlyRent)。
UserRecords是用户记录类,保存用户各种通话、短信的记录,    
各种计费规则将使用其中的部分或者全部记录。
其属性从上到下依次是:
市内拨打电话、省内(不含市内)拨打电话、省外拨打电话、
市内接听电话、省内(不含市内)接听电话、省外接听电话的记录
以及发送短信、接收短信的记录。

 

 

 

image.png

 

图2

 

图2中CommunicationRecord是抽象的通讯记录类:
包含callingNumber拨打号码、answerNumber接听号码两个属性。
CallRecord(通话记录)、MessageRecord(短信记录)是它的子类。CallRecord(通话记录类)包含属性:
通话的起始、结束时间以及
拨号地点的区号(callingAddressAreaCode)、接听地点的区号(answerAddressAreaCode)。
区号用于记录在哪个地点拨打和接听的电话。座机无法移动,就是本机区号,如果是手机号,则会有差异。

 

 

 

image.png
图3

 

图3是计费规则的相关类,这些类的核心方法是:
calCost(ArrayList<CallRecord> callRecords)。
该方法针根据输入参数callRecords中的所有记录计算某用户的某一项费用;如市话费。
输入参数callRecords的约束条件:必须是某一个用户的符合计费规则要求的所有记录。
SendMessageRule是发送短信的计费规则类,用于计算发送短信的费用。
LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三个类分别是座机拨打市内、省内、省外电话的计费规则类,用于实现这三种情况的费用计算。    

 

 

 

(提示:可以从UserRecords类中获取各种类型的callRecords)。
注意:以上图中所定义的类不是限定要求,根据实际需要自行补充或修改。

 

1,类图:

 

 

 源码:

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

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        TreeMap<String,User> users = new TreeMap<>();
        String s = in.nextLine();


        while( !s.equals("end")){

            String judge2 = "[u][-]1[3-9]\\d{9}[ ][3]";
            String messagejudge = "m-1[3-9]\\d{9} 1[3-9]\\d{9} [a-z|0-9| |,|.]++";

            if(s.matches(judge2)) {
                String []s1 = s.split("-| ");
                users.put(s1[1], new User(new UserRecords(), 100, 0));
            }
            if(s.matches(messagejudge)){
                for (Map.Entry<String, User> entry : users.entrySet()) {
                    String []s2 = s.split("-| ",4);
                    if (s2[1].equals(entry.getKey())){
                        String s3 =s2[3];
                        MessageRecord messageRecord = new MessageRecord(s3.length());
                        entry.getValue().userRecords.sendMessageRecords.add(messageRecord);
                    }
                }
            }

            s = in.nextLine();
        }
        for (Map.Entry<String, User> entry : users.entrySet()) {
            System.out.print(entry.getKey() + " ");
            System.out.printf("%.1f %.1f\n",100-new SendMessageRule().calCost(entry.getValue().userRecords.sendMessageRecords),new SendMessageRule().calCost(entry.getValue().userRecords.sendMessageRecords));

        }
    }

}

class SendMessageRule extends MessageChargeRule {
    public double calCost(ArrayList<MessageRecord> callRecords) {
        double sum = 0;
        int num = 0;
        for (int i = 0; i < callRecords.size(); i++) {
            if (callRecords.get(i).getLength() % 10 == 0) {
                num += callRecords.get(i).getLength() / 10;
            } else {
                num+=callRecords.get(i).getLength()/10+1;

            }
        }
        if(num<=3){
            return 100-num*0.1;
        }else if(num<=5){
            return 100-3*0.1-(num-3)*0.2;
        }else{
            return 100-3*0.1-2*0.2-0.3*(num-5);
        }
    }
}
class MessageRecord {
    private final int length;

    public MessageRecord(int length) {
        this.length = length;
    }


    public int getLength() {
        return length;
    }




}
class MessageChargeRule extends ChargeRule{
}
class User {
    UserRecords userRecords = new UserRecords();
    private double balance = 100;
    private String number;
    private final int num;

    public User(UserRecords userRecords,double balance,int num) {
        this.userRecords = userRecords;
        this.balance = balance;
        this.num = num;
    }


}
class UserRecords {
    ArrayList <CallRecord> callingInCityRecords = new ArrayList<>();
    ArrayList <CallRecord> callingInProvinceRecords = new ArrayList<>();
    ArrayList <CallRecord> callingInLandRecords = new ArrayList<>();
    ArrayList <CallRecord> manYouCallingInProvinceRecords = new ArrayList<>();
    ArrayList <CallRecord> manYouCallingInLandRecords = new ArrayList<>();

    ArrayList <CallRecord> answerInLandRecords = new ArrayList<>();

    ArrayList <MessageRecord> sendMessageRecords = new ArrayList<>();


}
abstract class CallChargeRule extends ChargeRule{
 
}
class CallRecord {
    private final Date startTime;
    private final Date endTime;
    private final String callingAddressAreaCode;
    private final String answerAddressAreaCode;



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

    public long Day(){
        long stateTimeLong = getStartTime().getTime();
        long endTimeLong = getEndTime().getTime();
        long second = (endTimeLong-stateTimeLong)/1000;
        return  second;
    }

    public Date getStartTime() {
        return startTime;
    }



    public Date getEndTime() {
        return endTime;
    }









}
abstract class ChargeMode {
    ArrayList <ChargeRule> arrayList = new ArrayList<>();

    public ChargeMode() {
        this.arrayList = arrayList;
    }

    public ArrayList<ChargeRule> getArrayList() {
        return arrayList;
    }

    public void setArrayList(ArrayList<ChargeRule> arrayList) {
        this.arrayList = arrayList;
    }

    public double calCost (UserRecords userRecords){
        return 0;
    }

    public double getMonthlyRent (){
        return 20;
    }
}
class ChargeRule {
}

abstract class CommunicationRecord {
    private String callingNumber;
    private String answerNumber;

    public String getCallingNumber() {
        return callingNumber;
    }

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

    public String getAnswerNumber() {
        return answerNumber;
    }

    public void setAnswerNumber(String answerNumber) {
        this.answerNumber = answerNumber;
    }
}
class LandlinePhoneCharging extends ChargeMode{
    double  monthlyRent = 20;
    public LandlinePhoneCharging() {
        super();
    }

    public double calCost (UserRecords userRecords){
        LandPhoneInlandRule landPhoneInlandRule = new LandPhoneInlandRule();
        LandPhoneInCityRule landPhoneInCityRule = new LandPhoneInCityRule();
        LandPhoneInProvinceRule landPhoneInProvinceRule = new LandPhoneInProvinceRule();

        return landPhoneInCityRule.calCost(userRecords.callingInCityRecords)+landPhoneInProvinceRule.calCost(userRecords.callingInProvinceRecords)
                +landPhoneInlandRule.calCost(userRecords.callingInLandRecords)+getMonthlyRent();
    }

    public double getMonthlyRent (){
        return monthlyRent;
    }
}
class LandPhoneInCityRule extends CallChargeRule{
    public double calCost (ArrayList<CallRecord> callRecords){
        double sum = 0;
        for (CallRecord callRecord : callRecords) {
            sum+=Math.ceil(callRecord.Day()/60.0);
        }
        return sum*0.1;
    }
}
class LandPhoneInlandRule extends CallChargeRule{
    public double calCost (ArrayList<CallRecord> callRecords){
        double sum = 0;
        for (CallRecord callRecord : callRecords) {
            sum+=Math.ceil(callRecord.Day()/60.0);
        }
        return sum*0.6;
    }
    public double phoneCalCost (ArrayList<CallRecord> callRecords){
        double sum = 0;
        for (CallRecord callRecord : callRecords) {
            sum+=Math.ceil(callRecord.Day()/60.0);
        }
        return sum*0.3;
    }

    public double answerInLandCalCost(ArrayList<CallRecord> callRecords){
        double sum = 0;
        for (CallRecord callRecord : callRecords) {
            sum+=Math.ceil(callRecord.Day()/60.0);
        }
        return sum*0.3;
    }

    public double manYouCalCost(ArrayList<CallRecord> callRecords){
        double sum = 0;
        for (CallRecord callRecord : callRecords) {
            sum+=Math.ceil(callRecord.Day()/60.0);
        }
        return sum*0.6;
    }
}
class LandPhoneInProvinceRule extends CallChargeRule{
    public double calCost (ArrayList<CallRecord> callRecords){
        double sum = 0;
        for (CallRecord callRecord : callRecords) {
            sum+=Math.ceil(callRecord.Day()/60.0);
        }
        return sum*0.3;
    }

    public double phoneCalCost(ArrayList<CallRecord> callRecords){
        double sum = 0;
        for (CallRecord callRecord : callRecords) {
            sum+=Math.ceil(callRecord.Day()/60.0);
        }
        return sum*0.2;
    }

    public double manYouCalCost(ArrayList<CallRecord> callRecords){
        double sum = 0;
        for (CallRecord callRecord : callRecords) {
            sum+=Math.ceil(callRecord.Day()/60.0);
        }
        return sum*0.3;
    }
}
class MobliePhoneCharging extends ChargeMode{
    double  monthlyRent = 15;
    public MobliePhoneCharging() {
        super();
    }

    public double calCost (UserRecords userRecords){
        LandPhoneInlandRule landPhoneInlandRule = new LandPhoneInlandRule();
        LandPhoneInCityRule landPhoneInCityRule = new LandPhoneInCityRule();
        LandPhoneInProvinceRule landPhoneInProvinceRule = new LandPhoneInProvinceRule();


        return landPhoneInCityRule.calCost(userRecords.callingInCityRecords)+landPhoneInProvinceRule.phoneCalCost(userRecords.callingInProvinceRecords)
                +landPhoneInlandRule.phoneCalCost(userRecords.callingInLandRecords)+landPhoneInlandRule.answerInLandCalCost(userRecords.answerInLandRecords)+
                landPhoneInProvinceRule.manYouCalCost(userRecords.manYouCallingInProvinceRecords)+getMonthlyRent()+
                landPhoneInlandRule.manYouCalCost(userRecords.manYouCallingInLandRecords);
    }

    public double getMonthlyRent (){
        return monthlyRent;
    }
}
View Code
分析:本题在第二次的基础上简化,只针对于手机与手机的短信计费,我们只需要再添加上短信计费的模块即可,不需要像二中那样反复考虑相互之间的关系。

  3、踩坑心得

  •   正则表达式
本次题目存在大量需要正则表达式的判断:     
1,确定输入电话号码的位数。
2,确定开头标志符号是否正确。
3,确定输入的电话号码是合法的电话(包含座机)。
4,确定输入日期和时间的正确。
5,确定输入区域的准确性。

  4、改进建议

  • 正则表达式:正则表达式的简化,这次题目的验证输入要用很多的正则表达,因为使用的不是很熟练,所以我的正则表达式的表述很繁杂,这对整体程序的运行是不好的,容易出现错误也不容易检查。
  • String judge2 = "[u][-]1[3-9]\\d{9}[ ][3]";
    String judge = "m-1[3-9]\\d{9} 1[3-9]\\d{9} [a-z|0-9| |,|.]++";

    这样会好很多,也一目了然哪里漏了什么。

  5、总结

  这3次作业让我对java的类和对象了解更加深刻,我也会更好的设计类来让程序的编写变的更加简单,迭代的使用可以让代码更加简洁逻辑清晰。编码的优化程度得到提高,程序的可读性更好了。但是对java的熟练程度有待提高,非常容易犯一些细节错误。我在JAVA上还需要更多的努力,才能更好的利用它,去写出更好的的代码。

标签:String,double,BLOG3,callRecord,ArrayList,计费,public
From: https://www.cnblogs.com/cpricloud/p/16953319.html

相关文章

  • Blog3-电信计费系列题目集
    前言电信计费系列题目虽然难度相对于多边形系列有所下降,但涉及知识点很广,主要如下:1、容器的使用2、抛出异常3、抽象类4、继承与多态5、正则表达式6、类和对象设计......