📄 userregistry.java
字号:
package com.jsfcompref.trainer;
import java.util.ArrayList;
import java.util.List;
/**
* UserRegistry.java serves as the POJO that manages a List of registered users
* for the Virtual Trainer application.
*
* This Java demo version is not persisted and creates a set of demo users
* upon instantiation.
*
* Like TrainingEventRegistry.java it is also registered as an application scoped
* managed bean.
*/
public class UserRegistry
{
private List RegisteredUsers = new ArrayList();
public UserRegistry()
{
//populate UserRegistry with initial vals
RegisteredUsers.add( new UserBean(1, "Guest", "Guest", "male", new java.util.Date(), "guest@vtrainer.com", "Basic", "guest", "guest", false));
RegisteredUsers.add( new UserBean(2, "Scott", "Tiger", "male", new java.util.Date(), "stiger@vtrainer.com", "Medium", "stiger", "welcome", false));
RegisteredUsers.add( new UserBean(3, "Claire", "Dassault", "female", new java.util.Date(), "claire@vtrainer.com", "Medium", "claire", "claire", false));
RegisteredUsers.add( new UserBean(4, "Joe", "Fitness", "male", new java.util.Date(), "jfitness@vtrainer.com", "Premium", "jfitness", "jfitness", true));
RegisteredUsers.add( new UserBean(5, "Jake", "DeJoque", "male", new java.util.Date(), "guest@vtrainer.com", "Premium", "jake", "jake", true));
RegisteredUsers.add( new UserBean(6, "Sally", "Sweats", "female", new java.util.Date(), "guest@vtrainer.com", "Premium", "sally", "sally", true));
}
public String AddRegisteredUser(UserBean newUser)
{
// Calculate new account id based on size of user registry
// For demo only - Persistent technology will use a sequence
int usercnt = RegisteredUsers.size();
newUser.setAccountNo(usercnt + 1);
// First check to see if userid already exists
if (!userIdAlreadyExists(newUser.getUserid())){
RegisteredUsers.add((UserBean) newUser);
return newUser.getUserid();
}
// Return userid of registered user
return null;
}
public UserBean findUserByCredentials(String userid, String password)
{
boolean found = false;
UserBean currentUser;
// temporarily turn ArrayList into static array of UserBeans
// then just return the first UserBean.
UserBean userArray[] = new UserBean[RegisteredUsers.size()];
userArray = (UserBean[])RegisteredUsers.toArray(userArray);
int i=0;
while (!found && i < RegisteredUsers.size())
{
currentUser = userArray[i];
if (currentUser.getUserid().equals(userid) && currentUser.getPassword().equals(password) )
{
// Found user!
found = true;
return userArray[i];
}
i++;
}
return null;
}
public boolean userIdAlreadyExists(String userid)
{
boolean found = false;
UserBean currentUser;
// Temporarily turn ArrayList into static array of UserBeans
// then just return the first UserBean.
UserBean userArray[] = new UserBean[RegisteredUsers.size()];
userArray = (UserBean[])RegisteredUsers.toArray(userArray);
int i=0;
while (!found && i < RegisteredUsers.size())
{
currentUser = userArray[i];
if (currentUser.getUserid().equals(userid) )
{
// Found user!
found = true;
return found;
}
i++;
}
return found;
}
public List findTrainerUsers()
{ // Returns only list of Users with trainer status
ArrayList trainerlist = new ArrayList();
for (int i = 0; i < this.RegisteredUsers.size(); i++) {
UserBean user = (UserBean) RegisteredUsers.get(i);
if (user.isTrainer()){
trainerlist.add(user);
}
}
return trainerlist;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -