By GokiSoft.com| 15:17 30/01/2023|
Java Advanced

Bài ôn luyện Java 2 - C2109L

I) Exception

Tạo class object Survey gồm các fields sau:

- fullname -> String

- age -> int

- email -> String

- address -> String

- note -> String

Yêu cầu tạo 1 hàm tạo đầy đủ tham số và getter/setter

Hàm tạo cần kiểm tra các điều kiện sau:

- fullname chỉ gồm các ký tự a-zA-Z và dấu cách. -> Sai yêu cầu -> throw exception FullnameFormatException

- age > 0 -> sai thì throw exception AgeFormatException

- email -> chứa ký tự @ -> Sai thì throw exception EmailFormatException

Tạo class Test -> phương thức main

- Tạo 4 đối tượng để test đủ 4 trường hợp trên -> Tạo ra lỗi FullnameFormatException, AgeFormatException, EmailFormatException và trường hợp thành công.

- Yêu cầu không sử dụng Scanner nhập thông tin fullname, age, email, address, note: được nhập từ hàm main

Source Code

#AgeFormatException.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package java2.overview.lesson03;

/**
 *
 * @author diepvan
 */
public class AgeFormatException extends Exception{
    
}

#EmailFormatException.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package java2.overview.lesson03;

/**
 *
 * @author diepvan
 */
public class EmailFormatException extends Exception{
    
}

#FullnameFormatException.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package java2.overview.lesson03;

/**
 *
 * @author diepvan
 */
public class FullnameFormatException extends Exception{
    
}

#Main.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package java2.overview.lesson03;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author diepvan
 */
public class Main {

    public static void main(String[] args) {
        Survey survey;
        try {
            survey = new Survey("Tran@Van A", 20, "a@gmail.com", "Ha Noi", "OKOK");
            System.out.println(survey);
        } catch (AgeFormatException ex) {
            System.out.println("Age is not correct");
        } catch (EmailFormatException ex) {
            System.out.println("Email is not correct");
        } catch (FullnameFormatException ex) {
            System.out.println("Fullname is not correct");
        }

        try {
            survey = new Survey("Tran Van A", -20, "a@gmail.com", "Ha Noi", "OKOK");
            System.out.println(survey);
        } catch (AgeFormatException ex) {
            System.out.println("Age is not correct");
        } catch (EmailFormatException ex) {
            System.out.println("Email is not correct");
        } catch (FullnameFormatException ex) {
            System.out.println("Fullname is not correct");
        }

        try {
            survey = new Survey("Tran Van A", 20, "agmail.com", "Ha Noi", "OKOK");
            System.out.println(survey);
        } catch (AgeFormatException ex) {
            System.out.println("Age is not correct");
        } catch (EmailFormatException ex) {
            System.out.println("Email is not correct");
        } catch (FullnameFormatException ex) {
            System.out.println("Fullname is not correct");
        }

        try {
            survey = new Survey("Tran Van A", 20, "a@gmail.com", "Ha Noi", "OKOK");
            System.out.println(survey);
        } catch (AgeFormatException ex) {
            System.out.println("Age is not correct");
        } catch (EmailFormatException ex) {
            System.out.println("Email is not correct");
        } catch (FullnameFormatException ex) {
            System.out.println("Fullname is not correct");
        }

        //Nhap du lieu
        String fullname, email, address, note;
        int age;

        InputStreamReader reader = new InputStreamReader(System.in);
        BufferedReader bufferedReader = new BufferedReader(reader);

        try {
            System.out.println("Nhap ten: ");
            fullname = bufferedReader.readLine();

            System.out.println("Nhap email: ");
            email = bufferedReader.readLine();

            System.out.println("Nhap dia chi: ");
            address = bufferedReader.readLine();

            System.out.println("Nhap ghi chu: ");
            note = bufferedReader.readLine();

            System.out.println("Nhap tuoi: ");
            age = Integer.parseInt(bufferedReader.readLine());

            System.out.println("fullname = " + fullname + ", email = " + email + ", address = " + address
                    + ", note = " + note + ", age = " + age);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

#Survey.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package java2.overview.lesson03;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author diepvan
 */
public class Survey {
    String fullname;
    int age;
    String address;
    String email;
    String note;

    public Survey(String fullname, int age, String email, String address, String note) throws AgeFormatException, EmailFormatException, FullnameFormatException {
        Pattern pattern = Pattern.compile("^[a-zA-Z ]{3,}$");
        Matcher matcher = pattern.matcher(fullname);
        boolean matchFound = matcher.find();
        if(!matchFound) {
            throw new FullnameFormatException();
        }

        this.fullname = fullname;
        
        if(age <= 0) {
            throw new AgeFormatException();
        }
        this.age = age;
        
        if(!email.contains("@")) {
            throw new EmailFormatException();
        }
        this.email = email;
        this.address = address;
        this.note = note;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public int getAge() {
        return age;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "Survey{" + "fullname=" + fullname + ", age=" + age + ", address=" + address + ", email=" + email + ", note=" + note + '}';
    }
}
Tags:



Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó