third

  1. 电信计费系列1-座机计费

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

输入格式:

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

输出格式:

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

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

建议类图:
参见图1、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
 

其余参考样例详见附件,未尽事宜以附件样例为准:

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

设计与分析:

1.主类

public class Main {

    public static void main(String[] args) throws ParseException {

    	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        Scanner sc = new Scanner(System.in);


        ArrayList<String> list = new ArrayList<String>();
        String line = null ;
        while(!"end".equals(line = sc.nextLine())){
            list.add(line);
        }
        ArrayList<CallRecord> callRecordArrayList = new ArrayList<>();

                for (String next : list) {
        String[] s1 = next.split(" ");
            String zhujiaohaoma = s1[0];
            String jietinghaoma = s1[1];
            String startTime = s1[2] + " " + s1[3];
            String endTime = s1[4] + " " + s1[5];


            Date startTime1 = dateFormat.parse(startTime);
            Date endTime1 = dateFormat.parse(startTime);
            CallRecord callRecord = new CallRecord();
            callRecord.setStartTime(startTime1);
            callRecord.setEndTime(endTime1);
            callRecord.setCallingNumber(zhujiaohaoma);
            callRecord.setAnswerNumber(jietinghaoma);

            callRecordArrayList.add(callRecord);
        }
        UserRecords userRecords = new UserRecords();
        userRecords.addAnswerInCityRecords(callRecordArrayList);

        // TODO
    }
		String s = sc.nextLine();
		zifuchuan.dasbduwac(s);
}
abstract class ChargeRule {

}

abstract class CallChargeRule extends ChargeRule {

    double calCost(List<CallRecord> callRecords) {
        return 0;
    }

}

  

电话费1 市

class LandPhoneInCityRule extends CallChargeRule{

    @Override
    double calCost(List<CallRecord> callRecords) {
        double sum = 0;
        for (CallRecord callRecord : callRecords) {
            int seconds = (int)((callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime())/1000);
            int min = (int)Math.ceil(seconds * 1.0 / 60);
            sum += min * 0.1;
        }
        return sum;
    }
}

  

电话费2  国

class LandPhoneInlandRule extends CallChargeRule{

    @Override
    double calCost(List<CallRecord> callRecords) {
        double sum = 0;
        for (CallRecord callRecord : callRecords) {
            int seconds = (int)((callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime())/1000);
            int min = (int)Math.ceil(seconds * 1.0 / 60);
            sum += min * 0.6;
        }
        return sum;
    }
}

  

电话费3  省

class LandPhoneInProvinceRule extends CallChargeRule{

    @Override
    double calCost(List<CallRecord> callRecords) {
        double sum = 0;
        for (CallRecord callRecord : callRecords) {
            int seconds = (int)((callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime())/1000);
            int min = (int)Math.ceil(seconds * 1.0 / 60);
            sum += min * 0.3;
        }
        return sum;
    }
}

  

 

电话号码  接  打

abstract class CommunicationRecord {
    private String callingNumber; 
    private String answerNumber; 
    public String getCallingNumber() {
        return callingNumber;
    }
    public void setCallingNumber(String callingNumber) {
        this.callingNumber = callingNumber;
    }
 
   public String getAnswerNumber() {
        return answerNumber;
    }
    public void setAnswerNumber(String answerNumber) {
        this.answerNumber = answerNumber;
    }
}

  

计时⏲

class CallRecord extends CommunicationRecord {

    private Date startTime;
    private Date endTime;

    private String callingAddressAreaCode;
    private String 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 ChargeMode {
    private List<ChargeRule> chargeRules = new ArrayList<>();

    public double calCost(UserRecords userRecords) {

        return 0;
    }
    public double getMonthlyRent() {

        return 0;
    }

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

    public void setChargeRules(List<ChargeRule> chargeRules) {
        this.chargeRules = chargeRules;
    }
}

 月租

class LandlinePhoneCharging extends ChargeMode {

    private double monthlyRent = 20;

    @Override
    public double calCost(UserRecords userRecords) {
        return super.calCost(userRecords);
    }

    @Override
    public double getMonthlyRent() {
        return this.monthlyRent;
    }
}

 用户

class User {

