大皮

去见、去学、去写。

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

倒数第二次作业了
还是熟悉的电费计时
话不多说,上代码

题目列表

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

实现南昌市电信分公司的计费程序,假设该公司针对手机和座机用户分别采取了两种计费方案,分别如下:
1、针对市内座机用户采用的计费方式(与电信计费系列1内容相同):
月租20元,接电话免费,市内拨打电话0.1元/分钟,省内长途0.3元/分钟,国内长途拨打0.6元/分钟。不足一分钟按一分钟计。
假设本市的区号:0791,江西省内各地市区号包括:0790~0799以及0701。
2、针对手机用户采用实时计费方式:
月租15元,市内省内接电话均免费,市内拨打市内电话0.1元/分钟,市内拨打省内电话0.2元/分钟,市内拨打省外电话0.3元/分钟,省内漫游打电话0.3元/分钟,省外漫游接听0.3元/分钟,省外漫游拨打0.6元/分钟;
注:被叫电话属于市内、省内还是国内由被叫电话的接听地点区号决定,比如以下案例中,南昌市手机用户13307912264在区号为020的广州接听了电话,主叫号码应被计算为拨打了一个省外长途,同时,手机用户13307912264也要被计算省外接听漫游费:
u-13307912264 1
t-079186330022 13307912264 020 2022.1.3 10:00:25 2022.1.3 10:05:11

输入

输入信息包括两种类型
1、逐行输入南昌市用户开户的信息,每行一个用户,含手机和座机用户
格式:u-号码 计费类型 (计费类型包括:0-座机 1-手机实时计费 2-手机A套餐)
例如:u-079186300001 0
座机号码由区号和电话号码拼接而成,电话号码包含7-8位数字,区号最高位是0。
手机号码由11位数字构成,最高位是1。
本题在电信计费系列1基础上增加类型1-手机实时计费。
手机设置0或者座机设置成1,此种错误可不做判断。
2、逐行输入本月某些用户的通讯信息,通讯信息格式:
座机呼叫座机:t-主叫号码 接听号码 起始时间 结束时间
t-079186330022 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:11
以上四项内容之间以一个英文空格分隔,
时间必须符合"yyyy.MM.dd HH:mm:ss"格式。提示:使用SimpleDateFormat类。
输入格式增加手机接打电话以及收发短信的格式,手机接打电话的信息除了号码之外需要额外记录拨打/接听的地点的区号,比如:
座机打手机:
t-主叫号码 接听号码 接听地点区号 起始时间 结束时间
t-079186330022 13305862264 020 2022.1.3 10:00:25 2022.1.3 10:05:11
手机互打:
t-主叫号码 拨号地点 接听号码 接听地点区号 起始时间 结束时间
t-18907910010 0791 13305862264 0371 2022.1.3 10:00:25 2022.1.3 10:05:11

注意:以上两类信息,先输入所有开户信息,再输入所有通讯信息,最后一行以“end”结束。

输出

根据输入的详细通讯信息,计算所有已开户的用户的当月费用(精确到小数点后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-1]$") || ch.matches("(^t-[0]{1}[0-9]{9,11} ((0[0-9]{9,11})|(1[0-9]{10} 0[0-9]{2,3})) [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])$)|(^t-1[0-9]{10} 0[0-9]{2,3} ((0[0-9]{9,11})|(1[0-9]{10} 0[0-9]{2,3})) [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],splitSet2[1]));
                                } 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,String ch2){
        User user = new User(ch);
        if(Objects.equals(ch2, "0"))
            user.chargeMode = new LandlinePhoneCharging();
        else if(Objects.equals(ch2, "1"))
             user.chargeMode = new MobilePhoneCharging();
        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(" ");

        if(splitSet.length==6) {
            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("0791")) {
                        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;
                }
            }
        }
        else if(splitSet.length==7){
            if(splitSet[1].length()>splitSet[2].length()){
                CallRecord callRecord = new CallRecord(splitSet[3], splitSet[4], splitSet[5], splitSet[6], splitSet[0].substring(0, 4), splitSet[2]);
                int cNum = judgePlace(splitSet[0].substring(0, 4)),aNum = judgePlace(splitSet[2]);
                for (User U : user) {
                    if (U.number.equals(splitSet[0])) {
                        if(aNum==1){
                              U.getUserRecords().addCallingInCityRecords(callRecord);
                        }
                        if(aNum==2){
                            U.getUserRecords().addCallingInProvinceRecords(callRecord);
                        }
                        if(aNum==0){
                            U.getUserRecords().addCallingInLandRecords(callRecord);
                            for (User U2 : user)
                                if (U2.number.equals(splitSet[1])){
                                    U2.getUserRecords().addAnswerRoamInLandRecords(callRecord);
                                    return user;
                                }
                        }
                        return user;

                    }
                }

            }
            else{
                CallRecord callRecord = new CallRecord(splitSet[3], splitSet[4], splitSet[5], splitSet[6], splitSet[1], splitSet[2].substring(0, 4));
                int cNum = judgePlace(splitSet[1]),aNum = judgePlace(splitSet[2].substring(0, 4));
                for (User U : user) {
                    if (U.number.equals(splitSet[0])) {
                        if(cNum == 0){
                            U.getUserRecords().addCallingRoamInLandRecords(callRecord);
                        }
                        if(cNum == 1){
                            if(aNum ==0){
                                U.getUserRecords().addCallingInLandRecords(callRecord);
                            }
                            else if(aNum ==1){
                                U.getUserRecords().addCallingInCityRecords(callRecord);
                            }
                            else {
                                U.getUserRecords().addCallingInProvinceRecords(callRecord);
                            }
                        }
                        if(cNum == 2){
                            U.getUserRecords().addCallingRoamInProvinceRecords(callRecord);
                        }
                    }
                }
            }
        }
        else if(splitSet.length==8){
            CallRecord callRecord = new CallRecord(splitSet[4], splitSet[5], splitSet[6], splitSet[7], splitSet[1], splitSet[3]);
            int cNum = judgePlace(splitSet[1]),aNum = judgePlace(splitSet[3]);
            for (User U : user) {
                if (U.number.equals(splitSet[0])) {
                    if(cNum == 0){
                        U.getUserRecords().addCallingRoamInLandRecords(callRecord);
                    }
                    if(cNum == 1){
                        if(aNum ==0){
                            U.getUserRecords().addCallingInLandRecords(callRecord);
                        }
                        else if(aNum ==1){
                            U.getUserRecords().addCallingInCityRecords(callRecord);
                        }
                        else {
                            U.getUserRecords().addCallingInProvinceRecords(callRecord);
                        }
                    }
                  //  System.out.printf(aNum+"\n");
                    if(cNum == 2){
                        U.getUserRecords().addCallingRoamInProvinceRecords(callRecord);
                    }
                    if(aNum == 0){
                        for (User U2 : user)
                            if (U2.number.equals(splitSet[2])){
                               // System.out.printf("111");
                                U2.getUserRecords().addAnswerRoamInLandRecords(callRecord);
                                return user;
                            }
                    }
                    return user;
                }
            }
        }
        return user;
}
    User judgeChargeforAnswering(User user,CallRecord callRecord){


        return user;
    }
   static int judgePlace(String ch){
        if (ch.equals("0791")) {
            return 1;
        }
        for (String tel2 : tel) {
            if (ch.equals(tel2)) {
                return 2;
            }
        }
        return 0;

    }

}








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 MobilePhoneCharging extends ChargeMode {

    private double monthlyRent = 15;


    @Override
    public double calCost(UserRecords userRecords) {
        double sum = 0;
        for (CallRecord callRecord:userRecords.getCallingInCityRecords())
            sum+= new MobilePhoneInlandRule().calCost(callRecord);
        for (CallRecord callRecord:userRecords.getCallingInLandRecords())
            sum+= new MobilePhoneInlandRule().calCost(callRecord);
        for (CallRecord callRecord:userRecords.getCallingInProvinceRecords())
            sum+= new MobilePhoneInProvinceRule().calCost(callRecord);
        for(CallRecord callRecord:userRecords.getCallingroamlnProvinceRecords())
            sum+= new MobilePhoneRoamInProvinceRule().calCost(callRecord);
        for(CallRecord callRecord:userRecords.getCallingroamlnLandRecords())
            sum+= new MobilePhoneRoamInLandRule().calCost(callRecord);
        for(CallRecord callRecord:userRecords.getAnswerroamlnLandRecords())
            sum+= new MobilePhoneRoamInLandRule().calCostans(callRecord);
        return sum;
    }

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

}

class MobilePhoneInCityRule extends CallChargeRule{
    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 MobilePhoneInlandRule extends CallChargeRule{
    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 MobilePhoneInProvinceRule extends CallChargeRule{
    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.2;
    }

}

