首页 > 其他分享 >Blog3:nchu-software-oop-2022-6~8

Blog3:nchu-software-oop-2022-6~8

时间:2022-12-10 21:57:22浏览次数:47  
标签:inputs return String nchu double Blog3 public oop ArrayList

Blog3:nchu-software-oop-2022-6~8

一、前言

  不知不觉,已到了本学期末尾,面向对象程序设计这门课也已经结课。课程虽然结束了,但需要学习和巩固的知识点还有很多。同时,这也是最后一次blog总结。这次Blog主要针对PTA6~8次大作业中的电信计费系列的题目进行总结。该系列重点是考查了正则表达式的正确使用,对类图的分析能力(即借助类图去编写代码,完成题目要求),抽象类和与其子类的继承关系,还有集合框架List相关练习。涉及的内容比较多,整套题目都感觉十分复杂。但是这系列题目将我们学习的内容融合在一起,锻炼我们的整体能力。比如在电信计费简化版本--座机计费题目中,我们要知道怎么处理那些通话记录--即首先运用split方法和substring等知识去将输入的通讯信息分开,分别得到用户开户的信息及其号码,然后才能创建一个用户,对于每条通讯信息需要用ArrayList去一一储存。该系列第二题是在原基础上加手机用户的参与,要创建新的手机用户类等等。在电信计费的题目中也接触学习到了一些新知识,比如时间需要Date类,才能进行差的计算,也熟练掌握了增强for循环的运用。

 

二、设计与分析

 题目1:

7-1 电信计费系列1-座机计费 分数 80 作者 蔡轲 单位 南昌航空大学

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

建议类图:
参见图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)。
******************************************************************************************************************************************************
import java.util.*;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.math.BigDecimal;

class Input//输入操作类
{
    public int Checkinput(String a)
    {
        String[] tokens=a.split(" ");
        if(tokens.length==2&&tokens[0].matches("^u-[0-9]{11,13}$")&&(Integer.parseInt(tokens[1])>=0&&Integer.parseInt(tokens[1])<=2))
            return 1;
        else if(tokens.length==6&&tokens[0].matches("[t]-0791[0-9]{7,8}")&&tokens[1].matches(".[0-9]{9,11}")&&(Checkdata(tokens[2])&&Checktime(tokens[3])&&Checkdata(tokens[4])&&Checktime(tokens[5])))
            return 2;
        else
            return 0;
    }
    public boolean Checktime(String tokens)
    {//判断时间合法
        if (tokens.matches("^([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$"))
            return true;
        else
            return false;
    }
    public boolean Checkdata(String tokens)
    {//判断日期合法
        if(!tokens.matches("\\d{4}+[.]\\d{1,2}+[.]\\d{1,2}+"))
            return false;
  
            String[] arr=tokens.split("\\.");
            int year=Integer.valueOf(arr[0]);
            int month=Integer.valueOf(arr[1]);
            int day=Integer.valueOf(arr[2]);

            if(month<1||month>12)
                return false;

            int[] monthdays=new int[]{31,0,31,30,31,30,31,31,30,31,30,31};
            if((year%4==0&&year%100!=0)||year%400==0)//闰年
                monthdays[1]=29;
            else
                monthdays[1]=28;
            if(day<1||day>monthdays[month-1])
                return false;
        
        return true;
    }
    
    public void inputUser(ArrayList<User> users, String a){
        User user=new User();
        String[] tokens=a.split(" ");
        String num=tokens[0].substring(2);
        for(User i:users){
            if(i.getNumber().equals(num))
                return;
        }
        user.setNumber(num);
        int mode=Integer.parseInt(tokens[1]);
        if(mode==0)
            user.setChargeMode(new LandlinePhoneCharging());
        users.add(user);
    }
    public void inputRecord(ArrayList<User> users, String input){
        String[] inputs=input.split(" ");
        inputs[0] = inputs[0].replace("t-", "");
 
        User callu = null, answeru = null;
        CallRecord callrecord = new CallRecord(inputs);

        for (User i : users){
            if (i.getNumber().equals(inputs[0]))
                callu = i;
            if (i.getNumber().equals(inputs[1]))
                answeru = i;
            if (callu != null && answeru != null)
                break;
        }
 
        if (callu != null){
            if (callrecord.getCallType() == 1)
                callu.getUserRecords().addCallingInCityRecords(callrecord);
            else if (callrecord.getCallType() == 2)
                callu.getUserRecords().addCallingInProvinceRecords(callrecord);
            else
                callu.getUserRecords().addCallingInLandRecords(callrecord);
        }
 
        if (answeru != null){
            if (callrecord.getCallType() == 1)
                answeru.getUserRecords().addAnswerInCityRecords(callrecord);
            else if (callrecord.getCallType() == 2)
                answeru.getUserRecords().aaddAnswerInProvinceRecords(callrecord);
            else
                answeru.getUserRecords().addAnswerInLandRecords(callrecord);
        }
    }
}

class UserRecords//用户记录类
{
    private ArrayList<CallRecord> callingInCityRecords=new ArrayList<CallRecord>();
    private ArrayList<CallRecord> callingInProvinceRecords=new ArrayList<CallRecord>();
    private ArrayList<CallRecord> callingInLandRecords=new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInCityRecords=new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInProvinceRecords=new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInLandRecords=new ArrayList<CallRecord>();
    private ArrayList<MessageRecord> sendMessageRecords=new ArrayList<MessageRecord>();
    private ArrayList<MessageRecord> receiveMessageRecords=new ArrayList<MessageRecord>();
    
    public void addCallingInCityRecords(CallRecord callRecord){
        callingInCityRecords.add(callRecord);
    }
    public void addCallingInProvinceRecords(CallRecord callRecord){
        callingInProvinceRecords.add(callRecord);
    }
    public void addCallingInLandRecords(CallRecord callRecord){
        callingInLandRecords.add(callRecord);
    }
    public void addAnswerInCityRecords(CallRecord answerRecord){
        answerInCityRecords.add(answerRecord);
    }
    public void aaddAnswerInProvinceRecords(CallRecord answerRecord){
        answerInProvinceRecords.add(answerRecord);
    }
    public void addAnswerInLandRecords(CallRecord answerRecord){
        answerInLandRecords.add(answerRecord);
    }
    
    public void addSendMessageRecords(MessageRecord callRecord){
        sendMessageRecords.add(callRecord);
    }
    public ArrayList<MessageRecord> getSendMessageRecords(){
        return sendMessageRecords;
    }
    public void setSendMessageRecords(ArrayList<MessageRecord> sendMessageRecords){
        this.sendMessageRecords = sendMessageRecords;
    }
    public void addReceiveMessageRecords(MessageRecord callRecord){
        receiveMessageRecords.add(callRecord);
    }
    public ArrayList<MessageRecord> getReceiveMessageRecords(){
        return receiveMessageRecords;
    }
    public void setReceiveMessageRecords(ArrayList<MessageRecord> receiveMessageRecords){
        this.receiveMessageRecords = receiveMessageRecords;
    }
 
    public ArrayList<CallRecord> getCallingInCityRecords(){
        return callingInCityRecords;
    }
    public void setCallingInCityRecords(ArrayList<CallRecord> callingInCityRecords){
        this.callingInCityRecords = callingInCityRecords;
    }
    public ArrayList<CallRecord> getCallingInProvinceRecords(){
        return callingInProvinceRecords;
    }
    public void setCallingInProvinceRecords(ArrayList<CallRecord> callingInProvinceRecords){
        this.callingInProvinceRecords = callingInProvinceRecords;
    }
    public ArrayList<CallRecord> getCallingInLandRecords(){
        return callingInLandRecords;
    }
    public void setCallingInLandRecords(ArrayList<CallRecord> callingInLandRecords){
        this.callingInLandRecords = callingInLandRecords;
    }
    
    public ArrayList<CallRecord> getAnswerInCityRecords(){
        return answerInCityRecords;
    }
     public void setAnswerInCityRecords(ArrayList<CallRecord> answerInCityRecords){
        this.answerInCityRecords = answerInCityRecords;
    }
    public ArrayList<CallRecord> getAnswerInProvinceRecords(){
        return answerInProvinceRecords;
    }
    public void setAnswerInProvinceRecords(ArrayList<CallRecord> answerInProvinceRecords){
        this.answerInProvinceRecords = answerInProvinceRecords;
    }
    public ArrayList<CallRecord> getAnswerInLandRecords(){
        return answerInLandRecords;
    }
    public void setAnswerInLandRecords(ArrayList<CallRecord> answerInLandRecords){
        this.answerInLandRecords = answerInLandRecords;
    }
}
abstract class ChargeMode//计费方式类
{
    protected ArrayList<ChargeRule> chargeRules = new ArrayList<>();
    
    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();
}
class LandlinePhoneCharging extends ChargeMode
{
    private double monthlyRent=20;
    public LandlinePhoneCharging(){
        super();
        chargeRules.add(new LandPhoneInCityRule());
        chargeRules.add(new LandPhoneInProvinceRule());
        chargeRules.add(new LandPhoneInlandRule());
    }

    @Override
    public double calCost(UserRecords userRecords){
        double sumCost=0;
        for(ChargeRule rule:chargeRules){
            sumCost=rule.calCost(userRecords)+sumCost;
        }
        return sumCost;
    }
    @Override
    public double getMonthlyRent(){
        return monthlyRent;
    }
}
class User//用户类
{
    private UserRecords userRecords=new UserRecords();
    private double balance=100;
    private ChargeMode chargeMode;
    private String number;
    
    public double calBalance(){
        return balance-chargeMode.getMonthlyRent()-chargeMode.calCost(userRecords);
    }
    public double calCost(){
        return chargeMode.calCost(userRecords);
    }
    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;
    }
}

abstract class CommunicationRecord//抽象通讯记录类
{
    protected String callingNumber;
    protected String answerNumber;
    
    public String getCallingNumber(){
        return callingNumber;
    }
    public void setCallingNumber(String callingNumber){
        this.callingNumber = callingNumber;
    }
    public String getAnswerNumber(){
        return answerNumber;
    }
    public void setAnswerNumbe(String answerNumber){
        this.answerNumber = answerNumber;
    }
}
class CallRecord extends CommunicationRecord//通话记录类
{
    private Date startTime;
    private Date endTime;
    private String callingAddressAreaCode;
    private String answerAddressAreaCode;
    
    public int getCallType(){
        if (callingAddressAreaCode.equals(answerAddressAreaCode))
            return 1;
        if (callingAddressAreaCode.matches("^079[0-9]$")||callingAddressAreaCode.equals("0701")){
            if (answerAddressAreaCode.matches("^079[0-9]$")||answerAddressAreaCode.equals("0701"))
                return 2;
        }
        return 3;
    }
    public CallRecord(String[] inputs){
        super();
        if (inputs[0].length()==10)
            callingAddressAreaCode=inputs[0].substring(0,3);
        else
            callingAddressAreaCode=inputs[0].substring(0,4);
            
        if (inputs[1].length()==10)
            answerAddressAreaCode=inputs[1].substring(0,3);
        else
            answerAddressAreaCode=inputs[1].substring(0,4);
            
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault());
        try{
            startTime = simpleDateFormat.parse(inputs[2] + " " + inputs[3]);
            endTime = simpleDateFormat.parse(inputs[4] + " " + inputs[5]);
        }catch(ParseException e){
        }
    }
    public CallRecord(Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) {
        super();
        this.startTime = startTime;
        this.endTime = endTime;
        this.callingAddressAreaCode=callingAddressAreaCode;
        this.answerAddressAreaCode=answerAddressAreaCode;
    }
    
    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;
    }
}
class MessageRecord extends CommunicationRecord//短信记录类
{
    private String message;
    
    public String getMessage(){
        return message;
    }
    public void setMessage(String message){
        this.message = message;
    }
}

abstract class ChargeRule
{
    abstract public double calCost(UserRecords userRecords);
}
abstract class CallChargeRule extends ChargeRule
{
    //public double calCost(ArrayList<CallRecord> callRecords);
}
class LandPhoneInCityRule extends CallChargeRule
{
    @Override
    public double calCost(UserRecords userRecords){
        double sumCost=0;
        for(CallRecord call:userRecords.getCallingInCityRecords())
        {
            double distanceS=(call.getEndTime().getTime()-call.getStartTime().getTime())/1000;
            if(distanceS<0)
                continue;
             double distanceM=(int)distanceS/60;
             if(distanceS%60!=0)
                distanceM=distanceM+1;
             sumCost=sumCost+distanceM*0.1;
        }
        return sumCost;
    }
}
class LandPhoneInProvinceRule extends CallChargeRule
{
    @Override
     public double calCost(UserRecords userRecords){
        double sumCost=0;
        for(CallRecord call:userRecords.getCallingInProvinceRecords())
        {
            double distanceS=(call.getEndTime().getTime()-call.getStartTime().getTime())/1000;
            if(distanceS<0)
                continue;
             double distanceM=(int)distanceS/60;
             if(distanceS%60!=0)
                distanceM=distanceM+1;
             sumCost=sumCost+distanceM*0.3;
        }
        return sumCost;
    }
}
class LandPhoneInlandRule extends CallChargeRule
{
    @Override
     public double calCost(UserRecords userRecords){
        double sumCost=0;
        for(CallRecord call:userRecords.getCallingInLandRecords())
        {
            double distanceS=(call.getEndTime().getTime()-call.getStartTime().getTime())/1000;
            if(distanceS<0)
                continue;
             double distanceM=(int)distanceS/60;
             if(distanceS%60!=0)
                distanceM=distanceM+1;
             sumCost=sumCost+distanceM*0.6;
        }
        return sumCost;
    }
}