    private UserRecords userRecords = new UserRecords();
    private double balance = 100d;
    private ChargeMode chargeMode;
    private String number;

    public double calBalance() {

        return this.balance;
    }

    public double calCost() {

        return 0;
    }

    public UserRecords getUserRecords() {
        return userRecords;
    }

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

    public double getBalance() {
        return balance;
    }

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

    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 UserRecords {
    
    private List<CallRecord> callingInCityRecords;
    private List<CallRecord> callingInProvinceRecords;
    private List<CallRecord> callingInLandRecords;
    private List<CallRecord> answerInCityRecords;
    private List<CallRecord> answerInProvinceRecords;
    private List<CallRecord> answerInLandRecords;
    
    private List<MessageRecord> sendMessageRecords;
    private List<MessageRecord> receiveMessageRecords;

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

    public void addCallingInCityRecords(List<CallRecord> callingInCityRecords) {
        this.callingInCityRecords = callingInCityRecords;
    }

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

    public void addCallingInProvinceRecords(List<CallRecord> callingInProvinceRecords) {
        this.callingInProvinceRecords = callingInProvinceRecords;
    }

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

    public void addCallingInLandRecords(List<CallRecord> callingInLandRecords) {
        this.callingInLandRecords = callingInLandRecords;
    }

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

    public void addAnswerInCityRecords(List<CallRecord> answerInCityRecords) {
        this.answerInCityRecords = answerInCityRecords;
    }

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

    public void addAnswerInProvinceRecords(List<CallRecord> answerInProvinceRecords) {
        this.answerInProvinceRecords = answerInProvinceRecords;
    }

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

    public void addAnswerInLandRecords(List<CallRecord> answerInLandRecords) {
        this.answerInLandRecords = answerInLandRecords;
    }

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

    public void addSendMessageRecords(List<MessageRecord> sendMessageRecords) {
        this.sendMessageRecords = sendMessageRecords;
    }

    public List<MessageRecord> getReceiveMessageRecords() {
        return receiveMessageRecords;
    }

    public void addReceiveMessageRecords(List<MessageRecord> receiveMessageRecords) {
        this.receiveMessageRecords = receiveMessageRecords;
    }
}

2.主类

public class Main {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        HashSet<User> alluserArrayList = new HashSet<>();
        ArrayList<CommunicationRecord> allcommunicationRecords = new ArrayList<>();
        String Line;
        do {
            Line = in.nextLine();
            if (Line.equals("")) continue;
            if (Line.charAt(0) == 'u') {
                alluserArrayList.add(LineToUser(Line));
            } else if (Line.charAt(0) == 't') {
                allcommunicationRecords.add(LineToCallRecord(Line));
            }
        } while (!"end".equals(Line));
        List<User> res = new ArrayList<>(alluserArrayList);
        Collections.sort(res);
        for(User u : res){
            double cost = 0;
            for (CommunicationRecord cRecord : allcommunicationRecords){
                if (isZhujiao(u,cRecord)){
                    if (u.getChargeMode() instanceof LandlinePhoneCharging){
                        //zuoji da
                        if (ChargeRule.InCity(((CallRecord)cRecord).getAnswerAddressAreaCode())){
                            long tm = CommunicationRecord.CalTimeCha(((CallRecord)cRecord).getStartTime(),((CallRecord)cRecord).getEndTime());
                            long min = tm/60,other = tm%60;
                            min +=other!=0?1:0;
                            cost += min*0.1;
                        }else if(ChargeRule.InProvince(((CallRecord)cRecord).getAnswerAddressAreaCode())){
                            long tm = CommunicationRecord.CalTimeCha(((CallRecord)cRecord).getStartTime(),((CallRecord)cRecord).getEndTime());
                            long min = tm/60,other = tm%60;
                            min +=other!=0?1:0;
                            cost += min*0.3;
                        }else if (ChargeRule.InLand(((CallRecord)cRecord).getAnswerAddressAreaCode())){
                            long tm = CommunicationRecord.CalTimeCha(((CallRecord)cRecord).getStartTime(),((CallRecord)cRecord).getEndTime());
                            long min = tm/60,other = tm%60;
                            min +=other!=0?1:0;
                            cost += min*0.6;
                        }
                    } else if(u.getChargeMode() instanceof PhoneCharging){
                        // shouji da
                        if (ChargeRule.InCity(((CallRecord)cRecord).getCallingAddressAreaCode())){
                            if (ChargeRule.InCity(cRecord.getAnswerNumber().substring(0,4))){
                                long tm = CommunicationRecord.CalTimeCha(((CallRecord)cRecord).getStartTime(),((CallRecord)cRecord).getEndTime());
                                long min = tm/60,other = tm%60;
                                min +=other!=0?1:0;
                                cost += min*0.1;
                            }else if (ChargeRule.InProvince(((CallRecord)cRecord).getAnswerAddressAreaCode())){
                                long tm = CommunicationRecord.CalTimeCha(((CallRecord)cRecord).getStartTime(),((CallRecord)cRecord).getEndTime());
                                long min = tm/60,other = tm%60;
                                min +=other!=0?1:0;
                                cost += min*0.2;
                            }else if (ChargeRule.InLand(((CallRecord)cRecord).getAnswerAddressAreaCode())){
                                long tm = CommunicationRecord.CalTimeCha(((CallRecord)cRecord).getStartTime(),((CallRecord)cRecord).getEndTime());
                                long min = tm/60,other = tm%60;
                                min +=other!=0?1:0;
                                cost += min*0.3;
                            }
                        }else if(ChargeRule.InProvince(((CallRecord)cRecord).getCallingAddressAreaCode())){
                            long tm = CommunicationRecord.CalTimeCha(((CallRecord)cRecord).getStartTime(),((CallRecord)cRecord).getEndTime());
                            long min = tm/60,other = tm%60;
                            min +=other!=0?1:0;
                            cost += min*0.3;
                        }else if (ChargeRule.InLand(((CallRecord)cRecord).getCallingAddressAreaCode())){
                            long tm = CommunicationRecord.CalTimeCha(((CallRecord)cRecord).getStartTime(),((CallRecord)cRecord).getEndTime());
                            long min = tm/60,other = tm%60;
                            min +=other!=0?1:0;
                            cost += min*0.6;
                        }
                    }
                }
//                else
                if (isJieTing(u,cRecord)){
                    if (u.getChargeMode() instanceof LandlinePhoneCharging){
                        //zuoji jie free
                    } else if(u.getChargeMode() instanceof PhoneCharging){
                        // shouji jie
                        if (ChargeRule.InLand(((CallRecord)cRecord).getAnswerAddressAreaCode())){
                            long tm = CommunicationRecord.CalTimeCha(((CallRecord)cRecord).getStartTime(),((CallRecord)cRecord).getEndTime());
                            long min = tm/60,other = tm%60;
                            min +=other!=0?1:0;
                            cost += min*0.3;
                        }
                    }
                }
            }
            System.out.printf("%s %.1f %.1f\n",u.getNumber(),cost,u.getBalance()-u.getChargeMode().getMonthlyRent()-cost);
        }
    }

 主叫号码

    private static boolean isZhujiao(User u , CommunicationRecord communicationRecord){
        return communicationRecord.getCallingNumber().equals(u.getNumber());
    }
    private static boolean isJieTing(User u , CommunicationRecord communicationRecord){
        return communicationRecord.getAnswerNumber().equals(u.getNumber());
    }
    private static User LineToUser(String str){
        str = str.substring(2);
        String[] data = str.split(" ");
        return new User(data[0],Integer.parseInt(data[1]));
    }
   private static CallRecord LineToCallRecord(String str){
        str = str.substring(2);
        String[] data = str.split(" ");
        int blk = BlockTimeFun(str);
        int tmBlk = BlockID(str,blk-3);

        String Time1,Time2;
        Time1 = str.substring(tmBlk,BlockID(str,blk-1));
        Time2 = str.substring(BlockID(str,blk-1));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        simpleDateFormat.setLenient(false);
        Date date1, date2;
        try {
            date1 = simpleDateFormat.parse(Time1);
            date2 = simpleDateFormat.parse(Time2);
        } catch (ParseException e) {
            return null;
        }
        if (data.length==2+4){
            return new CallRecord(data[0],data[1],date1,date2);
        }else if(data.length==4+4){
            return new CallRecord(data[0],data[1],data[2],data[3],date1,date2);
        }else if (data.length==3+4){
            CallRecord t =  new CallRecord();
            if (isNumb(data[1])){
                t.setCallingNumber(data[0]);
                t.setAnswerNumber(data[1]);
                t.setCallingAddressAreaCode(data[0].substring(0,4));
                t.setAnswerAddressAreaCode(data[2]);
                t.setStartTime(date1);
                t.setEndTime(date2);
                return t;
            }else {
                t.setCallingNumber(data[0]);
                t.setCallingAddressAreaCode(data[1]);
                t.setAnswerNumber(data[2]);
                t.setAnswerAddressAreaCode(data[2].substring(0,4));
                t.setStartTime(date1);
                t.setEndTime(date2);
                return t;
            }
        }
        return null;
    }

private static boolean isNumb(String s){
return s.length()>4;
}
private static boolean isAreaNum(String s){
return s.length()<=4;
}
public static int BlockTimeFun(String s){
int t = 0;
for (int i=0;i<s.length();i++){
if (s.charAt(i)==' ')t++;
}
return t;
}
private static int BlockID(String str, int cnt){
for (int i = 0, t = 0; i < str.length(); i++) {
if (t == cnt) return i;
if (str.charAt(i) == ' ') t++;
}
return str.length();
}
private static void handle1() {

}
private static void handle2() {

}

 

