實(shí)現(xiàn)一個(gè)簡單的電信計(jì)費(fèi)程序:
假設(shè)南昌市電信分公司針對市內(nèi)座機(jī)用戶采用的計(jì)費(fèi)方式:
月租20元,接電話免費(fèi),市內(nèi)撥打電話0.1元/分鐘,省內(nèi)長途0.3元/分鐘,國內(nèi)長途撥打0.6元/分鐘。不足一分鐘按一分鐘計(jì)。
南昌市的區(qū)號(hào):0791,江西省內(nèi)各地市區(qū)號(hào)包括:0790~0799以及0701。
輸入格式:
輸入信息包括兩種類型
1、逐行輸入南昌市用戶開戶的信息,每行一個(gè)用戶,
格式:u-號(hào)碼 計(jì)費(fèi)類型 (計(jì)費(fèi)類型包括:0-座機(jī) 1-手機(jī)實(shí)時(shí)計(jì)費(fèi) 2-手機(jī)A套餐)
例如:u-079186300001 0
座機(jī)號(hào)碼除區(qū)號(hào)外由是7-8位數(shù)字組成。
本題只考慮計(jì)費(fèi)類型0-座機(jī)計(jì)費(fèi),電信系列2、3題會(huì)逐步增加計(jì)費(fèi)類型。
2、逐行輸入本月某些用戶的通訊信息,通訊信息格式:
座機(jī)呼叫座機(jī):t-主叫號(hào)碼 接聽號(hào)碼 起始時(shí)間 結(jié)束時(shí)間
t-079186330022 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:11
以上四項(xiàng)內(nèi)容之間以一個(gè)英文空格分隔,
時(shí)間必須符合"yyyy.MM.dd HH:mm:ss"格式。提示:使用SimpleDateFormat類。
以上兩類信息,先輸入所有開戶信息,再輸入所有通訊信息,最后一行以“end”結(jié)束。
注意:
本題非法輸入只做格式非法的判斷,不做內(nèi)容是否合理的判斷(時(shí)間除外,否則無法計(jì)算),比如:
1、輸入的所有通訊信息均認(rèn)為是同一個(gè)月的通訊信息,不做日期是否在同一個(gè)月還是多個(gè)月的判定,直接將通訊費(fèi)用累加,因此月租只計(jì)算一次。
2、記錄中如果同一電話號(hào)碼的多條通話記錄時(shí)間出現(xiàn)重合,這種情況也不做判斷,直接 計(jì)算每條記錄的費(fèi)用并累加。
3、用戶區(qū)號(hào)不為南昌市的區(qū)號(hào)也作為正常用戶處理。
輸出格式:
根據(jù)輸入的詳細(xì)通訊信息,計(jì)算所有已開戶的用戶的當(dāng)月費(fèi)用(精確到小數(shù)點(diǎn)后2位,
單位元)。假設(shè)每個(gè)用戶初始余額是100元。
每條通訊信息單獨(dú)計(jì)費(fèi)后累加,不是將所有時(shí)間累計(jì)后統(tǒng)一計(jì)費(fèi)。
格式:號(hào)碼+英文空格符+總的話費(fèi)+英文空格符+余額
每個(gè)用戶一行,用戶之間按號(hào)碼字符從小到大排序。
錯(cuò)誤處理:
輸入數(shù)據(jù)中出現(xiàn)的不符合格式要求的行一律忽略。
建議類圖:
參見圖1、2、3,可根據(jù)理解自行調(diào)整:
![image.png]()
圖1
圖1中User是用戶類,包括屬性:
userRecords (用戶記錄)、balance(余額)、chargeMode(計(jì)費(fèi)方式)、number(號(hào)碼)。
ChargeMode是計(jì)費(fèi)方式的抽象類:
chargeRules是計(jì)費(fèi)方式所包含的各種計(jì)費(fèi)規(guī)則的集合,ChargeRule類的定義見圖3。
getMonthlyRent()方法用于返回月租(monthlyRent)。
UserRecords是用戶記錄類,保存用戶各種通話、短信的記錄,
各種計(jì)費(fèi)規(guī)則將使用其中的部分或者全部記錄。
其屬性從上到下依次是:
市內(nèi)撥打電話、省內(nèi)(不含市內(nèi))撥打電話、省外撥打電話、
市內(nèi)接聽電話、省內(nèi)(不含市內(nèi))接聽電話、省外接聽電話的記錄
以及發(fā)送短信、接收短信的記錄。
![image.png]()
圖2
圖2中CommunicationRecord是抽象的通訊記錄類:
包含callingNumber撥打號(hào)碼、answerNumber接聽號(hào)碼兩個(gè)屬性。
CallRecord(通話記錄)、MessageRecord(短信記錄)是它的子類。
CallRecord(通話記錄類)包含屬性:
通話的起始、結(jié)束時(shí)間以及
撥號(hào)地點(diǎn)的區(qū)號(hào)(callingAddressAreaCode)、接聽地點(diǎn)的區(qū)號(hào)(answerAddressAreaCode)。
區(qū)號(hào)用于記錄在哪個(gè)地點(diǎn)撥打和接聽的電話。座機(jī)無法移動(dòng),就是本機(jī)區(qū)號(hào),如果是手機(jī)號(hào),則會(huì)有差異。
![image.png]()
圖3
圖3是計(jì)費(fèi)規(guī)則的相關(guān)類,這些類的核心方法是:
calCost(ArrayList<CallRecord> callRecords)。
該方法針根據(jù)輸入?yún)?shù)callRecords中的所有記錄計(jì)算某用戶的某一項(xiàng)費(fèi)用;如市話費(fèi)。
輸入?yún)?shù)callRecords的約束條件:必須是某一個(gè)用戶的符合計(jì)費(fèi)規(guī)則要求的所有記錄。
LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三個(gè)類分別是
座機(jī)撥打市內(nèi)、省內(nèi)、省外電話的計(jì)費(fèi)規(guī)則類,用于實(shí)現(xiàn)這三種情況的費(fèi)用計(jì)算。
(提示:可以從UserRecords類中獲取各種類型的callRecords)。
后續(xù)擴(kuò)展說明:
后續(xù)題目集將增加手機(jī)用戶,手機(jī)用戶的計(jì)費(fèi)方式中除了與座機(jī)計(jì)費(fèi)類似的主叫通話費(fèi)之外,還包含市外接聽電話的漫游費(fèi)以及發(fā)短信的費(fèi)用。在本題的設(shè)計(jì)時(shí)可統(tǒng)一考慮。
通話記錄中,手機(jī)需要額外記錄撥打/接聽的地點(diǎn)的區(qū)號(hào),比如:
座機(jī)打手機(jī):t-主叫號(hào)碼 接聽號(hào)碼 接聽地點(diǎn)區(qū)號(hào) 起始時(shí)間 結(jié)束時(shí)間
t-079186330022 13305862264 020 2022.1.3 10:00:25 2022.1.3 10:05:11
手機(jī)互打:t-主叫號(hào)碼 撥號(hào)地點(diǎn) 接聽號(hào)碼 接聽地點(diǎn)區(qū)號(hào) 起始時(shí)間 結(jié)束時(shí)間
t-18907910010 0791 13305862264 0371 2022.1.3 10:00:25 2022.1.3 10:05:11
短信的格式:m-主叫號(hào)碼,接收號(hào)碼,短信內(nèi)容
m-18907910010 13305862264 welcome to jiangxi
m-13305862264 18907910010 thank you
輸入樣例:
在這里給出一組輸入。例如:
u-079186300001 0
t-079186300001 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:25
end
輸出樣例:
在這里給出相應(yīng)的輸出。例如:
079186300001 3.0 77.0
部分代碼:
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 Inputdeal {
public int check(String input) {
String[] inputs = input.split(" ");
if (inputs.length == 2) {
if (inputs[0].matches("^u-[0-9]{11,13}$")) {
if (Integer.parseInt(inputs[1]) >= 0) {
if (Integer.parseInt(inputs[1]) <= 2) {
return 1;
}
}
}
} else if (inputs.length == 6) {
// if(input.matches("[t]-0791[0-9]{7,8}\\s" + "0[0-9]{9,11}\\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;
// }
if (validate(inputs[2]))
if (validate(inputs[4]))
if (validatet(inputs[3]))
if (validatet(inputs[5]))
// if (inputs[0].matches("^t\\-[0-9]{10,12}$")) {
if (inputs[0].matches("[t]-0791[0-9]{7,8}")) {
if (inputs[1].matches(".[0-9]{9,11}"))
return 2;
}
}
return 0;
}
private boolean validatet(String string) {
String[] split = string.split(":");
if (!string.matches("^([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$")) {
return false;
}
return true;
}
public static boolean validate(String dateString) {
// 使用正則表達(dá)式 測試 字符 符合 dddd.dd.dd 的格式(d表示數(shù)字)
Pattern p = Pattern.compile("\\d{4}+[\\.]\\d{1,2}+[\\.]\\d{1,2}+");
Matcher m = p.matcher(dateString);
if (!m.matches()) {
return false;
}
// 得到年月日
String[] array = dateString.split("\\.");
int year = Integer.valueOf(array[0]);
int month = Integer.valueOf(array[1]);
int day = Integer.valueOf(array[2]);
if (month < 1 || month > 12) {
return false;
}
int[] monthLengths = new int[] { 0, 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (isLeapYear(year)) {
monthLengths[2] = 29;
} else {
monthLengths[2] = 28;
}
int monthLength = monthLengths[month];
if (day < 1 || day > monthLength) {
return false;
}
return true;
}
/** 是否是閏年 */
private static boolean isLeapYear(int year) {
return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
}
public boolean judge(String input) {
return false;
}
public void writeUser(ArrayList<User> users, String input) {
User user = new User();
String[] inputs = input.split(" ");
String num = inputs[0].substring(2);
for (User i : users) {
if (i.getNumber().equals(num)) {
return;
}
}
user.setNumber(num);
int mode = Integer.parseInt(inputs[1]);
if (mode == 0) {
user.setChargeMode(new LandlinePhoneCharging());
}
users.add(user);
}
public void writeRecord(ArrayList<User> users, String input) {
String[] inputs = input.split(" ");
inputs[0] = inputs[0].replace("t-", "");
User callu = null, answeru = null;
CallRecord callrecord = new CallRecord(inputs);
for (User i : users) {
if (i.getNumber().equals(inputs[0])) {
callu = i;
}
if (i.getNumber().equals(inputs[1])) {
answeru = i;
}
if (callu != null && answeru != null) {
break;
}
}
if (callu != null) {
if (callrecord.getCallType() == 1) {
callu.getUserRecords().addCallingInCityRecords(callrecord);
} else if (callrecord.getCallType() == 2) {
callu.getUserRecords().addCallingInProvinceRecords(callrecord);
} else {
callu.getUserRecords().addCallingInLandRecords(callrecord);
}
}
if (answeru != null) {
if (callrecord.getCallType() == 1) {
answeru.getUserRecords().addAnswerInCityRecords(callrecord);
} else if (callrecord.getCallType() == 2) {
answeru.getUserRecords().aaddAnswerInProvinceRecords(callrecord);
} else {
answeru.getUserRecords().addAnswerInLandRecords(callrecord);
}
}
}
}
abstract class CommunicationRecord {
protected String callingNumber;
protected String answerNumber;
public String getCallingNumber() {
return callingNumber;
}
public void setCallingNumber(String callingNumber) {
this.callingNumber = callingNumber;
}
public String getAnswerNumbe() {
return answerNumber;
}
public void setAnswerNumbe(String answerNumbe) {
this.answerNumber = answerNumbe;
}
}
abstract class ChargeRule {
abstract public double calCost(UserRecords userRecords);
}
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 Inputdeal {
public int check(String input) {
String[] inputs = input.split(" ");
if (inputs.length == 2) {
if (inputs[0].matches("^u-[0-9]{11,13}$")) {
if (Integer.parseInt(inputs[1]) >= 0) {
if (Integer.parseInt(inputs[1]) <= 2) {
return 1;
}
}
}
} else if (inputs.length == 6) {
// if(input.matches("[t]-0791[0-9]{7,8}\\s" + "0[0-9]{9,11}\\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;
// }
if (validate(inputs[2]))
if (validate(inputs[4]))
if (validatet(inputs[3]))
if (validatet(inputs[5]))
// if (inputs[0].matches("^t\\-[0-9]{10,12}$")) {
if (inputs[0].matches("[t]-0791[0-9]{7,8}")) {
if (inputs[1].matches(".[0-9]{9,11}"))
return 2;
}
}
return 0;
}
private boolean validatet(String string) {
String[] split = string.split(":");
if (!string.matches("^([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$")) {
return false;
}
return true;
}
public static boolean validate(String dateString) {
// 使用正則表達(dá)式 測試 字符 符合 dddd.dd.dd 的格式(d表示數(shù)字)
Pattern p = Pattern.compile("\\d{4}+[\\.]\\d{1,2}+[\\.]\\d{1,2}+");
Matcher m = p.matcher(dateString);
if (!m.matches()) {
return false;
}
// 得到年月日
String[] array = dateString.split("\\.");
int year = Integer.valueOf(array[0]);
int month = Integer.valueOf(array[1]);
int day = Integer.valueOf(array[2]);
if (month < 1 || month > 12) {
return false;
}
int[] monthLengths = new int[] { 0, 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (isLeapYear(year)) {
monthLengths[2] = 29;
} else {
monthLengths[2] = 28;
}
int monthLength = monthLengths[month];
if (day < 1 || day > monthLength) {
return false;
}
return true;
}
/** 是否是閏年 */
private static boolean isLeapYear(int year) {
return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
}
public boolean judge(String input) {
return false;
}
public void writeUser(ArrayList<User> users, String input) {
User user = new User();
String[] inputs = input.split(" ");
String num = inputs[0].substring(2);
for (User i : users) {
if (i.getNumber().equals(num)) {
return;
}
}
user.setNumber(num);
int mode = Integer.parseInt(inputs[1]);
if (mode == 0) {
user.setChargeMode(new LandlinePhoneCharging());
}
users.add(user);
}
public void writeRecord(ArrayList<User> users, String input) {
String[] inputs = input.split(" ");
inputs[0] = inputs[0].replace("t-", "");
User callu = null, answeru = null;
CallRecord callrecord = new CallRecord(inputs);
for (User i : users) {
if (i.getNumber().equals(inputs[0])) {
callu = i;
}
if (i.getNumber().equals(inputs[1])) {
answeru = i;
}
if (callu != null && answeru != null) {
break;
}
}
if (callu != null) {
if (callrecord.getCallType() == 1) {
callu.getUserRecords().addCallingInCityRecords(callrecord);
} else if (callrecord.getCallType() == 2) {
callu.getUserRecords().addCallingInProvinceRecords(callrecord);
} else {
callu.getUserRecords().addCallingInLandRecords(callrecord);
}
}
if (answeru != null) {
if (callrecord.getCallType() == 1) {
answeru.getUserRecords().addAnswerInCityRecords(callrecord);
} else if (callrecord.getCallType() == 2) {
answeru.getUserRecords().aaddAnswerInProvinceRecords(callrecord);
} else {
answeru.getUserRecords().addAnswerInLandRecords(callrecord);
}
}
}
}
abstract class CommunicationRecord {
protected String callingNumber;
protected String answerNumber;
public String getCallingNumber() {
return callingNumber;
}
public void setCallingNumber(String callingNumber) {
this.callingNumber = callingNumber;
}
public String getAnswerNumbe() {
return answerNumber;
}
public void setAnswerNumbe(String answerNumbe) {
this.answerNumber = answerNumbe;
}
}
abstract class ChargeRule {
abstract public double calCost(UserRecords userRecords);
}
圈復(fù)雜度:
![]()
踩坑心得:
- 圈復(fù)雜度處于合適范圍,整體代碼較好,這也和老師給的類圖有關(guān)。
- 自己的格式類有些笨拙,后面根據(jù)老師的類圖經(jīng)行了修改。
7-1 電信計(jì)費(fèi)系列2-手機(jī)+座機(jī)計(jì)費(fèi)
實(shí)現(xiàn)南昌市電信分公司的計(jì)費(fèi)程序,假設(shè)該公司針對手機(jī)和座機(jī)用戶分別采取了兩種計(jì)費(fèi)方案,分別如下:
1、針對市內(nèi)座機(jī)用戶采用的計(jì)費(fèi)方式(與電信計(jì)費(fèi)系列1內(nèi)容相同):
月租20元,接電話免費(fèi),市內(nèi)撥打電話0.1元/分鐘,省內(nèi)長途0.3元/分鐘,國內(nèi)長途撥打0.6元/分鐘。不足一分鐘按一分鐘計(jì)。
假設(shè)本市的區(qū)號(hào):0791,江西省內(nèi)各地市區(qū)號(hào)包括:0790~0799以及0701。
2、針對手機(jī)用戶采用實(shí)時(shí)計(jì)費(fèi)方式:
月租15元,市內(nèi)省內(nèi)接電話均免費(fèi),市內(nèi)撥打市內(nèi)電話0.1元/分鐘,市內(nèi)撥打省內(nèi)電話0.2元/分鐘,市內(nèi)撥打省外電話0.3元/分鐘,省內(nèi)漫游打電話0.3元/分鐘,省外漫游接聽0.3元/分鐘,省外漫游撥打0.6元/分鐘;
注:被叫電話屬于市內(nèi)、省內(nèi)還是國內(nèi)由被叫電話的接聽地點(diǎn)區(qū)號(hào)決定,比如以下案例中,南昌市手機(jī)用戶13307912264在區(qū)號(hào)為020的廣州接聽了電話,主叫號(hào)碼應(yīng)被計(jì)算為撥打了一個(gè)省外長途,同時(shí),手機(jī)用戶13307912264也要被計(jì)算省外接聽漫游費(fèi):
u-13307912264 1
t-079186330022 13307912264 020 2022.1.3 10:00:25 2022.1.3 10:05:11
輸入:
輸入信息包括兩種類型
1、逐行輸入南昌市用戶開戶的信息,每行一個(gè)用戶,含手機(jī)和座機(jī)用戶
格式:u-號(hào)碼 計(jì)費(fèi)類型 (計(jì)費(fèi)類型包括:0-座機(jī) 1-手機(jī)實(shí)時(shí)計(jì)費(fèi) 2-手機(jī)A套餐)
例如:u-079186300001 0
座機(jī)號(hào)碼由區(qū)號(hào)和電話號(hào)碼拼接而成,電話號(hào)碼包含7-8位數(shù)字,區(qū)號(hào)最高位是0。
手機(jī)號(hào)碼由11位數(shù)字構(gòu)成,最高位是1。
本題在電信計(jì)費(fèi)系列1基礎(chǔ)上增加類型1-手機(jī)實(shí)時(shí)計(jì)費(fèi)。
手機(jī)設(shè)置0或者座機(jī)設(shè)置成1,此種錯(cuò)誤可不做判斷。
2、逐行輸入本月某些用戶的通訊信息,通訊信息格式:
座機(jī)呼叫座機(jī):t-主叫號(hào)碼 接聽號(hào)碼 起始時(shí)間 結(jié)束時(shí)間
t-079186330022 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:11
以上四項(xiàng)內(nèi)容之間以一個(gè)英文空格分隔,
時(shí)間必須符合"yyyy.MM.dd HH:mm:ss"格式。提示:使用SimpleDateFormat類。
輸入格式增加手機(jī)接打電話以及收發(fā)短信的格式,手機(jī)接打電話的信息除了號(hào)碼之外需要額外記錄撥打/接聽的地點(diǎn)的區(qū)號(hào),比如:
座機(jī)打手機(jī):
t-主叫號(hào)碼 接聽號(hào)碼 接聽地點(diǎn)區(qū)號(hào) 起始時(shí)間 結(jié)束時(shí)間
t-079186330022 13305862264 020 2022.1.3 10:00:25 2022.1.3 10:05:11
手機(jī)互打:
t-主叫號(hào)碼 撥號(hào)地點(diǎn) 接聽號(hào)碼 接聽地點(diǎn)區(qū)號(hào) 起始時(shí)間 結(jié)束時(shí)間
t-18907910010 0791 13305862264 0371 2022.1.3 10:00:25 2022.1.3 10:05:11
注意:以上兩類信息,先輸入所有開戶信息,再輸入所有通訊信息,最后一行以“end”結(jié)束。
輸出:
根據(jù)輸入的詳細(xì)通訊信息,計(jì)算所有已開戶的用戶的當(dāng)月費(fèi)用(精確到小數(shù)點(diǎn)后2位,單位元)。假設(shè)每個(gè)用戶初始余額是100元。
每條通訊、短信信息均單獨(dú)計(jì)費(fèi)后累加,不是將所有信息累計(jì)后統(tǒng)一計(jì)費(fèi)。
格式:號(hào)碼+英文空格符+總的話費(fèi)+英文空格符+余額
每個(gè)用戶一行,用戶之間按號(hào)碼字符從小到大排序。
錯(cuò)誤處理:
輸入數(shù)據(jù)中出現(xiàn)的不符合格式要求的行一律忽略。
本題只做格式的錯(cuò)誤判斷,無需做內(nèi)容上不合理的判斷,比如同一個(gè)電話兩條通訊記錄的時(shí)間有重合、開戶號(hào)碼非南昌市的號(hào)碼等,此類情況都當(dāng)成正確的輸入計(jì)算。但時(shí)間的輸入必須符合要求,比如不能輸入2022.13.61 28:72:65。
建議類圖:
參見圖1、2、3:
![image.png]()
圖1
圖1中User是用戶類,包括屬性:
userRecords (用戶記錄)、balance(余額)、chargeMode(計(jì)費(fèi)方式)、number(號(hào)碼)。
ChargeMode是計(jì)費(fèi)方式的抽象類:
chargeRules是計(jì)費(fèi)方式所包含的各種計(jì)費(fèi)規(guī)則的集合,ChargeRule類的定義見圖3。
getMonthlyRent()方法用于返回月租(monthlyRent)。
UserRecords是用戶記錄類,保存用戶各種通話、短信的記錄,
各種計(jì)費(fèi)規(guī)則將使用其中的部分或者全部記錄。
其屬性從上到下依次是:
市內(nèi)撥打電話、省內(nèi)(不含市內(nèi))撥打電話、省外撥打電話、
市內(nèi)接聽電話、省內(nèi)(不含市內(nèi))接聽電話、省外接聽電話的記錄
以及發(fā)送短信、接收短信的記錄。
![image.png]()
圖2
圖2中CommunicationRecord是抽象的通訊記錄類:
包含callingNumber撥打號(hào)碼、answerNumber接聽號(hào)碼兩個(gè)屬性。
CallRecord(通話記錄)、MessageRecord(短信記錄)是它的子類。CallRecord(通話記錄類)包含屬性:
通話的起始、結(jié)束時(shí)間以及
撥號(hào)地點(diǎn)的區(qū)號(hào)(callingAddressAreaCode)、接聽地點(diǎn)的區(qū)號(hào)(answerAddressAreaCode)。
區(qū)號(hào)用于記錄在哪個(gè)地點(diǎn)撥打和接聽的電話。座機(jī)無法移動(dòng),就是本機(jī)區(qū)號(hào),如果是手機(jī)號(hào),則會(huì)有差異。
![image.png]()
圖3
圖3是計(jì)費(fèi)規(guī)則的相關(guān)類,這些類的核心方法是:
calCost(ArrayList<CallRecord> callRecords)。
該方法針根據(jù)輸入?yún)?shù)callRecords中的所有記錄計(jì)算某用戶的某一項(xiàng)費(fèi)用;如市話費(fèi)。
輸入?yún)?shù)callRecords的約束條件:必須是某一個(gè)用戶的符合計(jì)費(fèi)規(guī)則要求的所有記錄。
SendMessageRule是發(fā)送短信的計(jì)費(fèi)規(guī)則類,用于計(jì)算發(fā)送短信的費(fèi)用。
LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三個(gè)類分別是座機(jī)撥打市內(nèi)、省內(nèi)、省外電話的計(jì)費(fèi)規(guī)則類,用于實(shí)現(xiàn)這三種情況的費(fèi)用計(jì)算。
(提示:可以從UserRecords類中獲取各種類型的callRecords)。
注意:以上圖中所定義的類不是限定要求,根據(jù)實(shí)際需要自行補(bǔ)充或修改。
輸入樣例:
在這里給出一組輸入。例如:
u-13811111111 1
t-13811111111 0791 13811111110 020 2022.1.3 08:00:00 2022.1.3 08:09:20
end
輸出樣例:
在這里給出相應(yīng)的輸出。例如:
13811111111 3.0 82.0
部分代碼:
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;
}
}
圈復(fù)雜度:
![]()
踩坑心得:
圈復(fù)雜度不是很理想,判斷條件還是多了,在后續(xù)寫碼中需要更加注意。
改進(jìn)建議:
使用接口,繼承等來減少重復(fù)等,降低圈復(fù)雜度。
7-1 電信計(jì)費(fèi)系列3-短信計(jì)費(fèi)
實(shí)現(xiàn)一個(gè)簡單的電信計(jì)費(fèi)程序,針對手機(jī)的短信采用如下計(jì)費(fèi)方式:
1、接收短信免費(fèi),發(fā)送短信0.1元/條,超過3條0.2元/條,超過5條0.3元/條。
2、如果一次發(fā)送短信的字符數(shù)量超過10個(gè),按每10個(gè)字符一條短信進(jìn)行計(jì)算。
輸入:
輸入信息包括兩種類型
1、逐行輸入南昌市手機(jī)用戶開戶的信息,每行一個(gè)用戶。
格式:u-號(hào)碼 計(jì)費(fèi)類型 (計(jì)費(fèi)類型包括:0-座機(jī) 1-手機(jī)實(shí)時(shí)計(jì)費(fèi) 2-手機(jī)A套餐 3-手機(jī)短信計(jì)費(fèi))
例如:u-13305862264 3
座機(jī)號(hào)碼由區(qū)號(hào)和電話號(hào)碼拼接而成,電話號(hào)碼包含7-8位數(shù)字,區(qū)號(hào)最高位是0。
手機(jī)號(hào)碼由11位數(shù)字構(gòu)成,最高位是1。
本題只針對類型3-手機(jī)短信計(jì)費(fèi)。
2、逐行輸入本月某些用戶的短信信息,短信的格式:
m-主叫號(hào)碼,接收號(hào)碼,短信內(nèi)容 (短信內(nèi)容只能由數(shù)字、字母、空格、英文逗號(hào)、英文句號(hào)組成)
m-18907910010 13305862264 welcome to jiangxi.
m-13305862264 18907910010 thank you.
注意:以上兩類信息,先輸入所有開戶信息,再輸入所有通訊信息,最后一行以“end”結(jié)束。
輸出:
根據(jù)輸入的詳細(xì)短信信息,計(jì)算所有已開戶的用戶的當(dāng)月短信費(fèi)用(精確到小數(shù)點(diǎn)后2位,單位元)。假設(shè)每個(gè)用戶初始余額是100元。
每條短信信息均單獨(dú)計(jì)費(fèi)后累加,不是將所有信息累計(jì)后統(tǒng)一計(jì)費(fèi)。
格式:號(hào)碼+英文空格符+總的話費(fèi)+英文空格符+余額
每個(gè)用戶一行,用戶之間按號(hào)碼字符從小到大排序。
錯(cuò)誤處理:
輸入數(shù)據(jù)中出現(xiàn)的不符合格式要求的行一律忽略。
本題只做格式的錯(cuò)誤判斷,無需做內(nèi)容上不合理的判斷,比如同一個(gè)電話兩條通訊記錄的時(shí)間有重合、開戶號(hào)碼非南昌市的號(hào)碼、自己給自己打電話等,此類情況都當(dāng)成正確的輸入計(jì)算。但時(shí)間的輸入必須符合要求,比如不能輸入2022.13.61 28:72:65。
本題只考慮短信計(jì)費(fèi),不考慮通信費(fèi)用以及月租費(fèi)。
建議類圖:
參見圖1、2、3:
![image.png]()
圖1
圖1中User是用戶類,包括屬性:
userRecords (用戶記錄)、balance(余額)、chargeMode(計(jì)費(fèi)方式)、number(號(hào)碼)。
ChargeMode是計(jì)費(fèi)方式的抽象類:
chargeRules是計(jì)費(fèi)方式所包含的各種計(jì)費(fèi)規(guī)則的集合,ChargeRule類的定義見圖3。
getMonthlyRent()方法用于返回月租(monthlyRent)。
UserRecords是用戶記錄類,保存用戶各種通話、短信的記錄,
各種計(jì)費(fèi)規(guī)則將使用其中的部分或者全部記錄。
其屬性從上到下依次是:
市內(nèi)撥打電話、省內(nèi)(不含市內(nèi))撥打電話、省外撥打電話、
市內(nèi)接聽電話、省內(nèi)(不含市內(nèi))接聽電話、省外接聽電話的記錄
以及發(fā)送短信、接收短信的記錄。
![image.png]()
圖2
圖2中CommunicationRecord是抽象的通訊記錄類:
包含callingNumber撥打號(hào)碼、answerNumber接聽號(hào)碼兩個(gè)屬性。
CallRecord(通話記錄)、MessageRecord(短信記錄)是它的子類。
![image.png]()
圖3
圖3是計(jì)費(fèi)規(guī)則的相關(guān)類,這些類的核心方法是:
calCost(ArrayList callRecords)。
該方法針根據(jù)輸入?yún)?shù)callRecords中的所有記錄計(jì)算某用戶的某一項(xiàng)費(fèi)用;如市話費(fèi)。
輸入?yún)?shù)callRecords的約束條件:必須是某一個(gè)用戶的符合計(jì)費(fèi)規(guī)則要求的所有記錄。
SendMessageRule是發(fā)送短信的計(jì)費(fèi)規(guī)則類,用于計(jì)算發(fā)送短信的費(fèi)用。
LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三個(gè)類分別是座機(jī)撥打市內(nèi)、省內(nèi)、省外電話的計(jì)費(fèi)規(guī)則類,用于實(shí)現(xiàn)這三種情況的費(fèi)用計(jì)算。
(提示:可以從UserRecords類中獲取各種類型的callRecords)。
注意:以上圖中所定義的類不是限定要求,根據(jù)實(shí)際需要自行補(bǔ)充或修改。
輸入樣例:
在這里給出一組輸入。例如:
u-18907910010 3
m-18907910010 13305862264 aaaaaaaaaaaaaaaaaaaaaaa
end
輸出樣例:
在這里給出相應(yīng)的輸出。例如:
18907910010 0.3 99.7
### 輸入樣例1:
在這里給出一組輸入。例如:
u-18907910010 3
m-18907910010 13305862264 aaaaaaaaaaaa
m-18907910010 13305862264 aaaaaaa.
m-18907910010 13305862264 bb,bbbb
end
輸出樣例1:
在這里給出相應(yīng)的輸出。例如:
18907910010 0.5 99.5
部分代碼:
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;
}
}
圈復(fù)雜度:
![]()
踩坑心得:
圈復(fù)雜度不是很理想,判斷條件還是多了,在后續(xù)寫碼中需要更加注意。
改進(jìn)建議:
使用接口,繼承等來減少重復(fù)等,降低圈復(fù)雜度。
總結(jié):
通過這幾次的實(shí)驗(yàn),對于java的了解更加深入了,對于接口的掌握也更加牢固了,雖然這學(xué)期的學(xué)習(xí)就要結(jié)束了,但是我相信,在未來我也慧繼續(xù)學(xué)習(xí)并更好的使用java,java學(xué)習(xí)會(huì)一直伴隨著我,也感謝老師孜孜不倦的教誨,讓我由衷的喜歡上了這門課。