BLOG-3

BLOG近期总结
一:前言
对于近期的学习:主要是进行了pta的作业的学习,由于近期有同学反应java的作业太多,所以我们的老师将pta后三次题目集的难度大大降低了,第七次题目集是因为要用到之前的那些图形有关的代码,而之前的图形题目因为未完全做出,而导致了第七次pta作业的很多问题,但是最后三次的pta作业由于老师大大的降低了其难度,所以我们的分数普遍都更高一点,也就导致了这几次pta作业的题量、难度等都大大的降低了,而这次blog的主要是就是分析八九十三次题目集中的电信资费题目。
 
二:设计与分析
7-1 电信计费系列1-座机计费
源码如下:
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;

public class Main{
	public static void main(String []args) throws ParseException {					
		Scanner input = new Scanner(System.in);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ArrayList<User> arr = new ArrayList<>();
        int flag = 0;
        String sc = input.nextLine();
        String[] a ;
        while (!sc.equals("end")){
            if(sc.matches("[u][-](0791)[0-9]{7,8}[ ][0]")){
                a = sc.substring(2,sc.length()).split("\\s");
                
                for(int i = 0;i < arr.size();i++){
                    if(Objects.equals(arr.get(i).getNumber(), a[0])) {
                        flag = 1;
                    }
                }
                if(flag == 0) {
	                arr.add(new User(a[0]));
	                flag = 0;
                }
            }
            if(sc.matches("t-(\\d){11,12}\\s(\\d){10,12}\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]"
            		+ "|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])"
            		+ "|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))"
            		+ "|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))"
            		+ "|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d")){
                a = sc.substring(2,sc.length()).split("\\s");
                for(int i=0;i< arr.size();i++){
                    if(arr.get(i).getNumber().equals(a[0])){
                        if(a[1].startsWith("0791")){
                            arr.get(i).userRecord.addCallingInCityRecords(new CallRecord(a[2]+" "+a[3],a[4]+" "+a[5]));
                            arr.get(i).setUserRecord(arr.get(i).userRecord);
                        }
                        else if(a[1].substring(0,4).matches("(079\\d|0701)")){
                            arr.get(i).userRecord.addCallingInProvinceRecords(new CallRecord(a[2]+" "+a[3],a[4]+" "+a[5]));
                            arr.get(i).setUserRecord(arr.get(i).userRecord);
                        }
                        else{
                            arr.get(i).userRecord.addCallingInLandRecords(new CallRecord(a[2]+" "+a[3],a[4]+" "+a[5]));
                            arr.get(i).setUserRecord(arr.get(i).userRecord);
                        }
                    }
                }

            }
            sc = input.nextLine();
        }
        
        List<User> list = new ArrayList<>(arr);
        Collections.sort(list);
        
        for (User e : list){
            System.out.print(e.getNumber()+" ");
            System.out.print(new DecimalFormat("0.0#").format(e.calCost()));
            System.out.print(" ");
            System.out.print(new DecimalFormat("0.0#").format(e.calBalance()));
            System.out.println();
        }


	}
}

class User implements Comparable<User> {
    UserRecords userRecord = new UserRecords();
    private double balance = 100;
    ChargeMode chargeMode;
    private String number;
    
   public User(){

   }
   
   public void setBalance(double balance) {
	this.balance = balance;
   }

   public User(String number) {
       this.number = number;
   }

   public double calBalance(){
        return balance - calCost() - 20;
    }
   
    public double calCost(){
        LandPhoneInCityRule landPhoneInCityRule = new LandPhoneInCityRule();
        LandPhoneInIandRule landPhoneInIandRule = new LandPhoneInIandRule();
        LandPhoneInProvinceRule landPhoneInProvinceRule = new LandPhoneInProvinceRule();
        return landPhoneInIandRule.calCost(userRecord.getCallingInLandRecords())
        		+landPhoneInCityRule.calCost(userRecord.getCallingInCityRecords())
        		+landPhoneInProvinceRule.calCost(userRecord.getCallingInProvinceRecords());
    }
    
    public UserRecords getUserRecord(){
        return userRecord;
    }
    
    public void setUserRecord(UserRecords userRecord){
        this.userRecord = userRecord;
    }
    
    public double getBalance(){
        return balance;
    }
    
    public ChargeMode getChargeMode(){
        return chargeMode;
    }
    
    public void setChargeMode(ChargeMode chargeMode){
        this.chargeMode = chargeMode;
    }
    
    public String getNumber(){
        return number;
    }
    
    public void setNumber(String number){
        this.number = number;
    }
    
   public int compareTo(User o) {
       return this.getNumber().compareTo(o.getNumber());
   }

}

class UserRecords{
   ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();
   ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();
   ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();
   ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();
   ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();
   ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();
   ArrayList<MessageRecord> sendMessageRecord = new ArrayList<MessageRecord>();
   ArrayList<MessageRecord> receiveMessageRecord = new ArrayList<MessageRecord>();
   
   public void addCallingInCityRecords(CallRecord callRecord){
       callingInCityRecords.add(callRecord);
   }
   
   public void addCallingInProvinceRecords(CallRecord callRecord){
       callingInProvinceRecords.add(callRecord);
   }
   
   public void addCallingInLandRecords(CallRecord callRecord){
       callingInLandRecords.add(callRecord);
   }
   
   public void addAnswerInCityRecords(CallRecord callRecord){
       answerInCityRecords.add(callRecord);
   }
   
   public void addAnswerInProvinceRecords(CallRecord callRecord){
       answerInProvinceRecords.add(callRecord);
   }
   
   public void addAnswerInLandRecords(CallRecord callRecord){
       answerInLandRecords.add(callRecord);
   }
   
