PTA题目集6~8的总结

一、前言

  这三次题目集都是关于电信计费系列的题目,分别是座机计费、手机+座机计费和短信计费。题量适中,第一次写起来较为困难,后面两次由于有了第一次的基础,相对较简单。特别是第三次,短信计费的不用像前面两次那样需要考虑那么多问题,相对更简单一些。这三次题目集和以往的有些不同,就是都给出了相应的类图。虽然给出了类图,但是还是需要考虑很多问题,并且需要我们根据类图,来确定类与类之间的关系;同时,还要分别计算不同情况下的费用。

  这三次的题目集涵盖了所学的许多方面,例如说为了储存用户信息并且灵活调用而使用的Map容器,又比如说为了匹配正确输入而使用的String中的match()加正则表达式。在类设计上,三次题目集给出的类图有着许多继承、抽象类的使用。也主要考察我们对各个类之间的关系的理解以及对数据的封装、类的继承、多态、接口、抽象类、集合框架等多个知识的综合运用。

 

二、设计与分析

1、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元。
每条通讯信息单独计费后累加,不是将所有时间累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。

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

后续扩展说明:
后续题目集将增加手机用户,手机用户的计费方式中除了与座机计费类似的主叫通话费之外,还包含市外接听电话的漫游费以及发短信的费用。在本题的设计时可统一考虑。
通话记录中,手机需要额外记录拨打/接听的地点的区号,比如:
座机打手机:t-主叫号码 接听号码 接听地点区号 起始时间 结束时间
t-079186330022 13305862264 020 2022.1.3 10:00:25 2022.1.3 10:05:11
手机互打:t-主叫号码 拨号地点 接听号码 接听地点区号 起始时间 结束时间
t-18907910010 0791 13305862264 0371 2022.1.3 10:00:25 2022.1.3 10:05:11
短信的格式:m-主叫号码,接收号码,短信内容
m-18907910010 13305862264 welcome to jiangxi
m-13305862264 18907910010 thank you

我的源码


import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.text.ParseException;
public class Main {
	public static void main(String[] args) {
		Outputtool outputtool = new Outputtool();
		Inputdeal inputdeal = new Inputdeal();
		ArrayList<User> users = new ArrayList<>();
		Scanner in = new Scanner(System.in);
		String input = in.nextLine();
		while (!input.equals("end")) {
			if (1 == inputdeal.check(input)) {
				inputdeal.writeUser(users, input);
			} else if (2 == inputdeal.check(input)) {
				inputdeal.writeRecord(users, input);
			}
			input = in.nextLine();
		}
		users.sort(new Comparator<User>() {
			public int compare(User u1, User u2) {
				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());
	}
	public double calCost(UserRecords userRecords) {
		double sumCost = 0;
		for (ChargeRule rule : chargeRules) {
			sumCost += rule.calCost(userRecords);
		}
		return sumCost;
	}
	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 (validate(inputs[2]))
				if (validate(inputs[4]))
					if (validatet(inputs[3]))
						if (validatet(inputs[5]))
							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) {
		// 使用正则表达式 测试 字符 符合 dddd.dd.dd 的格式(d表示数字)
		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 usernew = new User();
		String[] inputs = input.split(" ");
		String num = inputs[0].substring(2);
		for (User i : users) {
			if (i.getNumber().equals(num)) {
				return;
			}
		}
		usernew.setNumber(num);
		int mode = Integer.parseInt(inputs[1]);
		if (mode == 0) {
			usernew.setChargeMode(new LandlinePhoneCharging());
		}
		users.add(usernew);
	}
	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 answerNumbe;
	public String getCallingNumber() {
		return callingNumber;
	}
	public void setCallingNumber(String callingNumber) {
		this.callingNumber = callingNumber;
	}
	public String getAnswerNumbe() {
		return answerNumbe;
	}
	public void setAnswerNumbe(String answerNumbe) {
		this.answerNumbe = answerNumbe;
	}
}
abstract class ChargeRule {
	abstract public double calCost(UserRecords userRecords);
}
class CallRecord extends CommunicationRecord {
	private Date startTime;
	private Date endTime;
	private String callingAddressAreaCode;
	private String answerAddressAreaCode;
	public int getCallType() {
		if (callingAddressAreaCode.equals(answerAddressAreaCode)) {
			return 1;
		}
		if (callingAddressAreaCode.matches("^079[0-9]$") || callingAddressAreaCode.equals("0701")) {
			if (answerAddressAreaCode.matches("^079[0-9]$") || answerAddressAreaCode.equals("0701")) {
				return 2;
			}
		}
		return 3;
	}
	public CallRecord(String[] inputs) {
		super();
		if (inputs[0].length() == 10) {
			callingAddressAreaCode = inputs[0].substring(0, 3);
		} else {
			callingAddressAreaCode = inputs[0].substring(0, 4);
		}
		if (inputs[1].length() == 10) {
			answerAddressAreaCode = inputs[1].substring(0, 3);
		} else {
			answerAddressAreaCode = inputs[1].substring(0, 4);
		}
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault());
		try {
			startTime = simpleDateFormat.parse(inputs[2] + " " + inputs[3]);
			endTime = simpleDateFormat.parse(inputs[4] + " " + inputs[5]);
		} catch (ParseException e) {
		}
	}
	public CallRecord(Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) {
		super();
		this.startTime = startTime;
		this.endTime = endTime;
		this.callingAddressAreaCode = callingAddressAreaCode;
		this.answerAddressAreaCode = answerAddressAreaCode;
	}
	public Date getStartTime() {
		return startTime;
	}
	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}
	public Date getEndTime() {
		return endTime;
	}
	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}
	public String getCallingAddressAreaCode() {
		return callingAddressAreaCode;
	}
	public void setCallingAddressAreaCode(String callingAddressAreaCode) {
		this.callingAddressAreaCode = callingAddressAreaCode;
	}
	public String getAnswerAddressAreaCode() {
		return answerAddressAreaCode;
	}
	public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
		this.answerAddressAreaCode = answerAddressAreaCode;
	}
}
abstract class CallChargeRule extends ChargeRule {
 
}
class LandPhoneInCityRule extends CallChargeRule {
	public double calCost(UserRecords userRecords) {
		double sumCost = 0;
		for (CallRecord call : userRecords.getCallingInCityRecords()) {
			double distanceS = (call.getEndTime().getTime() -call.getStartTime().getTime()) / 1000;
			if (distanceS < 0) {
				continue;
			}
			double distanceM = (int) distanceS / 60;
			if (distanceS % 60 != 0) {
				distanceM += 1;
			}
			sumCost += distanceM * 0.1;
		}
		return sumCost;
	}
}
class LandPhoneInProvinceRule extends CallChargeRule {
	public double calCost(UserRecords userRecords) {
		double sumCost = 0;
		for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
			double distanceS = (call.getEndTime().getTime() -call.getStartTime().getTime()) / 1000;
			if (distanceS < 0) {
				continue;
			}
			double distanceM = (int) distanceS / 60;
			if (distanceS % 60 != 0) {
				distanceM += 1;
			}
			sumCost += distanceM * 0.3;
		}
		return sumCost;
	}
}
class LandPhoneInlandRule extends CallChargeRule {
	public double calCost(UserRecords userRecords) {
		double sumCost = 0;
		for (CallRecord call : userRecords.getCallingInLandRecords()) {
			double distanceS = (call.getEndTime().getTime() -call.getStartTime().getTime()) / 1000;
			if (distanceS < 0) {
				continue;
			}
			double distanceM = (int) distanceS / 60;
			if (distanceS % 60 != 0) {
				distanceM += 1;
			}
			sumCost += distanceM * 0.6;
		}
		return sumCost;
	}
}
class MessageRecord extends CommunicationRecord {
	private String message;
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
}
class User {
	private UserRecords userRecords = new UserRecords();
	private double balance = 100;
	private ChargeMode chargeMode;
	private String number;
	public double calCost() {
		return chargeMode.calCost(userRecords);
	}
	public double calBalance() {
		return balance - chargeMode.getMonthlyRent() - chargeMode.calCost(userRecords);
	}
	public UserRecords getUserRecords() {
		return userRecords;
	}
	public void setUserRecords(UserRecords userRecords) {
		this.userRecords = userRecords;
	}
	public ChargeMode getChargeMode() {
		return chargeMode;
	}
	public void setChargeMode(ChargeMode chargeMode) {
		this.chargeMode = chargeMode;
	}
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
}
class Outputtool {
	public void output(double out) {
		BigDecimal numb = new BigDecimal(out);
		out = numb.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		System.out.print(out);
	}
}