 支付方式

abstract class ChargeRule {
    public static boolean InCity(String callingAddressAreaCode) {
        return "0791".equals(callingAddressAreaCode);
    }
    public static boolean InProvince(String callingAddressAreaCode) {
        if((InCity(callingAddressAreaCode))||(callingAddressAreaCode.length()<3))return false;
        if("0791".equals(callingAddressAreaCode))return true;

        return "079".equals(callingAddressAreaCode.substring(0, 3))&&(callingAddressAreaCode.charAt(3)>='0'&&callingAddressAreaCode.charAt(3)<='9');
    }
    public static boolean InLand(String callingAddressAreaCode){
        if (InCity(callingAddressAreaCode))return false;
        return !InProvince(callingAddressAreaCode);
    }

}
abstract class CallChargeRule extends ChargeRule {

    double calCost(List<CallRecord> callRecords) {
        return 0;
    }

}

 城市

class LandPhoneInCityRule extends CallChargeRule{

    @Override
    double calCost(List<CallRecord> callRecords) {
        double sum = 0;
        for (CallRecord callRecord : callRecords) {
            int seconds = (int)((callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime())/1000);
            int min = (int)Math.ceil(seconds * 1.0 / 60);
            sum += min * 0.1;
        }
        return sum;
    }
}

 国家

class LandPhoneInlandRule extends CallChargeRule{

    @Override
    double calCost(List<CallRecord> callRecords) {
        double sum = 0;
        for (CallRecord callRecord : callRecords) {
            int seconds = (int)((callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime())/1000);
            int min = (int)Math.ceil(seconds * 1.0 / 60);
            sum += min * 0.6;
        }
        return sum;
    }
}

省内

class LandPhoneInProvinceRule extends CallChargeRule{

    @Override
    double calCost(List<CallRecord> callRecords) {
        double sum = 0;
        for (CallRecord callRecord : callRecords) {
            int seconds = (int)((callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime())/1000);
            int min = (int)Math.ceil(seconds * 1.0 / 60);
            sum += min * 0.3;
        }
        return sum;
    }
}
abstract class CommunicationRecord {
    private String callingNumber;
    private String answerNumber;
    public CommunicationRecord() {
    }
    public CommunicationRecord(String callingNumber, String answerNumber) {
        super();
        this.callingNumber = callingNumber;
        this.answerNumber = answerNumber;
    }

    public String getCallingNumber() {
        return callingNumber;
    }

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

    public String getAnswerNumber() {
        return answerNumber;
    }

    public void setAnswerNumber(String answerNumber) {
        this.answerNumber = answerNumber;
    }
    public static long CalTimeCha(Date a, Date b){
        return  (b.getTime() - a.getTime()) /1000;
    }
}
class CallRecord extends CommunicationRecord {

    private Date startTime;
    private Date endTime;
    private String callingAddressAreaCode;
    private String answerAddressAreaCode;

    public CallRecord() {
    }

    //zuoji to zuoji
    public CallRecord(String callingNumber, String answerNumber, Date startTime, Date endTime) {
        super(callingNumber, answerNumber);
        this.startTime = startTime;
        this.endTime = endTime;
        callingAddressAreaCode=callingNumber.substring(0,4);
        answerAddressAreaCode=answerNumber.substring(0,4);
    }

    //phone to phone
    public CallRecord(String callingNumber, String callingAddressAreaCode, String answerNumber, String answerAddressAreaCode, Date startTime, Date endTime) {
        super(callingNumber, answerNumber);
        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 MessageRecord(String callingNumber, String answerNumber, String message) {
        super(callingNumber, answerNumber);
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

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

    private double monthlyRent = 20;


    private List<ChargeRule> chargeRules = new ArrayList<>();

    public double calCost(UserRecords userRecords) {
        double sum = 0;
        chargeRules.add(new LandPhoneInCityRule());
        chargeRules.add(new LandPhoneInProvinceRule());
        chargeRules.add(new LandPhoneInlandRule());
        LandPhoneInCityRule city = (LandPhoneInCityRule) chargeRules.get(0);
        LandPhoneInProvinceRule province = (LandPhoneInProvinceRule) chargeRules.get(1);
        LandPhoneInlandRule land = (LandPhoneInlandRule) chargeRules.get(2);
        sum = sum + city.calCost(userRecords.getCallingInCityRecords())
                + province.calCost(userRecords.getCallingInProvinceRecords())
                + land.calCost(userRecords.getCallingInLandRecords());
        return sum;
    }

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

    public void setChargeRules(List<ChargeRule> chargeRules) {
        this.chargeRules = chargeRules;
    }

    public double getMonthlyRent() {
        return monthlyRent;
    }
}

  月租

class ChargeMode0 extends ChargeMode {
    private double monthlyRent = 20;

    @Override
    public double calCost(UserRecords userRecords) {
        // TODO Auto-generated method stub
        return super.calCost(userRecords);
    }

    @Override
    public double getMonthlyRent() {
        // TODO Auto-generated method stub
        return monthlyRent;
    }

}
class ChargeMode1 extends ChargeMode {
    private double monthlyRent = 15;
    @Override
    public double calCost(UserRecords userRecords) {
        // TODO Auto-generated method stub
        return super.calCost(userRecords);
    }

    @Override
    public double getMonthlyRent() {
        // TODO Auto-generated method stub
        return super.getMonthlyRent();
    }

}
class PhoneCharging extends ChargeMode {
    private double monthlyRent = 15;

    public double getMonthlyRent() {
        return monthlyRent;
    }
}
class LandlinePhoneCharging extends ChargeMode {
    private double monthlyRent = 20;

    public double calCost(UserRecords userRecords) {
        double cost = 0;
        ArrayList<CallRecord> callInCityRecords = (ArrayList<CallRecord>) userRecords.getCallingInCityRecords();
        ArrayList<CallRecord> callInProvinceRecords = (ArrayList<CallRecord>) userRecords.getCallingInProvinceRecords();
        ArrayList<CallRecord> callInLandRecords = (ArrayList<CallRecord>) userRecords.getCallingInLandRecords();

        CallChargeRule landPhoneInCityRule = new LandPhoneInCityRule();
        CallChargeRule landPhoneInProvinceRule = new LandPhoneInProvinceRule();
        CallChargeRule landPhoneInLandRule = new LandPhoneInlandRule();

        cost += landPhoneInCityRule.calCost(callInCityRecords);
        cost += landPhoneInProvinceRule.calCost(callInProvinceRecords);
        cost += landPhoneInLandRule.calCost(callInLandRecords);
        return cost;
    }

    public double getMonthlyRent() {
        return monthlyRent;
    }
}

  用户

class User implements Comparable<User>{

    private UserRecords userRecords = new UserRecords();
    private double balance = 100;
    private ChargeMode chargeMode;
    private String number;

    public User(String number, int mod) {
        this.number = number;
        switch (mod) {
            case 0: {
                chargeMode = new LandlinePhoneCharging();
                break;
            }
            case 1: {
                chargeMode = new PhoneCharging();
            }
            case 2: {

            }
            default:
        }
    }

  花费

    public double calCost() {

        return chargeMode.calCost(userRecords);
    }

    public UserRecords getUserRecords() {
        return userRecords;
    }

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

    public double getBalance() {
        return balance;
    }

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

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

    @Override
    public int hashCode() {
        return 123;
    }

    @Override
    public boolean equals(Object obj) {
        return getNumber().equals(((User)obj).getNumber());
    }

    @Override
    public int compareTo(User o) {
        return getNumber().compareTo(o.getNumber());
//        return Integer.compare(Integer.parseInt(getNumber()),Integer.parseInt(o.getNumber()));

    }
}

  用户

class UserRecords {

    private List<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();
    private List<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();
    private List<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();
    private List<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();
    private List<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();
    private List<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();

    private List<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();
    private List<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();
    private CallRecord callRecord;

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

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

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

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

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

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

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

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

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

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

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

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

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

    public void addSendMessageRecords(List<MessageRecord> sendMessageRecords) {
        this.sendMessageRecords = sendMessageRecords;
    }

    public List<MessageRecord> getReceiveMessageRecords() {
        return receiveMessageRecords;
    }

    public void addReceiveMessageRecords(List<MessageRecord> receiveMessageRecords) {
        this.receiveMessageRecords = receiveMessageRecords;
    }
}

  

 

踩坑心得:

  1. 我在提交源码的过程中,遇到了很多不同的问题。在第一次作业中,虽然题目比较简单,但是很多琐碎的测试点和一些不存在的情况都有出现问题,要改了再改,不仅可能不能过一个测试点,甚至可能推翻别的测试点。
  2. 经常需要自己一遍又一遍的测试,或者是问一问已经过了测试点的同学。例如,对于一个时间的定义。
  3. 第二次作业中,对于ascll需要引入,需要经常查资料,写完了还是很多点过不去,分数越改越少。
  4. 第三次做作业,整体难度偏高,需要引入正则表达式,对于我这种基础不好的同学来讲很难上手,所以我应该致力于攻克测试点。作业没过的测试点还有很多,希望有机会可以重新尝试。
  5. 有的题目测试点有问题,正确的代码无法通过测试。
  6. 有些题目内容不够详细,需要提示。

改进建议:

  1. 基础不好应该先进行复习,做题时与资料结合,进行知识的巩固与加强。
  2. 写代码的时候进行注释,方便自己的后续修改以及同学们的帮助。
  3. 进行简易测试点的判断,把自己尽力能得到的分得到,例如,一些输入错误的测试点相对来说最容易。
  4. 通过习题学习知识点,使得自己记忆更加深刻。
  5. 对于类的掌握不够透彻,还需要加量学习,以便自己后续程序的书写
  6. 输入正确错误的格式掌握不够透彻,需要进一步学习使用Java语言对格式进行判断。例如第三次作业,基本上每一次都要进行格式的判断。
  7. 题目结果总是出现0,0的情况,还需要进一步了解此情况出现的原因。

总结:

  1. 在作业过程中,我能看出难度跨度较大,也就说明了我们永远不会知道以后会面对多么困难的问题,必须要用尽可能多的时间提升自己。
  2. 在这作业中,我发现有很多考点是自己上课并未注意到的,所以做题的过程中,可以更加清楚的意识到自己学习的重点内容是哪一部分。
  3. 在三次作业过程中,我也拓宽了自己的视野,学会借助外部资料来解决自己的某一个针对性问题,合理使用手中的资源。
  4. 由于第三次作业写得匆忙,我更加意识到时间的宝贵,意识到作业之所以给一整周的时间是为了让我们时时刻刻可以修改自己的错误,从作业发布就要及时下手。
  5. 遇到问题要积极求助,虚心学习,如果问题不能自己解决就要学习,身边的同学和老师都会帮助你。
  6. 由于作业的需求,我们迫不得已学习了ascll语言以及正则表达式等等,但这是一件对于我们来说很有利的事情。
 
posted @ 2022-06-09 21:27  酥乐Lee  阅读(196)  评论(0)    收藏  举报