首页 > 编程语言 >面向对象程序设计电信计费系列心得

面向对象程序设计电信计费系列心得

时间:2022-12-05 12:13:27浏览次数:38  
标签:s2 号码 double ArrayList 面向对象 new 计费 程序设计

  通过这几次的大作业,已经加强了封装函数和算法的实现,这次电信计费系列主要为通过已给类图实现功能,并没有太多算法设计和创新,但也有一些小问题和小设计存在,废话也不多说,我们直接看题!

题目集六 7-1

7-1 电信计费系列1-座机计费 分数 80 作者 蔡轲

实现一个简单的电信计费程序:
假设南昌市电信分公司针对市内座机用户采用的计费方式:
月租20元,接电话免费,市内拨打电话0.1元/分钟,省内长途0.3元/分钟,国内长途拨打0.6元/分钟。不足一分钟按一分钟计。
南昌市的区号:0791,江西省内各地市区号包括:0790~0799以及0701。

输入格式:

输入信息包括两种类型
1、逐行输入南昌市用户开户的信息,每行一个用户,
格式:u-号码 计费类型 (计费类型包括:0-座机 1-手机实时计费 2-手机A套餐)
例如:u-079186300001 0
座机号码除区号外由是7-8位数字组成。
本题只考虑计费类型0-座机计费,电信系列2、3题会逐步增加计费类型。
2、逐行输入本月某些用户的通讯信息,通讯信息格式:
座机呼叫座机:t-主叫号码 接听号码 起始时间 结束时间
t-079186330022 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:11
以上四项内容之间以一个英文空格分隔,
时间必须符合"yyyy.MM.dd HH:mm:ss"格式。提示:使用SimpleDateFormat类。
以上两类信息,先输入所有开户信息,再输入所有通讯信息,最后一行以“end”结束。
注意:
本题非法输入只做格式非法的判断,不做内容是否合理的判断(时间除外,否则无法计算),比如:
1、输入的所有通讯信息均认为是同一个月的通讯信息,不做日期是否在同一个月还是多个月的判定,直接将通讯费用累加,因此月租只计算一次。
2、记录中如果同一电话号码的多条通话记录时间出现重合,这种情况也不做判断,直接 计算每条记录的费用并累加。
3、用户区号不为南昌市的区号也作为正常用户处理。

输出格式:

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

错误处理:
输入数据中出现的不符合格式要求的行一律忽略。

建议类图:
参见图1、2、3,可根据理解自行调整:

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的约束条件:必须是某一个用户的符合计费规则要求的所有记录。

LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三个类分别是
座机拨打市内、省内、省外电话的计费规则类,用于实现这三种情况的费用计算。    
(提示:可以从UserRecords类中获取各种类型的callRecords)。

后续扩展说明:
后续题目集将增加手机用户,手机用户的计费方式中除了与座机计费类似的主叫通话费之外,还包含市外接听电话的漫游费以及发短信的费用。在本题的设计时可统一考虑。
通话记录中,手机需要额外记录拨打/接听的地点的区号,比如:
座机打手机:t-主叫号码 接听号码 接听地点区号 起始时间 结束时间
t-079186330022 13305862264 020 2022.1.3 10:00:25 2022.1.3 10:05:11
手机互打:t-主叫号码 拨号地点 接听号码 接听地点区号 起始时间 结束时间
t-18907910010 0791 13305862264 0371 2022.1.3 10:00:25 2022.1.3 10:05:11
短信的格式:m-主叫号码,接收号码,短信内容
m-18907910010 13305862264 welcome to jiangxi
m-13305862264 18907910010 thank you

输入样例:

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

u-079186300001 0
t-079186300001 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:25
end

输出样例:

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

079186300001 3.0 77.0

核心思路:

  main函数主要负责实现字符串的接收以及处理,转化为有效合法数据后,根据情况将通话记录加入到用户的(市内/省内/国内)通话记录中去.

  正则reg1用于匹配南昌市用户开户信息,正则reg2用于匹配用户通话记录,用户列表采用了ArrayList数组来存储(记得添加头文件import java.util.ArrayList;),定义了变量quhao1,quhao2用于暂存切割出来的通话记录两部手机的区号,还定义了一个boolean变量,用于记录该南昌开户号码是否重复(已存在),如果不存在就将输入的开户号码存入新建的用户对象并添加进入ArrayList数组.

  如果输入的格式为通话记录(reg2正则匹配成功),则先对接收到的字符串使用substring(切割函数),去除"t-",然后再使用split()函数,将字符串以空格为分隔符切割,s1[0]存的是拨打方的电话号码,s1[1]存的是接收方的电话号码,先对ArrayList数组进行遍历,检索拨打方号码在ArrayList数组中的序号,如果没找到就抛弃该通话记录不予处理,通话时间需要用concat函数将s1[2]和s1[3]拼接在一起,然后存入date时间类,然后根据两个号码的区号,将通话记录存入拨打方的(市内/省内/国内)通话记录中去.

