这三次大作业相对于,前面的pta大作业算法上比较简单,但是后面三次大作业逻辑上比较难,但是经过后面三次的大作业的完成,知道了怎么面向对象,虽然要建立很多类,但是这样管理起来更方便,修改起来,或者添加功能也更加的方便,不用去大段的修改代码,也避免了大量的代码重复。同时,对于其他那些看代码的人也可以很好的看懂,也更直观。电信系列的大作业主要涉及了容器的知识,对信息的储存和便利,同时还考了容器内部排序,通过Collections函数进行排序,也通过对不同的用户进行不同的分类,放在不同的容器中。当然,其中也还涉及到子类、父类,分类,类的创建,类的管理,正则表达式等等。

  难度相对于前几次的大作业来说,这三次的大作业难度还是相对比较低的,因为老师需要捞我们,所以pta的难度出的不大,但是前两次没有拿到满分,主要是正则表达式出问题了,但是一直找不到问题出现在哪里。

  这三次大作业也涉及到了Data的应用,对于Data的应用对我来说当时是一片空白,但是我通过查找资料,来找到相应的方法。

  1 import java.text.SimpleDateFormat;
  2 import java.util.ArrayList;
  3 import java.util.Collection;
  4 import java.util.Iterator;
  5 import java.util.Scanner;
  6 import java.util.TreeMap;
  7 
  8 public class Main {
  9     public static void main(String[] args) {        
 10         Scanner input = new Scanner(System.in);
 11         String str = input.nextLine();
 12         ChargeMode chargeMode = null;
 13         ArrayList<CallRecord> callRecords=new ArrayList<>();
 14         MessageRecord call =new MessageRecord();
 15         ArrayList<MessageRecord> messageRecords=new ArrayList<>();
 16         ArrayList<User> users =new ArrayList<>();
 17         while (!str.equals("end")) 
 18         {
 19             if(PanDuan(str))
 20             {
 21                 String[] str1 = str.split("-");
 22                 if(str1[0].equals("u"))
 23                 {
 24                     String[] str2=str1[1].split(" ");
 25                     if(str2[1].equals("3"))
 26                     {
 27                         chargeMode = new LandlinePhoneCharging();
 28                     }
 29                     int IsOrNo=0;
 30                     for(int i=0;i<users.size();i++)
 31                     {
 32                         if(users.get(i).getNumber().equals(str2[0]))
 33                         {
 34                             IsOrNo=1;
 35                         }
 36                     }
 37                     if(IsOrNo==0)
 38                     {
 39                         User user = new User();
 40                         user.setNumber(str2[0]);
 41                         user.setChargeMode(chargeMode);
 42                         users.add(user);
 43                     }
 44                 }
 45                 if(str1[0].equals("m"))
 46                 {
 47                     MessageRecord messageRecord = new MessageRecord();
 48                     str = str.substring(2);
 49                     String[] str3 = str.split(" ",3);
 50                     call.setMessage(str3[2]);
 51                     messageRecord.setCallingNumber(str3[0]);
 52                     messageRecord.setAnswerNumber(str3[1]);
 53                     messageRecords.add(messageRecord);
 54                 }
 55             }
 56             str = input.nextLine();
 57         }
 58         for (int i = 0; i < users.size(); i++) 
 59         {
 60             UserRecords userRecords = new UserRecords();
 61             for (int j = 0; j < messageRecords.size(); j++) 
 62             {
 63                 if (users.get(i).getNumber().equals(messageRecords.get(j).getCallingNumber())) 
 64                 {
 65                     userRecords.addSendMessageRecords(messageRecords.get(j));
 66                 }
 67             }
 68             users.get(i).setUserRecords(userRecords);
 69         }
 70         TreeMap<String, User> tree =new TreeMap<>();
 71         for(User user:users)
 72         {
 73             tree.put(user.getNumber(), user);
 74         }
 75         Iterator<User> iterator = tree.values().iterator();
 76         while (iterator.hasNext())
 77         {
 78             User user = iterator.next();
 79             System.out.print(user.getNumber());
 80             System.out.printf(" %.1f %.1f\n", chargeMode.calCost(call.getMessage()), 100-chargeMode.calCost(call.getMessage()));
 81         }
 82     }
 83     public static boolean PanDuan(String str) {
 84         if(str.matches("u-1\\d{10} [3]")) 
 85         {
 86             return true;
 87         }
 88         else 
 89         {
 90             if(str.matches("m-1\\d{10} 1\\d{10} ([a-zA-Z]|\\.|,| |\\d)*"))
 91             {
 92                 return true;
 93             }  
 94             else
 95             {
 96                 return false;
 97             } 
 98         }
 99             
100     }
101 }
102 abstract class CallChargeRule extends ChargeRule {
103     public abstract double calCost (ArrayList<CallRecord> callRecords);
104 }class CallRecord extends CommunicationRecord{
105     private String callingAddressAreaCode;
106     private String answerAddressAreaCode;
107 
108     public String getCallingAddressAreaCode() {
109         return callingAddressAreaCode;
110     }
111     public void setCallingAddressAreaCode(String callingAddressAreaCode) {
112         this.callingAddressAreaCode = callingAddressAreaCode;
113     }
114     public String getAnswerAddressAreaCode() {
115         return answerAddressAreaCode;
116     }
117     public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
118         this.answerAddressAreaCode = answerAddressAreaCode;
119     }
120     
121 }
122 abstract class ChargeMode {
123     private ArrayList<ChargeRule> chargeRules = new ArrayList<>();
124     public abstract double calCost(String string);
125     public ArrayList<ChargeRule> getChargeRules() {
126         return chargeRules;
127     }
128     public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
129         this.chargeRules = chargeRules;
130     }
131     public abstract double getMonthlyRent();//返回月租
132 
133 }
134 abstract class ChargeRule {
135     
136 }
137 abstract class CommunicationRecord {
138     protected String callingNumber;
139     protected String answerNumber;
140     public String getCallingNumber() {
141         return callingNumber;
142     }
143     public void setCallingNumber(String callingNumber) {
144         this.callingNumber = callingNumber;
145     }
146     public String getAnswerNumber() {
147         return answerNumber;
148     }
149     public void setAnswerNumber(String answerNumber) {
150         this.answerNumber = answerNumber;
151     }
152     
153 }
154 class LandlinePhoneCharging extends ChargeMode {
155     private double monthlyRent=0.0;
156     @Override
157     public double calCost (String string) {
158         double calcost=0;
159         int n=string.length()/10;
160         double m=(double)(string.length()/10.0);
161         if(m!=n)
162         {
163             n=n+1;
164         }
165         
166 
167         if(n > 5)
168         {
169             calcost = (n - 5) * 0.3 + 0.4 + 0.3;
170         }else if(n > 3)
171         {
172             calcost = (n - 3) * 0.2 + 0.3;
173         }else calcost = 0.1 * n;
174         return calcost;
175         
176  
177     }
178     
179     @Override
180     public double getMonthlyRent() {
181         return monthlyRent;        
182     }
183 }
184 class LandPhoneInCityRule extends CallChargeRule {
185     @Override
186     public double calCost(ArrayList<CallRecord> callRecords) {
187         
188         return 0;
189     }
190 
191 }
192 class LandPhoneInlandRule extends CallChargeRule {
193 
194     @Override
195     public double calCost(ArrayList<CallRecord> callRecords) {
196         // TODO Auto-generated method stub
197         return 0;
198     }
199 
200 }
201 class LandPhoneInProvinceRule extends CallChargeRule{
202 
203     @Override
204     public double calCost(ArrayList<CallRecord> callRecords) {
205         // TODO Auto-generated method stub
206         return 0;
207     }
208 
209 }
210 abstract class MessageChargeRule {
211     public abstract double calCost (ArrayList<MessageRecord> messageRecords);
212 }
213 class MessageRecord extends CommunicationRecord {
214     private String message;
215 
216     public String getMessage() {
217         return message;
218     }
219 
220     public void setMessage(String message) {
221         this.message = message;
222     }
223     
224 }class SendMessageRule extends MessageChargeRule{
225 
226     @Override
227     public double calCost(ArrayList<MessageRecord> messageRecords) {
228         // TODO Auto-generated method stub
229         return 0;
230     }
231 
232 }class User {
233     private UserRecords userRecords = new UserRecords();//用户记录类,保存用户各种通话、短信的记录
234     private double balance = 100;//余额
235     private ChargeMode chargeMode;//计费方式
236     private String number;//号码
237     public double calBalance() {
238         return 0;
239         
240     }
241     
242     public UserRecords getUserRecords() {
243         return userRecords;
244     }
245     public void setUserRecords(UserRecords userRecords) {
246         this.userRecords = userRecords;
247     }
248     public double getBalance() {
249         return balance;
250     }
251     public void setBalance(double balance) {
252         this.balance = balance;
253     }
254     public ChargeMode getChargeMode() {
255         return chargeMode;
256     }
257     public void setChargeMode(ChargeMode chargeMode) {
258         this.chargeMode = chargeMode;
259     }
260     public String getNumber() {
261         return number;
262     }
263     public void setNumber(String number) {
264         this.number = number;
265     }
266     
267     
268 }
269 class UserRecords {
270     private ArrayList<CallRecord> callinglnCityRecords = new ArrayList<CallRecord>();
271     private ArrayList<CallRecord> callinglnProvinceRecords = new ArrayList<CallRecord>();
272     private    ArrayList<CallRecord> callinglnLandRecords = new ArrayList<CallRecord>();
273     private ArrayList<CallRecord> answerlnCityRecords = new ArrayList<CallRecord>();
274     private    ArrayList<CallRecord> answerlnProvinceRecords = new ArrayList<CallRecord>();
275     private    ArrayList<CallRecord> answerlnLandRecords = new ArrayList<CallRecord>();
276     private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();
277     private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();
278     public ArrayList<CallRecord> getCallinglnCityRecords() {
279         return callinglnCityRecords;
280     }
281     public ArrayList<CallRecord> getCallinglnProvinceRecords() {
282         return callinglnProvinceRecords;
283     }
284     public ArrayList<CallRecord> getCallinglnLandRecords() {
285         return callinglnLandRecords;
286     }
287     public ArrayList<CallRecord> getAnswerlnCityRecords() {
288         return answerlnCityRecords;
289     }
290     public ArrayList<CallRecord> getAnswerlnProvinceRecords() {
291         return answerlnProvinceRecords;
292     }
293     public ArrayList<CallRecord> getAnswerlnLandRecords() {
294         return answerlnLandRecords;
295     }
296     public ArrayList<MessageRecord> getSenfMessageRecords() {
297         return sendMessageRecords;
298     }
299     public ArrayList<MessageRecord> getReceiveMessageRecords() {
300         return receiveMessageRecords;
301     }
302     public void addCallinglnCityRecords(CallRecord callRecord)
303     {
304         callinglnCityRecords.add(callRecord);
305     }
306     public void addCallinglnProvinceRecords(CallRecord callRecord)
307     {
308         callinglnProvinceRecords.add(callRecord);
309     }
310     public void addCallinglnLandRecords(CallRecord callRecord)
311     {
312         callinglnLandRecords.add(callRecord);
313     }
314     public void addAnswerlnCityRecords(CallRecord answerRecord)
315     {
316         answerlnCityRecords.add(answerRecord);
317     }
318     public void addAnswerlnProvinceRecords(CallRecord answerRecord)
319     {
320         answerlnProvinceRecords.add(answerRecord);
321     }
322     public void addAnswerlnLandRecords(CallRecord answerRecord)
323     {
324         answerlnLandRecords.add(answerRecord);
325     }
326     public void addSendMessageRecords(MessageRecord sendMseeageRecord)
327     {
328         sendMessageRecords.add(sendMseeageRecord);
329     }
330     public void addReceiveMessageRecords(MessageRecord receiveMessageRecords)
331     {
332         this.receiveMessageRecords.add(receiveMessageRecords);
333     }
334 }

 

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeMap;

public class Main {
    public static void main(String[] args) {        
        Scanner input = new Scanner(System.in);
        String str = input.nextLine();
        ChargeMode chargeMode = null;
        ArrayList<CallRecord> callRecords=new ArrayList<>();
        MessageRecord call =new MessageRecord();
        ArrayList<MessageRecord> messageRecords=new ArrayList<>();
        ArrayList<User> users =new ArrayList<>();
        while (!str.equals("end")) 
        {
            if(PanDuan(str))
            {
                String[] str1 = str.split("-");
                if(str1[0].equals("u"))
                {
                    String[] str2=str1[1].split(" ");
                    if(str2[1].equals("3"))
                    {
                        chargeMode = new LandlinePhoneCharging();
                    }
                    int IsOrNo=0;
                    for(int i=0;i<users.size();i++)
                    {
                        if(users.get(i).getNumber().equals(str2[0]))
                        {
                            IsOrNo=1;
                        }
                    }
                    if(IsOrNo==0)
                    {
                        User user = new User();
                        user.setNumber(str2[0]);
                        user.setChargeMode(chargeMode);
                        users.add(user);
                    }
                }
                if(str1[0].equals("m"))
                {
                    MessageRecord messageRecord = new MessageRecord();
                    str = str.substring(2);
                    String[] str3 = str.split(" ",3);
                    call.setMessage(str3[2]);
                    messageRecord.setCallingNumber(str3[0]);
                    messageRecord.setAnswerNumber(str3[1]);
                    messageRecords.add(messageRecord);
                }
            }
            str = input.nextLine();
        }
        for (int i = 0; i < users.size(); i++) 
        {
            UserRecords userRecords = new UserRecords();
            for (int j = 0; j < messageRecords.size(); j++) 
            {
                if (users.get(i).getNumber().equals(messageRecords.get(j).getCallingNumber())) 
                {
                    userRecords.addSendMessageRecords(messageRecords.get(j));
                }
            }
            users.get(i).setUserRecords(userRecords);
        }
        TreeMap<String, User> tree =new TreeMap<>();
        for(User user:users)
        {
            tree.put(user.getNumber(), user);
        }
        Iterator<User> iterator = tree.values().iterator();
        while (iterator.hasNext())
        {
            User user = iterator.next();
            System.out.print(user.getNumber());
            System.out.printf(" %.1f %.1f\n", chargeMode.calCost(call.getMessage()), 100-chargeMode.calCost(call.getMessage()));
        }
    }
    public static boolean PanDuan(String str) {
        if(str.matches("u-1\\d{10} [3]")) 
        {
            return true;
        }
        else 
        {
            if(str.matches("m-1\\d{10} 1\\d{10} ([a-zA-Z]|\\.|,| |\\d)*"))
            {
                return true;
            }  
            else
            {
                return false;
            } 
        }
            
    }
}
abstract class CallChargeRule extends ChargeRule {
    public abstract double calCost (ArrayList<CallRecord> callRecords);
}class CallRecord extends CommunicationRecord{
    private String callingAddressAreaCode;
    private String answerAddressAreaCode;

    public String getCallingAddressAreaCode() {
        return callingAddressAreaCode;
    }
    public void setCallingAddressAreaCode(String callingAddressAreaCode) {
        this.callingAddressAreaCode = callingAddressAreaCode;
    }
    public String getAnswerAddressAreaCode() {
        return answerAddressAreaCode;
    }
    public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
        this.answerAddressAreaCode = answerAddressAreaCode;
    }
    
}
abstract class ChargeMode {
    private ArrayList<ChargeRule> chargeRules = new ArrayList<>();
    public abstract double calCost(String string);
    public ArrayList<ChargeRule> getChargeRules() {
        return chargeRules;
    }
    public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
        this.chargeRules = chargeRules;
    }
    public abstract double getMonthlyRent();//返回月租

}
abstract class ChargeRule {
    
}
abstract class CommunicationRecord {
    protected String callingNumber;
    protected 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=0.0;
    @Override
    public double calCost (String string) {
        double calcost=0;
        int n=string.length()/10;
        double m=(double)(string.length()/10.0);
        if(m!=n)
        {
            n=n+1;
        }
        

        if(n > 5)
        {
            calcost = (n - 5) * 0.3 + 0.4 + 0.3;
        }else if(n > 3)
        {
            calcost = (n - 3) * 0.2 + 0.3;
        }else calcost = 0.1 * n;
        return calcost;
        
 
    }
    
    @Override
    public double getMonthlyRent() {
        return monthlyRent;        
    }
}
class LandPhoneInCityRule extends CallChargeRule {
    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        
        return 0;
    }

}
class LandPhoneInlandRule extends CallChargeRule {

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

}
class LandPhoneInProvinceRule extends CallChargeRule{

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

}
abstract class MessageChargeRule {
    public abstract double calCost (ArrayList<MessageRecord> messageRecords);
}
class MessageRecord extends CommunicationRecord {
    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
    
}class SendMessageRule extends MessageChargeRule{

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

}class User {
    private UserRecords userRecords = new UserRecords();//用户记录类,保存用户各种通话、短信的记录
    private double balance = 100;//余额
    private ChargeMode chargeMode;//计费方式
    private String number;//号码
    public double calBalance() {
        return 0;
        
    }
    
    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 UserRecords {
    private ArrayList<CallRecord> callinglnCityRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> callinglnProvinceRecords = new ArrayList<CallRecord>();
    private    ArrayList<CallRecord> callinglnLandRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerlnCityRecords = new ArrayList<CallRecord>();
    private    ArrayList<CallRecord> answerlnProvinceRecords = new ArrayList<CallRecord>();
    private    ArrayList<CallRecord> answerlnLandRecords = new ArrayList<CallRecord>();
    private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();
    private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();
    public ArrayList<CallRecord> getCallinglnCityRecords() {
        return callinglnCityRecords;
    }
    public ArrayList<CallRecord> getCallinglnProvinceRecords() {
        return callinglnProvinceRecords;
    }
    public ArrayList<CallRecord> getCallinglnLandRecords() {
        return callinglnLandRecords;
    }
    public ArrayList<CallRecord> getAnswerlnCityRecords() {
        return answerlnCityRecords;
    }
    public ArrayList<CallRecord> getAnswerlnProvinceRecords() {
        return answerlnProvinceRecords;
    }
    public ArrayList<CallRecord> getAnswerlnLandRecords() {
        return answerlnLandRecords;
    }
    public ArrayList<MessageRecord> getSenfMessageRecords() {
        return sendMessageRecords;
    }
    public ArrayList<MessageRecord> getReceiveMessageRecords() {
        return receiveMessageRecords;
    }
    public void addCallinglnCityRecords(CallRecord callRecord)
    {
        callinglnCityRecords.add(callRecord);
    }
    public void addCallinglnProvinceRecords(CallRecord callRecord)
    {
        callinglnProvinceRecords.add(callRecord);
    }
    public void addCallinglnLandRecords(CallRecord callRecord)
    {
        callinglnLandRecords.add(callRecord);
    }
    public void addAnswerlnCityRecords(CallRecord answerRecord)
    {
        answerlnCityRecords.add(answerRecord);
    }
    public void addAnswerlnProvinceRecords(CallRecord answerRecord)
    {
        answerlnProvinceRecords.add(answerRecord);
    }
    public void addAnswerlnLandRecords(CallRecord answerRecord)
    {
        answerlnLandRecords.add(answerRecord);
    }
    public void addSendMessageRecords(MessageRecord sendMseeageRecord)
    {
        sendMessageRecords.add(sendMseeageRecord);
    }
    public void addReceiveMessageRecords(MessageRecord receiveMessageRecords)
    {
        this.receiveMessageRecords.add(receiveMessageRecords);
    }
}

 

版权声明:本文为zwx1473785795原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/zwx1473785795/p/16367663.html