class output
{
    public void output(double out){
        BigDecimal numb=new BigDecimal(out);
        out=numb.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.print(out);
    }
}

public class Main
{
    public static void main(String[] args)
    {
        Input Input=new Input();
        ArrayList<User> users = new ArrayList<>();
        output output=new output();
        Scanner in=new Scanner(System.in);
        String a=in.nextLine();
        for(int i=0;;i++)
        {
            if(a.equals("end"))
                break;
            if(Input.Checkinput(a)==1)
                Input.inputUser(users,a);
            else if(Input.Checkinput(a)==2)
                Input.inputRecord(users,a);

            a=in.nextLine();
        }
        
        users.sort(new Comparator<User>(){
            @Override
            public int compare(User u1,User u2){
                if(Double.parseDouble(u1.getNumber()) > Double.parseDouble(u2.getNumber()))
                    return 1;
                else
                    return -1;
            }
        });
        for(User u:users){
            System.out.print(u.getNumber()+" ");
            output.output(u.calCost());
            System.out.print(" ");
            output.output(u.calBalance());
            System.out.print("\n");
        }
    }
}
电信系列1

  这一次题目要求我们仅仅只针对座机对于座机的拨打与接受,以南昌市的座机作为背景,进行电信计费的模拟。题目对于计费有一定的条件限制,这也是我们需要注意的地方,然后,根据题目设计好了user类和需要判断日期和计算时间的类,还有记录通话记录的类、设备类作为座机的父类。然后再去按照输入输出去设计Main类里的一些判断和与其他类的交互,中途很多次添加了一些额外判断去忽略那些错误输入。

 

 

题目2:

7-1 电信计费系列2-手机+座机计费 分数 80 作者 蔡轲 单位 南昌航空大学

实现南昌市电信分公司的计费程序,假设该公司针对手机和座机用户分别采取了两种计费方案,分别如下:
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、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三个类分别是座机拨打市内、省内、省外电话的计费规则类,用于实现这三种情况的费用计算。    
 ***********************************************************************************************************************************  
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.text.ParseException;
 
public class Main {
 
    public static void main(String[] args) {
 
        Outputtool outputtool = new Outputtool();
 
        Inputdeal inputdeal = new Inputdeal();
 
        ArrayList<User> users = new ArrayList<>();
 
        Scanner in = new Scanner(System.in);
 
        String input = in.nextLine();
 
        while (!input.equals("end")) {
            if (1 == inputdeal.check(input)) {
                inputdeal.writeUser(users, input);
            } else if (2 == inputdeal.check(input)) {
                inputdeal.writeRecord(users, input);
            }
            input = in.nextLine();
        }
 
        users.sort(new Comparator<User>() {
 
            @Override
            public int compare(User u1, User u2) {
                if (u1.getNumber().charAt(0) == '0' && u2.getNumber().charAt(0) != '0') {
                    return -1;
                } else if (u1.getNumber().charAt(0) != '0' && u2.getNumber().charAt(0) == '0') {
                    return 1;
                }
                if (Double.parseDouble(u1.getNumber()) > Double.parseDouble(u2.getNumber())) {
                    return 1;
                } else {
                    return -1;
                }
            }
        });
 
        for (User u : users) {
            System.out.print(u.getNumber() + " ");
            outputtool.output(u.calCost());
            System.out.print(" ");
            outputtool.output(u.calBalance());
            System.out.println();
 
        }
 
    }
 
}
 
abstract class ChargeMode {
    protected ArrayList<ChargeRule> chargeRules = new ArrayList<>();
 
    public abstract double calCost(UserRecords userRecords);
 
    public abstract double getMonthlyRent();
 
    public ArrayList<ChargeRule> getChargeRules() {
        return chargeRules;
    }
 
    public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
        this.chargeRules = chargeRules;
    }
}
 
class UserRecords {
 
    private ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();
    private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();
    private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();
 
    public void addCallingInCityRecords(CallRecord callRecord) {
        callingInCityRecords.add(callRecord);
    }
 
    public void addCallingInProvinceRecords(CallRecord callRecord) {
        callingInProvinceRecords.add(callRecord);
    }
 
    public void addCallingInLandRecords(CallRecord callRecord) {
        callingInLandRecords.add(callRecord);
    }
 
    public void addAnswerInCityRecords(CallRecord callRecord) {
        answerInCityRecords.add(callRecord);
    }
 
    public void aaddAnswerInProvinceRecords(CallRecord callRecord) {
        answerInProvinceRecords.add(callRecord);
    }
 
    public void addAnswerInLandRecords(CallRecord callRecord) {
        answerInLandRecords.add(callRecord);
    }
 
    public void addSendMessageRecords(MessageRecord callRecord) {
        sendMessageRecords.add(callRecord);
    }
 
    public void addReceiveMessageRecords(MessageRecord callRecord) {
        receiveMessageRecords.add(callRecord);
    }
 
    public ArrayList<CallRecord> getCallingInCityRecords() {
        return callingInCityRecords;
    }
 
    public void setCallingInCityRecords(ArrayList<CallRecord> callingInCityRecords) {
        this.callingInCityRecords = callingInCityRecords;
    }
 
    public ArrayList<CallRecord> getCallingInProvinceRecords() {
        return callingInProvinceRecords;
    }
 
    public void setCallingInProvinceRecords(ArrayList<CallRecord> callingInProvinceRecords) {
        this.callingInProvinceRecords = callingInProvinceRecords;
    }
 
    public ArrayList<CallRecord> getCallingInLandRecords() {
        return callingInLandRecords;
    }
 
    public void setCallingInLandRecords(ArrayList<CallRecord> callingInLandRecords) {
        this.callingInLandRecords = callingInLandRecords;
    }
 
    public ArrayList<CallRecord> getAnswerInCityRecords() {
        return answerInCityRecords;
    }
 
    public void setAnswerInCityRecords(ArrayList<CallRecord> answerInCityRecords) {
        this.answerInCityRecords = answerInCityRecords;
    }
 
    public ArrayList<CallRecord> getAnswerInProvinceRecords() {
        return answerInProvinceRecords;
    }
 
    public void setAnswerInProvinceRecords(ArrayList<CallRecord> answerInProvinceRecords) {
        this.answerInProvinceRecords = answerInProvinceRecords;
    }
 
    public ArrayList<CallRecord> getAnswerInLandRecords() {
        return answerInLandRecords;
    }
 
    public void setAnswerInLandRecords(ArrayList<CallRecord> answerInLandRecords) {
        this.answerInLandRecords = answerInLandRecords;
    }
 
    public ArrayList<MessageRecord> getSendMessageRecords() {
        return sendMessageRecords;
    }
 
    public void setSendMessageRecords(ArrayList<MessageRecord> sendMessageRecords) {
        this.sendMessageRecords = sendMessageRecords;
    }
 
    public ArrayList<MessageRecord> getReceiveMessageRecords() {
        return receiveMessageRecords;
    }
 