题目分析:

类图如下:

度量分析:

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

踩坑心得:

  这道题目涉及到对arrayList排序的问题。我开始是先声明一个User user[]的对象数组,先历便arrayList,将数据赋值给user[],再用compareTo方法对字符串进行冒泡排序,但是排序之后就出现了一个问题,我通过user数组赋值没有办法删除重复的元素,这就导致输出重复。

改进建议:

  1.这个代码的圈复杂度达到了19,这说明代码的质量不高。因为我在写题目的过程中,只注意需要完成题目要求的功能,而没有对代码进行优化,导致写了过多的if/else语句,据查找资料,if/else 和循环的使用会使得圈复杂度提高,改进的方法是将if/else语句换成switch语句,可以有效减少圈复杂度。

  2.可以将判断的逻辑颠倒过来,这样代码的思路就会更加的清晰,代码的可读性也会更高。

  3.可以将arrayList换成hashset,用集合简化题目。

 

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

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

我的源码

 import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.text.ParseException;

public class Main {

	public static void main(String[] args) {
		Outputtool outputtool = new Outputtool();
		Inputdeal inputdeal = new Inputdeal();
		ArrayList<User> users = new ArrayList<>();
		Scanner in = new Scanner(System.in);
		String input = in.nextLine();
		while (!input.equals("end")) {
			if (1 == inputdeal.check(input)) {
				inputdeal.writeUser(users, input);
			} else if (2 == inputdeal.check(input)) {
				inputdeal.writeRecord(users, input);
			}
			input = in.nextLine();
		}
		users.sort(new Comparator<User>() {
			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());
	}
	public double calCost(UserRecords userRecords) {
		double sumCost = 0;
		for (ChargeRule rule : chargeRules) {
			sumCost += rule.calCost(userRecords);
		}
		return sumCost;
	}
	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());
	}
	public double calCost(UserRecords userRecords) {
		double sumCost = 0;
		for (ChargeRule rule : chargeRules) {
			sumCost += rule.calCost(userRecords);
		}
		return sumCost;
	}
	public double getMonthlyRent() {
		return monthlyRent;
	}
}

