21201411-李英涵           

前言:这几次的作业较为简单,主要思路就是利用正则表达式来过滤掉不需要的信息。
 题量设置较为合理,比之前的多边形好做一些,应该是老师为了捞起来我们者对目前所学的Java知识有了更深一步的了解。所涉及到的内容将于下文引入并具体进行回顾。这三次的题目集涵盖了所学的许多方面,例如说为了储存用户信息并且灵活调用而使用的Map容器,又比如说为了匹配正确输入而使用的String中的match()加正则表达式。在类设计上,有着许多继承、抽象类的使用。
 
第六次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元。
每条通讯信息单独计费后累加,不是将所有时间累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。

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

类图:

输入样例:

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

u-079186300001 0
t-079186300001 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:25
end

输出样例:

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

079186300001 3.0 77.0
代码如下:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.Scanner;
import java.text.DecimalFormat;
public class Main {
public static void main(String[] arges) throws ParseException
{
Scanner in = new Scanner(System.in);
ArrayList<String> userDataS = new ArrayList<String>();
ArrayList<String> contactDataS = new ArrayList<String>();
String ss;
//录入所有信息并进行判断从而得到有用信息
while(true)
{
ss = in.nextLine();
if(ParseInput.IsLinePhone(ss)||ParseInput.IsPhone(ss))
{
boolean flag = true;
for(String c: userDataS){
if(c.equals(ss))
flag = false;
}
if(flag) userDataS.add(ss);
}
else if(ParseInput.IsLandPhoneToLandPhone(ss)||ParseInput.isLandPhoneToMobilePhone(ss)||
ParseInput.isMobilePhoneToLandPhone(ss)||ParseInput.isMobilePhoneToMobilePhone(ss)||ss.equals("end"))
{
break;
}
}
while(true)
{
if(ss.equals("end")) {
break;
}
if(ParseInput.IsLandPhoneToLandPhone(ss)||ParseInput.isLandPhoneToMobilePhone(ss)||
ParseInput.isMobilePhoneToLandPhone(ss)||ParseInput.isMobilePhoneToMobilePhone(ss))
{
contactDataS.add(ss);
}
ss = in.nextLine();
}
//对输出用户进行排序
userDataS.sort(Comparator.naturalOrder()); for(int i =0;i<userDataS.size(); i++)
{
User u = getUserData(userDataS.get(i),contactDataS);
System.out.print(u.getNumber()+" ");
System.out.print(OutFormat.doubleFormat(u.calCost())+" ");
System.out.println(OutFormat.doubleFormat(u.calBalance()));
}
} //开始为用户录入信息
public static User getUserData(String s , ArrayList<String> ss) throws ParseException//对每个用户的进行计算
{
User user = parseUserInput(s);
CallRecord[] callRecords = new CallRecord[ss.size()];
for(int i= 0; i<ss.size(); i++)
{
callRecords[i] = parseContactInput(ss.get(i));
if(callRecords[i] == null) continue;
if(callRecords[i].getCallingNumber().equals(user.getNumber()))
{
if(callRecords[i].getCallingAddressAreaCode().equals("NanChang"))
{
if(callRecords[i].getAnswerAddressAreaCode().equals("NanChang"))
{
user.getUserRecords().addCityCallingInCityRecords(callRecords[i]);
}
else if(callRecords[i].getAnswerAddressAreaCode().equals("JiangXi"))
{
user.getUserRecords().addCityCallingInProvinceRecords(callRecords[i]);
}
else if(callRecords[i].getAnswerAddressAreaCode().equals("China"))
{
user.getUserRecords().addCityCallingInLandRecords(callRecords[i]);
}
}
else if(callRecords[i].getCallingAddressAreaCode().equals("JiangXi")&&user.getNumber().matches("1[\\d]{10}"))
{
user.getUserRecords().addProvinceCallingRecords(callRecords[i]);
}
else if(callRecords[i].getCallingAddressAreaCode().equals("China")&&user.getNumber().matches("1[\\d]{10}"))
{
user.getUserRecords().addLandCallingRecords(callRecords[i]);
}
}
else if(callRecords[i].getAnswerNumber().equals(user.getNumber())&&user.getNumber().matches("1[\\d]{10}"))
{
if(callRecords[i].getAnswerAddressAreaCode().equals("China"))
{
user.getUserRecords().addanswerInLandRecords(callRecords[i]);
}
}
}
return user;
} public static User parseUserInput(String s)//对用户信息进行处理
{
s = s.substring(2);
String[] ss = s.split(" ");
int choice = -1; //初始化变量
if(ss[0].matches("0[\\d]{2,3}[\\d]{7,8}")) choice = 0;
else if(ss[0].length() == 11) choice = 1;
else if(ss[1] .equals("2")) choice = 2;
return new User(ss[0],choice);
} public static CallRecord parseContactInput(String s) throws ParseException //对通话信息进行处理
{
SimpleDateFormat another = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
s = s.substring(2);
String[] ss = s.split(" ");
if(ss.length == 6)
{
Date startTime = another.parse(ss[2]+" "+ss[3]);
Date endTime = another.parse(ss[4]+" "+ss[5]);
return new CallRecord(ss[0],ss[1],startTime,endTime);
}
else if(ss.length == 8)
{
Date startTime = another.parse(ss[4]+" "+ss[5]);
Date endTime = another.parse(ss[6]+" "+ss[7]);
return new CallRecord(ss[0],ss[1],ss[2],ss[3],startTime,endTime);
}
else if(ss.length == 7)
{
Date startTime = another.parse(ss[3]+" "+ss[4]);
Date endTime = another.parse(ss[5]+" "+ss[6]);
if(ss[1].matches("0[\\d]{2,3}"))
{
return new CallRecord(ss[0],ss[2],startTime,endTime,ss[1]);
}
else if(ss[2].matches("0[\\d]{2,3}"))
{
return new CallRecord(ss[0],ss[1],startTime,endTime,ss[2]);
}
}
return null; } }
abstract class CallChargeRule extends ChargeRule{
public abstract double calCost(ArrayList<CallRecord> callRecords); }
class CallRecord extends CommunicationRecord{
private Date startTime;
private Date endTime;
private String callingAddressAreaCode;
private String answerAddressAreaCode; //构造方法
public CallRecord(String callingNumber, String answerNumber ,Date startTime, Date endTime)
{
super(callingNumber,answerNumber);
this.startTime = startTime;
this.endTime = endTime;
//根据拨打电话确定拨打人地址
if(ParseLandlinePhoneInput.isInCity(callingNumber))
{
this.callingAddressAreaCode = "NanChang";
}
else if(ParseLandlinePhoneInput.isInProvince(callingNumber))
{
this.callingAddressAreaCode = "JiangXi";
}
else if(ParseLandlinePhoneInput.isInLand(callingNumber))
{
this.callingAddressAreaCode = "China";
}
//根据接听电话确定接听人地址
if(ParseLandlinePhoneInput.isInCity(answerNumber))
{
this.answerAddressAreaCode = "NanChang";
}
else if(ParseLandlinePhoneInput.isInProvince(answerNumber))
{
this.answerAddressAreaCode = "JiangXi";
}
else if(ParseLandlinePhoneInput.isInLand(answerNumber))
{
this.answerAddressAreaCode = "China";
}
} public CallRecord(String callingNumber,String callingAddressAreaCode,String answerNumber ,String answerAddressAreaCode,Date startTime, Date endTime)
{
super(callingNumber,answerNumber);
this.startTime = startTime;
this.endTime = endTime;
//根据拨打电话确定拨打人地址
if(callingAddressAreaCode.equals("0791"))
{
this.callingAddressAreaCode = "NanChang";
}
else if(callingAddressAreaCode.matches("(079[\\d]|0701)"))
{
this.callingAddressAreaCode = "JiangXi";
}
else if(callingAddressAreaCode.matches("0[\\d]{2,3}"))
{
this.callingAddressAreaCode = "China";
}
//根据接听电话确定接听人地址
if(answerAddressAreaCode.equals("0791"))
{
this.answerAddressAreaCode = "NanChang";
}
else if(answerAddressAreaCode.matches("(079[\\d]|0701)"))
{
this.answerAddressAreaCode = "JiangXi";
}
else if(answerAddressAreaCode.matches("0[\\d]{2,3}"))
{
this.answerAddressAreaCode = "China";
}
} public CallRecord(String callingNumber, String answerNumber ,Date startTime, Date endTime,String AddressAreaCode)
{
super(callingNumber,answerNumber);
this.startTime = startTime;
this.endTime = endTime;
//根据拨打电话确定拨打人地址
if(callingNumber.matches("0[\\d]{2,3}[\\d]{7,8}")&&answerNumber.matches("1[\\d]{10}")) //拨号人是座机
{
if(AddressAreaCode.equals("0791"))
{
this.answerAddressAreaCode = "NanChang";
}
else if(AddressAreaCode.matches("(079[\\d]|0701)"))
{
this.answerAddressAreaCode = "JiangXi";
}
else if(AddressAreaCode.matches("0[\\d]{2,3}"))
{
this.answerAddressAreaCode = "China";
}
if(ParseLandlinePhoneInput.isInCity(callingNumber))
{
this.callingAddressAreaCode = "NanChang";
}
else if(ParseLandlinePhoneInput.isInProvince(callingNumber))
{
this.callingAddressAreaCode = "JiangXi";
}
else if(ParseLandlinePhoneInput.isInLand(callingNumber))
{
this.callingAddressAreaCode = "China";
}
}
else if(answerNumber.matches("0[\\d]{2,3}[\\d]{7,8}")&&callingNumber.matches("1[\\d]{10}")) //拨号人是手机用户
{
if(AddressAreaCode.equals("0791"))
{
this.callingAddressAreaCode = "NanChang";
}
else if(AddressAreaCode.matches("(079[\\d]|0701)"))
{
this.callingAddressAreaCode = "JiangXi";
}
else if(AddressAreaCode.matches("0[\\d]{2,3}"))
{
this.callingAddressAreaCode = "China";
}
//根据接听电话确定接听人地址
if(ParseLandlinePhoneInput.isInCity(answerNumber))
{
this.answerAddressAreaCode = "NanChang";
}
else if(ParseLandlinePhoneInput.isInProvince(answerNumber))
{
this.answerAddressAreaCode = "JiangXi";
}
else if(ParseLandlinePhoneInput.isInLand(answerNumber))
{
this.answerAddressAreaCode = "China";
}
} } 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;
} @Override
public String getCallingNumber() {
return callingNumber;
}
@Override
public void setCallingNumber(String callingNumber) {
this.callingNumber = callingNumber; }
@Override
public String getAnswerNumber() {
return answerNumber;
}
@Override
public void setAnswerNumber(String answerNumber) {
this.answerNumber = answerNumber; } }
abstract class ChargeMode {
ArrayList<ChargeRule> changeRules = new ArrayList<ChargeRule>(); public abstract double calCost(UserRecords userRecords);
public abstract double getMonthlyRent(); //getter和setter方法
public ArrayList<ChargeRule> getChangeRules() {
return changeRules;
} public void setChangeRules(ArrayList<ChargeRule> changeRules) {
this.changeRules = changeRules;
} }
abstract class ChargeRule { }
abstract class CommunicationRecord {
public String callingNumber;
public String answerNumber; public CommunicationRecord(String callingNumber, String answerNumber) {
this.callingNumber = callingNumber;
this.answerNumber = answerNumber;
} public abstract String getCallingNumber(); public abstract void setCallingNumber(String callingNumber); public abstract String getAnswerNumber() ; public abstract void setAnswerNumber(String answerNumber) ; }
class LandlinePhoneCharging extends ChargeMode{
private double monthlyRent = 20; @Override
public double calCost(UserRecords userRecords) {
double cost1 = new LandPhoneInCityRule().calCost(userRecords.getCityCallingInCityRecords());
double cost2 = new LandPhoneInProvinceRule().calCost(userRecords.getCityCallingInProvinceRecords());
double cost3 = new LandPhoneInLandRule().calCost(userRecords.getCityCallingInLandRecords());
return (cost1 + cost2 + cost3);
} @Override
public double getMonthlyRent() {
return monthlyRent;
}
}
class LandPhoneInCityRule extends CallChargeRule{ @Override
public double calCost(ArrayList<CallRecord> callRecords) {
double cost = 0;
for(int i = 0; i< callRecords.size(); i++)
{
long seconds = parseTime.computationTime(callRecords.get(i).getStartTime() , callRecords.get(i).getEndTime());
if(seconds >= 0)
{
if(seconds % 60 ==0)
{
cost = cost + seconds/60 * 0.1;
}
else if(seconds % 60 !=0) //不足一分钟按一分钟记录
{
cost = cost + (seconds/60 + 1)*0.1;
}
}
}
return cost;
} }
class LandPhoneInLandRule extends CallChargeRule{ @Override
public double calCost(ArrayList<CallRecord> callRecords) {
double cost = 0;
for(int i = 0; i< callRecords.size(); i++)
{
long seconds = parseTime.computationTime(callRecords.get(i).getStartTime(),callRecords.get(i).getEndTime());
if(seconds >= 0)
{
if(seconds % 60 ==0)
{
cost = cost + seconds/60*0.6;
}
else if(seconds % 60 !=0) //不足一分钟按一分钟记录
{
cost = cost + (seconds/60+1)*0.6;
}
} }
return cost;
} }
class LandPhoneInProvinceRule extends CallChargeRule{ @Override
public double calCost(ArrayList<CallRecord> callRecords) {
double cost = 0;
for(int i = 0; i< callRecords.size(); i++)
{
long seconds = parseTime.computationTime(callRecords.get(i).getStartTime(),callRecords.get(i).getEndTime());
if(seconds >= 0)
{
if(seconds % 60 == 0)
{
cost = cost + seconds/60 * 0.3;
}
else if(seconds % 60 !=0) //不足一分钟按一分钟记录
{
cost = cost + (seconds/60 + 1)*0.3;
}
} }
return cost;
} }
class MessageRecord extends CommunicationRecord{ private String message; public MessageRecord(String callingNumber, String answerNumber ,String message) {
super(callingNumber, answerNumber);
this.message = message;
} public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} @Override
public String getCallingNumber( ) {
return callingNumber;
} @Override
public void setCallingNumber(String callingNumber) {
this.callingNumber = callingNumber;
} @Override
public String getAnswerNumber() {
return answerNumber;
} @Override
public void setAnswerNumber(String answerNumber) {
this.answerNumber = answerNumber; } }
class MobilePhoneAnswerInLand extends CallChargeRule{ @Override
public double calCost(ArrayList<CallRecord> callRecords) {
double cost = 0;
for(int i = 0; i< callRecords.size(); i++)
{
long seconds = parseTime.computationTime(callRecords.get(i).getStartTime() , callRecords.get(i).getEndTime());
if(seconds >= 0)
{
if(seconds % 60 ==0)
{
cost = cost + seconds/60 * 0.3;
}
else if(seconds % 60 !=0) //不足一分钟按一分钟记录
{
cost = cost + (seconds/60 + 1)*0.3;
}
}
}
return cost;
} }
class MobilePhoneCallingInLand extends CallChargeRule{
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double cost = 0;
for(int i = 0; i< callRecords.size(); i++)
{
long seconds = parseTime.computationTime(callRecords.get(i).getStartTime() , callRecords.get(i).getEndTime());
if(seconds >= 0)
{
if(seconds % 60 ==0)
{
cost = cost + seconds/60 * 0.6;
}
else if(seconds % 60 !=0) //不足一分钟按一分钟记录
{
cost = cost + (seconds/60 + 1)*0.6;
}
}
}
return cost;
} }
class MobilePhoneCallingInProvince extends CallChargeRule{
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double cost = 0;
for(int i = 0; i< callRecords.size(); i++)
{
long seconds = parseTime.computationTime(callRecords.get(i).getStartTime() , callRecords.get(i).getEndTime());
if(seconds >= 0)
{
if(seconds % 60 ==0)
{
cost = cost + seconds/60 * 0.3;
}
else if(seconds % 60 !=0) //不足一分钟按一分钟记录
{
cost = cost + (seconds/60 + 1)*0.3;
}
}
}
return cost;
}
}
class MobilePhoneCharging extends ChargeMode{ private double monthly = 15; @Override
public double calCost(UserRecords userRecords) {
double cost1 = new MobilePhoneCityToCity().calCost(userRecords.getCityCallingInCityRecords());
double cost2 = new MobilePhoneCityToProvince().calCost(userRecords.getCityCallingInProvinceRecords());
double cost3 = new MobilePhoneCityToLand().calCost(userRecords.getCityCallingInLandRecords());
double cost4 = new MobilePhoneCallingInProvince().calCost(userRecords.getProvinceCallingRecords());
double cost5 = new MobilePhoneCallingInLand().calCost(userRecords.getLandCallingRecords());
double cost6 = new MobilePhoneAnswerInLand().calCost(userRecords.getAnswerInLandRecords());
return cost1 + cost2 + cost3 + cost4 + cost5 + cost6;
} @Override
public double getMonthlyRent() {
return this.monthly;
} }
class MobilePhoneCityToCity extends CallChargeRule{ @Override
public double calCost(ArrayList<CallRecord> callRecords) {
double cost = 0;
for(int i = 0; i< callRecords.size(); i++)
{
long seconds = parseTime.computationTime(callRecords.get(i).getStartTime() , callRecords.get(i).getEndTime());
if(seconds >= 0)
{
if(seconds % 60 ==0)
{
cost = cost + seconds/60 * 0.1;
}
else if(seconds % 60 !=0) //不足一分钟按一分钟记录
{
cost = cost + (seconds/60 + 1)*0.1;
}
}
}
return cost;
} }
class MobilePhoneCityToLand extends CallChargeRule{ @Override
public double calCost(ArrayList<CallRecord> callRecords) {
double cost = 0;
for(int i = 0; i< callRecords.size(); i++)
{
long seconds = parseTime.computationTime(callRecords.get(i).getStartTime() , callRecords.get(i).getEndTime());
if(seconds >= 0)
{
if(seconds % 60 ==0)
{
cost = cost + seconds/60 * 0.3;
}
else if(seconds % 60 !=0) //不足一分钟按一分钟记录
{
cost = cost + (seconds/60 + 1)*0.3;
}
}
}
return cost;
} }
class MobilePhoneCityToProvince extends CallChargeRule{ @Override
public double calCost(ArrayList<CallRecord> callRecords) {
double cost = 0;
for(int i = 0; i< callRecords.size(); i++)
{
long seconds = parseTime.computationTime(callRecords.get(i).getStartTime() , callRecords.get(i).getEndTime());
if(seconds >= 0)
{
if(seconds % 60 ==0)
{
cost = cost + seconds/60 * 0.2;
}
else if(seconds % 60 !=0) //不足一分钟按一分钟记录
{
cost = cost + (seconds/60 + 1)*0.2;
}
}
}
return cost;
} }
class OutFormat {
//按要求格式化实数的输出。
public static Double doubleFormat(double b) {
DecimalFormat df = new DecimalFormat("#.00");
Double output = Double.valueOf(df.format(b));
return output;
}
}
class ParseInput { public static boolean IsLandPhoneToLandPhone(String s) {//判断是否符合座机打座机格式
if (s.matches("t-0[\\d]{2,3}[\\d]{7,8} "
+ "[\\d]{3,4}[\\d]{7,8} "
+ "[\\d]{4}.[\\d]{1,2}.[\\d]{1,2} [\\d]{2}:[\\d]{2}:[\\d]{2} "
+ "[\\d]{4}.[\\d]{1,2}.[\\d]{1,2} [\\d]{2}:[\\d]{2}:[\\d]{2}"))
{
String []str = s.split(" ");
String s1 = str[2] + " " + str[3];
String s2 = str[4] + " " + str[5];
if(parseTime.isValidDate(s1)&&parseTime.isValidDate(s2)&&s2.compareTo(s1) > 0)
return true;
else
return false;
}
else
return false;
} public static boolean isMobilePhoneToMobilePhone(String s)
{
if(s.matches("t-1[\\d]{10} [\\d]{3,4} "
+ "1[\\d]{10} [\\d]{3,4} "
+ "[\\d]{4}.[\\d]{1,2}.[\\d]{1,2} [\\d]{2}:[\\d]{2}:[\\d]{2} "
+ "[\\d]{4}.[\\d]{1,2}.[\\d]{1,2} [\\d]{2}:[\\d]{2}:[\\d]{2}"))
{
String []str = s.split(" ");
String s1 = str[4] + " " + str[5];
String s2 = str[6] + " " + str[7];
if(parseTime.isValidDate(s1)&&parseTime.isValidDate(s2)&&s2.compareTo(s1) > 0)
return true;
else
return false;
}
return false;
} public static boolean isMobilePhoneToLandPhone(String s)
{
if(s.matches("t-1[\\d]{10} [\\d]{3,4} "
+ "0[\\d]{2,3}[\\d]{7,8} "
+ "[\\d]{4}.[\\d]{1,2}.[\\d]{1,2} [\\d]{2}:[\\d]{2}:[\\d]{2} "
+ "[\\d]{4}.[\\d]{1,2}.[\\d]{1,2} [\\d]{2}:[\\d]{2}:[\\d]{2}"))
{
String []str = s.split(" ");
String s1 = str[3] + " " + str[4];
String s2 = str[5] + " " + str[6];
if(parseTime.isValidDate(s1)&&parseTime.isValidDate(s2)&&s2.compareTo(s1) > 0)
return true;
else
return false;
}
return false;
} public static boolean isLandPhoneToMobilePhone(String s)
{
if(s.matches("t-0[\\d]{2,3}[\\d]{7,8} "
+ "1[\\d]{10} [\\d]{3,4} "
+ "[\\d]{4}.[\\d]{1,2}.[\\d]{1,2} [\\d]{2}:[\\d]{2}:[\\d]{2} "
+ "[\\d]{4}.[\\d]{1,2}.[\\d]{1,2} [\\d]{2}:[\\d]{2}:[\\d]{2}"))
{
String []str = s.split(" ");
String s1 = str[3] + " " + str[4];
String s2 = str[5] + " " + str[6];
if(parseTime.isValidDate(s1)&&parseTime.isValidDate(s2)&&s2.compareTo(s1) > 0)
return true;
else
return false;
}
return false;
} public static boolean IsLinePhone(String s)
{
if(s.matches("u-0791[0-9]{7,8}[ ]0"))
{
return true;
}
return false;
} public static boolean IsPhone(String s)
{
if(s.matches("u-1[\\d]{10} [0|1|2]"))
{
return true;
}
return false;
} }
class ParseLandlinePhoneInput { //判断是否是在市内
public static boolean isInCity(String s)
{
if(s.matches("0791[\\d]{7,8}"))
{
return true;
}
return false;
} //判断是否是在省内
public static boolean isInProvince(String s)
{
if(s.matches("(079[\\d]|0701)[\\d]{7,8}")&&!isInCity(s))
{
return true;
}
return false;
} //判断是否是在国内
public static boolean isInLand(String s)
{
if(s.matches("0[\\d]{2,3}[\\d]{7,8}")&&!isInCity(s)&&!isInProvince(s))
{
return true;
}
return false;
} //判断是否符合座机用户信息格式
public static boolean isLandPhonerData(String s)
{
if(s.matches("u-0[\\d]{2,3}[\\d]{7,8} [0]"))
{
return true;
}
return false;
} }
class parseTime { public static long computationTime(Date startTime, Date endTime){
long diff = endTime.getTime() - startTime.getTime();
return diff / 1000 ;
} public static boolean isValidDate(String dateTime) {
if(dateTime == null ) {
return false;
}
DateFormat df = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
df.setLenient(false);//表示严格验证
try {
df.parse(dateTime);
} catch (ParseException e) {
return false;
}
return true;
}
}
class User {
UserRecords userRecords = new UserRecords();
ChargeMode chargeMode;
private double balance = 100;
private String number; public User(String number , int choice)
{
this.number = number;
if(choice == 0)
{
chargeMode = new LandlinePhoneCharging();
}
else if(choice == 1)
{
chargeMode = new MobilePhoneCharging();
}
else if(choice == 2)
{ } } public double calBalance()
{
return this.balance - chargeMode.getMonthlyRent() - this.calCost();
} public double calCost()
{
return chargeMode.calCost(userRecords);
} //getter和setter方法
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 double getBalance() {
return balance;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
} }
class UserRecords {
ArrayList<CallRecord> CityCallingInCityRecords = new ArrayList<CallRecord>();
ArrayList<CallRecord> CityCallingInProvinceRecords = new ArrayList<CallRecord>();
ArrayList<CallRecord> CityCallingInLandRecords = new ArrayList<CallRecord>();
ArrayList<CallRecord> ProvinceCallingRecords = new ArrayList<CallRecord>();
ArrayList<CallRecord> LandCallingRecords = new ArrayList<CallRecord>();
ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();
ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();
ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>(); public void addCityCallingInCityRecords(CallRecord callRecord)
{
CityCallingInCityRecords.add(callRecord);
} public void addCityCallingInProvinceRecords(CallRecord callRecord)
{
CityCallingInProvinceRecords.add(callRecord);
} public void addCityCallingInLandRecords(CallRecord callRecord)
{
CityCallingInLandRecords.add(callRecord);
} public void addProvinceCallingRecords(CallRecord callRecord)
{
ProvinceCallingRecords.add(callRecord);
} public void addLandCallingRecords(CallRecord callRecord)
{
LandCallingRecords.add(callRecord);
} public void addanswerInLandRecords(CallRecord callRecord)
{
answerInLandRecords.add(callRecord);
} public void addSendMessageRecords(MessageRecord sendMessageRecord)
{
sendMessageRecords.add(sendMessageRecord);
} public void addReceiveMessageRecords(MessageRecord receiveMessageRecord)
{
receiveMessageRecords.add(receiveMessageRecord);
} public ArrayList<CallRecord> getCityCallingInCityRecords() {
return CityCallingInCityRecords;
} public ArrayList<CallRecord> getCityCallingInProvinceRecords() {
return CityCallingInProvinceRecords;
} public ArrayList<CallRecord> getCityCallingInLandRecords() {
return CityCallingInLandRecords;
} public ArrayList<CallRecord> getProvinceCallingRecords() {
return ProvinceCallingRecords;
} public ArrayList<CallRecord> getLandCallingRecords() {
return LandCallingRecords;
} public ArrayList<CallRecord> getAnswerInLandRecords() {
return answerInLandRecords;
} public ArrayList<MessageRecord> getSendMessageRecords() {
return sendMessageRecords;
} public ArrayList<MessageRecord> getReceiveMessageRecords() {
return receiveMessageRecords;
} }

分析:这一次题目要求我们仅仅只针对座机对于座机的拨打与接受,以南昌市的座机作为背景,进行电信计费的模拟。题目对于计费有一定的条件限制,这也是我们需要注意的地方,然后,根据题目设计好了user类和需要判断日期和计算时间的类,还有记录通话记录的类、设备类作为座机的父类。然后再去按照输入输出去设计Main类里的一些判断和与其他类的交互,中途很多次添加了一些额外判断去忽略那些错误输入。

第七次

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元。
每条通讯、短信信息均单独计费后累加,不是将所有信息累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。
错误处理:
输入数据中出现的不符合格式要求的行一律忽略。

本题只做格式的错误判断,无需做内容上不合理的判断,比如同一个电话两条通讯记录的时间有重合、开户号码非南昌市的号码等,此类情况都当成正确的输入计算。但时间的输入必须符合要求,比如不能输入2022.13.61 28:72:65。

输入样例:

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

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

输出样例:

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

13811111111 3.0 82.0
代码如下:

  1 import java.text.ParseException;
2 import java.text.SimpleDateFormat;
3 import java.text.DateFormat;
4 import java.util.ArrayList;
5 import java.util.Comparator;
6 import java.util.Date;
7 import java.util.Scanner;
8 import java.text.DecimalFormat;
9 public class Main {
10 public static void main(String[] arges) throws ParseException
11 {
12 Scanner in = new Scanner(System.in);
13 ArrayList<String> userDataS = new ArrayList<String>();
14 ArrayList<String> contactDataS = new ArrayList<String>();
15 String ss;
16 //录入所有信息并进行判断从而得到有用信息
17 while(true)
18 {
19 ss = in.nextLine();
20 if(ParseInput.IsLinePhone(ss)||ParseInput.IsPhone(ss))
21 {
22 boolean flag = true;
23 for(String c: userDataS){
24 if(c.equals(ss))
25 flag = false;
26 }
27 if(flag) userDataS.add(ss);
28 }
29 else if(ParseInput.IsLandPhoneToLandPhone(ss)||ParseInput.isLandPhoneToMobilePhone(ss)||
30 ParseInput.isMobilePhoneToLandPhone(ss)||ParseInput.isMobilePhoneToMobilePhone(ss)||ss.equals("end"))
31 {
32 break;
33 }
34 }
35 while(true)
36 {
37 if(ss.equals("end")) {
38 break;
39 }
40 if(ParseInput.IsLandPhoneToLandPhone(ss)||ParseInput.isLandPhoneToMobilePhone(ss)||
41 ParseInput.isMobilePhoneToLandPhone(ss)||ParseInput.isMobilePhoneToMobilePhone(ss))
42 {
43 contactDataS.add(ss);
44 }
45 ss = in.nextLine();
46 }
47 //对输出用户进行排序
48 userDataS.sort(Comparator.naturalOrder());
49
50 for(int i =0;i<userDataS.size(); i++)
51 {
52 User u = getUserData(userDataS.get(i),contactDataS);
53 System.out.print(u.getNumber()+" ");
54 System.out.print(OutFormat.doubleFormat(u.calCost())+" ");
55 System.out.println(OutFormat.doubleFormat(u.calBalance()));
56 }
57 }
58
59 //开始为用户录入信息
60 public static User getUserData(String s , ArrayList<String> ss) throws ParseException//对每个用户的进行计算
61 {
62 User user = parseUserInput(s);
63 CallRecord[] callRecords = new CallRecord[ss.size()];
64 for(int i= 0; i<ss.size(); i++)
65 {
66 callRecords[i] = parseContactInput(ss.get(i));
67 if(callRecords[i] == null) continue;
68 if(callRecords[i].getCallingNumber().equals(user.getNumber()))
69 {
70 if(callRecords[i].getCallingAddressAreaCode().equals("NanChang"))
71 {
72 if(callRecords[i].getAnswerAddressAreaCode().equals("NanChang"))
73 {
74 user.getUserRecords().addCityCallingInCityRecords(callRecords[i]);
75 }
76 else if(callRecords[i].getAnswerAddressAreaCode().equals("JiangXi"))
77 {
78 user.getUserRecords().addCityCallingInProvinceRecords(callRecords[i]);
79 }
80 else if(callRecords[i].getAnswerAddressAreaCode().equals("China"))
81 {
82 user.getUserRecords().addCityCallingInLandRecords(callRecords[i]);
83 }
84 }
85 else if(callRecords[i].getCallingAddressAreaCode().equals("JiangXi")&&user.getNumber().matches("1[\\d]{10}"))
86 {
87 user.getUserRecords().addProvinceCallingRecords(callRecords[i]);
88 }
89 else if(callRecords[i].getCallingAddressAreaCode().equals("China")&&user.getNumber().matches("1[\\d]{10}"))
90 {
91 user.getUserRecords().addLandCallingRecords(callRecords[i]);
92 }
93 }
94 else if(callRecords[i].getAnswerNumber().equals(user.getNumber())&&user.getNumber().matches("1[\\d]{10}"))
95 {
96 if(callRecords[i].getAnswerAddressAreaCode().equals("China"))
97 {
98 user.getUserRecords().addanswerInLandRecords(callRecords[i]);
99 }
100 }
101 }
102 return user;
103 }
104
105 public static User parseUserInput(String s)//对用户信息进行处理
106 {
107 s = s.substring(2);
108 String[] ss = s.split(" ");
109 int choice = -1; //初始化变量
110 if(ss[0].matches("0[\\d]{2,3}[\\d]{7,8}")) choice = 0;
111 else if(ss[0].length() == 11) choice = 1;
112 else if(ss[1] .equals("2")) choice = 2;
113 return new User(ss[0],choice);
114 }
115
116 public static CallRecord parseContactInput(String s) throws ParseException //对通话信息进行处理
117 {
118 SimpleDateFormat another = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
119 s = s.substring(2);
120 String[] ss = s.split(" ");
121 if(ss.length == 6)
122 {
123 Date startTime = another.parse(ss[2]+" "+ss[3]);
124 Date endTime = another.parse(ss[4]+" "+ss[5]);
125 return new CallRecord(ss[0],ss[1],startTime,endTime);
126 }
127 else if(ss.length == 8)
128 {
129 Date startTime = another.parse(ss[4]+" "+ss[5]);
130 Date endTime = another.parse(ss[6]+" "+ss[7]);
131 return new CallRecord(ss[0],ss[1],ss[2],ss[3],startTime,endTime);
132 }
133 else if(ss.length == 7)
134 {
135 Date startTime = another.parse(ss[3]+" "+ss[4]);
136 Date endTime = another.parse(ss[5]+" "+ss[6]);
137 if(ss[1].matches("0[\\d]{2,3}"))
138 {
139 return new CallRecord(ss[0],ss[2],startTime,endTime,ss[1]);
140 }
141 else if(ss[2].matches("0[\\d]{2,3}"))
142 {
143 return new CallRecord(ss[0],ss[1],startTime,endTime,ss[2]);
144 }
145 }
146 return null;
147
148 }
149
150 }
151 abstract class CallChargeRule extends ChargeRule{
152 public abstract double calCost(ArrayList<CallRecord> callRecords);
153
154 }
155 class CallRecord extends CommunicationRecord{
156 private Date startTime;
157 private Date endTime;
158 private String callingAddressAreaCode;
159 private String answerAddressAreaCode;
160
161 //构造方法
162 public CallRecord(String callingNumber, String answerNumber ,Date startTime, Date endTime)
163 {
164 super(callingNumber,answerNumber);
165 this.startTime = startTime;
166 this.endTime = endTime;
167 //根据拨打电话确定拨打人地址
168 if(ParseLandlinePhoneInput.isInCity(callingNumber))
169 {
170 this.callingAddressAreaCode = "NanChang";
171 }
172 else if(ParseLandlinePhoneInput.isInProvince(callingNumber))
173 {
174 this.callingAddressAreaCode = "JiangXi";
175 }
176 else if(ParseLandlinePhoneInput.isInLand(callingNumber))
177 {
178 this.callingAddressAreaCode = "China";
179 }
180 //根据接听电话确定接听人地址
181 if(ParseLandlinePhoneInput.isInCity(answerNumber))
182 {
183 this.answerAddressAreaCode = "NanChang";
184 }
185 else if(ParseLandlinePhoneInput.isInProvince(answerNumber))
186 {
187 this.answerAddressAreaCode = "JiangXi";
188 }
189 else if(ParseLandlinePhoneInput.isInLand(answerNumber))
190 {
191 this.answerAddressAreaCode = "China";
192 }
193 }
194
195 public CallRecord(String callingNumber,String callingAddressAreaCode,String answerNumber ,String answerAddressAreaCode,Date startTime, Date endTime)
196 {
197 super(callingNumber,answerNumber);
198 this.startTime = startTime;
199 this.endTime = endTime;
200 //根据拨打电话确定拨打人地址
201 if(callingAddressAreaCode.equals("0791"))
202 {
203 this.callingAddressAreaCode = "NanChang";
204 }
205 else if(callingAddressAreaCode.matches("(079[\\d]|0701)"))
206 {
207 this.callingAddressAreaCode = "JiangXi";
208 }
209 else if(callingAddressAreaCode.matches("0[\\d]{2,3}"))
210 {
211 this.callingAddressAreaCode = "China";
212 }
213 //根据接听电话确定接听人地址
214 if(answerAddressAreaCode.equals("0791"))
215 {
216 this.answerAddressAreaCode = "NanChang";
217 }
218 else if(answerAddressAreaCode.matches("(079[\\d]|0701)"))
219 {
220 this.answerAddressAreaCode = "JiangXi";
221 }
222 else if(answerAddressAreaCode.matches("0[\\d]{2,3}"))
223 {
224 this.answerAddressAreaCode = "China";
225 }
226 }
227
228 public CallRecord(String callingNumber, String answerNumber ,Date startTime, Date endTime,String AddressAreaCode)
229 {
230 super(callingNumber,answerNumber);
231 this.startTime = startTime;
232 this.endTime = endTime;
233 //根据拨打电话确定拨打人地址
234 if(callingNumber.matches("0[\\d]{2,3}[\\d]{7,8}")&&answerNumber.matches("1[\\d]{10}")) //拨号人是座机
235 {
236 if(AddressAreaCode.equals("0791"))
237 {
238 this.answerAddressAreaCode = "NanChang";
239 }
240 else if(AddressAreaCode.matches("(079[\\d]|0701)"))
241 {
242 this.answerAddressAreaCode = "JiangXi";
243 }
244 else if(AddressAreaCode.matches("0[\\d]{2,3}"))
245 {
246 this.answerAddressAreaCode = "China";
247 }
248 if(ParseLandlinePhoneInput.isInCity(callingNumber))
249 {
250 this.callingAddressAreaCode = "NanChang";
251 }
252 else if(ParseLandlinePhoneInput.isInProvince(callingNumber))
253 {
254 this.callingAddressAreaCode = "JiangXi";
255 }
256 else if(ParseLandlinePhoneInput.isInLand(callingNumber))
257 {
258 this.callingAddressAreaCode = "China";
259 }
260 }
261 else if(answerNumber.matches("0[\\d]{2,3}[\\d]{7,8}")&&callingNumber.matches("1[\\d]{10}")) //拨号人是手机用户
262 {
263 if(AddressAreaCode.equals("0791"))
264 {
265 this.callingAddressAreaCode = "NanChang";
266 }
267 else if(AddressAreaCode.matches("(079[\\d]|0701)"))
268 {
269 this.callingAddressAreaCode = "JiangXi";
270 }
271 else if(AddressAreaCode.matches("0[\\d]{2,3}"))
272 {
273 this.callingAddressAreaCode = "China";
274 }
275 //根据接听电话确定接听人地址
276 if(ParseLandlinePhoneInput.isInCity(answerNumber))
277 {
278 this.answerAddressAreaCode = "NanChang";
279 }
280 else if(ParseLandlinePhoneInput.isInProvince(answerNumber))
281 {
282 this.answerAddressAreaCode = "JiangXi";
283 }
284 else if(ParseLandlinePhoneInput.isInLand(answerNumber))
285 {
286 this.answerAddressAreaCode = "China";
287 }
288 }
289
290 }
291
292 public Date getStartTime() {
293 return startTime;
294 }
295 public void setStartTime(Date startTime) {
296 this.startTime = startTime;
297 }
298 public Date getEndTime() {
299 return endTime;
300 }
301 public void setEndTime(Date endTime) {
302 this.endTime = endTime;
303 }
304 public String getCallingAddressAreaCode() {
305 return callingAddressAreaCode;
306 }
307 public void setCallingAddressAreaCode(String callingAddressAreaCode) {
308 this.callingAddressAreaCode = callingAddressAreaCode;
309 }
310 public String getAnswerAddressAreaCode() {
311 return answerAddressAreaCode;
312 }
313 public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
314 this.answerAddressAreaCode = answerAddressAreaCode;
315 }
316
317
318 @Override
319 public String getCallingNumber() {
320 return callingNumber;
321 }
322 @Override
323 public void setCallingNumber(String callingNumber) {
324 this.callingNumber = callingNumber;
325
326 }
327 @Override
328 public String getAnswerNumber() {
329 return answerNumber;
330 }
331 @Override
332 public void setAnswerNumber(String answerNumber) {
333 this.answerNumber = answerNumber;
334
335 }
336
337
338 }
339 abstract class ChargeMode {
340 ArrayList<ChargeRule> changeRules = new ArrayList<ChargeRule>();
341
342 public abstract double calCost(UserRecords userRecords);
343 public abstract double getMonthlyRent();
344
345 //getter和setter方法
346 public ArrayList<ChargeRule> getChangeRules() {
347 return changeRules;
348 }
349
350 public void setChangeRules(ArrayList<ChargeRule> changeRules) {
351 this.changeRules = changeRules;
352 }
353
354
355 }
356 abstract class ChargeRule {
357
358 }
359 abstract class CommunicationRecord {
360 public String callingNumber;
361 public String answerNumber;
362
363
364
365 public CommunicationRecord(String callingNumber, String answerNumber) {
366 this.callingNumber = callingNumber;
367 this.answerNumber = answerNumber;
368 }
369
370 public abstract String getCallingNumber();
371
372 public abstract void setCallingNumber(String callingNumber);
373
374 public abstract String getAnswerNumber() ;
375
376 public abstract void setAnswerNumber(String answerNumber) ;
377
378 }
379 class LandlinePhoneCharging extends ChargeMode{
380 private double monthlyRent = 20;
381
382 @Override
383 public double calCost(UserRecords userRecords) {
384 double cost1 = new LandPhoneInCityRule().calCost(userRecords.getCityCallingInCityRecords());
385 double cost2 = new LandPhoneInProvinceRule().calCost(userRecords.getCityCallingInProvinceRecords());
386 double cost3 = new LandPhoneInLandRule().calCost(userRecords.getCityCallingInLandRecords());
387 return (cost1 + cost2 + cost3);
388 }
389
390 @Override
391 public double getMonthlyRent() {
392 return monthlyRent;
393 }
394 }
395 class LandPhoneInCityRule extends CallChargeRule{
396
397 @Override
398 public double calCost(ArrayList<CallRecord> callRecords) {
399 double cost = 0;
400 for(int i = 0; i< callRecords.size(); i++)
401 {
402 long seconds = parseTime.computationTime(callRecords.get(i).getStartTime() , callRecords.get(i).getEndTime());
403 if(seconds >= 0)
404 {
405 if(seconds % 60 ==0)
406 {
407 cost = cost + seconds/60 * 0.1;
408 }
409 else if(seconds % 60 !=0) //不足一分钟按一分钟记录
410 {
411 cost = cost + (seconds/60 + 1)*0.1;
412 }
413 }
414 }
415 return cost;
416 }
417
418
419 }
420 class LandPhoneInLandRule extends CallChargeRule{
421
422 @Override
423 public double calCost(ArrayList<CallRecord> callRecords) {
424 double cost = 0;
425 for(int i = 0; i< callRecords.size(); i++)
426 {
427 long seconds = parseTime.computationTime(callRecords.get(i).getStartTime(),callRecords.get(i).getEndTime());
428 if(seconds >= 0)
429 {
430 if(seconds % 60 ==0)
431 {
432 cost = cost + seconds/60*0.6;
433 }
434 else if(seconds % 60 !=0) //不足一分钟按一分钟记录
435 {
436 cost = cost + (seconds/60+1)*0.6;
437 }
438 }
439
440 }
441 return cost;
442 }
443
444 }
445 class LandPhoneInProvinceRule extends CallChargeRule{
446
447 @Override
448 public double calCost(ArrayList<CallRecord> callRecords) {
449 double cost = 0;
450 for(int i = 0; i< callRecords.size(); i++)
451 {
452 long seconds = parseTime.computationTime(callRecords.get(i).getStartTime(),callRecords.get(i).getEndTime());
453 if(seconds >= 0)
454 {
455 if(seconds % 60 == 0)
456 {
457 cost = cost + seconds/60 * 0.3;
458 }
459 else if(seconds % 60 !=0) //不足一分钟按一分钟记录
460 {
461 cost = cost + (seconds/60 + 1)*0.3;
462 }
463 }
464
465 }
466 return cost;
467 }
468
469 }
470 class MessageRecord extends CommunicationRecord{
471
472 private String message;
473
474 public MessageRecord(String callingNumber, String answerNumber ,String message) {
475 super(callingNumber, answerNumber);
476 this.message = message;
477 }
478
479 public String getMessage() {
480 return message;
481 }
482
483 public void setMessage(String message) {
484 this.message = message;
485 }
486
487 @Override
488 public String getCallingNumber( ) {
489 return callingNumber;
490 }
491
492 @Override
493 public void setCallingNumber(String callingNumber) {
494 this.callingNumber = callingNumber;
495 }
496
497 @Override
498 public String getAnswerNumber() {
499 return answerNumber;
500 }
501
502 @Override
503 public void setAnswerNumber(String answerNumber) {
504 this.answerNumber = answerNumber;
505
506 }
507
508 }
509 class MobilePhoneAnswerInLand extends CallChargeRule{
510
511 @Override
512 public double calCost(ArrayList<CallRecord> callRecords) {
513 double cost = 0;
514 for(int i = 0; i< callRecords.size(); i++)
515 {
516 long seconds = parseTime.computationTime(callRecords.get(i).getStartTime() , callRecords.get(i).getEndTime());
517 if(seconds >= 0)
518 {
519 if(seconds % 60 ==0)
520 {
521 cost = cost + seconds/60 * 0.3;
522 }
523 else if(seconds % 60 !=0) //不足一分钟按一分钟记录
524 {
525 cost = cost + (seconds/60 + 1)*0.3;
526 }
527 }
528 }
529 return cost;
530 }
531
532 }
533 class MobilePhoneCallingInLand extends CallChargeRule{
534 @Override
535 public double calCost(ArrayList<CallRecord> callRecords) {
536 double cost = 0;
537 for(int i = 0; i< callRecords.size(); i++)
538 {
539 long seconds = parseTime.computationTime(callRecords.get(i).getStartTime() , callRecords.get(i).getEndTime());
540 if(seconds >= 0)
541 {
542 if(seconds % 60 ==0)
543 {
544 cost = cost + seconds/60 * 0.6;
545 }
546 else if(seconds % 60 !=0) //不足一分钟按一分钟记录
547 {
548 cost = cost + (seconds/60 + 1)*0.6;
549 }
550 }
551 }
552 return cost;
553 }
554
555 }
556 class MobilePhoneCallingInProvince extends CallChargeRule{
557 @Override
558 public double calCost(ArrayList<CallRecord> callRecords) {
559 double cost = 0;
560 for(int i = 0; i< callRecords.size(); i++)
561 {
562 long seconds = parseTime.computationTime(callRecords.get(i).getStartTime() , callRecords.get(i).getEndTime());
563 if(seconds >= 0)
564 {
565 if(seconds % 60 ==0)
566 {
567 cost = cost + seconds/60 * 0.3;
568 }
569 else if(seconds % 60 !=0) //不足一分钟按一分钟记录
570 {
571 cost = cost + (seconds/60 + 1)*0.3;
572 }
573 }
574 }
575 return cost;
576 }
577 }
578 class MobilePhoneCharging extends ChargeMode{
579
580 private double monthly = 15;
581
582 @Override
583 public double calCost(UserRecords userRecords) {
584 double cost1 = new MobilePhoneCityToCity().calCost(userRecords.getCityCallingInCityRecords());
585 double cost2 = new MobilePhoneCityToProvince().calCost(userRecords.getCityCallingInProvinceRecords());
586 double cost3 = new MobilePhoneCityToLand().calCost(userRecords.getCityCallingInLandRecords());
587 double cost4 = new MobilePhoneCallingInProvince().calCost(userRecords.getProvinceCallingRecords());
588 double cost5 = new MobilePhoneCallingInLand().calCost(userRecords.getLandCallingRecords());
589 double cost6 = new MobilePhoneAnswerInLand().calCost(userRecords.getAnswerInLandRecords());
590 return cost1 + cost2 + cost3 + cost4 + cost5 + cost6;
591 }
592
593 @Override
594 public double getMonthlyRent() {
595 return this.monthly;
596 }
597
598 }
599 class MobilePhoneCityToCity extends CallChargeRule{
600
601 @Override
602 public double calCost(ArrayList<CallRecord> callRecords) {
603 double cost = 0;
604 for(int i = 0; i< callRecords.size(); i++)
605 {
606 long seconds = parseTime.computationTime(callRecords.get(i).getStartTime() , callRecords.get(i).getEndTime());
607 if(seconds >= 0)
608 {
609 if(seconds % 60 ==0)
610 {
611 cost = cost + seconds/60 * 0.1;
612 }
613 else if(seconds % 60 !=0) //不足一分钟按一分钟记录
614 {
615 cost = cost + (seconds/60 + 1)*0.1;
616 }
617 }
618 }
619 return cost;
620 }
621
622
623 }
624 class MobilePhoneCityToLand extends CallChargeRule{
625
626 @Override
627 public double calCost(ArrayList<CallRecord> callRecords) {
628 double cost = 0;
629 for(int i = 0; i< callRecords.size(); i++)
630 {
631 long seconds = parseTime.computationTime(callRecords.get(i).getStartTime() , callRecords.get(i).getEndTime());
632 if(seconds >= 0)
633 {
634 if(seconds % 60 ==0)
635 {
636 cost = cost + seconds/60 * 0.3;
637 }
638 else if(seconds % 60 !=0) //不足一分钟按一分钟记录
639 {
640 cost = cost + (seconds/60 + 1)*0.3;
641 }
642 }
643 }
644 return cost;
645 }
646
647 }
648 class MobilePhoneCityToProvince extends CallChargeRule{
649
650 @Override
651 public double calCost(ArrayList<CallRecord> callRecords) {
652 double cost = 0;
653 for(int i = 0; i< callRecords.size(); i++)
654 {
655 long seconds = parseTime.computationTime(callRecords.get(i).getStartTime() , callRecords.get(i).getEndTime());
656 if(seconds >= 0)
657 {
658 if(seconds % 60 ==0)
659 {
660 cost = cost + seconds/60 * 0.2;
661 }
662 else if(seconds % 60 !=0) //不足一分钟按一分钟记录
663 {
664 cost = cost + (seconds/60 + 1)*0.2;
665 }
666 }
667 }
668 return cost;
669 }
670
671
672 }
673 class OutFormat {
674 //按要求格式化实数的输出。
675 public static Double doubleFormat(double b) {
676 DecimalFormat df = new DecimalFormat("#.00");
677 Double output = Double.valueOf(df.format(b));
678 return output;
679 }
680 }
681 class ParseInput {
682
683 public static boolean IsLandPhoneToLandPhone(String s) {//判断是否符合座机打座机格式
684 if (s.matches("t-0[\\d]{2,3}[\\d]{7,8} "
685 + "[\\d]{3,4}[\\d]{7,8} "
686 + "[\\d]{4}.[\\d]{1,2}.[\\d]{1,2} [\\d]{2}:[\\d]{2}:[\\d]{2} "
687 + "[\\d]{4}.[\\d]{1,2}.[\\d]{1,2} [\\d]{2}:[\\d]{2}:[\\d]{2}"))
688 {
689 String []str = s.split(" ");
690 String s1 = str[2] + " " + str[3];
691 String s2 = str[4] + " " + str[5];
692 if(parseTime.isValidDate(s1)&&parseTime.isValidDate(s2)&&s2.compareTo(s1) > 0)
693 return true;
694 else
695 return false;
696 }
697 else
698 return false;
699 }
700
701 public static boolean isMobilePhoneToMobilePhone(String s)
702 {
703 if(s.matches("t-1[\\d]{10} [\\d]{3,4} "
704 + "1[\\d]{10} [\\d]{3,4} "
705 + "[\\d]{4}.[\\d]{1,2}.[\\d]{1,2} [\\d]{2}:[\\d]{2}:[\\d]{2} "
706 + "[\\d]{4}.[\\d]{1,2}.[\\d]{1,2} [\\d]{2}:[\\d]{2}:[\\d]{2}"))
707 {
708 String []str = s.split(" ");
709 String s1 = str[4] + " " + str[5];
710 String s2 = str[6] + " " + str[7];
711 if(parseTime.isValidDate(s1)&&parseTime.isValidDate(s2)&&s2.compareTo(s1) > 0)
712 return true;
713 else
714 return false;
715 }
716 return false;
717 }
718
719 public static boolean isMobilePhoneToLandPhone(String s)
720 {
721 if(s.matches("t-1[\\d]{10} [\\d]{3,4} "
722 + "0[\\d]{2,3}[\\d]{7,8} "
723 + "[\\d]{4}.[\\d]{1,2}.[\\d]{1,2} [\\d]{2}:[\\d]{2}:[\\d]{2} "
724 + "[\\d]{4}.[\\d]{1,2}.[\\d]{1,2} [\\d]{2}:[\\d]{2}:[\\d]{2}"))
725 {
726 String []str = s.split(" ");
727 String s1 = str[3] + " " + str[4];
728 String s2 = str[5] + " " + str[6];
729 if(parseTime.isValidDate(s1)&&parseTime.isValidDate(s2)&&s2.compareTo(s1) > 0)
730 return true;
731 else
732 return false;
733 }
734 return false;
735 }
736
737 public static boolean isLandPhoneToMobilePhone(String s)
738 {
739 if(s.matches("t-0[\\d]{2,3}[\\d]{7,8} "
740 + "1[\\d]{10} [\\d]{3,4} "
741 + "[\\d]{4}.[\\d]{1,2}.[\\d]{1,2} [\\d]{2}:[\\d]{2}:[\\d]{2} "
742 + "[\\d]{4}.[\\d]{1,2}.[\\d]{1,2} [\\d]{2}:[\\d]{2}:[\\d]{2}"))
743 {
744 String []str = s.split(" ");
745 String s1 = str[3] + " " + str[4];
746 String s2 = str[5] + " " + str[6];
747 if(parseTime.isValidDate(s1)&&parseTime.isValidDate(s2)&&s2.compareTo(s1) > 0)
748 return true;
749 else
750 return false;
751 }
752 return false;
753 }
754
755 public static boolean IsLinePhone(String s)
756 {
757 if(s.matches("u-0791[0-9]{7,8}[ ]0"))
758 {
759 return true;
760 }
761 return false;
762 }
763
764 public static boolean IsPhone(String s)
765 {
766 if(s.matches("u-1[\\d]{10} [0|1|2]"))
767 {
768 return true;
769 }
770 return false;
771 }
772
773 }
774 class ParseLandlinePhoneInput {
775
776 //判断是否是在市内
777 public static boolean isInCity(String s)
778 {
779 if(s.matches("0791[\\d]{7,8}"))
780 {
781 return true;
782 }
783 return false;
784 }
785
786 //判断是否是在省内
787 public static boolean isInProvince(String s)
788 {
789 if(s.matches("(079[\\d]|0701)[\\d]{7,8}")&&!isInCity(s))
790 {
791 return true;
792 }
793 return false;
794 }
795
796 //判断是否是在国内
797 public static boolean isInLand(String s)
798 {
799 if(s.matches("0[\\d]{2,3}[\\d]{7,8}")&&!isInCity(s)&&!isInProvince(s))
800 {
801 return true;
802 }
803 return false;
804 }
805
806 //判断是否符合座机用户信息格式
807 public static boolean isLandPhonerData(String s)
808 {
809 if(s.matches("u-0[\\d]{2,3}[\\d]{7,8} [0]"))
810 {
811 return true;
812 }
813 return false;
814 }
815
816
817 }
818 class parseTime {
819
820 public static long computationTime(Date startTime, Date endTime){
821 long diff = endTime.getTime() - startTime.getTime();
822 return diff / 1000 ;
823 }
824
825 public static boolean isValidDate(String dateTime) {
826 if(dateTime == null ) {
827 return false;
828 }
829 DateFormat df = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
830 df.setLenient(false);//表示严格验证
831 try {
832 df.parse(dateTime);
833 } catch (ParseException e) {
834 return false;
835 }
836 return true;
837 }
838 }
839 class User {
840 UserRecords userRecords = new UserRecords();
841 ChargeMode chargeMode;
842 private double balance = 100;
843 private String number;
844
845 public User(String number , int choice)
846 {
847 this.number = number;
848 if(choice == 0)
849 {
850 chargeMode = new LandlinePhoneCharging();
851 }
852 else if(choice == 1)
853 {
854 chargeMode = new MobilePhoneCharging();
855 }
856 else if(choice == 2)
857 {
858
859 }
860
861 }
862
863 public double calBalance()
864 {
865 return this.balance - chargeMode.getMonthlyRent() - this.calCost();
866 }
867
868 public double calCost()
869 {
870 return chargeMode.calCost(userRecords);
871 }
872
873
874 //getter和setter方法
875 public UserRecords getUserRecords() {
876 return userRecords;
877 }
878 public void setUserRecords(UserRecords userRecords) {
879 this.userRecords = userRecords;
880 }
881 public ChargeMode getChargeMode() {
882 return chargeMode;
883 }
884 public void setChargeMode(ChargeMode chargeMode) {
885 this.chargeMode = chargeMode;
886 }
887 public double getBalance() {
888 return balance;
889 }
890 public String getNumber() {
891 return number;
892 }
893 public void setNumber(String number) {
894 this.number = number;
895 }
896
897 }
898 class UserRecords {
899 ArrayList<CallRecord> CityCallingInCityRecords = new ArrayList<CallRecord>();
900 ArrayList<CallRecord> CityCallingInProvinceRecords = new ArrayList<CallRecord>();
901 ArrayList<CallRecord> CityCallingInLandRecords = new ArrayList<CallRecord>();
902 ArrayList<CallRecord> ProvinceCallingRecords = new ArrayList<CallRecord>();
903 ArrayList<CallRecord> LandCallingRecords = new ArrayList<CallRecord>();
904 ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();
905 ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();
906 ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();
907
908 public void addCityCallingInCityRecords(CallRecord callRecord)
909 {
910 CityCallingInCityRecords.add(callRecord);
911 }
912
913 public void addCityCallingInProvinceRecords(CallRecord callRecord)
914 {
915 CityCallingInProvinceRecords.add(callRecord);
916 }
917
918 public void addCityCallingInLandRecords(CallRecord callRecord)
919 {
920 CityCallingInLandRecords.add(callRecord);
921 }
922
923 public void addProvinceCallingRecords(CallRecord callRecord)
924 {
925 ProvinceCallingRecords.add(callRecord);
926 }
927
928 public void addLandCallingRecords(CallRecord callRecord)
929 {
930 LandCallingRecords.add(callRecord);
931 }
932
933 public void addanswerInLandRecords(CallRecord callRecord)
934 {
935 answerInLandRecords.add(callRecord);
936 }
937
938 public void addSendMessageRecords(MessageRecord sendMessageRecord)
939 {
940 sendMessageRecords.add(sendMessageRecord);
941 }
942
943 public void addReceiveMessageRecords(MessageRecord receiveMessageRecord)
944 {
945 receiveMessageRecords.add(receiveMessageRecord);
946 }
947
948 public ArrayList<CallRecord> getCityCallingInCityRecords() {
949 return CityCallingInCityRecords;
950 }
951
952 public ArrayList<CallRecord> getCityCallingInProvinceRecords() {
953 return CityCallingInProvinceRecords;
954 }
955
956 public ArrayList<CallRecord> getCityCallingInLandRecords() {
957 return CityCallingInLandRecords;
958 }
959
960 public ArrayList<CallRecord> getProvinceCallingRecords() {
961 return ProvinceCallingRecords;
962 }
963
964 public ArrayList<CallRecord> getLandCallingRecords() {
965 return LandCallingRecords;
966 }
967
968 public ArrayList<CallRecord> getAnswerInLandRecords() {
969 return answerInLandRecords;
970 }
971
972 public ArrayList<MessageRecord> getSendMessageRecords() {
973 return sendMessageRecords;
974 }
975
976 public ArrayList<MessageRecord> getReceiveMessageRecords() {
977 return receiveMessageRecords;
978 }
979
980 }

分析:这道题主要是在上一次的大作业基础上添加了手机的相关功能设计思路跟写上一题座机的差不多,毕竟我也是在那个代码的基础上改进的,主要是将一部分的判断代码写到别的类里去了,但是复用性也不高,因为都有一部分的改动,主要是添加了手机类,并且让设备类成为它的父类。主要是判断相关的功能比较繁琐分为如下

座机打座机

(1)室内接

(2)座机知道是哪接通的

座机打手机

(1)座机知道手机是在哪的

(2)手机知道自己是在哪接通的

手机打座机

(1)手机自己在哪

(2)座机市内接通

手机打手机

(1)手机自己在哪

(2)手机在哪接的

第八次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。

本题只考虑短信计费,不考虑通信费用以及月租费。

建议类图:

输入样例:

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

u-18907910010 3
m-18907910010 13305862264 aaaaaaaaaaaaaaaaaaaaaaa
end

输出样例:

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

18907910010 0.3 99.7
### 输入样例1:

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

u-18907910010 3
m-18907910010 13305862264 aaaaaaaaaaaa
m-18907910010 13305862264 aaaaaaa.
m-18907910010 13305862264 bb,bbbb
end

输出样例1:

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

18907910010 0.5 99.5

代码如下:

  1 import java.text.ParseException;
2 import java.util.*;
3
4 public class Main {
5 public static void main(String[] args) throws ParseException {
6
7 try (Scanner input = new Scanner(System.in)) {
8 java.text.DecimalFormat df = new java.text.DecimalFormat("0.0#");
9
10 HashSet<String> init = new HashSet<String>();// 存储用户号码,代表User
11 ArrayList<String> operate = new ArrayList<String>();// 对User进行的操作
12 ArrayList<User> users = new ArrayList<User>();
13 while (true) {// 将信息录入
14 String t = input.nextLine();
15 if (t.equals("e"+"nd")) {
16 break;
17 } else {
18 int tt=t.length();
19 if (tt < 13) {
20 continue;
21 }
22 int ss=1;
23 if (ss==1&&t.charAt(0) == 'u') {// 首字母为u则为注册
24 init.add(t);
25 } else if (ss==1&t.charAt(0) == 'm') {// 首字母为t则为操作
26 operate.add(t);
27 }
28 }
29 }
30
31 for (String it : init) {
32 if (NumberJudge(it)) {
33 String nums[] = it.split("[- ]");
34 users.add(new User(new MessageCharging(), nums[1]));
35 }
36 }
37
38 for (String it : operate) {
39 if (MessageJudge(it)) {
40 String nums[] = it.split("[- ]");
41 String sendMessageNum = nums[1];
42 String message = it.substring(26);
43 for (User user : users) {
44 if (user.getNumber().equals(sendMessageNum)) {
45 user.getUserRecords().getSendMessageRecords().add(new MessageRecord(message));
46 }
47 }
48 }
49 }
50 for (User user : users) {
51 user.calCost();
52 user.calBalance();
53 }
54 int ua=users.size();
55 for (int i = 0; i <ua ; i++) {
56 int min = i;
57 for (int j = i; j < ua; j++) {
58 if (users.get(min).getNumber().compareTo(users.get(j).getNumber()) > 0) {
59 min = j;
60 }
61 }
62 Collections.swap(users, i, min);
63 }
64 for (User user : users) {
65 System.out.println(user.getNumber() + " " + df.format(user.calCost()) + " " +
66 df.format(user.getBalance()));
67 }
68 }
69 }
70
71
72 public static boolean NumberJudge(String arr) {
73 String numberjudge = "[u]"+"[-]1[3-9]\\d{9}[ ][3]";
74
75 if (arr.matches(numberjudge)) {
76 return true;
77 } else {
78 return false;
79 }
80 }
81
82 public static boolean MessageJudge(String arr) {
83 String messagejudge = "m-1"+"[3-9]\\d{9} 1[3-9]\\d{9} [a-z|A-Z|0-9| |,|.]++";
84
85 if (arr.matches(messagejudge)) {
86 return true;
87 } else {
88 return false;
89 }
90 }
91 }
92 abstract class ChargeMode {
93 private ArrayList<ChargeRule> chargeRules = new ArrayList<ChargeRule>();
94
95 public ArrayList<ChargeRule> getChargeRules() {
96 return chargeRules;
97 }
98
99 public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
100 this.chargeRules = chargeRules;
101 }
102
103 public abstract double calCost(UserRecords userRecords);
104
105 public abstract double getMonthlyRent();
106 }
107 abstract class ChargeRule {
108
109 }
110 abstract class CommunicationRecord {
111 private String callingNumber;//来电号码
112 private String answerNumber;//接电话号码
113
114 public String getCallingNumber() {
115 return callingNumber;
116 }
117
118 public void setCallingNumber(String callingNumber) {
119 this.callingNumber = callingNumber;
120 }
121
122 public String getAnswerNumber() {
123 return answerNumber;
124 }
125
126 public void setAnswerNumber(String answerNumber) {
127 this.answerNumber = answerNumber;
128 }
129
130 }
131 class MessageCharging extends ChargeMode {
132 public MessageCharging() {
133 getChargeRules().add(new SendMessageRule());
134 }
135 public double calCost(UserRecords userRecords) {
136 double cost = 0;
137 cost += ((SendMessageRule) getChargeRules().get(0)).calCost(userRecords.getSendMessageRecords());
138 return cost;
139 }
140 public double getMonthlyRent() {
141 return 0;
142 }
143
144 }
145
146 abstract class MessageChargingRule extends ChargeRule{
147 abstract double calCost(ArrayList<MessageRecord> messageRecords);
148 }
149 class MessageRecord extends CommunicationRecord {
150 private String message;
151
152 public MessageRecord() {
153 }
154
155 public MessageRecord(String message) {
156 this.message = message;
157 }
158
159 public String getMessage() {
160 return message;
161 }
162
163 public void setMessage(String message) {
164 this.message = message;
165 }
166
167 }
168 class SendMessageRule extends MessageChargingRule {
169 public double calCost(ArrayList<MessageRecord> messageRecords) {
170 double cost = 0;
171 int cnt = 0;
172 for (MessageRecord messageRecord : messageRecords) {
173 if (messageRecord.getMessage().length() % 10*10/10 == 0) {
174 cnt += messageRecord.getMessage().length() *10/ 100;
175 } else {
176 cnt += messageRecord.getMessage().length() *10/ 100 + 1;
177 }
178 }
179 if (cnt > 5) {
180 cost = 0.7+ (cnt - 5) * 0.3;
181 } else if (cnt<=5&&cnt > 3) {
182 cost = 0.3 + 0.2 * (cnt - 3);
183 } else {
184 cost = 0.1 * cnt;
185 }
186 return cost;
187 }
188 }
189 class User {
190 private UserRecords userRecords = new UserRecords();
191 private double balance = 100;
192 private ChargeMode chargeMode;
193 private String number;
194
195 public User() {
196 }
197
198 public User(ChargeMode chargeMode, String number) {
199 this.chargeMode = chargeMode;
200 this.number = number;
201 }
202
203 public double calBalance() {
204 this.balance = this.balance - this.calCost() - this.getChargeMode().getMonthlyRent();
205 return this.balance;
206 }
207
208 public double calCost() {
209 return chargeMode.calCost(userRecords);
210 }
211
212 public UserRecords getUserRecords() {
213 return userRecords;
214 }
215
216 public void setUserRecords(UserRecords userRecords) {
217 this.userRecords = userRecords;
218 }
219
220 public double getBalance() {
221 return balance;
222 }
223
224 public ChargeMode getChargeMode() {
225 return chargeMode;
226 }
227
228 public void setChargeMode(ChargeMode chargeMode) {
229 this.chargeMode = chargeMode;
230 }
231
232 public String getNumber() {
233 return number;
234 }
235
236 public void setNumber(String number) {
237 this.number = number;
238 }
239
240 }
241 class UserRecords {
242
243 private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();//发送短信电话
244 private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();//接受短信电话
245
246 public UserRecords() {
247 }
248
249
250 public ArrayList<MessageRecord> getSendMessageRecords() {
251 return sendMessageRecords;
252 }
253
254 public ArrayList<MessageRecord> getReceiveMessageRecords() {
255 return receiveMessageRecords;
256 }
257
258 public void addSendMessageRecords(MessageRecord sendMessageRecords) {
259 this.sendMessageRecords.add(sendMessageRecords);
260 }
261
262 public void addReceiveMessageRecords(MessageRecord receiveMessageRecords) {
263 this.receiveMessageRecords.add(receiveMessageRecords);
264 }
265 }

分析:增添了MessageRecord类去记录短信,方便计算费用。

踩坑心得:

1.判断输入格式是否正确,里面有许多小细节,一定要注意,

比如,区号可能是三位,

2.价钱计算。

计算的时候记得把时间转成整型,避免除以60后后面有小数,影响结果。

最后余数计算,不到一分钟按一分钟计。

3.判断是否输入正确用我自己写的函数切开再判断永远写不对,用他的正则表达式一用就过,

这边建议不要改它的判断方式。

4.短信的截取一定要找好位置,不要多截信息或少截信息。

四、改进建议

我的代码看起来还是有冗杂,比如一些User的容器在循环里因为得到数据而需要定义一些函数,在考虑可不可以用一些办法替代。还类设计的过于复杂,且部分类的关联度过强。

五、总结

1、这几次代码的设计让我认识到面向对象与面向过程的区别,对于面向对象来说,类的设计至关重要,能否把类的基础设计好是对后续代码编写难易、好坏有极大的影响。然后我的正则表达式还掌握的不好,还需要进一步学习,对于那些复杂的输入信息还不能很好的识别。下一步就是javafx的学习了。

最新文章

  1. Oracle AWR报告提取方法
  2. Java基础应用
  3. meta标签的小拓展
  4. C#语句1:选择语句一(if else )
  5. HDU2255-奔小康赚大钱-二分图最大权值匹配-KM算法
  6. Mastering Web Application Development with AngularJS 读书笔记-前记
  7. tornado--之cookie自定义(还有session)
  8. Python_Day_4(内置函数之篇)
  9. MYSQL中replace into的用法以及与inset into的区别
  10. mvc中ajax.beginform一次提交重复Post两次的问题解决
  11. 关于inline-block在IE8下无效的解决方法
  12. JS 逗号表达式
  13. 【Cocos2d-x游戏引擎开发笔记(25)】XML解析
  14. 【SICP归纳】2 高阶函数和数据抽象
  15. Mysql笔记3数据库基本操作
  16. C语言之linux内核可变参实现printf,sprintf
  17. nginx conf_ctx ****
  18. python list用法
  19. windows错误:错误0x80070091 目录不是空的
  20. python之基础1

热门文章

  1. django 安装cerlery error in anyjson setup command: use_2to3 is invalid.
  2. Asp.Net Core上传大文件请求体限制设置
  3. Delphi 多进程共享内存的简单封装单元
  4. js 自定義event
  5. laravel Auth的使用
  6. [NOIP1999 提高组] 旅行家的预算
  7. One-Shot Transfer Learning of Physics-Informed Neural Networks
  8. FB50 过帐码 没有定义
  9. cublas fp16
  10. dvwa sql盲注教程