前言
对于这三次大作业,主要的难题就是电信计费系列的题目,以及接口的使用还有迭代器的基本使用,最后一次大作业还复习了之前的多态的内容。总体来说这三次大作业难度不大,题量也适中,最难的就是第一次电信计费--座机计费的内容,第一次只要设计的较好,后面两次的手机以及短信的计费就比较简单。这次博客将针对这三次作业的电信计费系列的三题进行分析。
考察的知识点
- 抽象类
- 正则表达式
- 排序接口的使用
- 迭代器
待分析题目
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
说明:图1中User是用户类,包括属性:userRecords (用户记录)、balance(余额)、chargeMode(计费方式)、number(号码)。
ChargeMode是计费方式的抽象类:
chargeRules是计费方式所包含的各种计费规则的集合,ChargeRule类的定义见图3。
getMonthlyRent()方法用于返回月租(monthlyRent)。
UserRecords是用户记录类,保存用户各种通话、短信的记录,
各种计费规则将使用其中的部分或者全部记录。
其属性从上到下依次是:
市内拨打电话、省内(不含市内)拨打电话、省外拨打电话、
市内接听电话、省内(不含市内)接听电话、省外接听电话的记录
以及发送短信、接收短信的记录。
图二
图2中CommunicationRecord是抽象的通讯记录类:
包含callingNumber拨打号码、answerNumber接听号码两个属性。
CallRecord(通话记录)、MessageRecord(短信记录)是它的子类。
CallRecord(通话记录类)包含属性:
通话的起始、结束时间以及
拨号地点的区号(callingAddressAreaCode)、接听地点的区号(answerAddressAreaCode)。
区号用于记录在哪个地点拨打和接听的电话。座机无法移动,就是本机区号,如果是手机号,则会有差异。
图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
输入输出样例
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元。
每条通讯、短信信息均单独计费后累加,不是将所有信息累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。
错误处理
输入数据中出现的不符合格式要求的行一律忽略。
建议类图
同上一次题目集一样。
输入输出样例
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。
本题只考虑短信计费,不考虑通信费用以及月租费。
建议类图
同上。
输入输出样例
u-18907910010 3
m-18907910010 13305862264 aaaaaaaaaaaaaaaaaaaaaaa
end
18907910010 0.3 99.7
设计与分析
1、电信计费--座机计费
类图
圈复杂度分析图
设计
这次题目主要是看懂老师所提供的代码,分析各个类的关系以及每个类的属性与方法,去利用所给类图写出大致的框架。其次就是数据的分析处理,这里才是此次题目的难点所在。还有就是计算不同规则的费用,但计算所需费用的关键在于分类以及求时间。
- 对于数据处理
可以使用正则表达式,以及string的substring以及split方法来判断数据正确性以及提取出数据。对于时间的判断来说,可以使用SimpleDateFormat类。 - 对于时间计算
在拨打记录中就记下各种数据,特别的对于两个时间的提取,在此类中写一个计算记录通话时间的方法去返回时间;对于不同的拨打记录,这里就需要记录拨打电话和接听电话的区号,省内省外以及市内的,根据不同的计费方式求得花费的金额。 - 仔细分析所给类图结构,帮助实现要求。
- 对于两类数据,要做不同的处理,一个是添加用户,一个是添加通话记录。
- 还有就是需要使用到compare接口对输出结果进行排序。
2、电信计费--手机+座机计费
类图
圈复杂度分析图
设计
对于这次的题目,是在上一个题目的基础上,来增加了手机计费功能,也就是再添加四个类去实现手机计费功能。当然,输入数据的类型也变得更为的复杂:其中就包括了手机互打、座机互打以及手机与座机之间相互通话,这里就需要判断输入的通话记录里的双方是通过手机还是座机来拨打电话和接听电话的,记录不同的类型。还有就是对于区号的提取,由于类型的不同,输入数据中相应位置的数据的意义又所变换,需要我们去提取对应位置的数据。根据题目的要求,对不同类型的通话记录做不同的计算格式得到最终结果。
3、电信计费--短信计费
类图
圈复杂度分析图
设计
短信计费是整个电信计费中最简单的,它的要求简单,短信的计算主要抓住短信的内容以及短信的条数。这里就需要计算短信的条数以及短信内容的字符长度。
踩坑心得
主要的坑都是在座机计费中碰到
- 没有删掉重复开户的用户
- 在判断输入的时间的时候,没有考虑都闰年以及每个月的日子天数有特定的要求导致采用了需要剔除的数据。
- 最耽误我时间的一个点,就是因为我没有注意大小写,所以就导致了时间计算的错误(转化就出了错)
我的错误就是把前面MM小写和后面的HH小写,导致一直过不去很多测试点,又找不到原因。
其中的hh小写表示12小时;HH表示24小时。
4、在第二次手机+座机计费中的排序问题
一开始想的是座机0开头肯定是比1开头的手机小,排在前,就没改动排序,但是,后面才知道两者的位数不同,比较起来手机的号码比座机的号码小导致输出顺序出错。
注:对于短信计费没有什么踩坑的地方。
改进建议
此次其实没有什么自己设计的地方,都是利用老师所给的类图来完成作业,做了少部分的修改,大致还是利用所给类图。其他两道题的圈复杂度还可以,座机+手机的圈复杂度较高,有19,这也是因为它的情况较多,要考虑得多,但是这也恰恰反映了我的短板,就是不怎么会降低圈复杂度,也就是题目出现情况一多,就常用条件判断,这是需要学习的地方,也是修改和完善代码的一条可选之路。
源码附录
座机计费
点击查看代码
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.Locale;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
ArrayList<User> users = new ArrayList<>();
Input_row input_row = new Input_row();
Scanner in = new Scanner(System.in);
String input = in.nextLine();
while (!input.equals("end")) {
if (input_row.u_t(input) == 1) {
input_row.create_user(users, input);
} else if (input_row.u_t(input) == 2) {
input_row.create_userRecords(users, input);
}
input = 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;
}
}
});
java.text.DecimalFormat df = new java.text.DecimalFormat("0.0#");
for (User user : users) {
System.out.print(user.getNumber() + " ");
System.out.print(df.format(user.calCost()) + " ");
System.out.println(df.format(user.calBalance()));
}
}
}
/*
* 用户类
*/
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 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 addAnswerInProvinceRecords(CallRecord answerRecord) {
answerInProvinceRecords.add(answerRecord);
}
public void addAnswerInLandRecords(CallRecord answerRecord) {
answerInLandRecords.add(answerRecord);
}
public void addSendMessageRecords(MessageRecord sendMessageRecord) {
sendMessageRecords.add(sendMessageRecord);
}
public void addReceiveMessageRecords(MessageRecord receiveMessageRecord) {
receiveMessageRecords.add(receiveMessageRecord);
}
public ArrayList<CallRecord> getCallingInCityRecords() {
return callingInCityRecords;
}
public ArrayList<CallRecord> getCallingInProvinceRecords() {
return callingInProvinceRecords;
}
public ArrayList<CallRecord> getCallingInLandRecords() {
return callingInLandRecords;
}
public ArrayList<CallRecord> getAnswerInCityRecords() {
return answerInCityRecords;
}
public ArrayList<CallRecord> getAnswerInProvinceRecords() {
return answerInProvinceRecords;
}
public ArrayList<CallRecord> getAnswerInLandRecords() {
return answerInLandRecords;
}
public ArrayList<MessageRecord> getSendMessageRecords() {
return sendMessageRecords;
}
public ArrayList<MessageRecord> getReceiveMessageRecords() {
return receiveMessageRecords;
}
}
/*
* 计费方式的抽象类
*/
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 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.0;
for (ChargeRule rule : chargeRules) {
sumcost += rule.calCost(userRecords);
}
return sumcost;
}
@Override
public double getMonthlyRent() {
return monthlyRent;
}
}
/*
* 抽象的通讯记录类
*/
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 setanswerNumber(String answerNumber) {
this.answerNumber = answerNumber;
}
}
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 CallRecord(String s) {
super();
String[] str = s.split(" ");
if (str[0].substring(2).length() == 10) {
callingAddressAreaCode = str[0].substring(2, 5);
} else {
callingAddressAreaCode = str[0].substring(2, 6);
}
if (str[1].length() == 10) {
answerAddressAreaCode = str[1].substring(0, 3);
}
else {
answerAddressAreaCode = str[1].substring(0, 4);
}
// HH24小时制,hh12小时制!!
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault());
try {
startTime = simpleDateFormat.parse(str[2] + " " + str[3]);
endTime = simpleDateFormat.parse(str[4] + " " + str[5]);
} catch (ParseException e) {
}
}
// 得到通话时间
public int getTimes_callRecord() {
int times = 0;
double time = (endTime.getTime() - startTime.getTime()) / 1000;
times = (int) (time / 60);
if (time % 60 != 0)
times = times + 1;
if (time < 0)
times = -1;
return times;
}
// 判断省内、省外、市内通话
public int getCallType() {
if (answerAddressAreaCode.equals("0791")) {
return 1;
}
else if (answerAddressAreaCode.substring(0,3).equals("079") || answerAddressAreaCode.equals("0701")) {
return 2;
}
return 3;
}
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 CallChargeRule extends ChargeRule {
}
class LandPhoneInCityRule extends CallChargeRule {
@Override
public double calCost(UserRecords userRecords) {
double sumCost = 0;
for (CallRecord call : userRecords.getCallingInCityRecords()) {
if (call.getTimes_callRecord() < 0)
continue;
sumCost += call.getTimes_callRecord() * 0.1;
}
return sumCost;
}
}
class LandPhoneInlandRule extends CallChargeRule {
@Override
public double calCost(UserRecords userRecords) {
double sumCost = 0;
for (CallRecord call : userRecords.getCallingInLandRecords()) {
if (call.getTimes_callRecord() < 0)
continue;
sumCost += call.getTimes_callRecord() * 0.6;
}
return sumCost;
}
}
class LandPhoneInProvinceRule extends CallChargeRule {
@Override
public double calCost(UserRecords userRecords) {
double sumCost = 0;
for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
if (call.getTimes_callRecord() < 0)
continue;
sumCost += call.getTimes_callRecord() * 0.3;
}
return sumCost;
}
}
class Input_row {
public int u_t(String s) {
String[] str = s.split(" ");
if (str.length == 2) {
if (s.matches("[u]-[0][7]([9][0-9]|[0][1])[0-9]{7,8} [0-3]")) {
return 1;
}
} else if (str.length == 6) {
if (s.matches("^t\\-0\\d{9,11}\\s0\\d{9,11}((\\s\\d{4}\\.([1-9]|([1]{1}[0-2]{1}))\\.([1-9]|([1-2]{1}[0-9]{1})|3[0-1])\\s(([0-1][0-9])|(2[0-3]))\\:([0-5][0-9])\\:([0-5][0-9])){2})"))
{
if (check_date2(str[2]))
if (check_date2(str[4]))
return 2;
}
}
return 0;
}
public boolean check_date2(String s) {
String[] str = s.split("\\.");
int year = Integer.valueOf(str[0]);
int month = Integer.valueOf(str[1]);
int day = Integer.valueOf(str[2]);
if (month < 1 || month > 12)
return false;
int[] month_days = new int[] { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
month_days[2] = 29;
}
if (day < 1 || day > month_days[month])
return false;
return true;
}
//
public void create_user(ArrayList<User> users, String s) {
User user = new User();
String[] str = s.split(" ");
for (User useri : users) {
if (useri.getNumber().equals(str[0].substring(2)))
return;
}
user.setNumber(str[0].substring(2));
int mode = Integer.valueOf(str[1]);
if(mode==0)
user.setChargeMode(new LandlinePhoneCharging());
users.add(user);
}
public void create_userRecords(ArrayList<User> users, String s) {
String[] str = s.split(" ");
CallRecord callRecord = new CallRecord(s);
User call_user = null;
for (User i : users) {
if (i.getNumber().equals(str[0].substring(2)))
{call_user = i;break;}
}
if (call_user != null) {
if (callRecord.getCallType() == 1) {
call_user.getUserRecords().addCallingInCityRecords(callRecord);
} else if (callRecord.getCallType() == 2) {
call_user.getUserRecords().addCallingInProvinceRecords(callRecord);
} else {
call_user.getUserRecords().addCallingInLandRecords(callRecord);
}
}
}
}
手机+座机
点击查看代码
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.Locale;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
ArrayList<User> users = new ArrayList<>();
Input_row input_row = new Input_row();
Scanner in = new Scanner(System.in);
String input = in.nextLine();
while (!input.equals("end")) {
if (input_row.u_t(input) == 1) {
input_row.create_user(users, input);
} else if (input_row.u_t(input) == 2) {
input_row.create_userRecords(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;
}
}
});
java.text.DecimalFormat df = new java.text.DecimalFormat("0.0#");
for (User user : users) {
System.out.print(user.getNumber() + " ");
System.out.print(df.format(user.calCost()) + " ");
System.out.println(df.format(user.calBalance()));
}
}
}
/*
* 用户类
*/
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 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 addAnswerInProvinceRecords(CallRecord answerRecord) {
answerInProvinceRecords.add(answerRecord);
}
public void addAnswerInLandRecords(CallRecord answerRecord) {
answerInLandRecords.add(answerRecord);
}
public void addSendMessageRecords(MessageRecord sendMessageRecord) {
sendMessageRecords.add(sendMessageRecord);
}
public void addReceiveMessageRecords(MessageRecord receiveMessageRecord) {
receiveMessageRecords.add(receiveMessageRecord);
}
public ArrayList<CallRecord> getCallingInCityRecords() {
return callingInCityRecords;
}
public ArrayList<CallRecord> getCallingInProvinceRecords() {
return callingInProvinceRecords;
}
public ArrayList<CallRecord> getCallingInLandRecords() {
return callingInLandRecords;
}
public ArrayList<CallRecord> getAnswerInCityRecords() {
return answerInCityRecords;
}
public ArrayList<CallRecord> getAnswerInProvinceRecords() {
return answerInProvinceRecords;
}
public ArrayList<CallRecord> getAnswerInLandRecords() {
return answerInLandRecords;
}
public ArrayList<MessageRecord> getSendMessageRecords() {
return sendMessageRecords;
}
public ArrayList<MessageRecord> getReceiveMessageRecords() {
return receiveMessageRecords;
}
}
/*
* 计费方式的抽象类
*/
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 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.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) {
// TODO Auto-generated method stub
double sumCost = 0;
for (ChargeRule rule : chargeRules) {
sumCost += rule.calCost(userRecords);
}
return sumCost;
}
@Override
public double getMonthlyRent() {
// TODO Auto-generated method stub
return monthlyRent;
}
}
/*
* 抽象的通讯记录类
*/
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 setanswerNumber(String answerNumber) {
this.answerNumber = answerNumber;
}
}
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 CallRecord(String[] ins) {
super();
char type = ins[0].charAt(0);
ins[0] = ins[0].substring(2);
String strat_day = "", start_time = "", end_day = "", end_time = "";
if (type == 't') {
if (ins.length == 6) {
strat_day = ins[2];
start_time = ins[3];
end_day = ins[4];
end_time = ins[5];
callingAddressAreaCode = ins[0].substring(0, 4);
answerAddressAreaCode = ins[1].substring(0, 4);
// System.out.println(answerAddressAreaCode);
} else if (ins.length == 7) {
strat_day = ins[3];
start_time = ins[4];
end_day = ins[5];
end_time = ins[6];
// callingAddressAreaCode = ins[0].substring(0, 4);
// answerAddressAreaCode = ins[2];
if (ins[0].charAt(0) != '0') {
if (ins[2].length() == 10) {
answerAddressAreaCode = ins[2].substring(0, 3);
} else {
answerAddressAreaCode = ins[2].substring(0, 4);
}
callingAddressAreaCode = ins[1];
} else {
if (ins[0].length() == 10) {
callingAddressAreaCode = ins[0].substring(0, 3);
} else {
callingAddressAreaCode = ins[0].substring(0, 4);
}
answerAddressAreaCode = ins[2];
}
} else if (ins.length == 8) {
strat_day = ins[4];
start_time = ins[5];
end_day = ins[6];
end_time = ins[7];
callingAddressAreaCode = ins[1];
answerAddressAreaCode = ins[3];
}
} else if (type == 'm') {
}
// HH24小时制,hh12小时制!!
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault());
try {
startTime = simpleDateFormat.parse(strat_day + " " + start_time);
endTime = simpleDateFormat.parse(end_day + " " + end_time);
} catch (ParseException e) {
}
}
// 得到通话时间
public int getTimes_callRecord() {
int times = 0;
double time = (endTime.getTime() - startTime.getTime()) / 1000;
times = (int) (time / 60);
if (time % 60 != 0)
times = times + 1;
if (time < 0)
times = -1;
return times;
}
// 判断省内、省外、市内通话
public String getCallType() {
String type = "";
if (callingAddressAreaCode.equals("0791")) {
type = "1";
} else if (callingAddressAreaCode.matches("^079[\\d]$") || callingAddressAreaCode.equals("0701")) {
type = "2";
} else {
type = "3";
}
if (answerAddressAreaCode.equals("0791")) {
type += "1";
} else if (answerAddressAreaCode.matches("^079[\\d]$") || answerAddressAreaCode.equals("0701")) {
type += "2";
// System.out.println("1");
} else {
type += "3";
// System.out.println("2");
}
return type;
}
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 CallChargeRule extends ChargeRule {
}
class LandPhoneInCityRule extends CallChargeRule {
@Override
public double calCost(UserRecords userRecords) {
double sumCost = 0;
for (CallRecord call : userRecords.getCallingInCityRecords()) {
if (call.getTimes_callRecord() < 0)
continue;
if (call.getCallType().equals("11")) {
sumCost += call.getTimes_callRecord() * 0.1;
} else if (call.getCallType().equals("12")) {
sumCost += call.getTimes_callRecord() * 0.3;
// System.out.println("1");
} else if (call.getCallType().equals("13")) {
sumCost += call.getTimes_callRecord() * 0.6;
// System.out.println("2");
}
}
return sumCost;
}
}
class LandPhoneInlandRule extends CallChargeRule {
@Override
public double calCost(UserRecords userRecords) {
double sumCost = 0;
for (CallRecord call : userRecords.getCallingInLandRecords()) {
if (call.getTimes_callRecord() < 0)
continue;
sumCost += call.getTimes_callRecord() * 0.6;
}
return sumCost;
}
}
class LandPhoneInProvinceRule extends CallChargeRule {
@Override
public double calCost(UserRecords userRecords) {
double sumCost = 0;
for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
if (call.getTimes_callRecord() < 0)
continue;
sumCost += call.getTimes_callRecord() * 0.3;
}
return sumCost;
}
}
class MobilePhoneInCityRule extends CallChargeRule {
@Override
public double calCost(UserRecords userRecords) {
// TODO Auto-generated method stub
double sumCost = 0;
for (CallRecord call : userRecords.getCallingInCityRecords()) {
if (call.getTimes_callRecord() < 0)
continue;
else {
if (call.getCallType().equals("11")) {
sumCost += call.getTimes_callRecord() * 0.1;
} else if (call.getCallType().equals("12")) {
sumCost += call.getTimes_callRecord() * 0.2;
} else if (call.getCallType().equals("13")) {
sumCost += call.getTimes_callRecord() * 0.3;
}
}
}
return sumCost;
}
}
class MobilePhoneInlandRule extends CallChargeRule {
@Override
public double calCost(UserRecords userRecords) {
// TODO Auto-generated method stub
double sumCost = 0;
for (CallRecord call : userRecords.getCallingInLandRecords()) {
if (call.getTimes_callRecord() < 0)
continue;
sumCost += call.getTimes_callRecord() * 0.6;
}
for (CallRecord call : userRecords.getAnswerInLandRecords()) {
sumCost += call.getTimes_callRecord() * 0.3;
}
return sumCost;
}
}
class MobilePhoneInProvinceRule extends CallChargeRule {
@Override
public double calCost(UserRecords userRecords) {
// TODO Auto-generated method stub
double sumCost = 0;
for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
if (call.getTimes_callRecord() < 0)
continue;
else {
sumCost += call.getTimes_callRecord() * 0.3;
}
}
return sumCost;
}
}
class Input_row {
public int u_t(String s) {
if (s.matches("u-0\\d{10,11}\\s0") || s.matches("[u]-1[0-9]{10}\\s[1]")) {
return 1;
} else if (s.matches("(([t]-0791[0-9]{7,8}\\s" + "0[0-9]{9,11})|" + "([t]-0791[0-9]{7,8}\\s"
+ "1[0-9]{10}\\s" + "0[0-9]{2,3})|" + "([t]-1[0-9]{10}\\s" + "0[0-9]{2,3}\\s" + "0[0-9]{9,11})|"
+ "([t]-1[0-9]{10}\\s" + "0[0-9]{2,3}\\s" + "1[0-9]{10}\\s" + "0[0-9]{2,3}))"
+ "((\\s\\d{4}\\.([1-9]|([1]{1}[0-2]{1}))\\.([1-9]|([1-2]{1}[0-9]{1})|3[0-1])\\s(([0-1][0-9])|(2[0-3]))\\:([0-5][0-9])\\:([0-5][0-9])){2})"
)){
String[] ins=s.split(" ");
char type = ins[0].charAt(0);
ins[0] = ins[0].substring(2);
String strat_day = "", start_time = "", end_day = "", end_time = "";
if (type == 't') {
if (ins.length == 6) {
strat_day = ins[2];
start_time = ins[3];
end_day = ins[4];
end_time = ins[5];
} else if (ins.length == 7) {
strat_day = ins[3];
start_time = ins[4];
end_day = ins[5];
end_time = ins[6];
} else if (ins.length == 8) {
strat_day = ins[4];
start_time = ins[5];
end_day = ins[6];
end_time = ins[7];
}
} else if (type == 'm') {
}
if(check_date2(strat_day))
if(check_date2(end_day))
return 2;
}
return 0;
}
public boolean check_date2(String s) {
String[] str = s.split("\\.");
int year = Integer.valueOf(str[0]);
int month = Integer.valueOf(str[1]);
int day = Integer.valueOf(str[2]);
if (month < 1 || month > 12)
return false;
int[] month_days = new int[] { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
month_days[2] = 29;
}
if (day < 1 || day > month_days[month])
return false;
return true;
}
//
public void create_user(ArrayList<User> users, String s) {
User user = new User();
String[] str = s.split(" ");
for (User useri : users) {
if (useri.getNumber().equals(str[0].substring(2)))
return;
}
user.setNumber(str[0].substring(2));
int mode = Integer.valueOf(str[1]);
if (mode == 0)
user.setChargeMode(new LandlinePhoneCharging());
else if (mode == 1) {
user.setChargeMode(new MobilePhoneCharging());
}
users.add(user);
}
public void create_userRecords(ArrayList<User> users, String s) {
String[] str = s.split(" ");
CallRecord callrecord = new CallRecord(str);
User call_user = null, answer_user = null;
if (s.charAt(0) == 't') {
String in = ""; // 接听号码
if (str.length == 6) {
in = str[1];
} else if (str.length == 7) {
in = str[1];
} else if (str.length == 8) {
in = str[2];
}
for (User i : users) {
if (i.getNumber().equals(str[0])) { // !!!!!!!!!!!
call_user = i;
// break;
}
if (i.getNumber().equals(in)) {
answer_user = i;
}
if (call_user != null && answer_user != null) {
break;
}
}
if (call_user != null) {
if (callrecord.getCallType().matches("^1[1-3]$")) {
call_user.getUserRecords().addCallingInCityRecords(callrecord);
} else if (callrecord.getCallType().matches("^2[1-3]$")) {
call_user.getUserRecords().addCallingInProvinceRecords(callrecord);
} else {
call_user.getUserRecords().addCallingInLandRecords(callrecord);
}
}
if (answer_user != null) {
if (callrecord.getCallType().matches("^[1-3]1$")) {
answer_user.getUserRecords().addAnswerInCityRecords(callrecord);
} else if (callrecord.getCallType().matches("^[1-3]2$")) {
answer_user.getUserRecords().addAnswerInProvinceRecords(callrecord);
} else {
answer_user.getUserRecords().addAnswerInLandRecords(callrecord);
}
}
} else if (s.charAt(0) == 'm') {
}
}
}
短信
点击查看代码
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.Locale;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
ArrayList<User> users = new ArrayList<>();
Input_row input_row = new Input_row();
Scanner in = new Scanner(System.in);
String input = in.nextLine();
while (!input.equals("end")) {
if (input_row.u_t(input) == 1) {
input_row.create_user(users, input);
} else if (input_row.u_t(input) == 2) {
input_row.create_userRecords(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;
}
}
});
java.text.DecimalFormat df = new java.text.DecimalFormat("0.0#");
for (User user : users) {
System.out.print(user.getNumber() + " ");
System.out.print(df.format(user.calCost()) + " ");
System.out.println(df.format(user.calBalance()));
}
}
}
/*
* 用户类
*/
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 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 addAnswerInProvinceRecords(CallRecord answerRecord) {
answerInProvinceRecords.add(answerRecord);
}
public void addAnswerInLandRecords(CallRecord answerRecord) {
answerInLandRecords.add(answerRecord);
}
public void addSendMessageRecords(MessageRecord sendMessageRecord) {
sendMessageRecords.add(sendMessageRecord);
}
public void addReceiveMessageRecords(MessageRecord receiveMessageRecord) {
receiveMessageRecords.add(receiveMessageRecord);
}
public ArrayList<CallRecord> getCallingInCityRecords() {
return callingInCityRecords;
}
public ArrayList<CallRecord> getCallingInProvinceRecords() {
return callingInProvinceRecords;
}
public ArrayList<CallRecord> getCallingInLandRecords() {
return callingInLandRecords;
}
public ArrayList<CallRecord> getAnswerInCityRecords() {
return answerInCityRecords;
}
public ArrayList<CallRecord> getAnswerInProvinceRecords() {
return answerInProvinceRecords;
}
public ArrayList<CallRecord> getAnswerInLandRecords() {
return answerInLandRecords;
}
public ArrayList<MessageRecord> getSendMessageRecords() {
return sendMessageRecords;
}
public ArrayList<MessageRecord> getReceiveMessageRecords() {
return receiveMessageRecords;
}
}
/*
* 计费方式的抽象类
*/
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 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.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) {
// TODO Auto-generated method stub
double sumCost = 0;
for (ChargeRule rule : chargeRules) {
sumCost += rule.calCost(userRecords);
}
return sumCost;
}
@Override
public double getMonthlyRent() {
// TODO Auto-generated method stub
return monthlyRent;
}
}
class MobilePhoneMassageCharging extends ChargeMode {
private double monthlyRent = 0;
public MobilePhoneMassageCharging() {
super();
chargeRules.add(new MobilePhoneMessageRule());
}
@Override
public double calCost(UserRecords userRecords) {
// TODO Auto-generated method stub
double sumCost = 0;
for (ChargeRule rule : chargeRules) {
sumCost += rule.calCost(userRecords);
}
return sumCost;
// return 0;
}
@Override
public double getMonthlyRent() {
return monthlyRent;
// TODO Auto-generated method stub
// return 0;
}
}
/*
* 抽象的通讯记录类
*/
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 setanswerNumber(String answerNumber) {
this.answerNumber = answerNumber;
}
}
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 CallRecord(String[] ins) {
super();
char type = ins[0].charAt(0);
ins[0] = ins[0].substring(2);
String strat_day = "", start_time = "", end_day = "", end_time = "";
if (type == 't') {
if (ins.length == 6) {
strat_day = ins[2];
start_time = ins[3];
end_day = ins[4];
end_time = ins[5];
callingAddressAreaCode = ins[0].substring(0, 4);
answerAddressAreaCode = ins[1].substring(0, 4);
// System.out.println(answerAddressAreaCode);
} else if (ins.length == 7) {
strat_day = ins[3];
start_time = ins[4];
end_day = ins[5];
end_time = ins[6];
// callingAddressAreaCode = ins[0].substring(0, 4);
// answerAddressAreaCode = ins[2];
if (ins[0].charAt(0) != '0') {
if (ins[2].length() == 10) {
answerAddressAreaCode = ins[2].substring(0, 3);
} else {
answerAddressAreaCode = ins[2].substring(0, 4);
}
callingAddressAreaCode = ins[1];
} else {
if (ins[0].length() == 10) {
callingAddressAreaCode = ins[0].substring(0, 3);
} else {
callingAddressAreaCode = ins[0].substring(0, 4);
}
answerAddressAreaCode = ins[2];
}
} else if (ins.length == 8) {
strat_day = ins[4];
start_time = ins[5];
end_day = ins[6];
end_time = ins[7];
callingAddressAreaCode = ins[1];
answerAddressAreaCode = ins[3];
}
} else if (type == 'm') {
}
// HH24小时制,hh12小时制!!
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault());
try {
startTime = simpleDateFormat.parse(strat_day + " " + start_time);
endTime = simpleDateFormat.parse(end_day + " " + end_time);
} catch (ParseException e) {
}
}
// 得到通话时间
public int getTimes_callRecord() {
int times = 0;
double time = (endTime.getTime() - startTime.getTime()) / 1000;
times = (int) (time / 60);
if (time % 60 != 0)
times = times + 1;
if (time < 0)
times = -1;
return times;
}
// 判断省内、省外、市内通话
public String getCallType() {
String type = "";
if (callingAddressAreaCode.equals("0791")) {
type = "1";
} else if (callingAddressAreaCode.matches("^079[\\d]$") || callingAddressAreaCode.equals("0701")) {
type = "2";
} else {
type = "3";
}
if (answerAddressAreaCode.equals("0791")) {
type += "1";
} else if (answerAddressAreaCode.matches("^079[\\d]$") || answerAddressAreaCode.equals("0701")) {
type += "2";
// System.out.println("1");
} else {
type += "3";
// System.out.println("2");
}
return type;
}
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 input) {
this.message = input.substring(26);
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
abstract class CallChargeRule extends ChargeRule {
}
class LandPhoneInCityRule extends CallChargeRule {
@Override
public double calCost(UserRecords userRecords) {
double sumCost = 0;
for (CallRecord call : userRecords.getCallingInCityRecords()) {
if (call.getTimes_callRecord() < 0)
continue;
if (call.getCallType().equals("11")) {
sumCost += call.getTimes_callRecord() * 0.1;
} else if (call.getCallType().equals("12")) {
sumCost += call.getTimes_callRecord() * 0.3;
// System.out.println("1");
} else if (call.getCallType().equals("13")) {
sumCost += call.getTimes_callRecord() * 0.6;
// System.out.println("2");
}
}
return sumCost;
}
}
class MobilePhoneMessageRule extends CallChargeRule {
@Override
public double calCost(UserRecords userRecords) {
// TODO Auto-generated method stub
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 LandPhoneInlandRule extends CallChargeRule {
@Override
public double calCost(UserRecords userRecords) {
double sumCost = 0;
for (CallRecord call : userRecords.getCallingInLandRecords()) {
if (call.getTimes_callRecord() < 0)
continue;
sumCost += call.getTimes_callRecord() * 0.6;
}
return sumCost;
}
}
class LandPhoneInProvinceRule extends CallChargeRule {
@Override
public double calCost(UserRecords userRecords) {
double sumCost = 0;
for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
if (call.getTimes_callRecord() < 0)
continue;
sumCost += call.getTimes_callRecord() * 0.3;
}
return sumCost;
}
}
class MobilePhoneInCityRule extends CallChargeRule {
@Override
public double calCost(UserRecords userRecords) {
// TODO Auto-generated method stub
double sumCost = 0;
for (CallRecord call : userRecords.getCallingInCityRecords()) {
if (call.getTimes_callRecord() < 0)
continue;
else {
if (call.getCallType().equals("11")) {
sumCost += call.getTimes_callRecord() * 0.1;
} else if (call.getCallType().equals("12")) {
sumCost += call.getTimes_callRecord() * 0.2;
} else if (call.getCallType().equals("13")) {
sumCost += call.getTimes_callRecord() * 0.3;
}
}
}
return sumCost;
}
}
class MobilePhoneInlandRule extends CallChargeRule {
@Override
public double calCost(UserRecords userRecords) {
// TODO Auto-generated method stub
double sumCost = 0;
for (CallRecord call : userRecords.getCallingInLandRecords()) {
if (call.getTimes_callRecord() < 0)
continue;
sumCost += call.getTimes_callRecord() * 0.6;
}
for (CallRecord call : userRecords.getAnswerInLandRecords()) {
sumCost += call.getTimes_callRecord() * 0.3;
}
return sumCost;
}
}
class MobilePhoneInProvinceRule extends CallChargeRule {
@Override
public double calCost(UserRecords userRecords) {
// TODO Auto-generated method stub
double sumCost = 0;
for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
if (call.getTimes_callRecord() < 0)
continue;
else {
sumCost += call.getTimes_callRecord() * 0.3;
}
}
return sumCost;
}
}
class Input_row {
public int u_t(String s) {
if (s.matches("u-0\\d{10,11}\\s0") || s.matches("[u]-1[0-9]{10}\\s[13]")) {
return 1;
} else if (s.matches("[m]-1[0-9]{10}\\s" + "1[0-9]{10}\\s" + "[0-9a-zA-Z\\s\\.,]+")) {
return 2;
}
return 0;
}
public boolean check_date2(String s) {
String[] str = s.split("\\.");
int year = Integer.valueOf(str[0]);
int month = Integer.valueOf(str[1]);
int day = Integer.valueOf(str[2]);
if (month < 1 || month > 12)
return false;
int[] month_days = new int[] { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
month_days[2] = 29;
}
if (day < 1 || day > month_days[month])
return false;
return true;
}
//
public void create_user(ArrayList<User> users, String s) {
User user = new User();
String[] str = s.split(" ");
for (User useri : users) {
if (useri.getNumber().equals(str[0].substring(2)))
return;
}
user.setNumber(str[0].substring(2));
int mode = Integer.valueOf(str[1]);
if (mode == 0)
user.setChargeMode(new LandlinePhoneCharging());
else if (mode == 1) {
user.setChargeMode(new MobilePhoneCharging());
} else if (mode == 3) {
user.setChargeMode(new MobilePhoneMassageCharging());
}
users.add(user);
}
public void create_userRecords(ArrayList<User> users, String s) {
String[] str = s.split(" ");
// CallRecord callrecord = new CallRecord(str);
str[0] = str[0].substring(2);
User call_user = null, answer_user = null;
String in = ""; // 接听号码
if (str.length == 6) {
in = str[1];
} else if (str.length == 7) {
in = str[1];
} else if (str.length == 8) {
in = str[2];
} else {
in = str[1];
}
for (User i : users) {
if (i.getNumber().equals(str[0])) { // !!!!!!!!!!!
call_user = i;
// break;
}
if (i.getNumber().equals(in)) {
answer_user = i;
}
if (call_user != null && answer_user != null) {
break;
}
}
if (s.charAt(0) == 'm') {
MessageRecord messageRecord = new MessageRecord(s);
if (call_user != null) {
call_user.getUserRecords().addSendMessageRecords(messageRecord);
}
if (answer_user != null) {
answer_user.getUserRecords().addReceiveMessageRecords(messageRecord);
}
}
}
}
总结
通过电信计费系列pta以及其他几道小题的历练,我们面向对象程序设计课程的学习又更进一步了,虽然踩了很多坑,花费了很多的时间,但这也让我意识到了现在所学的东西远不足以对付今后甚至目前的学习和生活,我们需要掌握和改进的地方还有太多太多:
(1)在面对一些较为复杂的题目时,我们不应该直接不管三七二十一 就开始写代码,应该先仔细地审题,将题目简单化,在草稿纸上列出大概的框架,需要使用的类与对象、以及类与类之间、对象与对象之间该如何合理有效地联系起来都应该有个清晰的思路与框架;这三次的大作业都有老师的类图提供参考才使得我们同学做起来会顺手一点,但是如果要自己设计的话,肯定会碰到很多问题。
(2)碰到一些奇怪的数据时,应该积极地使用ecilpse的debug功能,而不是总是尝试用肉眼找出自己代码错误,因为当题目要我们完成的东西较为复杂时,即使我们的代码逻辑思路与框架很清晰,也很难直接用肉眼找出错误。特别是判断错误数据的时候,debug真的很好用,我在实验中碰到有出路的结果,也会去debug来找到错误,有的时候真的省事。
(3)我们应该在先前的基础上更加进一步地面向对象,通过类和对象的设计让自己的代码框架和逻辑更加清楚,合理且有效地使用类与对象,真正领悟面向对象的真谛,让自己的代码简单明了、逻辑清晰,而不是有大量的赘余,重复啰嗦。这里就需要理解老师上课讲的设计原则以及模式使用。
(4)对于正则表达式以及字符串处理要越来越熟悉,很多时候都需要用到!!!
最后感谢面向对象程序设计课程老师的作业监督与指导,在今后的学习中,我们应该更加努力,迎接接下来的学习!
标签:总结,return,String,ArrayList,ins,Pta6,new,题目,public From: https://www.cnblogs.com/wjxynybsclbc/p/16952986.html