开发者

Passing Enumerated argument

Im having an issue passing an enumerated value from my main to one of my classes. My program stores information about nerds: name, nerdfactor. It has a series of classes and subclasses. I have a super class which is extended to 2 other classes and a third class which extends to 1 of the before mentioned subclasses. what my program is supost to do is take filled arguments such as name, nerdfactor, and wether the nerd prefers WII, XBOX or PS3. Bellow is a clip from my main and the beginings of my subclass. If anyone can help me figure out how to pass the enumerated argument i'd be very grateful.

public static void main(String[] args) {

    // List to keep a few nerds in
    ArrayList<GenericNerd> nerdList = new ArrayList(10);
    GenericNerd nerd = new GenericNerd("Bugger McNosePicker", 9);
    nerdList.add(nerd);

    nerd = new ConsoleGeek("Freddy where's my mommy", 9, WII); // the WII should be an enum value
    nerdList.add(nerd);
}

Subclass ConsoleGeek

public class ConsoleGeek extends GenericNerd {

    enum ConsoleType {

        WII, PS3, XBOX
    };
    private ConsoleType console;

    public void setConsoleType(ConsoleType console) {
        this.console = console;
    }
    public ConsoleType getConsoleType(){
    return console;
}


public ConsoleGeek(String name, int nerdFactor, ConsoleType console) {
    super(name, nerdFactor);
    if (name == null) {
        System.out.println("Error: GenericNerd constructor - name is null!");
        System.exit(1);
    }
       // the setNerdFactor method handles parameter range checking
    setNerdFactor(nerdFactor);

    this.name = name;
    if (console == ConsoleType.WII) {

        // make sure WII players nerd factor is less than 6!
        if (nerdFactor > 5) {
            System.out.println("Warning: " + name
                    + " Prefers WII! (setting nerdFactor to 5).");
            setNerdFactor(5);
        }
        // Make sure XBOX players nerd factor is between 3 and 8
    } else if (console == ConsoleType.XBOX) {
        if (nerdFactor < 3 || nerdFactor > 8) {
            System.out.println("Warning: " + name
                    + " Prefers XBOX! (setting nerdFacto开发者_开发问答r to 8).");
            setNerdFactor(8);
        }

    }
}

I thought about the idea of making a new type of nerd like

ConsoleGeek supergeek = new ConsoleGeek(arguments)

but I'm pretty sure its supposed to stay in the other format.


nerd = new ConsoleGeek("Freddy where's my mommy", 9, ConsoleGeek.ConsoleType.WII);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