BLOG—3

一级目录

二级目录

三级目录

前言

对之前发布的6-8次PTA题目集(电信计费),相比于之前的大作业,这次大作业更加侧重点在抽象类的应用,在减少了繁琐的计算后,增加了类和类之间的应用。这次大作业是给了类图,因此我们只要跟随类图创造,对于我这种菜鸟而言也是难度大大滴。在本次大作业中,我们也需要应用到新的知识,比如:计算时间差需要运用SimpleDateformat类,用collection类将每个新开账户的费用输出,再比如要将新开账户的通话记录收集到一起,等等。

踩坑新得

1.在SimpleDateformat类知识点中,我们要注意SimpleDateFormat类是DateFormat的子类,叫日期格式化类,专门用来格式化和解析日期的,是一个以和语言环境有关的方式来格式化和解析日期的具体类,它允许进行格式化和解析。格式化指的是将存储日期的类转化为字符串记录的日期形式,这里主要指Date类转化为String类。解析是格式化的逆过程,指的是将表示日期的字符串转化为记载日期的类。
2.在collection类里,由于数组长度是固定,当添加的元素超过了数组的长度时需要对数组重新定义,所以java内部给我们提供了集合类,能存储任意对象,长度是可以改变的,随着元素的增加而增加,随着元素的减少而减少。
数组和集合的区别
数组既可以存储基本数据类型,又可以存储引用数据类型,基本数据类型存储的是值,引用数据类型存储的是地址值
集合只能存储引用数据类型(对象),集合中也可以存储基本数据类型,但是在存储的时候会自动装箱变成对象
数组长度是固定的,不能自动增长
集合的长度的是可变的,可以根据元素的增加而增长

改进建议

1.我需要在类方面下功夫,因为类是可以很好的避免不同的错误。多多回顾学习的Java原则,并加以运用与理解。
2.在今后的设计中,自己要学习使用一些方便的集合框架。它们可以帮我们减少很多错误,减少我们在修改时浪费的时间,使代码更加的简洁、方便,且实现功能。
3.多使用Java中的思想,例如,封装、多态、接口,并且理解其中的优缺点以及适用性;
4.对于很多未知的功能查阅资料,可以去查找事例,来认识与了解学习。

总结

通过,这几次的大作业,我清楚了要想学好Java语言要做到一下几点:
一、学会独立思考
  我们在学习Java的过程中一定要学会独立思考,一些基础的知识点我们应该铭记于心,只要用到了就能信手拈来,这样我们才会不断的前进。
  二、知识总结
  学习Java我们要做的不断地对所学的知识进行总结,要把自己常常遇到的问题和解决方法给记下来,还要记录自己的收获和不足,在以后的学习过程中还要不断地向前进行复习,温故而知新,这样我们就有了积累,这样我们会发现我们的进步是非常快的。
  三、看书
现在互联网的发达让我们有着大量的教学视频,还有各种资源供我们学习,这些都使我们的学习变得简单化。因此我们要在学习过程中多读书,书籍永远是ZUI好的知识载体,很多好的书籍不仅仅传播的是知识,而是作者所要传达的思想和方法,因此我们通过看书钻研书籍中的内容,这会使我们更加聪明。
   四、掌握算法和优化程序
 无论我们学习到什么程度,我们都要学会去不断地优化自己所写的程序,能用几行实现的就不要用十几行去实现,而且我们要在学习的中后期就要算法,尽量去写出高质量的程序。

设计与分析(代码较多,将此项放置最后)

7-1 电信计费系列1-座机计费

类图展示
在这里插入图片描述

执行代码

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 (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 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);
			}
		}
 
	}
 
}

在本题中,我学会了主要运用了类的继承相关的知识点,多态性的使用方法及接口的应用等。

7-2多态测试

题目:定义容器Container接口。模拟实现一个容器类层次结构,并进行接口的实现、抽象方法重写和多态机制测试。各容器类实现求表面积、体积的方法。

定义接口Container:
属性:
public static final double pi=3.1415926;
抽象方法:
public abstract double area();
public abstract double volume();
static double sumofArea(Container c[]);
static double sumofVolume(Container c[]);
其中两个静态方法分别计算返回容器数组中所有对象的面积之和、周长之和;
定义Cube类、Cylinder类均实现自Container接口。
Cube类(属性:边长double类型)、Cylinder类(属性:底圆半径、高,double类型)。

代码

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        Shape3D[] list = new Shape3D[n];
        for (int i = 0; i < n; i++) {
            String str = input.next();
            if (str.equals("cube")) {
                list[i] = new Cube(input.nextDouble());
            } else if (str.equals("cylinder")) {
                list[i] = new Cylinder(input.nextDouble(), input.nextDouble());
            }
        }
        double v = 0;
        double a = 0;
        for (Shape3D i : list) {
            v += i.volume();
            a += i.area();
        }
        System.out.printf("%.2f\n%.2f",a,v);
    }
}
abstract class Shape3D implements Container {
}

class Cylinder extends Shape3D {
    private double r, h;
    public Cylinder(double r, double h) {
        super();
        this.r = r;
        this.h = h;
    }
    @Override
    public double area() {
        return pi * r * r * 2 + 2 * pi * r * h;
    }
    @Override
    public double volume() {
        return pi * r * r * h;
    }

}
class Cube extends Shape3D {

    private double a;

    public Cube(double a) {
        this.a = a;
    }
    @Override
    public double area() {

        return 6 * a * a;
    }
    @Override
    public double volume() {

        return a * a * a;
    }

}
interface Container {
    public static final double pi=3.1415926;
    public abstract double area();
    public abstract double volume();
    static double sumofArea(Container c[]) {
        double sum = 0;
        for(Container i:c) {
            sum += i.area();
        }
        return sum;
    };
    static double sumofVolume(Container c[]) {
        double sum = 0;
        for(Container i:c) {
            sum += i.volume();
        }
        return sum;
    }
}

在本题中,我运用了类的继承、多态性使用方法以及接口的应用等知识点,进行设计代码。

7-1 电信计费系列2-手机+座机计费

类图展示
在这里插入图片描述
代码

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Date;
import java.util.Locale;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
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 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;
    }

    @SuppressWarnings("unused")
    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) {

        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 {

    @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 MessageRecord extends CommunicationRecord {

    private String message;

    public String getMessage() {
        return message;
    }

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

class User {

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

}

class Outputtool {

    @SuppressWarnings("deprecation")
    public void output(double out) {
        BigDecimal numb = new BigDecimal(out);
        out = numb.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.print(out);
    }
}

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

类图展示
在这里插入图片描述
代码

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) {
        BigDecimal numb = new BigDecimal(out);
        out = numb.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.print(out);
    }
}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值