一、
编写注册方法,判断注册的参数mobile是11位数字,否则抛出异常IllegalMobileException,调用者捕获异常并提示"手机不合法"。
SignupDemo.java:
public class SignupDemo {
public static void main(String[] args) {
String mobile = "12121121";
String pass = "121212";
try {
boolean f = signup(mobile, pass);
} catch (IllegalMobileException e) {
System.out.println("手机不合法");
}
}
public static boolean signup(String mobile, String pass)
throws IllegalMobileException {
if (mobile.length() != 11) {
throw new IllegalMobileException();
}
return false;
}
}
IllegalMobileException.java:
public class IllegalMobileException extends Exception {
public IllegalMobileException() {
super();
}
public IllegalMobileException(String msg) {
super(msg);
}
}
二、学生登录,如果学号和密码不匹配,显示登录不成功,否则显示登录成功;另外,如果学号不是10位数字抛出自定义异常。IllegalStudentNumException继承运行时异常RuntimeException。
Student.java:
public class Student{
public Student(){}
public Student(String studentNo,String pass){
this.studentNo = studentNo;
this.pass = pass;
}
private String studentNo;
private String pass;
public String getStudentNo() {
return studentNo;
}
public String getPass() {
return pass;
}
public boolean equals(Object obj) {
return super.equals(obj);
}
}
SigninDemo.java:
public class SigninDemo {
static List<Student> students=Arrays.asList(
new Student("2015010101","1223"),
new Student("2015010102","1234"),
new Student("2015010112","2312")
);
public static void main(String[] args) {
String mobile="12121121";
String pass="121212";
try {
if(signin(mobile,pass)){
System.out.println("成功登陆");
}else{
System.out.println("学号或密码不正确");
}
} catch (IllegalStudentNumException e) {
System.out.println("学号格式不合法");
}
}
public static boolean checkStuNo(String stuno)
throws IllegalStudentNumException{
if (stuno.length() != 10) {
throw new IllegalStudentNumException();
}
return true;
}
private static boolean signin(String stuno, String pass)
throws IllegalStudentNumException{
//判断stuno 为10位数字 不为十位数字抛出异常
if(checkStuNo(stuno)){
Student stu=new Student(stuno,pass);
return students.contains(stu);
}
return false;
}
}
IllegalStudentNumException.java:
public class IllegalStudentNumException extends RuntimeException{
public IllegalStudentNumException(){
super();
}
public IllegalStudentNumException(String msg){
super(msg);
}
}
三、在集合中存储着三个用户,分别为:"张三", "1234";"李四", "1232";"王五", "1231"。现实现控制台登录功能:
(1)密码正确:登录成功,任意键退出;
(2)用户名存在,密码错误:提示重新登录;
(3)用户名不存在,抛出自定义的NoUserFoundException,接收此异常后提示用户名不存在。
UserLogin.java:
public class UserLogin {
// 初始化用户信息
static UserInfo[] map = new UserInfo[3];
static {
map[0] = new UserInfo("张三", "1234");
map[1] = new UserInfo("李四", "1232");
map[2] = new UserInfo("王五", "1231");
}
public static void main(String[] args) {
// main中捕获异常进行处理提示:用户名不存在
boolean isExit = false;
String username = "李四";
String pass = "1232";
try {
isExit = login(username, pass);
if (isExit) {
System.out.println("登录成功");
} else {
System.out.println("密码错误");
}
} catch (NoUserFoundException e) {
System.out.println("用户名不存在");
}
}
public static boolean login(String username, String pass) throws NoUserFoundException {
int i = containsUser(username);
if (pass.equals(map[i].getPassword())) {
return true;
} else {
return false;
}
}
public static int containsUser(String username) throws NoUserFoundException {
for (int i = 0; i < map.length; i++) {
if (username.equals(map[i].getUsername())) {
return i;
}
}
throw new NoUserFoundException();
}
}
UserInfo.java:
public class UserInfo {
private String username;
private String password;
public UserInfo() {
}
public UserInfo(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "username:" + this.getUsername() + ";password:"
+ this.getPassword();
}
}
NoUserFoundException.java:
public class NoUserFoundException extends Exception{
public NoUserFoundException() {
super();
}
public NoUserFoundException(String msg) {
super(msg);
}
}