第三次java学习blog
前言
涉及知识点:
- 1.正则表达式的运用
- 2.字符串的操作
- 3.SimpleDateFormat类的使用
- 4.Date类的使用
- 5.泛型的使用
- 6.HashSet类的使用
- 7.多态的使用
- 8.继承的使用
- 9.接口的使用
- 10.异常的使用
题量与难度
这三次题目集的题量都不大,之后的作业都是在之前的代码上进行迭代,所以,只要第一次的类设计的好,之后的作业就会比较容易。
设计与分析
电信计费1
源码:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class Main {
public static void main(String[] args) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
Scanner input = new Scanner(System.in);
java.text.DecimalFormat df = new java.text.DecimalFormat("0.0#");
HashSet<String> init = new HashSet<>();// 存储用户号码,代表User
ArrayList<String> operate = new ArrayList<String>();// 对User进行的操作
ArrayList<User> users = new ArrayList<User>();
String reg = "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[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])";
while (true) {
String t = input.nextLine();
if (t.equals("end")) {
break;
} else {
if(t.length()<5){
continue;
}
if (t.charAt(0) == 'u') {
init.add(t);
} else if (t.charAt(0) == 't') {
operate.add(t);
}
}
}
for (String it : init) {
String nums[] = it.split("[- ]");
if (nums[1].length() >= 11 && nums[1].length() <= 12) {
if (nums[2].equals("0") && nums.length == 3) {
users.add(new User(new LandlinePhoneCharging(), nums[1]));// init
}
}
}
for (String it : operate) {
String ops[] = it.split("[- ]");
String startString = ops[3] + " " + ops[4];
String endString = ops[5] + " " + ops[6];
String callnum = ops[1];
String recenum = ops[2];
if (ops[1].length() >= 11 && ops[1].length() <= 12 && ops[2].length() <= 12 && ops[2].length() >= 10 && ops.length == 7 && startString.matches(reg) && endString.matches(reg)) {
try {
Date startDate = simpleDateFormat.parse(startString);
Date endDate = simpleDateFormat.parse(endString);
if (callnum.substring(0, 4).equals(recenum.substring(0, 4))) {
for (User user : users) {// city
if (user.getNumber().equals(callnum)) {
CallRecord callRecord = new CallRecord(startDate, endDate, callnum, recenum);
user.getUserRecords().getCallingInCityRecords().add(callRecord);
}
}
} else if (recenum.substring(0,3).equals("079")||recenum.substring(0,4).equals("0701")) {
for (User user : users) {// city
if (user.getNumber().equals(callnum)) {
CallRecord callRecord = new CallRecord(startDate, endDate, callnum, recenum);
user.getUserRecords().getCallingInProvinceRecords().add(callRecord);
}
}
} else {
for (User user : users) {// city
if (user.getNumber().equals(callnum)) {
CallRecord callRecord = new CallRecord(startDate, endDate, callnum, recenum);
user.getUserRecords().getCallingInLandRecords().add(callRecord);
}
}
}
} catch (ParseException e) {
// TODO Auto-generated catch block
continue;
}
}
}
for (User user : users) {
user.calCost();
user.calBalance();
}
for (int i = 0; i < users.size(); i++) {
int min = i;
for (int j = i; j < users.size(); j++) {
if (users.get(min).getNumber().compareTo(users.get(j).getNumber()) > 0) {
min = j;
}
}
Collections.swap(users, i, min);
}
for (User user : users) {
System.out.println(user.getNumber() + " " + df.format(user.calCost()) + " " + df.format(user.getBalance()));
}
}
}
class Judge{
}
abstract class CallChargeRule extends ChargeRule{
public abstract double calCost(ArrayList<CallRecord> callRecords);
}
class CallRecord extends CommunicationRecord {//通话记录 继承自抽象类
private Date startTime;//开始时间
private Date endTime;//结束时间
private String callingAddressAreaCode;//地区,存区号
private String answerAddressAreaCode;//地区,存区号
public CallRecord() {
}
public CallRecord(Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) {
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 {
private ArrayList<ChargeRule> chargeRules = new ArrayList<ChargeRule>();
public ArrayList<ChargeRule> getChargeRules() {
return chargeRules;
}
public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
this.chargeRules = chargeRules;
}
public abstract double calCost(UserRecords userRecords);
public abstract double getMonthlyRent();
}
abstract class ChargeRule {
}
abstract class CommunicationRecord {
private String callingNumber;//来电号码
private 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 LandlinePhoneCharging extends ChargeMode {
final double monthlyRent = 20;
public LandlinePhoneCharging() {
this.getChargeRules().add(new LandPhoneInCityRule());
this.getChargeRules().add(new LandPhoneInProvinceRule());
this.getChargeRules().add(new LandPhoneInlandRule());
}
@Override
public double calCost(UserRecords userRecords) {
// TODO Auto-generated method stub
double all = 0;
all += ((LandPhoneInCityRule) getChargeRules().get(0)).calCost(userRecords.getCallingInCityRecords());
all += ((LandPhoneInProvinceRule) getChargeRules().get(1)).calCost(userRecords.getCallingInProvinceRecords());
all += ((LandPhoneInlandRule) getChargeRules().get(2)).calCost(userRecords.getCallingInLandRecords());
return all;
}
@Override
public double getMonthlyRent() {
// TODO Auto-generated method stub
return monthlyRent;
}
}
class LandPhoneInCityRule extends CallChargeRule {
public LandPhoneInCityRule() {
}
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
// TODO Auto-generated method stub
double ans = 0;
for (CallRecord callRecord : callRecords) {
long a = callRecord.getStartTime().getTime();
long b = callRecord.getEndTime().getTime();
int c = (int) ((b - a) / 1000);
if (c % 60 != 0) {
ans += (c / 60 + 1) * 0.1;
} else {
ans += (c / 60) * 0.1;
}
}
return ans;
}
}
class LandPhoneInlandRule extends CallChargeRule {
public LandPhoneInlandRule() {
}
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
// TODO Auto-generated method stub
double ans = 0;
for (CallRecord callRecord : callRecords) {
long a = callRecord.getStartTime().getTime();
long b = callRecord.getEndTime().getTime();
int c = (int) ((b - a) / 1000);
if (c % 60 != 0) {
ans += (c / 60 + 1) * 0.6;
} else {
ans += (c / 60) * 0.6;
}
}
return ans;
}
}
class LandPhoneInProvinceRule extends CallChargeRule {
public LandPhoneInProvinceRule() {
}
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
// TODO Auto-generated method stub
double ans = 0;
for (CallRecord callRecord : callRecords) {
long a = callRecord.getStartTime().getTime();
long b = callRecord.getEndTime().getTime();
int c = (int) ((b - a) / 1000);
if (c % 60 != 0) {
ans += (c / 60 + 1) * 0.3;
} else {
ans += (c / 60) * 0.3;
}
}
return ans;
}
}
class MessageRecord extends CommunicationRecord {
private String message;
public MessageRecord() {
}
public MessageRecord(String message) {
this.message = 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 User() {
}
public User(ChargeMode chargeMode, String number) {
this.chargeMode = chargeMode;
this.number = number;
}
public double calBalance() {
this.balance = this.balance - this.calCost() - 20;
return this.balance;
}
public double calCost() {
return chargeMode.calCost(userRecords);
}
public UserRecords getUserRecords() {
return userRecords;
}
public void setUserRecords(UserRecords userRecords) {
this.userRecords = userRecords;
}
public double getBalance() {
return 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;
}
}
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> answerInCitRecords = new ArrayList<CallRecord>();
private ArrayList<CallRecord> answerInProvincRecords = 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 UserRecords() {
}
public ArrayList<CallRecord> getCallingInCityRecords() {
return callingInCityRecords;
}
public ArrayList<CallRecord> getCallingInProvinceRecords() {
return callingInProvinceRecords;
}
public ArrayList<CallRecord> getCallingInLandRecords() {
return callingInLandRecords;
}
public ArrayList<CallRecord> getAnswerInCitRecords() {
return answerInCitRecords;
}
public ArrayList<CallRecord> getAnswerInProvincRecords() {
return answerInProvincRecords;
}
public ArrayList<CallRecord> getAnswerInLandRecords() {
return answerInLandRecords;
}
public ArrayList<MessageRecord> getSendMessageRecords() {
return sendMessageRecords;
}
public ArrayList<MessageRecord> getReceiveMessageRecords() {
return receiveMessageRecords;
}
public void addCallingInCityRecords(CallRecord callingInCityRecords) {
this.callingInCityRecords.add(callingInCityRecords);
}
public void addCallingInProvinceRecords(CallRecord callingInProvinceRecords) {
this.callingInProvinceRecords.add(callingInProvinceRecords);
}
public void addCallingInLandRecords(CallRecord callingInLandRecords) {
this.callingInLandRecords.add(callingInLandRecords);
}
public void addAnswerInCitRecords(CallRecord answerInCitRecords) {
this.answerInCitRecords.add(answerInCitRecords);
}
public void addAnswerInProvincRecords(CallRecord answerInProvincRecords) {
this.answerInProvincRecords.add(answerInProvincRecords);
}
public void addAnswerInLandRecords(CallRecord answerInLandRecords) {
this.answerInLandRecords.add(answerInLandRecords);
}
public void addSendMessageRecords(MessageRecord sendMessageRecords) {
this.sendMessageRecords.add(sendMessageRecords);
}
public void addReceiveMessageRecords(MessageRecord receiveMessageRecords) {
this.receiveMessageRecords.add(receiveMessageRecords);
}
}
类图:
- 在上面的类图中,我们可以看到,User类中有一个UserRecords类型的属性,这个属性是一个UserRecords类型的对象,这个对象中包含了一些CallRecord类型的属性,这些属性是一个集合,这些集合中包含了一些CallRecord类型的对象,这些对象中包含了一些属性,这些属性是一些字符串类型的属性,这些属性是用户的通话记录,发送的短信记录,接收的短信记录。
- 利用了抽象计费类来表示计费模式,让后续的代码可以进行迭代,不需要修改。
- 利用了抽象、继承和多态进行编程,让代码的可维护性质更强,并且增强了代码的可读性和可维护性。
- 关于类的继承,可以看到,User类继承了CommunicationRecord类,而CommunicationRecord类继承了CallRecord类,而CallRecord类继承了CommunicationRecord类。
电信计费2
源码:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class Main {
public static void main(String[] args) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
Scanner input = new Scanner(System.in);
java.text.DecimalFormat df = new java.text.DecimalFormat("0.0#");
HashSet<String> init = new HashSet<String>();// 存储用户号码,代表User
ArrayList<String> operate = new ArrayList<String>();// 对User进行的操作
ArrayList<User> users = new ArrayList<User>();
String initCellphonenum = "[u][-]1[3-9]\\d{9}[ ][1]";
String initLandPhonenum = "[u][-]0791[0-9]{7,8}[ ][0]";
while (true) {// 将信息录入
String t = input.nextLine();
if (t.equals("end")) {
break;
} else {
if (t.length() < 13) {
continue;
}
if (t.charAt(0) == 'u') {// 首字母为u则为注册
init.add(t);
} else if (t.charAt(0) == 't') {// 首字母为t则为操作
operate.add(t);
}
}
}
for (String it : init) {
if (it.matches(initCellphonenum) || it.matches(initLandPhonenum)) {
String nums[] = it.split("[- ]");
if (nums[2].equals("1")) {
users.add(new User(new CellphoneCharging(), nums[1]));
} else if (nums[2].equals("0")) {
users.add(new User(new LandlinePhoneCharging(), nums[1]));
} else if (nums[2].equals("3")) {
}
}
} // TODO 用户的初始化已完成
// TODO 判断打电话操作时间,先利用字符串切割,然后判断切割后的字符串个数,通过个数以及手机号码来判断区号,
for (String it : operate) {
if (land_call_landjudge(it)) {
String arrs[] = it.split("[- ]");
String callNumber = arrs[1];
String answerNumber = arrs[2];
String startTime = arrs[3] + " " + arrs[4];
String endTime = arrs[5] + " " + arrs[6];
String Call_Code = callNumber.substring(0, 4);
String Answer_Code = answerNumber.substring(0, 4);
if (callNumber.equals(answerNumber)) {
continue;
}
if (Datejudge(startTime) && Datejudge(endTime)) {
Date startDate = simpleDateFormat.parse(startTime);
Date endDate = simpleDateFormat.parse(endTime);
for (User user : users) {
if (user.getNumber().equals(callNumber)) {
if (Answer_Code.equals("0791")) {
// TODO 判断是否为市内电话
user.getUserRecords().addCallingInCityRecords(
new CallRecord(startDate, endDate, Call_Code, Answer_Code));
break;
} else if (Answer_Code.substring(0, 3).equals("079") || Answer_Code.equals("0701")) {
// TODO 判断是否为市外电话
user.getUserRecords().addCallingInProvinceRecords(
new CallRecord(startDate, endDate, Call_Code, Answer_Code));
break;
} else {
// TODO 判断是否为国内电话
user.getUserRecords().addCallingInLandRecords(
new CallRecord(startDate, endDate, Call_Code, Answer_Code));
break;
}
}
}
}
} else if (land_call_cellJudge(it)) {
String arrs[] = it.split("[- ]");
String callNumber = arrs[1];
String answerNumber = arrs[2];
String startTime = arrs[4] + " " + arrs[5];
String endTime = arrs[6] + " " + arrs[7];
String Call_Code = callNumber.substring(0, 4);// 可能为省外电话,此时则不一定为前四位
String Answer_Code = arrs[3];
if (Datejudge(startTime) && Datejudge(endTime)) {
Date startDate = simpleDateFormat.parse(startTime);
Date endDate = simpleDateFormat.parse(endTime);
for (User user : users) {
if (user.getNumber().equals(callNumber)) {
if (Call_Code.equals(Answer_Code)) {
user.getUserRecords().addCallingInCityRecords(
new CallRecord(startDate, endDate, Call_Code, Answer_Code));
} else if (Call_Code.substring(0, 3).equals(Answer_Code.substring(0, 3))) {
user.getUserRecords().addCallingInProvinceRecords(
new CallRecord(startDate, endDate, Call_Code, Answer_Code));
} else {
user.getUserRecords().addCallingInLandRecords(
new CallRecord(startDate, endDate, Call_Code, Answer_Code));
}
}
}
if (!Answer_Code.equals("0701") && !Answer_Code.substring(0, 3).equals("079")) {// 不是江西省内区号
for (User user : users) {
if (user.getNumber().equals(answerNumber)) {
user.getUserRecords().addAnswerInLandRecords(
new CallRecord(startDate, endDate, Call_Code, Answer_Code));
break;
}
}
}
}
} else if (cell_call_landJudge(it)) {
String arrs[] = it.split("[- ]");
String callNumber = arrs[1];
String answerNumber = arrs[3];
String startTime = arrs[4] + " " + arrs[5];
String endTime = arrs[6] + " " + arrs[7];
String Call_Code = arrs[2];
String Answer_Code = answerNumber.substring(0, 4);
if (Datejudge(startTime) && Datejudge(endTime)) {
Date startDate = simpleDateFormat.parse(startTime);
Date endDate = simpleDateFormat.parse(endTime);
for (User user : users) {
if (user.getNumber().equals(callNumber)) {
if (Call_Code.equals("0791")) {
user.getUserRecords().addCallingInCityRecords(
new CallRecord(startDate, endDate, Call_Code, Answer_Code));
} else if (Call_Code.substring(0, 3).equals("079") || Call_Code.equals("0701")) {
user.getUserRecords().addCallingInProvinceRecords(
new CallRecord(startDate, endDate, Call_Code, Answer_Code));
} else {
user.getUserRecords().addCallingInLandRecords(
new CallRecord(startDate, endDate, Call_Code, Answer_Code));
}
}
}
for (User user : users) {// TODO 座机可能出问题
if (user.getNumber().equals(answerNumber)) {
user.getUserRecords().addAnswerInLandRecords(
new CallRecord(startDate, endDate, Call_Code, Answer_Code));
break;
}
}
}
} else if (cell_call_cellJudge(it)) {
String arrs[] = it.split("[- ]");
String callNumber = arrs[1];
String answerNumber = arrs[3];
String startTime = arrs[5] + " " + arrs[6];
String endTime = arrs[7] + " " + arrs[8];
String Call_Code = arrs[2];
String Answer_Code = arrs[4];
if (Datejudge(startTime) && Datejudge(endTime)) {
Date startDate = simpleDateFormat.parse(startTime);
Date endDate = simpleDateFormat.parse(endTime);
for (User user : users) {
if (user.getNumber().equals(callNumber)) {
if (Call_Code.equals("0791")) {
user.getUserRecords().addCallingInCityRecords(
new CallRecord(startDate, endDate, Call_Code, Answer_Code));
} else if (Call_Code.substring(0, 3).equals("079") || Call_Code.equals("0701")) {// 省内漫游通话
user.getUserRecords().addCallingInProvinceRecords(
new CallRecord(startDate, endDate, Call_Code, Answer_Code));
} else {
user.getUserRecords().addCallingInLandRecords(
new CallRecord(startDate, endDate, Call_Code, Answer_Code));
}
}
}
if (!Answer_Code.equals("0701") && !Answer_Code.substring(0, 3).equals("079")) {
for (User user2 : users) {
if (user2.getNumber().equals(answerNumber)) {
user2.getUserRecords().addAnswerInLandRecords(
new CallRecord(startDate, endDate, Call_Code, Answer_Code));
break;
}
}
}
}
} else {
continue;
}
}
for (User user : users) {
user.calCost();
user.calBalance();
}
for (int i = 0; i < users.size(); i++) {
int min = i;
for (int j = i; j < users.size(); j++) {
if (users.get(min).getNumber().compareTo(users.get(j).getNumber()) > 0) {
min = j;
}
}
Collections.swap(users, i, min);
}
for (User user : users) {
System.out.println(user.getNumber() + " " + df.format(user.calCost()) + " " +
df.format(user.getBalance()));
}
}
public static boolean Datejudge(String datestring) {
boolean flag = true;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
simpleDateFormat.setLenient(false);
try {
simpleDateFormat.parse(datestring);
} catch (Exception e) {
// TODO: handle exception
flag = false;
}
return flag;
}
public static boolean land_call_landjudge(String arr) {
String landcityincity = "t-0791\\d{7,8} 0\\d{9,11} \\d{4}.\\d{1,2}.\\d{1,2} \\d{2}:\\d{2}:\\d{2} \\d{4}.\\d{1,2}.\\d{1,2} \\d{2}:\\d{2}:\\d{2}";
if (arr.matches(landcityincity)) {
return true;
} else {
return false;
}
}
public static boolean land_call_cellJudge(String arr) {
String land_call_cell = "t-0\\d{9,11} 1\\d{10} 0\\d{2,3} \\d{4}.\\d{1,2}.\\d{1,2} \\d{2}:\\d{2}:\\d{2} \\d{4}.\\d{1,2}.\\d{1,2} \\d{2}:\\d{2}:\\d{2}";
if (arr.matches(land_call_cell)) {
return true;
} else {
return false;
}
}
public static boolean cell_call_landJudge(String arr) {
String cell_call_land = "t-1\\d{10} 0\\d{2,3} 0\\d{9,11} \\d{4}.\\d{1,2}.\\d{1,2} \\d{2}:\\d{2}:\\d{2} \\d{4}.\\d{1,2}.\\d{1,2} \\d{2}:\\d{2}:\\d{2}";
if (arr.matches(cell_call_land)) {
return true;
} else {
return false;
}
}
public static boolean cell_call_cellJudge(String arr) {
String cell_call_cell = "t-1\\d{10} 0\\d{2,3} 1\\d{10} 0\\d{2,3} \\d{4}.\\d{1,2}.\\d{1,2} \\d{2}:\\d{2}:\\d{2} \\d{4}.\\d{1,2}.\\d{1,2} \\d{2}:\\d{2}:\\d{2}";
if (arr.matches(cell_call_cell)) {
return true;
} else {
return false;
}
}
}
abstract class CallChargeRule extends ChargeRule{
public abstract double calCost(ArrayList<CallRecord> callRecords);
}
class CallRecord extends CommunicationRecord {//通话记录 继承自抽象类
private Date startTime;//开始时间
private Date endTime;//结束时间
private String callingAddressAreaCode;//地区,存区号
private String answerAddressAreaCode;//地区,存区号
public CallRecord() {
}
public CallRecord(Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) {
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;
}
}
class CellphoneCharging extends ChargeMode {
final double monthlyRent = 15;
public CellphoneCharging() {
getChargeRules().add(new CellphoneInCityRule());
getChargeRules().add(new CellphoneInProvinceRule());
getChargeRules().add(new CellphoneInlandRule());
getChargeRules().add(new CellphoneInlandAnsRule());
}
@Override
public double calCost(UserRecords userRecords) {
// TODO Auto-generated method stub
double all = 0;
all += ((CellphoneInCityRule) getChargeRules().get(0)).calCost(userRecords.getCallingInCityRecords());
all += ((CellphoneInProvinceRule) getChargeRules().get(1)).calCost(userRecords.getCallingInProvinceRecords());
all += ((CellphoneInlandRule) getChargeRules().get(2)).calCost(userRecords.getCallingInLandRecords());
all += ((CellphoneInlandAnsRule) getChargeRules().get(3)).calCost(userRecords.getAnswerInLandRecords());
return all;
}
@Override
public double getMonthlyRent() {
// TODO Auto-generated method stub
return monthlyRent;
}
}
class CellphoneInCityRule extends CallChargeRule {
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
// TODO Auto-generated method stub
double ans = 0;
for (CallRecord callRecord : callRecords) {
long a = callRecord.getStartTime().getTime();
long b = callRecord.getEndTime().getTime();
int c = (int) ((b - a) / 1000);
if (callRecord.getAnswerAddressAreaCode().equals("0791")) {
if (c % 60 != 0) {
ans += (c / 60 + 1) * 0.1;
} else {
ans += (c / 60) * 0.1;
}
} else if (callRecord.getAnswerAddressAreaCode().substring(0, 3).equals("079")||callRecord.getAnswerAddressAreaCode().equals("0701")) {
if (c % 60 != 0) {
ans += (c / 60 + 1) * 0.2;
} else {
ans += (c / 60) * 0.2;
}
} else {
if (c % 60 != 0) {
ans += (c / 60 + 1) * 0.3;
} else {
ans += (c / 60) * 0.3;
}
}
}
return ans;
}
}
class CellphoneInlandAnsRule extends CallChargeRule {
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double cost = 0;
for (CallRecord callRecord : callRecords) {
long a = callRecord.getStartTime().getTime();
long b = callRecord.getEndTime().getTime();
int c = (int) ((b - a) / 1000);
if (c % 60 != 0) {
cost += (c / 60 + 1) * 0.3;
} else {
cost += (c / 60) * 0.3;
}
}
return cost;
}
}
class CellphoneInlandRule extends CallChargeRule {
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
// TODO Auto-generated method stub
double ans = 0;
for (CallRecord callRecord : callRecords) {
long a = callRecord.getStartTime().getTime();
long b = callRecord.getEndTime().getTime();
int c = (int) ((b - a) / 1000);
if (c % 60 != 0) {
ans += (c / 60 + 1) * 0.6;
} else {
ans += (c / 60) * 0.6;
}
}
return ans;
}
}
class CellphoneInProvinceRule extends CallChargeRule{
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
// TODO Auto-generated method stub
double ans = 0;
for (CallRecord callRecord : callRecords) {
long a = callRecord.getStartTime().getTime();
long b = callRecord.getEndTime().getTime();
int c = (int) ((b - a) / 1000);
if (c % 60 != 0) {
ans += (c / 60 + 1) * 0.3;
} else {
ans += (c / 60) * 0.3;
}
}
return ans;
}
}
abstract class ChargeMode {
private ArrayList<ChargeRule> chargeRules = new ArrayList<ChargeRule>();
public ArrayList<ChargeRule> getChargeRules() {
return chargeRules;
}
public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
this.chargeRules = chargeRules;
}
public abstract double calCost(UserRecords userRecords);
public abstract double getMonthlyRent();
}
abstract class ChargeRule {
}
class CommunicationRecord {
private String callingNumber;//来电号码
private 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;
}
}
abstract class Judge {
public abstract boolean judge_landlinephone();
}
class LandlinePhoneCharging extends ChargeMode {
final double monthlyRent = 20;
public LandlinePhoneCharging() {
this.getChargeRules().add(new LandPhoneInCityRule());
this.getChargeRules().add(new LandPhoneInProvinceRule());
this.getChargeRules().add(new LandPhoneInlandRule());
}
@Override
public double calCost(UserRecords userRecords) {
// TODO Auto-generated method stub
double all = 0;
all += ((LandPhoneInCityRule) getChargeRules().get(0)).calCost(userRecords.getCallingInCityRecords());
all += ((LandPhoneInProvinceRule) getChargeRules().get(1)).calCost(userRecords.getCallingInProvinceRecords());
all += ((LandPhoneInlandRule) getChargeRules().get(2)).calCost(userRecords.getCallingInLandRecords());
return all;
}
@Override
public double getMonthlyRent() {
// TODO Auto-generated method stub
return monthlyRent;
}
}
class LandPhoneInCityRule extends CallChargeRule {
public LandPhoneInCityRule() {
}
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
// TODO Auto-generated method stub
double ans = 0;
for (CallRecord callRecord : callRecords) {
long a = callRecord.getStartTime().getTime();
long b = callRecord.getEndTime().getTime();
int c = (int) ((b - a) / 1000);
if (c % 60 != 0) {
ans += (c / 60 + 1) * 0.1;
} else {
ans += (c / 60) * 0.1;
}
}
return ans;
}
}
class LandPhoneInlandRule extends CallChargeRule {
public LandPhoneInlandRule() {
}
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
// TODO Auto-generated method stub
double ans = 0;
for (CallRecord callRecord : callRecords) {
long a = callRecord.getStartTime().getTime();
long b = callRecord.getEndTime().getTime();
int c = (int) ((b - a) / 1000);
if (c % 60 != 0) {
ans += (c / 60 + 1) * 0.6;
} else {
ans += (c / 60) * 0.6;
}
}
return ans;
}
}
class LandPhoneInProvinceRule extends CallChargeRule {
public LandPhoneInProvinceRule() {
}
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
// TODO Auto-generated method stub
double ans = 0;
for (CallRecord callRecord : callRecords) {
long a = callRecord.getStartTime().getTime();
long b = callRecord.getEndTime().getTime();
int c = (int) ((b - a) / 1000);
if (c % 60 != 0) {
ans += (c / 60 + 1) * 0.3;
} else {
ans += (c / 60) * 0.3;
}
}
return ans;
}
}
class MessageRecord extends CommunicationRecord {
private String message;
public MessageRecord() {
}
public MessageRecord(String message) {
this.message = 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 User() {
}
public User(ChargeMode chargeMode, String number) {
this.chargeMode = chargeMode;
this.number = number;
}
public double calBalance() {
this.balance = this.balance - this.calCost() - this.getChargeMode().getMonthlyRent();
return this.balance;
}
public double calCost() {
return chargeMode.calCost(userRecords);
}
public UserRecords getUserRecords() {
return userRecords;
}
public void setUserRecords(UserRecords userRecords) {
this.userRecords = userRecords;
}
public double getBalance() {
return 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;
}
}
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> answerInCitRecords = new ArrayList<CallRecord>();//市内接通电话
private ArrayList<CallRecord> answerInProvincRecords = 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 UserRecords() {
}
public ArrayList<CallRecord> getCallingInCityRecords() {
return callingInCityRecords;
}
public ArrayList<CallRecord> getCallingInProvinceRecords() {
return callingInProvinceRecords;
}
public ArrayList<CallRecord> getCallingInLandRecords() {
return callingInLandRecords;
}
public ArrayList<CallRecord> getAnswerInCitRecords() {
return answerInCitRecords;
}
public ArrayList<CallRecord> getAnswerInProvincRecords() {
return answerInProvincRecords;
}
public ArrayList<CallRecord> getAnswerInLandRecords() {
return answerInLandRecords;
}
public ArrayList<MessageRecord> getSendMessageRecords() {
return sendMessageRecords;
}
public ArrayList<MessageRecord> getReceiveMessageRecords() {
return receiveMessageRecords;
}
public void addCallingInCityRecords(CallRecord callingInCityRecords) {
this.callingInCityRecords.add(callingInCityRecords);
}
public void addCallingInProvinceRecords(CallRecord callingInProvinceRecords) {
this.callingInProvinceRecords.add(callingInProvinceRecords);
}
public void addCallingInLandRecords(CallRecord callingInLandRecords) {
this.callingInLandRecords.add(callingInLandRecords);
}
public void addAnswerInCitRecords(CallRecord answerInCitRecords) {
this.answerInCitRecords.add(answerInCitRecords);
}
public void addAnswerInProvincRecords(CallRecord answerInProvincRecords) {
this.answerInProvincRecords.add(answerInProvincRecords);
}
public void addAnswerInLandRecords(CallRecord answerInLandRecords) {
this.answerInLandRecords.add(answerInLandRecords);
}
public void addSendMessageRecords(MessageRecord sendMessageRecords) {
this.sendMessageRecords.add(sendMessageRecords);
}
public void addReceiveMessageRecords(MessageRecord receiveMessageRecords) {
this.receiveMessageRecords.add(receiveMessageRecords);
}
}
类图:
- 该次题目新增了一个手机类,并且增加了三种计费方式,由于之前的类没有设计好。所以这次的的设计和之前的设计有很大的不同之处,这次的设计对比上次的设计更加有结构性,并且其类之间的关系也更加明确;
对于格式错误的判断,这次将每种情况的正则表达式写出来,每个表达式封装成一个函数,这样就可以更加简单的判断了;并且每个函数对应一种情况,四个函数将所有的情况都一一对应,极大减少了格式的判断;
电信计费3
源码:
import java.text.ParseException;
import java.util.*;
public class Main {
public static void main(String[] args) throws ParseException {
try (Scanner input = new Scanner(System.in)) {
java.text.DecimalFormat df = new java.text.DecimalFormat("0.0#");
HashSet<String> init = new HashSet<String>();// 存储用户号码,代表User
ArrayList<String> operate = new ArrayList<String>();// 对User进行的操作
ArrayList<User> users = new ArrayList<User>();
while (true) {// 将信息录入
String t = input.nextLine();
if (t.equals("end")) {
break;
} else {
if (t.length() < 13) {
continue;
}
if (t.charAt(0) == 'u') {// 首字母为u则为注册
init.add(t);
} else if (t.charAt(0) == 'm') {// 首字母为t则为操作
operate.add(t);
}
}
}
for (String it : init) {
if (NumberJudge(it)) {
String nums[] = it.split("[- ]");
users.add(new User(new MessageCharging(), nums[1]));
}
}
for (String it : operate) {
if (MessageJudge(it)) {
String nums[] = it.split("[- ]");
String sendMessageNum = nums[1];
String message = it.substring(26);
for (User user : users) {
if (user.getNumber().equals(sendMessageNum)) {
user.getUserRecords().getSendMessageRecords().add(new MessageRecord(message));
}
}
}
}
for (User user : users) {
user.calCost();
user.calBalance();
}
for (int i = 0; i < users.size(); i++) {
int min = i;
for (int j = i; j < users.size(); j++) {
if (users.get(min).getNumber().compareTo(users.get(j).getNumber()) > 0) {
min = j;
}
}
Collections.swap(users, i, min);
}
for (User user : users) {
System.out.println(user.getNumber() + " " + df.format(user.calCost()) + " " +
df.format(user.getBalance()));
}
}
}
public static boolean NumberJudge(String arr) {
String numberjudge = "[u][-]1[3-9]\\d{9}[ ][3]";
if (arr.matches(numberjudge)) {
return true;
} else {
return false;
}
}
public static boolean MessageJudge(String arr) {
String messagejudge = "m-1[3-9]\\d{9} 1[3-9]\\d{9} [a-z|A-Z|0-9| |,|.]++";
if (arr.matches(messagejudge)) {
return true;
} else {
return false;
}
}
}
abstract class ChargeMode {
private ArrayList<ChargeRule> chargeRules = new ArrayList<ChargeRule>();
public ArrayList<ChargeRule> getChargeRules() {
return chargeRules;
}
public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
this.chargeRules = chargeRules;
}
public abstract double calCost(UserRecords userRecords);
public abstract double getMonthlyRent();
}
abstract class ChargeRule {
}
abstract class CommunicationRecord {
private String callingNumber;//来电号码
private 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 {
public MessageCharging() {
getChargeRules().add(new SendMessageRule());
}
@Override
public double calCost(UserRecords userRecords) {
double cost = 0;
cost += ((SendMessageRule) getChargeRules().get(0)).calCost(userRecords.getSendMessageRecords());
return cost;
}
@Override
public double getMonthlyRent() {
return 0;
}
}
abstract class MessageChargingRule extends ChargeRule{
abstract double calCost(ArrayList<MessageRecord> messageRecords);
}
class MessageRecord extends CommunicationRecord {
private String message;
public MessageRecord() {
}
public MessageRecord(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
class SendMessageRule extends MessageChargingRule {
@Override
public double calCost(ArrayList<MessageRecord> messageRecords) {
double cost = 0;
int cnt = 0;
for (MessageRecord messageRecord : messageRecords) {
if (messageRecord.getMessage().length() % 10 != 0) {
cnt += messageRecord.getMessage().length() / 10 + 1;
} else {
cnt += messageRecord.getMessage().length() / 10;
}
}
if (cnt > 5) {
cost = 0.1 * 3 + 0.2 * 2 + (cnt - 5) * 0.3;
} else if (cnt > 3) {
cost = 0.1 * 3 + 0.2 * (cnt - 3);
} else {
cost = 0.1 * cnt;
}
return cost;
}
}
class User {
private UserRecords userRecords = new UserRecords();
private double balance = 100;
private ChargeMode chargeMode;
private String number;
public User() {
}
public User(ChargeMode chargeMode, String number) {
this.chargeMode = chargeMode;
this.number = number;
}
public double calBalance() {
this.balance = this.balance - this.calCost() - this.getChargeMode().getMonthlyRent();
return this.balance;
}
public double calCost() {
return chargeMode.calCost(userRecords);
}
public UserRecords getUserRecords() {
return userRecords;
}
public void setUserRecords(UserRecords userRecords) {
this.userRecords = userRecords;
}
public double getBalance() {
return 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;
}
}
class UserRecords {
private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();//发送短信电话
private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();//接受短信电话
public UserRecords() {
}
public ArrayList<MessageRecord> getSendMessageRecords() {
return sendMessageRecords;
}
public ArrayList<MessageRecord> getReceiveMessageRecords() {
return receiveMessageRecords;
}
public void addSendMessageRecords(MessageRecord sendMessageRecords) {
this.sendMessageRecords.add(sendMessageRecords);
}
public void addReceiveMessageRecords(MessageRecord receiveMessageRecords) {
this.receiveMessageRecords.add(receiveMessageRecords);
}
}
类图:
- 本次新增了短信计费类,增加一种计费方式即可
- 该次题目的难度主要在于格式判断,利用正则表达式进行判断即可
- 格式判断完毕后进行拆分,从而解决问题
踩坑心得
- 1.起初未用正则表达式进行判断,导致很多格式错误
- 2.计费的判断方式没有设计好,导致无法进行计算
- 3.在类中很多地方的属性没有进行初始化,导致出现很多错误
- 4.在第三题中,没有考虑到月租费用,导致计算错误
- 5.在第三题中,没有考虑到利用字符串切割函数时,会将信息也切割,导致错误
改进建议
- 1.每次在加入属性的时候,都要进行初始化,防止出现错误
- 2.在每次进行计算的时候,都要进行判断,防止出现错误
- 3.在每次对一个题目动手的时候,都要先分析题目的类设计,并且为之后的迭代做好准备
总结
标签:OO,java,String,ArrayList,blog,user,new,return,public From: https://www.cnblogs.com/wzxJavaGo/p/16948749.html
- 在本阶段的学习中,我们做了一个关于客房的案例,这个案例用到了之前学习到的很多知识,并且让我们的知识理解更加深入,更加完善。在这段时间,我们也学习了javafx,并且进行了图形界面的开发。
- 在我看来,我觉得我们需要更加深入的学习,现在学习到的很多知识都比较浅,并且由于这段时间的考试较多,没有太多的时间去学习java,所以导致这段时间的知识掌握不是很牢固,应该加强对这段时间所学习到的知识的训练
- 对于老师这段时间的教学,我觉得这种方式确实更能提高我们学生各个方面的能力。