public static void main(String[] args){
        Scanner scan=new Scanner(System.in);
        String reg1="^u-[0-9]{11,12} 0$";                //正则一  用于匹配号码注册
        String reg2="^t-[0]{1}[0-9]{9,11} [0]{1}[0-9]{9,11} [0-9]{4}[.](([1-9]{1})|([1]{1}[0-2]{1}))[.]([1-9]|([1-2]{1}[0-9]{1})|(3[0-1])) (([0-1][0-9])|(2[0-3]))[:]([0-5][0-9])[:]([0-5][0-9]) [0-9]{4}[.](([1-9]{1})|([1]{1}[0-2]{1}))[.]([1-9]|([1-2]{1}[0-9]{1})|3[0-1]) (([0-1][0-9])|(2[0-3]))[:]([0-5][0-9])[:]([0-5][0-9])$";
                                                        //正则二      用于匹配通话记录
        ArrayList<User> user=new ArrayList<>();        //用户列表
        String str,quhao1,quhao2,sj1,sj2;
        String[] s1=new String[2];
        String[] s2=new String[6];
        while(scan.hasNext()){//while
            str=scan.nextLine();
            if(str.equals("end")) break;
            if(str.matches(reg1)){
                boolean cf=false;                    //用于记录是否重复
                str=str.substring(2);                //去除"t-"
                s1=str.split(" ");
                for(int a=0;a<user.size()&&!cf;a++){    //查找是否重复
                    if(s1[0].equals(user.get(a).getNumber())) cf=true;
                }
                if(!cf){
                    user.add(new User());
                    user.get(user.size()-1).setNumber(s1[0]);
                }
            }
            else if(str.matches(reg2)){
                str=str.substring(2);//去除头部
                s2=str.split(" ");//第0个存的是calling号码 第1个存的是answer号码
                quhao1=s2[0].substring(0,4);//取出号码1的区号
                quhao2=s2[1].substring(0,4);//取出号码2的区号
                sj1=s2[2].concat(" ");
                sj1=sj1.concat(s2[3]);//sj1为通话起始时间
                sj2=s2[4].concat(" ");
                sj2=sj2.concat(s2[5]);//sj2为通话结束时间
                int xuhao;
                for(xuhao=0;xuhao<user.size();xuhao++){        //遍历找到改用户所在位置
                    if(user.get(xuhao).getNumber().equals(s2[0])) break;
                }
                if(quhao1.equals(quhao2)){//市内通话记录
                    if(xuhao<user.size()){//找到对应用户的序号
                        CallRecord callRecord=new CallRecord();
                        callRecord.setCallingAddressAreaCode(quhao1);//设置calling来源地
                        callRecord.setAnswerAddressAreaCode(quhao2);//设置answer来源地
                        callRecord.setCallingNumber(s2[0]);//设置calling号码
                        callRecord.setAnswerNumber(s2[1]);//设置answer号码
                        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
                        try {
                            Date start=simpleDateFormat.parse(sj1);
                            callRecord.setStartTime(start);
                        } catch (ParseException e) {
                            throw new RuntimeException(e);
                        }
                        try {
                            Date end=simpleDateFormat.parse(sj2);
                            callRecord.setEndTime(end);
                        } catch (ParseException e) {
                            throw new RuntimeException(e);
                        }
                        user.get(xuhao).getUserRecords().addCallingInCityRecords(callRecord);
                    }//找到对应用户的序号
                }//市内通话记录
                else if(quhao2.charAt(0)=='0'&&quhao2.charAt(1)=='7'&&(quhao2.charAt(2)=='9'||(quhao2.charAt(2)=='0'&&quhao2.charAt(3)=='1'))){//省内通话
                    if(xuhao< user.size()){//找到对应用户的序号
                        CallRecord callRecord=new CallRecord();
                        callRecord.setCallingAddressAreaCode(quhao1);//设置calling来源地
                        callRecord.setAnswerAddressAreaCode(quhao2);//设置answer来源地
                        callRecord.setCallingNumber(s2[0]);//设置calling号码
                        callRecord.setAnswerNumber(s2[1]);//设置answer号码
                        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
                        try {
                            Date start=simpleDateFormat.parse(sj1);
                            callRecord.setStartTime(start);
                        } catch (ParseException e) {
                            throw new RuntimeException(e);
                        }
                        try {
                            Date end=simpleDateFormat.parse(sj2);
                            callRecord.setEndTime(end);
                        } catch (ParseException e) {
                            throw new RuntimeException(e);
                        }
                        user.get(xuhao).getUserRecords().addCallingInProvinceRecords(callRecord);
                    }//找到对应用户的序号
                }//省内通话
                else{//国内通话
                    if(xuhao< user.size()){//找到对应用户的序号
                        CallRecord callRecord=new CallRecord();
                        callRecord.setCallingAddressAreaCode(quhao1);//设置calling来源地
                        callRecord.setAnswerAddressAreaCode(quhao2);//设置answer来源地
                        callRecord.setCallingNumber(s2[0]);//设置calling号码
                        callRecord.setAnswerNumber(s2[1]);//设置answer号码
                        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
                        try {
                            Date start=simpleDateFormat.parse(sj1);
                            callRecord.setStartTime(start);
                        } catch (ParseException e) {
                            throw new RuntimeException(e);
                        }
                        try {
                            Date end=simpleDateFormat.parse(sj2);
                            callRecord.setEndTime(end);
                        } catch (ParseException e) {
                            throw new RuntimeException(e);
                        }
                        user.get(xuhao).getUserRecords().addCallingInlandRecords(callRecord);
                    }//找到对应用户的序号
                }//国内通话
            }//正则2
        }//while
        int min=0;
        User tmp;
        for(int a=0;a<user.size();a++){
            min=a;
            for(int b=a+1;b<user.size();b++){
                if(user.get(b).getNumber().compareTo(user.get(min).getNumber())<0) min=b;
            }
            tmp=user.get(a);
            user.set(a,user.get(min));
            user.set(min,tmp);
        }

        for(int a=0;a< user.size();a++){
            System.out.print(user.get(a).getNumber()+" ");
            Main.shuchu(Double.valueOf(user.get(a).calcost()));
            System.out.print(" ");
            Main.shuchu(Double.valueOf(user.get(a).calBalance()));
            System.out.println();
        }
/*测试用 将存入的各种通话记录输出
        for(int a=0;a<user.size();a++){//     user.get(0).getUserRecords().getCallingInProvinceRecords().size()
            System.out.println(user.get(a).getNumber());
            //System.out.print(user.get(0).getUserRecords().getCallingInProvinceRecords().get(a).getCallingNumber()+" ");
            //System.out.println(user.get(0).getUserRecords().getCallingInProvinceRecords().get(a).getAnswerNumber());
        }*/

    }

   User类属性有UserRecord通话记录类,balance余额类,LandlinePhoneCharging座机收费方式类,其余为简单的功能方法(注释里有说功能)

class User{
    private UserRecords userRecords=new UserRecords();
    private double balance=100;
    ChargeMode chargeMode=new LandlinePhoneCharging() ;
    private String number;
    double calBalance(){//获取余额
        double mix=0;
        mix=this.balance-this.chargeMode.calcost(this.userRecords)-this.chargeMode.getMonthlyRend();
        return mix;
    }
    double calcost(){//获取开销
        return this.chargeMode.calcost(this.userRecords);
    }
    public double getBalance(){
        return this.balance;
    }

    public String getNumber(){
        return this.number;
    }
    public UserRecords getUserRecords(){
        return this.userRecords;
    }
    public void setNumber(String number){
        this.number=number;
    }

}

   通话记录类UserRecords,有六个属性,市内拨打记录,省内拨打记录,国内拨打记录,市内接听记录,省内接听记录,国内接听记录(因为这题接听不要钱,所以这题用不到接听记录),USerRecord类里的方法均为对private变量的添加和获取.

class UserRecords{
    private ArrayList<CallRecord> callingInCityRecords=new ArrayList<>();
    private ArrayList<CallRecord> callingInProvinceRecords=new ArrayList<>();
    private ArrayList<CallRecord> callingInLandRecords=new ArrayList<>();
    private ArrayList<CallRecord> answerInCityRecords=new ArrayList<>();
    private ArrayList<CallRecord> answerInProvinceRecords=new ArrayList<>();
    private ArrayList<CallRecord> answerInLandRecords=new ArrayList<>();
    void addCallingInCityRecords(CallRecord callRecord){
        this.callingInCityRecords.add(callRecord);
    }
    void addCallingInProvinceRecords(CallRecord callRecord){
        this.callingInProvinceRecords.add(callRecord);
    }
    void addCallingInlandRecords(CallRecord callRecord){
        this.callingInLandRecords.add(callRecord);
    }
    void addanswerInCityRecords(CallRecord callRecord){
        this.answerInCityRecords.add(callRecord);
    }
    void addanswerInProvinceRecords(CallRecord callRecord){
        this.answerInProvinceRecords.add(callRecord);
    }void addanswerInlandRecords(CallRecord callRecord){
        this.answerInLandRecords.add(callRecord);
    }
    ArrayList<CallRecord> getCallingInCityRecords(){
        return this.callingInCityRecords;
    }
    ArrayList<CallRecord> getCallingInProvinceRecords(){
        return this.callingInProvinceRecords;
    }
    ArrayList<CallRecord> getCallingInLandRecords(){
        return this.callingInLandRecords;
    }
    ArrayList<CallRecord> getAnswerInCityRecords(){
        return this.answerInCityRecords;
    }
    ArrayList<CallRecord> getAnswerInProvinceRecords(){
        return this.answerInProvinceRecords;
    }
    ArrayList<CallRecord> getAnswerInLandRecords(){
        return this.answerInLandRecords;
    }
}

  以下为收费计算,结构较为简单,看注释就可以啦,很容易懂

abstract class ChargeRule{

}
abstract class CallChargeRule extends ChargeRule{
    abstract double calcost(ArrayList<CallRecord>  callRecord);
}
class LandPhoneInCityRule extends CallChargeRule{
    @Override
    double calcost(ArrayList<CallRecord>  callRecord){//市内计费
        long starttime,endtime,callingtime;
        double mix=0;
        for(int a=0;a<callRecord.size();a++){
            starttime=callRecord.get(a).getStartTime().getTime();
            endtime=callRecord.get(a).getEndTime().getTime();
            starttime/=1000;//换算到秒
            endtime/=1000;//换算到秒
            callingtime=endtime-starttime;
            if(callingtime%60==0) callingtime/=60;//换算到分
            else callingtime=callingtime/60+1;
            mix+=callingtime*0.1;
        }
        return mix;
    }
}
class LandPhoneInProvinceRule extends CallChargeRule{
    @Override
    double calcost(ArrayList<CallRecord> callRecord){//省内计费
        long starttime,endtime,callingtime;
        double mix=0;
        for(int a=0;a<callRecord.size();a++){
            starttime=callRecord.get(a).getStartTime().getTime();
            endtime=callRecord.get(a).getEndTime().getTime();
            starttime/=1000;//换算到秒
            endtime/=1000;//换算到秒
            callingtime=endtime-starttime;
            if(callingtime%60==0) callingtime/=60;//换算到分
            else callingtime=callingtime/60+1;
            mix+=callingtime*0.3;
        }
        return mix;
    }
}
class LandPhoneInlandRule extends CallChargeRule{
    @Override
    double calcost(ArrayList<CallRecord> callRecord){//国内计费
        long starttime,endtime,callingtime;
        double mix=0;
        for(int a=0;a<callRecord.size();a++){
            starttime=callRecord.get(a).getStartTime().getTime();
            endtime=callRecord.get(a).getEndTime().getTime();
            starttime/=1000;//换算到秒
            endtime/=1000;//换算到秒
            callingtime=endtime-starttime;
            if(callingtime%60==0) callingtime/=60;//换算到分
            else callingtime=callingtime/60+1;
            mix+=callingtime*0.6;
        }
        return mix;
    }
}

7-2 多态测试 分数 20

定义容器Container接口。模拟实现一个容器类层次结构,并进行接口的实现、抽象方法重写和多态机制测试。各容器类实现求表面积、体积的方法。

  1. 定义接口Container:
    属性:
    public static final double pi=3.1415926;
    抽象方法:
    public abstract double area();
    public abstract double volume();
    static double sumofArea(Container c[]);
    static double sumofVolume(Container c[]);
    其中两个静态方法分别计算返回容器数组中所有对象的面积之和、周长之和;
  2. 定义Cube类、Cylinder类均实现自Container接口。
    Cube类(属性:边长double类型)、Cylinder类(属性:底圆半径、高,double类型)。

输入格式:

第一行n表示对象个数,对象类型用cube、cylinder区分,cube表示立方体对象,后面输入边长,输入cylinder表示圆柱体对象,后面是底圆半径、高。

输出格式:

分别输出所有容器对象的表面积之和、体积之和,结果保留小数点后2位。

输入样例:

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

4
cube
15.7
cylinder
23.5 100
cube
46.8
cylinder
17.5 200

输出样例:

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

