0

我正在使用 blueJ 来写这个。我正在尝试做的是 ddd 一个从该方法调用getUserIterator()的方法UserGroup,并使用我的方法返回的方法来循环现有用户并将所有用户添加到管理员组中。我不知道该怎么做!!我的代码如下:returns an IteratorArrayListIteratoruserType"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(); 
    }
}
4

2 回答 2

1

我会把它写成

public class UserGroup implements Iterable<User> {
    private final List<User> people = new ArrayList<User>();

    public Iterator<User> iterator() {
        return people.iterator();
    }
    // OR to prevent the list being modified.
    public Iterator<User> iterator() {
        return Collections.unmodifableList(people).iterator();
    }
    // OR for extra paranoia
    public Iterator<User> iterator() {
        return Collections.unmodifableList(new ArrayList<Person>(people)).iterator();
    }
    // OR for even more paranoia
    public Iterator<User> iterator() {
        List<People> ret = new ArrayList<People>();
        for(People p : people)
            ret.add(new Person(p)); // take a deep copy.
        return Collections.unmodifableList(ret).iterator();
    }
}

// later
UserGroup userGroup = ...
for(User u : userGroup) {
   // do something with u
}

这假设您不需要担心您的调用者修改列表(这是不应该做的,但有些人担心这样的事情)

于 2013-11-10T15:29:38.323 回答
1

你需要一个

public Iterator<User> getIterator(){
  return people.iterator;
}

在 classUserGroup中,您可以在任何其他类(或方法)中访问它,这些类(或方法)处理迭代列表中的用户并执行诸如检查他们的类型是否对应于给定类型(就像您想对管理员做的那样)。

于 2013-11-10T15:29:38.590 回答