    public void setReceiveMessageRecords(ArrayList<MessageRecord> receiveMessageRecords) {
        this.receiveMessageRecords = receiveMessageRecords;
    }
 
}
 
class LandlinePhoneCharging extends ChargeMode {
 
    private double monthlyRent = 20;
 
    public LandlinePhoneCharging() {
        super();
        chargeRules.add(new LandPhoneInCityRule());
        chargeRules.add(new LandPhoneInProvinceRule());
        chargeRules.add(new LandPhoneInlandRule());
    }
 
    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (ChargeRule rule : chargeRules) {
            sumCost += rule.calCost(userRecords);
        }
        return sumCost;
    }
 
    @Override
    public double getMonthlyRent() {
        return monthlyRent;
    }
 
}
 
class MobilePhoneCharging extends ChargeMode {
 
    private double monthlyRent = 15;
 
    public MobilePhoneCharging() {
        super();
        chargeRules.add(new MobilePhoneInCityRule());
        chargeRules.add(new MobilePhoneInProvinceRule());
        chargeRules.add(new MobilePhoneInlandRule());
    }
 
    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (ChargeRule rule : chargeRules) {
            sumCost += rule.calCost(userRecords);
        }
        return sumCost;
    }
 
    @Override
    public double getMonthlyRent() {
        return monthlyRent;
    }
 
}
 
class Inputdeal {
 
    public int check(String input) {
        if (input.matches("[u]-0791[0-9]{7,8}\\s[0]") || input.matches("[u]-1[0-9]{10}\\s[1]")) {
            return 1;
//        } else if (input.charAt(0) == 'm') {
//            return 2;
        } else if (input.matches("(([t]-0791[0-9]{7,8}\\s" + "0[0-9]{9,11}\\s)|" 
                + "([t]-0791[0-9]{7,8}\\s" + "1[0-9]{10}\\s" + "0[0-9]{2,3}\\s)|" 
                + "([t]-1[0-9]{10}\\s" + "0[0-9]{2,3}\\s" + "0[0-9]{9,11}\\s)|" 
                + "([t]-1[0-9]{10}\\s" + "0[0-9]{2,3}\\s" + "1[0-9]{10}\\s" + "0[0-9]{2,3}\\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])")) {
            return 2;
        }
        return 0;
    }
 
    @SuppressWarnings("unused")
    private boolean validatet(String string) {
        if (!string.matches("^([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$")) {
            return false;
        }
        return true;
    }
 
    public static boolean validate(String dateString) {
        // 使用正则表达式 测试 字符 符合 dddd.dd.dd 的格式(d表示数字)
        Pattern p = Pattern.compile("\\d{4}+[\\.]\\d{1,2}+[\\.]\\d{1,2}+");
        Matcher m = p.matcher(dateString);
        if (!m.matches()) {
            return false;
        }
 
        // 得到年月日
        String[] array = dateString.split("\\.");
        int year = Integer.valueOf(array[0]);
        int month = Integer.valueOf(array[1]);
        int day = Integer.valueOf(array[2]);
 
        if (month < 1 || month > 12) {
            return false;
        }
        int[] monthLengths = new int[] { 0, 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        if (isLeapYear(year)) {
            monthLengths[2] = 29;
        } else {
            monthLengths[2] = 28;
        }
        int monthLength = monthLengths[month];
        if (day < 1 || day > monthLength) {
            return false;
        }
        return true;
    }
 
    /** 是否是闰年 */
    private static boolean isLeapYear(int year) {
        return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
    }
 
    public boolean judge(String input) {
 
        return false;
    }
 
    public void writeUser(ArrayList<User> users, String input) {
        User usernew = new User();
        String[] inputs = input.split(" ");
        String num = inputs[0].substring(2);
        for (User i : users) {
            if (i.getNumber().equals(num)) {
                return;
            }
        }
        usernew.setNumber(num);
        int mode = Integer.parseInt(inputs[1]);
        if (mode == 0) {
            usernew.setChargeMode(new LandlinePhoneCharging());
        } else if (mode == 1) {
            usernew.setChargeMode(new MobilePhoneCharging());
        }
        users.add(usernew);
    }
 
    public void writeRecord(ArrayList<User> users, String input) {
        String[] inputs = input.split(" ");
 
        User callu = null, answeru = null;
        CallRecord callrecord = new CallRecord(inputs);
 
        if (input.charAt(0) == 't') {
            String out = inputs[0];
            String in = "";
            if (inputs.length == 6) {
                in = inputs[1];
            } else if (inputs.length == 7) {
                in = inputs[1];
            } else if (inputs.length == 8) {
                in = inputs[2];
            }
 
            for (User i : users) {
                if (i.getNumber().equals(out)) {
                    callu = i;
                }
                if (i.getNumber().equals(in)) {
                    answeru = i;
                }
                if (callu != null && answeru != null) {
                    break;
                }
            }
 
            if (callu != null) {
                if (callrecord.getCallType().matches("^1[1-3]$")) {
                    callu.getUserRecords().addCallingInCityRecords(callrecord);
                } else if (callrecord.getCallType().matches("^2[1-3]$")) {
                    callu.getUserRecords().addCallingInProvinceRecords(callrecord);
                } else {
                    callu.getUserRecords().addCallingInLandRecords(callrecord);
                }
            }
 
            if (answeru != null) {
                if (callrecord.getCallType().matches("^[1-3]1$")) {
                    answeru.getUserRecords().addAnswerInCityRecords(callrecord);
                } else if (callrecord.getCallType().matches("^[1-3]2$")) {
                    answeru.getUserRecords().aaddAnswerInProvinceRecords(callrecord);
                } else {
                    answeru.getUserRecords().addAnswerInLandRecords(callrecord);
                }
            }
        } else if (input.charAt(0) == 'm') {
 
        }
 
    }
 
}
 
abstract class CommunicationRecord {
    protected String callingNumber;
    protected String answerNumbe;
 
    public String getCallingNumber() {
        return callingNumber;
    }
 
    public void setCallingNumber(String callingNumber) {
        this.callingNumber = callingNumber;
    }
 
    public String getAnswerNumbe() {
        return answerNumbe;
    }
 
    public void setAnswerNumbe(String answerNumbe) {
        this.answerNumbe = answerNumbe;
    }
 
}
 
abstract class ChargeRule {
 
    abstract public double calCost(UserRecords userRecords);
 
}
 
class CallRecord extends CommunicationRecord {
    private Date startTime;
    private Date endTime;
    private String callingAddressAreaCode;
    private String answerAddressAreaCode;
 
    public String getCallType() {
        String type = "";
        if (callingAddressAreaCode.equals("0791")) {
            type = type.concat("1");
        } else if (callingAddressAreaCode.matches("^079[023456789]$") || callingAddressAreaCode.equals("0701")) {
            type = type.concat("2");
        } else {
            type = type.concat("3");
        }
 
        if (answerAddressAreaCode.equals("0791")) {
            type = type.concat("1");
        } else if (answerAddressAreaCode.matches("^079[023456789]$") || answerAddressAreaCode.equals("0701")) {
            type = type.concat("2");
        } else {
            type = type.concat("3");
        }
 
        return type;
    }
 
    public CallRecord(String[] inputs) {
        super();
 
        char type = inputs[0].charAt(0);
        inputs[0] = inputs[0].substring(2);
 
        String sd = null, st = null, ed = null, et = null;
 
        if (type == 't') {
            if (inputs.length == 6) {
                sd = inputs[2];
                st = inputs[3];
                ed = inputs[4];
                et = inputs[5];
                callingAddressAreaCode = inputs[0].substring(0, 4);
                answerAddressAreaCode = inputs[1].substring(0, 4);
            } else if (inputs.length == 7) {
                sd = inputs[3];
                st = inputs[4];
                ed = inputs[5];
                et = inputs[6];
                if (inputs[0].charAt(0) != '0') {
                    if (inputs[2].length() == 10) {
                        answerAddressAreaCode = inputs[2].substring(0, 3);
                    } else {
                        answerAddressAreaCode = inputs[2].substring(0, 4);
                    }
                    callingAddressAreaCode = inputs[1];
                } else {
                    if (inputs[0].length() == 10) {
                        callingAddressAreaCode = inputs[0].substring(0, 3);
                    } else {
                        callingAddressAreaCode = inputs[0].substring(0, 4);
                    }
                    answerAddressAreaCode = inputs[2];
                }
            } else if (inputs.length == 8) {
                sd = inputs[4];
                st = inputs[5];
                ed = inputs[6];
                et = inputs[7];
                callingAddressAreaCode = inputs[1];
                answerAddressAreaCode = inputs[3];
            }
        } else if (type == 'm') {
 
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault());
        try {
            startTime = simpleDateFormat.parse(sd + " " + st);
            endTime = simpleDateFormat.parse(ed + " " + et);
        } catch (ParseException e) {
        }
 
    }
 
    public CallRecord(Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) {
        super();
        this.startTime = startTime;
        this.endTime = endTime;
        this.callingAddressAreaCode = callingAddressAreaCode;
        this.answerAddressAreaCode = answerAddressAreaCode;
    }
 
    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 CallChargeRule extends ChargeRule {
 
}
 
class LandPhoneInCityRule extends CallChargeRule {
 
    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (CallRecord call : userRecords.getCallingInCityRecords()) {
            double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (distanceS < 0) {
                continue;
            }
            double distanceM = (int) distanceS / 60;
            if (distanceS % 60 != 0) {
                distanceM += 1;
            }
            if (call.getCallType().equals("11")) {
                sumCost += distanceM * 0.1;
            } else if (call.getCallType().equals("12")) {
                sumCost += distanceM * 0.3;
            } else if (call.getCallType().equals("13")) {
                sumCost += distanceM * 0.6;
            }
        }
        return sumCost;
    }
 
}
 
class LandPhoneInlandRule extends CallChargeRule {
 
    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (CallRecord call : userRecords.getCallingInLandRecords()) {
            double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (distanceS < 0) {
                continue;
            }
            double distanceM = (int) distanceS / 60;
            if (distanceS % 60 != 0) {
                distanceM += 1;
            }
            sumCost += distanceM * 0.6;
        }
        return sumCost;
    }
 
}
 
class LandPhoneInProvinceRule extends CallChargeRule {
 
    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
            double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (distanceS < 0) {
                continue;
            }
            double distanceM = (int) distanceS / 60;
            if (distanceS % 60 != 0) {
                distanceM += 1;
            }
            sumCost += distanceM * 0.3;
        }
        return sumCost;
    }
 
}
 
class MobilePhoneInCityRule extends CallChargeRule {
 
    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (CallRecord call : userRecords.getCallingInCityRecords()) {
            double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (distanceS < 0) {
                continue;
            }
            double distanceM = (int) distanceS / 60;
            if (distanceS % 60 != 0) {
                distanceM += 1;
            }
            if (call.getCallType().equals("11")) {
                sumCost += distanceM * 0.1;
            } else if (call.getCallType().equals("12")) {
                sumCost += distanceM * 0.2;
            } else if (call.getCallType().equals("13")) {
                sumCost += distanceM * 0.3;
            }
 
        }
        return sumCost;
    }
 
}
 
class MobilePhoneInlandRule extends CallChargeRule {
 
    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (CallRecord call : userRecords.getCallingInLandRecords()) {
            double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (distanceS < 0) {
                continue;
            }
            double distanceM = (int) distanceS / 60;
            if (distanceS % 60 != 0) {
                distanceM += 1;
            }
            sumCost += distanceM * 0.6;
        }
        for (CallRecord call : userRecords.getAnswerInLandRecords()) {
            double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (distanceS < 0) {
                continue;
            }
            double distanceM = (int) distanceS / 60;
            if (distanceS % 60 != 0) {
                distanceM += 1;
            }
            sumCost += distanceM * 0.3;
        }
        return sumCost;
    }
 
}
 
class MobilePhoneInProvinceRule extends CallChargeRule {
 
    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
            double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (distanceS < 0) {
                continue;
            }
            double distanceM = (int) distanceS / 60;
            if (distanceS % 60 != 0) {
                distanceM += 1;
            }
            if (call.getCallType().equals("21")) {
                sumCost += distanceM * 0.3;
            } else if (call.getCallType().equals("22")) {
                sumCost += distanceM * 0.3;
            } else if (call.getCallType().equals("23")) {
                sumCost += distanceM * 0.3;
            }
        }
        return sumCost;
    }
 
}
 
class MessageRecord extends CommunicationRecord {
 
    private String message;
 
    public String getMessage() {
        return message;
    }
 
    public void setMessage(String message) {
        this.message = message;
    }
}
 
class User {
 
    private UserRecords userRecords = new UserRecords();
    private double balance = 100;
    private ChargeMode chargeMode;
    private String number;
 
    public double calCost() {
        return chargeMode.calCost(userRecords);
    }
 
    public double calBalance() {
        return balance - chargeMode.getMonthlyRent() - chargeMode.calCost(userRecords);
    }
 
    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;
    }
 
}
 
class Outputtool {
 
    @SuppressWarnings("deprecation")
    public void output(double out) {
//        java.text.DecimalFormat df=new java.text.DecimalFormat("#.##");
//        String a=df.format(out);
//        System.out.print(a);
        BigDecimal numb = new BigDecimal(out);
        out = numb.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.print(out);
    }
}
电信系列2

   该题首先要建立User类,里面包含了建户的基本信息,号码,计费类型等属性。接着还需要一个类可以接收信息,处理信息,并将其所读取到的信息进行分别存储到两个容器。然后将分类好的信息对应到每个用户之中,用UserRecord类来记录。通过对比输入信息中的号码与用户的开户号码,将通讯信息对应到每个用户下面,之后通过一系列计算将用户的电信费用展示出来。其中还需要我们创建新类,计算电话的时间差,以此来计算总费用,还要将ArrayList里的重复元素去掉。

 

 

题目3:

7-1 电信计费系列3-短信计费 分数 50 作者 蔡轲 单位 南昌航空大学

实现一个简单的电信计费程序,针对手机的短信采用如下计费方式:
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)。
 *********************************************************************************************************************************  分析:增添了MessageRecord类去记录短信,方便计算费用。相对于前面两道题,这一道题无疑简单了很多,类图清晰,也给了我们很多提示。但是不得不说一句,写起来还是遇到了很多困难,并且也花了很多时间去完成。
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.text.ParseException;
 
public class Main {
 
    public static void main(String[] args) {
 
        Outputtool outputtool = new Outputtool();
 
        Inputdeal inputdeal = new Inputdeal();
 
        ArrayList<User> users = new ArrayList<>();
 
        Scanner in = new Scanner(System.in);
 
        String input = in.nextLine();
 
        while (!input.equals("end")) {
            if (1 == inputdeal.check(input)) {
                inputdeal.writeUser(users, input);
            } else if (2 == inputdeal.check(input)) {
                inputdeal.writeRecord(users, input);
            }
            input = in.nextLine();
        }
 
        users.sort(new Comparator<User>() {
 
            @Override
            public int compare(User u1, User u2) {
                if (u1.getNumber().charAt(0) == '0' && u2.getNumber().charAt(0) != '0') {
                    return -1;
                } else if (u1.getNumber().charAt(0) != '0' && u2.getNumber().charAt(0) == '0') {
                    return 1;
                }
                if (Double.parseDouble(u1.getNumber()) > Double.parseDouble(u2.getNumber())) {
                    return 1;
                } else {
                    return -1;
                }
            }
        });
 
        for (User u : users) {
            System.out.print(u.getNumber() + " ");
            outputtool.output(u.calCost());
            System.out.print(" ");
            outputtool.output(u.calBalance());
            System.out.println();
 
        }
 
    }
 
}
 
abstract class ChargeMode {
    protected ArrayList<ChargeRule> chargeRules = new ArrayList<>();
 
    public abstract double calCost(UserRecords userRecords);
 
    public abstract double getMonthlyRent();
 
    public ArrayList<ChargeRule> getChargeRules() {
        return chargeRules;
    }
 
    public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
        this.chargeRules = chargeRules;
    }
}
 
class UserRecords {
 
    private ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();
    private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();
    private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();
 
    public void addCallingInCityRecords(CallRecord callRecord) {
        callingInCityRecords.add(callRecord);
    }
 
    public void addCallingInProvinceRecords(CallRecord callRecord) {
        callingInProvinceRecords.add(callRecord);
    }
 
    public void addCallingInLandRecords(CallRecord callRecord) {
        callingInLandRecords.add(callRecord);
    }
 
    public void addAnswerInCityRecords(CallRecord callRecord) {
        answerInCityRecords.add(callRecord);
    }
 
    public void aaddAnswerInProvinceRecords(CallRecord callRecord) {
        answerInProvinceRecords.add(callRecord);
    }
 
    public void addAnswerInLandRecords(CallRecord callRecord) {
        answerInLandRecords.add(callRecord);
    }
 
    public void addSendMessageRecords(MessageRecord callRecord) {
        sendMessageRecords.add(callRecord);
    }
 
    public void addReceiveMessageRecords(MessageRecord callRecord) {
        receiveMessageRecords.add(callRecord);
    }
 
    public ArrayList<CallRecord> getCallingInCityRecords() {
        return callingInCityRecords;
    }
 
    public void setCallingInCityRecords(ArrayList<CallRecord> callingInCityRecords) {
        this.callingInCityRecords = callingInCityRecords;
    }
 
    public ArrayList<CallRecord> getCallingInProvinceRecords() {
        return callingInProvinceRecords;
    }
 
    public void setCallingInProvinceRecords(ArrayList<CallRecord> callingInProvinceRecords) {
        this.callingInProvinceRecords = callingInProvinceRecords;
    }
 
    public ArrayList<CallRecord> getCallingInLandRecords() {
        return callingInLandRecords;
    }
 
    public void setCallingInLandRecords(ArrayList<CallRecord> callingInLandRecords) {
        this.callingInLandRecords = callingInLandRecords;
    }
 
    public ArrayList<CallRecord> getAnswerInCityRecords() {
        return answerInCityRecords;
    }
 
    public void setAnswerInCityRecords(ArrayList<CallRecord> answerInCityRecords) {
        this.answerInCityRecords = answerInCityRecords;
    }
 
    public ArrayList<CallRecord> getAnswerInProvinceRecords() {
        return answerInProvinceRecords;
    }
 
    public void setAnswerInProvinceRecords(ArrayList<CallRecord> answerInProvinceRecords) {
        this.answerInProvinceRecords = answerInProvinceRecords;
    }
 
    public ArrayList<CallRecord> getAnswerInLandRecords() {
        return answerInLandRecords;
    }
 
    public void setAnswerInLandRecords(ArrayList<CallRecord> answerInLandRecords) {
        this.answerInLandRecords = answerInLandRecords;
    }
 
    public ArrayList<MessageRecord> getSendMessageRecords() {
        return sendMessageRecords;
    }
 
    public void setSendMessageRecords(ArrayList<MessageRecord> sendMessageRecords) {
        this.sendMessageRecords = sendMessageRecords;
    }
 
    public ArrayList<MessageRecord> getReceiveMessageRecords() {
        return receiveMessageRecords;
    }
 
    public void setReceiveMessageRecords(ArrayList<MessageRecord> receiveMessageRecords) {
        this.receiveMessageRecords = receiveMessageRecords;
    }
 
}
 
class LandlinePhoneCharging extends ChargeMode {
 
    private double monthlyRent = 20;
 
    public LandlinePhoneCharging() {
        super();
        chargeRules.add(new LandPhoneInCityRule());
        chargeRules.add(new LandPhoneInProvinceRule());
        chargeRules.add(new LandPhoneInlandRule());
    }
 
    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (ChargeRule rule : chargeRules) {
            sumCost += rule.calCost(userRecords);
        }
        return sumCost;
    }
 
    @Override
    public double getMonthlyRent() {
        return monthlyRent;
    }
 
}
 
class MobilePhoneCharging extends ChargeMode {
 
    private double monthlyRent = 15;
 
    public MobilePhoneCharging() {
        super();
        chargeRules.add(new MobilePhoneInCityRule());
        chargeRules.add(new MobilePhoneInProvinceRule());
        chargeRules.add(new MobilePhoneInlandRule());
    }
 
    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (ChargeRule rule : chargeRules) {
            sumCost += rule.calCost(userRecords);
        }
        return sumCost;
    }
 
    @Override
    public double getMonthlyRent() {
        return monthlyRent;
    }
 
}
 
