大皮

去见、去学、去写。

南昌航空大学 软件学院 pta Java 第六次作业 蔡珂 opp

激动的心,颤抖的手。
终于熬过了边角线三次作业了。
难度可以说是腰斩。
基本就是按图说话。
但第一题还是有点点麻烦。
别急,先把代码贴上去。

drawing

题目列表

7-1 电信计费系列1-座机计费

实现一个简单的电信计费程序:
假设南昌市电信分公司针对市内座机用户采用的计费方式:
月租20元,接电话免费,市内拨打电话0.1元/分钟,省内长途0.3元/分钟,国内长途拨打0.6元/分钟。不足一分钟按一分钟计。
南昌市的区号:0791,江西省内各地市区号包括:0790~0799以及0701。

输入格式:

输入信息包括两种类型
1、逐行输入南昌市用户开户的信息,每行一个用户,
格式:u-号码 计费类型 (计费类型包括:0-座机 1-手机实时计费 2-手机A套餐)
例如:u-079186300001 0
座机号码除区号外由是7-8位数字组成。
本题只考虑计费类型0-座机计费,电信系列2、3题会逐步增加计费类型。
2、逐行输入本月某些用户的通讯信息,通讯信息格式:
座机呼叫座机:t-主叫号码 接听号码 起始时间 结束时间
t-079186330022 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:11
以上四项内容之间以一个英文空格分隔,
时间必须符合"yyyy.MM.dd HH:mm:ss"格式。提示:使用SimpleDateFormat类。
以上两类信息,先输入所有开户信息,再输入所有通讯信息,最后一行以“end”结束。
注意:
本题非法输入只做格式非法的判断,不做内容是否合理的判断(时间除外,否则无法计算),比如:
1、输入的所有通讯信息均认为是同一个月的通讯信息,不做日期是否在同一个月还是多个月的判定,直接将通讯费用累加,因此月租只计算一次。
2、记录中如果同一电话号码的多条通话记录时间出现重合,这种情况也不做判断,直接 计算每条记录的费用并累加。
3、用户区号不为南昌市的区号也作为正常用户处理。

输出格式:

根据输入的详细通讯信息,计算所有已开户的用户的当月费用(精确到小数点后2位,
单位元)。假设每个用户初始余额是100元。
每条通讯信息单独计费后累加,不是将所有时间累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。

错误处理:
输入数据中出现的不符合格式要求的行一律忽略。

代码如下


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




public class Main {
    static String []tel = {"0792","0793","0794","0795","0796","0797","0798","0799","0701"};
    public static void main(String[] args) throws ParseException {
        Scanner input = new Scanner(System.in);
        String ch = new String();
        ch = input.nextLine();
        ArrayList<User>users = new ArrayList<>();
        SimpleDateFormat date=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
            //   System.out.printf(ch);
            while (!ch.equals("end")) {
                //  System.out.printf(ch+"\n");
                try {
                    if ((ch.matches("^u-[0-9]{11,12} 0$") || ch.matches("^t-[0]{1}[0-9]{9,11} [0]{1}[0-9]{9,11} [0-9]{4}[.](([1-9]{1})|([1]{1}[0-2]{1}))[.]([1-9]|([1-2]{1}[0-9]{1})|(3[0-1])) (([0-1][0-9])|(2[0-3]))[:]([0-5][0-9])[:]([0-5][0-9]) [0-9]{4}[.](([1-9]{1})|([1]{1}[0-2]{1}))[.]([1-9]|([1-2]{1}[0-9]{1})|3[0-1]) (([0-1][0-9])|(2[0-3]))[:]([0-5][0-9])[:]([0-5][0-9])$"))) {
                        String[] splitSet = ch.split("-");
                        //String[] splitset2 = splitSet[1].split(" ");
                        if (splitSet[0].equals("u")) {
                            String[] splitSet2 = splitSet[1].split(" ");
                            if(!numEqual(users,splitSet2[0])){
                                try {
                                    users.add(newUesr(splitSet2[0]));
                                } catch (Exception a){
                                    
                                }
                                
                            }
                                
                        } else {
                                users = newRecord(users, splitSet[1]);
                        }
                    }
                }catch (Exception e){

                }

                ch = input.nextLine();
            }

        Collections.sort(users, new Cmp());
        for(User U:users){
            try {
                System.out.println(U.getNumber()+" "+printDouble(U.calCost())+" "+printDouble(U.calBalance()));
            }
            catch (Exception ps){
                
            }
        }


    }
     static User newUesr(String ch){
        User user = new User(ch);
         user.chargeMode = new LandlinePhoneCharging();
        return user;
    }
    public static double printDouble(double num) {
        String str = String.format("%.3f",num);
        num = Double.parseDouble(str);
        return num;
    }
    static boolean numEqual(ArrayList<User> User,String num){
        for(User user:User){
            if(user.getNumber().equals(num))
                return true;
        }
        return false;

    }
    static  ArrayList<User> newRecord(ArrayList<User> user,String ch) throws ParseException {
        String[] splitSet = ch.split(" ");


        CallRecord callRecord = new CallRecord(splitSet[2],splitSet[3],splitSet[4],splitSet[5],splitSet[0].substring(0,4),splitSet[1].substring(0,4));
        for(User U : user){
            if(U.number.equals(splitSet[0])){
                if(callRecord.callingAddressAreaCode.equals(callRecord.answerAddressAreaCode)){
                    U.getUserRecords().addCallingInCityRecords(callRecord);
                    return user;
                }
                for(String tel2:tel){
                    if(callRecord.answerAddressAreaCode.equals(tel2)){
                        U.getUserRecords().addCallingInProvinceRecords(callRecord);
                        return user;
                    }
                }
                U.getUserRecords().addCallingInLandRecords(callRecord);
                return user;
            }
        }
        return user;
}

}