56771.13
472290.12

 核心思路:

  功能较为简单,创建好两个类,构建好计算面积和表面积的方法,主函数对接收进来的数据进行判断,创建对应的类,然后调用计算面积和表面积的方法,求和然后输出即可

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        int num=scan.nextInt();
        double mj=0,tj=0;
        double n1,n2;
        String str;
        for(int a=0;a<num;a++){
            scan.nextLine();
            str=scan.nextLine();

            if(str.equals("cube")) {
                n1=scan.nextDouble();
                cu zft=new cu(n1);
                mj+=zft.area();
                tj+=zft.volume();
            }
            if(str.equals("cylinder")){
                n1=scan.nextDouble();
                n2=scan.nextDouble();
                cy yzt=new cy(n1,n2);
                mj+=yzt.area();
                tj+=yzt.volume();
            }
        }
        Main.shuchu(mj);
        System.out.println();
        Main.shuchu(tj);
    }
    public static void shuchu(double n){//输出精度转换保留2位
        String str=Double.toString(n);
        int xb=str.indexOf(".");
        if(xb!=-1){//有小数点
            String str1=str.substring(xb+1);
            if(str1.length()>2) System.out.printf("%.2f",n);
            else System.out.print(n);
        }//有小数点
        else System.out.print(n);
    }//输出精度转换保留3位
}
abstract class cube{
    double n;//边长
    public cube(double num){
        this.n=num;
    }
    public abstract double area();//求表面积
    public abstract double volume();//求体积
}
class cu extends cube{
    public cu(double num){
        super(num);
    }
    @Override
    public double area(){
        return n*n*6;
    }
    @Override
    public double volume(){
        return n*n*n;
    }
}

abstract class cylinder{
    double r,h;//半径和高
    public static final double pi=3.1415926;
    public cylinder(double rr,double hh){
        this.r=rr;
        this.h=hh;
    }
    public abstract double area();
    public abstract double volume();
}
class cy extends cylinder{
    public cy(double r,double h){
        super(r,h);
    }
    @Override
    public double area(){
        return pi*this.r*this.r*2+h*2*pi*this.r;
    }
    @Override
    public double volume(){
        return pi*this.r*this.r*h;
    }
}

 

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)。
注意:以上图中所定义的类不是限定要求,根据实际需要自行补充或修改。

输入样例:

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

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

核心思路:

   main函数如下,我将输入进行了封装,封装成了方法input(ArrayList<User> user),然后对接收的数据进行排序,使用字符串自带的compareTo方法进行比对号码大小,然后用一个之间变量tmp来暂存数据,实现交换,排序使用的是冒泡排序,因为比较简单易懂