   public void addSendMessageRecord(MessageRecord sendMessageRecord){
       this.sendMessageRecord.add(sendMessageRecord);
   }
   
   public void addReceiveMessageRecord(MessageRecord receiveMessageRecord){
       this.receiveMessageRecord.add(receiveMessageRecord);
   }
   
   public ArrayList<MessageRecord> getSendMessageRecord(){
       return sendMessageRecord;
   }
   
   public ArrayList<MessageRecord> getReceiveMessageRecord(){
       return receiveMessageRecord;
   }
   
   public ArrayList<CallRecord> getCallingInCityRecords(){
       return callingInCityRecords;
   }
   
   public ArrayList<CallRecord> getCallingInProvinceRecords(){
       return callingInProvinceRecords;
   }
   
   public ArrayList<CallRecord> getCallingInLandRecords(){
       return callingInLandRecords;
   }
   
   public ArrayList<CallRecord> getAnswerInCityRecords(){
       return answerInCityRecords;
   }
   
   public ArrayList<CallRecord> getAnswerInProvinceRecords(){
       return answerInProvinceRecords;
   }
   
   public ArrayList<CallRecord> getAnswerInLandRecords(){
       return answerInLandRecords;
   }


}

abstract class ChargeMode{
	   ArrayList<ChargeRule> a = new ArrayList<ChargeRule>();
	   
	   public ArrayList<ChargeRule> getChargeRule(){
	       return a;
	   }
	   
	   public void setChargeRule(ArrayList<ChargeRule> chargeRule){
	       this.a = a;
	   }
	   
	   public double getMonthlyRent(){
	       return 20;
	   }
	   
	   public double calCost(UserRecords userRecords){
	       return 0;
	   }
	   
	}

class LandlinePhoneCharging extends ChargeMode{
   private double monthlyRent = 20;

   public double calCost(UserRecords userRecords){
       return monthlyRent * 1;
   }
   
   public double getMonthlyRent(){
       return monthlyRent;
   }
}

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 CallRecord extends ChargeRule{
   private Date startTime;
   private Date endTime;
   private String callingAddressAreaCode;
   private String AnswerAddressAreaCode;

   public CallRecord(String startTime,String endTime) throws ParseException {
       this.startTime =  new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").parse(startTime);
       this.endTime = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").parse(endTime);
   }

   public Date getStartTime(){
       return startTime;
   }
   
   public void setStartTime(Date startTime){
       this.startTime = startTime;
   }
   
   public Date getEndTime(){
       return endTime;
   }
   
   public void setEndTime(Date endTime){
       this.endTime = endTime;
   }
   
   public String getCallingAddressAreaCode(){
       return callingAddressAreaCode;
   }
   
   public void setCallingAddressAreaCode(String callingAddressAreaCode){
       this.callingAddressAreaCode = callingAddressAreaCode;
   }

   public String getAnswerAddressAreaCode() {
       return AnswerAddressAreaCode;
   }

   public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
       this.AnswerAddressAreaCode = answerAddressAreaCode;
   }
   
}

class MessageRecord extends ChargeRule{
   private String message;

   public String getMessage() {
       return message;
   }

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

abstract class ChargeRule{

}

abstract class CallChargeRule extends ChargeRule{
   public double calCost(ArrayList<CallRecord>callRecords){
       return 0;
   }
}

class LandPhoneInCityRule extends CallChargeRule{
   public double calCost(ArrayList<CallRecord> callRecords){
       double cost = 0;
       double  minute;
       for (CallRecord e: callRecords){
           minute = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime()) / 60000);
           cost =  cost + 0.1 * minute;
       }
       return cost;
   }
}

class LandPhoneInProvinceRule extends CallChargeRule{
	   public double calCost(ArrayList<CallRecord> callRecords){
	       double cost = 0;
	       double  minute;
	       for (CallRecord e: callRecords){
	           minute = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime()) / 60000);
	           cost =  cost + 0.3 * minute;
	       }
	       return cost;
	    }
}   

class LandPhoneInIandRule extends CallChargeRule{
   public double calCost(ArrayList<CallRecord> callRecords){
       double cost = 0;
       double  minute;
       for (CallRecord e: callRecords){
           minute = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime()) /  60000);
           cost =  cost + 0.6 * minute;
       }
       return cost;
   }
}

  类图如下:

 

 题目分析:

本道题目由于老师已经给出了类图,所以第一步的事情就是将所有给出的类给补全,补全之后,在去主函数中,分别利用正则表达式去判断每一段输入的字符串,之后进行容器的添加,计算容器中的花费金额。除此之外,本道题目由于采用的是多个类之间的关系,所以要将每一个类与类之间的关系理顺。

踩坑心得:本道题目的正则表达式是一个非常有难度的地方,由于本人非常菜鸡所以询问了同学。

7-2 多态测试

源码如下:

import java.util.ArrayList;
import java.util.Scanner;
public class Main {
	
	public static void main(String[] args) {
		ArrayList<Container> a = new ArrayList<Container>();
		cube cu;
		cylinder cy;
		Scanner input = new Scanner(System.in);
		double count = input.nextDouble();
		for(int i = 0 ; i < count; i++) {
			
			String sc = input.next();
			if(sc.equals("cube")) {
				cu = new cube(input.nextDouble());
				a.add(cu);
			}
			if(sc.equals("cylinder")) {
				cy = new cylinder(input.nextDouble(),input.nextDouble());
				a.add(cy);
			}
		}
		double s =  Container.sumofArea(a.toArray(new Container[a.size()]));
		double v = Container.sumofVolume(a.toArray(new Container[a.size()]));
		System.out.printf("%.2f\n",s);
		System.out.printf("%.2f",v);
		
	}
}

