首页 > 编程语言 >Java 第三阶段总结

Java 第三阶段总结

时间:2022-12-10 19:56:46浏览次数:47  
标签:总结 第三阶段 Java String ArrayList callRecord void return public

前言

这几次的PTA相对于前面的多边形来说,我觉得更简单了。因为给了对应的类图,只需要弄明白每个类的作用,就很好写了。而且老师给的时间也很充足,不存在没写完的情况,只不过要多注意细节,特别是正则表达式的判断,这部分很重要。

知识点主要考察的是继承、多态以及一些库类的应用。

设计分析

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.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String str = in.nextLine();
		ArrayList<User> users = new ArrayList<>();

            while(!str.equals("end")) {
                try {
                    Inputer.disposeStr(users,str);    
                } catch (Exception e) {}
                str = in.nextLine();
            }
		//compare()接受两个同类型的不同对象进行比较 
		users.sort(new Comparator<User>() {
			@Override
			public int compare(User u1, User u2) {
				if (Double.parseDouble(u1.getNumber()) > Double.parseDouble(u2.getNumber())) {
					return 1;
				} else {
					return -1;
				}
			}
		});
		
		for (User u : users) {
			String temp = String.format("%.1f",u.calCost());
			double cost = Double.parseDouble(temp);
			temp = String.format("%.1f",u.calBalance());
			double balance = Double.parseDouble(temp);
			System.out.println(u.getNumber() + " "+ cost+" "+balance);
		}
		in.close();
	}
}
/**
 * 用户类
 * 包括手机和座机用户 
 * 手机用户有两种计费方式 1,2 
 * 座机用户只有一种即 0 固定月租为20
 */
class User {
	//用户记录
	private UserRecords userRecords = new UserRecords();
	//余额
	private double balance = 100;
	//计费方式
	private ChargeMode chargeMode;
	//号码
	private String number;
	
	//get、set方法
	public UserRecords getUserRecords() {
		return userRecords;
	}
	
	public void setUserRecords(UserRecords userRecords) {
		this.userRecords = userRecords;
	}
	
	public double getBalance() {
		return balance;
	}
	
	public void setBalance(double balance) {
		this.balance = balance;
	}
	
	public 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;
	}
	
	/**
	 * 计算每月余额
	 * @return 
	 */
	public double calBalance() {
		return balance - chargeMode.getMonthlyRent() - chargeMode.calCost(userRecords);
	}
	
	/**
	 * 计算每月消费
	 * @return
	 */
	public double calCost() {
		return chargeMode.calCost(userRecords);
	}
}

/**
 * 用户记录类,保存用户各种通话、短信的记录
 */
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>();
 
	/**
	 *  添加市内拨打电话记录
	 * @param callRecord 通话记录
	 */
	public void addCallingInCityRecords(CallRecord callRecord) {
		callingInCityRecords.add(callRecord);
	}
	/**
	 *  添加省内拨打电话记录
	 * @param callRecord 通话记录
	 */
	public void addCallingInProvinceRecords(CallRecord callRecord) {
		callingInProvinceRecords.add(callRecord);
	}
	/**
	 *  添加省外拨打电话记录
	 * @param callRecord 通话记录
	 */
	public void addCallingInLandRecords(CallRecord callRecord) {
		callingInLandRecords.add(callRecord);
	}
	/**
	 *  添加市内接收电话记录
	 * @param callRecord 通话记录
	 */
	public void addAnswerInCityRecords(CallRecord callRecord) {
		answerInCityRecords.add(callRecord);
	}
	/**
	 *  添加省内接收电话记录
	 * @param callRecord 通话记录
	 */
	public void aaddAnswerInProvinceRecords(CallRecord callRecord) {
		answerInProvinceRecords.add(callRecord);
	}
	/**
	 *  添加省外接收电话记录
	 * @param callRecord 通话记录
	 */
	public void addAnswerInLandRecords(CallRecord callRecord) {
		answerInLandRecords.add(callRecord);
	}
	/**
	 *  添加发送短信记录
	 * @param callRecord 短信记录
	 */
	public void addSendMessageRecords(MessageRecord callRecord) {
		sendMessageRecords.add(callRecord);
	}
	/**
	 *  添加接受短信记录
	 * @param callRecord 短信记录
	 */
	public void addReceiveMessageRecords(MessageRecord callRecord) {
		receiveMessageRecords.add(callRecord);
	}
 
	//以下为各种属性的set、get方法
	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 MessageRecord extends CommunicationRecord{
	//记录短信的信息
	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
}

/**
 * 座机拨打省内电话的计费规则类
 */
class LandPhoneInProvinceRule extends CallChargeRule {

	@Override
	public double calCost(UserRecords userRecords) {
		double sum = 0;
		for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
			// .getTime()返回毫秒数 即 /1000
			double second = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
			
			if (second < 0) {
				continue;
			}
			double minute = (int) second / 60;
			//不足一分钟按一分钟计算
			if (second % 60 != 0) {
				minute += 1;
			}
			sum += minute * 0.3;
		}
		return sum;
	}

}

/**
 * 	座机拨打省外电话的计费规则类
 */
 class LandPhoneInLandRule extends CallChargeRule{

	@Override
	public double calCost(UserRecords userRecords) {
		double sum = 0;
		for (CallRecord call : userRecords.getCallingInLandRecords()) {
			// .getTime()返回毫秒数 即 /1000
			double second = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
			
			if (second < 0) {
				continue;
			}
			double minute = (int) second / 60;
			//不足一分钟按一分钟计算
			if (second % 60 != 0) {
				minute += 1;
			}
			sum += minute * 0.6;
		}
		return sum;
	}

}
/**
 *	座机拨打市内电话的计费规则类
 */
 class LandPhoneInCityRule extends CallChargeRule{

	@Override
	public double calCost(UserRecords userRecords) {
		double sum = 0;
		//每条通话记录计算总额
		for (CallRecord call : userRecords.getCallingInCityRecords()) {
			// .getTime()返回毫秒数 即 /1000
			double second = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
//			System.out.print(second);
			if (second < 0) {
				continue;
			}
			double minute = (int) second / 60;
			if (second % 60 != 0) {
				minute += 1;
			}
			sum += minute * 0.1;
		}
		return sum;
	}
}

/**
 *  该类为座机计费方式的计算
 */
 class LandlinePhoneCharging extends ChargeMode {
	//座机月租固定为20元
	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 Inputer {

	/**
	 * 对输入的每一行信息做处理 利用正则判断格式对错 以及 是否符合要求
	 * 
	 * @param str 传入输入的信息
	 */
	public static void disposeStr(ArrayList<User> users, String str) {
		String[] s = str.split(" ");
		String flag = str.substring(0, 1);
		if (flag.equals("u")) {
			if (s[0].matches("^u-(079[0-9]|0701)\\d{7,8}$") && s[1].matches("^[0-2]$")) {
				dealUsers(users, str);
			}
		} else if (flag.equals("t")) { // 是否需要考虑 时间1比时间2大的情况
			if (s[0].matches("^t-(079[0-9]|0701)\\d{7,8}$") && s[1].matches("^[0-9]{10,12}$")) {
				if (matchDate(s[2]) && matchDate(s[4]) && matchTime(s[3]) && matchTime(s[5])) {
					dealRecords(users, str);
				}
			}
		} else if (flag.equals("m")) {

		}

	}

	

	/**
	 * 对用户的信息做处理 目前只包含座机用户之间的处理
	 * 
	 * @param user 用户表
	 * @param str  用户信息
	 */
	public static void dealUsers(ArrayList<User> users, String str) {
		User u = new User();
		String in[] = str.split(" ");
        String number = in[0].substring(2);
		// 先检查号码是否重复 不可以有重复号码
		for (User user : users) {
			if (user.getNumber().equals(number) ) {
				return;
			}
		}
		if (in[1].equals("0")) { // 座机用户
			u.setChargeMode(new LandlinePhoneCharging());
		} else if (in[1].equals("1")) {// 手机用户 计费方式1

		} else if (in[1].equals("2")) {// 手机用户 计费方式2

		}
		u.setNumber(number);
		users.add(u);
	}

	/**
	 * 对通信的内容做处理d
	 * @param user 用户表
	 * @param str 通信记录 包括通话和短信
	 */
	public static void dealRecords(ArrayList<User> user,String str) {
		CallRecord call = new CallRecord();
		String in[] = str.split(" ");
        String number = in[0].substring(2);
//		for(int i=0;i<in.length;i++)
//			System.out.println(in[i]+" ");
		User callu = null,answeru = null;
		//先判断拨打 或者接听的用户是否在用户表内 即该次通话是否合理
		for(User i:user) {
			if(i.getNumber().equals(number)) {
				callu = i;
			}
			if(i.getNumber().equals(in[1])) {
				answeru = i;
			}
		}
		call.setCallingAddressAreaCode(in[0].substring(2,6));
		call.setAnswerAddressAreaCode(in[1].substring(0,4));
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
		try {
			call.setStartTime(simpleDateFormat.parse(in[2] + " " + in[3]));
			call.setEndTime(simpleDateFormat.parse(in[4] + " " + in[5]));
		} catch (ParseException e) {
		}
		if(callu!=null) {
			if(matchAdressArea(call.getAnswerAddressAreaCode()) == 1) {
				callu.getUserRecords().addCallingInCityRecords(call);
			}
			else if(matchAdressArea(call.getAnswerAddressAreaCode()) == 2) {
				callu.getUserRecords().addCallingInProvinceRecords(call);
			}else callu.getUserRecords().addCallingInLandRecords(call);
		}
		if(answeru!=null) {
			if(matchAdressArea(call.getCallingAddressAreaCode()) == 1) {
				callu.getUserRecords().addAnswerInCityRecords(call);
			}
			else if(matchAdressArea(call.getCallingAddressAreaCode()) == 2) {
				callu.getUserRecords().aaddAnswerInProvinceRecords(call);
			}else callu.getUserRecords().addAnswerInLandRecords(call);
		}
	}
	//判断通话类型 
	public static void getCallType() {
		
	}
	//匹配区号的地域 是否为市内1、省内2、省外3
	public static int matchAdressArea(String str) {
		if(str.matches("^0791$")) {
			return 1;
		}else if(str.matches("^(079[023456789]|0701)$")){
			return 2;
		}else return 3;
	}
	// 匹配日期
	public static boolean matchDate(String str) {
		
		String[] data = str.split("\\.");
		//先判断大致格式
		String regEx = "^(19|20)\\d\\d\\.([1-9]|1[0-2])\\.([1-9]|[12][0-9]|3[01])$";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		if (!m.matches()) {
			return false;
		}
		//下面判断日期是否合理 以及润平年的判断
		int year = Integer.parseInt(data[0]);
		int month = Integer.parseInt(data[1]);
		int day = Integer.parseInt(data[2]);
		int[] days = {0,31,28,31,30,31,30,31,31,30,31,30,31};//平年
		
		if(isLeapYear(year)) {
			days[2]=29;
		}
		//月日的判断
		for(int i=1;i<=12;i++) {
			if(day>days[month] || day<=0) {
				return false;
			}
		}
		return true;
	}
    //闰年
	public static boolean isLeapYear(int year) {
		return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
	}


	// 匹配时间
	public static boolean matchTime(String str) {
		String regEx = "^([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		if (!m.matches()) {
			return false;
		}
		return true;
	}
	//对时间进行处理
	public static void dealDate(String[] inputs) {

	}
}


/**
 *   抽象类、通讯记录类 
 *   包括通话和发送短信,记录两者的号码
 */
 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);
}


/**
 * 计费方式的抽象类
 */
 abstract class ChargeMode {
	//计费方式所包含的各种计费规则的集合
	protected ArrayList<ChargeRule> chargeRules = new ArrayList<>();
	 
	public abstract double calCost(UserRecords userRecords);
 
	/**
	 * @return 返回月租
	 */
	public abstract double getMonthlyRent();
	//计费规则的get、set方法
	public ArrayList<ChargeRule> getChargeRules() {
		return chargeRules;
	}
 
	public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
		this.chargeRules = chargeRules;
	}

}

