Blog第三次作业
第三次BLOG作业
前言: 不知不觉又度过了一个月,这个月主要的学习内容为PTA的电话计费和实验了。
这次迭代作业题量不多,难度也有所下降,老师的类图也给出来了,其实更多的是自己的理解。
PTA:电话计费:
直接把最终代码奉上叭:
1.Terminal(package)

1 package Terminal; 2 3 import java.util.Scanner; 4 5 import User.User; 6 7 public class Main { 8 9 private static Terminal t = new Terminal(); 10 11 public static void main(String[] args) { 12 Scanner in = new Scanner(System.in); 13 14 for (String s = in.nextLine(); !s.equals("end"); s = in.nextLine()) { 15 if (new RightFormat().isAccountOpening(s)) { 16 String[] m = ParseInput.parseInput(s); 17 t.accountOpening(m); 18 } 19 if (new RightFormat().isImformation(s)) { 20 String[] m = ParseInput.parseInput(s); 21 t.addRecord(m); 22 } 23 if(new RightFormat().isMessage(s)) { 24 String[] m = ParseInput.parseInput(s); 25 t.addMessageRecord(m); 26 } 27 } 28 29 in.close(); 30 31 for (int i = 0; i < t.getUserList().size(); i++) { 32 int min = i; 33 for (int j = i + 1; j < t.getUserList().size(); j++) { 34 if (t.getUserList().get(j).getNumber().charAt(0) == t.getUserList().get(min).getNumber().charAt(0) && 35 Double.parseDouble(t.getUserList().get(j).getNumber()) < Double.parseDouble(t.getUserList().get(min).getNumber())) 36 min = j; 37 } 38 if (min != i) { 39 User temp = t.getUserList().get(min); 40 t.getUserList().set(min, t.getUserList().get(i)); 41 t.getUserList().set(i, temp); 42 } 43 } 44 45 46 for (User u : t.getUserList()) { 47 if(u.getNumber().matches("0[0-9]{9,11}")) { 48 System.out.print(u.getNumber() + " "); 49 System.out.print(String.format("%.1f", u.calCost()) + " "); 50 u.calBalance(); 51 System.out.println(String.format("%.1f", u.getBalance())); 52 } 53 } 54 55 for (User u : t.getUserList()) { 56 if(u.getNumber().matches("1[0-9]{10}")) { 57 System.out.print(u.getNumber() + " "); 58 System.out.print(String.format("%.1f", u.calCost()) + " "); 59 u.calBalance(); 60 System.out.println(String.format("%.1f", u.getBalance())); 61 } 62 } 63 } 64 }

