I've ran into a problem with a method in my program, the task i have been set involves stopping the if statement from running after the balance hits -500, i'v tried if(price < buy && balance > -500) and the current one, while(balance > -500) gives me the result of -594 but the expected result is -495.
although if i take out the {} for the while loop and place a break; right next to the while() it just runs through the junit test which uses a method on the subject class(using observer strategy) to change the price(in this case its setting the price to 0.99 7 times). From my testing the current solution gives the closest answer, but the part I'm asking for help with is stopping it so it doesn't go over the -500 mark. ' Thanks in advance and if you want me to add in any more code to help, let me know
@Override
public double getBalance() {
System.out.println("balance " + price);
while(balance > -500)break;
if( price < buy ){
outcome = price * increment;
System.out.println("OUT " + outcome + " ");
}else if(price > sell && portfolio >= 100){
income = price * increment;
}
double total = income - outcome;
balance = balance + total;
}
return balance;
}
edit - ok ive indented it a bit better i hope
I think the problem is that there are no checks if a balance - total
will go below -500 before the next iteration begins; simplest way to do this with your current code would be:
double total = income - outcome;
if(balance + total >= -500) {
balance = balance + total;
} else {
break;
}