abstract class CallChargeRule extends ChargeRule{
    public abstract double calCost(CallRecord callRecord);
}


class CallRecord extends CommunicationRecord{
   Date startTime,endTime;
   String callingAddressAreaCode,answerAddressAreaCode;
    public CallRecord(){}
    public CallRecord(String a,String b,String c,String d,String callingAddressAreaCode,String answerAddressAreaCode) throws ParseException {
        super();
        SimpleDateFormat date=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        this.callingAddressAreaCode =callingAddressAreaCode;
        this.answerAddressAreaCode = answerAddressAreaCode;
        this.setStartTime(date.parse(a+" "+b));
        this.setEndTime(date.parse(c+" "+d));
    }
   Date getStartTime(){
     return startTime;
   }
   Date getEndTime(){
     return endTime;
   }
   String getCallingAddressAreaCode(){
    return callingAddressAreaCode;
   }
   String getAnswerAddressAreaCode(){
    return answerAddressAreaCode;
   }
   void setStartTime(Date startTime){
     this.startTime = startTime;
   }
   void setEndTime(Date endTime){
    this.endTime = endTime;
   }
   void setCallingAddressAreaCode(String callingAddressAreaCode){
     this.callingAddressAreaCode=callingAddressAreaCode;
   }
   void setAnswerAddressAreaCode(String answerAddressAreaCode){
    this.answerAddressAreaCode =answerAddressAreaCode;
   }

}

abstract class ChargeMode {
    ArrayList<ChargeRule>chargeRules =new ArrayList<ChargeRule>();
    ArrayList<ChargeRule> getChargeRules(){
        return chargeRules;
    }
    void  setChargeRules(ArrayList<ChargeRule> chargeRules) {
        this.chargeRules = (ArrayList<ChargeRule>) (chargeRules.clone());
    }
    public  abstract  double calCost(UserRecords userRecords);
    public  abstract  double getMonthlyRent();
}


 abstract class ChargeRule {
}





class Cmp implements Comparator<User> {
    @Override
    public int compare(User U1, User U2) {
        
        return U1.getNumber().compareTo(U2.getNumber());
    }
}



abstract class CommunicationRecord {
    String callingNumer,answerNumer;
    void setCallingNumer(String callingNumer){
        this.callingNumer=callingNumer;
    }
    void  setAnswerNumer(String answerNumer){
        this.answerNumer =answerNumer;
    }
    String getCallingNumer(){
        return callingNumer;
    }
    String getAnswerNumer(){
        return answerNumer;
    }
}