/**
 * 通话记录类
 *
 */
 class CallRecord extends CommunicationRecord{
	//通话的起始、结束时间
	private Date startTime;
	private Date endTime;
	//拨号地点的区号、接听地点的区号
	//区号用于记录在哪个地点拨打和接听的电话 座机无法移动,就是本机区号,如果是手机号,则会有差异。
	private String callingAddressAreaCode;
	private String answerAddressAreaCode;
	
	public CallRecord() {
		
	}
	
	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{
	
}

(这里就不放自己的类图了,因为是根据题目的类图写的)

报表:

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

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

输入样例:
在这里给出一组输入。例如:
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

代码:

点击查看代码
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String str = in.nextLine();
		ArrayList<User> users = new ArrayList<>();
		while(!str.equals("end")) {
			 try {
                    Inputer.disposeStr(users,str);    
                } catch (Exception e) {}
			str = in.nextLine();
		}
		
		//compare()接受两个同类型的不同对象进行比较  
		//排序 方法重写 
		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) {
			//格式化 保留两位小数
			String temp = String.format("%.1f",u.calCost());
			double cost = Double.parseDouble(temp);
			temp = String.format("%.1f",u.calBalance());
			double balance = Double.parseDouble(temp);
			System.out.println(u.getNumber() + " "+ cost+" "+balance);
		}
		in.close();
	}
}

/**
 * 拨打电话计费规则抽象类
 */
abstract class CallChargeRule extends ChargeRule{
	
}

/**
 * 通话记录类
 *
 */
class CallRecord extends CommunicationRecord {
	// 通话的起始、结束时间
	private Date startTime;
	private Date endTime;
	// 拨号地点的区号、接听地点的区号
	// 区号用于记录在哪个地点拨打和接听的电话 座机无法移动,就是本机区号,如果是手机号,则会有差异。
	private String callingAddressAreaCode;
	private String answerAddressAreaCode;

	public CallRecord(String[] in) {
		super();

		String st1 = null, st2 = null, ed1 = null, ed2 = null;  //分别记录时间

		if (in.length == 6) { // 座机打座机
			st1 = in[2];
			st2 = in[4];
			ed1 = in[3];
			ed2 = in[5];

			if (in[0].length() == 10) { // 3位区号 7位座机号 4 7 |3 8 
				callingAddressAreaCode = in[0].substring(2, 5);
				answerAddressAreaCode = in[1].substring(0, 3);
			} else {
				callingAddressAreaCode = in[0].substring(2, 6);
				answerAddressAreaCode = in[1].substring(0, 4);
			}
		} else if (in.length == 7) { // 座机打手机或- 座机打手机
			st1 = in[3];
			st2 = in[5];
			ed1 = in[4];
			ed2 = in[6];
			if (in[0].charAt(2) != '0') { //shoujida
				if (in[2].length() == 10) {
					answerAddressAreaCode = in[2].substring(0, 3);
				} else {
					answerAddressAreaCode = in[2].substring(0, 4);
				}
				callingAddressAreaCode = in[1];
			} else { //座机打手机
				if (in[0].length() == 10) {
					callingAddressAreaCode = in[0].substring(2, 5);
				} else {
					callingAddressAreaCode = in[0].substring(2, 6);
				}
				answerAddressAreaCode = in[2];
			}
		} else if (in.length == 8) { // 手机打手机
			st1 = in[4];
			st2 = in[6];
			ed1 = in[5];
			ed2 = in[7];
			callingAddressAreaCode = in[1];
			answerAddressAreaCode = in[3]; 
		}
		// 时间
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
		try {
			startTime = simpleDateFormat.parse(st1 + " " + ed1);
			endTime = simpleDateFormat.parse(st2 + " " + ed2);
		} 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 ChargeMode {
	//计费方式所包含的各种计费规则的集合 
	//
	protected ArrayList<ChargeRule> chargeRules = new ArrayList<>();
	 
	/**
	 * 计算每月通话 发送短信的消费
	 */
	public abstract double calCost(UserRecords userRecords);
 
	/**
	 * @return 返回月租
	 */
	public abstract double getMonthlyRent();
	
	//计费规则的get、set方法
	public ArrayList<ChargeRule> getChargeRules() {
		return chargeRules;
	}
 
	public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
		this.chargeRules = chargeRules;
	}

}

/**
 * 计费规则抽象类
 */
abstract class ChargeRule {
	abstract public double calCost(UserRecords userRecords);
}

/**
 *   抽象类、通讯记录类 
 *   包括通话和发送短信,记录两者的号码
 */
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;
	}

}

/**
 * 对输入进行处理的一个类
 */
class Inputer {

	/**
	 * 对输入的每一行信息做处理 利用正则判断格式对错 以及 是否符合要求
	 * 
	 * @param str 传入输入的信息
	 */
	public static void disposeStr(ArrayList<User> users, String str) {
		String[] s = str.split(" ");
		String flag = str.substring(0, 1);
		if (flag.equals("u")) {
            if(str.matches("^u-(0[0-9]{10,11}\\s[0]|1\\d{10}\\s[1])$")) {
				dealUsers(users, str);
			}
// 			if ((s[0].matches("^u-(079[0-9]|0701)\\d{7,8}$") || s[0].matches("^u-1[0-9]{10}$"))
// 					&& s[1].matches("^[0-2]$")) {
// 				dealUsers(users, str);
// 			}
		} else if (flag.equals("t")) { // 是否需要考虑 时间1比时间2大的情况
            if(str.matches("^t-0[0-9]{9,11}\\s"+"0[0-9]{9,11}\\s\\d{4}.\\d{1,2}.\\d{1,2}\\s\\d{2}:\\d{2}:\\d{2}\\s\\d{4}.\\d{1,2}.\\d{1,2}\\s\\d{2}:\\d{2}:\\d{2}$")){
// 			if (s[0].matches("^t-(010|\\d{4})\\d{7,8}$") && s[1].matches("^[0-9]{10,12}$")) {
				if (matchDate(s[2]) && matchDate(s[4]) && matchTime(s[3]) && matchTime(s[5])) {
					dealRecords(users, str);
				}
			}
            if(str.matches("^t-0[0-9]{9,11}\\s[1]\\d{10}\\s(\\d){3,4}\\s\\d{4}.\\d{1,2}.\\d{1,2}\\s\\d{2}:\\d{2}:\\d{2}\\s\\d{4}.\\d{1,2}.\\d{1,2}\\s\\d{2}:\\d{2}:\\d{2}$")){
// 			if (s[0].matches("^t-(010|\\d{4})\\d{7,8}$") && s[1].matches("^1[0-9]{10}$")) {
				if (matchDate(s[3]) && matchDate(s[5]) && matchTime(s[4]) && matchTime(s[6])) {
					dealRecords(users, str);
				}
			}
            if(str.matches("^t-1(\\d){10}\\s(\\d){3,4}\\s0[0-9]{9,11}\\s\\d{4}.\\d{1,2}.\\d{1,2}\\s\\d{2}:\\d{2}:\\d{2}\\s\\d{4}.\\d{1,2}.\\d{1,2}\\s\\d{2}:\\d{2}:\\d{2}$")){
// 			if (s[0].matches("^t-1[0-9]{10}$") && s[2].matches("^(010|\\d{4})\\d{7,8}$") ) {
				if (matchDate(s[3]) && matchDate(s[5]) && matchTime(s[4]) && matchTime(s[6])) {
					dealRecords(users, str);
				}
			}
            if(str.matches("^t-1(\\d){10}\\s\\s(\\d){3,4}\\s1(\\d){10}\\s(\\d){3,4}\\s\\d{4}.\\d{1,2}.\\d{1,2}\\s\\d{2}:\\d{2}:\\d{2}\\s\\d{4}.\\d{1,2}.\\d{1,2}\\s\\d{2}:\\d{2}:\\d{2}$")){
// 			if (s[0].matches("^t-1[0-9]{10}$") && s[2].matches("^1[0-9]{10}$")) {
				if (matchDate(s[4]) && matchDate(s[6]) && matchTime(s[5]) && matchTime(s[7])) {
					dealRecords(users, str);
				}
			}

		} else if (flag.equals("m")) {

		}
	}