1 package Terminal; 2 3 import java.util.ArrayList; 4 5 import CommunicationRecord.CallRecord; 6 import CommunicationRecord.MessageRecord; 7 import User.CellPhoneCharging; 8 import User.CellPhoneMessageCharging; 9 import User.ChargeMode; 10 import User.LandlinePhoneCharging; 11 import User.User; 12 import User.UserRecords; 13 14 public class Terminal { 15 16 private ArrayList<User> userList = new ArrayList<User>(); 17 18 public ArrayList<User> getUserList() { 19 return userList; 20 } 21 22 public void setUserList(ArrayList<User> userList) { 23 this.userList = userList; 24 } 25 26 public ChargeMode getMode(String s) { 27 if (s.equals("0")) 28 return new LandlinePhoneCharging(); 29 else if (s.equals("1")) 30 return new CellPhoneCharging(); 31 else if (s.equals("3")) 32 return new CellPhoneMessageCharging(); 33 return null; 34 } 35 36 public CallRecord creatRecord(String s[]) { 37 if (s.length == 6) 38 return new CallRecord(s[0], s[1], StringToDate.toDate(s[2] + " " + s[3]), 39 StringToDate.toDate(s[4] + " " + s[5]), s[0].substring(0, 4), s[1].substring(0, 4)); 40 if (s.length == 7) { 41 if (s[1].length() > 4) 42 return new CallRecord(s[0], s[1], StringToDate.toDate(s[3] + " " + s[4]), 43 StringToDate.toDate(s[5] + " " + s[6]), s[0].substring(0, 4), s[2]); 44 if (s[1].length() <= 4) 45 return new CallRecord(s[0], s[2], StringToDate.toDate(s[3] + " " + s[4]), 46 StringToDate.toDate(s[5] + " " + s[6]), s[1], s[2].substring(0, 4)); 47 } 48 if (s.length == 8) 49 return new CallRecord(s[0], s[2], StringToDate.toDate(s[4] + " " + s[5]), 50 StringToDate.toDate(s[6] + " " + s[7]), s[1], s[3]); 51 return null; 52 53 } 54 55 public ArrayList<MessageRecord> creatMessageRecord(String s[]) { 56 ArrayList<MessageRecord> messages = new ArrayList<>(); 57 int i = 0; 58 for( ;i < s[2].length(); i+=10) { 59 if(i + 10 >= s[2].length()) 60 break; 61 messages.add(new MessageRecord(s[0],s[1],s[2].substring(i, i+10))); 62 } 63 messages.add(new MessageRecord(s[0],s[1],s[2].substring(i))); 64 65 return messages; 66 } 67 68 public void addRecord(String[] m) { 69 CallRecord record = creatRecord(m); 70 for (User u : userList) { 71 UserRecords records = u.getUserRecords(); 72 if (u.equals(record.getCallingNumber())) { 73 this.chooseRecordOfMode_calling(records, record, record.getAnsewrAddressAreaCode()); 74 } 75 if (u.equals(record.getAnswerNumber())) { 76 this.chooseRecordOfMode_answer(records, record, record.getAnsewrAddressAreaCode()); 77 } 78 u.setUserRecords(records); 79 } 80 } 81 82 public UserRecords chooseRecordOfMode_calling(UserRecords records, CallRecord callRecord, String areaCode) { 83 if (areaCode.equals("0791")) 84 records.addCallingInCityRecords(callRecord); 85 else if (areaCode.matches("(079[0-9])|(0701)")) 86 records.addCallingInProvinceRecords(callRecord); 87 else if (areaCode.matches("\\d{3,4}")) 88 records.addCallingInLandRecords(callRecord); 89 return records; 90 } 91 92 public UserRecords chooseRecordOfMode_answer(UserRecords records, CallRecord callRecord, String areaCode) { 93 if (areaCode.equals("0791")) 94 records.addAnswerInCityRecords(callRecord); 95 else if (areaCode.matches("(079[0-9])|(0701)")) 96 records.addAnswerInProvinceRecords(callRecord); 97 else if (areaCode.matches("\\d{3,4}")) 98 records.addAnswerInLandRecords(callRecord); 99 return records; 100 } 101 102 public void accountOpening(String[] m) { 103 boolean flag = true; 104 for (User u : userList) { 105 if (u.getNumber().equals(m[0])) 106 flag = false; 107 } 108 if (flag) 109 userList.add(new User(m[0], getMode(m[1]))); 110 } 111 112 public void addMessageRecord(String[] m) { 113 ArrayList<MessageRecord> messageRecords = creatMessageRecord(m); 114 for (User u : userList) { 115 UserRecords records = u.getUserRecords(); 116 if (u.equals(m[0])) { 117 for(MessageRecord a : messageRecords) { 118 records.addSendMessageRecords(a); 119 } 120 } 121 if (u.equals(m[1])) { 122 for(MessageRecord a : messageRecords) { 123 records.addSendMessageRecords(a); 124 } 125 } 126 u.setUserRecords(records); 127 } 128 } 129 130 }

1 package Terminal; 2 3 public class ParseInput { 4 public static String[] parseInput(String s) { 5 if (s.equals("")) 6 return null; 7 else 8 return s.substring(2).split("\\s"); 9 } 10 }

1 package Terminal; 2 3 public class RightFormat { 4 5 private String date = "(((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\." 6 + "((([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]))))|" 7 + "((([0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" 8 + "\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]))"; 9 10 private String areaCode = "([0-9]{3,4})"; 11 12 private String landPhoneNumber = "(0[0-9]{9,11})"; 13 14 private String cellPhoneNumber = "(" + "(1[0-9]{10})" + "\\s" + areaCode + ")"; 15 16 private String phoneNumber = "(" + landPhoneNumber + "|" + cellPhoneNumber + ")"; 17 18 public String getDate() { 19 return date; 20 } 21 22 public void setDate(String date) { 23 this.date = date; 24 } 25 26 public String getAreaCode() { 27 return areaCode; 28 } 29 30 public void setAreaCode(String areaCode) { 31 this.areaCode = areaCode; 32 } 33 34 public String getLandPhoneNumber() { 35 return landPhoneNumber; 36 } 37 38 public void setLandPhoneNumber(String landPhoneNumber) { 39 this.landPhoneNumber = landPhoneNumber; 40 } 41 42 public String getCellPhoneNumber() { 43 return cellPhoneNumber; 44 } 45 46 public void setCellPhoneNumber(String cellPhoneNumber) { 47 this.cellPhoneNumber = cellPhoneNumber; 48 } 49 50 public String getPhoneNumber() { 51 return phoneNumber; 52 } 53 54 public void setPhoneNumber(String phoneNumber) { 55 this.phoneNumber = phoneNumber; 56 } 57 58 public boolean isImformation(String s) { 59 if (s.matches("[t]-" + phoneNumber + "\\s" + phoneNumber + "\\s" + date + "\\s" + date)) 60 return true; 61 return false; 62 } 63 64 public boolean isAccountOpening(String s) { 65 if (s.matches("[u]\\-" + "((0[0-9]{9,11})|([1][0-9]{10}))" + "\\s" + "[0-3]")) 66 return true; 67 return false; 68 } 69 70 public boolean isMessage(String s) { 71 if(s.matches("[m]-" + "((0[0-9]{9,11})|([1][0-9]{10}))" +"\\s"+ "((0[0-9]{9,11})|([1][0-9]{10}))" + "\\s" + "[A-Z|a-z|0-9|\\.|\\,|\\s]+")) 72 return true; 73 return false; 74 } 75 }

1 package Terminal; 2 3 import java.text.ParseException; 4 import java.text.SimpleDateFormat; 5 import java.util.Date; 6 7 public class StringToDate { 8 public static Date toDate(String s) { 9 Date date = null; 10 try { 11 date = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").parse(s); 12 } catch (ParseException e) { 13 // TODO Auto-generated catch block 14 e.printStackTrace(); 15 } 16 17 return date; 18 } 19 }
2.User(package)

1 package User; 2 3 import ChargeRule.*; 4 5 public class User { 6 private double balance; 7 private ChargeMode chargeMode; 8 private String number; 9 private UserRecords userRecords = new UserRecords(); 10 11 public User(String number, ChargeMode chargeMode) { 12 this.number = number; 13 this.chargeMode = chargeMode; 14 this.balance = 100; 15 } 16 17 public double getBalance() { 18 return balance; 19 } 20 21 public void calBalance() { 22 this.balance -= this.calCost() + this.chargeMode.getMonthlyRent(); 23 } 24 25 public ChargeMode getChargeMode() { 26 return chargeMode; 27 } 28 29 public void setChargeMode(ChargeMode chargeMode) { 30 this.chargeMode = chargeMode; 31 } 32 33 public String getNumber() { 34 return number; 35 } 36 37 public void setNumber(String number) { 38 this.number = number; 39 } 40 41 public UserRecords getUserRecords() { 42 return userRecords; 43 } 44 45 public void setUserRecords(UserRecords userRecords) { 46 this.userRecords = userRecords; 47 } 48 49 public double calCost() { 50 double cost = 0; 51 for(ChargeRule r : this.chargeMode.getChargeRules()) { 52 cost += r.calCost(userRecords); 53 } 54 return cost; 55 } 56 57 @Override 58 public boolean equals(Object obj) { 59 User o = (User) obj; 60 if (this.number.equals(o.number)) 61 return true; 62 return false; 63 } 64 65 public boolean equals(String number) { 66 if (this.number.equals(number)) 67 return true; 68 return false; 69 } 70 }

1 package User; 2 3 import java.util.ArrayList; 4 5 import CommunicationRecord.CallRecord; 6 import CommunicationRecord.MessageRecord; 7 8 public class UserRecords { 9 private ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>(); 10 private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>(); 11 private ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>(); 12 private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>(); 13 private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>(); 14 private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>(); 15 private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>(); 16 private ArrayList<MessageRecord> recieveMessageRecords = new ArrayList<MessageRecord>(); 17 18 public void addCallingInCityRecords(CallRecord callingInCityRecords) { 19 this.callingInCityRecords.add(callingInCityRecords); 20 } 21 22 public void addCallingInProvinceRecords(CallRecord callingInProvinceRecords) { 23 this.callingInProvinceRecords.add(callingInProvinceRecords); 24 } 25 26 public void addCallingInLandRecords(CallRecord callingInLandRecords) { 27 this.callingInLandRecords.add(callingInLandRecords); 28 } 29 30 public void addAnswerInCityRecords(CallRecord callingInCityRecords) { 31 this.answerInCityRecords.add(callingInCityRecords); 32 } 33 34 public void addAnswerInProvinceRecords(CallRecord callingInProvinceRecords) { 35 this.answerInProvinceRecords.add(callingInProvinceRecords); 36 } 37 38 public void addAnswerInLandRecords(CallRecord callingInLandRecords) { 39 this.answerInLandRecords.add(callingInLandRecords); 40 } 41 42 public void addSendMessageRecords(MessageRecord sendMessageRecords) { 43 this.sendMessageRecords.add(sendMessageRecords); 44 } 45 46 public void setSendMessageRecords(ArrayList<MessageRecord> sendMessageRecords) { 47 this.sendMessageRecords = sendMessageRecords; 48 } 49 50 public void setRecieveMessageRecords(ArrayList<MessageRecord> recieveMessageRecords) { 51 this.recieveMessageRecords = recieveMessageRecords; 52 } 53 54 public void addRecieveMessageRecords(MessageRecord recieveMessageRecords) { 55 this.sendMessageRecords.add(recieveMessageRecords); 56 } 57 58 public ArrayList<CallRecord> getCallingInCityRecords() { 59 return callingInCityRecords; 60 } 61 62 public ArrayList<CallRecord> getCallingInProvinceRecords() { 63 return callingInProvinceRecords; 64 } 65 66 public ArrayList<CallRecord> getCallingInLandRecords() { 67 return callingInLandRecords; 68 } 69 70 public ArrayList<MessageRecord> getSendMessageRecords() { 71 return sendMessageRecords; 72 } 73 74 public ArrayList<CallRecord> getAnswerInCityRecords() { 75 return answerInCityRecords; 76 } 77 78 public ArrayList<CallRecord> getAnswerInProvinceRecords() { 79 return answerInProvinceRecords; 80 } 81 82 public ArrayList<CallRecord> getAnswerInLandRecords() { 83 return answerInLandRecords; 84 } 85 86 public ArrayList<MessageRecord> getRecieveMessageRecords() { 87 return recieveMessageRecords; 88 } 89 }

1 package User; 2 3 import java.util.ArrayList; 4 5 import ChargeRule.*; 6 7 public class LandlinePhoneCharging extends ChargeMode { 8 9 private double monthlyRent = 20; 10 11 public LandlinePhoneCharging() { 12 super(creatRules()); 13 } 14 15 @Override 16 public double getMonthlyRent() { 17 return this.monthlyRent; 18 } 19 20 public static ArrayList<ChargeRule> creatRules(){ 21 ArrayList<ChargeRule> chargeRules = new ArrayList<ChargeRule>(); 22 23 chargeRules.add(new LandPhoneInCityRule()); 24 chargeRules.add(new LandPhoneInProvinceRule()); 25 chargeRules.add(new LandPhoneLandRule()); 26 27 return chargeRules; 28 } 29 30 }

1 package User; 2 3 import java.util.ArrayList; 4 5 import ChargeRule.*; 6 7 public class CellPhoneCharging extends ChargeMode { 8 9 private double monthlyRent = 15; 10 11 12 @Override 13 public double getMonthlyRent() { 14 return this.monthlyRent; 15 } 16 17 public CellPhoneCharging() { 18 super(creatRules()); 19 } 20 21 public static ArrayList<ChargeRule> creatRules(){ 22 ArrayList<ChargeRule> chargeRules = new ArrayList<ChargeRule>(); 23 24 chargeRules.add(new CellPhoneInCityRule()); 25 chargeRules.add(new CellPhoneInProvinceRule()); 26 chargeRules.add(new CellPhoneLandRule()); 27 28 return chargeRules; 29 } 30 31 }

1 package User; 2 3 import java.util.ArrayList; 4 import ChargeRule.ChargeRule; 5 import ChargeRule.SendMessageRule; 6 7 public class CellPhoneMessageCharging extends ChargeMode { 8 9 private double monthlyRent = 0; 10 11 public CellPhoneMessageCharging() { 12 super(creatRules()); 13 // TODO Auto-generated constructor stub 14 } 15 16 @Override 17 public double getMonthlyRent() { 18 // TODO Auto-generated method stub 19 return this.monthlyRent; 20 } 21 22 public static ArrayList<ChargeRule> creatRules(){ 23 ArrayList<ChargeRule> chargeRules = new ArrayList<ChargeRule>(); 24 25 chargeRules.add(new SendMessageRule()); 26 27 return chargeRules; 28 } 29 30 }

1 package User; 2 3 import java.util.ArrayList; 4 5 import ChargeRule.ChargeRule; 6 7 public abstract class ChargeMode { 8 private ArrayList<ChargeRule> chargeRules ; 9 10 public ChargeMode(ArrayList<ChargeRule> chargeRules) { 11 this.chargeRules = chargeRules; 12 } 13 14 public ArrayList<ChargeRule> getChargeRules() { 15 return chargeRules; 16 } 17 18 public void setChargeRules(ArrayList<ChargeRule> chargeRules) { 19 this.chargeRules = chargeRules; 20 } 21 22 public abstract double getMonthlyRent(); 23 }
3.CommunicationRecord(package)

1 package CommunicationRecord; 2 3 public abstract class CommunicationRecord { 4 protected String callingNumber; 5 protected String answerNumber; 6 7 public String getCallingNumber() { 8 return callingNumber; 9 } 10 11 public void setCallingNumber(String callingNumber) { 12 this.callingNumber = callingNumber; 13 } 14 15 public String getAnswerNumber() { 16 return answerNumber; 17 } 18 19 public void setAnswerNumber(String answerNumber) { 20 this.answerNumber = answerNumber; 21 } 22 23 public CommunicationRecord() { 24 super(); 25 } 26 27 public CommunicationRecord(String callingNumber, String answerNumber) { 28 super(); 29 this.callingNumber = callingNumber; 30 this.answerNumber = answerNumber; 31 } 32 33 }

1 package CommunicationRecord; 2 3 import java.util.Date; 4 5 public class CallRecord extends CommunicationRecord { 6 private Date startTime; 7 private Date endTime; 8 private String callingAddressAreaCode; 9 private String ansewrAddressAreaCode; 10 private long callingTime; 11 12 public Date getStartTime() { 13 return startTime; 14 } 15 16 public void setStartTime(Date startTime) { 17 this.startTime = startTime; 18 } 19 20 public Date getEndTime() { 21 return endTime; 22 } 23 24 25 public void setEndTime(Date endTime) { 26 this.endTime = endTime; 27 } 28 29 public String getCallingAddressAreaCode() { 30 return callingAddressAreaCode; 31 } 32 33 public void setCallingAddressAreaCode(String callingAddressAreaCode) { 34 this.callingAddressAreaCode = callingAddressAreaCode; 35 } 36 37 public String getAnsewrAddressAreaCode() { 38 return ansewrAddressAreaCode; 39 } 40 41 public void setAnsewrAddressAreaCode(String ansewrAddressAreaCode) { 42 this.ansewrAddressAreaCode = ansewrAddressAreaCode; 43 } 44 45 public CallRecord(String callingNumber, String answerNumber, Date date1, Date date2, String callingAddressAreaCode, String ansewrAddressAreaCode) { 46 super(callingNumber, answerNumber); 47 this.startTime = date1; 48 this.endTime = date2; 49 this.callingAddressAreaCode = callingAddressAreaCode; 50 this.ansewrAddressAreaCode = ansewrAddressAreaCode; 51 this.callingTime = time(); 52 } 53 54 public long time() { 55 long time = (this.endTime.getTime() - this.startTime.getTime()) / 1000; 56 return time % 60 == 0 ? time / 60 : (long) (time / 60) + 1; 57 } 58 59 public long getCallingTime() { 60 return callingTime; 61 } 62 63 }

1 package CommunicationRecord; 2 3 public class MessageRecord extends CommunicationRecord { 4 private String message; 5 6 public MessageRecord(String callingNumber, String answerNumber, String message) { 7 super(callingNumber, answerNumber); 8 this.message = message; 9 } 10 11 public String getMessage() { 12 return message; 13 } 14 15 public void setMessage(String message) { 16 this.message = message; 17 } 18 }
4.ChargeRule(package)

1 package ChargeRule; 2 3 import User.UserRecords; 4 5 public abstract class ChargeRule { 6 7 public abstract double calCost(UserRecords userRecords); 8 }

1 package ChargeRule; 2 3 4 5 public abstract class CallChargeRule extends ChargeRule { 6 7 }

1 package ChargeRule; 2 3 import CommunicationRecord.CallRecord; 4 import User.UserRecords; 5 6 public class CellPhoneInCityRule extends CallChargeRule { 7 8 @Override 9 public double calCost(UserRecords userRecords) { 10 double cost = 0; 11 for(CallRecord c : userRecords.getCallingInCityRecords()) { 12 if(c.getCallingAddressAreaCode().equals("0791")) 13 cost += 0.1*c.getCallingTime(); 14 else if(c.getCallingAddressAreaCode().matches("079[0-9]|0701")) 15 cost += 0.3*c.getCallingTime(); 16 else 17 cost += 0.6*c.getCallingTime(); 18 } 19 return cost; 20 } 21 22 }

1 package ChargeRule; 2 3 import CommunicationRecord.CallRecord; 4 import User.UserRecords; 5 6 public class CellPhoneInProvinceRule extends CallChargeRule { 7 8 @Override 9 public double calCost(UserRecords userRecords) { 10 double cost = 0; 11 for(CallRecord c : userRecords.getCallingInProvinceRecords()) { 12 if(c.getCallingAddressAreaCode().equals("0791")) 13 cost += 0.2*c.getCallingTime(); 14 else if(c.getCallingAddressAreaCode().matches("079[0-9]|0701")) 15 cost += 0.3*c.getCallingTime(); 16 else 17 cost += 0.6*c.getCallingTime(); 18 } 19 return cost; 20 } 21 22 }

1 package ChargeRule; 2 3 import CommunicationRecord.CallRecord; 4 import User.UserRecords; 5 6 public class CellPhoneLandRule extends CallChargeRule{ 7 8 @Override 9 public double calCost(UserRecords userRecords) { 10 double cost = 0; 11 for(CallRecord c : userRecords.getCallingInLandRecords()) { 12 if(c.getCallingAddressAreaCode().equals("0791")) 13 cost += 0.3*c.getCallingTime(); 14 else if(c.getCallingAddressAreaCode().matches("079[0-9]|0701")) 15 cost += 0.3*c.getCallingTime(); 16 else 17 cost += 0.6*c.getCallingTime(); 18 } 19 20 for(CallRecord c : userRecords.getAnswerInLandRecords()) { 21 cost += 0.3*c.getCallingTime(); 22 } 23 return cost; 24 } 25 }

1 package ChargeRule; 2 3 import CommunicationRecord.CallRecord; 4 import User.UserRecords; 5 6 public class LandPhoneInCityRule extends CallChargeRule { 7 8 @Override 9 public double calCost(UserRecords userRecords) { 10 double cost = 0; 11 for (CallRecord c : userRecords.getCallingInCityRecords()) { 12 cost += c.getCallingTime() * 0.1; 13 } 14 return cost; 15 } 16 17 }

1 package ChargeRule; 2 3 import CommunicationRecord.CallRecord; 4 import User.UserRecords; 5 6 public class LandPhoneInProvinceRule extends CallChargeRule { 7 8 @Override 9 public double calCost(UserRecords userRecords) { 10 double cost = 0; 11 for (CallRecord c : userRecords.getCallingInProvinceRecords()) { 12 cost += c.getCallingTime() * 0.3; 13 } 14 return cost; 15 } 16 17 }


1 package ChargeRule; 2 3 4 5 public abstract class MessageChargeRule extends ChargeRule { 6 7 }

1 package ChargeRule; 2 3 4 import User.UserRecords; 5 6 public class SendMessageRule extends MessageChargeRule { 7 8 @Override 9 public double calCost(UserRecords userRecords) { 10 int a = userRecords.getSendMessageRecords().size() ; 11 double cost = 0; 12 if(a <= 3) { 13 return 0.1 * a; 14 }else if(a > 3 && a <=5) { 15 return 0.3 + (a-3) * 0.2; 16 }else if(a > 5) { 17 return 0.3 + 0.4 + (a-5) * 0.3; 18 } 19 return cost; 20 } 21 }
以上就是这次作业的代码了。老师的类图:
说一下以上作业心得吧。这次作业是分三次做完的,老师给出了几个类图,在最开始的时候,还是有点不明白,这几个类的作用,也算是跌跌撞撞中摸索出来了方法。
其实就是搞清楚几个大类的层次关系。用户和用户记录,用户记录和用户规则,用户和用户规则。我的逻辑是用户记录管理这多条记录,然后用户通过收费模式能够计算出该模式的总价,然后不同模式下有着相应的收费标准,不同套餐得到用户的记录,然后把所有规则都循环对应计费,得出总价。
遇到的问题 最初的数据输入,比如正则表达式的应用可能会比较复杂,涉及到时间的这部分,然后就是对数据切割,然后打包处理成记录(callRecord),然后一些区号的区分,手机的判定,格式的判定都有藏很多细节。
然后对类设计也有一些感悟,比如一些数据的管理类之间的关系,使用者和终端的关系。如何判断设计两个类之间的关系等等。比如User和ChargeMode的依赖关系,ChargeMode和Rule的组合关系等。理解好类间关系,之后的增加Mode和增加Rule也就是水到渠成的事,前面的代码都不需要改动(这里吐槽一下最后一次作业代码超限问题,不得不删除一些类,然后有一些组合关系例如UserRecords和CallRecord,删着删着就删不明白了)。
2.客房预订
老师上课的时候,为我们搭了一个客房预订系统的框架,并要求我们补全代码。

1 package test54; 2 3 import java.util.ArrayList; 4 5 public class Hotel { 6 private int No; 7 private String name; 8 ArrayList<Room> roomList = new ArrayList<>(); 9 10 public Hotel() { 11 // TODO Auto-generated constructor stub 12 } 13 14 public Hotel(int no, String name, ArrayList<Room> roomList) { 15 super(); 16 No = no; 17 this.name = name; 18 this.roomList = roomList; 19 } 20 21 public int getNo() { 22 return No; 23 } 24 25 public void setNo(int no) { 26 No = no; 27 } 28 29 public String getName() { 30 return name; 31 } 32 33 public void setName(String name) { 34 this.name = name; 35 } 36 37 public ArrayList<Room> getRoomList() { 38 return roomList; 39 } 40 41 public void setRoomList(ArrayList<Room> roomList) { 42 this.roomList = roomList; 43 } 44 45 public void addRoom(Room room) { 46 this.roomList.add(room); 47 } 48 49 public void removeRoom(Room room) { 50 this.roomList.remove(room); 51 } 52 53 public ArrayList<Room> getRoomListbyFloor(int floor){ 54 ArrayList<Room> list = new ArrayList<>(); 55 56 for(Room room:this.roomList) { 57 if(room.getFloor() == floor) { 58 list.add(room); 59 } 60 } 61 62 return list; 63 } 64 65 public Room getRoombyNo(String No) { 66 for(Room room : this.roomList) 67 if(room.getRoomNo().equals(No)) 68 return room; 69 return null; 70 } 71 72 }

1 package test54; 2 3 public abstract class Customer { 4 private String ID; 5 private String name; 6 private String vipType; 7 private double vipDiscount; 8 9 public double getVipDiscount() { 10 return vipDiscount; 11 } 12 13 public void setVipDiscount(double vipDiscount) { 14 this.vipDiscount = vipDiscount; 15 } 16 17 public Customer() { 18 // TODO Auto-generated constructor stub 19 } 20 21 public Customer(String iD, String name) { 22 super(); 23 this.ID = iD; 24 this.name = name; 25 } 26 27 public String getID() { 28 return ID; 29 } 30 31 public void setID(String iD) { 32 ID = iD; 33 } 34 35 public String getVipType() { 36 return vipType; 37 } 38 39 public void setVipType(String vipType) { 40 this.vipType = vipType; 41 } 42 43 public String getName() { 44 return name; 45 } 46 47 public void setName(String name) { 48 this.name = name; 49 } 50 51 public void placeanOrder(Order order) { 52 order.setCustomer(this); 53 } 54 }

1 package test54; 2 3 import java.time.LocalDate; 4 5 public class PaymentRecord { 6 private String OrderId; 7 private LocalDate paydatatime; 8 private double amount; 9 10 public String getOrderId() { 11 return OrderId; 12 } 13 14 public void setOrderId(String orderId) { 15 OrderId = orderId; 16 } 17 18 public LocalDate getPaydatatime() { 19 return paydatatime; 20 } 21 22 public void setPaydatatime(LocalDate paydatatime) { 23 this.paydatatime = paydatatime; 24 } 25 26 public double getAmount() { 27 return amount; 28 } 29 30 public void setAmount(double amount) { 31 this.amount = amount; 32 } 33 34 public PaymentRecord(String orderId, LocalDate paydatatime, double amount) { 35 super(); 36 this.OrderId = orderId; 37 this.paydatatime = paydatatime; 38 this.amount = amount; 39 } 40 41 public void showRecord() { 42 System.out.println("订单号:"+this.OrderId+" "+"总花费:"+this.amount+" "+"支付时间"+this.paydatatime); 43 } 44 }

1 package test54; 2 3 public abstract class Room { 4 private String roomNo; 5 private int floor; 6 private String type; 7 private double price; 8 9 public Room() { 10 // TODO Auto-generated constructor stub 11 } 12 13 public Room(String roomNo, int floor) { 14 super(); 15 this.roomNo = roomNo; 16 this.floor = floor; 17 } 18 19 public String getRoomNo() { 20 return roomNo; 21 } 22 23 public void setRoomNo(String roomNo) { 24 this.roomNo = roomNo; 25 } 26 27 public int getFloor() { 28 return floor; 29 } 30 31 public void setFloor(int floor) { 32 this.floor = floor; 33 } 34 35 public String getType() { 36 return type; 37 } 38 39 public void setType(String type) { 40 this.type = type; 41 } 42 43 public double getPrice() { 44 return price; 45 } 46 47 public void setPrice(double price) { 48 this.price = price; 49 } 50 51 public void showRoom() { 52 System.out.println("类型:"+this.type+" 房号:"+this.roomNo+" 楼层:"+this.floor+" 价格:"+this.price+"/天"); 53 } 54 }

1 package test54; 2 3 public abstract class PaymentMode { 4 5 6 public double getAmount(Order order) { 7 double amount = 0; 8 9 for(OrderDetail orderDetail:order.getOdList()) { 10 amount += orderDetail.getDaysNum() * 11 orderDetail.getRoom().getPrice(); 12 } 13 14 return amount * order.getCustomer().getVipDiscount(); 15 } 16 }

1 package test54; 2 3 public class SilverCardCustomer extends Customer { 4 public SilverCardCustomer() { 5 this.setVipDiscount(0.8); 6 } 7 }

1 package test54; 2 3 import java.time.LocalDate; 4 import java.util.ArrayList; 5 6 public class Order { 7 private String orderNo; 8 private LocalDate orderDate; 9 private Customer customer; 10 private ArrayList<OrderDetail> OdList = new ArrayList<>(); 11 12 public Order() { 13 // TODO Auto-generated constructor stub 14 } 15 16 public Order(String orderNo, LocalDate orderDate, Customer customer, 17 ArrayList<OrderDetail> odList) { 18 super(); 19 this.orderNo = orderNo; 20 this.orderDate = orderDate; 21 this.customer = customer; 22 this.OdList = odList; 23 } 24 25 public String getOrderNo() { 26 return orderNo; 27 } 28 29 public void setOrderNo(String orderNo) { 30 this.orderNo = orderNo; 31 } 32 33 public LocalDate getOrderDate() { 34 return orderDate; 35 } 36 37 public void setOrderDate(LocalDate orderDate) { 38 this.orderDate = orderDate; 39 } 40 41 public Customer getCustomer() { 42 return customer; 43 } 44 45 public void setCustomer(Customer customer) { 46 this.customer = customer; 47 } 48 49 public ArrayList<OrderDetail> getOdList() { 50 return OdList; 51 } 52 53 public void setOdList(ArrayList<OrderDetail> odList) { 54 OdList = odList; 55 } 56 57 public void showOrder() { 58 for(OrderDetail o : this.OdList) { 59 o.show(); 60 } 61 } 62 }

1 package test54; 2 3 import java.util.ArrayList; 4 5 public class OrderDetail { 6 private String id; 7 private Room room; 8 private ArrayList<Customer> customerList = new ArrayList<>(); 9 private int daysNum; 10 11 public OrderDetail() { 12 // TODO Auto-generated constructor stub 13 } 14 15 public OrderDetail(String id, Room room, ArrayList<Customer> customerList, int daysNum) { 16 super(); 17 this.id = id; 18 this.room = room; 19 this.customerList = customerList; 20 this.daysNum = daysNum; 21 } 22 23 public String getId() { 24 return id; 25 } 26 27 public void setId(String id) { 28 this.id = id; 29 } 30 31 public Room getRoom() { 32 return room; 33 } 34 35 public void setRoom(Room room) { 36 this.room = room; 37 } 38 39 public ArrayList<Customer> getCustomerList() { 40 return customerList; 41 } 42 43 public void setCustomerList(ArrayList<Customer> customerList) { 44 this.customerList = customerList; 45 } 46 47 public int getDaysNum() { 48 return daysNum; 49 } 50 51 public void setDaysNum(int daysNum) { 52 this.daysNum = daysNum; 53 } 54 55 public void show() { 56 this.room.showRoom(); 57 System.out.println("id:"+this.id+" 天数:"+this.daysNum); 58 } 59 }

1 package test54; 2 3 public class EcoRoom extends Room { 4 public EcoRoom(String roomNo, int floor) { 5 super(roomNo, floor); 6 this.setType("经济房"); 7 this.setPrice(180); 8 } 9 }

1 package test54; 2 3 public class StrRoom extends Room { 4 public StrRoom(String roomNo, int floor) { 5 super( roomNo, floor); 6 this.setType("标准房"); 7 this.setPrice(240); 8 } 9 }

1 package test54; 2 3 public class GoldCardCustomer extends Customer { 4 public GoldCardCustomer() { 5 this.setVipDiscount(0.7); 6 } 7 }

1 package test54; 2 3 public class AliPay extends PaymentMode { 4 5 }

1 package test54; 2 3 public class Carsh extends PaymentMode { 4 5 }
这是客房预订系统的代码和类图。
这个题目不算难,老师让我们2个小时内也是绰绰有余的,其实和电话那个系统有一些相似之处,例如支付记录。
本次Blog其实更多的是对之前的总结,希望接下来的学习能够更加顺利吧!