class LandlinePhoneCharging  extends ChargeMode {
    double monthlyRent =20;
    public double calCost(UserRecords userRecords){
        double sum=0;
        for (CallRecord callRecord:userRecords.getCallingInCityRecords())
            sum+= new LandPhoneInCityRule().calCost(callRecord);
        for (CallRecord callRecord:userRecords.getCallingInLandRecords())
            sum+= new LandPhoneInLandRule().calCost(callRecord);
        for (CallRecord callRecord:userRecords.getCallingInProvinceRecords())
            sum+= new LandPhoneInProvinceRule().calCost(callRecord);

        return sum;
    }

    public double getMonthlyRent(){
        return monthlyRent;
    }
}


class LandPhoneInCityRule extends CallChargeRule{
    @Override
    public double calCost(CallRecord callRecord) {
        double sec = (callRecord.getEndTime().getTime() -callRecord.getStartTime().getTime())/ 1000;
        if (sec < 0) {
            return 0;
        }
        double minu = (int) sec / 60;
        if (sec % 60 != 0) {
            minu += 1;
        }

        return  minu * 0.1;
    }
}


 class LandPhoneInLandRule extends CallChargeRule{
  @Override
  public double calCost(CallRecord callRecord) {
     double sec = (callRecord.getEndTime().getTime() -callRecord.getStartTime().getTime())/ 1000;
     if (sec < 0) {
      return 0;
     }
     double minu = (int) sec / 60;
     if (sec % 60 != 0) {
      minu += 1;
     }

     return  minu * 0.6;
  }
}



class LandPhoneInProvinceRule extends CallChargeRule{
    @Override
    public double calCost(CallRecord callRecord) {
        double sec = (callRecord.getEndTime().getTime() -callRecord.getStartTime().getTime())/ 1000;
        if (sec < 0) {
            return 0;
        }
        double minu = (int) sec / 60;
        if (sec % 60 != 0) {
            minu += 1;
        }

        return  minu * 0.3;
    }
}


class MessageRecord extends CommunicationRecord{
    String message;
    String getMessage(){
        return message;
    }
    void setMessage(String message){
        this.message=message;
    }
}






class User {
    UserRecords userRecords = new UserRecords();
    double balance =100;
    ChargeMode chargeMode;
    String number;
    public User(){}
    public User(String number){
        this.number = number;
    }
    double calBalance(){
        return balance -chargeMode.getMonthlyRent() - chargeMode.calCost(userRecords);
    }
    double  calCost(){

        return chargeMode.calCost(userRecords);
    }
    UserRecords getUserRecords(){
        return userRecords;
    }
    void  setUserRecords(UserRecords userRecords){
        this.userRecords=userRecords;
    }

    double getBalance(){
        return balance;
    }
    ChargeMode getChargeMode(){
        return chargeMode;
    }
    void  setChargeMode(ChargeMode chargeMode){
        this.chargeMode=chargeMode;
    }
    String getNumber(){
        return number;
    }
    void setNumber(String number){
        this.number =number;
    }
}





class UserRecords {
    ArrayList<CallRecord> callinglnCityRecords= new ArrayList<CallRecord>();
    ArrayList<CallRecord> callinglnProvinceRecords= new ArrayList<CallRecord>();
    ArrayList<CallRecord> callinglnLandRecords= new ArrayList<CallRecord>();
    ArrayList<CallRecord> answerlnCityRecords= new ArrayList<CallRecord>();
    ArrayList<CallRecord> answerlnProvinceRecords= new ArrayList<CallRecord>();
    ArrayList<CallRecord> answerlnLandRecords= new ArrayList<CallRecord>();
    ArrayList<MessageRecord> sendMessageRecords= new ArrayList<MessageRecord>();
    ArrayList<MessageRecord> receiveMessageRecords= new ArrayList<MessageRecord>();
    void addCallingInCityRecords(CallRecord callRecord){
        callinglnCityRecords.add(callRecord);
    }
    void addCallingInProvinceRecords(CallRecord callRecord){
        callinglnProvinceRecords.add(callRecord);
    }
    void addCallingInLandRecords(CallRecord callRecord){
        callinglnLandRecords.add(callRecord);
    }
    void addAnswerInCityRecords(CallRecord callRecord){
        answerlnCityRecords.add(callRecord);
    }
    void addAnswerInProvinceRecords(CallRecord callRecord){
        answerlnProvinceRecords.add(callRecord);
    }
    void addAnswerInLandRecords(CallRecord callRecord){
        answerlnLandRecords.add(callRecord);
    }
     void addSendMessageRecords(MessageRecord messageRecord){
        sendMessageRecords.add(messageRecord);
     }
    void addReceiveMessageRecords(MessageRecord messageRecord){
        receiveMessageRecords.add(messageRecord);
    }
    ArrayList<CallRecord> getCallingInCityRecords(){
        return  callinglnCityRecords;
    }
    ArrayList<CallRecord> getCallingInProvinceRecords(){
        return  callinglnProvinceRecords;
    }
    ArrayList<CallRecord> getCallingInLandRecords(){
        return  callinglnLandRecords;
    }
    ArrayList<CallRecord> getAnswerInCityRecords(){
        return  answerlnCityRecords;
    }
    ArrayList<CallRecord> getAnswerInProvinceRecords(){
        return  answerlnProvinceRecords;
    }
    ArrayList<CallRecord> getAnswerInLandRecords(){
        return  answerlnLandRecords;
    }
    ArrayList<MessageRecord> getSendMessageRecords(){
        return  sendMessageRecords;
    }
    ArrayList<MessageRecord> getReceiveMessageRecords(){
        return  receiveMessageRecords;
    }
}

7-2 多态测试

定义容器Container接口。模拟实现一个容器类层次结构,并进行接口的实现、抽象方法重写和多态机制测试。各容器类实现求表面积、体积的方法。

    定义接口Container:
    属性:
    public static final double pi=3.1415926;
    抽象方法:
    public abstract double area();
    public abstract double volume();
    static double sumofArea(Container c[]);
    static double sumofVolume(Container c[]);
    其中两个静态方法分别计算返回容器数组中所有对象的面积之和、周长之和;
    定义Cube类、Cylinder类均实现自Container接口。
    Cube类(属性:边长double类型)、Cylinder类(属性:底圆半径、高,double类型)。

输入格式:

第一行n表示对象个数,对象类型用cube、cylinder区分,cube表示立方体对象,后面输入边长,输入cylinder表示圆柱体对象,后面是底圆半径、高。

输出格式:

分别输出所有容器对象的表面积之和、体积之和,结果保留小数点后2位。

代码如下


import java.util.Scanner;

interface Container {
    public static final double pi=3.1415926;
    //抽象方法:
    public abstract double area();
    public abstract double volume();
    static double sumofArea(Container c[],int n){
        double num=0;
        for(int i=1;i<=n;i++){
            num+=c[i].area();
      //      System.out.printf(i+" "+"a"+num+"\n");
        }
        return num;
    }
    static double sumofVolume(Container c[],int n){
        double num=0;
        for(int i=1;i<=n;i++) {
            num += c[i].volume();
         //   System.out.printf("v" + num + "\n");
        }
        return num;
    }
}



public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int n= input.nextInt();
        Shape []c = new Shape[1000];double a=0,b=0;
       // c[0] = new Cylinder();
        for(int i=1;i<=n;i++)
        {
            String ch= input.next();
           // System.out.printf(ch+"  \n");
            if(ch.equals("cube")){
                a = input.nextDouble();
                c[i] = new Cube(a);
         //       System.out.printf("114514-");
            }
            else if(ch.equals("cylinder")){
                a = input.nextDouble();
                b = input.nextDouble();
                c[i] = new Cylinder(a,b);
            //    System.out.printf("114532-");
            }
           // System.out.printf(i+" "+c[1].area()+" "+c[1].volume()+"\n");
        }

        System.out.printf("%.2f\n%.2f",Container.sumofArea(c,n),Container.sumofVolume(c,n));

    }
}


class Cylinder extends  Shape{
    public double a,b;
    public Cylinder(){

    }
    public Cylinder(double c,double d){
        a=c;
        b=d;
    }
    public double area() {
        return pi*a*a*2+2*pi*a*b;

    }
    public  double volume(){
        return pi*a*a*b;
    }
}



class Cube extends Shape{
    public  double a=0;

    public Cube(){

    }
    public Cube(double b){
        a=b;
    }
    public double area() {

        return a*a*6;
    }
    public  double volume(){
        return a*a*a;
    }

}


abstract class Shape implements Container{

}

很简单蛤,嘻嘻

posted on 2022-10-28 14:18  大皮QAQ  阅读(82)  评论(0编辑  收藏  举报

导航

回到顶部叭QAQ