class MobilePhoneMassageCharging extends ChargeMode {
 
    private double monthlyRent = 0;
 
    public MobilePhoneMassageCharging() {
        super();
        chargeRules.add(new MobilePhoneMessageRule());
    }
 
    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (ChargeRule rule : chargeRules) {
            sumCost += rule.calCost(userRecords);
        }
        return sumCost;
    }
 
    @Override
    public double getMonthlyRent() {
        return monthlyRent;
    }
 
}
 
class Inputdeal {
 
    public int check(String input) {
        if (input.matches("[u]-0791[0-9]{7,8}\\s[0]") || input.matches("[u]-1[0-9]{10}\\s[13]")) {
            return 1;
        } else if (input.matches("[m]-1[0-9]{10}\\s" + "1[0-9]{10}\\s" + "[0-9a-zA-Z\\s\\.,]+")) {
            return 2;
        }
        return 0;
    }
 
    public void writeUser(ArrayList<User> users, String input) {
        User usernew = new User();
        String[] inputs = input.split(" ");
        String num = inputs[0].substring(2);
        for (User i : users) {
            if (i.getNumber().equals(num)) {
                return;
            }
        }
        usernew.setNumber(num);
        int mode = Integer.parseInt(inputs[1]);
        if (mode == 0) {
            usernew.setChargeMode(new LandlinePhoneCharging());
        } else if (mode == 1) {
            usernew.setChargeMode(new MobilePhoneCharging());
        } else if (mode == 3) {
            usernew.setChargeMode(new MobilePhoneMassageCharging());
        }
        users.add(usernew);
    }
 
    public void writeRecord(ArrayList<User> users, String input) {
        String[] inputs = input.split(" ");
        inputs[0] = inputs[0].substring(2);
 
        User callu = null, answeru = null;
 
        String out = inputs[0];
        String in = "";
        if (inputs.length == 6) {
            in = inputs[1];
        } else if (inputs.length == 7) {
            in = inputs[1];
        } else if (inputs.length == 8) {
            in = inputs[2];
        } else {
            in = inputs[1];
        }
 
        for (User i : users) {
            if (i.getNumber().equals(out)) {
                callu = i;
            }
            if (i.getNumber().equals(in)) {
                answeru = i;
            }
            if (callu != null && answeru != null) {
                break;
            }
        }
 
        if (input.charAt(0) == 'm') {
            MessageRecord messageRecord = new MessageRecord(input);
            if (callu != null) {
                callu.getUserRecords().addSendMessageRecords(messageRecord);
                ;
            }
            if (answeru != null) {
                callu.getUserRecords().addReceiveMessageRecords(messageRecord);
            }
        }
 
    }
 
}
 
abstract class CommunicationRecord {
    protected String callingNumber;
    protected String answerNumbe;
 
    public String getCallingNumber() {
        return callingNumber;
    }
 
    public void setCallingNumber(String callingNumber) {
        this.callingNumber = callingNumber;
    }
 
    public String getAnswerNumbe() {
        return answerNumbe;
    }
 
    public void setAnswerNumbe(String answerNumbe) {
        this.answerNumbe = answerNumbe;
    }
 
}
 
abstract class ChargeRule {
 
    abstract public double calCost(UserRecords userRecords);
 
}
 
class CallRecord extends CommunicationRecord {
    private Date startTime;
    private Date endTime;
    private String callingAddressAreaCode;
    private String answerAddressAreaCode;
 
    public String getCallType() {
        String type = "";
        if (callingAddressAreaCode.equals("0791")) {
            type = type.concat("1");
        } else if (callingAddressAreaCode.matches("^079[023456789]$") || callingAddressAreaCode.equals("0701")) {
            type = type.concat("2");
        } else {
            type = type.concat("3");
        }
 
        if (answerAddressAreaCode.equals("0791")) {
            type = type.concat("1");
        } else if (answerAddressAreaCode.matches("^079[023456789]$") || answerAddressAreaCode.equals("0701")) {
            type = type.concat("2");
        } else {
            type = type.concat("3");
        }
 
        return type;
    }
 
    public CallRecord(String[] inputs) {
        super();
 
        char type = inputs[0].charAt(0);
 
        String sd = null, st = null, ed = null, et = null;
 
        if (type == 't') {
            if (inputs.length == 6) {
                sd = inputs[2];
                st = inputs[3];
                ed = inputs[4];
                et = inputs[5];
                callingAddressAreaCode = inputs[0].substring(0, 4);
                answerAddressAreaCode = inputs[1].substring(0, 4);
            } else if (inputs.length == 7) {
                sd = inputs[3];
                st = inputs[4];
                ed = inputs[5];
                et = inputs[6];
                if (inputs[0].charAt(0) != '0') {
                    if (inputs[2].length() == 10) {
                        answerAddressAreaCode = inputs[2].substring(0, 3);
                    } else {
                        answerAddressAreaCode = inputs[2].substring(0, 4);
                    }
                    callingAddressAreaCode = inputs[1];
                } else {
                    if (inputs[0].length() == 10) {
                        callingAddressAreaCode = inputs[0].substring(0, 3);
                    } else {
                        callingAddressAreaCode = inputs[0].substring(0, 4);
                    }
                    answerAddressAreaCode = inputs[2];
                }
            } else if (inputs.length == 8) {
                sd = inputs[4];
                st = inputs[5];
                ed = inputs[6];
                et = inputs[7];
                callingAddressAreaCode = inputs[1];
                answerAddressAreaCode = inputs[3];
            }
        } else if (type == 'm') {
 
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault());
        try {
            startTime = simpleDateFormat.parse(sd + " " + st);
            endTime = simpleDateFormat.parse(ed + " " + et);
        } catch (ParseException e) {
        }
 
    }
 
    public CallRecord(Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) {
        super();
        this.startTime = startTime;
        this.endTime = endTime;
        this.callingAddressAreaCode = callingAddressAreaCode;
        this.answerAddressAreaCode = answerAddressAreaCode;
    }
 
    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 CallChargeRule extends ChargeRule {
 
}
 
class LandPhoneInCityRule extends CallChargeRule {
 
    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (CallRecord call : userRecords.getCallingInCityRecords()) {
            double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (distanceS < 0) {
                continue;
            }
            double distanceM = (int) distanceS / 60;
            if (distanceS % 60 != 0) {
                distanceM += 1;
            }
            if (call.getCallType().equals("11")) {
                sumCost += distanceM * 0.1;
            } else if (call.getCallType().equals("12")) {
                sumCost += distanceM * 0.3;
            } else if (call.getCallType().equals("13")) {
                sumCost += distanceM * 0.6;
            }
        }
        return sumCost;
    }
 
}
 
