Valhalla Legends Forums Archive | Java Programming | testing stacks.

AuthorMessageTime
TooTallDawg
i'm trying to test 3 stacks.  below is a method that test only 1 stack.  can anyone help me code this method in order for it to test all 3 stacks?

private Stack<Product> fragile;
private Stack<Product> normal;
private Stack<Product> heavy;
.
.
.
.
public Product getProduct() {
       
    if(fragile.empty()) {
            return null;
    } else {
            return fragile.pop();
    }
}

July 10, 2006, 7:12 PM
Myndfyr
Test all three stacks for what?  Hepatitis?
July 10, 2006, 8:14 PM
HdxBmx27
What order do you want them to be ested in?
And do you not know of nested ifs?
Tho I wouldnt use them here.
If this is what your looking for here you go:
[code]public Product getProduct() {
     
    if(fragile.empty() != true)
            return fragile.pop();

    if(normal.empty() != true)
            return normal.pop();

    if(heavy.empty() != true)
            return heavy.pop();

    return null;
}[/code]
Thats my only guess as for what he wants from the lil info provided.
~-~(HDX)~-~
July 10, 2006, 10:52 PM
TooTallDawg
thanks.  that code works just fine.
July 11, 2006, 2:55 AM
Ender
I don't want to come off as an asshole, but those boolean literals, true/false, are ugly and unnecessary. Perhaps you did it for clarity, though.
July 16, 2006, 4:20 AM
TooTallDawg
would you prefer this:

    public Product getProduct() {
        if(!fragile.empty()) {
            return fragile.pop();
        } else if(!normal.empty()) {
            return normal.pop();
        } else if(!heavy.empty()) {
            return heavy.pop();
        }
        return null;
    }
July 17, 2006, 3:53 PM

Search