class Inputdeal {
	public int check(String input) {
		if (input.matches("[u]-0791[0-9]{7,8}\\s[0]") || input.matches("[u]-1[0-9]{10}\\s[1]")) {
			return 1;
//		} else if (input.charAt(0) == 'm') {
//			return 2;
		} else if (input.matches("(([t]-0791[0-9]{7,8}\\s" + "0[0-9]{9,11}\\s)|" 
				+ "([t]-0791[0-9]{7,8}\\s" + "1[0-9]{10}\\s" + "0[0-9]{2,3}\\s)|" 
				+ "([t]-1[0-9]{10}\\s" + "0[0-9]{2,3}\\s" + "0[0-9]{9,11}\\s)|" 
				+ "([t]-1[0-9]{10}\\s" + "0[0-9]{2,3}\\s" + "1[0-9]{10}\\s" + "0[0-9]{2,3}\\s))"
				+ "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.(((0?[13578]|1[02])\\.(0?"
				+ "[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((("
				+ "[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))"
				+ "\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\\s"
				+ "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.("
				+ "[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|((("
				+ "[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))"
				+ "\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])")) {
			return 2;
		}
		return 0;
	}
	private boolean validatet(String string) {
		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) {
		// 使用正则表达式 测试 字符 符合 dddd.dd.dd 的格式(d表示数字)
		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 usernew = new User();
		String[] inputs = input.split(" ");
		String num = inputs[0].substring(2);
		for (User i : users) {
			if (i.getNumber().equals(num)) {
				return;
			}
		}
		usernew.setNumber(num);
		int mode = Integer.parseInt(inputs[1]);
		if (mode == 0) {
			usernew.setChargeMode(new LandlinePhoneCharging());
		} else if (mode == 1) {
			usernew.setChargeMode(new MobilePhoneCharging());
		}
		users.add(usernew);
	}
	public void writeRecord(ArrayList<User> users, String input) {
		String[] inputs = input.split(" ");
		User callu = null, answeru = null;
		CallRecord callrecord = new CallRecord(inputs);
		if (input.charAt(0) == 't') {
			String out = inputs[0];
			String in = "";
			if (inputs.length == 6) {
				in = inputs[1];
			} else if (inputs.length == 7) {
				in = inputs[1];
			} else if (inputs.length == 8) {
				in = inputs[2];
			}
			for (User i : users) {
				if (i.getNumber().equals(out)) {
					callu = i;
				}
				if (i.getNumber().equals(in)) {
					answeru = i;
				}
				if (callu != null && answeru != null) {
					break;
				}
			}
			if (callu != null) {
				if (callrecord.getCallType().matches("^1[1-3]$")) {
					callu.getUserRecords().addCallingInCityRecords(callrecord);
				} else if (callrecord.getCallType().matches("^2[1-3]$")) {
					callu.getUserRecords().addCallingInProvinceRecords(callrecord);
				} else {
					callu.getUserRecords().addCallingInLandRecords(callrecord);
				}
			}
			if (answeru != null) {
				if (callrecord.getCallType().matches("^[1-3]1$")) {
					answeru.getUserRecords().addAnswerInCityRecords(callrecord);
				} else if (callrecord.getCallType().matches("^[1-3]2$")) {
					answeru.getUserRecords().aaddAnswerInProvinceRecords(callrecord);
				} else {
					answeru.getUserRecords().addAnswerInLandRecords(callrecord);
				}
			}
		} else if (input.charAt(0) == 'm') {
		}
	}
}

abstract class CommunicationRecord {
	protected String callingNumber;
	protected String answerNumbe;
	public String getCallingNumber() {
		return callingNumber;
	}
	public void setCallingNumber(String callingNumber) {
		this.callingNumber = callingNumber;
	}
	public String getAnswerNumbe() {
		return answerNumbe;
	}
	public void setAnswerNumbe(String answerNumbe) {
		this.answerNumbe = answerNumbe;
	}
}

abstract class ChargeRule {
	abstract public double calCost(UserRecords userRecords);

}

class CallRecord extends CommunicationRecord {
	private Date startTime;
	private Date endTime;
	private String callingAddressAreaCode;
	private String answerAddressAreaCode;

	public String getCallType() {
		String type = "";
		if (callingAddressAreaCode.equals("0791")) {
			type = type.concat("1");
		} else if (callingAddressAreaCode.matches("^079[023456789]$") || callingAddressAreaCode.equals("0701")) {
			type = type.concat("2");
		} else {
			type = type.concat("3");
		}
		if (answerAddressAreaCode.equals("0791")) {
			type = type.concat("1");
		} else if (answerAddressAreaCode.matches("^079[023456789]$") || answerAddressAreaCode.equals("0701")) {
			type = type.concat("2");
		} else {
			type = type.concat("3");
		}
		return type;
	}
	public CallRecord(String[] inputs) {
		super();
		char type = inputs[0].charAt(0);
		inputs[0] = inputs[0].substring(2);
		String sd = null, st = null, ed = null, et = null;
		if (type == 't') {
			if (inputs.length == 6) {
				sd = inputs[2];
				st = inputs[3];
				ed = inputs[4];
				et = inputs[5];
				callingAddressAreaCode = inputs[0].substring(0, 4);
				answerAddressAreaCode = inputs[1].substring(0, 4);
			} else if (inputs.length == 7) {
				sd = inputs[3];
				st = inputs[4];
				ed = inputs[5];
				et = inputs[6];
				if (inputs[0].charAt(0) != '0') {
					if (inputs[2].length() == 10) {
						answerAddressAreaCode = inputs[2].substring(0, 3);
					} else {
						answerAddressAreaCode = inputs[2].substring(0, 4);
					}
					callingAddressAreaCode = inputs[1];
				} else {
					if (inputs[0].length() == 10) {
						callingAddressAreaCode = inputs[0].substring(0, 3);
					} else {
						callingAddressAreaCode = inputs[0].substring(0, 4);
					}
					answerAddressAreaCode = inputs[2];
				}
			} else if (inputs.length == 8) {
				sd = inputs[4];
				st = inputs[5];
				ed = inputs[6];
				et = inputs[7];
				callingAddressAreaCode = inputs[1];
				answerAddressAreaCode = inputs[3];
			}
		} else if (type == 'm') {

		}
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault());
		try {
			startTime = simpleDateFormat.parse(sd + " " + st);
			endTime = simpleDateFormat.parse(ed + " " + et);
		} catch (ParseException e) {
		}
	}
	public CallRecord(Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) {
		super();
		this.startTime = startTime;
		this.endTime = endTime;
		this.callingAddressAreaCode = callingAddressAreaCode;
		this.answerAddressAreaCode = answerAddressAreaCode;
	}
	public Date getStartTime() {
		return startTime;
	}
	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}
	public Date getEndTime() {
		return endTime;
	}
	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}
	public String getCallingAddressAreaCode() {
		return callingAddressAreaCode;
	}
	public void setCallingAddressAreaCode(String callingAddressAreaCode) {
		this.callingAddressAreaCode = callingAddressAreaCode;
	}
	public String getAnswerAddressAreaCode() {
		return answerAddressAreaCode;
	}
	public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
		this.answerAddressAreaCode = answerAddressAreaCode;
	}
}

abstract class CallChargeRule extends ChargeRule {

}

class LandPhoneInCityRule extends CallChargeRule {
	public double calCost(UserRecords userRecords) {
		double sumCost = 0;
		for (CallRecord call : userRecords.getCallingInCityRecords()) {
			double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
			if (distanceS < 0) {
				continue;
			}
			double distanceM = (int) distanceS / 60;
			if (distanceS % 60 != 0) {
				distanceM += 1;
			}
			if (call.getCallType().equals("11")) {
				sumCost += distanceM * 0.1;
			} else if (call.getCallType().equals("12")) {
				sumCost += distanceM * 0.3;
			} else if (call.getCallType().equals("13")) {
				sumCost += distanceM * 0.6;
			}
		}
		return sumCost;
	}
}

class LandPhoneInlandRule extends CallChargeRule {
	public double calCost(UserRecords userRecords) {
		double sumCost = 0;
		for (CallRecord call : userRecords.getCallingInLandRecords()) {
			double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
			if (distanceS < 0) {
				continue;
			}
			double distanceM = (int) distanceS / 60;
			if (distanceS % 60 != 0) {
				distanceM += 1;
			}
			sumCost += distanceM * 0.6;
		}
		return sumCost;
	}
}

class LandPhoneInProvinceRule extends CallChargeRule {
	public double calCost(UserRecords userRecords) {
		double sumCost = 0;
		for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
			double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
			if (distanceS < 0) {
				continue;
			}
			double distanceM = (int) distanceS / 60;
			if (distanceS % 60 != 0) {
				distanceM += 1;
			}
			sumCost += distanceM * 0.3;
		}
		return sumCost;
	}
}

class MobilePhoneInCityRule extends CallChargeRule {
	public double calCost(UserRecords userRecords) {
		double sumCost = 0;
		for (CallRecord call : userRecords.getCallingInCityRecords()) {
			double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
			if (distanceS < 0) {
				continue;
			}
			double distanceM = (int) distanceS / 60;
			if (distanceS % 60 != 0) {
				distanceM += 1;
			}
			if (call.getCallType().equals("11")) {
				sumCost += distanceM * 0.1;
			} else if (call.getCallType().equals("12")) {
				sumCost += distanceM * 0.2;
			} else if (call.getCallType().equals("13")) {
				sumCost += distanceM * 0.3;
			}

		}
		return sumCost;
	}
}

class MobilePhoneInlandRule extends CallChargeRule {
	public double calCost(UserRecords userRecords) {
		double sumCost = 0;
		for (CallRecord call : userRecords.getCallingInLandRecords()) {
			double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
			if (distanceS < 0) {
				continue;
			}
			double distanceM = (int) distanceS / 60;
			if (distanceS % 60 != 0) {
				distanceM += 1;
			}
			sumCost += distanceM * 0.6;
		}
		for (CallRecord call : userRecords.getAnswerInLandRecords()) {
			double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
			if (distanceS < 0) {
				continue;
			}
			double distanceM = (int) distanceS / 60;
			if (distanceS % 60 != 0) {
				distanceM += 1;
			}
			sumCost += distanceM * 0.3;
		}
		return sumCost;
	}
}

class MobilePhoneInProvinceRule extends CallChargeRule {
	public double calCost(UserRecords userRecords) {
		double sumCost = 0;
		for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
			double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
			if (distanceS < 0) {
				continue;
			}
			double distanceM = (int) distanceS / 60;
			if (distanceS % 60 != 0) {
				distanceM += 1;
			}
			if (call.getCallType().equals("21")) {
				sumCost += distanceM * 0.3;
			} else if (call.getCallType().equals("22")) {
				sumCost += distanceM * 0.3;
			} else if (call.getCallType().equals("23")) {
				sumCost += distanceM * 0.3;
			}
		}
		return sumCost;
	}
}

class MessageRecord extends CommunicationRecord {
	private String message;
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
}

class User {
	private UserRecords userRecords = new UserRecords();
	private double balance = 100;
	private ChargeMode chargeMode;
	private String number;

	public double calCost() {
		return chargeMode.calCost(userRecords);
	}
	public double calBalance() {
		return balance - chargeMode.getMonthlyRent() - chargeMode.calCost(userRecords);
	}
	public UserRecords getUserRecords() {
		return userRecords;
	}
	public void setUserRecords(UserRecords userRecords) {
		this.userRecords = userRecords;
	}
	public ChargeMode getChargeMode() {
		return chargeMode;
	}
	public void setChargeMode(ChargeMode chargeMode) {
		this.chargeMode = chargeMode;
	}
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
}

class Outputtool {
	public void output(double out) {
   	java.text.DecimalFormat df=new java.text.DecimalFormat("#.##");
//		String a=df.format(out);
//		System.out.print(a);
		BigDecimal numb = new BigDecimal(out);
		out = numb.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		System.out.print(out);
	}
}

题目分析:

类图如下:

度量分析:

  此题的难点在于对于手机来说:拨打电话和接听电话都需要计费,不但需要判断是拨打还是接听,还需要判断所在的位置。对于接听来说,所在的位置如果发生变化,相应的计费金额也会有所改变。核心就来到了判断输入是否合法,输入的时间是否合法等问题,其意思是说如过无法判断输入数据的准确与否,就无法写出这道题目。我同样采取了正则表达式判断时间,对于本题给出的格式yyyy-MM-dd HH:mm:ss。我写了一个简易的时间正则表达式。在获得输入的数据后需要了解计费规则的相关类,这些类的核心方法是: calCost(ArrayList<CallRecord> callRecords)。 该方法针根据输入参数callRecords中的所有记录计算某用户的某一项费用;如市话费。 输入参数callRecords的约束条件:必须是某一个用户的符合计费规则要求的所有记录。 SendMessageRule是发送短信的计费规则类,用于计算发送短信的费用。

踩坑心得:

  刚开始我使用的是数组装输入的数据,但发现用数组时,长度固定,于是使用动态数组来存储不固定的数据,如:ArrayList<String> title=new ArrayList<>();并用其title.add(input.nextLine())来添加数据;用title.get(++i)来获取数据;写本题时需要考虑好用户的重复输入、用户的非法输入、是用户打座机还是座机打用户、用户是拨打电话方还是接听电话方、用户拨打电话时的位置(市内、省内、省外)、用户在何时打电话以及何时打完电话等等一系类情况,否则会导致输出结果错误。

改进建议:

  通过不同的正则表达式可以简化代码长度,使其更好的维护和使用;可以通过写一个函数来实现公共的功能,减小冗杂程度,使其得到优化。

 

3、7-1 电信计费系列3-短信计费

实现一个简单的电信计费程序,针对手机的短信采用如下计费方式:
1、接收短信免费,发送短信0.1元/条,超过3条0.2元/条,超过5条0.3元/条。
2、如果一次发送短信的字符数量超过10个,按每10个字符一条短信进行计算。

输入:
输入信息包括两种类型
1、逐行输入南昌市手机用户开户的信息,每行一个用户。
格式:u-号码 计费类型 (计费类型包括:0-座机 1-手机实时计费 2-手机A套餐 3-手机短信计费)
例如:u-13305862264 3
座机号码由区号和电话号码拼接而成,电话号码包含7-8位数字,区号最高位是0。
手机号码由11位数字构成,最高位是1。
本题只针对类型3-手机短信计费。
2、逐行输入本月某些用户的短信信息,短信的格式:
m-主叫号码,接收号码,短信内容 (短信内容只能由数字、字母、空格、英文逗号、英文句号组成)
m-18907910010 13305862264 welcome to jiangxi.
m-13305862264 18907910010 thank you.

注意:以上两类信息,先输入所有开户信息,再输入所有通讯信息,最后一行以“end”结束。
输出:
根据输入的详细短信信息,计算所有已开户的用户的当月短信费用(精确到小数点后2位,单位元)。假设每个用户初始余额是100元。
每条短信信息均单独计费后累加,不是将所有信息累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。
错误处理:
输入数据中出现的不符合格式要求的行一律忽略。
本题只做格式的错误判断,无需做内容上不合理的判断,比如同一个电话两条通讯记录的时间有重合、开户号码非南昌市的号码、自己给自己打电话等,此类情况都当成正确的输入计算。但时间的输入必须符合要求,比如不能输入2022.13.61 28:72:65。

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

我的源码

 import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.text.ParseException;
 
public class Main {
 
	public static void main(String[] args) {
 
		Outputtool outputtool = new Outputtool();
 
		Inputdeal inputdeal = new Inputdeal();
 
		ArrayList<User> users = new ArrayList<>();
 
		Scanner in = new Scanner(System.in);
 
		String input = in.nextLine();
 
		while (!input.equals("end")) {
			if (1 == inputdeal.check(input)) {
				inputdeal.writeUser(users, input);
			} else if (2 == inputdeal.check(input)) {
				inputdeal.writeRecord(users, input);
			}
			input = in.nextLine();
		}
 
		users.sort(new Comparator<User>() {
 
			@Override
			public int compare(User u1, User u2) {
				if (u1.getNumber().charAt(0) == '0' && u2.getNumber().charAt(0) != '0') {
					return -1;
				} else if (u1.getNumber().charAt(0) != '0' && u2.getNumber().charAt(0) == '0') {
					return 1;
				}
				if (Double.parseDouble(u1.getNumber()) > Double.parseDouble(u2.getNumber())) {
					return 1;
				} else {
					return -1;
				}
			}
		});
 
		for (User u : users) {
			System.out.print(u.getNumber() + " ");
			outputtool.output(u.calCost());
			System.out.print(" ");
			outputtool.output(u.calBalance());
			System.out.println();
 
		}
 
	}
 
}
 
abstract class ChargeMode {
	protected ArrayList<ChargeRule> chargeRules = new ArrayList<>();
 
	public abstract double calCost(UserRecords userRecords);
 
	public abstract double getMonthlyRent();
 
	public ArrayList<ChargeRule> getChargeRules() {
		return chargeRules;
	}
 
	public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
		this.chargeRules = chargeRules;
	}
}
 
class UserRecords {
 
	private ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();
	private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();
	private ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();
	private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();
	private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();
	private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();
	private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();
	private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();
 
	public void addCallingInCityRecords(CallRecord callRecord) {
		callingInCityRecords.add(callRecord);
	}
 
	public void addCallingInProvinceRecords(CallRecord callRecord) {
		callingInProvinceRecords.add(callRecord);
	}
 
	public void addCallingInLandRecords(CallRecord callRecord) {
		callingInLandRecords.add(callRecord);
	}
 
	public void addAnswerInCityRecords(CallRecord callRecord) {
		answerInCityRecords.add(callRecord);
	}
 
	public void aaddAnswerInProvinceRecords(CallRecord callRecord) {
		answerInProvinceRecords.add(callRecord);
	}
 
	public void addAnswerInLandRecords(CallRecord callRecord) {
		answerInLandRecords.add(callRecord);
	}
 
	public void addSendMessageRecords(MessageRecord callRecord) {
		sendMessageRecords.add(callRecord);
	}
 
	public void addReceiveMessageRecords(MessageRecord callRecord) {
		receiveMessageRecords.add(callRecord);
	}
 
	public ArrayList<CallRecord> getCallingInCityRecords() {
		return callingInCityRecords;
	}
 
	public void setCallingInCityRecords(ArrayList<CallRecord> callingInCityRecords) {
		this.callingInCityRecords = callingInCityRecords;
	}
 
	public ArrayList<CallRecord> getCallingInProvinceRecords() {
		return callingInProvinceRecords;
	}
 
	public void setCallingInProvinceRecords(ArrayList<CallRecord> callingInProvinceRecords) {
		this.callingInProvinceRecords = callingInProvinceRecords;
	}
 
	public ArrayList<CallRecord> getCallingInLandRecords() {
		return callingInLandRecords;
	}
 