interface Container {
	public static final double pi=3.1415926;
	public abstract double area();
	public abstract double volume();
	static double sumofArea(Container c[]) {
		double s = 0;
		for(int i = 0 ; i < c.length ; i++) {
			s = s + c[i].area();
		}	
		return s;
	}
	
	static double sumofVolume(Container c[]) {
		double v = 0;
		for(int i = 0 ; i < c.length ; i++) {
			v = v + c[i].volume();
		}
		return v;
	}

}
class cube implements Container {
	private double a;
	
	public cube(double a) {
		super();
		this.a = a;
	}

	@Override
	public double area() {
		double s = 0;
		s = 6 * a * a;
		// TODO Auto-generated method stub
		return s;
	}

	@Override
	public double volume() {
		double v = 0;
		v = a * a * a;
		// TODO Auto-generated method stub
		return v;
	}
	
	

}
class cylinder implements Container {
	private double r;
	private double h;

	public cylinder(double r, double h) {
		super();
		this.r = r;
		this.h = h;
	}

	@Override
	public double area() {
		double s = 0;
		s = 2 * pi * r * h + pi * r * r * 2;
		// TODO Auto-generated method stub
		return s;
	}

	@Override
	public double volume() {
		double v = 0;
		v = pi * r * r * h;
		// TODO Auto-generated method stub
		return v;
	}

}

  题目分析:本道题目并没有什么难度,所要求的东西均在题目中给出,只要一步一步的按照题目理一遍就能将本道题目写出。

 

7-1 电信计费系列2-手机+座机计费

源码如下:

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;

public class Main{
	public static void main(String []args) throws ParseException {					
		Scanner input = new Scanner(System.in);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ArrayList<User> arr = new ArrayList<>();
        int flag = 0;
        String sc = input.nextLine();
        String[] a ;
        //z1
        while (!sc.equals("end")){
        	//z1
            if(sc.matches("[u][-](0791)[0-9]{7,8}[ ][0]")){
                a = sc.substring(2,sc.length()).split("\\s");
                
                for(int i = 0;i < arr.size();i++){
                    if(Objects.equals(arr.get(i).getNumber(), a[0])) {
                        flag = 1;
                    }
                }
                if(flag == 0) {
	                arr.add(new User(a[0]));
	                flag = 0;
                }
            }
            //s1
            else if(sc.matches("u-1[0-9]{10} [1]")) {
            	 a = sc.substring(2,sc.length()).split("\\s");
            	 for(int i = 0;i < arr.size();i++){
                     if(Objects.equals(arr.get(i).getNumber(), a[0])) {
                         flag = 1;
                     }
                 }
                 if(flag == 0) {
 	                arr.add(new User(a[0]));
 	                flag = 0;
                 }
            }
            //z1,z2
           if(sc.matches("t-0\\d{9,11}\\s0\\d{9,11}\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d")){
                a = sc.substring(2,sc.length()).split("\\s");
                for(int i=0 ; i< arr.size() ; i++){
                    if(arr.get(i).number.equals(a[0])){
                        if(a[1].startsWith("0791")){
                            arr.get(i).userRecord.addCallingInCityRecords(new CallRecord(a[2]+" "+a[3],a[4]+" "+a[5]));
                            arr.get(i).setUserRecord(arr.get(i).userRecord);
                        }
                        else if(a[1].substring(0,4).matches("(079\\d|0701)")){
                            arr.get(i).userRecord.addCallingInProvinceRecords(new CallRecord(a[2]+" "+a[3],a[4]+" "+a[5]));
                            arr.get(i).setUserRecord(arr.get(i).userRecord);
                        }
                        else{
                            arr.get(i).userRecord.addCallingInLandRecords(new CallRecord(a[2]+" "+a[3],a[4]+" "+a[5]));
                            arr.get(i).setUserRecord(arr.get(i).userRecord);
                        }
                    }
                }
            }
            //z1,s2
            else if(sc.matches("t-(0[0-9]{3}|020)\\d{7,9} \\d{11} (\\d{3}|\\d{4})\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d")){
            	a = sc.substring(2,sc.length()).split("\\s");
            	for (int i=0;i< arr.size();i++){
                    if (arr.get(i).number.equals(a[0])){
                        if (a[2].startsWith("0791")){
                            arr.get(i).userRecord.addCallingInCityRecords(new CallRecord(a[3] + " " + a[4], a[5] + " " + a[6]));
                        }
                        else if (a[2].matches("(079\\d|0701)")){
                            arr.get(i).userRecord.addCallingInProvinceRecords(new CallRecord(a[3] + " " + a[4], a[5] + " " + a[6]));
                        }
                        else {
                            arr.get(i).userRecord.addCallingInLandRecords(new CallRecord(a[3] + " " + a[4], a[5] + " " + a[6]));
                        }
                    }
                    if (arr.get(i).number.equals(a[1])){
                        if (!a[2].matches("(079\\d|0701)"))
                            arr.get(i).userRecord.addAnswerInLandRecords(new CallRecord(a[3] + " " + a[4], a[5] + " " + a[6]));
                    }
                } 
            }
            //s1,s2
            else if (sc.matches("t-\\d{11} (\\d{3}|\\d{4}) \\d{11} (\\d{3}|\\d{4})\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d")){
            	a = sc.substring(2,sc.length()).split("\\s");
            	 for (int i=0;i< arr.size();i++){
                     if (arr.get(i).number.equals(a[0])){
                         if (a[1].equals("0791")) {
                             if (a[3].matches("0791"))
                                 arr.get(i).userRecord.addCallingInCityRecords(new CallRecord(a[4] + " " + a[5], a[6] + " " + a[7]));
                             else if (a[3].matches("(079\\d|0701)"))
                                 arr.get(i).userRecord.addCallingInProvinceRecords(new CallRecord(a[4] + " " + a[5], a[6] + " " + a[7]));
                             else
                                 arr.get(i).userRecord.addCallingInLandRecords(new CallRecord(a[4] + " " + a[5], a[6] + " " + a[7]));
                         }
                         else if (a[1].matches("(0701|079\\d)")){
                             arr.get(i).userRecord.addCallingInProvinceRoamRecords(new CallRecord(a[4] + " " + a[5], a[6] + " " + a[7]));
                         }
                         else
                             arr.get(i).userRecord.addCallingInLandRoamRecords(new CallRecord(a[4] + " " + a[5], a[6] + " " + a[7]));
                     }
                     if (arr.get(i).number.equals(a[2])){
                         if (!a[3].matches("(079\\d|0701)"))
                             arr.get(i).userRecord.addAnswerInLandRecords(new CallRecord(a[4] + " " + a[5], a[6] + " " + a[7]));
                     }
                 }
            }
            //s1,z2
            else if (sc.matches( "t-\\d{11} (020|0\\d{3}) \\d{10,13}\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\s((((1[6-9]|[2-9]\\d)\\d{2}).([13578]|1[02]).([1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2}).([13456789]|1[012]).([1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-2-([1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-2-29-)) (20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d" )){
            	a = sc.substring(2,sc.length()).split("\\s");
            	for (int i=0;i< arr.size();i++){
                    if (arr.get(i).number.equals(a[0])){
                        if (a[1].equals("0791")){
                            if (a[2].matches("0791\\d{6,8}"))
                                arr.get(i).userRecord.addCallingInCityRecords(new CallRecord(a[3] + " " + a[4], a[5] + " " + a[6]));
                            else if (a[2].matches("(079\\d|0701)\\d{6,8}"))
                                arr.get(i).userRecord.addCallingInProvinceRecords(new CallRecord(a[3] + " " + a[4], a[5] + " " + a[6]));
                            else
                                arr.get(i).userRecord.addCallingInLandRecords(new CallRecord(a[3] + " " + a[4], a[5] + " " + a[6]));
                        }
                        else if (a[1].matches("(0701|0790|079[2-9])")){
                            arr.get(i).userRecord.addCallingInProvinceRoamRecords(new CallRecord(a[3] + " " + a[4], a[5] + " " + a[6]));
                        }
                        else
                            arr.get(i).userRecord.addCallingInLandRoamRecords(new CallRecord(a[3] + " " + a[4], a[5] + " " + a[6]));
                    }
                }
            }
            sc = input.nextLine();
        }
        
        List<User> list = new ArrayList<>(arr);
        Collections.sort(list);
        
        for (User e : list){
            System.out.print(e.getNumber()+" ");
            System.out.print(new DecimalFormat("0.0#").format(e.calCost()));
            System.out.print(" ");
            System.out.print(new DecimalFormat("0.0#").format(e.calBalance()));
            System.out.println();
        }


	}
}

class User implements Comparable<User> {
    UserRecords userRecord = new UserRecords();
    double balance = 100;
    ChargeMode chargeMode;
    String number;
    public User(){

    }
    public User(String number) {
        this.number = number;
    }

    public double calBalance(){
        if(number.matches("(0791)\\d{6,8}"))
            return balance-calCost()-20;
        return balance-calCost()-15;
    }
    public double calCost(){
        LandPhoneInCityRule landPhoneInCityRule = new LandPhoneInCityRule();
        LandPhoneInIandRule landPhoneInIandRule = new LandPhoneInIandRule();
        LandPhoneInProvinceRule landPhoneInProvinceRule = new LandPhoneInProvinceRule();
        PhoneInCityRule phoneInCityRule = new PhoneInCityRule();
        PhoneInIandRule phoneInIandRule = new PhoneInIandRule();
        PhoneInProvinceRule phoneInProvinceRule = new PhoneInProvinceRule();
        CallPhoneInIandRoamRule callphoneInIandRoamRule = new CallPhoneInIandRoamRule();
        CallPhoneInProvinceRoamRule callphoneInProvinceRoamRule = new CallPhoneInProvinceRoamRule();
        AnswerPhoneInLandRoamRule answerPhoneInLandRoamRule = new AnswerPhoneInLandRoamRule();
        if(number.matches("(0791)\\d{6,8}")) {
            return landPhoneInIandRule.calCost(userRecord.getCallingInLandRecords())
            		+landPhoneInCityRule.calCost(userRecord.getCallingInCityRecords())
            		+landPhoneInProvinceRule.calCost(userRecord.getCallingInProvinceRecords());
        }
        return phoneInIandRule.calCost(userRecord.getCallingInLandRecords())
        		+phoneInCityRule.calCost(userRecord.getCallingInCityRecords())
        		+phoneInProvinceRule.calCost(userRecord.getCallingInProvinceRecords())
        		+answerPhoneInLandRoamRule.calCost(userRecord.getAnswerInLandRecords())
        		+callphoneInIandRoamRule.calCost(userRecord.getCallingInLandRoamRecords())
        		+callphoneInProvinceRoamRule.calCost(userRecord.getCallingInProvinceRoamRecords());
    }
    
    public UserRecords getUserRecord(){
        return userRecord;
    }
    
    public void setUserRecord(UserRecords userRecord){
        this.userRecord = userRecord;
    }
    
    public double getBalance(){
        return balance;
    }
    
    public ChargeMode getChargeMode(){
        return chargeMode;
    }
    
    public void setChargeMode(ChargeMode chargeMode){
        this.chargeMode = chargeMode;
    }
    
    public String getNumber(){
        return number;
    }
    
    public void setNumber(String number){
        this.number = number;
    }
    
    public int compareTo(User o) {
        return this.getNumber().compareTo(o.getNumber());
    }

}
abstract class ChargeMode{
    ArrayList<ChargeRule> list = new ArrayList<ChargeRule>();
    public ArrayList<ChargeRule> getChargeRule(){
        return list;
    }
    public void setChargeRule(ArrayList<ChargeRule> chargeRule){
        this.list = list;
    }
    public double calCost(UserRecords userRecords){
        return 0;
    }
    public double getMonthlyRent(){
        return 20;
    }
}
class UserRecords{
    ArrayList<CallRecord> callingInProvinceRoamRecords =  new ArrayList<CallRecord>();
    ArrayList<CallRecord> callingInLandRoamRecords =  new ArrayList<CallRecord>();
    ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();
    ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();
    ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();
    ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();
    ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();
    ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();
    ArrayList<MessageRecord> sendMessageRecord = new ArrayList<MessageRecord>();
    ArrayList<MessageRecord> receiveMessageRecord = new ArrayList<MessageRecord>();
    
    public void addCallingInProvinceRoamRecords(CallRecord callRecord){
        callingInProvinceRoamRecords.add(callRecord);
    }
    
    public void addCallingInLandRoamRecords(CallRecord callRecord){
        callingInLandRoamRecords.add(callRecord);
    }
    
    public void addCallingInCityRecords(CallRecord callRecord){
        callingInCityRecords.add(callRecord);
    }
    
    public void addCallingInProvinceRecords(CallRecord callRecord){
        callingInProvinceRecords.add(callRecord);
    }
    
    public void addCallingInLandRecords(CallRecord callRecord){
        callingInLandRecords.add(callRecord);
    }
    
    public void addAnswerInCityRecords(CallRecord callRecord){
        answerInCityRecords.add(callRecord);
    }
    
    public void addAnswerInProvinceRecords(CallRecord callRecord){
        answerInProvinceRecords.add(callRecord);
    }
    
    public void addAnswerInLandRecords(CallRecord callRecord){
        answerInLandRecords.add(callRecord);
    }
    
    public void addSendMessageRecord(MessageRecord sendMessageRecord){
        this.sendMessageRecord.add(sendMessageRecord);
    }
    
    public void addReceiveMessageRecord(MessageRecord receiveMessageRecord){
        this.receiveMessageRecord.add(receiveMessageRecord);
    }
    
    public ArrayList<MessageRecord> getSendMessageRecord(){
        return sendMessageRecord;
    }
    
    public ArrayList<MessageRecord> getReceiveMessageRecord(){
        return receiveMessageRecord;
    }
    
    public ArrayList<CallRecord> getCallingInCityRecords(){
        return callingInCityRecords;
    }
    
    public ArrayList<CallRecord> getCallingInProvinceRecords(){
        return callingInProvinceRecords;
    }
    
    public ArrayList<CallRecord> getCallingInLandRecords(){
        return callingInLandRecords;
    }
    
    public ArrayList<CallRecord> getAnswerInCityRecords(){
        return answerInCityRecords;
    }
    
    public ArrayList<CallRecord> getAnswerInProvinceRecords(){
        return answerInProvinceRecords;
    }
    
    public ArrayList<CallRecord> getAnswerInLandRecords(){
        return answerInLandRecords;
    }
    
    public ArrayList<CallRecord> getCallingInProvinceRoamRecords(){
        return  callingInProvinceRoamRecords;
    }
    
    public ArrayList<CallRecord> getCallingInLandRoamRecords(){
        return callingInLandRoamRecords;
    }


}
class LandlinePhoneCharging extends ChargeMode{
    double monthlyRent = 20;

    public double calCost(UserRecords userRecords){
        return 0;
    }
    
    public double getMonthlyRent(){
        return monthlyRent;
    }
}
class CommunicationRecord{
    String callingNumber;
    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 CallRecord extends ChargeRule{
    Date startTime;
    Date endTime;
    String callingAddressAreaCode;
    String AnswerAddressAreaCode;

    public CallRecord(String startTime,String endTime) throws ParseException {
        this.startTime =  new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").parse(startTime);
        this.endTime = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").parse(endTime);
    }

    public Date getStartTime(){
        return startTime;
    }
    
    public void setStartTime(Date startTime){
        this.startTime = startTime;
    }
    
    public Date getEndTime(){
        return endTime;
    }
    
    public void setEndTime(Date endTime){
        this.endTime = endTime;
    }
    
    public String getCallingAddressAreaCode(){
        return callingAddressAreaCode;
    }
    
    public void setCallingAddressAreaCode(String callingAddressAreaCode){
        this.callingAddressAreaCode = callingAddressAreaCode;
    }

    public String getAnswerAddressAreaCode() {
        return AnswerAddressAreaCode;
    }

    public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
        this.AnswerAddressAreaCode = answerAddressAreaCode;
    }
    
}

class MessageRecord extends ChargeRule{
    String message;

    public String getMessage() {
        return message;
    }

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

abstract class ChargeRule{

}

abstract class CallChargeRule extends ChargeRule{
    public double calCost(ArrayList<CallRecord>callRecords){
        return 0;
    }
}

class LandPhoneInCityRule extends CallChargeRule{
    public double calCost(ArrayList<CallRecord> callRecords){
        double cost = 0;
        double  minutes;
        for (CallRecord e: callRecords){
            minutes = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime())/ (double) 60000);
            cost = cost + 0.1 * minutes;
        }
        return cost;
    }
}

class LandPhoneInProvinceRule extends CallChargeRule{
    public double calCost(ArrayList<CallRecord> callRecords){
        double cost = 0;
        double  minutes;
        for (CallRecord e: callRecords){
            minutes = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime())/ (double) 60000);
            cost = cost + 0.3 * minutes;
        }
        return cost;
    }
}

class LandPhoneInIandRule extends CallChargeRule{
    public double calCost(ArrayList<CallRecord> callRecords){
        double cost = 0;
        double  minutes;
        for (CallRecord e: callRecords){
            minutes = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime())/ (double) 60000);
            cost = cost + 0.6 * minutes;
        }
        return cost;
    }
}

class PhoneInCityRule extends CallChargeRule{
    public double calCost(ArrayList<CallRecord> callRecords){
        double cost = 0;
        double  minutes;
        for (CallRecord e: callRecords){
            minutes = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime())/ (double) 60000);
            cost = cost + 0.1 * minutes;
        }
        return cost;
    }
}

class PhoneInProvinceRule extends CallChargeRule{
    public double calCost(ArrayList<CallRecord> callRecords){
        double cost = 0;
        double  minutes;
        for (CallRecord e: callRecords){
            minutes = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime())/ (double) 60000);
            cost = cost + 0.2 * minutes;
        }
        return cost;
    }
}

class PhoneInIandRule extends CallChargeRule{
    public double calCost(ArrayList<CallRecord> callRecords){
        double cost = 0;
        double  minutes;
        for (CallRecord e: callRecords){
            minutes = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime())/ (double) 60000);
            cost = cost + 0.3 * minutes;
        }
        return cost;
    }
}

class CallPhoneInIandRoamRule extends CallChargeRule{
    public double calCost(ArrayList<CallRecord> callRecords){
        double cost = 0;
        double  minutes;
        for (CallRecord e: callRecords){
            minutes = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime())/ (double) 60000);
            cost = cost + 0.6 * minutes;
        }
        return cost;
    }
}

class CallPhoneInProvinceRoamRule extends CallChargeRule{
    public double calCost(ArrayList<CallRecord> callRecords){
        double cost = 0;
        double  minutes;
        for (CallRecord e: callRecords){
            minutes = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime())/ (double) 60000);
            cost = cost + 0.3 * minutes;
        }
        return cost;
    }
}

class AnswerPhoneInLandRoamRule extends CallChargeRule{
    public double calCost(ArrayList<CallRecord> callRecords){
        double cost = 0;
        double  minutes;
        for (CallRecord e: callRecords){
            minutes = (int) Math.ceil((double) (e.getEndTime().getTime()-e.getStartTime().getTime())/ (double) 60000);
            cost = cost + 0.3 * minutes;
        }
        return cost;
    }
}

  类图如下:

 

 

 

 题目分析:本道题目在之前的那道题目上进行添加一些算钱的类,依然如同上题一样利用正则表达式来判别输入的字符串之后在进行相应的开户等操作,本道题目由于有上一题的参考,所以添加几个算钱的类,并且在总的算钱的类里面更改一下算钱方式即可。

 

采坑心得:本道题目的关于年份的判断出了一点问题,经多次修改为修改正确,放弃了。

7-2 sdut-Collection-sort--C~K的班级(II)

源码如下:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner (System.in);
		int n = input.nextInt();
		String a = input.nextLine();
		ArrayList<String> list = new ArrayList<String>();
	    while(n-- != 0) {
	    	a = input.nextLine();
	    	if(list.contains(a)) {
	    		continue;
	    	}
	    	else {
	    		list.add(a);
	    	}
	    }
	    System.out.println(list.size());
	    
	    List<String> b = new ArrayList<>(list);
        Collections.sort(list);
	    
	    for(String c : list) {
	    	System.out.println(c);
	    }
	    
	}
}

  题目分析:本道题目的难度在于用了一个contains()函数去判断是否有重复的数据输入,之后排序一下输出即可。

7-3 阅读程序,按照题目需求修改程序

源码如下:

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;


//1、导入相关包

//定义员工类
class Employee implements Comparable<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;
	}

	@Override
	public int compareTo(Employee o) {
		// TODO Auto-generated method stub
		return 0;
	}
}

//主函数
public class Main {

	public static void main(String[] args) {
		
		ArrayList<Employee> list = new ArrayList<Employee>();
				// 1、创建有序集合对象

   // 创建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);
			list.add(employee);
		}			
		 List<Employee> c = new ArrayList<>(list);
	     Collections.sort(list);
	     
	     for(Employee a : list) {
	    	 System.out.println(a.getName() + "---" + a.getAge());
	     }
				
				
		/*		// 2、创建迭代器遍历集合
				Iterator it;
				
				//3、遍历
				while (it.hasNext()) {
					//4、集合中对象未知,向下转型
					Employee e =   (Employee)it.next();
					System.out.println(e.getName() + "---" + e.getAge());
				}
	
	*/
	}

}

  题目分析:本道题目由于我未使用题目给出的迭代器方法,所以本题不做分析。

7-1 电信计费系列3-短信计费

 源码如下:
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class Main {
	
    public static void main(String[] args) throws ParseException {
    	Scanner input = new Scanner(System.in);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ArrayList<User> users = new ArrayList<>();
        String sc = input.nextLine();
        String[] a ;
        while (!sc.equals("end")){
            int j = 0;
            if(sc.matches("u-1[0-9]{10} [3]")){
                a = sc.substring(2).split("\\s");
                if (users.size() == 0){
                    users.add(new User(a[0],new MessageCharging()));}
                else {
                    for (int i = 0; i < users.size(); i++) {
                        if (a[0].equals(users.get(i).getNumber())) {
                            j = 1;
                            break;
                        }
                    }
                    if (j == 0 && users.size() != 0)
                        users.add(new User(a[0],new MessageCharging()));
                }
            }
            else if(sc.matches("m-1\\d{10} 1\\d{10} (\\w|\\,|\\.| )+") || sc.matches("m-1\\d{10} 1\\d{10}")){
                a = sc.substring(2).split("\\s");
                for (User e:users){
                    if (a[0].equals(e.getNumber())) {
                        e.getUserRecords().addSendMessageRecord(new MessageRecord(sc.substring(26,sc.length())));
                    }
                }
            }
            sc = input.nextLine();
        }
        
        Collections.sort(users);
        
        for (User e:users) {
            System.out.print(e.getNumber()+" ");
            System.out.printf("%.1f",e.calCost());
            System.out.print(" ");
            System.out.printf("%.1f",e.calBalance());
            System.out.println();
        }
    }
}

class User implements Comparable<User> {
    UserRecords userRecords = new UserRecords();
    double balance = 100;
    ChargeMode chargeMode;
    ChargePhone chargePhone;
    String number;
    public User(){

    }

    public User(String s, LandlinePhoneCharging landlinePhoneCharging) {
        this.number = s;
    }

    public User(String s, MobilePhoneCharging mobilePhoneCharging) {
        this.number = s;
    }

    public User(String s, MessageCharging messageCharging) {
        this.number = s;
    }

    public double calBalance(){
        return balance-calCost();
    }

    public double calCost() {
        MessageChargeRule messageChargeRule = new MessageChargeRule();
        return messageChargeRule.calCost(userRecords.sendMessageRecord);
    }
    
    public UserRecords getUserRecords(){
        return userRecords;
    }
    
    public void setUserRecords(UserRecords userRecords){
        this.userRecords = userRecords;
    }
    
    public double getBalance(){
        return balance;
    }
    
    public ChargeMode getChargeMode(){
        return chargeMode;
    }
    
    public void setChargeMode(ChargeMode chargeMode){
        this.chargeMode = chargeMode;
    }
    
    public String getNumber(){
        return number;
    }

    public ChargePhone getChargePhone() {
        return chargePhone;
    }

    public void setNumber(String number){
        this.number = number;
    }
    
    public int compareTo(User o) {
        return this.getNumber().compareTo(o.getNumber());
    }

}

abstract class ChargeMode{
    ArrayList<ChargeRule> list = new ArrayList<ChargeRule>();
    public ArrayList<ChargeRule> getChargeRule(){
        return list;
    }
    public void setChargeRule(ArrayList<ChargeRule> chargeRule){
        this.list = list;
    }
    public double calCost(UserRecords userRecords){
        return 0;
    }
    public double getMonthlyRent(){
        return 0;
    }
}

abstract class ChargePhone{
    ArrayList<ChargeRule> list = new ArrayList<ChargeRule>();
    public ArrayList<ChargeRule> getChargeRule(){
        return list;
    }
    public void setChargeRule(ArrayList<ChargeRule> chargeRule){
        this.list = list;
    }
    public double calCost(UserRecords userRecords){
        return 0;
    }
    public double getMonthlyRent(){
        return 15;
    }
}

abstract class ChargeMessage{
    ArrayList<ChargeRule> list = new ArrayList<ChargeRule>();
    public ArrayList<ChargeRule> getChargeRule(){
        return list;
    }
    public void setChargeRule(ArrayList<ChargeRule> chargeRule){
        this.list = list;
    }
    public double calCost(UserRecords userRecords){
        return 0;
    }
    public double getMonthlyRent(){
        return 0;
    }
}

class UserRecords{
    ArrayList<MessageRecord> sendMessageRecord = new ArrayList<MessageRecord>();
    ArrayList<MessageRecord> receiveMessageRecord = new ArrayList<MessageRecord>();
    
    public void addSendMessageRecord(MessageRecord sendMessageRecord){
        this.sendMessageRecord.add(sendMessageRecord);
    }
    
    public void addReceiveMessageRecord(MessageRecord receiveMessageRecord){
        this.receiveMessageRecord.add(receiveMessageRecord);
    }
    
    public ArrayList<MessageRecord> getSendMessageRecord(){
        return sendMessageRecord;
    }
    
    public ArrayList<MessageRecord> getReceiveMessageRecord(){
        return receiveMessageRecord;
    }
    
}

class LandlinePhoneCharging extends ChargeMode{
    double monthlyRent = 20;

    public double calCost(UserRecords userRecords){
        return 0;
    }
    public double getMonthlyRent(){
        return monthlyRent;
    }
}

class MobilePhoneCharging extends ChargePhone{
    double monthlyRent = 15;

    public double calCost(UserRecords userRecords){
        return 0;
    }
    public double getMonthlyRent(){
        return monthlyRent;
    }
}

class MessageCharging extends ChargeMessage{
    double monthlyRent = 0;

    public double calCost(UserRecords userRecords){
        return 0;
    }
    public double getMonthlyRent(){
        return monthlyRent;
    }
}

class CommunicationRecord{
    String callingNumber;
    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 CallRecord extends CommunicationRecord{
    Date startTime;
    Date endTime;
    String callingAddressAreaCode;
    String AnswerAddressAreaCode;

    public CallRecord(String startTime,String endTime) throws ParseException {
        this.startTime =  new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").parse(startTime);
        this.endTime = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").parse(endTime);
    }

    public Date getStartTime(){
        return startTime;
    }
    public void setStartTime(Date startTime){
        this.startTime = startTime;
    }
    public Date getEndTime(){
        return endTime;
    }
    public void setEndTime(Date endTime){
        this.endTime = endTime;
    }
    public String getCallingAddressAreaCode(){
        return callingAddressAreaCode;
    }
    public void setCallingAddressAreaCode(String callingAddressAreaCode){
        this.callingAddressAreaCode = callingAddressAreaCode;
    }

    public String getAnswerAddressAreaCode() {
        return AnswerAddressAreaCode;
    }

    public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
        this.AnswerAddressAreaCode = answerAddressAreaCode;
    }
}

