Checking if a Boolean Within Another Class Object is True
So I have a project I'm working on, and for one part, I have to check if a boolean is true. However, this boolean is a local variable in a method of another class type I made, and I don't know how to check for that.
So here's the method in the non-main class, "Stock":
Code:
public boolean Buy(int num, double pr)
{
boolean ok = false;
if (pr > 0.95*sprice && pr < 1.05*sprice)
{
ok = true;
svolume -= num;
sprice = pr;
}
return ok;
}
public boolean Sell(int num, double pr)
{
boolean ok = false;
if (pr > 0.95*sprice && pr < 1.05*sprice)
{
ok = true;
svolume += num;
sprice = pr;
}
return ok;
}
and here's how I'm trying to check, unsuccessfully.
Code:
choice4 = Integer.parseInt(JOptionPane.showInputDialog("Please enter one of the following numbers to sell that stock:" + stockOut + "\n" + (numberOfStocks + 1) + " to exit"));
if(choice4 != (numberOfStocks + 1)) {
Boolean testSell = myStocks[choice4 - 1].Sell(numberBought = Integer.parseInt(JOptionPane.showInputDialog("How many of the " + myStocks[choice4 - 1].getName() + " stocks would you like to sell?")), newPrice = Double.parseDouble(JOptionPane.showInputDialog("Please input the new price of the " + myStocks[choice4 - 1].getName() + " stocks.")));
if(testSell = true) {
myCustomers[choice1 - 1].successfulSell((choice1 - 1), newPrice, numberBought);
} else {
JOptionPane.showMessageDialog(null, "Invalid price entry.");
}
}
So does anyone know how I would go about this?
Thanks in advance!
Re: Checking if a Boolean Within Another Class Object is True
The value returned by the buy(...) method is what you're after. Having said that, where do you call the buy method in the second bit of code?
Note that method names (like variable names) should begin with a lower-case letter, not a capital letter, and so the method should be named buy(...) not Buy(...). Class names should begin with capital letters.
Re: Checking if a Boolean Within Another Class Object is True
Quote:
Originally Posted by
Fubarable
The value returned by the buy(...) method is what you're after. Having said that, where do you call the buy method in the second bit of code?
Note that method names (like variable names) should begin with a lower-case letter, not a capital letter, and so the method should be named buy(...) not Buy(...). Class names should begin with capital letters.
I was using the sell method as the example. The buy method is the same thing, just with the buy method being called rather than the sell.
Re: Checking if a Boolean Within Another Class Object is True
Quote:
Originally Posted by
Alpharius120
I was using the sell method as the example. The buy method is the same thing, just with the buy method being called rather than the sell.
I would suggest that you not do this -- it is guaranteed to confuse us (at least me). Please understand that we are at a distinct disadvantage since we do not have your whole program in front of us, nor do we know any of the background information or your thought processes involved in this process. So if you are going to have us analyze a method, show us that method itself rather than a different one. How are we to assume that the methods are symmetric without seeing it?
So having said that, your code posted has at least one minor problem and at least one major problem. The minor problem is that you're using a Boolean instead of boolean, with the latter being a primitive and the former being a reference type wrapper for the primitive type. Use the primitive type unless you have no choice (such as if you were putting them into a collection) as there's no need for the extra over-head that the wrapper type requires.
The major problem is that you're re-assigning the variable it seems. You're doing this:
Code:
if(testSell = true) {
which is trying to assign true to testSell. Does that even compile? It should never be done. I suspect you're trying to do this:
Code:
if(testSell == true) {
Notice the difference? This tests if testSell is true or not, but even this shouldn't be done. Instead you should use:
Re: Checking if a Boolean Within Another Class Object is True
Oh, okay, makes sense.
Thank you very much, and thanks for the suggestions!
Re: Checking if a Boolean Within Another Class Object is True