📄 userlogin.java
字号:
/*
* Author: Naizheng Bian
* Version: 1.0
* Date: 12/09/2001
*/
package serverPackage;
import java.io.*;
import java.util.*;
import mediaPackage.Constants;
//import mediaPackage.*;
//import serverPackage.*;
/**
* This class handles the user login
*/
class UserLogin {
/** The information about the users */
Hashtable mUserInfoTable = null;
/** The file output stream to write to a file */
FileOutputStream mFileOutStream = null;
/** The object output stream for writing to a file */
ObjectOutputStream mObjectOutStream = null;
/** The variable indicating if the stream is open or not */
boolean mOpenStream;
/**
* The default Constructor for initialization of the FileInputStream
*/
public UserLogin() {
mUserInfoTable = new Hashtable();
ObjectInputStream objInStream = null;
try {
objInStream = new ObjectInputStream(new FileInputStream("userInfo.data"));
UserInfo u = (UserInfo) objInStream.readObject();
while (u != null) {
mUserInfoTable.put(u.getUserName(),u);
u = (UserInfo) objInStream.readObject();
}
}catch (EOFException e) {}
catch (FileNotFoundException e) {}
catch (IOException e) {
System.err.println("userLogin:getUserInfo:IOException " + e.getMessage());
}
catch (Exception e) {
System.err.println("userLogin:getUserInfo:Exception " + e.getMessage());
}
try { if (objInStream != null) objInStream.close(); }
catch (IOException ie) {}
mOpenStream = false;
}
/**
* This method gets the user password for the given username
* If the user is not present in the file then null is returned
*
* @param userName Name of the user whose password has to be returned
* @return The user Information
*/
private UserInfo getUserInfo(String userName) {
UserInfo info = null;
info = (UserInfo) mUserInfoTable.get(userName);
return info;
}
/**
* This method verifies the password of the user and returns true if the user is
* allowed to login
*
* @param userName The user's name
* @param password The user's password
* @return <code>true</code> If the password is correct
* <code>false</code> If the password is incorrect
*/
public UserInfo loginUser(String userName, String password ,int category) {
UserInfo uInfo = getUserInfo(userName);
if ((uInfo != null) && ((uInfo.getUserPassword()).equals(password))&&(category==Constants.USER||category==uInfo.getCategory())) {
return uInfo;
}
return null;
}
/**
* This method adds a new user
* @param uInfo The information about the new user
* @return true, if the new information is added, otherwise returns false
*/
private boolean addUser(UserInfo uInfo) {
Object o = mUserInfoTable.put(uInfo.getUserName(),uInfo);
if (o != null) return false;
if (!mOpenStream) {
try {
mFileOutStream = new FileOutputStream("userInfo.data");
mObjectOutStream = new ObjectOutputStream(mFileOutStream);
Enumeration e = mUserInfoTable.elements();
while (e.hasMoreElements()) {
UserInfo usInfo = (UserInfo) e.nextElement();
mObjectOutStream.writeObject(usInfo);
}
}
catch (IOException e) {
System.err.println("userLogin:addUser:IOException " + e.getMessage());
}
catch (Exception e) {
System.err.println("userLogin:addUser:Exception " + e.getMessage());
}
mOpenStream = true;
}
try {
mObjectOutStream.writeObject(uInfo);
mObjectOutStream.flush();
}
catch (IOException e) {
System.err.println("userLogin:addUser:IOException " + e.getMessage());
return false;
}
catch (Exception e) {
System.err.println("userLogin:addUser:Exception " + e.getMessage());
return false;
}
return true;
}
/**
* This method registers a new user and returns true if the user is
* registered successfully
*
* @param userName The name of the user
* @param password The password of the user
* @return true If the user is registered succcessfully
* false If the user cannot be registered
*/
public boolean registerUser(UserInfo usInfo) {
UserInfo uInfo = getUserInfo(usInfo.getUserName());
if (uInfo != null) {
return false;
}
return addUser(usInfo);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -