我正在使用 blueJ 来写这个。我正在尝试做的是 ddd 一个从该方法调用getUserIterator()
的方法UserGroup
,并使用我的方法返回的方法来循环现有用户并将所有用户添加到管理员组中。我不知道该怎么做!!我的代码如下:returns an Iterator
ArrayList
Iterator
userType
"admin"
用户等级:
package user;
public class User{
public enum UserType{
ADMIN, EDITOR, USER;
}
private String id;
private UserType userPermissions;
private String actualName;
public User(String username, UserType userType, String name){
id = username;
userPermissions = userType;
actualName= name;
}
public String getUsername(){
return id;
}
public UserType getUserType(){
return userPermissions;
}
public String getName(){
return actualName;
}
public void setUserType(UserType input){
userPermissions = input;
}
}
userGroup 类是:
package user;
import java.util.*;
import user.User.UserType;
public class UserGroup{
private ArrayList<User> people;
public UserGroup(){
people = new ArrayList<User>();
}
public void addSampleData(){
people.add(new User("jar1g13", UserType.ADMIN,"Jonny"));
people.add(new User("ao9", UserType.EDITOR,"Aniruddh"));
people.add(new User("pe6", UserType.USER,"Peter"));
people.add(new User("mat73", UserType.USER,"Matthew"));
people.add(new User("ora69", UserType.EDITOR,"Oranthi"));
people.add(new User("ben12", UserType.USER,"Benedict"));
people.add(new User("cam30", UserType.ADMIN,"Cambyse"));
people.add(new User("are20", UserType.USER,"Alex"));
people.add(new User("lim19", UserType.USER,"Liam"));
people.add(new User("ada13", UserType.EDITOR,"Adam"));
}
public User getUser(int idx){
return people.get(idx);
}
public void printUsernames(){
for (User user: people){
System.out.printf("%s %s\n", user.getUsername(), user.getUserType());
}
}
public void removeFirstUser(){
people.remove(0);
}
public void removeLastUser(){
people.remove(people.size()-1);
}
public void removeUser(String username){
Iterator<User> itr = people.iterator();
while (itr.hasNext()){
User element = (User) itr.next();
if (element.getUsername().equals(username)){
itr.remove();
}
}
}
}
主类:
package user;
public class Main{
public static void main(String[] args){
UserGroup grp = new UserGroup();
grp.addSampleData();
grp.printUsernames();
}
public static void admins(){
UserGroup administrators - new UserGroup();
}
}