public static void main(String[] args){
        ArrayList<User> user=new ArrayList<>();
        Main.input(user);
        int min;
        User tmp;
        for(int a=0;a<user.size();a++){//排序
            min=a;
            for(int b=a+1;b<user.size();b++){
                if(user.get(b).getNumber().compareTo(user.get(min).getNumber())<0) min=b;
            }
            tmp=user.get(a);
            user.set(a,user.get(min));
            user.set(min,tmp);
        }

        //输出结果
        for (User value : user) {
            System.out.print(value.getNumber() + " ");
            Main.shuchu(value.calcost());
            System.out.print(" ");
            Main.shuchu(value.calBalance());
            System.out.println();
        }
/*
//测试输出
        for(int a=0;a<user.get(0).getUserRecords().getCallingInCityRecords().size();a++){// user.size()
            //System.out.println(user.get(a).getNumber());
            System.out.print(user.get(0).getUserRecords().getCallingInCityRecords().get(a).getCallingNumber()+" "+user.get(0).getUserRecords().getCallingInCityRecords().get(a).getCallingAddressAreaCode()+" ");
            System.out.println(user.get(0).getUserRecords().getCallingInCityRecords().get(a).getAnswerNumber()+" "+user.get(0).getUserRecords().getCallingInCityRecords().get(a).getAnswerAddressAreaCode());
        }
*/
    }

  input函数用户接收键盘键入的数据,处理后存入ArrayList数组对应位置,主要核心思想与题目集六的7-1一致

    public static void input(ArrayList<User> user){
        Scanner scan=new Scanner(System.in);
        String reg1="(^u-0[0-9]{10,11} [0|1]$)|(^u-1[0-9]{10} [0|1]$)";
        String reg2="(^t-[0]{1}[0-9]{9,11} ((0[0-9]{9,11})|(1[0-9]{10} 0[0-9]{2,3})) [0-9]{4}[.](([1-9]{1})|([1]{1}[0-2]{1}))[.]([1-9]|([1-2]{1}[0-9]{1})|(3[0-1])) (([0-1][0-9])|(2[0-3]))[:]([0-5][0-9])[:]([0-5][0-9]) [0-9]{4}[.](([1-9]{1})|([1]{1}[0-2]{1}))[.]([1-9]|([1-2]{1}[0-9]{1})|3[0-1]) (([0-1][0-9])|(2[0-3]))[:]([0-5][0-9])[:]([0-5][0-9])$)|(^t-1[0-9]{10} 0[0-9]{2,3} ((0[0-9]{9,11})|(1[0-9]{10} 0[0-9]{2,3})) [0-9]{4}[.](([1-9]{1})|([1]{1}[0-2]{1}))[.]([1-9]|([1-2]{1}[0-9]{1})|(3[0-1])) (([0-1][0-9])|(2[0-3]))[:]([0-5][0-9])[:]([0-5][0-9]) [0-9]{4}[.](([1-9]{1})|([1]{1}[0-2]{1}))[.]([1-9]|([1-2]{1}[0-9]{1})|3[0-1]) (([0-1][0-9])|(2[0-3]))[:]([0-5][0-9])[:]([0-5][0-9])$)";
        String reg3="((0701)|(079\\d))";//省内匹配
        String str,quhao1,quhao2,sj1,sj2,num1,num2;
        String[] s1;
        String[] s2;
        while(scan.hasNext()){//while
            str=scan.nextLine();
            if(str.equals("end")) break;
            if(str.matches(reg1)){
                boolean cf=false;
                str=str.substring(2);
                s1=str.split(" ");
                for (User value : user) {
                    if (s1[0].equals(value.getNumber())) {
                        cf = true;
                        break;
                    }
                }
                if(!cf){
                    switch (s1[1]) {
                        case "0": user.add(new User(0));
                                    break;
                        case "1": user.add(new User(1));
                                    break;
                    }
                    user.get(user.size()-1).setNumber(s1[0]);
                }
            }
            else if(str.matches(reg2)){
                str=str.substring(2);//去除头部
                s2=str.split(" ");//第0个存的是calling号码 第1个存的是calling号码区号 第2个存的是answer号码 第3个存的是answer号码区号
                if(s2.length==6) {
                    quhao1 = s2[0].substring(0, 4);//取出号码1的区号
                    quhao2 = s2[1].substring(0, 4);//取出号码2的区号
                    num1=s2[0];
                    num2=s2[1];
                    sj1 = s2[2].concat(" ");
                    sj1 = sj1.concat(s2[3]);//sj1为通话起始时间
                    sj2 = s2[4].concat(" ");
                    sj2 = sj2.concat(s2[5]);//sj2为通话结束时间
                }
                else if(s2.length==7) {
                    if(s2[0].charAt(0)=='1'){
                        quhao1=s2[1];//取出号码1的区号
                        quhao2 = s2[2].substring(0, 4);//取出号码2的区号
                        num1=s2[0];
                        num2=s2[2];
                    }
                    else{
                        quhao1 = s2[0].substring(0, 4);//取出号码1的区号
                        quhao2 = s2[2];//取出号码2的区号
                        num1=s2[0];
                        num2=s2[1];
                    }
                    sj1 = s2[3].concat(" ");
                    sj1 = sj1.concat(s2[4]);//sj1为通话起始时间
                    sj2 = s2[5].concat(" ");
                    sj2 = sj2.concat(s2[6]);//sj2为通话结束时间
                }
                else {
                    quhao1 = s2[1];//取出号码1的区号
                    quhao2 = s2[3];//取出号码2的区号
                    num1=s2[0];
                    num2=s2[2];
                    sj1 = s2[4].concat(" ");
                    sj1 = sj1.concat(s2[5]);//sj1为通话起始时间
                    sj2 = s2[6].concat(" ");
                    sj2 = sj2.concat(s2[7]);//sj2为通话结束时间
                }
                int xuhao1=0,xuhao2=0;
                boolean find1=false,find2=false;
                for(;xuhao1<user.size()&&xuhao2<user.size();){
                    if(user.get(xuhao1).getNumber().equals(num1)) find1=true;
                    if(user.get(xuhao2).getNumber().equals(num2)) find2=true;
                    if(!find1) xuhao1++;
                    if(!find2) xuhao2++;
                    if(find1&&find2) break;
                }

                if(xuhao1<user.size()||xuhao2<user.size()){//if成功找到对应用户的序号
                    CallRecord callRecord=new CallRecord();
                    callRecord.setCallingAddressAreaCode(quhao1);//设置calling来源地
                    callRecord.setAnswerAddressAreaCode(quhao2);//设置answer来源地
                    callRecord.setCallingNumber(num1);//设置calling号码
                    callRecord.setAnswerNumber(num2);//设置answer号码
                    SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
                    try {
                        Date start=simpleDateFormat.parse(sj1);
                        callRecord.setStartTime(start);
                    } catch (ParseException e) {
                        throw new RuntimeException(e);
                    }
                    try {
                        Date end=simpleDateFormat.parse(sj2);
                        callRecord.setEndTime(end);
                    } catch (ParseException e) {
                        throw new RuntimeException(e);
                    }
                    if(xuhao1<user.size()) {
                        if (num1.charAt(0) == '0') user.get(xuhao1).getUserRecords().addCallingInCityRecords(callRecord);
                        else if (quhao1.equals("0791"))
                            user.get(xuhao1).getUserRecords().addCallingInCityRecords(callRecord);
                        else if (quhao1.matches(reg3))
                            user.get(xuhao1).getUserRecords().addCallingInProvinceRecords(callRecord);
                        else user.get(xuhao1).getUserRecords().addCallingInlandRecords(callRecord);
                    }
                    if(xuhao2<user.size()) {
                        if (num2.charAt(0) == '0') user.get(xuhao2).getUserRecords().addanswerInCityRecords(callRecord);
                        else if (quhao2.equals("0791"))
                            user.get(xuhao2).getUserRecords().addanswerInCityRecords(callRecord);
                        else if (quhao2.matches(reg3))
                            user.get(xuhao2).getUserRecords().addanswerInProvinceRecords(callRecord);
                        else user.get(xuhao2).getUserRecords().addanswerInlandRecords(callRecord);
                    }

                }//if成功找到对应用户的序号
            }//正则2
        }//while
    }

   其余照着类图画即可,没什么太大难度,

 

class User{
    private final UserRecords userRecords=new UserRecords();
    private final double balance=100;
    ChargeMode chargeMode;
    private String number;
    User(int n){//构造器选择收费模式
        if(n==0) this.chargeMode=new LandlinePhoneCharging();
        if(n==1) this.chargeMode=new MobileCharging();
    }
    double calBalance(){//获取余额
        double mix;
        mix=this.balance-this.chargeMode.calcost(this.userRecords)-this.chargeMode.getMonthlyRend();
        return mix;
    }
    double calcost(){//获取开销
        return this.chargeMode.calcost(this.userRecords);
    }

    public String getNumber(){
        return this.number;
    }
    public UserRecords getUserRecords(){
        return this.userRecords;
    }
    public void setNumber(String number){
        this.number=number;
    }

}
abstract class ChargeMode{
    private final ArrayList<ChargeRule> chargeRules = new ArrayList<>();
    ArrayList<ChargeRule> getChargeRules(){
        return this.chargeRules;
    }
    void setChargeRules(ArrayList<ChargeRule> chargeRules){
        for(int a=0;a<chargeRules.size();a++){
            this.chargeRules.add(chargeRules.get(a));
        }
    }
    public abstract double calcost(UserRecords userRecords);
    abstract double getMonthlyRend();
}
class UserRecords{
    private ArrayList<CallRecord> callingInCityRecords=new ArrayList<>();//市内拨打
    private ArrayList<CallRecord> callingInProvinceRecords=new ArrayList<>();//省内拨打
    private ArrayList<CallRecord> callingInLandRecords=new ArrayList<>();//国内拨打
    private ArrayList<CallRecord> answerInCityRecords=new ArrayList<>();//市内接听
    private ArrayList<CallRecord> answerInProvinceRecords=new ArrayList<>();//省内接听
    private ArrayList<CallRecord> answerInLandRecords=new ArrayList<>();//国内接听
    void addCallingInCityRecords(CallRecord callRecord){
        this.callingInCityRecords.add(callRecord);
    }
    void addCallingInProvinceRecords(CallRecord callRecord){
        this.callingInProvinceRecords.add(callRecord);
    }
    void addCallingInlandRecords(CallRecord callRecord){
        this.callingInLandRecords.add(callRecord);
    }
    void addanswerInCityRecords(CallRecord callRecord){
        this.answerInCityRecords.add(callRecord);
    }
    void addanswerInProvinceRecords(CallRecord callRecord){
        this.answerInProvinceRecords.add(callRecord);
    }void addanswerInlandRecords(CallRecord callRecord){
        this.answerInLandRecords.add(callRecord);
    }
    ArrayList<CallRecord> getCallingInCityRecords(){
        return this.callingInCityRecords;
    }
    ArrayList<CallRecord> getCallingInProvinceRecords(){
        return this.callingInProvinceRecords;
    }
    ArrayList<CallRecord> getCallingInLandRecords(){
        return this.callingInLandRecords;
    }
    ArrayList<CallRecord> getAnswerInCityRecords(){
        return this.answerInCityRecords;
    }
    ArrayList<CallRecord> getAnswerInProvinceRecords(){
        return this.answerInProvinceRecords;
    }
    ArrayList<CallRecord> getAnswerInLandRecords(){
        return this.answerInLandRecords;
    }
}

class MobileCharging extends ChargeMode{
    private double monthlyRent=15;
    MobileCharging(){
        super();
        getChargeRules().add(new MobileInCityRule());
        getChargeRules().add(new MobileInProvinceRule());
        getChargeRules().add(new MobileInlandRule());
    }
    @Override
    public double calcost(UserRecords userRecords){//计算话费
        double mix=0;
        mix+=((MobileInCityRule)getChargeRules().get(0)).calcost(userRecords.getCallingInCityRecords(),userRecords.getAnswerInCityRecords());
        mix+=((MobileInProvinceRule)getChargeRules().get(1)).calcost(userRecords.getCallingInProvinceRecords(),userRecords.getAnswerInProvinceRecords());
        mix+=((MobileInlandRule)getChargeRules().get(2)).calcost(userRecords.getCallingInLandRecords(),userRecords.getAnswerInLandRecords());
        return mix;
    }
    @Override
    double getMonthlyRend(){
        return this.monthlyRent;
    }
}
class LandlinePhoneCharging extends ChargeMode{
    private double monthlyRent=20;
    LandlinePhoneCharging(){
        super();
        getChargeRules().add(new LandPhoneInCityRule());
        getChargeRules().add(new LandPhoneInProvinceRule());
        getChargeRules().add(new LandPhoneInlandRule());
    }
    @Override
    public double calcost(UserRecords userRecords){//计算话费
        double mix=0;
        mix+=((LandPhoneInCityRule)getChargeRules().get(0)).calcost(userRecords.getCallingInCityRecords(),userRecords.getAnswerInCityRecords());
        mix+=((LandPhoneInProvinceRule)getChargeRules().get(1)).calcost(userRecords.getCallingInProvinceRecords(),userRecords.getAnswerInProvinceRecords());
        mix+=((LandPhoneInlandRule)getChargeRules().get(2)).calcost(userRecords.getCallingInLandRecords(),userRecords.getAnswerInLandRecords());
        return mix;
    }
    @Override
    double getMonthlyRend(){
        return this.monthlyRent;
    }
}
abstract class ChargeRule{

}
abstract class CallChargeRule extends ChargeRule{
    abstract double calcost(ArrayList<CallRecord>  callRecord,ArrayList<CallRecord>  answerRecord);
}
class LandPhoneInCityRule extends CallChargeRule{
    @Override
    double calcost(ArrayList<CallRecord>  callRecord,ArrayList<CallRecord> answerRecord){//市内计费
        long starttime,endtime,callingtime;
        String reg3="((0701)|(079\\d))";
        double mix=0;
        for(int a=0;a<callRecord.size();a++){
            starttime=callRecord.get(a).getStartTime().getTime();
            endtime=callRecord.get(a).getEndTime().getTime();
            starttime/=1000;//换算到秒
            endtime/=1000;//换算到秒
            callingtime=endtime-starttime;
            if(callingtime%60==0) callingtime/=60;//换算到分
            else callingtime=callingtime/60+1;
            if(callRecord.get(a).getAnswerAddressAreaCode().equals("0791")) mix+=callingtime*0.1;
            else if(callRecord.get(a).getAnswerAddressAreaCode().matches(reg3)) mix+=callingtime*0.3;
            else mix+=callingtime*0.6;
        }
        return mix;
    }
}
class MobileInCityRule extends CallChargeRule{
    @Override
    double calcost(ArrayList<CallRecord> callRecord,ArrayList<CallRecord> answerRecord){//市内计费
        String reg3="((0701)|(079\\d))";
        long starttime,endtime,callingtime;
        double mix=0;
        for(int a=0;a<callRecord.size();a++){
            starttime=callRecord.get(a).getStartTime().getTime();
            endtime=callRecord.get(a).getEndTime().getTime();
            starttime/=1000;//换算到秒
            endtime/=1000;//换算到秒
            callingtime=endtime-starttime;
            if(callingtime%60==0) callingtime/=60;//换算到分
            else callingtime=callingtime/60+1;
            if(callRecord.get(a).getAnswerAddressAreaCode().equals("0791")) mix+=callingtime*0.1;
            else if(callRecord.get(a).getAnswerAddressAreaCode().matches(reg3)) mix+=callingtime*0.2;
            else mix+=callingtime*0.3;
        }
        return mix;
    }
}
class LandPhoneInProvinceRule extends CallChargeRule{
    @Override
    double calcost(ArrayList<CallRecord> callRecord,ArrayList<CallRecord> answerRecord){//省内计费
        long starttime,endtime,callingtime;
        double mix=0;
        for(int a=0;a<callRecord.size();a++){
            starttime=callRecord.get(a).getStartTime().getTime();
            endtime=callRecord.get(a).getEndTime().getTime();
            starttime/=1000;//换算到秒
            endtime/=1000;//换算到秒
            callingtime=endtime-starttime;
            if(callingtime%60==0) callingtime/=60;//换算到分
            else callingtime=callingtime/60+1;
            mix+=callingtime*0.3;
        }
        return mix;
    }
}
class MobileInProvinceRule extends CallChargeRule{
    @Override
    double calcost(ArrayList<CallRecord> callRecord,ArrayList<CallRecord> answerRecord){//省内计费
        long starttime,endtime,callingtime;
        double mix=0;
        for(int a=0;a<callRecord.size();a++){
            starttime=callRecord.get(a).getStartTime().getTime();
            endtime=callRecord.get(a).getEndTime().getTime();
            starttime/=1000;//换算到秒
            endtime/=1000;//换算到秒
            callingtime=endtime-starttime;
            if(callingtime%60==0) callingtime/=60;//换算到分
            else callingtime=callingtime/60+1;
            mix+=callingtime*0.3;
        }
        return mix;
    }
}
class LandPhoneInlandRule extends CallChargeRule{
    @Override
    double calcost(ArrayList<CallRecord> callRecord,ArrayList<CallRecord> answerRecord){//国内计费
        long starttime,endtime,callingtime;
        double mix=0;
        for(int a=0;a<callRecord.size();a++){
            starttime=callRecord.get(a).getStartTime().getTime();
            endtime=callRecord.get(a).getEndTime().getTime();
            starttime/=1000;//换算到秒
            endtime/=1000;//换算到秒
            callingtime=endtime-starttime;
            if(callingtime%60==0) callingtime/=60;//换算到分
            else callingtime=callingtime/60+1;
            mix+=callingtime*0.6;
        }
        return mix;
    }
}
class MobileInlandRule extends CallChargeRule{
    @Override
    double calcost(ArrayList<CallRecord> callRecord,ArrayList<CallRecord> answerRecord){//国内计费
        long starttime,endtime,callingtime;
        double mix=0;
        for(int a=0;a<callRecord.size();a++){
            starttime=callRecord.get(a).getStartTime().getTime();
            endtime=callRecord.get(a).getEndTime().getTime();
            starttime/=1000;//换算到秒
            endtime/=1000;//换算到秒
            callingtime=endtime-starttime;
            if(callingtime%60==0) callingtime/=60;//换算到分
            else callingtime=callingtime/60+1;
            mix+=callingtime*0.6;
        }
        for(int a=0;a<answerRecord.size();a++){
            starttime=answerRecord.get(a).getStartTime().getTime();
            endtime=answerRecord.get(a).getEndTime().getTime();
            starttime/=1000;//换算到秒
            endtime/=1000;//换算到秒
            callingtime=endtime-starttime;
            if(callingtime%60==0) callingtime/=60;//换算到分
            else callingtime=callingtime/60+1;
            mix+=callingtime*0.3;
        }
        return mix;
    }
}
abstract class CommunicationRecord{
    protected String callingNumber;
    protected String answerNumber;
    protected String getCallingNumber(){
        return this.callingNumber;
    }
    protected String getAnswerNumber(){
        return this.answerNumber;
    }
    void setCallingNumber(String callingNumber){
        this.callingNumber=callingNumber;
    }
    String answerCallingNumber(){
        return this.answerNumber;
    }
    void setAnswerNumber(String answerNumber){
        this.answerNumber=answerNumber;
    }
}

class CallRecord extends CommunicationRecord{
    private Date startTime;
    private Date endTime;
    private String callingAddressAreaCode;
    private String answerAddressAreaCode;
    Date getStartTime(){
        return this.startTime;
    }
    void setStartTime(Date startTime){
        this.startTime=startTime;
    }
    Date getEndTime(){
        return this.endTime;
    }
    void setEndTime(Date endTime){
        this.endTime=endTime;
    }
    String getCallingAddressAreaCode(){
        return this.callingAddressAreaCode;
    }
    void setCallingAddressAreaCode(String callingAddressAreaCode){
        this.callingAddressAreaCode=callingAddressAreaCode;
    }
    String getAnswerAddressAreaCode(){
        return answerAddressAreaCode;
    }
    void setAnswerAddressAreaCode(String answerAddressAreaCode){
        this.answerAddressAreaCode=answerAddressAreaCode;
    }
}

class MessageRecord extends CommunicationRecord{
    String message;
}

 

 

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

实现一个简单的电信计费程序,针对手机的短信采用如下计费方式
1、接收短信免费,发送短信0.1元/条,超过3条0.2元/条,超过5条0.3元/条。
2、如果一次发送短信的字符数量超过10个,按每10个字符一条短信进行计算。

输入:
输入信息包括两种类型
1、逐行输入南昌市手机用户开户的信息,每行一个用户。
格式:u-号码 计费类型 (计费类型包括:0-座机 1-手机实时计费 2-手机A套餐 3-手机短信计费)
例如:u-13305862264 3
座机号码由区号和电话号码拼接而成,电话号码包含7-8位数字,区号最高位是0。
手机号码由11位数字构成,最高位是1。
本题只针对类型3-手机短信计费。
2、逐行输入本月某些用户的短信信息,短信的格式:
m-主叫号码,接收号码,短信内容 (短信内容只能由数字、字母、空格、英文逗号、英文句号组成)
m-18907910010 13305862264 welcome to jiangxi.
m-13305862264 18907910010 thank you.