	public void setCallingInLandRecords(ArrayList<CallRecord> callingInLandRecords) {
		this.callingInLandRecords = callingInLandRecords;
	}
 
	public ArrayList<CallRecord> getAnswerInCityRecords() {
		return answerInCityRecords;
	}
 
	public void setAnswerInCityRecords(ArrayList<CallRecord> answerInCityRecords) {
		this.answerInCityRecords = answerInCityRecords;
	}
 
	public ArrayList<CallRecord> getAnswerInProvinceRecords() {
		return answerInProvinceRecords;
	}
 
	public void setAnswerInProvinceRecords(ArrayList<CallRecord> answerInProvinceRecords) {
		this.answerInProvinceRecords = answerInProvinceRecords;
	}
 
	public ArrayList<CallRecord> getAnswerInLandRecords() {
		return answerInLandRecords;
	}
 
	public void setAnswerInLandRecords(ArrayList<CallRecord> answerInLandRecords) {
		this.answerInLandRecords = answerInLandRecords;
	}
 
	public ArrayList<MessageRecord> getSendMessageRecords() {
		return sendMessageRecords;
	}
 
	public void setSendMessageRecords(ArrayList<MessageRecord> sendMessageRecords) {
		this.sendMessageRecords = sendMessageRecords;
	}
 
	public ArrayList<MessageRecord> getReceiveMessageRecords() {
		return receiveMessageRecords;
	}
 
	public void setReceiveMessageRecords(ArrayList<MessageRecord> receiveMessageRecords) {
		this.receiveMessageRecords = receiveMessageRecords;
	}
 
}
 
class LandlinePhoneCharging extends ChargeMode {
 
	private double monthlyRent = 20;
 
	public LandlinePhoneCharging() {
		super();
		chargeRules.add(new LandPhoneInCityRule());
		chargeRules.add(new LandPhoneInProvinceRule());
		chargeRules.add(new LandPhoneInlandRule());
	}
 
	@Override
	public double calCost(UserRecords userRecords) {
		double sumCost = 0;
		for (ChargeRule rule : chargeRules) {
			sumCost += rule.calCost(userRecords);
		}
		return sumCost;
	}
 
	@Override
	public double getMonthlyRent() {
		return monthlyRent;
	}
 
}
 
class MobilePhoneCharging extends ChargeMode {
 
	private double monthlyRent = 15;
 
	public MobilePhoneCharging() {
		super();
		chargeRules.add(new MobilePhoneInCityRule());
		chargeRules.add(new MobilePhoneInProvinceRule());
		chargeRules.add(new MobilePhoneInlandRule());
	}
 
	@Override
	public double calCost(UserRecords userRecords) {
		double sumCost = 0;
		for (ChargeRule rule : chargeRules) {
			sumCost += rule.calCost(userRecords);
		}
		return sumCost;
	}
 
	@Override
	public double getMonthlyRent() {
		return monthlyRent;
	}
 
}
 
class MobilePhoneMassageCharging extends ChargeMode {
 
	private double monthlyRent = 0;
 
	public MobilePhoneMassageCharging() {
		super();
		chargeRules.add(new MobilePhoneMessageRule());
	}
 
	@Override
	public double calCost(UserRecords userRecords) {
		double sumCost = 0;
		for (ChargeRule rule : chargeRules) {
			sumCost += rule.calCost(userRecords);
		}
		return sumCost;
	}
 
	@Override
	public double getMonthlyRent() {
		return monthlyRent;
	}
 
}
 
class Inputdeal {
 
	public int check(String input) {
		if (input.matches("[u]-0791[0-9]{7,8}\\s[0]") || input.matches("[u]-1[0-9]{10}\\s[13]")) {
			return 1;
		} else if (input.matches("[m]-1[0-9]{10}\\s" + "1[0-9]{10}\\s" + "[0-9a-zA-Z\\s\\.,]+")) {
			return 2;
		}
		return 0;
	}
 
	public void writeUser(ArrayList<User> users, String input) {
		User usernew = new User();
		String[] inputs = input.split(" ");
		String num = inputs[0].substring(2);
		for (User i : users) {
			if (i.getNumber().equals(num)) {
				return;
			}
		}
		usernew.setNumber(num);
		int mode = Integer.parseInt(inputs[1]);
		if (mode == 0) {
			usernew.setChargeMode(new LandlinePhoneCharging());
		} else if (mode == 1) {
			usernew.setChargeMode(new MobilePhoneCharging());
		} else if (mode == 3) {
			usernew.setChargeMode(new MobilePhoneMassageCharging());
		}
		users.add(usernew);
	}
 
	public void writeRecord(ArrayList<User> users, String input) {
		String[] inputs = input.split(" ");
		inputs[0] = inputs[0].substring(2);
 
		User callu = null, answeru = null;
 
		String out = inputs[0];
		String in = "";
		if (inputs.length == 6) {
			in = inputs[1];
		} else if (inputs.length == 7) {
			in = inputs[1];
		} else if (inputs.length == 8) {
			in = inputs[2];
		} else {
			in = inputs[1];
		}
 
		for (User i : users) {
			if (i.getNumber().equals(out)) {
				callu = i;
			}
			if (i.getNumber().equals(in)) {
				answeru = i;
			}
			if (callu != null && answeru != null) {
				break;
			}
		}
 
		if (input.charAt(0) == 'm') {
			MessageRecord messageRecord = new MessageRecord(input);
			if (callu != null) {
				callu.getUserRecords().addSendMessageRecords(messageRecord);
			}
			if (answeru != null) {
				callu.getUserRecords().addReceiveMessageRecords(messageRecord);
			}
		}
 
	}
 
}
 
abstract class CommunicationRecord {
	protected String callingNumber;
	protected String answerNumbe;
 
	public String getCallingNumber() {
		return callingNumber;
	}
 
	public void setCallingNumber(String callingNumber) {
		this.callingNumber = callingNumber;
	}
 
	public String getAnswerNumbe() {
		return answerNumbe;
	}
 
