Java面向对象例题总结


这次Java例题做的不是很好。

例题一的代码如下。

  1 import java.util.Date;
  2 import java.util.ArrayList;
  3 import java.text.ParseException;
  4 import java.text.SimpleDateFormat;
  5 import java.util.Date;
  6 import java.util.Scanner;
  7 import java.util.regex.*;
  8 
  9 abstract class CallChargeRule extends ChargeRule{
 10 
 11     public CallChargeRule() {
 12         // TODO Auto-generated constructor stub
 13     }
 14 
 15 }
 16 class CallRecord {
 17 
 18     private Date startTime;
 19     private Date endTime;
 20     private String callingAddressAreaCode;
 21     private String answerAddressAreaCode;
 22     
 23     public CallRecord(Date date,Date date2,String callingAddressAreaCode,String answerAddressAreaCode) {
 24         this.answerAddressAreaCode = answerAddressAreaCode;
 25         this.callingAddressAreaCode = callingAddressAreaCode;
 26         this.startTime = date;
 27         this.endTime = date2;
 28     }
 29 
 30     public Date getStartTime() {
 31         return startTime;
 32     }
 33 
 34     public void setStartTime(Date startTime) {
 35         this.startTime = startTime;
 36     }
 37 
 38     public Date getEndTime() {
 39         return endTime;
 40     }
 41 
 42     public void setEndTime(Date endTime) {
 43         this.endTime = endTime;
 44     }
 45 
 46     public String getCallingAddressAreaCode() {
 47         return callingAddressAreaCode;
 48     }
 49 
 50     public void setCallingAddressAreaCode(String callingAddressAreaCode) {
 51         this.callingAddressAreaCode = callingAddressAreaCode;
 52     }
 53 
 54     public String getAnswerAddressAreaCode() {
 55         return answerAddressAreaCode;
 56     }
 57 
 58     public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
 59         this.answerAddressAreaCode = answerAddressAreaCode;
 60     }
 61 
 62 }
 63 abstract class ChargeMode {
 64 
 65     private ArrayList chargeRules;
 66     
 67     public ChargeMode() {
 68         chargeRules = new ArrayList();
 69     }
 70 
 71     public ArrayList getChargeRules() {
 72         return chargeRules;
 73     }
 74 
 75     public void setChargeRules(ArrayList chargeRules) {
 76         this.chargeRules = chargeRules;
 77     }
 78     
 79     public abstract double calCost(UserRecords userRecords);
 80     
 81     public abstract double getMonthlyRent();
 82 
 83 }
 84 abstract class ChargeRule {
 85 
 86     public ChargeRule() {
 87         // TODO Auto-generated constructor stub
 88     }
 89     
 90     public abstract double calCost(ArrayList callRecords);
 91 
 92 }
 93 abstract class CommunicationRecord {
 94 
 95     private String callingNumber;
 96     private String answerNumber;
 97     
 98     public String getCallingNumber() {
 99         return callingNumber;
100     }
101     public void setCallingNumber(String callingNumber) {
102         this.callingNumber = callingNumber;
103     }
104     public String getAnswerNumber() {
105         return answerNumber;
106     }
107     public void setAnswerNumber(String answerNumber) {
108         this.answerNumber = answerNumber;
109     }
110 
111 }
112 class LandlinePhoneCharging extends ChargeMode {
113 
114     private double monthlyRent;
115     
116     public LandlinePhoneCharging(double monthlyRent) {
117         this.monthlyRent = monthlyRent;
118     }
119 
120     public double calCost(UserRecords userRecords) {
121         // TODO Auto-generated method stub
122         double cost = 0;
123         for(CallRecord i : userRecords.getCallingInCityRecords()) {
124             cost += (int)Math.ceil((i.getEndTime().getTime() - i.getStartTime().getTime()) * 1.0 / 60 / 1000) * 0.1;
125         }
126         for(CallRecord i : userRecords.getCallingInProvinceRecords()) {
127             cost += (int)Math.ceil((i.getEndTime().getTime() - i.getStartTime().getTime()) * 1.0 / 60 / 1000) * 0.3;
128         }
129         for(CallRecord i : userRecords.getCallingInLandRecords()) {
130             cost += (int)Math.ceil((i.getEndTime().getTime() - i.getStartTime().getTime()) * 1.0 / 60 / 1000) * 0.6;
131         }
132         return cost;
133     }
134 
135     @Override
136     public double getMonthlyRent() {
137         // TODO Auto-generated method stub
138         return monthlyRent;
139     }
140 
141 }
142 class LandPhoneInCityRule extends CallChargeRule {
143 
144     public LandPhoneInCityRule() {
145         // TODO Auto-generated constructor stub
146     }
147 
148     @Override
149     public double calCost(ArrayList callRecords) {
150         // TODO Auto-generated method stub
151         return 0;
152     }
153 
154 }
155 class LandPhoneInlandRule extends CallChargeRule {
156 
157     public LandPhoneInlandRule() {
158         // TODO Auto-generated constructor stub
159     }
160 
161     @Override
162     public double calCost(ArrayList callRecords) {
163         // TODO Auto-generated method stub
164         return 0;
165     }
166 
167 }
168 class LandPhoneInProvinceRule extends CallChargeRule {
169 
170     public LandPhoneInProvinceRule() {
171         // TODO Auto-generated constructor stub
172     }
173 
174     @Override
175     public double calCost(ArrayList callRecords) {
176         // TODO Auto-generated method stub
177         return 0;
178     }
179 
180 }
181 class MessageRecord extends CommunicationRecord {
182 
183     private String message;
184     
185     public MessageRecord(String message,String callingNumber,String answerNumber) {
186         this.message = message;
187         this.setCallingNumber(callingNumber);
188         this.setAnswerNumber(answerNumber);
189     }
190 
191     public String getMessage() {
192         return message;
193     }
194 
195     public void setMessage(String message) {
196         this.message = message;
197     }
198 
199 }
200 class User {
201 
202     private UserRecords userRecords;
203     private double balance;
204     private ChargeMode chargeMode;
205     private String number;
206     
207     public User(String number,double money) {
208         userRecords = new UserRecords();
209         chargeMode = new LandlinePhoneCharging(20);
210         this.balance = money;
211         this.number = number;
212     }
213     
214     public double calBalance() {
215         return balance - calCost() - chargeMode.getMonthlyRent();
216     }
217     
218     public double calCost() {
219         return chargeMode.calCost(userRecords);
220     }
221 
222     public UserRecords getUserRecords() {
223         return userRecords;
224     }
225 
226     public void setUserRecords(UserRecords userRecords) {
227         this.userRecords = userRecords;
228     }
229 
230     public double getBalance() {
231         return balance;
232     }
233 
234     public void setBalance(double balance) {
235         this.balance = balance;
236     }
237 
238     public ChargeMode getChargeMode() {
239         return chargeMode;
240     }
241 
242     public void setChargeMode(ChargeMode chargeMode) {
243         this.chargeMode = chargeMode;
244     }
245 
246     public String getNumber() {
247         return number;
248     }
249 
250     public void setNumber(String number) {
251         this.number = number;
252     }
253 
254 }
255 class UserData {
256 
257     private ArrayList users;
258     
259     public UserData() {
260         users = new ArrayList();
261     }
262     
263     public void addUser(User user) {
264         int x = 0;
265         for(int i = 0;i < users.size();i ++) {
266             if(user.getNumber().compareTo(users.get(i).getNumber()) < 0) {
267                 users.add(i, user);
268                 x = 1;
269                 break;
270             }
271             else if(user.getNumber().compareTo(users.get(i).getNumber()) == 0) {
272                 return;
273             }
274         }
275         if(x == 0) {
276             users.add(users.size(),user);
277         }
278     }
279     
280     public void removeUser(User user) {
281         users.remove(user);
282     }
283 
284     public ArrayList getUsers() {
285         return users;
286     }
287     
288     public String show() {
289         String a = "";
290         for(User i : users) {
291             a += ( i.getNumber() + " " + String.format("%.1f", i.calCost()) + " " + String.format("%.1f", i.calBalance()) + "\n");
292         }
293         return a;
294     }
295     
296     public User phoneNumberFindUser(String number) {
297         for(User i : users) {
298             if(i.getNumber().equals(number)) {
299                 return i;
300             }
301         }
302         return null;
303     }
304 
305 }
306 class UserRecords {
307 
308     private ArrayList callingInCityRecords;
309     private ArrayList callingInProvinceRecords;
310     private ArrayList callingInLandRecords;
311     private ArrayList answerInCityRecords;
312     private ArrayList answerInProvinceRecords;
313     private ArrayList answerInLandRecords;
314     private ArrayList sendMessageRecords;
315     private ArrayList receiveMessageRecords;
316     
317     public UserRecords() {
318         callingInCityRecords = new ArrayList();
319         callingInCityRecords = new ArrayList();
320         callingInProvinceRecords = new ArrayList();
321         callingInLandRecords = new ArrayList();
322         answerInCityRecords = new ArrayList();
323         answerInProvinceRecords = new ArrayList();
324         answerInLandRecords = new ArrayList();
325         sendMessageRecords = new ArrayList();
326         receiveMessageRecords = new ArrayList();
327     }
328     
329     public void addCallingInCityRecords(CallRecord callRecord) {
330         this.callingInCityRecords.add(callRecord);
331     }
332     
333     public void addCallingInProvinceRecords(CallRecord callRecord) {
334         this.callingInProvinceRecords.add(callRecord);
335     }
336     
337     public void addCallingInLandRecords(CallRecord callRecord) {
338         this.callingInLandRecords.add(callRecord);
339     }
340     
341     public void addAnswerInCityRecords(CallRecord callRecord) {
342         this.answerInCityRecords.add(callRecord);
343     }
344     
345     public void addAnswerInProvinceRecords(CallRecord callRecord) {
346         this.answerInProvinceRecords.add(callRecord);
347     }
348     
349     public void addAnswerInLandRecords(CallRecord callRecord) {
350         this.answerInLandRecords.add(callRecord);
351     }
352     
353     public void addSendMessageRecords(MessageRecord messageRecord) {
354         this.sendMessageRecords.add(messageRecord);
355     }
356     
357     public void addReceiveMessageRecords(MessageRecord messageRecord) {
358         this.receiveMessageRecords.add(messageRecord);
359     }
360 
361     public ArrayList getCallingInCityRecords() {
362         return callingInCityRecords;
363     }
364 
365     public ArrayList getCallingInProvinceRecords() {
366         return callingInProvinceRecords;
367     }
368 
369     public ArrayList getCallingInLandRecords() {
370         return callingInLandRecords;
371     }
372 
373     public ArrayList getAnswerInCityRecords() {
374         return answerInCityRecords;
375     }
376 
377     public ArrayList getAnswerInProvinceRecords() {
378         return answerInProvinceRecords;
379     }
380 
381     public ArrayList getAnswerInLandRecords() {
382         return answerInLandRecords;
383     }
384 
385     public ArrayList getSendMessageRecords() {
386         return sendMessageRecords;
387     }
388 
389     public ArrayList getReceiveMessageRecords() {
390         return receiveMessageRecords;
391     }
392 
393 }
394 
395 public class Main {
396     
397     private static UserData userData = new UserData();
398 
399     public static int isleap(int year) {
400         if(year % 4 == 0 && year % 100 !=0 || year % 400 == 0) {
401             return 1;
402         }
403         return 0;
404     }
405     
406     public static boolean checkDate(int y,int m,int d) {
407         int monthday[][] = {{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};
408         if(y >= 0 && m <=12 && m >= 1 && d >=1 && d <= monthday[isleap(y)][m]) {
409             return true;
410         }
411         return false;
412     }
413 
414     public static boolean checkInputone(String a) {
415         if(Pattern.matches("u-\\d{11,12} 0",a)) {
416             return true;
417         }
418         else if(Pattern.matches("t-\\d{11,12} \\d{11,12} \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]",a)) {
419             String b[] = a.split(" ");
420             String c[] = b[2].split("\\.");
421             String d[] = b[4].split("\\.");
422             for(int i = 0;i < 3;i ++) {
423                 if(c[i].charAt(0) == '0' || d[i].charAt(0) == '0') {
424                     return false;
425                 }
426             }
427             if(checkDate(Integer.parseInt(c[0]),Integer.parseInt(c[1]),Integer.parseInt(c[2])) && checkDate(Integer.parseInt(d[0]),Integer.parseInt(d[1]),Integer.parseInt(d[2]))) {
428                 return true;
429             }
430         }
431         return false;
432     }
433     
434     public static ArrayList checkInput(ArrayList a) {
435         for(int i = 0;i < a.size();i ++) {
436             if(!checkInputone(a.get(i))) {
437                 a.remove(i);
438             }
439         }
440         return a;
441     }
442     
443     public static ArrayList getInput() {
444         ArrayList a = new ArrayList();
445         Scanner in = new Scanner(System.in);
446         String b = in.nextLine();
447         while(!b.equals("end")) {
448             a.add(b);
449             b = in.nextLine();
450         }
451         return checkInput(a);
452     }
453     
454     public static String ugetNumber(String a) {
455         String b = "";
456         for(int i = 2;i < a.length();i ++) {
457             if(a.charAt(i) == ' ') {
458                 break;
459             }
460             else {
461                 b += a.charAt(i);
462             }
463         }
464         return b;
465     }
466     
467     public static String processDate(String a) {
468         String b = "";
469         for(int i = 0;i < a.length();i ++) {
470             b += a.charAt(i);
471             if(a.charAt(i) == '.') {
472                 for(int j = i + 1;j < a.length();j ++) {
473                     if(a.charAt(j) == '.' || a.charAt(j) == ' ') {
474                         if(j == i + 2) {
475                             b += '0';
476                         }
477                         break;
478                     }
479                 }
480             }
481         }
482         return b;
483     }
484     
485     public static void workone(String a) throws ParseException {
486         if(a.charAt(0) == 'u') {
487             userData.addUser(new User(ugetNumber(a),100));
488         }
489         else if(a.charAt(0) == 't') {
490             a = a.substring(2);
491             String b[] = a.split(" ");
492             SimpleDateFormat  sdf =new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
493             Date startDate = sdf.parse(processDate(b[2] + " " + b[3]));
494             Date endDate = sdf.parse(processDate(b[4] + " " + b[5]));
495             if(b[0].substring(0, 4).equals(b[1].substring(0, 4))) {
496                 if(userData.phoneNumberFindUser(b[0]) != null && startDate.getTime() < endDate.getTime()){
497                     userData.phoneNumberFindUser(b[0]).getUserRecords().addCallingInCityRecords(new CallRecord(startDate,endDate,b[0].substring(0, 4),b[1].substring(0, 4)));
498                 }
499                 if(userData.phoneNumberFindUser(b[1]) != null && startDate.getTime() < endDate.getTime()) {
500                     userData.phoneNumberFindUser(b[1]).getUserRecords().addAnswerInCityRecords(new CallRecord(startDate,endDate,b[0].substring(0, 4),b[1].substring(0, 4)));
501                 }
502             }
503             else {
504                 if((b[0].substring(0, 3).equals("079") || b[0].substring(0, 4).equals("0701")) && (b[1].substring(0, 3).equals("079") || b[1].substring(0, 4).equals("0701"))) {
505                     if(userData.phoneNumberFindUser(b[0]) != null && startDate.getTime() < endDate.getTime()){
506                         userData.phoneNumberFindUser(b[0]).getUserRecords().addCallingInProvinceRecords(new CallRecord(startDate,endDate,b[0].substring(0, 4),b[1].substring(0, 4)));
507                     }
508                     if(userData.phoneNumberFindUser(b[1]) != null && startDate.getTime() < endDate.getTime()) {
509                         userData.phoneNumberFindUser(b[1]).getUserRecords().addAnswerInProvinceRecords(new CallRecord(startDate,endDate,b[0].substring(0, 4),b[1].substring(0, 4)));
510                     }
511                 }
512                 else {
513                     if(userData.phoneNumberFindUser(b[0]) != null && startDate.getTime() < endDate.getTime()){
514                         userData.phoneNumberFindUser(b[0]).getUserRecords().addCallingInLandRecords(new CallRecord(startDate,endDate,b[0].substring(0, 4),b[1].substring(0, 4)));
515                     }
516                     if(userData.phoneNumberFindUser(b[1]) != null && startDate.getTime() < endDate.getTime()) {
517                         userData.phoneNumberFindUser(b[1]).getUserRecords().addAnswerInLandRecords(new CallRecord(startDate,endDate,b[0].substring(0, 4),b[1].substring(0, 4)));
518                     }
519                 }
520             }
521         }
522     }
523     
524     public static void work(ArrayList a) {
525         for(String i : a) {
526             try {
527                 workone(i);
528             } catch (ParseException e) {
529                 // TODO Auto-generated catch block
530                 e.printStackTrace();
531             }
532         }
533     }
534     
535     public static String getOutput(ArrayList a) {
536         work(a);
537         return userData.show();
538     }
539     
540     public static void main(String[] args) {
541         System.out.printf(getOutput(getInput()));
542     }
543 
544 }
例题一代码

例题一的代码有一个很大问题就是数据的存储是错误的,这是由于我没有充分理解这个题目的意思,或者是对电线收费的概念生疏。在储存数据的workone方法中,就能看出来这个错误。

例题二的代码如下。

import java.util.Date;
import java.util.ArrayList;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.regex.*;



abstract class CallChargeRule extends ChargeRule{

    public CallChargeRule() {
        // TODO Auto-generated constructor stub
    }

}
class CallRecord {

    private Date startTime;
    private Date endTime;
    private String callingAddressAreaCode;
    private String answerAddressAreaCode;
    
    public CallRecord(Date date,Date date2,String callingAddressAreaCode,String answerAddressAreaCode) {
        this.answerAddressAreaCode = answerAddressAreaCode;
        this.callingAddressAreaCode = callingAddressAreaCode;
        this.startTime = date;
        this.endTime = date2;
    }

    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 chargeRules;
    
    public ChargeMode() {
        chargeRules = new ArrayList();
    }

    public ArrayList getChargeRules() {
        return chargeRules;
    }

    public void setChargeRules(ArrayList chargeRules) {
        this.chargeRules = chargeRules;
    }
    
    public abstract double calCost(UserRecords userRecords);
    
    public abstract double getMonthlyRent();

}
abstract class ChargeRule {

    public ChargeRule() {
        // TODO Auto-generated constructor stub
    }
    
    public abstract double calCost(ArrayList callRecords);

}
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 {

    private double monthlyRent;
    
    public LandlinePhoneCharging(double monthlyRent) {
        this.monthlyRent = monthlyRent;
    }

    public double calCost(UserRecords userRecords) {
        // TODO Auto-generated method stub
        double cost = 0;
        for(CallRecord i : userRecords.getCallingInCityRecords()) {
            if(i.getAnswerAddressAreaCode().equals(i.getCallingAddressAreaCode())) {
                cost += (int)Math.ceil((i.getEndTime().getTime() - i.getStartTime().getTime()) * 1.0 / 60 / 1000) * 0.1;
            }
            else if(Pattern.matches("(0701)|(079[0-9])",i.getAnswerAddressAreaCode())) {
                cost += (int)Math.ceil((i.getEndTime().getTime() - i.getStartTime().getTime()) * 1.0 / 60 / 1000) * 0.3;
            }
            else {
                cost += (int)Math.ceil((i.getEndTime().getTime() - i.getStartTime().getTime()) * 1.0 / 60 / 1000) * 0.6;
            }
        }
        return cost;
    }

    @Override
    public double getMonthlyRent() {
        // TODO Auto-generated method stub
        return monthlyRent;
    }

}
class LandPhoneInCityRule extends CallChargeRule {

    public LandPhoneInCityRule() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public double calCost(ArrayList callRecords) {
        // TODO Auto-generated method stub
        return 0;
    }

}
class LandPhoneInlandRule extends CallChargeRule {

    public LandPhoneInlandRule() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public double calCost(ArrayList callRecords) {
        // TODO Auto-generated method stub
        return 0;
    }

}
class LandPhoneInProvinceRule extends CallChargeRule {

    public LandPhoneInProvinceRule() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public double calCost(ArrayList callRecords) {
        // TODO Auto-generated method stub
        return 0;
    }

}
class MessageRecord extends CommunicationRecord {

    private String message;
    
    public MessageRecord(String message,String callingNumber,String answerNumber) {
        this.message = message;
        this.setCallingNumber(callingNumber);
        this.setAnswerNumber(answerNumber);
    }

    public String getMessage() {
        return message;
    }

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

}
class User {

    private UserRecords userRecords;
    private double balance;
    private ChargeMode chargeMode;
    private String number;
    
    public User(String number,double money,ChargeMode chargeMode) {
        userRecords = new UserRecords();
        this.chargeMode = chargeMode;
        this.balance = money;
        this.number = number;
    }
    
    public double calBalance() {
        return balance - calCost() - chargeMode.getMonthlyRent();
    }
    
    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 void setBalance(double balance) {
        this.balance = balance;
    }

    public ChargeMode getChargeMode() {
        return chargeMode;
    }

    public void setChargeMode(ChargeMode chargeMode) {
        this.chargeMode = chargeMode;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

}
class UserData {

    private ArrayList users;
    
    public UserData() {
        users = new ArrayList();
    }
    
    public void addUser(User user) {
        int x = 0;
        for(int i = 0;i < users.size();i ++) {
            if(user.getNumber().compareTo(users.get(i).getNumber()) < 0) {
                users.add(i, user);
                x = 1;
                break;
            }
            else if(user.getNumber().compareTo(users.get(i).getNumber()) == 0) {
                return;
            }
        }
        if(x == 0) {
            users.add(users.size(),user);
        }
    }
    
    public void removeUser(User user) {
        users.remove(user);
    }

    public ArrayList getUsers() {
        return users;
    }
    
    public String show() {
        String a = "";
        for(User i : users) {
            a += ( i.getNumber() + " " + String.format("%.1f", i.calCost()) + " " + String.format("%.1f", i.calBalance()));
            if(!i.getNumber().equals(users.get(users.size() - 1).getNumber())){
                a += "\n";
            }
        }
        return a;
    }
    
    public User phoneNumberFindUser(String number) {
        for(User i : users) {
            if(i.getNumber().equals(number)) {
                return i;
            }
        }
        return null;
    }

}
class UserRecords {

    private ArrayList callingInCityRecords;
    private ArrayList callingInProvinceRecords;
    private ArrayList callingInLandRecords;
    private ArrayList answerInCityRecords;
    private ArrayList answerInProvinceRecords;
    private ArrayList answerInLandRecords;
    private ArrayList sendMessageRecords;
    private ArrayList receiveMessageRecords;
    
    public UserRecords() {
        callingInCityRecords = new ArrayList();
        callingInCityRecords = new ArrayList();
        callingInProvinceRecords = new ArrayList();
        callingInLandRecords = new ArrayList();
        answerInCityRecords = new ArrayList();
        answerInProvinceRecords = new ArrayList();
        answerInLandRecords = new ArrayList();
        sendMessageRecords = new ArrayList();
        receiveMessageRecords = new ArrayList();
    }
    
    public void addCallingInCityRecords(CallRecord callRecord) {
        this.callingInCityRecords.add(callRecord);
    }
    
    public void addCallingInProvinceRecords(CallRecord callRecord) {
        this.callingInProvinceRecords.add(callRecord);
    }
    
    public void addCallingInLandRecords(CallRecord callRecord) {
        this.callingInLandRecords.add(callRecord);
    }
    
    public void addAnswerInCityRecords(CallRecord callRecord) {
        this.answerInCityRecords.add(callRecord);
    }
    
    public void addAnswerInProvinceRecords(CallRecord callRecord) {
        this.answerInProvinceRecords.add(callRecord);
    }
    
    public void addAnswerInLandRecords(CallRecord callRecord) {
        this.answerInLandRecords.add(callRecord);
    }
    
    public void addSendMessageRecords(MessageRecord messageRecord) {
        this.sendMessageRecords.add(messageRecord);
    }
    
    public void addReceiveMessageRecords(MessageRecord messageRecord) {
        this.receiveMessageRecords.add(messageRecord);
    }

    public ArrayList getCallingInCityRecords() {
        return callingInCityRecords;
    }

    public ArrayList getCallingInProvinceRecords() {
        return callingInProvinceRecords;
    }

    public ArrayList getCallingInLandRecords() {
        return callingInLandRecords;
    }

    public ArrayList getAnswerInCityRecords() {
        return answerInCityRecords;
    }

    public ArrayList getAnswerInProvinceRecords() {
        return answerInProvinceRecords;
    }

    public ArrayList getAnswerInLandRecords() {
        return answerInLandRecords;
    }

    public ArrayList getSendMessageRecords() {
        return sendMessageRecords;
    }

    public ArrayList getReceiveMessageRecords() {
        return receiveMessageRecords;
    }

}

class LandmobilePhoneCharging extends ChargeMode {

    private double monthlyRent;
    
    public LandmobilePhoneCharging(double monthlyRent) {
        this.monthlyRent = monthlyRent;
    }
    
    @Override
    public double calCost(UserRecords userRecords) {
        double cost = 0;
        for(CallRecord i : userRecords.getCallingInCityRecords()) {
            if(i.getAnswerAddressAreaCode().equals(i.getCallingAddressAreaCode())) {
                cost += (int)Math.ceil((i.getEndTime().getTime() - i.getStartTime().getTime()) * 1.0 / 60 / 1000) * 0.1;
            }
            else if(Pattern.matches("(0701)|(079[0-9])",i.getAnswerAddressAreaCode())) {
                cost += (int)Math.ceil((i.getEndTime().getTime() - i.getStartTime().getTime()) * 1.0 / 60 / 1000) * 0.2;
            }
            else {
                cost += (int)Math.ceil((i.getEndTime().getTime() - i.getStartTime().getTime()) * 1.0 / 60 / 1000) * 0.3;
            }
        }
        for(CallRecord i : userRecords.getCallingInProvinceRecords()) {
            cost += (int)Math.ceil((i.getEndTime().getTime() - i.getStartTime().getTime()) * 1.0 / 60 / 1000) * 0.3;
        }
        for(CallRecord i : userRecords.getCallingInLandRecords()) {
            cost += (int)Math.ceil((i.getEndTime().getTime() - i.getStartTime().getTime()) * 1.0 / 60 / 1000) * 0.6;
        }
        for(CallRecord i : userRecords.getAnswerInLandRecords()) {
            cost += (int)Math.ceil((i.getEndTime().getTime() - i.getStartTime().getTime()) * 1.0 / 60 / 1000) * 0.3;
        }
        return cost;
    }

    @Override
    public double getMonthlyRent() {
        return monthlyRent;
    }

}


public class Main {
    
    private static UserData userData = new UserData();
    
    public static int isleap(int year) {
        if(year % 4 == 0 && year % 100 !=0 || year % 400 == 0) {
            return 1;
        }
        return 0;
    }
    
    public static boolean checkDate(int y,int m,int d) {
        int monthday[][] = {{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};
        if(y >= 0 && m <=12 && m >= 1 && d >=1 && d <= monthday[isleap(y)][m]) {
            return true;
        }
        return false;
    }

    public static boolean checkInputone(String a) {
        if(a.equals("")) {
            return false;
        }
        if(Pattern.matches("u-\\d{10,12} [0-1]",a)) {
            return true;
        }
        else if(Pattern.matches("t-\\d{1,12} \\d{1,12} \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]",a)) {
            String b[] = a.split(" ");
            String c[] = b[2].split("\\.");
            String d[] = b[4].split("\\.");
            for(int i = 0;i < 3;i ++) {
                if(c[i].charAt(0) == '0' || d[i].charAt(0) == '0') {
                    return false;
                }
            }
            if(checkDate(Integer.parseInt(c[0]),Integer.parseInt(c[1]),Integer.parseInt(c[2])) && checkDate(Integer.parseInt(d[0]),Integer.parseInt(d[1]),Integer.parseInt(d[2]))) {
                return true;
            }
        }
        else if(Pattern.matches("t-\\d{11,11} \\d{1,} \\d{1,12} \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]",a)) {
            String b[] = a.split(" ");
            String c[] = b[3].split("\\.");
            String d[] = b[5].split("\\.");
            for(int i = 0;i < 3;i ++) {
                if(c[i].charAt(0) == '0' || d[i].charAt(0) == '0') {
                    return false;
                }
            }
            if(checkDate(Integer.parseInt(c[0]),Integer.parseInt(c[1]),Integer.parseInt(c[2])) && checkDate(Integer.parseInt(d[0]),Integer.parseInt(d[1]),Integer.parseInt(d[2]))) {
                return true;
            }
        }
        else if(Pattern.matches("t-\\d{11,11} \\d{1,} \\d{11,11} \\d{1,} \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]",a)) {
            String b[] = a.split(" ");
            String c[] = b[4].split("\\.");
            String d[] = b[6].split("\\.");
            for(int i = 0;i < 3;i ++) {
                if(c[i].charAt(0) == '0' || d[i].charAt(0) == '0') {
                    return false;
                }
            }
            if(checkDate(Integer.parseInt(c[0]),Integer.parseInt(c[1]),Integer.parseInt(c[2])) && checkDate(Integer.parseInt(d[0]),Integer.parseInt(d[1]),Integer.parseInt(d[2]))) {
                return true;
            }
        }
        else if(Pattern.matches("t-\\d{1,12} \\d{11,11} \\d{1,} \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]",a)) {
            String b[] = a.split(" ");
            String c[] = b[3].split("\\.");
            String d[] = b[5].split("\\.");
            for(int i = 0;i < 3;i ++) {
                if(c[i].charAt(0) == '0' || d[i].charAt(0) == '0') {
                    return false;
                }
            }
            if(checkDate(Integer.parseInt(c[0]),Integer.parseInt(c[1]),Integer.parseInt(c[2])) && checkDate(Integer.parseInt(d[0]),Integer.parseInt(d[1]),Integer.parseInt(d[2]))) {
                return true;
            }
        }
        return false;
    }
    
    public static ArrayList checkInput(ArrayList a) {
        for(int i = 0;i < a.size();i ++) {
            if(!checkInputone(a.get(i))) {
                a.remove(i);
            }
        }
        return a;
    }
    
    public static ArrayList getInput() {
        ArrayList a = new ArrayList();
        Scanner in = new Scanner(System.in);
        String b = in.nextLine();
        while(!b.equals("end")) {
            a.add(b);
            if(in.hasNextLine()) {
                b = in.nextLine();
            }
        }
        return checkInput(a);
    }
    
    public static String ugetNumber(String a) {
        String b = "";
        for(int i = 2;i < a.length();i ++) {
            if(a.charAt(i) == ' ') {
                break;
            }
            else {
                b += a.charAt(i);
            }
        }
        return b;
    }
    
    public static String processDate(String a) {
        String b = "";
        for(int i = 0;i < a.length();i ++) {
            b += a.charAt(i);
            if(a.charAt(i) == '.') {
                for(int j = i + 1;j < a.length();j ++) {
                    if(a.charAt(j) == '.' || a.charAt(j) == ' ') {
                        if(j == i + 2) {
                            b += '0';
                        }
                        break;
                    }
                }
            }
        }
        return b;
    }
    
    public static void workone(String a) throws ParseException {
        if(a.charAt(0) == 'u') {
            if(a.charAt(a.length() - 1) == '0') {
                userData.addUser(new User(ugetNumber(a),100,new LandlinePhoneCharging(20)));
            }
            else if(a.charAt(a.length() - 1) == '1') {
                userData.addUser(new User(ugetNumber(a),100,new LandmobilePhoneCharging(15)));
            }
            else {
                System.out.println("Wrong");
                System.exit(0);
            }
        }
        else if(Pattern.matches("t-\\d{1,12} \\d{1,12} \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]",a)) {
            a = a.substring(2);
            String b[] = a.split(" ");
            SimpleDateFormat  sdf =new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
            Date startDate = sdf.parse(processDate(b[2] + " " + b[3]));
            Date endDate = sdf.parse(processDate(b[4] + " " + b[5]));
            if(userData.phoneNumberFindUser(b[0]) != null) {
                userData.phoneNumberFindUser(b[0]).getUserRecords().addCallingInCityRecords(new CallRecord(startDate,endDate,b[0].substring(0, 4),b[1].substring(0, 4)));
            }
            if(userData.phoneNumberFindUser(b[1]) != null) {
                userData.phoneNumberFindUser(b[1]).getUserRecords().addAnswerInCityRecords(new CallRecord(startDate,endDate,b[0].substring(0, 4),b[1].substring(0, 4)));
            }
        }
        else if(Pattern.matches("t-\\d{11,11} \\d{1,} \\d{1,12} \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]",a)) {
            a = a.substring(2);
            String b[] = a.split(" ");
            SimpleDateFormat  sdf =new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
            Date startDate = sdf.parse(processDate(b[3] + " " + b[4]));
            Date endDate = sdf.parse(processDate(b[5] + " " + b[6]));
            if(userData.phoneNumberFindUser(b[0]) != null) {
                if(b[1].equals("0791")) {
                    userData.phoneNumberFindUser(b[0]).getUserRecords().addCallingInCityRecords(new CallRecord(startDate,endDate,b[1],b[2].substring(0, 4)));
                }
                else if(Pattern.matches("(079[0-9])|0701",b[1])) {
                    userData.phoneNumberFindUser(b[0]).getUserRecords().addCallingInProvinceRecords(new CallRecord(startDate,endDate,b[1],b[2].substring(0, 4)));
                }
                else {
                    userData.phoneNumberFindUser(b[0]).getUserRecords().addCallingInLandRecords(new CallRecord(startDate,endDate,b[1],b[2].substring(0, 4)));
                }
            }
            if(userData.phoneNumberFindUser(b[2]) != null) {
                userData.phoneNumberFindUser(b[2]).getUserRecords().addAnswerInCityRecords(new CallRecord(startDate,endDate,b[1],b[2].substring(0, 4)));
            }
        }
        else if(Pattern.matches("t-\\d{11,11} \\d{1,} \\d{11,11} \\d{1,} \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]",a)) {
            a = a.substring(2);
            String b[] = a.split(" ");
            SimpleDateFormat  sdf =new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
            Date startDate = sdf.parse(processDate(b[4] + " " + b[5]));
            Date endDate = sdf.parse(processDate(b[6] + " " + b[7]));
            if(userData.phoneNumberFindUser(b[0]) != null) {
                if(b[1].equals("0791")) {
                    userData.phoneNumberFindUser(b[0]).getUserRecords().addCallingInCityRecords(new CallRecord(startDate,endDate,b[1],b[3]));
                }
                else if(Pattern.matches("(079[0-9])|0701",b[1])) {
                    userData.phoneNumberFindUser(b[0]).getUserRecords().addCallingInProvinceRecords(new CallRecord(startDate,endDate,b[1],b[3]));
                }
                else {
                    userData.phoneNumberFindUser(b[0]).getUserRecords().addCallingInLandRecords(new CallRecord(startDate,endDate,b[1],b[3]));
                }
            }
            if(userData.phoneNumberFindUser(b[2]) != null) {
                if(b[3].equals("0791")) {
                    userData.phoneNumberFindUser(b[2]).getUserRecords().addAnswerInCityRecords(new CallRecord(startDate,endDate,b[1],b[3]));
                }
                else if(Pattern.matches("(079[0-9])|0701",b[3])) {
                    userData.phoneNumberFindUser(b[2]).getUserRecords().addAnswerInProvinceRecords(new CallRecord(startDate,endDate,b[1],b[3]));
                }
                else {
                    userData.phoneNumberFindUser(b[2]).getUserRecords().addAnswerInLandRecords(new CallRecord(startDate,endDate,b[1],b[3]));
                }
            }
        }
        else if(Pattern.matches("t-\\d{1,12} \\d{11,11} \\d{1,} \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]",a)) {
            a = a.substring(2);
            String b[] = a.split(" ");
            SimpleDateFormat  sdf =new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
            Date startDate = sdf.parse(processDate(b[3] + " " + b[4]));
            Date endDate = sdf.parse(processDate(b[5] + " " + b[6]));
            if(userData.phoneNumberFindUser(b[0]) != null) {
                userData.phoneNumberFindUser(b[0]).getUserRecords().addCallingInCityRecords(new CallRecord(startDate,endDate,b[0].substring(0, 4),b[2]));
            }
            if(userData.phoneNumberFindUser(b[1]) != null) {
                if(b[2].equals("0791")) {
                    userData.phoneNumberFindUser(b[1]).getUserRecords().addAnswerInCityRecords(new CallRecord(startDate,endDate,b[0].substring(0, 4),b[2]));
                }
                else if(Pattern.matches("(079[0-9])|0701",b[2])) {
                    userData.phoneNumberFindUser(b[1]).getUserRecords().addAnswerInProvinceRecords(new CallRecord(startDate,endDate,b[0].substring(0, 4),b[2]));
                }
                else {
                    userData.phoneNumberFindUser(b[1]).getUserRecords().addAnswerInLandRecords(new CallRecord(startDate,endDate,b[0].substring(0, 4),b[2]));
                }
            }
        }
    }
    
    public static void work(ArrayList a) {
        for(String i : a) {
            try {
                workone(i);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    
    public static String getOutput(ArrayList a) {
        work(a);
        return userData.show();
    }
    
    public static void main(String[] args) {
        System.out.printf(getOutput(getInput()));
    }

}
例题二代码

在读了例题二后,我才理解了这个例题的意思,然后修改了workone方法对数据存储进行了修改。至于还有一个计费规则的类我没用,可能是因为我懒得用了。

例题三的代码如下。

  1 import java.util.Date;
  2 import java.util.ArrayList;
  3 import java.text.ParseException;
  4 import java.text.SimpleDateFormat;
  5 import java.util.Date;
  6 import java.util.Scanner;
  7 import java.util.regex.*;
  8 
  9 
 10 
 11 abstract class CallChargeRule extends ChargeRule{
 12 
 13     public abstract double calCost(ArrayList callRecords);
 14     
 15     public CallChargeRule() {
 16         // TODO Auto-generated constructor stub
 17     }
 18 
 19 }
 20 class CallRecord extends CommunicationRecord{
 21 
 22     private Date startTime;
 23     private Date endTime;
 24     private String callingAddressAreaCode;
 25     private String answerAddressAreaCode;
 26     
 27     public CallRecord(Date date,Date date2,String callingAddressAreaCode,String answerAddressAreaCode,String callingNumber,String answerNumber) {
 28         this.answerAddressAreaCode = answerAddressAreaCode;
 29         this.callingAddressAreaCode = callingAddressAreaCode;
 30         this.startTime = date;
 31         this.endTime = date2;
 32         this.setCallingNumber(callingNumber);
 33         this.setAnswerNumber(answerNumber);
 34     }
 35 
 36     public Date getStartTime() {
 37         return startTime;
 38     }
 39 
 40     public void setStartTime(Date startTime) {
 41         this.startTime = startTime;
 42     }
 43 
 44     public Date getEndTime() {
 45         return endTime;
 46     }
 47 
 48     public void setEndTime(Date endTime) {
 49         this.endTime = endTime;
 50     }
 51 
 52     public String getCallingAddressAreaCode() {
 53         return callingAddressAreaCode;
 54     }
 55 
 56     public void setCallingAddressAreaCode(String callingAddressAreaCode) {
 57         this.callingAddressAreaCode = callingAddressAreaCode;
 58     }
 59 
 60     public String getAnswerAddressAreaCode() {
 61         return answerAddressAreaCode;
 62     }
 63 
 64     public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
 65         this.answerAddressAreaCode = answerAddressAreaCode;
 66     }
 67 
 68 }
 69 abstract class ChargeMode {
 70 
 71     public ChargeMode() {
 72         
 73     }
 74     
 75     public abstract double calCost(UserRecords userRecords);
 76     
 77     public abstract double getMonthlyRent();
 78 
 79 }
 80 abstract class ChargeRule {
 81 
 82     public ChargeRule() {
 83         // TODO Auto-generated constructor stub
 84     }
 85 
 86 }
 87 abstract class CommunicationRecord {
 88 
 89     private String callingNumber;
 90     private String answerNumber;
 91     
 92     public String getCallingNumber() {
 93         return callingNumber;
 94     }
 95     public void setCallingNumber(String callingNumber) {
 96         this.callingNumber = callingNumber;
 97     }
 98     public String getAnswerNumber() {
 99         return answerNumber;
100     }
101     public void setAnswerNumber(String answerNumber) {
102         this.answerNumber = answerNumber;
103     }
104 
105 }
106 class LandlinePhoneCharging extends ChargeMode {
107 
108     private double monthlyRent;
109     
110     public LandlinePhoneCharging(double monthlyRent) {
111         this.monthlyRent = monthlyRent;
112     }
113 
114     public double calCost(UserRecords userRecords) {
115         // TODO Auto-generated method stub
116         double cost = 0;
117         for(CallRecord i : userRecords.getCallingInCityRecords()) {
118             if(i.getAnswerAddressAreaCode().equals(i.getCallingAddressAreaCode())) {
119                 cost += (int)Math.ceil((i.getEndTime().getTime() - i.getStartTime().getTime()) * 1.0 / 60 / 1000) * 0.1;
120             }
121             else if(Pattern.matches("(0701)|(079[0-9])",i.getAnswerAddressAreaCode())) {
122                 cost += (int)Math.ceil((i.getEndTime().getTime() - i.getStartTime().getTime()) * 1.0 / 60 / 1000) * 0.3;
123             }
124             else {
125                 cost += (int)Math.ceil((i.getEndTime().getTime() - i.getStartTime().getTime()) * 1.0 / 60 / 1000) * 0.6;
126             }
127         }
128         return cost;
129     }
130 
131     @Override
132     public double getMonthlyRent() {
133         // TODO Auto-generated method stub
134         return monthlyRent;
135     }
136 
137 }
138 class MessageRecord extends CommunicationRecord {
139 
140     private String message;
141     
142     public MessageRecord(String message,String callingNumber,String answerNumber) {
143         this.message = message;
144         this.setCallingNumber(callingNumber);
145         this.setAnswerNumber(answerNumber);
146     }
147 
148     public String getMessage() {
149         return message;
150     }
151 
152     public void setMessage(String message) {
153         this.message = message;
154     }
155 
156 }
157 class User {
158 
159     private UserRecords userRecords;
160     private double balance;
161     private ChargeMode chargeMode;
162     private String number;
163     
164     public User(String number,double money,ChargeMode chargeMode) {
165         userRecords = new UserRecords();
166         this.chargeMode = chargeMode;
167         this.balance = money;
168         this.number = number;
169     }
170     
171     public double calBalance() {
172         return balance - calCost() - chargeMode.getMonthlyRent();
173     }
174     
175     public double calCost() {
176         return chargeMode.calCost(userRecords);
177     }
178 
179     public UserRecords getUserRecords() {
180         return userRecords;
181     }
182 
183     public void setUserRecords(UserRecords userRecords) {
184         this.userRecords = userRecords;
185     }
186 
187     public double getBalance() {
188         return balance;
189     }
190 
191     public void setBalance(double balance) {
192         this.balance = balance;
193     }
194 
195     public ChargeMode getChargeMode() {
196         return chargeMode;
197     }
198 
199     public void setChargeMode(ChargeMode chargeMode) {
200         this.chargeMode = chargeMode;
201     }
202 
203     public String getNumber() {
204         return number;
205     }
206 
207     public void setNumber(String number) {
208         this.number = number;
209     }
210 
211 }
212 class UserData {
213 
214     private ArrayList users;
215     
216     public UserData() {
217         users = new ArrayList();
218     }
219     
220     public void addUser(User user) {
221         int x = 0;
222         for(int i = 0;i < users.size();i ++) {
223             if(user.getNumber().compareTo(users.get(i).getNumber()) < 0) {
224                 users.add(i, user);
225                 x = 1;
226                 break;
227             }
228             else if(user.getNumber().compareTo(users.get(i).getNumber()) == 0) {
229                 return;
230             }
231         }
232         if(x == 0) {
233             users.add(users.size(),user);
234         }
235     }
236     
237     public void removeUser(User user) {
238         users.remove(user);
239     }
240 
241     public ArrayList getUsers() {
242         return users;
243     }
244     
245     public String show() {
246         String a = "";
247         for(User i : users) {
248             a += ( i.getNumber() + " " + String.format("%.1f", i.calCost()) + " " + String.format("%.1f", i.calBalance()));
249             if(!i.getNumber().equals(users.get(users.size() - 1).getNumber())){
250                 a += "\n";
251             }
252         }
253         return a;
254     }
255     
256     public User phoneNumberFindUser(String number) {
257         for(User i : users) {
258             if(i.getNumber().equals(number)) {
259                 return i;
260             }
261         }
262         return null;
263     }
264 
265 }
266 class UserRecords {
267 
268     private ArrayList callingInCityRecords;
269     private ArrayList callingInProvinceRecords;
270     private ArrayList callingInLandRecords;
271     private ArrayList answerInCityRecords;
272     private ArrayList answerInProvinceRecords;
273     private ArrayList answerInLandRecords;
274     private ArrayList sendMessageRecords;
275     private ArrayList receiveMessageRecords;
276     
277     public UserRecords() {
278         callingInCityRecords = new ArrayList();
279         callingInCityRecords = new ArrayList();
280         callingInProvinceRecords = new ArrayList();
281         callingInLandRecords = new ArrayList();
282         answerInCityRecords = new ArrayList();
283         answerInProvinceRecords = new ArrayList();
284         answerInLandRecords = new ArrayList();
285         sendMessageRecords = new ArrayList();
286         receiveMessageRecords = new ArrayList();
287     }
288     
289     public void addCallingInCityRecords(CallRecord callRecord) {
290         this.callingInCityRecords.add(callRecord);
291     }
292     
293     public void addCallingInProvinceRecords(CallRecord callRecord) {
294         this.callingInProvinceRecords.add(callRecord);
295     }
296     
297     public void addCallingInLandRecords(CallRecord callRecord) {
298         this.callingInLandRecords.add(callRecord);
299     }
300     
301     public void addAnswerInCityRecords(CallRecord callRecord) {
302         this.answerInCityRecords.add(callRecord);
303     }
304     
305     public void addAnswerInProvinceRecords(CallRecord callRecord) {
306         this.answerInProvinceRecords.add(callRecord);
307     }
308     
309     public void addAnswerInLandRecords(CallRecord callRecord) {
310         this.answerInLandRecords.add(callRecord);
311     }
312     
313     public void addSendMessageRecords(MessageRecord messageRecord) {
314         this.sendMessageRecords.add(messageRecord);
315     }
316     
317     public void addReceiveMessageRecords(MessageRecord messageRecord) {
318         this.receiveMessageRecords.add(messageRecord);
319     }
320 
321     public ArrayList getCallingInCityRecords() {
322         return callingInCityRecords;
323     }
324 
325     public ArrayList getCallingInProvinceRecords() {
326         return callingInProvinceRecords;
327     }
328 
329     public ArrayList getCallingInLandRecords() {
330         return callingInLandRecords;
331     }
332 
333     public ArrayList getAnswerInCityRecords() {
334         return answerInCityRecords;
335     }
336 
337     public ArrayList getAnswerInProvinceRecords() {
338         return answerInProvinceRecords;
339     }
340 
341     public ArrayList getAnswerInLandRecords() {
342         return answerInLandRecords;
343     }
344 
345     public ArrayList getSendMessageRecords() {
346         return sendMessageRecords;
347     }
348 
349     public ArrayList getReceiveMessageRecords() {
350         return receiveMessageRecords;
351     }
352 
353 }
354 
355 class LandmobilePhoneCharging extends ChargeMode {
356 
357     private double monthlyRent;
358     
359     public LandmobilePhoneCharging(double monthlyRent) {
360         this.monthlyRent = monthlyRent;
361     }
362     
363     @Override
364     public double calCost(UserRecords userRecords) {
365         double cost = 0;
366         for(CallRecord i : userRecords.getCallingInCityRecords()) {
367             if(i.getAnswerAddressAreaCode().equals(i.getCallingAddressAreaCode())) {
368                 cost += (int)Math.ceil((i.getEndTime().getTime() - i.getStartTime().getTime()) * 1.0 / 60 / 1000) * 0.1;
369             }
370             else if(Pattern.matches("(0701)|(079[0-9])",i.getAnswerAddressAreaCode())) {
371                 cost += (int)Math.ceil((i.getEndTime().getTime() - i.getStartTime().getTime()) * 1.0 / 60 / 1000) * 0.2;
372             }
373             else {
374                 cost += (int)Math.ceil((i.getEndTime().getTime() - i.getStartTime().getTime()) * 1.0 / 60 / 1000) * 0.3;
375             }
376         }
377         for(CallRecord i : userRecords.getCallingInProvinceRecords()) {
378             cost += (int)Math.ceil((i.getEndTime().getTime() - i.getStartTime().getTime()) * 1.0 / 60 / 1000) * 0.3;
379         }
380         for(CallRecord i : userRecords.getCallingInLandRecords()) {
381             cost += (int)Math.ceil((i.getEndTime().getTime() - i.getStartTime().getTime()) * 1.0 / 60 / 1000) * 0.6;
382         }
383         for(CallRecord i : userRecords.getAnswerInLandRecords()) {
384             cost += (int)Math.ceil((i.getEndTime().getTime() - i.getStartTime().getTime()) * 1.0 / 60 / 1000) * 0.3;
385         }
386         int messageNum = 0;
387         for(MessageRecord i : userRecords.getSendMessageRecords()) {
388             messageNum += Math.ceil(i.getMessage().length() / 10.0);
389         }
390         if(messageNum <= 3) {
391             cost += messageNum*0.1;
392         }
393         else if(messageNum <= 5) {
394             cost += ((messageNum - 3) * 0.2 + 0.3);
395         }
396         else {
397             cost += ((messageNum - 5) * 0.3 + 0.7);
398         }
399         return cost;
400     }
401 
402     @Override
403     public double getMonthlyRent() {
404         return monthlyRent;
405     }
406 
407 }
408 
409 public class Main {
410     
411     private static UserData userData = new UserData();
412     
413     public static int isleap(int year) {
414         if(year % 4 == 0 && year % 100 !=0 || year % 400 == 0) {
415             return 1;
416         }
417         return 0;
418     }
419     
420     public static boolean checkDate(int y,int m,int d) {
421         int monthday[][] = {{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};
422         if(y >= 0 && m <=12 && m >= 1 && d >=1 && d <= monthday[isleap(y)][m]) {
423             return true;
424         }
425         return false;
426     }
427 
428     public static boolean checkInputone(String a) {
429         if(a.equals("")) {
430             return false;
431         }
432         if(Pattern.matches("u-\\d{11,12} [0-3]",a)) {
433             return true;
434         }
435         else if(Pattern.matches("t-\\d{11,12} \\d{11,12} \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]",a)) {
436             String b[] = a.split(" ");
437             String c[] = b[2].split("\\.");
438             String d[] = b[4].split("\\.");
439             for(int i = 0;i < 3;i ++) {
440                 if(c[i].charAt(0) == '0' || d[i].charAt(0) == '0') {
441                     return false;
442                 }
443             }
444             if(checkDate(Integer.parseInt(c[0]),Integer.parseInt(c[1]),Integer.parseInt(c[2])) && checkDate(Integer.parseInt(d[0]),Integer.parseInt(d[1]),Integer.parseInt(d[2]))) {
445                 return true;
446             }
447         }
448         else if(Pattern.matches("t-\\d{11,11} \\d{1,} \\d{11,12} \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]",a)) {
449             return true;
450         }
451         else if(Pattern.matches("t-\\d{11,11} \\d{1,} \\d{11,11} \\d{1,} \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]",a)) {
452             return true;
453         }
454         else if(Pattern.matches("t-\\d{11,12} \\d{11,11} \\d{1,} \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]",a)) {
455             return true;
456         }
457         else if(Pattern.matches("^m-1\\d{10,10} 1\\d{10,10} [\\s,.\\da-zA-Z]+$",a)){
458             return true;
459         }
460         return false;
461     }
462     
463     public static ArrayList checkInput(ArrayList a) {
464         for(int i = 0;i < a.size();i ++) {
465             if(!checkInputone(a.get(i))) {
466                 a.remove(i);
467             }
468         }
469         return a;
470     }
471     
472     public static ArrayList getInput() {
473         ArrayList a = new ArrayList();
474         Scanner in = new Scanner(System.in);
475         String b = in.nextLine();
476         while(!b.equals("end")) {
477             a.add(b);
478             if(in.hasNextLine()) {
479                 b = in.nextLine();
480             }
481         }
482         return checkInput(a);
483     }
484     
485     public static String ugetNumber(String a) {
486         String b = "";
487         for(int i = 2;i < a.length();i ++) {
488             if(a.charAt(i) == ' ') {
489                 break;
490             }
491             else {
492                 b += a.charAt(i);
493             }
494         }
495         return b;
496     }
497     
498     public static String processDate(String a) {
499         String b = "";
500         for(int i = 0;i < a.length();i ++) {
501             b += a.charAt(i);
502             if(a.charAt(i) == '.') {
503                 for(int j = i + 1;j < a.length();j ++) {
504                     if(a.charAt(j) == '.' || a.charAt(j) == ' ') {
505                         if(j == i + 2) {
506                             b += '0';
507                         }
508                         break;
509                     }
510                 }
511             }
512         }
513         return b;
514     }
515     
516     public static void workone(String a) throws ParseException {
517         if(a.charAt(0) == 'u') {
518             if(a.charAt(a.length() - 1) == '0') {
519                 userData.addUser(new User(ugetNumber(a),100,new LandlinePhoneCharging(20)));
520             }
521             else if(a.charAt(a.length() - 1) == '1') {
522                 userData.addUser(new User(ugetNumber(a),100,new LandmobilePhoneCharging(15)));
523             }
524             else if(a.charAt(a.length() - 1) == '3') {
525                 userData.addUser(new User(ugetNumber(a),100,new LandmobilePhoneCharging(0)));
526             }
527             else {
528                 System.out.println("Wrong");
529                 System.exit(0);
530             }
531         }
532         else if(Pattern.matches("t-\\d{11,12} \\d{11,12} \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]",a)) {
533             a = a.substring(2);
534             String b[] = a.split(" ");
535             SimpleDateFormat  sdf =new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
536             Date startDate = sdf.parse(processDate(b[2] + " " + b[3]));
537             Date endDate = sdf.parse(processDate(b[4] + " " + b[5]));
538             if(userData.phoneNumberFindUser(b[0]) != null) {
539                 userData.phoneNumberFindUser(b[0]).getUserRecords().addCallingInCityRecords(new CallRecord(startDate,endDate,b[0].substring(0, 4),b[1].substring(0, 4),b[0],b[1]));
540             }
541             if(userData.phoneNumberFindUser(b[1]) != null) {
542                 userData.phoneNumberFindUser(b[1]).getUserRecords().addAnswerInCityRecords(new CallRecord(startDate,endDate,b[0].substring(0, 4),b[1].substring(0, 4),b[0],b[1]));
543             }
544         }
545         else if(Pattern.matches("t-\\d{11,11} \\d{1,} \\d{11,12} \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]",a)) {
546             a = a.substring(2);
547             String b[] = a.split(" ");
548             SimpleDateFormat  sdf =new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
549             Date startDate = sdf.parse(processDate(b[3] + " " + b[4]));
550             Date endDate = sdf.parse(processDate(b[5] + " " + b[6]));
551             if(userData.phoneNumberFindUser(b[0]) != null) {
552                 if(b[1].equals("0791")) {
553                     userData.phoneNumberFindUser(b[0]).getUserRecords().addCallingInCityRecords(new CallRecord(startDate,endDate,b[1],b[2].substring(0, 4),b[0],b[2]));
554                 }
555                 else if(Pattern.matches("(079[0-9])|0701",b[1])) {
556                     userData.phoneNumberFindUser(b[0]).getUserRecords().addCallingInProvinceRecords(new CallRecord(startDate,endDate,b[1],b[2].substring(0, 4),b[0],b[2]));
557                 }
558                 else {
559                     userData.phoneNumberFindUser(b[0]).getUserRecords().addCallingInLandRecords(new CallRecord(startDate,endDate,b[1],b[2].substring(0, 4),b[0],b[2]));
560                 }
561             }
562             if(userData.phoneNumberFindUser(b[2]) != null) {
563                 userData.phoneNumberFindUser(b[2]).getUserRecords().addAnswerInCityRecords(new CallRecord(startDate,endDate,b[1],b[2].substring(0, 4),b[0],b[2]));
564             }
565         }
566         else if(Pattern.matches("t-\\d{11,11} \\d{1,} \\d{11,11} \\d{1,} \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]",a)) {
567             a = a.substring(2);
568             String b[] = a.split(" ");
569             SimpleDateFormat  sdf =new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
570             Date startDate = sdf.parse(processDate(b[4] + " " + b[5]));
571             Date endDate = sdf.parse(processDate(b[6] + " " + b[7]));
572             if(userData.phoneNumberFindUser(b[0]) != null) {
573                 if(b[1].equals("0791")) {
574                     userData.phoneNumberFindUser(b[0]).getUserRecords().addCallingInCityRecords(new CallRecord(startDate,endDate,b[1],b[3],b[0],b[2]));
575                 }
576                 else if(Pattern.matches("(079[0-9])|0701",b[1])) {
577                     userData.phoneNumberFindUser(b[0]).getUserRecords().addCallingInProvinceRecords(new CallRecord(startDate,endDate,b[1],b[3],b[0],b[2]));
578                 }
579                 else {
580                     userData.phoneNumberFindUser(b[0]).getUserRecords().addCallingInLandRecords(new CallRecord(startDate,endDate,b[1],b[3],b[0],b[2]));
581                 }
582             }
583             if(userData.phoneNumberFindUser(b[2]) != null) {
584                 if(b[3].equals("0791")) {
585                     userData.phoneNumberFindUser(b[2]).getUserRecords().addAnswerInCityRecords(new CallRecord(startDate,endDate,b[1],b[3],b[0],b[2]));
586                 }
587                 else if(Pattern.matches("(079[0-9])|0701",b[3])) {
588                     userData.phoneNumberFindUser(b[2]).getUserRecords().addAnswerInProvinceRecords(new CallRecord(startDate,endDate,b[1],b[3],b[0],b[2]));
589                 }
590                 else {
591                     userData.phoneNumberFindUser(b[2]).getUserRecords().addAnswerInLandRecords(new CallRecord(startDate,endDate,b[1],b[3],b[0],b[2]));
592                 }
593             }
594         }
595         else if(Pattern.matches("t-\\d{11,12} \\d{11,11} \\d{1,} \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9] \\d{1,}.\\d{1,2}.\\d{1,2} ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]",a)) {
596             a = a.substring(2);
597             String b[] = a.split(" ");
598             SimpleDateFormat  sdf =new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
599             Date startDate = sdf.parse(processDate(b[3] + " " + b[4]));
600             Date endDate = sdf.parse(processDate(b[5] + " " + b[6]));
601             if(userData.phoneNumberFindUser(b[0]) != null) {
602                 userData.phoneNumberFindUser(b[0]).getUserRecords().addCallingInCityRecords(new CallRecord(startDate,endDate,b[0].substring(0, 4),b[2],b[0],b[1]));
603             }
604             if(userData.phoneNumberFindUser(b[1]) != null) {
605                 if(b[2].equals("0791")) {
606                     userData.phoneNumberFindUser(b[1]).getUserRecords().addAnswerInCityRecords(new CallRecord(startDate,endDate,b[0].substring(0, 4),b[2],b[0],b[1]));
607                 }
608                 else if(Pattern.matches("(079[0-9])|0701",b[2])) {
609                     userData.phoneNumberFindUser(b[1]).getUserRecords().addAnswerInProvinceRecords(new CallRecord(startDate,endDate,b[0].substring(0, 4),b[2],b[0],b[1]));
610                 }
611                 else {
612                     userData.phoneNumberFindUser(b[1]).getUserRecords().addAnswerInLandRecords(new CallRecord(startDate,endDate,b[0].substring(0, 4),b[2],b[0],b[1]));
613                 }
614             }
615         }
616         else if(Pattern.matches("^m-1\\d{10,10} 1\\d{10,10} [\\s,.\\da-zA-Z]+$",a)) {
617             a = a.substring(2);
618             String b[] = new String[5];
619             b[0] = a.substring(0,11);
620             b[1] = a.substring(12,23);
621             b[3] = a.substring(24);
622             if(userData.phoneNumberFindUser(b[0]) != null) {
623                 userData.phoneNumberFindUser(b[0]).getUserRecords().addSendMessageRecords(new MessageRecord(b[3],b[0],b[1]));
624             }
625             if(userData.phoneNumberFindUser(b[1]) != null) {
626                 userData.phoneNumberFindUser(b[0]).getUserRecords().addReceiveMessageRecords(new MessageRecord(b[3],b[0],b[1]));
627             }
628         }
629     }
630     
631     public static void work(ArrayList a) {
632         for(String i : a) {
633             try {
634                 workone(i);
635             } catch (ParseException e) {
636                 // TODO Auto-generated catch block
637                 e.printStackTrace();
638             }
639         }
640     }
641     
642     public static String getOutput(ArrayList a) {
643         work(a);
644         return userData.show();
645     }
646     
647     public static void main(String[] args) {
648         System.out.printf(getOutput(getInput()));
649     }
650 
651 }
例题三代码

例题三的代码没有很大的新意。

总结:

这三个例题带给我一个新的体验,就是,我从代码运行报错发现了事情运行的逻辑错误,这就是面向对象很有意思的地方。

相关