	/**
	 * 对用户的信息做处理 目前只包含座机用户之间的处理
	 * 
	 * @param user 用户表
	 * @param str  用户信息
	 */
	public static void dealUsers(ArrayList<User> users, String str) {
		User u = new User();
		String in[] = str.split(" ");
		String number = in[0].substring(2);
		// 先检查号码是否重复 不可以有重复号码
		for (User user : users) {
			if (user.getNumber().equals(number)) {
				return;
			}
		}
		u.setNumber(number);
		if (in[1].equals("0")) { // 座机用户
			u.setChargeMode(new LandlinePhoneCharging());
		} else if (in[1].equals("1")) { // 手机用户 计费方式1
			u.setChargeMode(new MobilePhoneCharging());
		} else if (in[1].equals("2")) { // 手机用户 计费方式2

		}
		
		users.add(u);
	}

	/**
	 * 对通信的内容做处理d
	 * 
	 * @param user 用户表
	 * @param str  通信记录 包括通话 -短信
	 */
	public static void dealRecords(ArrayList<User> user, String str) {
		String in[] = str.split(" ");
		String number = in[0].substring(2);
			// 对字符处理得到该次通话记录
		CallRecord call = new CallRecord(in);
		
		int flag = 0;
		if(in.length==8)
			flag = 2;
		else if(in.length==6)
			flag = 1;
		else if(in.length==7 ) {
			if(in[1].length()<=4)
				flag = 2;
			else flag = 1;
		}
		User callu = null, answeru = null;
		// 先判断拨打 或者接听的用户是否在用户表内 即该次通话是否合理
		for (User i : user) {
			if (i.getNumber().equals(number)) {
				callu = i;
			}
			if (i.getNumber().equals(in[flag])) {
				answeru = i;
			}
		}
		// 该次通话 的拨打人在用户表内
		if (callu != null) {
			if (matchAdressArea(call.getCallingAddressAreaCode()) == 1) {
				callu.getUserRecords().addCallingInCityRecords(call); //市内拨打
			} else if (matchAdressArea(call.getCallingAddressAreaCode()) == 2) {
				callu.getUserRecords().addCallingInProvinceRecords(call);//省内拨打
			} else
				callu.getUserRecords().addCallingInLandRecords(call);//省外拨打
		}
		// 该次通话 的接听人在用户表内
		if (answeru != null) {
			if (matchAdressArea(call.getAnswerAddressAreaCode()) == 1) {
				answeru.getUserRecords().addAnswerInCityRecords(call); //市内接听
			} else if (matchAdressArea(call.getAnswerAddressAreaCode()) == 2) {
				answeru.getUserRecords().aaddAnswerInProvinceRecords(call); //省内接听
			} else
				answeru.getUserRecords().addAnswerInLandRecords(call); //省外接听
		}
	}

	// 判断通话类型
	public static void getCallType() {

	}

	// 匹配区号的地域 是否为市内1、省内2、省外3
	public static int matchAdressArea(String str) {
		if (str.matches("^0791$")) {
			return 1;
		} else if (str.matches("^(079[023456789]|0701)$")) {
			return 2;
		} else
			return 3;
	}

	// 匹配日期
	public static boolean matchDate(String str) {

		String[] data = str.split("\\.");
		// 先判断大致格式
		String regEx = "^(19|20)\\d\\d\\.([1-9]|1[0-2])\\.([1-9]|[12][0-9]|3[01])$";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		if (!m.matches()) {
			return false;
		}
		// 下面判断日期是否合理 以及润平年的判断
		int year = Integer.parseInt(data[0]);
		int month = Integer.parseInt(data[1]);
		int day = Integer.parseInt(data[2]);
		int[] days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };// 平年

		if (isLeapYear(year)) {
			days[2] = 29;
		}
		// 月日的判断
		for (int i = 1; i <= 12; i++) {
			if (day > days[month] || day <= 0) {
				return false;
			}
		}
		return true;
	}

	// 闰年
	public static boolean isLeapYear(int year) {
		return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
	}

	// 匹配时间 匹配 01? 还是1
	public static boolean matchTime(String str) {
		String regEx = "^([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		if (!m.matches()) {
			return false;
		}
		return true;
	}

	// 对时间进行处理
	public static void dealDate(String[] inputs) {

	}
}

/**
 *  该类为座机计费方式的计算
 */
class LandlinePhoneCharging extends ChargeMode {
	//座机月租固定为20元
	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 LandPhoneInCityRule extends CallChargeRule{
	
	@Override
	public double calCost(UserRecords userRecords) {
		double sum = 0;
		//每条通话记录计算总额
		for (CallRecord call : userRecords.getCallingInCityRecords()) {//市内拨打 接免费
			int flag = Inputer.matchAdressArea(call.getAnswerAddressAreaCode());
			// .getTime()返回毫秒数 即 /1000
			double second = (call.getEndTime().getTime() - call.getStartTime().getTime() ) / 1000;
			if (second < 0) {
				continue;
			}
			double minute = (int) second / 60;
			if (second % 60 != 0) {
				minute += 1;
			}
			if (flag == 1) // 打市内
				sum += minute * 0.1;
			else if (flag == 2)// 打省内
				sum += minute * 0.3;
			else if (flag == 3)// 打省外
				sum += minute * 0.6;
		}
		return sum;
	}
}

/**
 * 	座机拨打省外电话的计费规则类
 */
class LandPhoneInLandRule extends CallChargeRule{

	@Override
	public double calCost(UserRecords userRecords) {
		double sum = 0;
		for (CallRecord call : userRecords.getCallingInLandRecords()) {
			// .getTime()返回毫秒数 即 /1000
			double second = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
			if (second < 0) {
				continue;
			}
			double minute = (int) second / 60;
			//不足一分钟按一分钟计算
			if (second % 60 != 0) {
				minute += 1;
			}
			sum += minute * 0.6;
		}
		return sum;
	}

}

/**
 * 座机拨打省内电话的计费规则类
 */
class LandPhoneInProvinceRule extends CallChargeRule {

	@Override
	public double calCost(UserRecords userRecords) {
		double sum = 0;
		for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
			// .getTime()返回毫秒数 即 /1000
			double second = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
			if (second < 0) {
				continue;
			}
			double minute = (int) second / 60;
			//不足一分钟按一分钟计算
			if (second % 60 != 0) {
				minute += 1;
			}
			sum += minute * 0.3;
		}
		return sum;
	}

}

/**
 *  短信记录类
 */
class MessageRecord extends CommunicationRecord{
	//记录短信的信息
	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
}

/**
 *  手机计费方式计算
 */
class MobilePhoneCharging extends ChargeMode {
	//手机月租固定为15元
	private double monthlyRent = 15;
	//手机计费
	public MobilePhoneCharging(){
		super();
		chargeRules.add(new MobilePhoneInCityRule()); //市内打市内
		chargeRules.add(new MobilePhoneInProvinceRule());//市内打省内
		chargeRules.add(new MobilePhoneInLandRule());//市内打省外
//		chargeRules.add(new MobilePhoneRoamingInProvinceRule());//省内漫游打电话
//		chargeRules.add(new MobilePhoneRoamingInLandRule());//省外漫游接
//		chargeRules.add(new MobilePhoneRoamingInLandCallRule());//省外漫游打
		
	}
	
	@Override
	public double calCost(UserRecords userRecords) {
		// TODO 自动生成的方法存根
		double sumCost = 0;
		for (ChargeRule rule : chargeRules) {
			sumCost += rule.calCost(userRecords);
		}
		return sumCost;
	}

	@Override
	public double getMonthlyRent() {
		// TODO 自动生成的方法存根
		return monthlyRent;
	}

}

/**
 * 手机市内拨打市内计费规则
 */
class MobilePhoneInCityRule extends CallChargeRule {

	@Override
	public double calCost(UserRecords userRecords) {
		// TODO 自动生成的方法存根
		double sum = 0;
		// 每条通话记录计算总额
		for (CallRecord call : userRecords.getCallingInCityRecords()) {// 市内拨打
			int flag = Inputer.matchAdressArea(call.getAnswerAddressAreaCode());

			// .getTime()返回毫秒数 即 /1000
			double second = (call.getEndTime().getTime() - call.getStartTime().getTime()) / 1000;
			if (second < 0) {
				continue;
			}
			double minute = (int) second / 60;
			if (second % 60 != 0) {
				minute += 1;
			}
			if (flag == 1) // 打市内
				sum += minute * 0.1;
			else if (flag == 2)// 打省内
				sum += minute * 0.2;
			else if (flag == 3)// 打省外
				sum += minute * 0.3;
		}

		return sum;
	}

}

/**
 *  手机市内拨打省外计费规则
 */
class MobilePhoneInLandRule extends CallChargeRule {

	@Override
	public double calCost(UserRecords userRecords) {
		// TODO 自动生成的方法存根
		double sum = 0;
		for (CallRecord call : userRecords.getCallingInLandRecords()) { //省外拨打
			// .getTime()返回毫秒数 即 /1000
			double second = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
			if (second < 0) {
				continue;
			}
			double minute = (int) second / 60;
			//不足一分钟按一分钟计算
			if (second % 60 != 0) {
				minute += 1;
			}
			sum += minute * 0.6;
		}
		for (CallRecord call : userRecords.getAnswerInLandRecords()) {//省外接听
			// .getTime()返回毫秒数 即 /1000
			double second = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
			if (second < 0) {
				continue;
			}
			double minute = (int) second / 60;
			//不足一分钟按一分钟计算
			if (second % 60 != 0) {
				minute += 1;
			}
			sum += minute * 0.3;
		}
		return sum;
	}

}

/**
 * 手机市内拨打省内计费规则
 */
class MobilePhoneInProvinceRule extends CallChargeRule {

	@Override
	public double calCost(UserRecords userRecords) {
		// TODO 自动生成的方法存根
		double sum = 0;
		for (CallRecord call : userRecords.getCallingInProvinceRecords()) { //省内拨打
			// .getTime()返回毫秒数 即 /1000
			double second = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
			if (second < 0) {
				continue;
			}
			double minute = (int) second / 60;
			//不足一分钟按一分钟计算
			if (second % 60 != 0) {
				minute += 1;
			}
			sum += minute * 0.3;
		}
		return sum;
	}

}

/**
 * 用户类 包括手机和座机用户 手机用户有两种计费方式 1,2 座机用户只有一种即 0 固定月租为20
 */
class User {
	// 用户记录
	private UserRecords userRecords = new UserRecords();
	// 余额
	private double balance = 100;
	// 计费方式
	private ChargeMode chargeMode;
	// 号码
	private String number;

	// get、set方法
	public UserRecords getUserRecords() {
		return userRecords;
	}

	public void setUserRecords(UserRecords userRecords) {
		this.userRecords = userRecords;
	}

	public double getBalance() {
		return balance;
	}

	public void setBalance(double balance) {
		this.balance = balance;
	}

	public 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;
	}

	/**
	 * 计算每月余额
	 * 
	 * @return
	 */
	public double calBalance() {
		return balance - chargeMode.getMonthlyRent() - chargeMode.calCost(userRecords);
	}

	/**
	 * 计算每月消费
	 * 
	 * @return
	 */
	public double calCost() {
		return chargeMode.calCost(userRecords);
	}

	@Override
	public String toString() {
		return balance + " " + number;
	}
}