	public void setAnswerNumbe(String answerNumbe) {
		this.answerNumbe = answerNumbe;
	}
 
}
 
abstract class ChargeRule {
 
	abstract public double calCost(UserRecords userRecords);
 
}
 
class CallRecord extends CommunicationRecord {
	private Date startTime;
	private Date endTime;
	private String callingAddressAreaCode;
	private String answerAddressAreaCode;
 
	public String getCallType() {
		String type = "";
		if (callingAddressAreaCode.equals("0791")) {
			type = type.concat("1");
		} else if (callingAddressAreaCode.matches("^079[023456789]$") || callingAddressAreaCode.equals("0701")) {
			type = type.concat("2");
		} else {
			type = type.concat("3");
		}
 
		if (answerAddressAreaCode.equals("0791")) {
			type = type.concat("1");
		} else if (answerAddressAreaCode.matches("^079[023456789]$") || answerAddressAreaCode.equals("0701")) {
			type = type.concat("2");
		} else {
			type = type.concat("3");
		}
 
		return type;
	}
 
	public CallRecord(String[] inputs) {
		super();
 
		char type = inputs[0].charAt(0);
 
		String sd = null, st = null, ed = null, et = null;
 
		if (type == 't') {
			if (inputs.length == 6) {
				sd = inputs[2];
				st = inputs[3];
				ed = inputs[4];
				et = inputs[5];
				callingAddressAreaCode = inputs[0].substring(0, 4);
				answerAddressAreaCode = inputs[1].substring(0, 4);
			} else if (inputs.length == 7) {
				sd = inputs[3];
				st = inputs[4];
				ed = inputs[5];
				et = inputs[6];
				if (inputs[0].charAt(0) != '0') {
					if (inputs[2].length() == 10) {
						answerAddressAreaCode = inputs[2].substring(0, 3);
					} else {
						answerAddressAreaCode = inputs[2].substring(0, 4);
					}
					callingAddressAreaCode = inputs[1];
				} else {
					if (inputs[0].length() == 10) {
						callingAddressAreaCode = inputs[0].substring(0, 3);
					} else {
						callingAddressAreaCode = inputs[0].substring(0, 4);
					}
					answerAddressAreaCode = inputs[2];
				}
			} else if (inputs.length == 8) {
				sd = inputs[4];
				st = inputs[5];
				ed = inputs[6];
				et = inputs[7];
				callingAddressAreaCode = inputs[1];
				answerAddressAreaCode = inputs[3];
			}
		} else if (type == 'm') {
 
		}
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault());
		try {
			startTime = simpleDateFormat.parse(sd + " " + st);
			endTime = simpleDateFormat.parse(ed + " " + et);
		} catch (ParseException e) {
		}
 
	}
 
	public CallRecord(Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) {
		super();
		this.startTime = startTime;
		this.endTime = endTime;
		this.callingAddressAreaCode = callingAddressAreaCode;
		this.answerAddressAreaCode = answerAddressAreaCode;
	}
 
	public Date getStartTime() {
		return startTime;
	}
 
	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}
 
	public Date getEndTime() {
		return endTime;
	}
 
	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}
 
	public String getCallingAddressAreaCode() {
		return callingAddressAreaCode;
	}
 
	public void setCallingAddressAreaCode(String callingAddressAreaCode) {
		this.callingAddressAreaCode = callingAddressAreaCode;
	}
 
	public String getAnswerAddressAreaCode() {
		return answerAddressAreaCode;
	}
 
	public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
		this.answerAddressAreaCode = answerAddressAreaCode;
	}
}
 
abstract class CallChargeRule extends ChargeRule {
 
}
 
class LandPhoneInCityRule extends CallChargeRule {
 
	@Override
	public double calCost(UserRecords userRecords) {
		double sumCost = 0;
		for (CallRecord call : userRecords.getCallingInCityRecords()) {
			double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
			if (distanceS < 0) {
				continue;
			}
			double distanceM = (int) distanceS / 60;
			if (distanceS % 60 != 0) {
				distanceM += 1;
			}
			if (call.getCallType().equals("11")) {
				sumCost += distanceM * 0.1;
			} else if (call.getCallType().equals("12")) {
				sumCost += distanceM * 0.3;
			} else if (call.getCallType().equals("13")) {
				sumCost += distanceM * 0.6;
			}
		}
		return sumCost;
	}
 
}
 
class LandPhoneInlandRule extends CallChargeRule {
 
	@Override
	public double calCost(UserRecords userRecords) {
		double sumCost = 0;
		for (CallRecord call : userRecords.getCallingInLandRecords()) {
			double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
			if (distanceS < 0) {
				continue;
			}
			double distanceM = (int) distanceS / 60;
			if (distanceS % 60 != 0) {
				distanceM += 1;
			}
			sumCost += distanceM * 0.6;
		}
		return sumCost;
	}
 
}
 
class LandPhoneInProvinceRule extends CallChargeRule {
 
	@Override
	public double calCost(UserRecords userRecords) {
		double sumCost = 0;
		for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
			double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
			if (distanceS < 0) {
				continue;
			}
			double distanceM = (int) distanceS / 60;
			if (distanceS % 60 != 0) {
				distanceM += 1;
			}
			sumCost += distanceM * 0.3;
		}
		return sumCost;
	}
 
}
 
class MobilePhoneInCityRule extends CallChargeRule {
 
	@Override
	public double calCost(UserRecords userRecords) {
		double sumCost = 0;
		for (CallRecord call : userRecords.getCallingInCityRecords()) {
			double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
			if (distanceS < 0) {
				continue;
			}
			double distanceM = (int) distanceS / 60;
			if (distanceS % 60 != 0) {
				distanceM += 1;
			}
			if (call.getCallType().equals("11")) {
				sumCost += distanceM * 0.1;
			} else if (call.getCallType().equals("12")) {
				sumCost += distanceM * 0.2;
			} else if (call.getCallType().equals("13")) {
				sumCost += distanceM * 0.3;
			}
 
		}
		return sumCost;
	}
 
}
 
