[java]代码库
FieldRequiredException.java
package Experience;
public class FieldRequiredException extends Exception{
public FieldRequiredException(String who) {
super(who+"不能为空!");
}
}
WrongPasswordException.java
package Experience;
public class WrongPasswordException extends Exception{
public WrongPasswordException() {
super("密码错误!");
}
}
UserNameNotFoundException.java
package Experience;
public class UserNameNotFoundException extends Exception{
public UserNameNotFoundException() {
super("用户名不存在!");
}
}
LoginService.java
package Experience;
public class LoginService {
public void login(String UserName,String password)
throws FieldRequiredException,WrongPasswordException,
UserNameNotFoundException{
if (UserName==null||UserName.trim().length()<1) {
throw new FieldRequiredException("用户名");
}
if (password==null||password.length()<1) {
throw new FieldRequiredException("密码");
}
if (UserName.trim().equals("admin")) {
if (password.equals("admin")) {
return;
}else {
throw new WrongPasswordException();
}
}else {
throw new UserNameNotFoundException();
}
}
}
LoginClient.java
package Experience;
import java.util.Scanner;
public class LoginClient {
public static void main(String[] args) {
try {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入用户名:");
String UserName=scanner.nextLine();
System.out.println("请输入密码:");
String password=scanner.nextLine();
LoginService loginService=new LoginService();
loginService.login(UserName, password);
} catch (FieldRequiredException e) {
System.err.println(e.getMessage());
} catch (WrongPasswordException e) {
System.err.println(e.getMessage());
} catch (UserNameNotFoundException e) {
System.err.println(e.getMessage());
}
}
}
中级程序员
by: 东城 发表于:2016-05-12 09:42:04 顶(0) | 踩(0) 回复
回复评论