注意:以上两类信息,先输入所有开户信息,再输入所有通讯信息,最后一行以“end”结束。
输出:
根据输入的详细短信信息,计算所有已开户的用户的当月短信费用(精确到小数点后2位,单位元)。假设每个用户初始余额是100元。
每条短信信息均单独计费后累加,不是将所有信息累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。
错误处理:
输入数据中出现的不符合格式要求的行一律忽略。
本题只做格式的错误判断,无需做内容上不合理的判断,比如同一个电话两条通讯记录的时间有重合、开户号码非南昌市的号码、自己给自己打电话等,此类情况都当成正确的输入计算。但时间的输入必须符合要求,比如不能输入2022.13.61 28:72:65。

本题只考虑短信计费,不考虑通信费用以及月租费。

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

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(短信记录)是它的子类。

image.png

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

(提示:可以从UserRecords类中获取各种类型的callRecords)。

注意:以上图中所定义的类不是限定要求,根据实际需要自行补充或修改。

输入样例:

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

u-18907910010 3
m-18907910010 13305862264 aaaaaaaaaaaaaaaaaaaaaaa
end

输出样例:

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

18907910010 0.3 99.7
### 输入样例1:

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

u-18907910010 3
m-18907910010 13305862264 aaaaaaaaaaaa
m-18907910010 13305862264 aaaaaaa.
m-18907910010 13305862264 bb,bbbb
end

输出样例1:

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

18907910010 0.5 99.5

  短信计费较为简单,照着题目给的类图画即可

  input函数 与上一题的input类似,没啥创新的,唯一要注意的点是,短信信息不合法如果出现非字母数字  '.'  ,  ','   ,  ' '这些字符则说明该短信是不合法的,整条短信都不会发送,所以不计入发送的数目

    public static void input(ArrayList<User> user){
        Scanner scan=new Scanner(System.in);
        String reg1="(^u-0[0-9]{10,11} 3)|(^u-1[0-9]{10} 3)";
        String reg2="(^m-1[0-9]{10} 1[0-9]{10} [A-Za-z0-9. ,]*)";
        String str,quhao1,quhao2,sj1,sj2,num1,num2;
        String[] s1;
        String[] s2=new String[3];
        while(scan.hasNext()){//while
            str=scan.nextLine();
            if(str.equals("end")) break;
            if(str.matches(reg1)){
                boolean cf=false;
                str=str.substring(2);
                s1=str.split(" ");
                for (User value : user) {
                    if (s1[0].equals(value.getNumber())) {
                        cf = true;
                        break;
                    }
                }
                if(!cf){
                        user.add(new User(3));
                    user.get(user.size()-1).setNumber(s1[0]);
                }
            }
            else if(str.matches(reg2)){
                str=str.substring(2);//去除头部
                int tmp=str.indexOf(" ");
                s2[0]=str.substring(0,tmp);
                str=str.substring(tmp+1);
                tmp=str.indexOf(" ");
                s2[1]=str.substring(0,tmp);
                s2[2]=str.substring(tmp+1);


                int xuhao1=0;
                boolean find1=false,find2=false;
                for(;xuhao1<user.size();){
                    if(user.get(xuhao1).getNumber().equals(s2[0])) find1=true;
                    if(!find1) xuhao1++;
                    if(find1) break;
                }

                if(xuhao1<user.size()){//if成功找到对应用户的序号
                    MessageRecord messageRecord=new MessageRecord();
                    messageRecord.setMessage(s2[2]);
                    user.get(xuhao1).getUserRecords().addReceiveMessageRecords(messageRecord);
                }//if成功找到对应用户的序号
            }//正则2
        }//while
    }
class MobileCharging extends ChargeMode{
    private double monthlyRent=15;
    MobileCharging(){
        super();
        getChargeRules().add(new MobileInCityRule());
        getChargeRules().add(new MobileInProvinceRule());
        getChargeRules().add(new MobileInlandRule());
        getChargeRules().add(new SendMessageRule());
    }
    @Override
    public double calcost(UserRecords userRecords){//计算短信费
        double mix=0;
        mix+=((SendMessageRule)getChargeRules().get(3)).calcost(userRecords.getReceiveMessageRecords());
        return mix;
    }
    @Override
    double getMonthlyRend(){
        return this.monthlyRent;
    }
}
abstract class MessageChargeRule extends ChargeRule{
    abstract double calcost(ArrayList<MessageRecord> messageRecords);
}
class SendMessageRule extends MessageChargeRule{
    @Override
    double calcost(ArrayList<MessageRecord> MessageRecord){
        boolean legitimate=true;
        int tmp=0;
        for(int a=0;a<MessageRecord.size();a++){
            tmp+=MessageRecord.get(a).getMessage().length()/10;
            if(MessageRecord.get(a).getMessage().length()%10!=0) tmp++;
        }
        if(tmp>5) return 0.7+(tmp-5)*0.3;
        else if(tmp>3) return 0.3+(tmp-3)*0.2;
        else return tmp*0.1;
    }
}

 

作业心得:

  通过这次大作业,加强了对题目问题的分析能力,这三次电信计费练习涉及了许多输入有效性的判断。更清楚的认识到了面向对象的重要性。在基础的语法下熟练使用继承、多态、抽象类、包装类进行编写代码,面向对象的程序设计的重点在于如何创建对象,对象完成相应功能,类图的设计很重要。吸取了多边形系列的教训,认识到了代码的合理设计的重要性。第一次代码如果能合理设计,那么第二第三次代码只需要基于第一次题目的基础上增加相应功能,功能拓展我不需要改变太多代码就能实现。也通过系列题目认识到了自己在算法上的不足,以及知识面拓展能力较差,以后要多阅读官方文档和大佬的博客文章学习知识点。老师的课堂上也灌输了很多开发过程中有用的经验和知识点.

标签:s2,号码,double,ArrayList,面向对象,new,计费,程序设计
From: https://www.cnblogs.com/-Jeffrey-/p/16951612.html

相关文章