-1

I'm currently solving the PaymentCard exercise in https://java-programming.mooc.fi/part-4/1-introduction-to-object-oriented-programming and the output of this program should not be a negative balance. If ever the balance gets negative, it will not be printed. I added a conditional statement in both methods but my output keeps printing a negative balance.

Any help would genuinely be appreciated. Thanks!

//Desired Output:The card has a balance 5.0 euros
//              The card has a balance 0.40000000000000036 euros
//              The card has a balance 0.40000000000000036 euros
//My 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 -4.199999999999999 euros
public class MainProgram {

    public static void main(String[] args) { 
        PaymentCard card = new PaymentCard(5);
        System.out.println(card);

        card.eatHeartily();
        System.out.println(card);

        card.eatHeartily();
        System.out.println(card);

    }
}

public class PaymentCard {

    private double balance;
    public PaymentCard(double openingBalance) {
        this.balance = openingBalance;
    }

    public String toString() {
        return "The card has a balance of " + this.balance + " euros";
    }

    public void eatAffordably() {
        if (this.balance > 0) {
            this.balance = this.balance - 2.60;
        }   
    }

    public void eatHeartily() {
        if (this.balance > 0) {
            this.balance = this.balance - 4.60;
        }    
    }
}
David Buck
  • 3,752
  • 35
  • 31
  • 35
bimyou
  • 21
  • 1
  • 5

2 Answers2

0

Rather than having a toString method

public String toString() {

    return "The card has a balance of " + this.balance + " euros";

}

and calling System.out.println(card);

create a method that does the actual printing e.g.

void printCard () {
    if (this.balance > 0) {
        System.out.println(card);
    }
}
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • Thanks. I tried to apply your solution and it works fine, but the exercise from https://java-programming.mooc.fi/part-4/1-introduction-to-object-oriented-programming requires me to use a `toString` method. – bimyou Apr 01 '20 at 02:45
  • `System.out.println(card);` is still using `toString` – Scary Wombat Apr 01 '20 at 02:47
  • I know but the exercise on the page is strictly prohibiting me from using `System.out.println(card)` and only allowing me to create a `toString` method in the program. The output's correct but the website's exercise detects it as incorrect. – bimyou Apr 01 '20 at 03:04
  • I did not tell you to delete the method `toString`. – Scary Wombat Apr 01 '20 at 03:06
0

Obviously you can print only amount greater then zero, but I think a more correct and elegant solution is take into consideration also the amount you subtract:

public void eatAffordably() {

    if (this.balance >= 2.60) {

        this.balance = this.balance - 2.60;
    } 

}

public void eatHeartily() {

    if (this.balance >= 4.60) {

        this.balance = this.balance - 4.60;
    } 

}
Renato
  • 2,077
  • 1
  • 11
  • 22