public class NameException extends Exception {
public NameException() {
super("The name cannot be an integer.");
}
public NameException(String message) {
super(message);
}
}
public class AgeException extends Exception{
public AgeException() {
super("Age is too high.");
}
public AgeException(String message) {
super(message);
}
}
public class Employee {
private String name;
private int age;
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
import java.util.Scanner;
public class Main {
public static Employee createEmployee(String name, int age) throws NameException, AgeException {
if (age > 50) {
throw new AgeException();
}
try {
Double.parseDouble(name);
throw new NameException();
} catch (NumberFormatException e) {
}
return new Employee(name, age);
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String name = in.nextLine();
int age = Integer.parseInt(in.nextLine());
try {
createEmployee(name,age);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Comments
Thank you so much!!!
Leave a comment