 class MobilePhoneRoamInLandRule extends CallChargeRule{
    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;
    }
    public double calCostans(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 MobilePhoneRoamInProvinceRule extends CallChargeRule{
    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 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> callingroamlnProvinceRecords= new ArrayList<CallRecord>();
    ArrayList<CallRecord> callingroamlnLandRecords= new ArrayList<CallRecord>();
    ArrayList<CallRecord> answerlnCityRecords= new ArrayList<CallRecord>();
    ArrayList<CallRecord> answerlnProvinceRecords= new ArrayList<CallRecord>();
    ArrayList<CallRecord> answerlnLandRecords= new ArrayList<CallRecord>();
    ArrayList<CallRecord> answerroamlnProvinceRecords= new ArrayList<CallRecord>();
    ArrayList<CallRecord> answerroamlnLandRecords= 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 addCallingRoamInProvinceRecords(CallRecord callRecord){
        callingroamlnProvinceRecords.add(callRecord);
    }
    void addCallingRoamInLandRecords(CallRecord callRecord){
        callingroamlnLandRecords.add(callRecord);
    }
    void addAnswerInCityRecords(CallRecord callRecord){
        answerlnCityRecords.add(callRecord);
    }
    void addAnswerInProvinceRecords(CallRecord callRecord){
        answerlnProvinceRecords.add(callRecord);
    }
    void addAnswerInLandRecords(CallRecord callRecord){
        answerlnLandRecords.add(callRecord);
    }
    void addAnswerRoamInProvinceRecords(CallRecord callRecord){
        answerroamlnProvinceRecords.add(callRecord);
    }
    void addAnswerRoamInLandRecords(CallRecord callRecord){
        answerroamlnLandRecords.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> getCallingroamlnProvinceRecords(){return callingroamlnProvinceRecords;}
    ArrayList<CallRecord> getCallingroamlnLandRecords(){return callingroamlnLandRecords;}
    ArrayList<CallRecord> getAnswerroamlnProvinceRecords(){return answerroamlnProvinceRecords;}
    ArrayList<CallRecord> getAnswerroamlnLandRecords(){return answerroamlnLandRecords;}

    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 sdut-Collection-sort--C~K的班级(II)

经过不懈的努力,C~K终于当上了班主任。

现在他要统计班里学生的名单,但是C~K在教务系统中导出班级名单时出了问题,发现会有同学的信息重复,现在他想把重复的同学信息删掉,只保留一个,
但是工作量太大了,所以找到了会编程的你,你能帮他解决这个问题吗?

输入格式:

第一行输入一个N,代表C~K导出的名单共有N行(N<100000).

接下来的N行,每一行包括一个同学的信息,学号 姓名 年龄 性别。

输出格式:

第一行输出一个n,代表删除重复名字后C~K的班级共有几人。

接下来的n行,输出每一个同学的信息,输出按照学号从小到大的顺序。


import java.util.*;


//主函数
public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<String> c= new ArrayList<String>(); ;
        int t = sc.nextInt();
        sc.nextLine();
        for (int i = 0; i < t; i++) {

            String employeeName = sc.nextLine();
            if(!c.contains(employeeName))
                c.add(employeeName);
        }

        c.sort(new Comparator<String>() {

            @Override
            public int compare(String s1, String s2) {
                return Integer.parseInt(s1.substring(0, 4))-Integer.parseInt(s2.substring(0, 4));
            }
        });

        // 2、创建迭代器遍历集合
        Iterator it =c.iterator();
        System.out.printf(c.size()+"\n");
        //3、遍历
        for (String ch:c) {

            System.out.println(ch);
        }
    }

}

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

功能需求:
      使用集合存储3个员工的信息(有序);
      通过迭代器依次找出所有的员工。

提示:学生复制以下代码到编程区,并按需求进行调试修改。
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Scanner;

//定义员工类
class Employee {

    private String name;
    private int age;

    public Employee() {
        super();
    }

    public Employee(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

//主函数
public class Main {

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

        // 创建3个员工元素对象
        for (int i = 0; i < 3; i++) {
            Scanner sc = new Scanner(System.in);
            String employeeName = sc.next();
            int employeeAge = sc.nextInt();
            //System.out.printf("1\n");
            Employee employee = new Employee(employeeName, employeeAge);
            c.add(employee);
        }



        // 2、创建迭代器遍历集合
        Iterator it =c.iterator();

        //3、遍历
        while (it.hasNext()) {

            //4、集合中对象未知,向下转型
            Employee e = (Employee) it.next();

            System.out.println(e.getName() + "---" + e.getAge());
        }
    }

}

posted on 2022-12-10 18:55  大皮QAQ  阅读(129)  评论(0编辑  收藏  举报

导航

回到顶部叭QAQ