OO第三次博客作业
这期间作业主要都是在之前基础上进行扩展,主要加深之前知识点印象,难度依然较大
资费题目主要考验多态设计,利用继承带来便利大大降低代码量,提高代码的复用度
1 import java.math.BigDecimal; 2 import java.math.RoundingMode; 3 import java.text.ParseException; 4 import java.text.SimpleDateFormat; 5 import java.util.*; 6 7 /** 8 * @author LeGend 9 */ 10 public class Main { 11 public static final String regStr2 = "[t]-0791[0-9]{7,8}\\s" + "0[0-9]{9,11}\\s" + 12 "((([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?" + 13 "[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" + 14 "[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" + 15 "\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\\s" + 16 "((([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])\\.(" + 17 "[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" + 18 "[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" + 19 "\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])"; 20 public static final String regStr1 = "u-0791\\d{7,8}\\s[0-2]"; 21 public static Scanner input = new Scanner(System.in); 22 23 public static void main(String[] args) { 24 HashMap<String, User> users = new HashMap<>(); 25 26 String str = input.nextLine(); 27 28 for (; !"end".equals(str); str = input.nextLine()) { 29 if (str.matches(regStr1)) { 30 String number = str.substring(2, str.indexOf(' ')); 31 User user = new User(); 32 if (str.charAt(str.length() - 1) == '0') { 33 user.setChargeMode(new LandlinePhoneCharging()); 34 } 35 user.setNumber(number); 36 users.put(user.getNumber(), user); 37 } else if (str.matches(regStr2)) { 38 39 String[] strings = str.split(" "); 40 41 String numberCalling = strings[0].substring(2); 42 43 // if((strings[1].length() != 11 && strings[1].length() != 12)){ 44 // str = input.nextLine(); 45 // continue; 46 // } 47 48 User caller = users.get(numberCalling); 49 if (caller != null) { 50 CallRecord callRecord = new CallRecord(); 51 callRecord.setCallingNumber(numberCalling); 52 callRecord.setAnswerNumber(strings[1]); 53 Date start; 54 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); 55 try { 56 start = simpleDateFormat.parse(strings[2] + " " + strings[3]); 57 } catch (ParseException e) { 58 continue; 59 } 60 callRecord.setStartTime(start); 61 62 Date end = null; 63 try { 64 end = simpleDateFormat.parse(strings[4] + " " + strings[5]); 65 } catch (ParseException e) { 66 continue; 67 } 68 callRecord.setEndTime(end); 69 70 if (end.before(start)) { 71 continue; 72 } 73 74 String area = strings[1].substring(0, 4); 75 if ("0791".equals(area)) { 76 caller.getUserRecords().addCallingInCityRecords(callRecord); 77 } else if (area.matches("[0][7]([9][0|2-9]|[0][1])")) { 78 caller.getUserRecords().addCallingInProvinceRecords(callRecord); 79 } else { 80 caller.getUserRecords().addCallingInLandRecords(callRecord); 81 } 82 } 83 User answerer = users.get(strings[1]); 84 if (answerer != null) { 85 CallRecord callRecord = new CallRecord(); 86 callRecord.setCallingNumber(numberCalling); 87 callRecord.setAnswerNumber(strings[1]); 88 Date start; 89 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); 90 try { 91 start = simpleDateFormat.parse(strings[2] + " " + strings[3]); 92 } catch (ParseException e) { 93 continue; 94 } 95 callRecord.setStartTime(start); 96 97 Date end = null; 98 try { 99 end = simpleDateFormat.parse(strings[4] + " " + strings[5]); 100 } catch (ParseException e) { 101 continue; 102 } 103 callRecord.setEndTime(end); 104 105 String area = strings[1].substring(0, 4); 106 if ("0791".equals(area)) { 107 answerer.getUserRecords().addAnswerInCityRecords(callRecord); 108 } else if (area.matches("07(9/d)|(01)")) { 109 answerer.getUserRecords().addAnswerInProvinceRecords(callRecord); 110 } else { 111 answerer.getUserRecords().addAnswerInLandRecords(callRecord); 112 } 113 } 114 } 115 } 116 117 118 User[] users1 = users.values().toArray(new User[0]); 119 Arrays.sort(users1, new cmp()); 120 for (User user : users1) { 121 System.out.print(user.getNumber() + " "); 122 System.out.print(user.calCost().setScale(2, RoundingMode.HALF_EVEN).doubleValue() + " "); 123 System.out.println(user.calBalance().setScale(2, RoundingMode.HALF_EVEN).doubleValue()); 124 } 125 126 } 127 128 129 } 130 131 class cmp implements Comparator<User> { 132 133 @Override 134 public int compare(User o1, User o2) { 135 return o1.getNumber().compareTo(o2.getNumber()); 136 } 137 } 138 139 140 class User { 141 private BigDecimal balance = new BigDecimal("100"); 142 private UserRecords userRecords = new UserRecords(); 143 private ChargeMode chargeMode; 144 private String number; 145 146 public User() { 147 } 148 149 public User(ChargeMode chargeMode, String number) { 150 this.chargeMode = chargeMode; 151 this.number = number; 152 } 153 154 public BigDecimal calBalance() { 155 balance = balance.subtract(calCost()); 156 balance = balance.subtract(chargeMode.getMonthlyRent()); 157 return balance; 158 } 159 160 public BigDecimal calCost() { 161 BigDecimal cost; 162 cost = chargeMode.calCost(userRecords); 163 return cost; 164 } 165 166 public UserRecords getUserRecords() { 167 return userRecords; 168 } 169 170 public void setUserRecords(UserRecords userRecords) { 171 this.userRecords = userRecords; 172 } 173 174 public BigDecimal getBalance() { 175 return balance; 176 } 177 178 public ChargeMode getChargeMode() { 179 return chargeMode; 180 } 181 182 public void setChargeMode(ChargeMode chargeMode) { 183 this.chargeMode = chargeMode; 184 } 185 186 public String getNumber() { 187 return number; 188 } 189 190 public void setNumber(String number) { 191 this.number = number; 192 } 193 } 194 195 class UserRecords { 196 private final ArrayList<CallRecord> callingCityRecords = new ArrayList<>(); 197 private final ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<>(); 198 private final ArrayList<CallRecord> callingInLandRecords = new ArrayList<>(); 199 private final ArrayList<CallRecord> answerInCityRecords = new ArrayList<>(); 200 private final ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<>(); 201 private final ArrayList<CallRecord> answerInLandRecords = new ArrayList<>(); 202 private final ArrayList<MessageRecord> sendMessageRecords = new ArrayList<>(); 203 private final ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<>(); 204 205 public ArrayList<CallRecord> getCallingCityRecords() { 206 return callingCityRecords; 207 } 208 209 public ArrayList<CallRecord> getCallingInProvinceRecords() { 210 return callingInProvinceRecords; 211 } 212 213 public ArrayList<CallRecord> getCallingInLandRecords() { 214 return callingInLandRecords; 215 } 216 217 public ArrayList<CallRecord> getAnswerInCityRecords() { 218 return answerInCityRecords; 219 } 220 221 public ArrayList<CallRecord> getAnswerInProvinceRecords() { 222 return answerInProvinceRecords; 223 } 224 225 public ArrayList<CallRecord> getAnswerInLandRecords() { 226 return answerInLandRecords; 227 } 228 229 public ArrayList<MessageRecord> getSendMessageRecords() { 230 return sendMessageRecords; 231 } 232 233 public ArrayList<MessageRecord> getReceiveMessageRecords() { 234 return receiveMessageRecords; 235 } 236 237 public void addCallingInCityRecords(CallRecord callRecord) { 238 callingCityRecords.add(callRecord); 239 } 240 241 public void addCallingInProvinceRecords(CallRecord callRecord) { 242 callingInProvinceRecords.add(callRecord); 243 } 244 245 public void addCallingInLandRecords(CallRecord callRecord) { 246 callingInLandRecords.add(callRecord); 247 } 248 249 public void addAnswerInCityRecords(CallRecord answerRecord) { 250 answerInCityRecords.add(answerRecord); 251 } 252 253 public void addAnswerInProvinceRecords(CallRecord answerRecord) { 254 answerInProvinceRecords.add(answerRecord); 255 } 256 257 public void addAnswerInLandRecords(CallRecord answerRecord) { 258 answerInLandRecords.add(answerRecord); 259 } 260 261 public void addSendMessageRecords(MessageRecord sendMessageRecord) { 262 sendMessageRecords.add(sendMessageRecord); 263 } 264 265 public void addReceiveMessageRecords(MessageRecord receiveMessageRecord) { 266 receiveMessageRecords.add(receiveMessageRecord); 267 } 268 269 270 } 271 272 abstract class CommunicationRecord { 273 private String callingNumber; 274 private String answerNumber; 275 276 277 public CommunicationRecord() { 278 } 279 280 public CommunicationRecord(String callingNumber, String answerNumber) { 281 this.callingNumber = callingNumber; 282 this.answerNumber = answerNumber; 283 } 284 285 public String getCallingNumber() { 286 return callingNumber; 287 } 288 289 public void setCallingNumber(String callingNumber) { 290 this.callingNumber = callingNumber; 291 } 292 293 public String getAnswerNumber() { 294 return answerNumber; 295 } 296 297 public void setAnswerNumber(String answerNumber) { 298 this.answerNumber = answerNumber; 299 } 300 } 301 302 class CallRecord extends CommunicationRecord { 303 private Date startTime; 304 private Date endTime; 305 private String callingAddressAreaCode; 306 private String answerAddressAreaCode; 307 308 public CallRecord() { 309 } 310 311 public CallRecord(Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) { 312 this.startTime = startTime; 313 this.endTime = endTime; 314 this.callingAddressAreaCode = callingAddressAreaCode; 315 this.answerAddressAreaCode = answerAddressAreaCode; 316 } 317 318 public CallRecord(String callingNumber, String answerNumber, Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) { 319 super(callingNumber, answerNumber); 320 this.startTime = startTime; 321 this.endTime = endTime; 322 this.callingAddressAreaCode = callingAddressAreaCode; 323 this.answerAddressAreaCode = answerAddressAreaCode; 324 } 325 326 public Date getStartTime() { 327 return startTime; 328 } 329 330 public void setStartTime(Date startTime) { 331 this.startTime = startTime; 332 } 333 334 public Date getEndTime() { 335 return endTime; 336 } 337 338 public void setEndTime(Date endTime) { 339 this.endTime = endTime; 340 } 341 342 public String getCallingAddressAreaCode() { 343 return callingAddressAreaCode; 344 } 345 346 public void setCallingAddressAreaCode(String callingAddressAreaCode) { 347 this.callingAddressAreaCode = callingAddressAreaCode; 348 } 349 350 public String getAnswerAddressAreaCode() { 351 return answerAddressAreaCode; 352 } 353 354 public void setAnswerAddressAreaCode(String answerAddressAreaCode) { 355 this.answerAddressAreaCode = answerAddressAreaCode; 356 } 357 } 358 359 class MessageRecord extends CommunicationRecord { 360 private String message; 361 362 public MessageRecord() { 363 } 364 365 public MessageRecord(String message) { 366 this.message = message; 367 } 368 369 public MessageRecord(String callingNumber, String answerNumber, String message) { 370 super(callingNumber, answerNumber); 371 this.message = message; 372 } 373 374 public String getMessage() { 375 return message; 376 } 377 378 public void setMessage(String message) { 379 this.message = message; 380 } 381 } 382 383 abstract class ChargeMode { 384 private ArrayList<ChargeRule> chargeRules = new ArrayList<>(); 385 386 387 public ArrayList<ChargeRule> getChargeRules() { 388 return chargeRules; 389 } 390 391 public void setChargeRules(ArrayList<ChargeRule> chargeRules) { 392 this.chargeRules = chargeRules; 393 } 394 395 public abstract BigDecimal calCost(UserRecords userRecords); 396 397 public abstract BigDecimal getMonthlyRent(); 398 399 } 400 401 class LandlinePhoneCharging extends ChargeMode { 402 private final BigDecimal monthlyRent = new BigDecimal("20"); 403 404 public LandlinePhoneCharging() { 405 super.getChargeRules().add(new LandPhoneInCityRule()); 406 super.getChargeRules().add(new LandPhoneInProvinceRule()); 407 super.getChargeRules().add(new LandPhoneInlandRule()); 408 } 409 410 @Override 411 public BigDecimal calCost(UserRecords userRecords) { 412 BigDecimal cost = new BigDecimal(0); 413 cost = cost.add(super.getChargeRules().get(0).calCost(userRecords.getCallingCityRecords())); 414 cost = cost.add(super.getChargeRules().get(1).calCost(userRecords.getCallingInProvinceRecords())); 415 cost = cost.add(super.getChargeRules().get(2).calCost(userRecords.getCallingInLandRecords())); 416 return cost; 417 } 418 419 @Override 420 public BigDecimal getMonthlyRent() { 421 return monthlyRent; 422 } 423 } 424 425 abstract class ChargeRule { 426 abstract BigDecimal calCost(ArrayList<CallRecord> callRecords); 427 } 428 429 abstract class CallChargeRule extends ChargeRule { 430 431 } 432 433 class LandPhoneInCityRule extends CallChargeRule { 434 @Override 435 BigDecimal calCost(ArrayList<CallRecord> callRecords) { 436 BigDecimal result = new BigDecimal(0); 437 438 for (CallRecord callRecord : callRecords) { 439 long lastMills = callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime(); 440 lastMills /= 1000; 441 lastMills += 59; 442 lastMills /= 60; 443 result = result.add(new BigDecimal(lastMills).multiply(new BigDecimal("0.1"))); 444 } 445 return result; 446 } 447 } 448 449 class LandPhoneInProvinceRule extends CallChargeRule { 450 451 @Override 452 BigDecimal calCost(ArrayList<CallRecord> callRecords) { 453 BigDecimal result = new BigDecimal(0); 454 455 for (CallRecord callRecord : callRecords) { 456 long lastMills = callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime(); 457 lastMills /= 1000; 458 lastMills += 59; 459 lastMills /= 60; 460 result = result.add(new BigDecimal(lastMills).multiply(new BigDecimal("0.3"))); 461 } 462 return result; 463 } 464 } 465 466 class LandPhoneInlandRule extends CallChargeRule { 467 468 @Override 469 BigDecimal calCost(ArrayList<CallRecord> callRecords) { 470 BigDecimal result = new BigDecimal(0); 471 472 for (CallRecord callRecord : callRecords) { 473 long lastMills = callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime(); 474 lastMills /= 1000; 475 lastMills += 59; 476 lastMills /= 60; 477 result = result.add(new BigDecimal(lastMills).multiply(new BigDecimal("0.6"))); 478 } 479 return result; 480 } 481 }
1 import java.util.Scanner; 2 3 /** 4 * @author LeGend 5 */ 6 public class Main { 7 public static Scanner input = new Scanner(System.in); 8 9 public static void main(String[] args) { 10 int n = input.nextInt(); 11 Container[] containers = new Container[n]; 12 for (int loop = 0; loop != n; ++loop) { 13 String str = input.next(); 14 if ("cube".equals(str)) { 15 containers[loop] = new Cube(input.nextDouble()); 16 } else if ("cylinder".equals(str)) { 17 containers[loop] = new Cylinder(input.nextDouble(), input.nextDouble()); 18 } 19 } 20 System.out.printf("%.2f\n%.2f", Container.sumofArea(containers), Container.sumofVolume(containers)); 21 } 22 23 } 24 25 abstract class Container { 26 public static final double PI = 3.1415926; 27 28 public static double sumofArea(Container[] c) { 29 double result = 0; 30 for (Container container : c) { 31 result += container.area(); 32 } 33 return result; 34 } 35 36 public static double sumofVolume(Container[] c) { 37 double result = 0; 38 for (Container container : c) { 39 result += container.volume(); 40 } 41 return result; 42 } 43 44 public abstract double area(); 45 46 public abstract double volume(); 47 } 48 49 class Cube extends Container { 50 51 private double line; 52 53 public Cube() { 54 } 55 56 public Cube(double line) { 57 this.line = line; 58 } 59 60 public double getLine() { 61 return line; 62 } 63 64 public void setLine(double line) { 65 this.line = line; 66 } 67 68 @Override 69 public double area() { 70 return line * line * 6; 71 } 72 73 @Override 74 public double volume() { 75 return line * line * line; 76 } 77 } 78 79 class Cylinder extends Container { 80 public double height; 81 private double radius; 82 83 public Cylinder() { 84 } 85 86 public Cylinder(double radius, double height) { 87 this.radius = radius; 88 this.height = height; 89 } 90 91 public double getRadius() { 92 return radius; 93 } 94 95 public void setRadius(double radius) { 96 this.radius = radius; 97 } 98 99 public double getHeight() { 100 return height; 101 } 102 103 public void setHeight(double height) { 104 this.height = height; 105 } 106 107 @Override 108 public double area() { 109 return PI * radius * radius * 2 + height * 2 * PI * radius; 110 } 111 112 @Override 113 public double volume() { 114 return PI * radius * radius * height; 115 } 116 }
1 import java.math.BigDecimal; 2 import java.math.RoundingMode; 3 import java.text.ParseException; 4 import java.text.SimpleDateFormat; 5 import java.util.*; 6 7 /** 8 * @author LeGend 9 */ 10 public class Main { 11 public static final String regStr2 = "[t]-0791[0-9]{7,8}\\s" + "0[0-9]{9,11}\\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])"; 12 public static final String regStr1 = "u-0791\\d{7,8}\\s[0-2]"; 13 public static Scanner input = new Scanner(System.in); 14 public static String datePatten = "((([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])\\.(" + "[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])"; 15 public static String patten1 = "[u][-][0][7][9][1][0-9]{7,8}\\s[0]"; 16 public static String patten2 = "[u][-][1][3][0-9]{9}\\s[1]"; 17 public static String patten3 = "[t]-0791[0-9]{7,8}\\s" + "0[0-9]{9,11}\\s" + datePatten + "\\s" + datePatten;//座机打座机 18 public static String patten4 = "[t]-0791[0-9]{7,8}\\s" + "1[0-9]{10}\\s" + "0[0-9]{2,3}\\s" + datePatten + "\\s" + datePatten;//座机打手机 19 public static String patten5 = "[t]-1[0-9]{10}\\s" + "0[0-9]{2,3}\\s" + "0[0-9]{10,11}\\s" + datePatten + "\\s" + datePatten;//手机打座机 20 public static String patten6 = "[t]-1[0-9]{10}\\s" + "0[0-9]{2,3}\\s" + "1[0-9]{10}\\s" + "0[0-9]{2,3}\\s" + datePatten + "\\s" + datePatten;//手机打手机 21 22 public static void main(String[] args) { 23 HashMap<String, User> users = new HashMap<>(); 24 25 String str = input.nextLine(); 26 27 for (; !"end".equals(str); str = input.nextLine()) { 28 if (str.matches(patten1)) { 29 String number = str.substring(2, str.indexOf(' ')); 30 User user = new User(); 31 user.setChargeMode(new LandlinePhoneCharging()); 32 user.setNumber(number); 33 users.put(user.getNumber(), user); 34 } else if (str.matches(patten2)) { 35 36 String number = str.substring(2, str.indexOf(' ')); 37 User user = new User(); 38 user.setChargeMode(new MobilePhoneCharging()); 39 user.setNumber(number); 40 users.put(user.getNumber(), user); 41 } else if (str.matches(patten3)) { 42 43 String[] strings = str.split(" "); 44 45 String numberCalling = strings[0].substring(2); 46 47 48 User caller = users.get(numberCalling); 49 if (caller != null) { 50 CallRecord callRecord = new CallRecord(); 51 callRecord.setCallingNumber(numberCalling); 52 callRecord.setAnswerNumber(strings[1]); 53 Date start; 54 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); 55 try { 56 start = simpleDateFormat.parse(strings[2] + " " + strings[3]); 57 } catch (ParseException e) { 58 continue; 59 } 60 callRecord.setStartTime(start); 61 62 Date end = null; 63 try { 64 end = simpleDateFormat.parse(strings[4] + " " + strings[5]); 65 } catch (ParseException e) { 66 continue; 67 } 68 callRecord.setEndTime(end); 69 70 if (end.before(start)) { 71 continue; 72 } 73 74 String area = strings[1].substring(0, 4); 75 if ("0791".equals(area)) { 76 caller.getUserRecords().addCallingInCityRecords(callRecord); 77 } else if (area.matches("[0][7]([9][0|2-9]|[0][1])")) { 78 caller.getUserRecords().addCallingInProvinceRecords(callRecord); 79 } else { 80 caller.getUserRecords().addCallingInLandRecords(callRecord); 81 } 82 } 83 User answerer = users.get(strings[1]); 84 if (answerer != null) { 85 CallRecord callRecord = new CallRecord(); 86 callRecord.setCallingNumber(numberCalling); 87 callRecord.setAnswerNumber(strings[1]); 88 Date start; 89 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); 90 try { 91 start = simpleDateFormat.parse(strings[2] + " " + strings[3]); 92 } catch (ParseException e) { 93 continue; 94 } 95 callRecord.setStartTime(start); 96 97 Date end = null; 98 try { 99 end = simpleDateFormat.parse(strings[4] + " " + strings[5]); 100 } catch (ParseException e) { 101 continue; 102 } 103 callRecord.setEndTime(end); 104 105 String area = strings[1].substring(0, 4); 106 if ("0791".equals(area)) { 107 answerer.getUserRecords().addAnswerInCityRecords(callRecord); 108 } else if (area.matches("07(9/d)|(01)")) { 109 answerer.getUserRecords().addAnswerInProvinceRecords(callRecord); 110 } else { 111 answerer.getUserRecords().addAnswerInLandRecords(callRecord); 112 } 113 } 114 } else if (str.matches(patten4)) { 115 //座机打手机 116 117 String[] strings = str.split(" "); 118 119 String numberCalling = strings[0].substring(2); 120 121 122 User caller = users.get(numberCalling); 123 if (caller != null) { 124 CallRecord callRecord = new CallRecord(); 125 callRecord.setCallingNumber(numberCalling); 126 callRecord.setAnswerNumber(strings[1]); 127 Date start; 128 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); 129 try { 130 start = simpleDateFormat.parse(strings[3] + " " + strings[4]); 131 } catch (ParseException e) { 132 continue; 133 } 134 callRecord.setStartTime(start); 135 136 Date end = null; 137 try { 138 end = simpleDateFormat.parse(strings[5] + " " + strings[6]); 139 } catch (ParseException e) { 140 continue; 141 } 142 callRecord.setEndTime(end); 143 144 if (end.before(start)) { 145 continue; 146 } 147 148 String area = strings[2]; 149 if ("0791".equals(area)) { 150 caller.getUserRecords().addCallingInCityRecords(callRecord); 151 } else if (area.matches("[0][7]([9][0|2-9]|[0][1])")) { 152 caller.getUserRecords().addCallingInProvinceRecords(callRecord); 153 } else { 154 caller.getUserRecords().addCallingInLandRecords(callRecord); 155 } 156 } 157 User answerer = users.get(strings[1]); 158 if (answerer != null) { 159 CallRecord callRecord = new CallRecord(); 160 callRecord.setCallingNumber(numberCalling); 161 callRecord.setAnswerNumber(strings[1]); 162 Date start; 163 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); 164 try { 165 start = simpleDateFormat.parse(strings[3] + " " + strings[4]); 166 } catch (ParseException e) { 167 continue; 168 } 169 callRecord.setStartTime(start); 170 171 Date end = null; 172 try { 173 end = simpleDateFormat.parse(strings[5] + " " + strings[6]); 174 } catch (ParseException e) { 175 continue; 176 } 177 callRecord.setEndTime(end); 178 179 String area = strings[2]; 180 if ("0791".equals(area)) { 181 answerer.getUserRecords().addAnswerInCityRecords(callRecord); 182 } else if (area.matches("07(9/d)|(01)")) { 183 answerer.getUserRecords().getAnswerInProvinceRoamRecords().add(callRecord); 184 } else { 185 answerer.getUserRecords().getAnswerInLandRoamRecords().add(callRecord); 186 } 187 } 188 } else if (str.matches(patten5)) { 189 //手机打座机 190 String[] strings = str.split(" "); 191 192 String numberCalling = strings[0].substring(2); 193 194 195 User caller = users.get(numberCalling); 196 if (caller != null) { 197 CallRecord callRecord = new CallRecord(); 198 callRecord.setCallingNumber(numberCalling); 199 callRecord.setAnswerNumber(strings[1]); 200 Date start; 201 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); 202 try { 203 start = simpleDateFormat.parse(strings[3] + " " + strings[4]); 204 } catch (ParseException e) { 205 continue; 206 } 207 callRecord.setStartTime(start); 208 209 Date end = null; 210 try { 211 end = simpleDateFormat.parse(strings[5] + " " + strings[6]); 212 } catch (ParseException e) { 213 continue; 214 } 215 callRecord.setEndTime(end); 216 217 if (end.before(start)) { 218 continue; 219 } 220 221 String callArea = strings[1]; 222 String pickArea = strings[2].substring(0, 4); 223 if ("0791".equals(callArea)) { 224 if ("0791".equals(pickArea)) { 225 caller.getUserRecords().addCallingInCityRecords(callRecord); 226 } else if (pickArea.matches("[0][7]([9][0|2-9]|[0][1])")) { 227 caller.getUserRecords().getCallingInProvinceRecords().add(callRecord); 228 } else { 229 caller.getUserRecords().getCallingInLandRecords().add(callRecord); 230 } 231 }else if(callArea.matches("[0][7]([9][0|2-9]|[0][1])")){ 232 caller.getUserRecords().getCallingInProvinceRoamRecords().add(callRecord); 233 }else { 234 caller.getUserRecords().getCallingInLandRoamRecords().add(callRecord); 235 } 236 } 237 User answerer = users.get(strings[1]); 238 if (answerer != null) { 239 CallRecord callRecord = new CallRecord(); 240 callRecord.setCallingNumber(numberCalling); 241 callRecord.setAnswerNumber(strings[1]); 242 Date start; 243 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); 244 try { 245 start = simpleDateFormat.parse(strings[2] + " " + strings[3]); 246 } catch (ParseException e) { 247 continue; 248 } 249 callRecord.setStartTime(start); 250 251 Date end = null; 252 try { 253 end = simpleDateFormat.parse(strings[4] + " " + strings[5]); 254 } catch (ParseException e) { 255 continue; 256 } 257 callRecord.setEndTime(end); 258 259 String callArea = strings[1]; 260 String pickArea = strings[2].substring(0, 4); 261 if ("0791".equals(pickArea)) { 262 answerer.getUserRecords().addAnswerInCityRecords(callRecord); 263 } else if (pickArea.matches("07(9/d)|(01)")) { 264 answerer.getUserRecords().addAnswerInProvinceRecords(callRecord); 265 } else { 266 answerer.getUserRecords().addAnswerInLandRecords(callRecord); 267 } 268 } 269 } else if (str.matches(patten6)) { 270 //手机打手机 271 String[] strings = str.split(" "); 272 273 String numberCalling = strings[0].substring(2); 274 275 boolean roam = false; 276 277 User caller = users.get(numberCalling); 278 if (caller != null) { 279 CallRecord callRecord = new CallRecord(); 280 callRecord.setCallingNumber(numberCalling); 281 callRecord.setAnswerNumber(strings[2]); 282 Date start; 283 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); 284 try { 285 start = simpleDateFormat.parse(strings[4] + " " + strings[5]); 286 } catch (ParseException e) { 287 continue; 288 } 289 callRecord.setStartTime(start); 290 291 Date end = null; 292 try { 293 end = simpleDateFormat.parse(strings[6] + " " + strings[7]); 294 } catch (ParseException e) { 295 continue; 296 } 297 callRecord.setEndTime(end); 298 299 if (end.before(start)) { 300 continue; 301 } 302 303 String pickArea = strings[3]; 304 String callArea = strings[1]; 305 if ("0791".equals(callArea)) { 306 if ("0791".equals(pickArea)) { 307 caller.getUserRecords().addCallingInCityRecords(callRecord); 308 } else if (pickArea.matches("[0][7]([9][0|2-9]|[0][1])")) { 309 roam = true; 310 caller.getUserRecords().getCallingInProvinceRecords().add(callRecord); 311 } else { 312 roam = true; 313 caller.getUserRecords().getCallingInLandRecords().add(callRecord); 314 } 315 } else if (callArea.matches("[0][7]([9][0|2-9]|[0][1])")) { 316 if ("0791".equals(pickArea)) { 317 roam = true; 318 caller.getUserRecords().getCallingInProvinceRoamRecords().add(callRecord); 319 } else if (pickArea.matches("[0][7]([9][0|2-9]|[0][1])")) { 320 roam = true; 321 caller.getUserRecords().getCallingInProvinceRoamRecords().add(callRecord); 322 } else { 323 roam = true; 324 caller.getUserRecords().getCallingInLandRoamRecords().add(callRecord); 325 } 326 } 327 } 328 User answerer = users.get(strings[2]); 329 if (answerer != null) { 330 CallRecord callRecord = new CallRecord(); 331 callRecord.setCallingNumber(numberCalling); 332 callRecord.setAnswerNumber(strings[1]); 333 Date start; 334 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss"); 335 try { 336 start = simpleDateFormat.parse(strings[4] + " " + strings[5]); 337 } catch (ParseException e) { 338 continue; 339 } 340 callRecord.setStartTime(start); 341 342 Date end = null; 343 try { 344 end = simpleDateFormat.parse(strings[6] + " " + strings[7]); 345 } catch (ParseException e) { 346 continue; 347 } 348 callRecord.setEndTime(end); 349 350 String pickArea = strings[3]; 351 String callArea = strings[1]; 352 353 if (roam) { 354 answerer.getUserRecords().getAnswerInLandRoamRecords().add(callRecord); 355 continue; 356 } 357 358 if ("0791".equals(pickArea)) { 359 answerer.getUserRecords().getAnswerInCityRecords().add(callRecord); 360 } else if (pickArea.matches("[0][7]([9][0|2-9]|[0][1])")) { 361 answerer.getUserRecords().getAnswerInProvinceRoamRecords().add(callRecord); 362 } else { 363 answerer.getUserRecords().getAnswerInLandRoamRecords().add(callRecord); 364 } 365 } 366 367 } 368 } 369 User[] users1 = users.values().toArray(new User[0]); 370 Arrays.sort(users1, new cmp()); 371 for (User user : users1) { 372 System.out.print(user.getNumber() + " "); 373 System.out.print(user.calCost().setScale(2, RoundingMode.HALF_EVEN).doubleValue() + " "); 374 System.out.println(user.calBalance().setScale(2, RoundingMode.HALF_EVEN).doubleValue()); 375 }x 376 } 377 } 378 379 380 class cmp implements Comparator<User> { 381 382 @Override 383 public int compare(User o1, User o2) { 384 return o1.getNumber().compareTo(o2.getNumber()); 385 } 386 } 387 388 389 class User { 390 private BigDecimal balance = new BigDecimal("100"); 391 private UserRecords userRecords = new UserRecords(); 392 private ChargeMode chargeMode; 393 private String number; 394 395 public User() { 396 } 397 398 public User(ChargeMode chargeMode, String number) { 399 this.chargeMode = chargeMode; 400 this.number = number; 401 } 402 403 public BigDecimal calBalance() { 404 balance = balance.subtract(calCost()); 405 balance = balance.subtract(chargeMode.getMonthlyRent()); 406 return balance; 407 } 408 409 public BigDecimal calCost() { 410 BigDecimal cost; 411 cost = chargeMode.calCost(userRecords); 412 return cost; 413 } 414 415 public UserRecords getUserRecords() { 416 return userRecords; 417 } 418 419 public void setUserRecords(UserRecords userRecords) { 420 this.userRecords = userRecords; 421 } 422 423 public BigDecimal getBalance() { 424 return balance; 425 } 426 427 public ChargeMode getChargeMode() { 428 return chargeMode; 429 } 430 431 public void setChargeMode(ChargeMode chargeMode) { 432 this.chargeMode = chargeMode; 433 } 434 435 public String getNumber() { 436 return number; 437 } 438 439 public void setNumber(String number) { 440 this.number = number; 441 } 442 } 443 444 class UserRecords { 445 private final ArrayList<CallRecord> callingCityRecords = new ArrayList<>(); 446 private final ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<>(); 447 private final ArrayList<CallRecord> callingInLandRecords = new ArrayList<>(); 448 private final ArrayList<CallRecord> callingInProvinceRoamRecords = new ArrayList<>(); 449 private final ArrayList<CallRecord> callingInLandRoamRecords = new ArrayList<>(); 450 451 452 private final ArrayList<CallRecord> answerInCityRecords = new ArrayList<>(); 453 private final ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<>(); 454 private final ArrayList<CallRecord> answerInLandRecords = new ArrayList<>(); 455 456 private final ArrayList<CallRecord> answerInProvinceRoamRecords = new ArrayList<>(); 457 private final ArrayList<CallRecord> answerInLandRoamRecords = new ArrayList<>(); 458 private final ArrayList<MessageRecord> sendMessageRecords = new ArrayList<>(); 459 private final ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<>(); 460 461 462 public ArrayList<CallRecord> getCallingCityRecords() { 463 return callingCityRecords; 464 } 465 466 public ArrayList<CallRecord> getCallingInProvinceRecords() { 467 return callingInProvinceRecords; 468 } 469 470 public ArrayList<CallRecord> getCallingInLandRecords() { 471 return callingInLandRecords; 472 } 473 474 public ArrayList<CallRecord> getAnswerInCityRecords() { 475 return answerInCityRecords; 476 } 477 478 public ArrayList<CallRecord> getAnswerInProvinceRecords() { 479 return answerInProvinceRecords; 480 } 481 482 public ArrayList<CallRecord> getAnswerInLandRecords() { 483 return answerInLandRecords; 484 } 485 486 public ArrayList<MessageRecord> getSendMessageRecords() { 487 return sendMessageRecords; 488 } 489 490 public ArrayList<MessageRecord> getReceiveMessageRecords() { 491 return receiveMessageRecords; 492 } 493 494 public void addCallingInCityRecords(CallRecord callRecord) { 495 callingCityRecords.add(callRecord); 496 } 497 498 public void addCallingInProvinceRecords(CallRecord callRecord) { 499 callingInProvinceRecords.add(callRecord); 500 } 501 502 public void addCallingInLandRecords(CallRecord callRecord) { 503 callingInLandRecords.add(callRecord); 504 } 505 506 public void addAnswerInCityRecords(CallRecord answerRecord) { 507 answerInCityRecords.add(answerRecord); 508 } 509 510 public void addAnswerInProvinceRecords(CallRecord answerRecord) { 511 answerInProvinceRecords.add(answerRecord); 512 } 513 514 public void addAnswerInLandRecords(CallRecord answerRecord) { 515 answerInLandRecords.add(answerRecord); 516 } 517 518 public void addSendMessageRecords(MessageRecord sendMessageRecord) { 519 sendMessageRecords.add(sendMessageRecord); 520 } 521 522 public void addReceiveMessageRecords(MessageRecord receiveMessageRecord) { 523 receiveMessageRecords.add(receiveMessageRecord); 524 } 525 526 public ArrayList<CallRecord> getCallingInProvinceRoamRecords() { 527 return callingInProvinceRoamRecords; 528 } 529 530 public ArrayList<CallRecord> getCallingInLandRoamRecords() { 531 return callingInLandRoamRecords; 532 } 533 534 public ArrayList<CallRecord> getAnswerInProvinceRoamRecords() { 535 return answerInProvinceRoamRecords; 536 } 537 538 public ArrayList<CallRecord> getAnswerInLandRoamRecords() { 539 return answerInLandRoamRecords; 540 } 541 } 542 543 abstract class CommunicationRecord { 544 private String callingNumber; 545 private String answerNumber; 546 547 548 public CommunicationRecord() { 549 } 550 551 public CommunicationRecord(String callingNumber, String answerNumber) { 552 this.callingNumber = callingNumber; 553 this.answerNumber = answerNumber; 554 } 555 556 public String getCallingNumber() { 557 return callingNumber; 558 } 559 560 public void setCallingNumber(String callingNumber) { 561 this.callingNumber = callingNumber; 562 } 563 564 public String getAnswerNumber() { 565 return answerNumber; 566 } 567 568 public void setAnswerNumber(String answerNumber) { 569 this.answerNumber = answerNumber; 570 } 571 } 572 573 class CallRecord extends CommunicationRecord { 574 private Date startTime; 575 private Date endTime; 576 private String callingAddressAreaCode; 577 private String answerAddressAreaCode; 578 579 public CallRecord() { 580 } 581 582 public CallRecord(Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) { 583 this.startTime = startTime; 584 this.endTime = endTime; 585 this.callingAddressAreaCode = callingAddressAreaCode; 586 this.answerAddressAreaCode = answerAddressAreaCode; 587 } 588 589 public CallRecord(String callingNumber, String answerNumber, Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) { 590 super(callingNumber, answerNumber); 591 this.startTime = startTime; 592 this.endTime = endTime; 593 this.callingAddressAreaCode = callingAddressAreaCode; 594 this.answerAddressAreaCode = answerAddressAreaCode; 595 } 596 597 public Date getStartTime() { 598 return startTime; 599 } 600 601 public void setStartTime(Date startTime) { 602 this.startTime = startTime; 603 } 604 605 public Date getEndTime() { 606 return endTime; 607 } 608 609 public void setEndTime(Date endTime) { 610 this.endTime = endTime; 611 } 612 613 public String getCallingAddressAreaCode() { 614 return callingAddressAreaCode; 615 } 616 617 public void setCallingAddressAreaCode(String callingAddressAreaCode) { 618 this.callingAddressAreaCode = callingAddressAreaCode; 619 } 620 621 public String getAnswerAddressAreaCode() { 622 return answerAddressAreaCode; 623 } 624 625 public void setAnswerAddressAreaCode(String answerAddressAreaCode) { 626 this.answerAddressAreaCode = answerAddressAreaCode; 627 } 628 } 629 630 class MessageRecord extends CommunicationRecord { 631 private String message; 632 633 public MessageRecord() { 634 } 635 636 public MessageRecord(String message) { 637 this.message = message; 638 } 639 640 public MessageRecord(String callingNumber, String answerNumber, String message) { 641 super(callingNumber, answerNumber); 642 this.message = message; 643 } 644 645 public String getMessage() { 646 return message; 647 } 648 649 public void setMessage(String message) { 650 this.message = message; 651 } 652 } 653 654 abstract class ChargeMode { 655 private ArrayList<ChargeRule> chargeRules = new ArrayList<>(); 656 657 658 public ArrayList<ChargeRule> getChargeRules() { 659 return chargeRules; 660 } 661 662 public void setChargeRules(ArrayList<ChargeRule> chargeRules) { 663 this.chargeRules = chargeRules; 664 } 665 666 public abstract BigDecimal calCost(UserRecords userRecords); 667 668 public abstract BigDecimal getMonthlyRent(); 669 670 } 671 672 class LandlinePhoneCharging extends ChargeMode { 673 private final BigDecimal monthlyRent = new BigDecimal("20"); 674 675 public LandlinePhoneCharging() { 676 super.getChargeRules().add(new LandPhoneInCityRule()); 677 super.getChargeRules().add(new LandPhoneInProvinceRule()); 678 super.getChargeRules().add(new LandPhoneInlandRule()); 679 } 680 681 @Override 682 public BigDecimal calCost(UserRecords userRecords) { 683 BigDecimal cost = new BigDecimal(0); 684 cost = cost.add(super.getChargeRules().get(0).calCostCall(userRecords.getCallingCityRecords())); 685 cost = cost.add(super.getChargeRules().get(1).calCostCall(userRecords.getCallingInProvinceRecords())); 686 cost = cost.add(super.getChargeRules().get(2).calCostCall(userRecords.getCallingInLandRecords())); 687 return cost; 688 } 689 690 @Override 691 public BigDecimal getMonthlyRent() { 692 return monthlyRent; 693 } 694 } 695 696 class MobilePhoneCharging extends ChargeMode { 697 private final BigDecimal monthlyRent = new BigDecimal("15"); 698 699 public MobilePhoneCharging() { 700 super.getChargeRules().add(new MobilePhoneInCityRule()); 701 super.getChargeRules().add(new MobilePhoneInProvinceRule()); 702 super.getChargeRules().add(new MobilePhoneInLandRule()); 703 super.getChargeRules().add(new MobilePhoneInProvinceRomeRule()); 704 super.getChargeRules().add(new MobilePhoneInLaneRomeRule()); 705 } 706 707 @Override 708 public BigDecimal calCost(UserRecords userRecords) { 709 BigDecimal cost = new BigDecimal(0); 710 cost = cost.add(super.getChargeRules().get(0).calCostCall(userRecords.getCallingCityRecords())); 711 cost = cost.add(super.getChargeRules().get(1).calCostCall(userRecords.getCallingInProvinceRecords())); 712 cost = cost.add(super.getChargeRules().get(2).calCostCall(userRecords.getCallingInLandRecords())); 713 cost = cost.add(super.getChargeRules().get(3).calCostCall(userRecords.getCallingInProvinceRecords())); 714 cost = cost.add(super.getChargeRules().get(4).calCostCall(userRecords.getCallingInLandRoamRecords())); 715 716 cost = cost.add(super.getChargeRules().get(0).calCostPick(userRecords.getAnswerInCityRecords())); 717 cost = cost.add(super.getChargeRules().get(1).calCostPick((userRecords.getAnswerInProvinceRecords()))); 718 cost = cost.add(super.getChargeRules().get(2).calCostPick((userRecords.getAnswerInLandRecords()))); 719 cost = cost.add(super.getChargeRules().get(3).calCostPick((userRecords.getAnswerInProvinceRoamRecords()))); 720 cost = cost.add(super.getChargeRules().get(4).calCostPick((userRecords.getAnswerInLandRoamRecords()))); 721 return cost; 722 } 723 724 @Override 725 public BigDecimal getMonthlyRent() { 726 return monthlyRent; 727 } 728 } 729 730 abstract class ChargeRule { 731 abstract BigDecimal calCostCall(ArrayList<CallRecord> callRecords); 732 733 abstract BigDecimal calCostPick(ArrayList<CallRecord> callRecords); 734 735 736 } 737 738 abstract class CallChargeRule extends ChargeRule { 739 740 } 741 742 class LandPhoneInCityRule extends CallChargeRule { 743 @Override 744 BigDecimal calCostCall(ArrayList<CallRecord> callRecords) { 745 BigDecimal result = new BigDecimal(0); 746 747 for (CallRecord callRecord : callRecords) { 748 long lastMills = callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime(); 749 lastMills /= 1000; 750 lastMills += 59; 751 lastMills /= 60; 752 result = result.add(new BigDecimal(lastMills).multiply(new BigDecimal("0.1"))); 753 } 754 return result; 755 } 756 757 @Override 758 BigDecimal calCostPick(ArrayList<CallRecord> callRecords) { 759 BigDecimal result = new BigDecimal(0); 760 761 for (CallRecord callRecord : callRecords) { 762 long lastMills = callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime(); 763 lastMills /= 1000; 764 lastMills += 59; 765 lastMills /= 60; 766 result = result.add(new BigDecimal(lastMills).multiply(new BigDecimal("0"))); 767 } 768 return result; 769 } 770 } 771 772 class LandPhoneInProvinceRule extends CallChargeRule { 773 774 @Override 775 BigDecimal calCostCall(ArrayList<CallRecord> callRecords) { 776 BigDecimal result = new BigDecimal(0); 777 778 for (CallRecord callRecord : callRecords) { 779 long lastMills = callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime(); 780 lastMills /= 1000; 781 lastMills += 59; 782 lastMills /= 60; 783 result = result.add(new BigDecimal(lastMills).multiply(new BigDecimal("0.3"))); 784 } 785 return result; 786 } 787 788 @Override 789 BigDecimal calCostPick(ArrayList<CallRecord> callRecords) { 790 BigDecimal result = new BigDecimal(0); 791 792 for (CallRecord callRecord : callRecords) { 793 long lastMills = callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime(); 794 lastMills /= 1000; 795 lastMills += 59; 796 lastMills /= 60; 797 result = result.add(new BigDecimal(lastMills).multiply(new BigDecimal("0"))); 798 } 799 return result; 800 } 801 } 802 803 class LandPhoneInlandRule extends CallChargeRule { 804 805 @Override 806 BigDecimal calCostCall(ArrayList<CallRecord> callRecords) { 807 BigDecimal result = new BigDecimal(0); 808 809 for (CallRecord callRecord : callRecords) { 810 long lastMills = callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime(); 811 lastMills /= 1000; 812 lastMills += 59; 813 lastMills /= 60; 814 result = result.add(new BigDecimal(lastMills).multiply(new BigDecimal("0.6"))); 815 } 816 return result; 817 } 818 819 @Override 820 BigDecimal calCostPick(ArrayList<CallRecord> callRecords) { 821 BigDecimal result = new BigDecimal(0); 822 823 for (CallRecord callRecord : callRecords) { 824 long lastMills = callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime(); 825 lastMills /= 1000; 826 lastMills += 59; 827 lastMills /= 60; 828 result = result.add(new BigDecimal(lastMills).multiply(new BigDecimal("0"))); 829 } 830 return result; 831 } 832 } 833 834 class MobilePhoneInCityRule extends CallChargeRule { 835 836 @Override 837 BigDecimal calCostCall(ArrayList<CallRecord> callRecords) { 838 BigDecimal result = new BigDecimal(0); 839 840 for (CallRecord callRecord : callRecords) { 841 long lastMills = callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime(); 842 lastMills /= 1000; 843 lastMills += 59; 844 lastMills /= 60; 845 result = result.add(new BigDecimal(lastMills).multiply(new BigDecimal("0.1"))); 846 } 847 return result; 848 } 849 850 @Override 851 BigDecimal calCostPick(ArrayList<CallRecord> callRecords) { 852 BigDecimal result = new BigDecimal(0); 853 854 for (CallRecord callRecord : callRecords) { 855 long lastMills = callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime(); 856 lastMills /= 1000; 857 lastMills += 59; 858 lastMills /= 60; 859 result = result.add(new BigDecimal(lastMills).multiply(new BigDecimal("0"))); 860 } 861 return result; 862 } 863 864 } 865 866 class MobilePhoneInProvinceRule extends CallChargeRule { 867 868 @Override 869 BigDecimal calCostCall(ArrayList<CallRecord> callRecords) { 870 BigDecimal result = new BigDecimal(0); 871 872 for (CallRecord callRecord : callRecords) { 873 long lastMills = callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime(); 874 lastMills /= 1000; 875 lastMills += 59; 876 lastMills /= 60; 877 result = result.add(new BigDecimal(lastMills).multiply(new BigDecimal("0.2"))); 878 } 879 return result; 880 } 881 882 @Override 883 BigDecimal calCostPick(ArrayList<CallRecord> callRecords) { 884 BigDecimal result = new BigDecimal(0); 885 886 for (CallRecord callRecord : callRecords) { 887 long lastMills = callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime(); 888 lastMills /= 1000; 889 lastMills += 59; 890 lastMills /= 60; 891 result = result.add(new BigDecimal(lastMills).multiply(new BigDecimal("0"))); 892 } 893 return result; 894 } 895 } 896 897 class MobilePhoneInLandRule extends CallChargeRule { 898 899 @Override 900 BigDecimal calCostCall(ArrayList<CallRecord> callRecords) { 901 BigDecimal result = new BigDecimal(0); 902 903 for (CallRecord callRecord : callRecords) { 904 long lastMills = callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime(); 905 lastMills /= 1000; 906 lastMills += 59; 907 lastMills /= 60; 908 result = result.add(new BigDecimal(lastMills).multiply(new BigDecimal("0.3"))); 909 } 910 return result; 911 } 912 913 @Override 914 BigDecimal calCostPick(ArrayList<CallRecord> callRecords) { 915 BigDecimal result = new BigDecimal(0); 916 917 for (CallRecord callRecord : callRecords) { 918 long lastMills = callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime(); 919 lastMills /= 1000; 920 lastMills += 59; 921 lastMills /= 60; 922 result = result.add(new BigDecimal(lastMills).multiply(new BigDecimal("0"))); 923 } 924 return result; 925 } 926 } 927 928 class MobilePhoneInProvinceRomeRule extends CallChargeRule { 929 930 @Override 931 BigDecimal calCostCall(ArrayList<CallRecord> callRecords) { 932 BigDecimal result = new BigDecimal(0); 933 934 for (CallRecord callRecord : callRecords) { 935 long lastMills = callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime(); 936 lastMills /= 1000; 937 lastMills += 59; 938 lastMills /= 60; 939 result = result.add(new BigDecimal(lastMills).multiply(new BigDecimal("0.3"))); 940 } 941 return result; 942 } 943 944 @Override 945 BigDecimal calCostPick(ArrayList<CallRecord> callRecords) { 946 BigDecimal result = new BigDecimal(0); 947 948 for (CallRecord callRecord : callRecords) { 949 long lastMills = callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime(); 950 lastMills /= 1000; 951 lastMills += 59; 952 lastMills /= 60; 953 result = result.add(new BigDecimal(lastMills).multiply(new BigDecimal("0.3"))); 954 } 955 return result; 956 } 957 } 958 959 class MobilePhoneInLaneRomeRule extends CallChargeRule { 960 961 @Override 962 BigDecimal calCostCall(ArrayList<CallRecord> callRecords) { 963 BigDecimal result = new BigDecimal(0); 964 965 for (CallRecord callRecord : callRecords) { 966 long lastMills = callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime(); 967 lastMills /= 1000; 968 lastMills += 59; 969 lastMills /= 60; 970 result = result.add(new BigDecimal(lastMills).multiply(new BigDecimal("0.6"))); 971 } 972 return result; 973 } 974 975 @Override 976 BigDecimal calCostPick(ArrayList<CallRecord> callRecords) { 977 BigDecimal result = new BigDecimal(0); 978 979 for (CallRecord callRecord : callRecords) { 980 long lastMills = callRecord.getEndTime().getTime() - callRecord.getStartTime().getTime(); 981 lastMills /= 1000; 982 lastMills += 59; 983 lastMills /= 60; 984 result = result.add(new BigDecimal(lastMills).multiply(new BigDecimal("0.6"))); 985 } 986 return result; 987 } 988 }
import java.util.ArrayList; import java.util.Comparator; import java.util.Scanner; /** * @author LeGend */ public class Main { public static Scanner in = new Scanner(System.in); public static void main(String[] args) { int n = in.nextInt(); ArrayList<Student> students = new ArrayList<>(); for (int loop = 0; loop != n; ++loop) { String number = in.next(); String name = in.next(); int age = in.nextInt(); char sex = in.next().charAt(0); Student student = new Student(number, name, age, sex); if (!students.contains(student)) { students.add(student); } } students.sort(new Student.cmp()); System.out.println(students.size()); for (Student student : students) { System.out.println(student); } } } class Student implements Comparator<Student> { private String number; private String name; private int age; private char sex; public Student() { } public Student(String number, String name, int age, char sex) { this.number = number; this.name = name; this.age = age; this.sex = sex; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } static class cmp implements Comparator<Student>{ @Override public int compare(Student o1, Student o2) { return Integer.compare(Integer.parseInt(o1.getNumber()), Integer.parseInt(o2.getNumber())); } } @Override public int compare(Student o1, Student o2) { return Integer.compare(Integer.parseInt(o1.getNumber()), Integer.parseInt(o2.getNumber())); } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Student student = (Student) o; if (getAge() != student.getAge()) { return false; } if (getSex() != student.getSex()) { return false; } if (!getNumber().equals(student.getNumber())) { return false; } return getName().equals(student.getName()); } @Override public int hashCode() { int result = getNumber().hashCode(); result = 31 * result + getName().hashCode(); result = 31 * result + getAge(); result = 31 * result + (int) getSex(); return result; } @Override public String toString() { StringBuilder stringBuilder = new StringBuilder(number); stringBuilder.append(" "); stringBuilder.append(name); stringBuilder.append(" "); stringBuilder.append(age); stringBuilder.append(" "); stringBuilder.append(sex); return stringBuilder.toString(); } }
// 1、导入相关包 //定义员工类 class Employee { private String name; private int age; public Employee() { super(); } public Employee(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } //主函数 public class Main { public static void main(String[] args) { // 1、创建有序集合对象 Collection c ; // 创建3个员工元素对象 for (int i = 0; i < 3; i++) { Scanner sc = new Scanner(System.in); String employeeName = sc.nextLine(); int employeeAge = sc.nextInt(); Employee employee = new Employee(employeeName, employeeAge); c.add(employee); } // 2、创建迭代器遍历集合 Iterator it; //3、遍历 while (it.hasnext) { //4、集合中对象未知,向下转型 Employee e = it.next(); System.out.println(e.getName() + "---" + e.getAge()); } } }
// 1、导入相关包 import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.Scanner; //定义员工类 class Employee { private String name; private int age; public Employee() { super(); } public Employee(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } //主函数 public class Main { public static void main(String[] args) { // 1、创建有序集合对象 Collection<Employee> c = new ArrayList<>(); Scanner sc = new Scanner(System.in); // 创建3个员工元素对象 for (int i = 0; i < 3; i++) { String employeeName = sc.nextLine(); int employeeAge = Integer.parseInt(sc.nextLine()); Employee employee = new Employee(employeeName, employeeAge); c.add(employee); } // 2、创建迭代器遍历集合 Iterator it = c.iterator(); //3、遍历 while (it.hasNext()) { //4、集合中对象未知,向下转型 Employee e = (Employee) it.next(); System.out.println(e.getName() + "---" + e.getAge()); } } }
1 import java.lang.reflect.Array; 2 import java.math.BigDecimal; 3 import java.math.RoundingMode; 4 import java.util.Arrays; 5 import java.util.Comparator; 6 import java.util.HashMap; 7 import java.util.Scanner; 8 9 public class Main { 10 public static Scanner input = new Scanner(System.in); 11 12 13 public static String messagePattern = "([a-z0-9A-Z]|[\\s]|[,]|[.])+"; 14 15 public static String pattern2 = "[m]-1[0-9]{10}\\s" +"1[0-9]{10}\\s"+messagePattern; 16 17 18 public static void main(String[] args) { 19 HashMap<String, User> users = new HashMap<>(); 20 21 for (String str = input.nextLine(); !"end".equals(str); str = input.nextLine()) { 22 if (str.matches("[u][-][1][0-9]{10}\\s[3]")) { 23 String number = str.substring(2, str.indexOf(" ")); 24 User user = new User(number); 25 users.put(number, user); 26 } else if (str.matches(messagePattern)) { 27 String[] strings = str.split(" "); 28 String sender = strings[0].substring(2); 29 User send = users.get(sender); 30 if(send == null){ 31 continue; 32 } 33 send.charge(str.substring(26).length()); 34 } 35 } 36 37 User[] users1 = users.values().toArray(new User[0]); 38 Arrays.sort(users1,new cmp()); 39 for(User user:users1){ 40 System.out.println(user); 41 } 42 43 } 44 45 public static String round(BigDecimal bigDecimal) { 46 bigDecimal = bigDecimal.setScale(2, RoundingMode.HALF_EVEN); 47 return bigDecimal.toPlainString(); 48 } 49 50 } 51 52 class User { 53 private String number; 54 private BigDecimal balance = new BigDecimal(100); 55 56 private BigDecimal fee = new BigDecimal(0); 57 58 private int amount = 1; 59 60 public User(String number) { 61 this.number = number; 62 } 63 64 public User() { 65 } 66 67 public BigDecimal getBalance() { 68 return balance; 69 } 70 71 public void setBalance(BigDecimal balance) { 72 this.balance = balance; 73 } 74 75 public void charge(int textLength) { 76 int num = (textLength - 1) / 10 + 1; 77 for (int loop = 0; loop != num; ++loop) { 78 if (amount <= 3) { 79 fee = fee.add(new BigDecimal("0.1")); 80 } else if (amount <= 5) { 81 fee = fee.add(new BigDecimal("0.2")); 82 } else { 83 fee = fee.add(new BigDecimal("0.3")); 84 } 85 ++amount; 86 } 87 } 88 89 public String getNumber() { 90 return number; 91 } 92 93 public void setNumber(String number) { 94 this.number = number; 95 } 96 97 @Override 98 public String toString() { 99 StringBuffer stringBuffer = new StringBuffer(); 100 101 stringBuffer.append(number); 102 stringBuffer.append(" "); 103 stringBuffer.append(fee.setScale(2,RoundingMode.HALF_EVEN).doubleValue()); 104 stringBuffer.append(" "); 105 stringBuffer.append(balance.subtract(fee).setScale(2,RoundingMode.HALF_EVEN).doubleValue()); 106 107 return stringBuffer.toString(); 108 } 109 } 110 111 class cmp implements Comparator<User> { 112 113 @Override 114 public int compare(User o1, User o2) { 115 return Integer.compare(Integer.parseInt(o1.getNumber()), Integer.parseInt(o2.getNumber())); 116 } 117 }
1 import java.util.Scanner; 2 3 public class Main { 4 public static Scanner input = new Scanner(System.in); 5 6 public static void main(String[] args) { 7 int n = input.nextInt(); 8 Shop shop = new Shop(n); 9 System.out.println("使用了面值为50的购物券进行支付"); 10 shop.getCoupons50().buy(); 11 System.out.println(shop); 12 System.out.println("使用了面值为100的购物券进行支付"); 13 shop.getCoupons100().buy(); 14 System.out.println(shop); 15 } 16 } 17 18 class Shop { 19 private final InnerCoupons coupons50 = new InnerCoupons(50); 20 private final InnerCoupons coupons100 = new InnerCoupons(100); 21 private int milkCount; 22 23 public Shop() { 24 } 25 26 public Shop(int milkCount) { 27 this.milkCount = milkCount; 28 } 29 30 public int getMilkCount() { 31 return milkCount; 32 } 33 34 public void setMilkCount(int milkCount) { 35 this.milkCount = milkCount; 36 } 37 38 public InnerCoupons getCoupons50() { 39 return coupons50; 40 } 41 42 public InnerCoupons getCoupons100() { 43 return coupons100; 44 } 45 46 @Override 47 public String toString() { 48 return String.format("牛奶还剩%d箱",milkCount); 49 } 50 51 class InnerCoupons { 52 private int value; 53 54 public InnerCoupons() { 55 } 56 57 public InnerCoupons(int value) { 58 this.value = value; 59 } 60 61 public void buy() { 62 milkCount -= value / 50; 63 } 64 } 65 }
//动物发生模拟器. 请在下面的【】处添加代码。 public class AnimalShoutTest2 { public static void main(String[] args) { Cat cat = new Cat(); Dog dog = new Dog(); Goat goat = new Goat(); speak(cat); speak(dog); speak(goat); } //定义静态方法speak() 【】 } //定义抽象类Animal 【】class Animal{ 【】 } //基于Animal类,定义猫类Cat,并重写两个抽象方法 class Cat 【】{ 【】 【】 } //基于Animal类,定义狗类Dog,并重写两个抽象方法 class Dog 【】{ 【】 【】 } //基于Animal类,定义山羊类Goat,并重写两个抽象方法 class Goat 【】{ 【】 【】 }
//动物发生模拟器. 请在下面的【】处添加代码。 public class Main { public static void main(String[] args) { Cat cat = new Cat(); Dog dog = new Dog(); Goat goat = new Goat(); speak(cat); speak(dog); speak(goat); } //定义静态方法speak() public static void speak(Animal animal){ System.out.println( animal.getAnimalClass() + "的叫声:"+ animal.shout()); } } //定义抽象类Animal abstract class Animal{ public abstract String getAnimalClass(); public abstract String shout(); } //基于Animal类,定义猫类Cat,并重写两个抽象方法 class Cat extends Animal{ @Override public String getAnimalClass() { return "猫"; } @Override public String shout() { return "喵喵"; } } //基于Animal类,定义狗类Dog,并重写两个抽象方法 class Dog extends Animal{ @Override public String getAnimalClass() { return "狗"; } @Override public String shout() { return "汪汪"; } } //基于Animal类,定义山羊类Goat,并重写两个抽象方法 class Goat extends Animal{ @Override public String getAnimalClass() { return "山羊"; } @Override public String shout() { return "咩咩"; } }