class LandPhoneInlandRule extends CallChargeRule {
 
    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (CallRecord call : userRecords.getCallingInLandRecords()) {
            double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (distanceS < 0) {
                continue;
            }
            double distanceM = (int) distanceS / 60;
            if (distanceS % 60 != 0) {
                distanceM += 1;
            }
            sumCost += distanceM * 0.6;
        }
        return sumCost;
    }
 
}
 
class LandPhoneInProvinceRule extends CallChargeRule {
 
    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
            double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (distanceS < 0) {
                continue;
            }
            double distanceM = (int) distanceS / 60;
            if (distanceS % 60 != 0) {
                distanceM += 1;
            }
            sumCost += distanceM * 0.3;
        }
        return sumCost;
    }
 
}
 
class MobilePhoneInCityRule extends CallChargeRule {
 
    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (CallRecord call : userRecords.getCallingInCityRecords()) {
            double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (distanceS < 0) {
                continue;
            }
            double distanceM = (int) distanceS / 60;
            if (distanceS % 60 != 0) {
                distanceM += 1;
            }
            if (call.getCallType().equals("11")) {
                sumCost += distanceM * 0.1;
            } else if (call.getCallType().equals("12")) {
                sumCost += distanceM * 0.2;
            } else if (call.getCallType().equals("13")) {
                sumCost += distanceM * 0.3;
            }
 
        }
        return sumCost;
    }
 
}
 
class MobilePhoneInlandRule extends CallChargeRule {
 
    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (CallRecord call : userRecords.getCallingInLandRecords()) {
            double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (distanceS < 0) {
                continue;
            }
            double distanceM = (int) distanceS / 60;
            if (distanceS % 60 != 0) {
                distanceM += 1;
            }
            sumCost += distanceM * 0.6;
        }
        for (CallRecord call : userRecords.getAnswerInLandRecords()) {
            double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (distanceS < 0) {
                continue;
            }
            double distanceM = (int) distanceS / 60;
            if (distanceS % 60 != 0) {
                distanceM += 1;
            }
            sumCost += distanceM * 0.3;
        }
        return sumCost;
    }
 
}
 
class MobilePhoneInProvinceRule extends CallChargeRule {
 
    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
            double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (distanceS < 0) {
                continue;
            }
            double distanceM = (int) distanceS / 60;
            if (distanceS % 60 != 0) {
                distanceM += 1;
            }
            if (call.getCallType().equals("21")) {
                sumCost += distanceM * 0.3;
            } else if (call.getCallType().equals("22")) {
                sumCost += distanceM * 0.3;
            } else if (call.getCallType().equals("23")) {
                sumCost += distanceM * 0.3;
            }
        }
        return sumCost;
    }
 
}
 
class MobilePhoneMessageRule extends CallChargeRule {
 
    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        int number = 0;
        for (MessageRecord m : userRecords.getSendMessageRecords()) {
            int length = m.getMessage().length();
            if (length <= 10) {
                number++;
            } else {
                number += length / 10;
                if (length % 10 != 0) {
                    number++;
                }
            }
        }
        if (number <= 3) {
            sumCost = number * 0.1;
        } else if (number <= 5) {
            sumCost = 0.3 + 0.2 * (number - 3);
        } else {
            sumCost = 0.7 + 0.3 * (number - 5);
        }
        return sumCost;
    }
 
}
 
class MessageRecord extends CommunicationRecord {
 
    private String message;
 
    public MessageRecord(String input) {
        super();
        this.message = input.substring(26);
    }
 
    public String getMessage() {
        return message;
    }
 
    public void setMessage(String message) {
        this.message = message;
    }
}
 
class User {
 
    private UserRecords userRecords = new UserRecords();
    private double balance = 100;
    private ChargeMode chargeMode;
    private String number;
 
    public double calCost() {
        return chargeMode.calCost(userRecords);
    }
 
    public double calBalance() {
        return balance - chargeMode.getMonthlyRent() - chargeMode.calCost(userRecords);
    }
 
    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;
    }
 
}
 
class Outputtool {
 
    @SuppressWarnings("deprecation")
    public void output(double out) {
//        java.text.DecimalFormat df=new java.text.DecimalFormat("#.##");
//        String a=df.format(out);
//        System.out.print(a);
        BigDecimal numb = new BigDecimal(out);
        out = numb.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.print(out);
    }
}
电信系列3

 

三、踩坑心得

1、在写给出类图题时,天真地以为给出的是完整的类图,没有进行思考就敲代码去了,到后面根本搞不清楚思路。所以说拿到题目应该把大致的方向找好。

2、关于类相关的知识点很多,由于对于类与类之间的种种关系掌握的并不好,所以在写代码时,整体的框架都很混乱,思路也不清晰。

3、这个系列题型用到了正则表达式,对于正则表达式的使用并不熟练,在这方面卡壳了好久。

4、在计算价钱时,由于未把时间转换成int型,导致后面出现误差,造成报错,这个小东西太磨人了,找了好久才发现这个问题。

 

四、代码改进建议 

1、很多知识点使用起来十分的不熟练,写得很繁琐,希望可以通过更多的学习提高自己的能力。

2、重新设计新的类,包括了新的数据存放数组和方法,节省代码空间。

3、多做继承应用之类的题目,不能第一时间反应出来总归到底是做的题目不够写的代码太少了。

4、对于正则表达式的运用不够熟练,对于Java的集中设计原则不会合理的运用,对于一些集合类的掌握程度不高,每次作业花费的时间较长,需要查阅大量的资料才能解决问题,作业的成功率不高,得到的分数不高。解决方法:遇到不懂的知识点上网查阅资料,请教他人,多看书记住一些基本的知识点。

 

五、总结

  通过本学期的PTA作业学会了正则表达式的使用。正则表达式是一种可以用于模式匹配和替换的强有力的工具,可以看成是多个字符的组成规则。了解到Java中存放对象的三种类型的集合:Set(集)List(表)Map(映射)。Set是最简单的集合,集合中的对象排序是没有特定方式的,并且没有重复的对象,Set的接口的两个主要实现类是:HashSet和TreeSet。HashSet:按照哈希算法来存取集合中的对象,HashSet会调用对象的hashCode()方法获得哈希码,从而算出对象在集合中的位置。TreeSet:采用树结构来存储数据,每一个元素都是一个节点,一层一层的依次排列。List以线性方式存储元素,允许集合中存放重复元素。List接口的主要实现来是ArrayList和LinkedList。Map:集合中的每一个元素包含一对键对象和值对象,集合中的键对象不能重复,值对象可以重复。HashMap按照哈希算法来存取键值对象。Java中的集合类能够使程序方便的存储和操纵数目不固定的一组数据。

  面向对象编程不能注重于按步骤一步一步解决问题,而应该注重于将问题中的部分分成各个对象,各司其职,谁做了什么事,谁又做了什么事,他们共同作用的结果解决了一件事。可以说,面向对象编程更接近现实世界。

 

标签:inputs,return,String,nchu,double,Blog3,public,oop,ArrayList
From: https://www.cnblogs.com/www-hhh/p/16960297.html

相关文章