class MessageRecord extends CommunicationRecord{
    String message;

    public MessageRecord(String message) {
        this.message = 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) {
        return super.calCost(messageRecords);
    }
}

abstract class ChargeRule{

}

abstract class CallChargeRule {
	
    public double calCost(ArrayList<CallRecord>callRecords){
        return 0;
    }
    
}

class MessageChargeRule extends ChargeRule{
	
    public double calCost(ArrayList<MessageRecord> messageRecords) {
        double cost = 0;
        double count = 0;
        
        for (int i = 0; i < messageRecords.size(); i++) {
            double a = messageRecords.get(i).message.length();
            
            while (a > 0){
                count++;
                a = a - 10;
            }
        }
        
        if(count <= 3){
            cost = 0.1 * count;
        }
        else if(count > 3 && count <= 5){
            cost = 0.3 + 0.2 * (count - 3);
        }
        else if(count > 5){
            cost = 0.7 + 0.3 * ( count - 5) ;
        }
        
        return cost;
    }
}

  

类图如下:

 

 

 

 题目分析:

本道题目由于和之前收费并无关系,所以可以将之前很多的代码进行删除,修改,最后进行一个关于短信的收费计算即可,由于其类之间的关系基本没有变化,所以只需要在main函数里面进行修改即可。

采坑心得:

本道题目关于如何将短信收费的计算做好是非常重要的。

7-2 编写一个类Shop(商店)、内部类InnerCoupons(内部购物券)

源码如下:

import java.util.Scanner;


public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		Shop myshop = new Shop(input.nextInt());
		Shop.InnerCoupons in = myshop.new InnerCoupons();
		in.buy();
	}
}

class Shop{
	InnerCoupons coupons50 = new InnerCoupons(50);
	InnerCoupons coupons100 = new InnerCoupons(100);
	private int milkCount;
	
	public Shop() {
		super();
	}

	public Shop(int milkCount) {
		super();
		this.milkCount = milkCount;
	}

	public int getMilkCount() {
		return milkCount;
	}

	public void setMilkCount(int milkCount) {
		this.milkCount = milkCount;
	}
	
	public InnerCoupons getCoupons50() {
		return coupons50;
	}

	public void setCoupons50(InnerCoupons coupons50) {
		this.coupons50 = coupons50;
	}

	public InnerCoupons getCoupons100() {
		return coupons100;
	}

	public void setCoupons100(InnerCoupons coupons100) {
		this.coupons100 = coupons100;
	}

	class InnerCoupons{	
		int value;
		
		public InnerCoupons(int value) {
			super();
			this.value = value;
		}
		
		public InnerCoupons() {
			
		}

		public void buy() {
			System.out.println("使用了面值为50的购物券进行支付");
			setMilkCount(milkCount - coupons50.value / 50);
			System.out.println("牛奶还剩" + getMilkCount() + "箱");
			System.out.println("使用了面值为100的购物券进行支付");
			setMilkCount(milkCount - coupons100.value / 50);
			System.out.println("牛奶还剩" + getMilkCount() + "箱");
		}
		
	}
	
	
}

  题目分析:

本次题目的难点在于内部类的使用,其余并没有什么难度。

 

7-3 动物发声模拟器(多态)

源码如下:

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.print(animal.getAnimalClass()+"的叫声:");
	  animal.shout();
  }

}


//定义抽象类Animal
abstract class Animal{
	public String getAnimalClass() {
		return null;
	}
	public void shout() {
		
	}
}
//基于Animal类,定义猫类Cat,并重写两个抽象方法
class Cat extends Animal{
	@Override
	public String getAnimalClass() {
		return "猫";
	}
	@Override
	public void shout() {
		System.out.println("喵喵");
	}
}
//基于Animal类,定义狗类Dog,并重写两个抽象方法
class Dog extends Animal{
	@Override
	public String getAnimalClass() {
		return "狗";
	}
	@Override
	public void shout() {
		System.out.println("汪汪");
	}
}
//基于Animal类,定义山羊类Goat,并重写两个抽象方法
class Goat extends Animal{
	@Override
	public String getAnimalClass() {
		return "山羊";
	}
	@Override
	public void shout() {
		System.out.println("咩咩");
	}
}

题目分析:本道题目进行的简单的多肽使用,并没有什么难度。

采坑心得:获取动物名的时候记得设置String’函数。

 

四:改进建议

对于第八次题目集的作业:资费题目由于给出了类图,并没有什么需要改进的地方。

 

对于第九次题目集第五次作业:资费题目中由于正则表达式出了点错误,所以尽量避免使用正则表达式或者将正则表达式修改正确。

 

对于第十次题目集第二次作业:资费题目未出现什么大的问题,并不需要修改。

 

 

五:总结

通过这几次题目集的练习,从最初对java的简单认知到现在对java有了一定的认知,算是完成了对于java这门课程的入门,但是题目的难度越来越大于,对于自身的能力的提升也应跟随上脚步。并且这几次题目对于正则表达式的训练需要加强,对于类与类之间关系需要有较为清晰的认知,这几次的pta作业由于老师降低了难度,但是对我来说题目的难度依旧是非常的大。除此之外,最近开始了java的实训,其中将学习使用swing去制作界面,所以接下来的java学习更加不能松懈,需要不断的接受新知识来扩充自己,增强自己的能力。

posted @ 2022-06-16 20:32  ffhjnf  阅读(110)  评论(0)    收藏  举报