/**
 * 用户记录类,保存用户各种通话、短信的记录
 */
class UserRecords {
	//拨打市内电话、省内(不含市内)拨打电话、省外拨打电话的记录
	private ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();
	private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();
	private ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();
	//接听市内电话、省内(不含市内)接听电话、省外接听电话的记录
	private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();
	private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();
	private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();
	//
//	private ArrayList<CallRecord> answerIn = new ArrayList<CallRecord>();
//	private ArrayList<CallRecord> answerInProvinceR = new ArrayList<CallRecord>();
	
	
	//发送短信、接收短信的记录
	private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();
	private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();
 
	/**
	 *  添加市内拨打电话记录
	 * @param callRecord 通话记录
	 */
	public void addCallingInCityRecords(CallRecord callRecord) {
		callingInCityRecords.add(callRecord);
	}
	/**
	 *  添加省内拨打电话记录
	 * @param callRecord 通话记录
	 */
	public void addCallingInProvinceRecords(CallRecord callRecord) {
		callingInProvinceRecords.add(callRecord);
	}
	/**
	 *  添加省外拨打电话记录
	 * @param callRecord 通话记录
	 */
	public void addCallingInLandRecords(CallRecord callRecord) {
		callingInLandRecords.add(callRecord);
	}
	/**
	 *  添加市内接收电话记录
	 * @param callRecord 通话记录
	 */
	public void addAnswerInCityRecords(CallRecord callRecord) {
		answerInCityRecords.add(callRecord);
	}
	/**
	 *  添加省内接收电话记录
	 * @param callRecord 通话记录
	 */
	public void aaddAnswerInProvinceRecords(CallRecord callRecord) {
		answerInProvinceRecords.add(callRecord);
	}
	/**
	 *  添加省外接收电话记录
	 * @param callRecord 通话记录
	 */
	public void addAnswerInLandRecords(CallRecord callRecord) {
		answerInLandRecords.add(callRecord);
	}
	/**
	 *  添加发送短信记录
	 * @param callRecord 短信记录
	 */
	public void addSendMessageRecords(MessageRecord callRecord) {
		sendMessageRecords.add(callRecord);
	}
	/**
	 *  添加接受短信记录
	 * @param callRecord 短信记录
	 */
	public void addReceiveMessageRecords(MessageRecord callRecord) {
		receiveMessageRecords.add(callRecord);
	}
 
	//以下为各种属性的set、get方法
	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;
	}
}


报表:

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

代码:

点击查看代码
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String str = in.nextLine();
		ArrayList<User> users = new ArrayList<>();
		while(!str.equals("end")) {
			Inputer.disposeStr(users,str);
			str = in.nextLine();
		}
		
		//compare()接受两个同类型的不同对象进行比较  
		//排序 方法重写 
		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) {
			//格式化 保留两位小数
			String temp = String.format("%.1f",u.calCost());
			double cost = Double.parseDouble(temp);
			temp = String.format("%.1f",u.calBalance());
			double balance = Double.parseDouble(temp);
			System.out.println(u.getNumber() + " "+ cost+" "+balance);
		}
		in.close();
	}
}

/**
 * 用户类 包括手机和座机用户 手机用户有两种计费方式 1,2 座机用户只有一种即 0 固定月租为20
 */
class User {
	// 用户记录
	private UserRecords userRecords = new UserRecords();
	// 余额
	private double balance = 100;
	// 计费方式
	private ChargeMode chargeMode;
	// 号码
	private String number;

	// get、set方法
	public UserRecords getUserRecords() {
		return userRecords;
	}

	public void setUserRecords(UserRecords userRecords) {
		this.userRecords = userRecords;
	}

	public double getBalance() {
		return balance;
	}

	public void setBalance(double balance) {
		this.balance = balance;
	}

	public 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;
	}

	/**
	 * 计算每月余额
	 * 
	 * @return
	 */
	public double calBalance() {
		return balance - chargeMode.getMonthlyRent() - chargeMode.calCost(userRecords);
	}

	/**
	 * 计算每月消费
	 * 
	 * @return
	 */
	public double calCost() {
		return chargeMode.calCost(userRecords);
	}

	@Override
	public String toString() {
		return balance + " " + number;
	}
}


/**
 * 用户记录类,保存用户各种通话、短信的记录
 */
class UserRecords {
	//拨打市内电话、省内(不含市内)拨打电话、省外拨打电话的记录
	private ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();
	private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();
	private ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();
	//接听市内电话、省内(不含市内)接听电话、省外接听电话的记录
	private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();
	private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();
	private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();
	//发送短信、接收短信的记录
	private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();
	private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();
 
	/**
	 *  添加市内拨打电话记录
	 * @param callRecord 通话记录
	 */
	public void addCallingInCityRecords(CallRecord callRecord) {
		callingInCityRecords.add(callRecord);
	}
	/**
	 *  添加省内拨打电话记录
	 * @param callRecord 通话记录
	 */
	public void addCallingInProvinceRecords(CallRecord callRecord) {
		callingInProvinceRecords.add(callRecord);
	}
	/**
	 *  添加省外拨打电话记录
	 * @param callRecord 通话记录
	 */
	public void addCallingInLandRecords(CallRecord callRecord) {
		callingInLandRecords.add(callRecord);
	}
	/**
	 *  添加市内接收电话记录
	 * @param callRecord 通话记录
	 */
	public void addAnswerInCityRecords(CallRecord callRecord) {
		answerInCityRecords.add(callRecord);
	}
	/**
	 *  添加省内接收电话记录
	 * @param callRecord 通话记录
	 */
	public void aaddAnswerInProvinceRecords(CallRecord callRecord) {
		answerInProvinceRecords.add(callRecord);
	}
	/**
	 *  添加省外接收电话记录
	 * @param callRecord 通话记录
	 */
	public void addAnswerInLandRecords(CallRecord callRecord) {
		answerInLandRecords.add(callRecord);
	}
	/**
	 *  添加发送短信记录
	 * @param callRecord 短信记录
	 */
	public void addSendMessageRecords(MessageRecord callRecord) {
		sendMessageRecords.add(callRecord);
	}
	/**
	 *  添加接受短信记录
	 * @param callRecord 短信记录
	 */
	public void addReceiveMessageRecords(MessageRecord callRecord) {
		receiveMessageRecords.add(callRecord);
	}
 
