package Productions;
import java.util.Vector;
public class SetOfUsers extends Vector<User> {
private static SetOfUsers register = null;
public SetOfUsers(){
super();
}
public static SetOfUsers getInstance(){
if (register == null) {
register = new SetOfUsers();
}
return register;
}
public void addUser(User aUser){
super.add(aUser);
}
public User findUserByName(String name){
for(int i = 0; i < size(); i++){
User user = elementAt(i);
if(user.getName().equals(name)){
return user;
}
}
return null;
}
}
這裏是我的用戶類
package Productions;
public class User {
private final String name;
private final String password;
private String projectName;
private String type;
public User(String name, String password,String type){
this.name = name;
this.password = password;
this.type = type; //type of user
}
public boolean checkPassword(String pass)
{
if(password.equals(pass))
return true;
else
return false;
}
public String getName(){
return name;
}
public String getPassword(){
return password;
}
public String getType(){
return type;
}
public void allocateTask(){
}
public void removeTask(){
}
@Override
public String toString() {
return "Staff Name: " + this.getName() +
", Staff Pass: " + this.getPassword();
}
}
以上就是我測試的類,它擁有一個setOfUser
名稱
public void testFindUserByName() {
String name = "Bob";
SetOfUsers instance = new SetOfUsers();
// User expResult = null;
//User result = instance.findUserByName(name);
//assertEquals(expResult, result);
User result;
instance.add(result);
// User result = instance.findUserByName(name);
//then
assertEquals("Bob", result.getName());
}
當我運行上述測試時,我得到一個java空指針異常,我不明白這應該是因爲我期待Bob
這個名字。
試圖瞭解究竟是什麼類是指設計! –