目录
一、前言
本次总结的是PTA6-PTA8的所有题目,主要涉及同一个系列,电信计费系列,由座机收费到手机收费再到短信收费,同一系列的题目,更考验代码的复用性。这次老师给出了可参考的类图,由于给出的类和函数、属性等过多,这使我们省去很多时间去构造,设计类的结构等内容。总体比多边形的作业好得多。
二、设计与分析
第六次作业
7-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,可根据理解自行调整:
图1
图1中User是用户类,包括属性:
userRecords (用户记录)、balance(余额)、chargeMode(计费方式)、number(号码)。
ChargeMode是计费方式的抽象类:
chargeRules是计费方式所包含的各种计费规则的集合,ChargeRule类的定义见图3。
getMonthlyRent()方法用于返回月租(monthlyRent)。
UserRecords是用户记录类,保存用户各种通话、短信的记录,
各种计费规则将使用其中的部分或者全部记录。
其属性从上到下依次是:
市内拨打电话、省内(不含市内)拨打电话、省外拨打电话、
市内接听电话、省内(不含市内)接听电话、省外接听电话的记录
以及发送短信、接收短信的记录。
图2
图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
代码展示
点击查看代码
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String str[];
ArrayList<User> users = new ArrayList<User>();
ArrayList<CallRecord> callRecords = new ArrayList<>();
String regex1 = "u-[0-9]{10,12}\\s[0-3]$";
String regex2 = "t-[0-9]{10,12}\\s[0-9]{10,12}\\s[0-9]{4}.([1-9]|1[0-2]).(([1-9])|((1|2)[0-9])|30|31)\\s([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]\\s[0-9]{4}.([1-9]|1[0-2]).(([1-9])|((1|2)[0-9])|30|31)\\s([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$";
//String regex2 = "^t-[0-9]{10,12}\\s[0-9]{10,12}\\s[0-9]{4}.([1-9]|1[0-2]).(([1-9])|((1|2)[0-9])|30|31)\\s([0|1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]\\s[0-9]{4}.([1-9]|1[0-2]).(([1-9])|((1|2)[0-9])|30|31)\\s([0|1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$";
String s;
s = input.nextLine();
while (!s.equals("end")){
if(s.matches(regex1)) {
//String areaCode;
//CallRecord callRecord = new CallRecord();
boolean l = true;
str = s.split("-| ");
//areaCode = str[1].substring(0, 4);
//callRecord.setCallingAddressAreaCode(areaCode);
LandlinePhoneCharging phoneCharging = new LandlinePhoneCharging();
User user = new User(phoneCharging,str[1]);
for (User u : users) {
if(u.getNumber().equals(user.number)) {
l = false;
}
}
if(l == true) {
users.add(user);
}
}
else if(s.matches(regex2)) {
CallRecord callRecord = new CallRecord();
String callingAddressAreaCode,answerAddressAreaCode;
Date starttime=null,endtime=null;
str = s.split("-| ");
callRecord.setCallingNumber(str[1]);
callRecord.setAnswerNumber(str[2]);
if(str[1].length()==10)
callingAddressAreaCode = str[1].substring(0,3);
else
callingAddressAreaCode = str[1].substring(0,4);
callRecord.setCallingAddressAreaCode(callingAddressAreaCode);
if(str[2].length() == 10)
answerAddressAreaCode = str[2].substring(0,3);
else
answerAddressAreaCode = str[2].substring(0,4);
callRecord.setAnswerAddressAreaCode(answerAddressAreaCode);
DateFormat df = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
df.setLenient(false);//表示严格验证
try {
starttime = df.parse(str[3] + " " + str[4]);
callRecord.setStartTime(starttime);
} catch (ParseException e) {
}
try {
endtime = df.parse(str[5] + " " + str[6]);
callRecord.setEndTime(endtime);
} catch (ParseException e) {
}
//if(endtime.getTime() - starttime.getTime() > 0)
callRecords.add(callRecord);
}
s = input.nextLine();
}
//去重复记录,辅助集合
ArrayList<CallRecord> l = new ArrayList<>();
for (CallRecord i:callRecords) {
if(!l.contains(i)) {
l.add(i);
}
}
callRecords.clear();
callRecords.addAll(l);
for (User i : users) {
boolean flag = false;
UserRecords userRecords = new UserRecords();
for (CallRecord j : callRecords) {
if(i.getNumber().equals(j.getCallingNumber())) {
flag = true;
if(j.getAnswerAddressAreaCode().matches("0791"))
userRecords.addCallingInCityRecords(j);
else if(j.getAnswerAddressAreaCode().matches("07(01|9[0-9])"))
userRecords.addCallingInProvinceRecords(j);
else
userRecords.addCallingInLandRecords(j);
}
}
if(flag)
{
i.setUserRecords(userRecords);flag=false;
}
}
Collections.sort(users,new Comparator<User>() {
@Override
public int compare(User o1, User o2) {
// TODO Auto-generated method stub
double x=Double.parseDouble(o1.getNumber());
double y=Double.parseDouble(o2.getNumber());
return (int) (x-y);
}
});
for (User i : users) {
System.out.println(i.getNumber() + " " + String.format("%.1f %.1f",i.calCost(),i.calBalance()));
}
}
}
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 MessageRecord extends CommunicationRecord {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
class CallRecord extends CommunicationRecord {
Date startTime;
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;
}
}
abstract class ChargeRule {
}
abstract class CallChargeRule extends ChargeRule {
public abstract double calCost(ArrayList<CallRecord> callRecords);
}
class LandPhoneInCityRule extends CallChargeRule {
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double sum = 0;
for (CallRecord i : callRecords) {
double s = i.endTime.getTime() - i.startTime.getTime();
s = s / 1000 / 60;
if (s % 1 == 0)
sum += s * 0.1;
else {
s = s - s % 1 + 1;
sum += s * 0.1;
}
}
return sum;
}
}
class LandPhoneInProvinceRule extends CallChargeRule {
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double sum = 0;
for (CallRecord i : callRecords) {
double s = i.endTime.getTime() - i.startTime.getTime();
s = s / 1000 / 60;
if (s % 1 == 0)
sum += s * 0.3;
else {
s = s - s % 1 + 1;
sum += s * 0.3;
}
}
return sum;
}
}
class LandPhoneInLandRule extends CallChargeRule {
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double sum = 0;
for (CallRecord i : callRecords) {
double s = i.endTime.getTime() - i.startTime.getTime();
s = s / 1000 / 60;
if (s % 1 == 0)
sum += s * 0.6;
else {
s = s - s % 1 + 1;
sum += s * 0.6;
}
}
return sum;
}
}
abstract class ChargeMode {
private ArrayList<ChargeRule> chargeRules = new ArrayList<>();
public ArrayList<ChargeRule> getChargeRules() {
return chargeRules;
}
public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
this.chargeRules = chargeRules;
}
public abstract double calCost(UserRecords userRecords);
abstract double getMouthlyRent();
}
class LandlinePhoneCharging extends ChargeMode {
private double mouthlyRent = 20;
@Override
public double calCost(UserRecords userRecords) {
double sum = 0;
LandPhoneInLandRule landPhoneInLandRule = new LandPhoneInLandRule();
LandPhoneInCityRule landPhoneInCityRule = new LandPhoneInCityRule();
LandPhoneInProvinceRule landPhoneInProvinceRule = new LandPhoneInProvinceRule();
sum = sum + landPhoneInLandRule.calCost(userRecords.getCallingInLandRecords());
sum = sum + landPhoneInCityRule.calCost(userRecords.getCallingInCityRecords());
sum = sum + landPhoneInProvinceRule.calCost(userRecords.getCallingInProvinceRecords());
return sum;
}
@Override
public double getMouthlyRent() {
return mouthlyRent;
}
}
class UserRecords {
ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();
ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();
ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();
ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();
ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();
ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();
ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();
ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();
public void addCallingInCityRecords(CallRecord callRacord) {
callingInCityRecords.add(callRacord);
}
public void addCallingInProvinceRecords(CallRecord callRacord) {
callingInProvinceRecords.add(callRacord);
}
public void addCallingInLandRecords(CallRecord callRacord) {
callingInLandRecords.add(callRacord);
}
public void addAnswerInCityRecords(CallRecord answerRecord) {
answerInCityRecords.add(answerRecord);
//System.out.println(answerInLandRecords.get(0).getAnswerNumber());
}
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 addReceiveMessageRecord(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;
}
}
class User {
UserRecords userRecords = new UserRecords();
double balance = 100;
ChargeMode chargeMode;
String number;
User(ChargeMode chargeMode, String number) {
this.chargeMode = chargeMode;
this.number = number;
}
double calBalance() {
return balance - (calCost() + chargeMode.getMouthlyRent());
}
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 String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public ChargeMode getChargeMode() {
return chargeMode;
}
public void setChargeMode(ChargeMode chargeMode) {
this.chargeMode = chargeMode;
}
}
类图
代码报表分析
解释与心得
解决问题先考虑解决问题的架构,这次大作业我们只需要根据题目给出的类图依次实现。首先对输入的数据进行判断,建立用户返回1,用户打电话返回2,对数据的处理返回大于2即可说明数据输入错误,丢弃数据不对其进行处理,当返回1时,需要比较该用户是否已经注册,如果没有注册就加入列表中,否则不处理,当返回2时,需要讲该数据根据号码加入到电话列表;当输入end结束,根据号码进行排序输出本月的话费以及余额。数据错误的情况是座机号码不对,因为我国的地区号可能为3位或4位,因此座机号码的长度为10到12,而不是11到12,(这个点如果不清楚会卡住);在加入电话列表时,需要判断打电话和接电话所在区域,可分为市内、省内、国内,根据区域号进行判断加入到不同的列表中,计算话费时,根据列表中的信息进行计算。
第七次大作业
7-1电信计费系列2-手机+座机计费
题目
实现南昌市电信分公司的计费程序,假设该公司针对手机和座机用户分别采取了两种计费方案,分别如下:
1、针对市内座机用户采用的计费方式(与电信计费系列1内容相同):
月租20元,接电话免费,市内拨打电话0.1元/分钟,省内长途0.3元/分钟,国内长途拨打0.6元/分钟。不足一分钟按一分钟计。
假设本市的区号:0791,江西省内各地市区号包括:0790~0799以及0701。
2、针对手机用户采用实时计费方式:
月租15元,市内省内接电话均免费,市内拨打市内电话0.1元/分钟,市内拨打省内电话0.2元/分钟,市内拨打省外电话0.3元/分钟,省内漫游打电话0.3元/分钟,省外漫游接听0.3元/分钟,省外漫游拨打0.6元/分钟;
注:被叫电话属于市内、省内还是国内由被叫电话的接听地点区号决定,比如以下案例中,南昌市手机用户13307912264在区号为020的广州接听了电话,主叫号码应被计算为拨打了一个省外长途,同时,手机用户13307912264也要被计算省外接听漫游费:
u-13307912264 1
t-079186330022 13307912264 020 2022.1.3 10:00:25 2022.1.3 10:05:11
输入:
输入信息包括两种类型
1、逐行输入南昌市用户开户的信息,每行一个用户,含手机和座机用户
格式:u-号码 计费类型 (计费类型包括:0-座机 1-手机实时计费 2-手机A套餐)
例如:u-079186300001 0
座机号码由区号和电话号码拼接而成,电话号码包含7-8位数字,区号最高位是0。
手机号码由11位数字构成,最高位是1。
本题在电信计费系列1基础上增加类型1-手机实时计费。
手机设置0或者座机设置成1,此种错误可不做判断。
2、逐行输入本月某些用户的通讯信息,通讯信息格式:
座机呼叫座机:t-主叫号码 接听号码 起始时间 结束时间
t-079186330022 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:11
以上四项内容之间以一个英文空格分隔,
时间必须符合"yyyy.MM.dd HH:mm:ss"格式。提示:使用SimpleDateFormat类。
输入格式增加手机接打电话以及收发短信的格式,手机接打电话的信息除了号码之外需要额外记录拨打/接听的地点的区号,比如:
座机打手机:
t-主叫号码 接听号码 接听地点区号 起始时间 结束时间
t-079186330022 13305862264 020 2022.1.3 10:00:25 2022.1.3 10:05:11
手机互打:
t-主叫号码 拨号地点 接听号码 接听地点区号 起始时间 结束时间
t-18907910010 0791 13305862264 0371 2022.1.3 10:00:25 2022.1.3 10:05:11
注意:以上两类信息,先输入所有开户信息,再输入所有通讯信息,最后一行以“end”结束。
输出:
根据输入的详细通讯信息,计算所有已开户的用户的当月费用(精确到小数点后2位,单位元)。假设每个用户初始余额是100元。
每条通讯、短信信息均单独计费后累加,不是将所有信息累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。
错误处理:
输入数据中出现的不符合格式要求的行一律忽略。
代码展示
点击查看代码
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.text.ParseException;
public class Main {
public static void main(String[] args) {
Outputtool outputtool = new Outputtool();
Inputdeal inputdeal = new Inputdeal();
ArrayList<User> users = new ArrayList<>();
Scanner in = new Scanner(System.in);
String input = in.nextLine();
while (!input.equals("end")) {
if (1 == inputdeal.check(input)) {
inputdeal.wUser(users, input);
} else if (2 == inputdeal.check(input)) {
inputdeal.wRecord(users, input);
}
input = in.nextLine();
}
users.sort(new Comparator<User>() {
@Override
public int compare(User u1, User u2) {
if (u1.getNumber().charAt(0) == '0' && u2.getNumber().charAt(0) != '0') {
return -1;
} else if (u1.getNumber().charAt(0) != '0' && u2.getNumber().charAt(0) == '0') {
return 1;
}
if (Double.parseDouble(u1.getNumber()) > Double.parseDouble(u2.getNumber())) {
return 1;
} else {
return -1;
}
}
});
for (User u : users) {
System.out.print(u.getNumber() + " ");
outputtool.output(u.calCost());
System.out.print(" ");
outputtool.output(u.calBalance());
System.out.println();
}
}
}
abstract class ChargeMode {
protected ArrayList<ChargeRule> chargeRules = new ArrayList<>();
public abstract double calCost(UserRecords userRecords);
public abstract double getMonthlyRent();
public ArrayList<ChargeRule> getChargeRules() {
return chargeRules;
}
public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
this.chargeRules = chargeRules;
}
}
class UserRecords {
private ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();
private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();
private ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();
private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();
private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();
private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();
private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();
private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();
public void addCallingInCityRecords(CallRecord callRecord) {
callingInCityRecords.add(callRecord);
}
public void addCallingInProvinceRecords(CallRecord callRecord) {
callingInProvinceRecords.add(callRecord);
}
public void addCallingInLandRecords(CallRecord callRecord) {
callingInLandRecords.add(callRecord);
}
public void addAnswerInCityRecords(CallRecord callRecord) {
answerInCityRecords.add(callRecord);
}
public void aaddAnswerInProvinceRecords(CallRecord callRecord) {
answerInProvinceRecords.add(callRecord);
}
public void addAnswerInLandRecords(CallRecord callRecord) {
answerInLandRecords.add(callRecord);
}
public void addSendMessageRecords(MessageRecord callRecord) {
sendMessageRecords.add(callRecord);
}
public void addReceiveMessageRecords(MessageRecord callRecord) {
receiveMessageRecords.add(callRecord);
}
public ArrayList<CallRecord> getCallingInCityRecords() {
return callingInCityRecords;
}
public void setCallingInCityRecords(ArrayList<CallRecord> callingInCityRecords) {
this.callingInCityRecords = callingInCityRecords;
}
public ArrayList<CallRecord> getCallingInProvinceRecords() {
return callingInProvinceRecords;
}
public void setCallingInProvinceRecords(ArrayList<CallRecord> callingInProvinceRecords) {
this.callingInProvinceRecords = callingInProvinceRecords;
}
public ArrayList<CallRecord> getCallingInLandRecords() {
return callingInLandRecords;
}
public void setCallingInLandRecords(ArrayList<CallRecord> callingInLandRecords) {
this.callingInLandRecords = callingInLandRecords;
}
public ArrayList<CallRecord> getAnswerInCityRecords() {
return answerInCityRecords;
}
public void setAnswerInCityRecords(ArrayList<CallRecord> answerInCityRecords) {
this.answerInCityRecords = answerInCityRecords;
}
public ArrayList<CallRecord> getAnswerInProvinceRecords() {
return answerInProvinceRecords;
}
public void setAnswerInProvinceRecords(ArrayList<CallRecord> answerInProvinceRecords) {
this.answerInProvinceRecords = answerInProvinceRecords;
}
public ArrayList<CallRecord> getAnswerInLandRecords() {
return answerInLandRecords;
}
public void setAnswerInLandRecords(ArrayList<CallRecord> answerInLandRecords) {
this.answerInLandRecords = answerInLandRecords;
}
public ArrayList<MessageRecord> getSendMessageRecords() {
return sendMessageRecords;
}
public void setSendMessageRecords(ArrayList<MessageRecord> sendMessageRecords) {
this.sendMessageRecords = sendMessageRecords;
}
public ArrayList<MessageRecord> getReceiveMessageRecords() {
return receiveMessageRecords;
}
public void setReceiveMessageRecords(ArrayList<MessageRecord> receiveMessageRecords) {
this.receiveMessageRecords = receiveMessageRecords;
}
}
class LandlinePhoneCharging extends ChargeMode {
private double monthlyRent = 20;
public LandlinePhoneCharging() {
super();
chargeRules.add(new LandPhoneInCityRule());
chargeRules.add(new LandPhoneInProvinceRule());
chargeRules.add(new LandPhoneInlandRule());
}
@Override
public double calCost(UserRecords userRecords) {
double sumCost = 0;
for (ChargeRule rule : chargeRules) {
sumCost += rule.calCost(userRecords);
}
return sumCost;
}
@Override
public double getMonthlyRent() {
return monthlyRent;
}
}
class MobilePhoneCharging extends ChargeMode {
private double monthlyRent = 15;
public MobilePhoneCharging() {
super();
chargeRules.add(new MobilePhoneInCityRule());
chargeRules.add(new MobilePhoneInProvinceRule());
chargeRules.add(new MobilePhoneInlandRule());
}
@Override
public double calCost(UserRecords userRecords) {
double sumCost = 0;
for (ChargeRule rule : chargeRules) {
sumCost += rule.calCost(userRecords);
}
return sumCost;
}
@Override
public double getMonthlyRent() {
return monthlyRent;
}
}
class Inputdeal {
public int check(String input) {
if (input.matches("[u]-0791[0-9]{7,8}\\s[0]") || input.matches("[u]-1[0-9]{10}\\s[1]")) {
return 1;
// } else if (input.charAt(0) == 'm') {
// return 2;
} else if (input.matches("(([t]-0791[0-9]{7,8}\\s" + "0[0-9]{9,11}\\s)|"
+ "([t]-0791[0-9]{7,8}\\s" + "1[0-9]{10}\\s" + "0[0-9]{2,3}\\s)|"
+ "([t]-1[0-9]{10}\\s" + "0[0-9]{2,3}\\s" + "0[0-9]{9,11}\\s)|"
+ "([t]-1[0-9]{10}\\s" + "0[0-9]{2,3}\\s" + "1[0-9]{10}\\s" + "0[0-9]{2,3}\\s))"
+ "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.(((0?[13578]|1[02])\\.(0?"
+ "[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((("
+ "[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))"
+ "\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\\s"
+ "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.("
+ "[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((("
+ "[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))"
+ "\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])")) {
return 2;
}
return 0;
}
public void wUser(ArrayList<User> user, String input) {
User usernew = new User();
String[] inputs = input.split(" ");
String number = inputs[0].substring(2);
for (int i = 0; i < user.size(); i++) {
if(user.get(i).getNumber().equals(number))
return;
}
usernew.setNumber(number);
int mode = Integer.parseInt(inputs[1]);
if (mode == 0) {
usernew.setChargeMode(new LandlinePhoneCharging());
} else if (mode == 1) {
usernew.setChargeMode(new MobilePhoneCharging());
}
user.add(usernew);
}
public void wRecord(ArrayList<User> user, String input) {
String[] inputs = input.split(" ");
User callu = null, answeru = null;
CallRecord callrecord = new CallRecord(inputs);
if (input.charAt(0) == 't') {
String out = inputs[0];
String in = "";
if (inputs.length == 6) {
in = inputs[1];
} else if (inputs.length == 7) {
in = inputs[1];
} else if (inputs.length == 8) {
in = inputs[2];
}
for (User i : user) {
if (i.getNumber().equals(out)) {
callu = i;
}
if (i.getNumber().equals(in)) {
answeru = i;
}
if (callu != null && answeru != null) {
break;
}
}
if (callu != null) {
if (callrecord.getCallType().matches("^1[1-3]$")) {
callu.getUserRecords().addCallingInCityRecords(callrecord);
} else if (callrecord.getCallType().matches("^2[1-3]$")) {
callu.getUserRecords().addCallingInProvinceRecords(callrecord);
} else {
callu.getUserRecords().addCallingInLandRecords(callrecord);
}
}
if (answeru != null) {
if (callrecord.getCallType().matches("^[1-3]1$")) {
answeru.getUserRecords().addAnswerInCityRecords(callrecord);
} else if (callrecord.getCallType().matches("^[1-3]2$")) {
answeru.getUserRecords().aaddAnswerInProvinceRecords(callrecord);
} else {
answeru.getUserRecords().addAnswerInLandRecords(callrecord);
}
}
} else if (input.charAt(0) == 'm') {
}
}
}
abstract class CommunicationRecord {
protected String callingNumber;
protected String answerNumbe;
public String getCallingNumber() {
return callingNumber;
}
public void setCallingNumber(String callingNumber) {
this.callingNumber = callingNumber;
}
public String getAnswerNumbe() {
return answerNumbe;
}
public void setAnswerNumbe(String answerNumbe) {
this.answerNumbe = answerNumbe;
}
}
abstract class ChargeRule {
abstract public double calCost(UserRecords userRecords);
}
class CallRecord extends CommunicationRecord {
private Date startTime;
private Date endTime;
private String callingAddressAreaCode;
private String answerAddressAreaCode;
public String getCallType() {
String type = "";
if (callingAddressAreaCode.equals("0791")) {
type = type.concat("1");
} else if (callingAddressAreaCode.matches("^079[023456789]$") || callingAddressAreaCode.equals("0701")) {
type = type.concat("2");
} else {
type = type.concat("3");
}
if (answerAddressAreaCode.equals("0791")) {
type = type.concat("1");
} else if (answerAddressAreaCode.matches("^079[023456789]$") || answerAddressAreaCode.equals("0701")) {
type = type.concat("2");
} else {
type = type.concat("3");
}
return type;
}
public CallRecord(String[] inputs) {
super();
char type = inputs[0].charAt(0);
inputs[0] = inputs[0].substring(2);
String sd = null, st = null, ed = null, et = null;
if (type == 't') {
if (inputs.length == 6) {
sd = inputs[2];
st = inputs[3];
ed = inputs[4];
et = inputs[5];
callingAddressAreaCode = inputs[0].substring(0, 4);
answerAddressAreaCode = inputs[1].substring(0, 4);
} else if (inputs.length == 7) {
sd = inputs[3];
st = inputs[4];
ed = inputs[5];
et = inputs[6];
if (inputs[0].charAt(0) != '0') {
if (inputs[2].length() == 10) {
answerAddressAreaCode = inputs[2].substring(0, 3);
} else {
answerAddressAreaCode = inputs[2].substring(0, 4);
}
callingAddressAreaCode = inputs[1];
} else {
if (inputs[0].length() == 10) {
callingAddressAreaCode = inputs[0].substring(0, 3);
} else {
callingAddressAreaCode = inputs[0].substring(0, 4);
}
answerAddressAreaCode = inputs[2];
}
} else if (inputs.length == 8) {
sd = inputs[4];
st = inputs[5];
ed = inputs[6];
et = inputs[7];
callingAddressAreaCode = inputs[1];
answerAddressAreaCode = inputs[3];
}
} else if (type == 'm') {
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault());
try {
startTime = simpleDateFormat.parse(sd + " " + st);
endTime = simpleDateFormat.parse(ed + " " + et);
} catch (ParseException e) {
}
}
public CallRecord(Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) {
super();
this.startTime = startTime;
this.endTime = endTime;
this.callingAddressAreaCode = callingAddressAreaCode;
this.answerAddressAreaCode = answerAddressAreaCode;
}
public Date getStartTime() {
return startTime;
}
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
public Date getEndTime() {
return endTime;
}
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
public String getCallingAddressAreaCode() {
return callingAddressAreaCode;
}
public void setCallingAddressAreaCode(String callingAddressAreaCode) {
this.callingAddressAreaCode = callingAddressAreaCode;
}
public String getAnswerAddressAreaCode() {
return answerAddressAreaCode;
}
public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
this.answerAddressAreaCode = answerAddressAreaCode;
}
}
abstract class CallChargeRule extends ChargeRule {
}
class LandPhoneInCityRule extends CallChargeRule {
@Override
public double calCost(UserRecords userRecords) {
double sum = 0;
for (CallRecord call : userRecords.getCallingInCityRecords()) {
double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
System.out.println(distanceS);
if (distanceS < 0) {
continue;
}
double timeM = (int) distanceS / 60;
if (distanceS % 60 != 0) {
timeM += 1;
}
if (call.getCallType().equals("11")) {
sum += timeM * 0.1;
} else if (call.getCallType().equals("12")) {
sum += timeM * 0.3;
} else if (call.getCallType().equals("13")) {
sum += timeM * 0.6;
}
}
return sum;
}
}
class LandPhoneInlandRule extends CallChargeRule {
@Override
public double calCost(UserRecords userRecords) {
double sum = 0;
for (CallRecord call : userRecords.getCallingInLandRecords()) {
double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
if (distanceS < 0) {
continue;
}
double timeM = (int) distanceS / 60;
if (distanceS % 60 != 0) {
timeM += 1;
}
sum += timeM * 0.6;
}
return sum;
}
}
class LandPhoneInProvinceRule extends CallChargeRule {
@Override
public double calCost(UserRecords userRecords) {
double sum = 0;
for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
if (distanceS < 0) {
continue;
}
double timeM = (int) distanceS / 60;
if (distanceS % 60 != 0) {
timeM += 1;
}
sum += timeM * 0.3;
}
return sum;
}
}
class MobilePhoneInCityRule extends CallChargeRule {
@Override
public double calCost(UserRecords userRecords) {
double sum = 0;
for (CallRecord call : userRecords.getCallingInCityRecords()) {
double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
if (distanceS < 0) {
continue;
}
double timeM = (int) distanceS / 60;
if (distanceS % 60 != 0) {
timeM += 1;
}
if (call.getCallType().equals("11")) {
sum += timeM * 0.1;
} else if (call.getCallType().equals("12")) {
sum += timeM * 0.2;
} else if (call.getCallType().equals("13")) {
sum += timeM * 0.3;
}
}
return sum;
}
}
class MobilePhoneInlandRule extends CallChargeRule {
@Override
public double calCost(UserRecords userRecords) {
double sum = 0;
for (CallRecord call : userRecords.getCallingInLandRecords()) {
double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
if (distanceS < 0) {
continue;
}
double timeM = (int) distanceS / 60;
if (distanceS % 60 != 0) {
timeM += 1;
}
sum += timeM * 0.6;
}
for (CallRecord call : userRecords.getAnswerInLandRecords()) {
double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
if (distanceS < 0) {
continue;
}
double timeM = (int) distanceS / 60;
if (distanceS % 60 != 0) {
timeM += 1;
}
sum += timeM * 0.3;
}
return sum;
}
}
class MobilePhoneInProvinceRule extends CallChargeRule {
@Override
public double calCost(UserRecords userRecords) {
double sum = 0;
for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
if (distanceS < 0) {
continue;
}
double timeM = (int) distanceS / 60;
if (distanceS % 60 != 0) {
timeM += 1;
}
if (call.getCallType().equals("21")) {
sum += timeM * 0.3;
} else if (call.getCallType().equals("22")) {
sum += timeM * 0.3;
} else if (call.getCallType().equals("23")) {
sum += timeM * 0.3;
}
}
return sum;
}
}
class MessageRecord extends CommunicationRecord {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
class User {
private UserRecords userRecords = new UserRecords();
private double balance = 100;
private ChargeMode chargeMode;
private String number;
public double calCost() {
return chargeMode.calCost(userRecords);
}
public double calBalance() {
return balance - chargeMode.getMonthlyRent() - chargeMode.calCost(userRecords);
}
public UserRecords getUserRecords() {
return userRecords;
}
public void setUserRecords(UserRecords userRecords) {
this.userRecords = userRecords;
}
public ChargeMode getChargeMode() {
return chargeMode;
}
public void setChargeMode(ChargeMode chargeMode) {
this.chargeMode = chargeMode;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
class Outputtool {
@SuppressWarnings("deprecation")
public void output(double out) {
// java.text.DecimalFormat df=new java.text.DecimalFormat("#.##");
// String a=df.format(out);
// System.out.print(a);
BigDecimal numb = new BigDecimal(out);
out = numb.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
System.out.print(out);
}
}
类图
生成报表
解释与心得
第二题与第一题相比,类图上唯一的区别是在抽象计费规则类CallChargeRule下多写了几个手机收费的子类。主要的考察还是集中在无效输入上。
第八次大作业
7-1 电信计费系列3-短信计费
题目
实现一个简单的电信计费程序,针对手机的短信采用如下计费方式:
1、接收短信免费,发送短信0.1元/条,超过3条0.2元/条,超过5条0.3元/条。
2、如果一次发送短信的字符数量超过10个,按每10个字符一条短信进行计算。
输入:
输入信息包括两种类型
1、逐行输入南昌市手机用户开户的信息,每行一个用户。
格式:u-号码 计费类型 (计费类型包括:0-座机 1-手机实时计费 2-手机A套餐 3-手机短信计费)
例如:u-13305862264 3
座机号码由区号和电话号码拼接而成,电话号码包含7-8位数字,区号最高位是0。
手机号码由11位数字构成,最高位是1。
本题只针对类型3-手机短信计费。
2、逐行输入本月某些用户的短信信息,短信的格式:
m-主叫号码,接收号码,短信内容 (短信内容只能由数字、字母、空格、英文逗号、英文句号组成)
m-18907910010 13305862264 welcome to jiangxi.
m-13305862264 18907910010 thank you.
注意:以上两类信息,先输入所有开户信息,再输入所有通讯信息,最后一行以“end”结束。
输出:
根据输入的详细短信信息,计算所有已开户的用户的当月短信费用(精确到小数点后2位,单位元)。假设每个用户初始余额是100元。
每条短信信息均单独计费后累加,不是将所有信息累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。
错误处理:
输入数据中出现的不符合格式要求的行一律忽略。
本题只做格式的错误判断,无需做内容上不合理的判断,比如同一个电话两条通讯记录的时间有重合、开户号码非南昌市的号码、自己给自己打电话等,此类情况都当成正确的输入计算。但时间的输入必须符合要求,比如不能输入2022.13.61 28:72:65。
本题只考虑短信计费,不考虑通信费用以及月租费。
代码展示
点击查看代码
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.text.ParseException;
public class Main {
public static void main(String[] args) {
Outputtool outputtool = new Outputtool();
Inputdeal inputdeal = new Inputdeal();
ArrayList<User> users = new ArrayList<>();
Scanner in = new Scanner(System.in);
String input = in.nextLine();
while (!input.equals("end")) {
if (1 == inputdeal.check(input)) {
inputdeal.writeUser(users, input);
} else if (2 == inputdeal.check(input)) {
inputdeal.writeRecord(users, input);
}
input = in.nextLine();
}
users.sort(new Comparator<User>() {
@Override
public int compare(User u1, User u2) {
if (u1.getNumber().charAt(0) == '0' && u2.getNumber().charAt(0) != '0') {
return -1;
} else if (u1.getNumber().charAt(0) != '0' && u2.getNumber().charAt(0) == '0') {
return 1;
}
if (Double.parseDouble(u1.getNumber()) > Double.parseDouble(u2.getNumber())) {
return 1;
} else {
return -1;
}
}
});
for (User u : users) {
System.out.print(u.getNumber() + " ");
outputtool.output(u.calCost());
System.out.print(" ");
outputtool.output(u.calBalance());
System.out.println();
}
}
}
abstract class ChargeMode {
protected ArrayList<ChargeRule> chargeRules = new ArrayList<>();
public abstract double calCost(UserRecords userRecords);
public abstract double getMonthlyRent();
public ArrayList<ChargeRule> getChargeRules() {
return chargeRules;
}
public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
this.chargeRules = chargeRules;
}
}
class UserRecords {
private ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();
private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();
private ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();
private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();
private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();
private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();
private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();
private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();
public void addCallingInCityRecords(CallRecord callRecord) {
callingInCityRecords.add(callRecord);
}
public void addCallingInProvinceRecords(CallRecord callRecord) {
callingInProvinceRecords.add(callRecord);
}
public void addCallingInLandRecords(CallRecord callRecord) {
callingInLandRecords.add(callRecord);
}
public void addAnswerInCityRecords(CallRecord callRecord) {
answerInCityRecords.add(callRecord);
}
public void aaddAnswerInProvinceRecords(CallRecord callRecord) {
answerInProvinceRecords.add(callRecord);
}
public void addAnswerInLandRecords(CallRecord callRecord) {
answerInLandRecords.add(callRecord);
}
public void addSendMessageRecords(MessageRecord callRecord) {
sendMessageRecords.add(callRecord);
}
public void addReceiveMessageRecords(MessageRecord callRecord) {
receiveMessageRecords.add(callRecord);
}
public ArrayList<CallRecord> getCallingInCityRecords() {
return callingInCityRecords;
}
public void setCallingInCityRecords(ArrayList<CallRecord> callingInCityRecords) {
this.callingInCityRecords = callingInCityRecords;
}
public ArrayList<CallRecord> getCallingInProvinceRecords() {
return callingInProvinceRecords;
}
public void setCallingInProvinceRecords(ArrayList<CallRecord> callingInProvinceRecords) {
this.callingInProvinceRecords = callingInProvinceRecords;
}
public ArrayList<CallRecord> getCallingInLandRecords() {
return callingInLandRecords;
}
public void setCallingInLandRecords(ArrayList<CallRecord> callingInLandRecords) {
this.callingInLandRecords = callingInLandRecords;
}
public ArrayList<CallRecord> getAnswerInCityRecords() {
return answerInCityRecords;
}
public void setAnswerInCityRecords(ArrayList<CallRecord> answerInCityRecords) {
this.answerInCityRecords = answerInCityRecords;
}
public ArrayList<CallRecord> getAnswerInProvinceRecords() {
return answerInProvinceRecords;
}
public void setAnswerInProvinceRecords(ArrayList<CallRecord> answerInProvinceRecords) {
this.answerInProvinceRecords = answerInProvinceRecords;
}
public ArrayList<CallRecord> getAnswerInLandRecords() {
return answerInLandRecords;
}
public void setAnswerInLandRecords(ArrayList<CallRecord> answerInLandRecords) {
this.answerInLandRecords = answerInLandRecords;
}
public ArrayList<MessageRecord> getSendMessageRecords() {
return sendMessageRecords;
}
public void setSendMessageRecords(ArrayList<MessageRecord> sendMessageRecords) {
this.sendMessageRecords = sendMessageRecords;
}
public ArrayList<MessageRecord> getReceiveMessageRecords() {
return receiveMessageRecords;
}
public void setReceiveMessageRecords(ArrayList<MessageRecord> receiveMessageRecords) {
this.receiveMessageRecords = receiveMessageRecords;
}
}
class LandlinePhoneCharging extends ChargeMode {
private double monthlyRent = 20;
public LandlinePhoneCharging() {
super();
chargeRules.add(new LandPhoneInCityRule());
chargeRules.add(new LandPhoneInProvinceRule());
chargeRules.add(new LandPhoneInlandRule());
}
@Override
public double calCost(UserRecords userRecords) {
double sumCost = 0;
for (ChargeRule rule : chargeRules) {
sumCost += rule.calCost(userRecords);
}
return sumCost;
}
@Override
public double getMonthlyRent() {
return monthlyRent;
}
}
class MobilePhoneCharging extends ChargeMode {
private double monthlyRent = 15;
public MobilePhoneCharging() {
super();
chargeRules.add(new MobilePhoneInCityRule());
chargeRules.add(new MobilePhoneInProvinceRule());
chargeRules.add(new MobilePhoneInlandRule());
}
@Override
public double calCost(UserRecords userRecords) {
double sumCost = 0;
for (ChargeRule rule : chargeRules) {
sumCost += rule.calCost(userRecords);
}
return sumCost;
}
@Override
public double getMonthlyRent() {
return monthlyRent;
}
}
class MobilePhoneMassageCharging extends ChargeMode {
private double monthlyRent = 0;
public MobilePhoneMassageCharging() {
super();
chargeRules.add(new MobilePhoneMessageRule());
}
@Override
public double calCost(UserRecords userRecords) {
double sumCost = 0;
for (ChargeRule rule : chargeRules) {
sumCost += rule.calCost(userRecords);
}
return sumCost;
}
@Override
public double getMonthlyRent() {
return monthlyRent;
}
}
class Inputdeal {
public int check(String input) {
if (input.matches("[u]-0791[0-9]{6,9}\\s[0]") || input.matches("[u]-1[0-9]{10}\\s[13]")) {
return 1;
} else if (input.matches("[m]-1[0-9]{10}\\s" + "1[0-9]{10}\\s" + "[0-9a-zA-Z\\s\\.,]+")) {
return 2;
}
return 0;
}
public void writeUser(ArrayList<User> users, String input) {
User usernew = new User();
String[] inputs = input.split(" ");
String num = inputs[0].substring(2);
for (User i : users) {
if (i.getNumber().equals(num)) {
return;
}
}
usernew.setNumber(num);
int mode = Integer.parseInt(inputs[1]);
if (mode == 0) {
usernew.setChargeMode(new LandlinePhoneCharging());
} else if (mode == 1) {
usernew.setChargeMode(new MobilePhoneCharging());
} else if (mode == 3) {
usernew.setChargeMode(new MobilePhoneMassageCharging());
}
users.add(usernew);
}
public void writeRecord(ArrayList<User> users, String input) {
String[] inputs = input.split(" ");
inputs[0] = inputs[0].substring(2);
User callu = null, answeru = null;
String out = inputs[0];
String in = "";
if (inputs.length == 6) {
in = inputs[1];
} else if (inputs.length == 7) {
in = inputs[1];
} else if (inputs.length == 8) {
in = inputs[2];
} else {
in = inputs[1];
}
for (User i : users) {
if (i.getNumber().equals(out)) {
callu = i;
}
if (i.getNumber().equals(in)) {
answeru = i;
}
if (callu != null && answeru != null) {
break;
}
}
if (input.charAt(0) == 'm') {
MessageRecord messageRecord = new MessageRecord(input);
if (callu != null) {
callu.getUserRecords().addSendMessageRecords(messageRecord);
;
}
if (answeru != null) {
callu.getUserRecords().addReceiveMessageRecords(messageRecord);
}
}
}
}
abstract class CommunicationRecord {
protected String callingNumber;
protected String answerNumbe;
public String getCallingNumber() {
return callingNumber;
}
public void setCallingNumber(String callingNumber) {
this.callingNumber = callingNumber;
}
public String getAnswerNumbe() {
return answerNumbe;
}
public void setAnswerNumbe(String answerNumbe) {
this.answerNumbe = answerNumbe;
}
}
abstract class ChargeRule {
abstract public double calCost(UserRecords userRecords);
}
class CallRecord extends CommunicationRecord {
private Date startTime;
private Date endTime;
private String callingAddressAreaCode;
private String answerAddressAreaCode;
public String getCallType() {
String type = "";
if (callingAddressAreaCode.equals("0791")) {
type = type.concat("1");
} else if (callingAddressAreaCode.matches("^079[023456789]$") || callingAddressAreaCode.equals("0701")) {
type = type.concat("2");
} else {
type = type.concat("3");
}
if (answerAddressAreaCode.equals("0791")) {
type = type.concat("1");
} else if (answerAddressAreaCode.matches("^079[023456789]$") || answerAddressAreaCode.equals("0701")) {
type = type.concat("2");
} else {
type = type.concat("3");
}
return type;
}
public CallRecord(String[] inputs) {
super();
char type = inputs[0].charAt(0);
String sd = null, st = null, ed = null, et = null;
if (type == 't') {
if (inputs.length == 6) {
sd = inputs[2];
st = inputs[3];
ed = inputs[4];
et = inputs[5];
callingAddressAreaCode = inputs[0].substring(0, 4);
answerAddressAreaCode = inputs[1].substring(0, 4);
} else if (inputs.length == 7) {
sd = inputs[3];
st = inputs[4];
ed = inputs[5];
et = inputs[6];
if (inputs[0].charAt(0) != '0') {
if (inputs[2].length() == 10) {
answerAddressAreaCode = inputs[2].substring(0, 3);
} else {
answerAddressAreaCode = inputs[2].substring(0, 4);
}
callingAddressAreaCode = inputs[1];
} else {
if (inputs[0].length() == 10) {
callingAddressAreaCode = inputs[0].substring(0, 3);
} else {
callingAddressAreaCode = inputs[0].substring(0, 4);
}
answerAddressAreaCode = inputs[2];
}
} else if (inputs.length == 8) {
sd = inputs[4];
st = inputs[5];
ed = inputs[6];
et = inputs[7];
callingAddressAreaCode = inputs[1];
answerAddressAreaCode = inputs[3];
}
} else if (type == 'm') {
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault());
try {
startTime = simpleDateFormat.parse(sd + " " + st);
endTime = simpleDateFormat.parse(ed + " " + et);
} catch (ParseException e) {
}
}
public CallRecord(Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) {
super();
this.startTime = startTime;
this.endTime = endTime;
this.callingAddressAreaCode = callingAddressAreaCode;
this.answerAddressAreaCode = answerAddressAreaCode;
}
public Date getStartTime() {
return startTime;
}
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
public Date getEndTime() {
return endTime;
}
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
public String getCallingAddressAreaCode() {
return callingAddressAreaCode;
}
public void setCallingAddressAreaCode(String callingAddressAreaCode) {
this.callingAddressAreaCode = callingAddressAreaCode;
}
public String getAnswerAddressAreaCode() {
return answerAddressAreaCode;
}
public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
this.answerAddressAreaCode = answerAddressAreaCode;
}
}
abstract class CallChargeRule extends ChargeRule {
}
class LandPhoneInCityRule extends CallChargeRule {
@Override
public double calCost(UserRecords userRecords) {
double sumCost = 0;
for (CallRecord call : userRecords.getCallingInCityRecords()) {
double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
if (distanceS < 0) {
continue;
}
double distanceM = (int) distanceS / 60;
if (distanceS % 60 != 0) {
distanceM += 1;
}
if (call.getCallType().equals("11")) {
sumCost += distanceM * 0.1;
} else if (call.getCallType().equals("12")) {
sumCost += distanceM * 0.3;
} else if (call.getCallType().equals("13")) {
sumCost += distanceM * 0.6;
}
}
return sumCost;
}
}
class LandPhoneInlandRule extends CallChargeRule {
@Override
public double calCost(UserRecords userRecords) {
double sumCost = 0;
for (CallRecord call : userRecords.getCallingInLandRecords()) {
double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
if (distanceS < 0) {
continue;
}
double distanceM = (int) distanceS / 60;
if (distanceS % 60 != 0) {
distanceM += 1;
}
sumCost += distanceM * 0.6;
}
return sumCost;
}
}
class LandPhoneInProvinceRule extends CallChargeRule {
@Override
public double calCost(UserRecords userRecords) {
double sumCost = 0;
for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
if (distanceS < 0) {
continue;
}
double distanceM = (int) distanceS / 60;
if (distanceS % 60 != 0) {
distanceM += 1;
}
sumCost += distanceM * 0.3;
}
return sumCost;
}
}
class MobilePhoneInCityRule extends CallChargeRule {
@Override
public double calCost(UserRecords userRecords) {
double sumCost = 0;
for (CallRecord call : userRecords.getCallingInCityRecords()) {
double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
if (distanceS < 0) {
continue;
}
double distanceM = (int) distanceS / 60;
if (distanceS % 60 != 0) {
distanceM += 1;
}
if (call.getCallType().equals("11")) {
sumCost += distanceM * 0.1;
} else if (call.getCallType().equals("12")) {
sumCost += distanceM * 0.2;
} else if (call.getCallType().equals("13")) {
sumCost += distanceM * 0.3;
}
}
return sumCost;
}
}
class MobilePhoneInlandRule extends CallChargeRule {
@Override
public double calCost(UserRecords userRecords) {
double sumCost = 0;
for (CallRecord call : userRecords.getCallingInLandRecords()) {
double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
if (distanceS < 0) {
continue;
}
double distanceM = (int) distanceS / 60;
if (distanceS % 60 != 0) {
distanceM += 1;
}
sumCost += distanceM * 0.6;
}
for (CallRecord call : userRecords.getAnswerInLandRecords()) {
double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
if (distanceS < 0) {
continue;
}
double distanceM = (int) distanceS / 60;
if (distanceS % 60 != 0) {
distanceM += 1;
}
sumCost += distanceM * 0.3;
}
return sumCost;
}
}
class MobilePhoneInProvinceRule extends CallChargeRule {
@Override
public double calCost(UserRecords userRecords) {
double sumCost = 0;
for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
if (distanceS < 0) {
continue;
}
double distanceM = (int) distanceS / 60;
if (distanceS % 60 != 0) {
distanceM += 1;
}
if (call.getCallType().equals("21")) {
sumCost += distanceM * 0.3;
} else if (call.getCallType().equals("22")) {
sumCost += distanceM * 0.3;
} else if (call.getCallType().equals("23")) {
sumCost += distanceM * 0.3;
}
}
return sumCost;
}
}
class MobilePhoneMessageRule extends CallChargeRule {
@Override
public double calCost(UserRecords userRecords) {
double sumCost = 0;
int number = 0;
for (MessageRecord m : userRecords.getSendMessageRecords()) {
int length = m.getMessage().length();
if (length <= 10) {
number++;
} else {
number += length / 10;
if (length % 10 != 0) {
number++;
}
}
}
if (number <= 3) {
sumCost = number * 0.1;
} else if (number <= 5) {
sumCost = 0.3 + 0.2 * (number - 3);
} else {
sumCost = 0.7 + 0.3 * (number - 5);
}
return sumCost;
}
}
class MessageRecord extends CommunicationRecord {
private String message;
public MessageRecord(String input) {
super();
this.message = input.substring(26);
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
class User {
private UserRecords userRecords = new UserRecords();
private double balance = 100;
private ChargeMode chargeMode;
private String number;
public double calCost() {
return chargeMode.calCost(userRecords);
}
public double calBalance() {
return balance - chargeMode.getMonthlyRent() - chargeMode.calCost(userRecords);
}
public UserRecords getUserRecords() {
return userRecords;
}
public void setUserRecords(UserRecords userRecords) {
this.userRecords = userRecords;
}
public ChargeMode getChargeMode() {
return chargeMode;
}
public void setChargeMode(ChargeMode chargeMode) {
this.chargeMode = chargeMode;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
class Outputtool {
@SuppressWarnings("deprecation")
public void output(double out) {
BigDecimal numb = new BigDecimal(out);
out = numb.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
System.out.print(out);
}
}
类图
生成报表
解释与心得
题目比较简单,有了前两次作业的经验,根据类图实现即可,计费规则也很简单只有一条,格式判断相较于上次也很简单,注意细节即可。
三、踩坑心得
这三次电信计费练习涉及了许多输入有效性的判断。也卡了较长时间。在第一题时,比较专注于时间输入有效性的判断,对SimpleDateFormat类的不了解也导致第一题使用了错误的方法判断的时间,对其余部分的格式判断也简单判断,没有使用正则表达式判断,导致座机计费格式判断并不全面。在手机+座机计费这一题时,在学会使用SimpleDateFormat类进行时间判断后,由于第一题对通话记录的判断集中于号码位数的判断,未对号码字符是否是数字进行判断导致花费了很多时间,最终使用正则表达式进行了前面的号码的判断之后还剩余两分没有获得,反复尝试后发现是时间输入格式不符合导致的,不了解什么样的输入可以被SimpleDateFormat转化却不符合yyyy.MM.dd HH:mm:ss。第三次短信收费计算的时候有了前两次的总结,写的时候就比较顺利了。
四、改进建议
这次的Main函数的代码太过于长了,导致很不好寻找,思路不清晰,耦合度有点高。
在设计架构方面,笔者感觉第二单元的架构会比第一单元清晰。第一单元每次作业笔者都进行了重构,而且每次都感觉是在面向过程编程,而第二单元后两次作业沿用的都是第一次的架构,并且第二单元完成三次作业的时间相比第一单元少很多,每次作业平均花费时间只有第一单元每次作业的一半左右。在正确性上,笔者实际上没有对自己第二单元的作业进行较多测试,因此,再次感觉一个良好的架构对编程的效率和正确性都提供了很好的保障。
五、总结
对于自己写的程序来说bug还是有很多的,但形成的原因也是有很多的,比如类的混乱,还有随机数的产生导致有些点可以过,但随机数可能使那些点不能过。还有那些正则表达式超级长,看到就脑阔疼,今后我个人会尽量避免写出这样的代码,除非真的万不得已。
当然在写第五次大作业的时候7-1中的第3次情况写了一大段,但是当运行的时候就不能运行,一开始以为时是单纯的进不了循环,当测试几个样例的时候发现可以进入,那个时候就快崩溃了,当时代码写的太杂了,而且都是很混乱的,导致自己根本改不好,只能无奈的全删了,去骗分。当时就后悔了,应该讲那些代码写进一个类,在类里面进行判断,这样代码既不会写错,到时候改的时候也会更清晰明了一点。不会像这样气急败坏的删除了。
面向对象不一定比面向过程,面向结果优越,但是优秀的封装和类一定好太多了,第三次作业能写满分,我相信他的封装和代码一定比别人更清晰明了,也更容易看的懂。
标签:inputs,return,第三次,double,ArrayList,博客,public,String From: https://www.cnblogs.com/1127sj/p/16971574.html