newbie cần giúp về tạo Exception

AnhHoang

New Member
24/10/21
1
0
1
Hà Nội
Em có bài tập như hình bên dưới và đã hoàn thành phần bắt Exception liên quan tới nhập liệu còn phần định nghĩa 1 Exception mong mọi người giúp em ạ ( nếu có code mẫu thì em cảm ơn ạ)
screenshot_1634781523.png

dưới đây là phần code mà em đã làm đc
Class Student
Java:
public class Student{
    private int id;
    private LocalDate dob;
    private String name;

    public Student() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public LocalDate getDob() {
        return dob;
    }

    public void setDob(LocalDate dob) {
        this.dob = dob;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public int hashCode() {
        int hash = 5;
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Student other = (Student) obj;
        if (this.id != other.id) {
            return false;
        }
        return true;
    }
    
    

    @Override
    public String toString() {
        return "Student{" + "id=" + id + ", dob=" + dob + ", name=" + name + "}\n";
    }

    public void Input(){
        while (true) {           
            try {
                id = Validator.getInteger("Enter id: ", 0, Integer.MAX_VALUE);
                break;
            } catch (Exception e) {
                System.out.println(e.getMessage());
                System.out.println("Retype");
            }
        }
        
        try {
            name = Validator.getString("Enter name: ", 2, 100);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            System.out.println("-> name gets default value");
            name = "Student_" + id;
        }
        
        try {
            dob = Validator.getLocalDate("Enter dob: ", LocalDate.now().minusYears(80), LocalDate.now().minusYears(17), "dd/MM/yyyy");
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            System.out.println("-> empty info");
        }
    }
}
Class ArrayStudent
Java:
public class StudentAr {
    ArrayList<Student> st;
    Scanner sc = new Scanner(System.in);

    public StudentAr() {
        st = new ArrayList<>();
    }
    
    public void Input(){
        int count = 1;
        while(count == 1){
            Student s = new Student();

            s.Input();
            st.add(s);
            System.out.println("Information student have been added");
            try {
                count = Validator.getInteger("Add new student. 0_stop, 1_continue", 0, 1);
            } catch (Exception ex) {
                count = 0;
            }
        }
    }

    @Override
    public String toString() {
        return "StudentList{" + st + '}';
    }
    
    
    
    public void SortByID(){
        Collections.sort(st, new SortByID());
        for (int i = 0; i < st.size(); i++) {
            System.out.println(st);
            break;
        }
    }
    
    public void SortByName(){
        Collections.sort(st, new SortByName());
        for (int i = 0; i < st.size(); i++) {
            System.out.println(st);
            break;
        }
    }
    
    public void SortByDob(){
        Collections.sort(st, new SortByDob());
        for (int i = 0; i < st.size(); i++) {
            System.out.println(st);
            break;
        }
    }
}
Validator
Java:
public class Validator {
    public static Scanner sc = new Scanner(System.in);
    
    public static int getInteger(String mess, int min, int max) throws Exception{
        System.out.println(mess);
        int i = 0;
        try {
            i = sc.nextInt();
            sc.nextLine();
        } catch (InputMismatchException e) {
            sc.nextLine();
            throw e;
        }
        if (i< min || i > max) {
            throw new Exception("data is out of the requested range");
        }
        return i;
    }
    
    public static String getString(String mess, int minLength, int maxLength) throws Exception{
        System.out.println(mess);
        String s = sc.nextLine();
        if (s.length() < minLength || s.length() > maxLength) {
            throw new Exception("string length does not match");
        }
        return s;
    }
    
    public static LocalDate getLocalDate(String mess, LocalDate min, LocalDate max, String pattern) throws Exception{
        System.out.println(mess);
        String s = sc.nextLine();
        LocalDate dob = LocalDate.parse(s, DateTimeFormatter.ofPattern(pattern));
        if (dob.compareTo(min) < 0 || dob.compareTo(max) > 0) {
            throw new Exception("import date not working in requested range");
        }
        return dob;
    }
}
Sort By Id
Java:
public class SortByID implements Comparator<Student>{
    
    @Override
    public int compare(Student t1, Student t2) {
        if (t1.getId() > t2.getId()) {
            return 1;
        } else if (t1.getId() < t2.getId()){
            return -1;
        } else {
            return 0;
        }
    }
}
SortByName
Java:
public class SortByName implements Comparator<Student>{
        @Override
        public int compare(Student t1, Student t2){
        if (t1.getName().compareTo(t2.getName()) > 0) {
            return 1;
        } else if (t1.getName().compareTo(t2.getName()) < 0) {
            return -1;
        } else {
            return 0;
        }
    }
}
SortByDob
Java:
public class SortByDob  implements Comparator<Student>{

    @Override
    public int compare(Student t1, Student t2) {
        return (t2.getDob().compareTo(t1.getDob()));
    }

}
Class Main
Java:
public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        StudentAr sa = new StudentAr();
        sa.Input();
        System.out.println(sa);
        int sum;
        System.out.println("1. Sort by id");
        System.out.println("2. Sort by name");
        System.out.println("3. Sort by dob");
        System.out.print("Enter TC(1,2,3): ");
        sum = sc.nextInt();
        switch(sum){
            case 1:{
                sa.SortByID();
                break;
            }
            case 2:{
                sa.SortByName();
                break;
            }
            case 3:{
                sa.SortByDob();
                break;
            }
        }
    }
}