• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

电信计费系列2-手机+座机计费

武飞扬头像
哦豁果奶
帮助1

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、逐行输入南昌市用户开户的信息,每行一个用户,含手机和座机用户
格式: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:

学新通


图1

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

学新通

图2

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

学新通


图3

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

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

输入样例:

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

  1.  
    u-13811111111 1
  2.  
    t-13811111111 0791 13811111110 020 2022.1.3 08:00:00 2022.1.3 08:09:20
  3.  
    end

输出样例:

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

13811111111 3.0 82.0

更多内容详见附件:

电信计费简化版-座机计费说明.pdf

代码长度限制

40 KB

时间限制

400 ms

内存限制

64 MB

我滴答案:

  1.  
    // package integration;
  2.  
     
  3.  
    import java.util.ArrayList;
  4.  
    import java.util.Comparator;
  5.  
    import java.util.Scanner;
  6.  
    import java.util.regex.Matcher;
  7.  
    import java.util.regex.Pattern;
  8.  
    import java.math.BigDecimal;
  9.  
    import java.text.SimpleDateFormat;
  10.  
    import java.util.Date;
  11.  
    import java.util.Locale;
  12.  
    import java.text.ParseException;
  13.  
     
  14.  
    public class Main {
  15.  
     
  16.  
    public static void main(String[] args) {
  17.  
     
  18.  
    Outputtool outputtool = new Outputtool();
  19.  
     
  20.  
    Inputdeal inputdeal = new Inputdeal();
  21.  
     
  22.  
    ArrayList<User> users = new ArrayList<>();
  23.  
     
  24.  
    Scanner in = new Scanner(System.in);
  25.  
     
  26.  
    String input = in.nextLine();
  27.  
     
  28.  
    while (!input.equals("end")) {
  29.  
    if (1 == inputdeal.check(input)) {
  30.  
    inputdeal.writeUser(users, input);
  31.  
    } else if (2 == inputdeal.check(input)) {
  32.  
    inputdeal.writeRecord(users, input);
  33.  
    }
  34.  
    input = in.nextLine();
  35.  
    }
  36.  
     
  37.  
    users.sort(new Comparator<User>() {
  38.  
     
  39.  
    @Override
  40.  
    public int compare(User u1, User u2) {
  41.  
    if (u1.getNumber().charAt(0) == '0' && u2.getNumber().charAt(0) != '0') {
  42.  
    return -1;
  43.  
    } else if (u1.getNumber().charAt(0) != '0' && u2.getNumber().charAt(0) == '0') {
  44.  
    return 1;
  45.  
    }
  46.  
    if (Double.parseDouble(u1.getNumber()) > Double.parseDouble(u2.getNumber())) {
  47.  
    return 1;
  48.  
    } else {
  49.  
    return -1;
  50.  
    }
  51.  
    }
  52.  
    });
  53.  
     
  54.  
    for (User u : users) {
  55.  
    System.out.print(u.getNumber() " ");
  56.  
    outputtool.output(u.calCost());
  57.  
    System.out.print(" ");
  58.  
    outputtool.output(u.calBalance());
  59.  
    System.out.println();
  60.  
     
  61.  
    }
  62.  
     
  63.  
    }
  64.  
     
  65.  
    }
  66.  
     
  67.  
    abstract class ChargeMode {
  68.  
    protected ArrayList<ChargeRule> chargeRules = new ArrayList<>();
  69.  
     
  70.  
    public abstract double calCost(UserRecords userRecords);
  71.  
     
  72.  
    public abstract double getMonthlyRent();
  73.  
     
  74.  
    public ArrayList<ChargeRule> getChargeRules() {
  75.  
    return chargeRules;
  76.  
    }
  77.  
     
  78.  
    public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
  79.  
    this.chargeRules = chargeRules;
  80.  
    }
  81.  
    }
  82.  
     
  83.  
    class UserRecords {
  84.  
     
  85.  
    private ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();
  86.  
    private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();
  87.  
    private ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();
  88.  
    private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();
  89.  
    private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();
  90.  
    private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();
  91.  
    private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();
  92.  
    private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();
  93.  
     
  94.  
    public void addCallingInCityRecords(CallRecord callRecord) {
  95.  
    callingInCityRecords.add(callRecord);
  96.  
    }
  97.  
     
  98.  
    public void addCallingInProvinceRecords(CallRecord callRecord) {
  99.  
    callingInProvinceRecords.add(callRecord);
  100.  
    }
  101.  
     
  102.  
    public void addCallingInLandRecords(CallRecord callRecord) {
  103.  
    callingInLandRecords.add(callRecord);
  104.  
    }
  105.  
     
  106.  
    public void addAnswerInCityRecords(CallRecord callRecord) {
  107.  
    answerInCityRecords.add(callRecord);
  108.  
    }
  109.  
     
  110.  
    public void aaddAnswerInProvinceRecords(CallRecord callRecord) {
  111.  
    answerInProvinceRecords.add(callRecord);
  112.  
    }
  113.  
     
  114.  
    public void addAnswerInLandRecords(CallRecord callRecord) {
  115.  
    answerInLandRecords.add(callRecord);
  116.  
    }
  117.  
     
  118.  
    public void addSendMessageRecords(MessageRecord callRecord) {
  119.  
    sendMessageRecords.add(callRecord);
  120.  
    }
  121.  
     
  122.  
    public void addReceiveMessageRecords(MessageRecord callRecord) {
  123.  
    receiveMessageRecords.add(callRecord);
  124.  
    }
  125.  
     
  126.  
    public ArrayList<CallRecord> getCallingInCityRecords() {
  127.  
    return callingInCityRecords;
  128.  
    }
  129.  
     
  130.  
    public void setCallingInCityRecords(ArrayList<CallRecord> callingInCityRecords) {
  131.  
    this.callingInCityRecords = callingInCityRecords;
  132.  
    }
  133.  
     
  134.  
    public ArrayList<CallRecord> getCallingInProvinceRecords() {
  135.  
    return callingInProvinceRecords;
  136.  
    }
  137.  
     
  138.  
    public void setCallingInProvinceRecords(ArrayList<CallRecord> callingInProvinceRecords) {
  139.  
    this.callingInProvinceRecords = callingInProvinceRecords;
  140.  
    }
  141.  
     
  142.  
    public ArrayList<CallRecord> getCallingInLandRecords() {
  143.  
    return callingInLandRecords;
  144.  
    }
  145.  
     
  146.  
    public void setCallingInLandRecords(ArrayList<CallRecord> callingInLandRecords) {
  147.  
    this.callingInLandRecords = callingInLandRecords;
  148.  
    }
  149.  
     
  150.  
    public ArrayList<CallRecord> getAnswerInCityRecords() {
  151.  
    return answerInCityRecords;
  152.  
    }
  153.  
     
  154.  
    public void setAnswerInCityRecords(ArrayList<CallRecord> answerInCityRecords) {
  155.  
    this.answerInCityRecords = answerInCityRecords;
  156.  
    }
  157.  
     
  158.  
    public ArrayList<CallRecord> getAnswerInProvinceRecords() {
  159.  
    return answerInProvinceRecords;
  160.  
    }
  161.  
     
  162.  
    public void setAnswerInProvinceRecords(ArrayList<CallRecord> answerInProvinceRecords) {
  163.  
    this.answerInProvinceRecords = answerInProvinceRecords;
  164.  
    }
  165.  
     
  166.  
    public ArrayList<CallRecord> getAnswerInLandRecords() {
  167.  
    return answerInLandRecords;
  168.  
    }
  169.  
     
  170.  
    public void setAnswerInLandRecords(ArrayList<CallRecord> answerInLandRecords) {
  171.  
    this.answerInLandRecords = answerInLandRecords;
  172.  
    }
  173.  
     
  174.  
    public ArrayList<MessageRecord> getSendMessageRecords() {
  175.  
    return sendMessageRecords;
  176.  
    }
  177.  
     
  178.  
    public void setSendMessageRecords(ArrayList<MessageRecord> sendMessageRecords) {
  179.  
    this.sendMessageRecords = sendMessageRecords;
  180.  
    }
  181.  
     
  182.  
    public ArrayList<MessageRecord> getReceiveMessageRecords() {
  183.  
    return receiveMessageRecords;
  184.  
    }
  185.  
     
  186.  
    public void setReceiveMessageRecords(ArrayList<MessageRecord> receiveMessageRecords) {
  187.  
    this.receiveMessageRecords = receiveMessageRecords;
  188.  
    }
  189.  
     
  190.  
    }
  191.  
     
  192.  
    class LandlinePhoneCharging extends ChargeMode {
  193.  
     
  194.  
    private double monthlyRent = 20;
  195.  
     
  196.  
    public LandlinePhoneCharging() {
  197.  
    super();
  198.  
    chargeRules.add(new LandPhoneInCityRule());
  199.  
    chargeRules.add(new LandPhoneInProvinceRule());
  200.  
    chargeRules.add(new LandPhoneInlandRule());
  201.  
    }
  202.  
     
  203.  
    @Override
  204.  
    public double calCost(UserRecords userRecords) {
  205.  
    double sumCost = 0;
  206.  
    for (ChargeRule rule : chargeRules) {
  207.  
    sumCost = rule.calCost(userRecords);
  208.  
    }
  209.  
    return sumCost;
  210.  
    }
  211.  
     
  212.  
    @Override
  213.  
    public double getMonthlyRent() {
  214.  
    return monthlyRent;
  215.  
    }
  216.  
     
  217.  
    }
  218.  
     
  219.  
    class MobilePhoneCharging extends ChargeMode {
  220.  
     
  221.  
    private double monthlyRent = 15;
  222.  
     
  223.  
    public MobilePhoneCharging() {
  224.  
    super();
  225.  
    chargeRules.add(new MobilePhoneInCityRule());
  226.  
    chargeRules.add(new MobilePhoneInProvinceRule());
  227.  
    chargeRules.add(new MobilePhoneInlandRule());
  228.  
    }
  229.  
     
  230.  
    @Override
  231.  
    public double calCost(UserRecords userRecords) {
  232.  
    double sumCost = 0;
  233.  
    for (ChargeRule rule : chargeRules) {
  234.  
    sumCost = rule.calCost(userRecords);
  235.  
    }
  236.  
    return sumCost;
  237.  
    }
  238.  
     
  239.  
    @Override
  240.  
    public double getMonthlyRent() {
  241.  
    return monthlyRent;
  242.  
    }
  243.  
     
  244.  
    }
  245.  
     
  246.  
    class Inputdeal {
  247.  
     
  248.  
    public int check(String input) {
  249.  
    if (input.matches("[u]-0791[0-9]{7,8}\\s[0]") || input.matches("[u]-1[0-9]{10}\\s[1]")) {
  250.  
    return 1;
  251.  
    // } else if (input.charAt(0) == 'm') {
  252.  
    // return 2;
  253.  
    } else if (input.matches("(([t]-0791[0-9]{7,8}\\s" "0[0-9]{9,11}\\s)|"
  254.  
    "([t]-0791[0-9]{7,8}\\s" "1[0-9]{10}\\s" "0[0-9]{2,3}\\s)|"
  255.  
    "([t]-1[0-9]{10}\\s" "0[0-9]{2,3}\\s" "0[0-9]{9,11}\\s)|"
  256.  
    "([t]-1[0-9]{10}\\s" "0[0-9]{2,3}\\s" "1[0-9]{10}\\s" "0[0-9]{2,3}\\s))"
  257.  
     
  258.  
    "((([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?"
  259.  
    "[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((("
  260.  
    "[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))"
  261.  
    "\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\\s"
  262.  
    "((([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])\\.("
  263.  
    "[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((("
  264.  
    "[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))"
  265.  
    "\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])")) {
  266.  
    return 2;
  267.  
    }
  268.  
    return 0;
  269.  
    }
  270.  
     
  271.  
    @SuppressWarnings("unused")
  272.  
    private boolean validatet(String string) {
  273.  
    if (!string.matches("^([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$")) {
  274.  
    return false;
  275.  
    }
  276.  
    return true;
  277.  
    }
  278.  
     
  279.  
    public static boolean validate(String dateString) {
  280.  
    // 使用正则表达式 测试 字符 符合 dddd.dd.dd 的格式(d表示数字)
  281.  
    Pattern p = Pattern.compile("\\d{4} [\\.]\\d{1,2} [\\.]\\d{1,2} ");
  282.  
    Matcher m = p.matcher(dateString);
  283.  
    if (!m.matches()) {
  284.  
    return false;
  285.  
    }
  286.  
     
  287.  
    // 得到年月日
  288.  
    String[] array = dateString.split("\\.");
  289.  
    int year = Integer.valueOf(array[0]);
  290.  
    int month = Integer.valueOf(array[1]);
  291.  
    int day = Integer.valueOf(array[2]);
  292.  
     
  293.  
    if (month < 1 || month > 12) {
  294.  
    return false;
  295.  
    }
  296.  
    int[] monthLengths = new int[] { 0, 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  297.  
    if (isLeapYear(year)) {
  298.  
    monthLengths[2] = 29;
  299.  
    } else {
  300.  
    monthLengths[2] = 28;
  301.  
    }
  302.  
    int monthLength = monthLengths[month];
  303.  
    if (day < 1 || day > monthLength) {
  304.  
    return false;
  305.  
    }
  306.  
    return true;
  307.  
    }
  308.  
     
  309.  
    /** 是否是闰年 */
  310.  
    private static boolean isLeapYear(int year) {
  311.  
    return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
  312.  
    }
  313.  
     
  314.  
    public boolean judge(String input) {
  315.  
     
  316.  
    return false;
  317.  
    }
  318.  
     
  319.  
    public void writeUser(ArrayList<User> users, String input) {
  320.  
    User usernew = new User();
  321.  
    String[] inputs = input.split(" ");
  322.  
    String num = inputs[0].substring(2);
  323.  
    for (User i : users) {
  324.  
    if (i.getNumber().equals(num)) {
  325.  
    return;
  326.  
    }
  327.  
    }
  328.  
    usernew.setNumber(num);
  329.  
    int mode = Integer.parseInt(inputs[1]);
  330.  
    if (mode == 0) {
  331.  
    usernew.setChargeMode(new LandlinePhoneCharging());
  332.  
    } else if (mode == 1) {
  333.  
    usernew.setChargeMode(new MobilePhoneCharging());
  334.  
    }
  335.  
    users.add(usernew);
  336.  
    }
  337.  
     
  338.  
    public void writeRecord(ArrayList<User> users, String input) {
  339.  
    String[] inputs = input.split(" ");
  340.  
     
  341.  
    User callu = null, answeru = null;
  342.  
    CallRecord callrecord = new CallRecord(inputs);
  343.  
     
  344.  
    if (input.charAt(0) == 't') {
  345.  
    String out = inputs[0];
  346.  
    String in = "";
  347.  
    if (inputs.length == 6) {
  348.  
    in = inputs[1];
  349.  
    } else if (inputs.length == 7) {
  350.  
    in = inputs[1];
  351.  
    } else if (inputs.length == 8) {
  352.  
    in = inputs[2];
  353.  
    }
  354.  
     
  355.  
    for (User i : users) {
  356.  
    if (i.getNumber().equals(out)) {
  357.  
    callu = i;
  358.  
    }
  359.  
    if (i.getNumber().equals(in)) {
  360.  
    answeru = i;
  361.  
    }
  362.  
    if (callu != null && answeru != null) {
  363.  
    break;
  364.  
    }
  365.  
    }
  366.  
     
  367.  
    if (callu != null) {
  368.  
    if (callrecord.getCallType().matches("^1[1-3]$")) {
  369.  
    callu.getUserRecords().addCallingInCityRecords(callrecord);
  370.  
    } else if (callrecord.getCallType().matches("^2[1-3]$")) {
  371.  
    callu.getUserRecords().addCallingInProvinceRecords(callrecord);
  372.  
    } else {
  373.  
    callu.getUserRecords().addCallingInLandRecords(callrecord);
  374.  
    }
  375.  
    }
  376.  
     
  377.  
    if (answeru != null) {
  378.  
    if (callrecord.getCallType().matches("^[1-3]1$")) {
  379.  
    answeru.getUserRecords().addAnswerInCityRecords(callrecord);
  380.  
    } else if (callrecord.getCallType().matches("^[1-3]2$")) {
  381.  
    answeru.getUserRecords().aaddAnswerInProvinceRecords(callrecord);
  382.  
    } else {
  383.  
    answeru.getUserRecords().addAnswerInLandRecords(callrecord);
  384.  
    }
  385.  
    }
  386.  
    } else if (input.charAt(0) == 'm') {
  387.  
     
  388.  
    }
  389.  
     
  390.  
    }
  391.  
     
  392.  
    }
  393.  
     
  394.  
    abstract class CommunicationRecord {
  395.  
    protected String callingNumber;
  396.  
    protected String answerNumbe;
  397.  
     
  398.  
    public String getCallingNumber() {
  399.  
    return callingNumber;
  400.  
    }
  401.  
     
  402.  
    public void setCallingNumber(String callingNumber) {
  403.  
    this.callingNumber = callingNumber;
  404.  
    }
  405.  
     
  406.  
    public String getAnswerNumbe() {
  407.  
    return answerNumbe;
  408.  
    }
  409.  
     
  410.  
    public void setAnswerNumbe(String answerNumbe) {
  411.  
    this.answerNumbe = answerNumbe;
  412.  
    }
  413.  
     
  414.  
    }
  415.  
     
  416.  
    abstract class ChargeRule {
  417.  
     
  418.  
    abstract public double calCost(UserRecords userRecords);
  419.  
     
  420.  
    }
  421.  
     
  422.  
    class CallRecord extends CommunicationRecord {
  423.  
    private Date startTime;
  424.  
    private Date endTime;
  425.  
    private String callingAddressAreaCode;
  426.  
    private String answerAddressAreaCode;
  427.  
     
  428.  
    public String getCallType() {
  429.  
    String type = "";
  430.  
    if (callingAddressAreaCode.equals("0791")) {
  431.  
    type = type.concat("1");
  432.  
    } else if (callingAddressAreaCode.matches("^079[023456789]$") || callingAddressAreaCode.equals("0701")) {
  433.  
    type = type.concat("2");
  434.  
    } else {
  435.  
    type = type.concat("3");
  436.  
    }
  437.  
     
  438.  
    if (answerAddressAreaCode.equals("0791")) {
  439.  
    type = type.concat("1");
  440.  
    } else if (answerAddressAreaCode.matches("^079[023456789]$") || answerAddressAreaCode.equals("0701")) {
  441.  
    type = type.concat("2");
  442.  
    } else {
  443.  
    type = type.concat("3");
  444.  
    }
  445.  
     
  446.  
    return type;
  447.  
    }
  448.  
     
  449.  
    public CallRecord(String[] inputs) {
  450.  
    super();
  451.  
     
  452.  
    char type = inputs[0].charAt(0);
  453.  
    inputs[0] = inputs[0].substring(2);
  454.  
     
  455.  
    String sd = null, st = null, ed = null, et = null;
  456.  
     
  457.  
    if (type == 't') {
  458.  
    if (inputs.length == 6) {
  459.  
    sd = inputs[2];
  460.  
    st = inputs[3];
  461.  
    ed = inputs[4];
  462.  
    et = inputs[5];
  463.  
    callingAddressAreaCode = inputs[0].substring(0, 4);
  464.  
    answerAddressAreaCode = inputs[1].substring(0, 4);
  465.  
    } else if (inputs.length == 7) {
  466.  
    sd = inputs[3];
  467.  
    st = inputs[4];
  468.  
    ed = inputs[5];
  469.  
    et = inputs[6];
  470.  
    if (inputs[0].charAt(0) != '0') {
  471.  
    if (inputs[2].length() == 10) {
  472.  
    answerAddressAreaCode = inputs[2].substring(0, 3);
  473.  
    } else {
  474.  
    answerAddressAreaCode = inputs[2].substring(0, 4);
  475.  
    }
  476.  
    callingAddressAreaCode = inputs[1];
  477.  
    } else {
  478.  
    if (inputs[0].length() == 10) {
  479.  
    callingAddressAreaCode = inputs[0].substring(0, 3);
  480.  
    } else {
  481.  
    callingAddressAreaCode = inputs[0].substring(0, 4);
  482.  
    }
  483.  
    answerAddressAreaCode = inputs[2];
  484.  
    }
  485.  
    } else if (inputs.length == 8) {
  486.  
    sd = inputs[4];
  487.  
    st = inputs[5];
  488.  
    ed = inputs[6];
  489.  
    et = inputs[7];
  490.  
    callingAddressAreaCode = inputs[1];
  491.  
    answerAddressAreaCode = inputs[3];
  492.  
    }
  493.  
    } else if (type == 'm') {
  494.  
     
  495.  
    }
  496.  
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault());
  497.  
    try {
  498.  
    startTime = simpleDateFormat.parse(sd " " st);
  499.  
    endTime = simpleDateFormat.parse(ed " " et);
  500.  
    } catch (ParseException e) {
  501.  
    }
  502.  
     
  503.  
    }
  504.  
     
  505.  
    public CallRecord(Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) {
  506.  
    super();
  507.  
    this.startTime = startTime;
  508.  
    this.endTime = endTime;
  509.  
    this.callingAddressAreaCode = callingAddressAreaCode;
  510.  
    this.answerAddressAreaCode = answerAddressAreaCode;
  511.  
    }
  512.  
     
  513.  
    public Date getStartTime() {
  514.  
    return startTime;
  515.  
    }
  516.  
     
  517.  
    public void setStartTime(Date startTime) {
  518.  
    this.startTime = startTime;
  519.  
    }
  520.  
     
  521.  
    public Date getEndTime() {
  522.  
    return endTime;
  523.  
    }
  524.  
     
  525.  
    public void setEndTime(Date endTime) {
  526.  
    this.endTime = endTime;
  527.  
    }
  528.  
     
  529.  
    public String getCallingAddressAreaCode() {
  530.  
    return callingAddressAreaCode;
  531.  
    }
  532.  
     
  533.  
    public void setCallingAddressAreaCode(String callingAddressAreaCode) {
  534.  
    this.callingAddressAreaCode = callingAddressAreaCode;
  535.  
    }
  536.  
     
  537.  
    public String getAnswerAddressAreaCode() {
  538.  
    return answerAddressAreaCode;
  539.  
    }
  540.  
     
  541.  
    public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
  542.  
    this.answerAddressAreaCode = answerAddressAreaCode;
  543.  
    }
  544.  
    }
  545.  
     
  546.  
    abstract class CallChargeRule extends ChargeRule {
  547.  
     
  548.  
    }
  549.  
     
  550.  
    class LandPhoneInCityRule extends CallChargeRule {
  551.  
     
  552.  
    @Override
  553.  
    public double calCost(UserRecords userRecords) {
  554.  
    double sumCost = 0;
  555.  
    for (CallRecord call : userRecords.getCallingInCityRecords()) {
  556.  
    double distanceS = (-call.getStartTime().getTime() call.getEndTime().getTime()) / 1000;
  557.  
    if (distanceS < 0) {
  558.  
    continue;
  559.  
    }
  560.  
    double distanceM = (int) distanceS / 60;
  561.  
    if (distanceS % 60 != 0) {
  562.  
    distanceM = 1;
  563.  
    }
  564.  
    if (call.getCallType().equals("11")) {
  565.  
    sumCost = distanceM * 0.1;
  566.  
    } else if (call.getCallType().equals("12")) {
  567.  
    sumCost = distanceM * 0.3;
  568.  
    } else if (call.getCallType().equals("13")) {
  569.  
    sumCost = distanceM * 0.6;
  570.  
    }
  571.  
    }
  572.  
    return sumCost;
  573.  
    }
  574.  
     
  575.  
    }
  576.  
     
  577.  
    class LandPhoneInlandRule extends CallChargeRule {
  578.  
     
  579.  
    @Override
  580.  
    public double calCost(UserRecords userRecords) {
  581.  
    double sumCost = 0;
  582.  
    for (CallRecord call : userRecords.getCallingInLandRecords()) {
  583.  
    double distanceS = (-call.getStartTime().getTime() call.getEndTime().getTime()) / 1000;
  584.  
    if (distanceS < 0) {
  585.  
    continue;
  586.  
    }
  587.  
    double distanceM = (int) distanceS / 60;
  588.  
    if (distanceS % 60 != 0) {
  589.  
    distanceM = 1;
  590.  
    }
  591.  
    sumCost = distanceM * 0.6;
  592.  
    }
  593.  
    return sumCost;
  594.  
    }
  595.  
     
  596.  
    }
  597.  
     
  598.  
    class LandPhoneInProvinceRule extends CallChargeRule {
  599.  
     
  600.  
    @Override
  601.  
    public double calCost(UserRecords userRecords) {
  602.  
    double sumCost = 0;
  603.  
    for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
  604.  
    double distanceS = (-call.getStartTime().getTime() call.getEndTime().getTime()) / 1000;
  605.  
    if (distanceS < 0) {
  606.  
    continue;
  607.  
    }
  608.  
    double distanceM = (int) distanceS / 60;
  609.  
    if (distanceS % 60 != 0) {
  610.  
    distanceM = 1;
  611.  
    }
  612.  
    sumCost = distanceM * 0.3;
  613.  
    }
  614.  
    return sumCost;
  615.  
    }
  616.  
     
  617.  
    }
  618.  
     
  619.  
    class MobilePhoneInCityRule extends CallChargeRule {
  620.  
     
  621.  
    @Override
  622.  
    public double calCost(UserRecords userRecords) {
  623.  
    double sumCost = 0;
  624.  
    for (CallRecord call : userRecords.getCallingInCityRecords()) {
  625.  
    double distanceS = (-call.getStartTime().getTime() call.getEndTime().getTime()) / 1000;
  626.  
    if (distanceS < 0) {
  627.  
    continue;
  628.  
    }
  629.  
    double distanceM = (int) distanceS / 60;
  630.  
    if (distanceS % 60 != 0) {
  631.  
    distanceM = 1;
  632.  
    }
  633.  
    if (call.getCallType().equals("11")) {
  634.  
    sumCost = distanceM * 0.1;
  635.  
    } else if (call.getCallType().equals("12")) {
  636.  
    sumCost = distanceM * 0.2;
  637.  
    } else if (call.getCallType().equals("13")) {
  638.  
    sumCost = distanceM * 0.3;
  639.  
    }
  640.  
     
  641.  
    }
  642.  
    return sumCost;
  643.  
    }
  644.  
     
  645.  
    }
  646.  
     
  647.  
    class MobilePhoneInlandRule extends CallChargeRule {
  648.  
     
  649.  
    @Override
  650.  
    public double calCost(UserRecords userRecords) {
  651.  
    double sumCost = 0;
  652.  
    for (CallRecord call : userRecords.getCallingInLandRecords()) {
  653.  
    double distanceS = (-call.getStartTime().getTime() call.getEndTime().getTime()) / 1000;
  654.  
    if (distanceS < 0) {
  655.  
    continue;
  656.  
    }
  657.  
    double distanceM = (int) distanceS / 60;
  658.  
    if (distanceS % 60 != 0) {
  659.  
    distanceM = 1;
  660.  
    }
  661.  
    sumCost = distanceM * 0.6;
  662.  
    }
  663.  
    for (CallRecord call : userRecords.getAnswerInLandRecords()) {
  664.  
    double distanceS = (-call.getStartTime().getTime() call.getEndTime().getTime()) / 1000;
  665.  
    if (distanceS < 0) {
  666.  
    continue;
  667.  
    }
  668.  
    double distanceM = (int) distanceS / 60;
  669.  
    if (distanceS % 60 != 0) {
  670.  
    distanceM = 1;
  671.  
    }
  672.  
    sumCost = distanceM * 0.3;
  673.  
    }
  674.  
    return sumCost;
  675.  
    }
  676.  
     
  677.  
    }
  678.  
     
  679.  
    class MobilePhoneInProvinceRule extends CallChargeRule {
  680.  
     
  681.  
    @Override
  682.  
    public double calCost(UserRecords userRecords) {
  683.  
    double sumCost = 0;
  684.  
    for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
  685.  
    double distanceS = (-call.getStartTime().getTime() call.getEndTime().getTime()) / 1000;
  686.  
    if (distanceS < 0) {
  687.  
    continue;
  688.  
    }
  689.  
    double distanceM = (int) distanceS / 60;
  690.  
    if (distanceS % 60 != 0) {
  691.  
    distanceM = 1;
  692.  
    }
  693.  
    if (call.getCallType().equals("21")) {
  694.  
    sumCost = distanceM * 0.3;
  695.  
    } else if (call.getCallType().equals("22")) {
  696.  
    sumCost = distanceM * 0.3;
  697.  
    } else if (call.getCallType().equals("23")) {
  698.  
    sumCost = distanceM * 0.3;
  699.  
    }
  700.  
    }
  701.  
    return sumCost;
  702.  
    }
  703.  
     
  704.  
    }
  705.  
     
  706.  
    class MessageRecord extends CommunicationRecord {
  707.  
     
  708.  
    private String message;
  709.  
     
  710.  
    public String getMessage() {
  711.  
    return message;
  712.  
    }
  713.  
     
  714.  
    public void setMessage(String message) {
  715.  
    this.message = message;
  716.  
    }
  717.  
    }
  718.  
     
  719.  
    class User {
  720.  
     
  721.  
    private UserRecords userRecords = new UserRecords();
  722.  
    private double balance = 100;
  723.  
    private ChargeMode chargeMode;
  724.  
    private String number;
  725.  
     
  726.  
    public double calCost() {
  727.  
    return chargeMode.calCost(userRecords);
  728.  
    }
  729.  
     
  730.  
    public double calBalance() {
  731.  
    return balance - chargeMode.getMonthlyRent() - chargeMode.calCost(userRecords);
  732.  
    }
  733.  
     
  734.  
    public UserRecords getUserRecords() {
  735.  
    return userRecords;
  736.  
    }
  737.  
     
  738.  
    public void setUserRecords(UserRecords userRecords) {
  739.  
    this.userRecords = userRecords;
  740.  
    }
  741.  
     
  742.  
    public ChargeMode getChargeMode() {
  743.  
    return chargeMode;
  744.  
    }
  745.  
     
  746.  
    public void setChargeMode(ChargeMode chargeMode) {
  747.  
    this.chargeMode = chargeMode;
  748.  
    }
  749.  
     
  750.  
    public String getNumber() {
  751.  
    return number;
  752.  
    }
  753.  
     
  754.  
    public void setNumber(String number) {
  755.  
    this.number = number;
  756.  
    }
  757.  
     
  758.  
    }
  759.  
     
  760.  
    class Outputtool {
  761.  
     
  762.  
    @SuppressWarnings("deprecation")
  763.  
    public void output(double out) {
  764.  
    // java.text.DecimalFormat df=new java.text.DecimalFormat("#.##");
  765.  
    // String a=df.format(out);
  766.  
    // System.out.print(a);
  767.  
    BigDecimal numb = new BigDecimal(out);
  768.  
    out = numb.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
  769.  
    System.out.print(out);
  770.  
    }
  771.  
    }
学新通

我滴总结:

先上我滴类图

链接:https://pan.百度.com/s/1ZKvJGoYzhAavvtl3U-5xLg?pwd=1111 
提取码:1111

其实和上一次的类图没有改变什么,

新增了手机rule类,再之前的框架上增加了手机计费规则,

在输入处理增加了手机输入的判断。

注意,这一次千万别判断短信,有几个测试点是关于短信的,一定要把短信判断是错误输入。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfbbgee
系列文章
更多 icon
同类精品
更多 icon
继续加载