Import Java. util. hashmap; import Java. util. map; public class loginservice {private Map <string, string> map; Public loginservice () {map = new hashmap <string, string> (); map. put ("Zhang San", "123"); map. put ("Li Si", "123"); map. put ("", "123"); map. put ("Zhao six", "1234"); map. put ("Sun Qi", "1234");} public Boolean login (string name, string password) throws nouserexception, errorpswdexception {If (map. containskey (name) {string pswd = map. get (name); If (pswd. equals (password) = false) {Throw new errorpswdexception ("User Name" + name + "password is not" + password );}} else {Throw new nouserexception ("this user name is not found in dB");} return true;} public static void main (string [] ARGs) {loginservice service = new loginservice (); string name = "zhangsan 1"; string pswd = "1235"; try {Boolean passed = service. login (name, pswd); system. out. println ("welcome" + name + "to log on to the system. ");} catch (nullpointerexception e) {system. out. println ("the database is not ready. Please notify the system administrator. ");} catch (nouserexception e) {system. out. println ("username" + name + "does not exist, coming soon to the registration page");} catch (errorpswdexception e) {system. out. println ("user's" + name + "password" + pswd + "incorrect, please specify it before entering");} catch (exception e) {e. printstacktrace ();}}}
Two exception classes:
public class ErrorPswdException extends Exception{ public ErrorPswdException(String msg){ super(msg); }}
public class NoUserException extends Exception{ public NoUserException(String msg){ super(msg); }}