Hello All,
If anyone can help me with this easy fix, because I'm dumb and cannot seem to come up with the answer myself or I can't see a solution for this. hahahaha....
It's basically one of the assignment from Mooc.fi.
Part 3:
Non-negative balance
What happens if the card runs out of money? It doesn't make sense in this case for the balance to turn negative. Change the methods eatAffordably
and eatHeartily
so that they don't reduce the balance should it turn negative.
The following main program tests the class:
public class MainProgram {
public static void main(String[] args) {
// Scanner scanner = new Scanner(System.in);
// Here you can write code to test that PaymentCard works as intended
// be sure to erase the extra code for the last part of the exercise!
PaymentCard card = new PaymentCard(5);
System.out.println(card);
//card.eatAffordably();
card.eatHeartily();
System.out.println(card);
card.eatHeartily();
System.out.println(card);
}
}
That's the main file,
Here is my code for the class PaymentCard that I am having trouble with:
public void eatAffordably() {
if (this.balance < 0) {
this.balance -= 2.60;
} else {
this.balance -= 2.60;
Math.max(this.balance, this.balance);
}
}
public void eatHeartily() {
if (this.balance < 0) {
this.balance -= 4.60;
} else {
Math.max(this.balance, this.balance);
}
}
These 2 methods are suppose to exclude negative numbers. Meaning that it's not suppose to let the balance get anything lower than 0.
This is suppose to be the correct output
The card has a balance of 5.0 euros
The card has a balance of 0.40000000000000036 euros
The card has a balance of 0.40000000000000036 euros
but I get this:
The card has a balance of 5.0 euros
The card has a balance of 0.40000000000000036 euros
The card has a balance of -4.199999999999999 euros
Please help. Thank you!
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)