Untitled

 avatar
unknown
plain_text
4 years ago
3.0 kB
10
Indexable
package com.company;


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

class NhanVien implements Comparable<NhanVien> {

    final private String name;
    final private String gender;
    final private String DoB;
    final private String address;
    final private String tax_code;
    final private String contract_date;
    final private String ID;
    private int birth_year;
    private int birth_month;
    private int birth_day;

    public NhanVien(String name, String gender, String DoB, String address,
                    String tax_code, String contract_date, String stt) {
        this.name = name;
        this.gender = gender;
        this.DoB = this.chuan_hoa_date(DoB);
        this.address = address;
        this.tax_code = tax_code;
        this.contract_date = chuan_hoa_date(contract_date);
        this.ID = stt;
        birth_month = 0;
        birth_day = 0;
        birth_year = 0;
        split_DoB();
    }

    @Override
    public int compareTo(NhanVien nv) {
        int year_cmp = Integer.compare(this.birth_year, nv.birth_year);
        if (year_cmp == 0) {
            int month_cmp = Integer.compare(this.birth_month, nv.birth_month);
            if (month_cmp == 0) {
                return Integer.compare(this.birth_day, nv.birth_day);
            } else
                return month_cmp;
        } else return year_cmp;
    }

    private String chuan_hoa_date(String Date) {
        if (Date.charAt(1) == '/') Date = "0" + Date;
        if (Date.charAt(4) == '/') Date = Date.substring(0, 3) + "0" + Date.substring(3);
        return Date;
    }

    private void split_DoB() {
        birth_day = Integer.parseInt(this.DoB.substring(0, 2));
        birth_month = Integer.parseInt(this.DoB.substring(3, 5));
        birth_year = Integer.parseInt(this.DoB.substring(6));
    }


    public void print_info() {
        System.out.print(this.ID + " ");
        System.out.print(this.name + " ");
        System.out.print(this.gender + " ");
        System.out.print(this.DoB + " ");
        System.out.print(this.address + " ");
        System.out.print(this.tax_code + " ");
        System.out.print(this.contract_date + " ");
        System.out.println();
    }
}

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num_of_sv = sc.nextInt();
        ArrayList<NhanVien> nv_list = new ArrayList<>();
        sc.nextLine();
        for (int i = 1; i <= num_of_sv; i++) {
            String stt = Integer.toString(i);
            while (stt.length() < 5)
                stt = "0" + stt;
            NhanVien nv = new NhanVien(sc.nextLine(), sc.nextLine(), sc.nextLine(), sc.nextLine(), sc.nextLine(),
                    sc.nextLine(), stt);
            nv_list.add(nv);
        }
        Collections.sort(nv_list);

        for (int i = 0; i < num_of_sv; i++) {
            nv_list.get(i).print_info();
        }
    }
}
Editor is loading...