class MobilePhoneInlandRule extends CallChargeRule {
 
	@Override
	public double calCost(UserRecords userRecords) {
		double sumCost = 0;
		for (CallRecord call : userRecords.getCallingInLandRecords()) {
			double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
			if (distanceS < 0) {
				continue;
			}
			double distanceM = (int) distanceS / 60;
			if (distanceS % 60 != 0) {
				distanceM += 1;
			}
			sumCost += distanceM * 0.6;
		}
		for (CallRecord call : userRecords.getAnswerInLandRecords()) {
			double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
			if (distanceS < 0) {
				continue;
			}
			double distanceM = (int) distanceS / 60;
			if (distanceS % 60 != 0) {
				distanceM += 1;
			}
			sumCost += distanceM * 0.3;
		}
		return sumCost;
	}
 
}
 
class MobilePhoneInProvinceRule extends CallChargeRule {
 
	@Override
	public double calCost(UserRecords userRecords) {
		double sumCost = 0;
		for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
			double distanceS = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
			if (distanceS < 0) {
				continue;
			}
			double distanceM = (int) distanceS / 60;
			if (distanceS % 60 != 0) {
				distanceM += 1;
			}
			if (call.getCallType().equals("21")) {
				sumCost += distanceM * 0.3;
			} else if (call.getCallType().equals("22")) {
				sumCost += distanceM * 0.3;
			} else if (call.getCallType().equals("23")) {
				sumCost += distanceM * 0.3;
			}
		}
		return sumCost;
	}
 
}
 
class MobilePhoneMessageRule extends CallChargeRule {
 
	@Override
	public double calCost(UserRecords userRecords) {
		double sumCost = 0;
		int number = 0;
		for (MessageRecord m : userRecords.getSendMessageRecords()) {
			int length = m.getMessage().length();
			if (length <= 10) {
				number++;
			} else {
				number += length / 10;
				if (length % 10 != 0) {
					number++;
				}
			}
		}
		if (number <= 3) {
			sumCost = number * 0.1;
		} else if (number <= 5) {
			sumCost = 0.3 + 0.2 * (number - 3);
		} else {
			sumCost = 0.7 + 0.3 * (number - 5);
		}
		return sumCost;
	}
 
}
 
class MessageRecord extends CommunicationRecord {
 
	private String message;
 
	public MessageRecord(String input) {
		super();
		this.message = input.substring(26);
	}
 
	public String getMessage() {
		return message;
	}
 
	public void setMessage(String message) {
		this.message = message;
	}
}
 
class User {
 
	private UserRecords userRecords = new UserRecords();
	private double balance = 100;
	private ChargeMode chargeMode;
	private String number;
 
	public double calCost() {
		return chargeMode.calCost(userRecords);
	}
 
	public double calBalance() {
		return balance - chargeMode.getMonthlyRent() - chargeMode.calCost(userRecords);
	}
 
	public UserRecords getUserRecords() {
		return userRecords;
	}
 
	public void setUserRecords(UserRecords userRecords) {
		this.userRecords = userRecords;
	}
 
	public ChargeMode getChargeMode() {
		return chargeMode;
	}
 
	public void setChargeMode(ChargeMode chargeMode) {
		this.chargeMode = chargeMode;
	}
 
	public String getNumber() {
		return number;
	}
 
	public void setNumber(String number) {
		this.number = number;
	}
 
}
 
class Outputtool {
 
	@SuppressWarnings("deprecation")
	public void output(double out) {
//		java.text.DecimalFormat df=new java.text.DecimalFormat("#.##");
//		String a=df.format(out);
//		System.out.print(a);
		BigDecimal numb = new BigDecimal(out);
		out = numb.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		System.out.print(out);
	}
}

题目分析:

类图如下:

度量分析:

 

   本题只需要实现短信计费即可,相比于前两次的电信计费题目都更简单一些。所以我在原本代码的基础做了删减,同时增加了一些跟需求有关的类,例如短信计费规则类MassageRule,短信的计费类则直接使用原本代码的MobilePhoneChargeing。

踩坑心得:

  短信对于电话来说相对的简单一些,需要判断的情况也没有电话那么复杂。需要注意的就是短信条数的统计,短信计费是通过发送短信的条数进行的,短信条数也包括一条短信内容中每8个字算一条短信,不足8个算成8个来计算。

改进建议:

  这题可以将获取字符串变成一个字符数组;可以将split方法改进,提高程序的执行效率!

 

三、总结

  在这几次的题目集中,我从中学习到了SimpleDateFormat类如何使用;了解了try catch 错误处理,执行规则;如何对ArrayList中sort方法的重写;还有内部类使用方法及好处等等。

  在这段的学习当中,我学习了很多,有封装,多态,抽象类,接口,容器,内部类,还有最后面的javafx,我在接口,javafx方面掌握的不好,并且存在很多知识漏洞,有大问题也有细微的错误,在这当中我对于类以及对象有了更深的了解,并在基础的语法下熟练使用继承、多态、抽象类、包装类进行编写代码,对JAVA有了进一步的理解;面向对象的程序设计的重点在于如何创建对象,以及使用对象来做一些事,这使得理解类与对象是尤为的重要;对于题目的了解也显得尤为重要,不要毫无头绪进行编写,要有方向,这要才有更高的效率,通过编写题目要搞清楚自己的缺点在哪里、不足在哪里,要对其进行及时的弥补,多进行总结,多进行思考,通过本次的学习我发现了自身不足有以下几点:

  1、当类和对象非常多时,我对类之间的关系就有些混乱,这说明我还需要多加练习,应用需要更加熟练。

  2、对包装类中的方法不够清楚,导致使用时常常报错,在之后需要多次使用其中的方法。

  3、数学方面的逻辑思维能力有点差,导致解决问题时不能立刻想出解决问题的办法,今后还需要多编写一些类似的算法题,让自己更加熟练针对不同的题因该用何种方法进行相应计算。

 

posted @ 2022-12-08 16:19  曦-Aurora  阅读(76)  评论(0)    收藏  举报