	//以下为各种属性的set、get方法
	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 Inputer {

	/**
	 * 对输入的每一行信息做处理 利用正则判断格式对错 以及 是否符合要求
	 * 
	 * @param str 传入输入的信息
	 */
	public static void disposeStr(ArrayList<User> users, String str) {
		String[] s = str.split(" ");
		String flag = str.substring(0, 1);
		if (flag.equals("u")) {
			if(str.matches("^u-((079[0-9]|0701)\\d{7,8}|1[0-9]{10})\\s[0-3]$")) {
				dealUsers(users, str);
			}
		} else if (flag.equals("t")) { // 是否需要考虑 时间1比时间2大的情况
			if (s[0].matches("^t-(010|\\d{4})\\d{7,8}$") && s[1].matches("^[0-9]{10,12}$")) {
				if (matchDate(s[2]) && matchDate(s[4]) && matchTime(s[3]) && matchTime(s[5])) {
					dealRecords(users, str);
				}
			}
			if (s[0].matches("^t-(010|\\d{4})\\d{7,8}$") && s[1].matches("^1[0-9]{10}$")) {
				if (matchDate(s[3]) && matchDate(s[5]) && matchTime(s[4]) && matchTime(s[6])) {
					dealRecords(users, str);
				}
			}
			if (s[0].matches("^t-1[0-9]{10}$") && s[2].matches("^(010|\\d{4})\\d{7,8}$") ) {
				if (matchDate(s[3]) && matchDate(s[5]) && matchTime(s[4]) && matchTime(s[6])) {
					dealRecords(users, str);
				}
			}
			if (s[0].matches("^t-1[0-9]{10}$") && s[2].matches("^1[0-9]{10}$")) {
				if (matchDate(s[4]) && matchDate(s[6]) && matchTime(s[5]) && matchTime(s[7])) {
					dealRecords(users, str);
				}
			}
		} else if (flag.equals("m")) {
			if (str.matches("^m-1[0-9]{10}\\s" + "1[0-9]{10}\\s" + "[0-9a-zA-Z\\s\\.,]+$")) {
				dealRecords(users,str);
				
			}
		}
	}

	/**
	 * 对用户的信息做处理 目前只包含座机用户之间的处理
	 * 
	 * @param user 用户表
	 * @param str  用户信息
	 */
	public static void dealUsers(ArrayList<User> users, String str) {
		User u = new User();
		String in[] = str.split(" ");
		String number = in[0].substring(2);
		// 先检查号码是否重复 不可以有重复号码
		for (User user : users) {
			if (user.getNumber().equals(number)) {
				return;
			}
		}
		u.setNumber(number);
		if (in[1].equals("0")) { // 座机用户
// 			u.setChargeMode(new LandlinePhoneCharging());
		} else if (in[1].equals("1")) { // 手机用户 计费方式1
// 			u.setChargeMode(new MobilePhoneCharging());
		} else if (in[1].equals("3")) { 
			u.setChargeMode(new MessageCharging());
		}
		users.add(u);
	}

	/**
	 * 对通信的内容做处理d
	 * 
	 * @param user 用户表
	 * @param str  通信记录 包括通话 -短信
	 */
	public static void dealRecords(ArrayList<User> user, String str) {
		String in[] = str.split(" ");
		String number = in[0].substring(2);
//		CallRecord call = new CallRecord(in);;
			// 对字符处理得到该次通话记录
		
		int flag = 0;
		if(in.length==8)
			flag = 2;
		else if(in.length==6)
			flag = 1;
		else if(in.length==7 ) {
			if(in[1].length()<=4)
				flag = 2;
			else flag = 1;
		}
		User callu = null, answeru = null;
		// 先判断拨打 或者接听的用户是否在用户表内 即该次通话是否合理
		for (User i : user) {
			if (i.getNumber().equals(number)) {
				callu = i;
			}
			if (i.getNumber().equals(in[flag])) {
				answeru = i;
			}
		}
//		// 该次通话 的拨打人在用户表内
//		if (callu != null) {
//			if (matchAdressArea(call.getCallingAddressAreaCode()) == 1) {
//				callu.getUserRecords().addCallingInCityRecords(call); //市内拨打
//			} else if (matchAdressArea(call.getCallingAddressAreaCode()) == 2) {
//				callu.getUserRecords().addCallingInProvinceRecords(call);//省内拨打
//			} else
//				callu.getUserRecords().addCallingInLandRecords(call);//省外拨打
//		}
//		// 该次通话 的接听人在用户表内
//		if (answeru != null) {
//			if (matchAdressArea(call.getAnswerAddressAreaCode()) == 1) {
//				answeru.getUserRecords().addAnswerInCityRecords(call); //市内接听
//			} else if (matchAdressArea(call.getAnswerAddressAreaCode()) == 2) {
//				answeru.getUserRecords().aaddAnswerInProvinceRecords(call); //省内接听
//			} else
//				answeru.getUserRecords().addAnswerInLandRecords(call); //省外接听
//		}
		
		if (str.charAt(0) == 'm') {
			MessageRecord messageRecord = new MessageRecord(str);
			if (callu != null) {
				callu.getUserRecords().addSendMessageRecords(messageRecord);
				;
			}
			if (answeru != null) {
				callu.getUserRecords().addReceiveMessageRecords(messageRecord);
			}
		}
		
	}
	
	// 判断通话类型
	public static void getCallType() {

	}

	// 匹配区号的地域 是否为市内1、省内2、省外3
	public static int matchAdressArea(String str) {
		if (str.matches("^0791$")) {
			return 1;
		} else if (str.matches("^(079[023456789]|0701)$")) {
			return 2;
		} else
			return 3;
	}

	// 匹配日期
	public static boolean matchDate(String str) {

		String[] data = str.split("\\.");
		// 先判断大致格式
		String regEx = "^(19|20)\\d\\d\\.([1-9]|1[0-2])\\.([1-9]|[12][0-9]|3[01])$";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		if (!m.matches()) {
			return false;
		}
		// 下面判断日期是否合理 以及润平年的判断
		int year = Integer.parseInt(data[0]);
		int month = Integer.parseInt(data[1]);
		int day = Integer.parseInt(data[2]);
		int[] days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };// 平年

		if (isLeapYear(year)) {
			days[2] = 29;
		}
		// 月日的判断
		for (int i = 1; i <= 12; i++) {
			if (day > days[month] || day <= 0) {
				return false;
			}
		}
		return true;
	}

	// 闰年
	public static boolean isLeapYear(int year) {
		return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
	}

	// 匹配时间 匹配 01? 还是1
	public static boolean matchTime(String str) {
		String regEx = "^([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		if (!m.matches()) {
			return false;
		}
		return true;
	}

	// 对时间进行处理
	public static void dealDate(String[] inputs) {

	}
}

/**
 * 拨打电话计费规则抽象类
 */
abstract class CallChargeRule extends ChargeRule{
	
}


/**
 * 通话记录类
 *
 */
class CallRecord extends CommunicationRecord {
	// 通话的起始、结束时间
	private Date startTime;
	private Date endTime;
	// 拨号地点的区号、接听地点的区号
	// 区号用于记录在哪个地点拨打和接听的电话 座机无法移动,就是本机区号,如果是手机号,则会有差异。
	private String callingAddressAreaCode;
	private String answerAddressAreaCode;

	public CallRecord(String[] in) {
		super();

		String st1 = null, st2 = null, ed1 = null, ed2 = null;  //分别记录时间

		if (in.length == 6) { // 座机打座机
			st1 = in[2];
			st2 = in[4];
			ed1 = in[3];
			ed2 = in[5];

			if (in[0].length() == 10) { // 3位区号 7位座机号 4 7 |3 8 
				callingAddressAreaCode = in[0].substring(2, 5);
				answerAddressAreaCode = in[1].substring(0, 3);
			} else {
				callingAddressAreaCode = in[0].substring(2, 6);
				answerAddressAreaCode = in[1].substring(0, 4);
			}
		} else if (in.length == 7) { // 座机打手机或- 座机打手机
			st1 = in[3];
			st2 = in[5];
			ed1 = in[4];
			ed2 = in[6];
			if (in[0].charAt(2) != '0') { //shoujida
				if (in[2].length() == 10) {
					answerAddressAreaCode = in[2].substring(0, 3);
				} else {
					answerAddressAreaCode = in[2].substring(0, 4);
				}
				callingAddressAreaCode = in[1];
			} else { //座机打手机
				if (in[0].length() == 10) {
					callingAddressAreaCode = in[0].substring(2, 5);
				} else {
					callingAddressAreaCode = in[0].substring(2, 6);
				}
				answerAddressAreaCode = in[2];
			}
		} else if (in.length == 8) { // 手机打手机
			st1 = in[4];
			st2 = in[6];
			ed1 = in[5];
			ed2 = in[7];
			callingAddressAreaCode = in[1];
			answerAddressAreaCode = in[3]; 
		}
		// 时间
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
		try {
			startTime = simpleDateFormat.parse(st1 + " " + ed1);
			endTime = simpleDateFormat.parse(st2 + " " + ed2);
		} 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 ChargeMode {
	//计费方式所包含的各种计费规则的集合 
	//
	protected ArrayList<ChargeRule> chargeRules = new ArrayList<>();
	 
	/**
	 * 计算每月通话 发送短信的消费
	 */
	public abstract double calCost(UserRecords userRecords);
 
	/**
	 * @return 返回月租
	 */
	public abstract double getMonthlyRent();
	
	//计费规则的get、set方法
	public ArrayList<ChargeRule> getChargeRules() {
		return chargeRules;
	}
 
	public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
		this.chargeRules = chargeRules;
	}

}

/**
 * 计费规则抽象类
 */
abstract class ChargeRule {
	abstract public double calCost(UserRecords userRecords);
}

/**
 *   抽象类、通讯记录类 
 *   包括通话和发送短信,记录两者的号码
 */
abstract class CommunicationRecord {
	//记录号码
	protected String callingNumber;
	protected String answerNumber;
 
	public String getCallingNumber() {
		return callingNumber;
	}
 
	public void setCallingNumber(String callingNumber) {
		this.callingNumber = callingNumber;
	}
 
	public String getAnswerNumber() {
		return answerNumber;
	}
 
	public void setAnswerNumber(String answerNumber) {
		this.answerNumber = answerNumber;
	}

}

class MessageCharging extends ChargeMode{

	MessageCharging(){
		super();
		chargeRules.add(new MessageRule());
	}
	@Override
	public double calCost(UserRecords userRecords) {
		// TODO 自动生成的方法存根
		double sum = 0;
		for (ChargeRule rule : chargeRules) {
			sum += rule.calCost(userRecords);
		}
		return sum;
	}

	@Override
	public double getMonthlyRent() {
		// TODO 自动生成的方法存根
		
		return 0;
	}

}

/**
 *  短信记录类
 */
class MessageRecord extends CommunicationRecord{
	//记录短信的信息
	private String message;

	MessageRecord(String in){
		super();
		this.message = in.substring(26);
	}
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
}


class MessageRule extends ChargeRule {

	@Override
	public double calCost(UserRecords userRecords) {
		// TODO 自动生成的方法存根
		double sum = 0;
		int number = 0; // 条数
		// 每条通话记录计算总额
		for (MessageRecord call : userRecords.getSendMessageRecords()) {// 市内拨打
			int length = call.getMessage().length();
			if (length <= 10) {
				number++;
			} else {
				number += length / 10;
				if (length % 10 != 0) {
					number++;
				}
			}
		}
		if (number <= 3) {
			sum = number * 0.1;
		} else if (number <= 5) {
			sum = 0.3 + 0.2 * (number - 3);
		} else {
			sum = 0.7 + 0.3 * (number - 5);
		}
		return sum;
	}

}

报表:

踩坑心得

  1. 在对数组类型的数据进行处理时,应该明确数组的大小。
  2. 在判断区号时,有三位的区号,我这里默认写的是四位,所以一直有一个点过不去QAQ。
  3. 对用户信息处理时,要进行去重操作,并且要考虑到用户拨打或接收的多种情况。
  4. 对每条通信信息处理的操作应该放在CallRecord类的构造函数里面,inputer类只需对不合法的信息做处理即可。

改进建议

总结

总的来说,这几次的题目不难。主要都是根据老师给的类图写的,如果题目没有给出类图,自己是否能够把类设计成这样,还需要更加努力学习这方面的知识,即类的设计模式。

标签:总结,第三阶段,Java,String,ArrayList,callRecord,void,return,public
From: https://www.cnblogs.com/su995/p/16972059.html

相关文章