📄 userdirectory.java
字号:
package registerapp;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.util.*;
import java.net.URL;
import net.yefei.cmn.user.*;
/*
* @author ye fei
* @version 1.00, 2003/04/15
* @copyright www.studyjava.com
*/
public class UserDirectory {
private User user=null;
/**
*It a static vector, all user info in it.
*/
private static Vector alluserinfo=null;
/**
*
*/
private static User userin=null;
/**
*User Data object.
*/
private static UserData userdata=null;
/**
*UserDirectory construction
*/
public UserDirectory() throws UserDirectoryException {
try{
userin=new User();
alluserinfo= userin.ListUserInfo();
}
catch (Exception e) {
throw new UserDirectoryException();
}
}
/**
* Transform id so that it will match any conventions used by user
* directory. The default implementation forces the id to
* uppercase. Does <b>not</b> expect the userId to be null and
* will throw a NPE if it is.
*
* @exception Throws Null Pointer Exception if userId is null.
*/
public String fixId(String userId) {
return userId.toUpperCase();
}
/**
*Check the Password,if Password is Valid return true
*/
public boolean isValidPassword(String userId, String password) {
// no null passwords
if (null==password) return false;
// conform userId to uppercase
String _userId = userId;
// no passwords for non-users
if (!isUserExist(_userId)) return false;
// does password match user's password
return (password.equals(getPassword(password)));
}
/**
*Check the Admin,if it an admin return true
*/
public boolean isAdmin(String userId, String password) {
// no null passwords
if (null==password ||userId==null) return false;
if (userId.equalsIgnoreCase("admin"))
if (password.equalsIgnoreCase("admin"))
return true;
return false;
}
/**
*Check the user,if user exist return true
*/
public boolean isUserExist(String userId) {
// no null users
if (null==userId) return false;
Enumeration enu=alluserinfo.elements();
while (enu.hasMoreElements())
{
UserData userdata = (UserData) enu.nextElement();
// if not null, it's a user
if (userdata.getUserName().equalsIgnoreCase(userId))
return true;
}
return false;
}
/**
* get Password
*/
public String getPassword(String userId) {
Enumeration enu=alluserinfo.elements();
String pass=null;
while (enu.hasMoreElements())
{
UserData userdata = (UserData) enu.nextElement();
if (userdata.getPassword().equalsIgnoreCase(userId))
pass= userdata.getPassword();
}
return pass;
}
/**
*get User Ids
*/
public Enumeration getUserIds() {
return alluserinfo.elements();
}
/**
*setUser(username,password1,UserSex,Email);
*/
public void setUser(String password,String UserName, String UserSex,String Email) throws
UserDirectoryException {
// no nulls
if ((null==UserName) || (null==password)) {
throw new UserDirectoryException();
}
try {
userin.InsertUserInfo(password,UserName,UserSex,Email);
/* conform userId to uppercase when stored
p.put(fixId(userId), password);
String o = this.getClass().getClassLoader().getResource(UserDirectoryFile).getFile();
p.store(new FileOutputStream(o), UserDirectoryHeader);*/
System.out.println("Succeed Insert User Info!!!");
}
catch (Exception e) {
throw new UserDirectoryException();
}
}
} // end UserDirectory
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -