Java method to add daily interest to bank account after month is over For an assignment, I am told to write two methods: incrementDay() and incrementMonth():


  incrementDay()
  
  This method takes no input and returns nothing. If the previous
  month's balance was not paid in full, the credit card charges interest
  on the current balance after each day. To do this, the class will need
  two additional fields, a variable that keeps track of the interest
  charged so far and a boolean variable that is a paid-in-full flag that
  keeps track of whether the user has paid the balance in full.
  
  incrementDay works as follows: If the paid-in-full flag is false then
  sum the account balance and the interest charged so far, then multiply
  this sum by the interest rate divided by 365, and add this value to
  the interest charged so far.
  
  If the paid-in-full flag is true, then there is nothing to do.
  
  incrementMonth()
  
  This method takes no input and returns nothing. Add the interest
  charged so far to the account balance. Reset the interest charged so
  far to 0.
  
  If the total payment this month is greater than or equal to the
  monthly payment, set the paid-in-full flag to true, otherwise set the
  flag to false.
  
  If the minimum monthly payment is smaller than the monthly payment and
  the total payment this month is less than the minimum monthly payment,
  add the late payment penalty to the current balance.
  
  Set the total payment this month to 0.
  
  Set the monthly payment equal to the current balance.


I'm just asking for someone to read my code and tell me if the logic and tests work in all cases.

public void incrementDay()
  {
    if(!paidInFullFlag)
    {
      interestCharged += (this.balance + interestCharged) * (interestRate/365);
    }
  }

  /*Method adds interestCharged to balance and checks whether or not account has been paid in full for month*/
  public void incrementMonth()
  {
    this.balance += interestCharged;
    this.interestCharged = 0;
    if(this.sumPayment &gt;= this.monthlyPayment)
    {
      this.paidInFullFlag = true;
    }
    else 
    {
      this.paidInFullFlag = false;
    }

    if(minMonthly &lt; monthlyPayment &amp;&amp; sumPayment &lt; minMonthly)
    {
      this.balance += latePaymentPenalty;
    }
    this.sumPayment = 0;
    this.monthlyPayment = this.balance;
  }


Test:

&gt; CreditCardAccount c = new CreditCardAccount()
&gt; c.setCreditLimit(100)
&gt; c.setInterestRate(.08)
&gt; c.setLatePaymentPenalty(3)
&gt; c.setMinMonthlyPayment(20)
&gt; c.charge(30)
true
&gt; c.getBalance()
30.0
&gt; c.payment(5) //pay 5 dollars towards balance
&gt; c.incrementDay() 
&gt; c.incrementMonth()
&gt; c.getBalance() //flag is initially set to true, so no late payment penalty or interest is charged, but flag is set to false for next month
25.0
&gt; c.payment(5)
&gt; c.charge(20)
true
&gt; c.getBalance()
40.0
&gt; c.incrementMonth() //late payment penalty is charged
&gt; c.getBalance() 
43.0
&gt; c.incrementDay()
&gt; c.getBalance()
43.0
&gt; c.incrementMonth()
&gt; c.getBalance() //interest AND late payment penalty is added
46.00942465753425
Conventions

Java conventions state that code is indented with 4 spaces, not 2. Java also states that the brace should be on the same line, though this is more of your choice; the important part is consistency. After formatting, your code will then look like:

public void incrementDay() {
    if (!paidInFullFlag) {
        interestCharged += (this.balance + interestCharged) * (interestRate
                / 365);
    }
}

/*
 * Method adds interestCharged to balance and checks whether or not account
 * has been paid in full for month
 */
public void incrementMonth() {
    this.balance += interestCharged;
    this.interestCharged = 0;
    if (this.sumPayment &gt;= this.monthlyPayment) {
        this.paidInFullFlag = true;
    } else {
        this.paidInFullFlag = false;
    }

    if (minMonthly &lt; monthlyPayment &amp;&amp; sumPayment &lt; minMonthly) {
        this.balance += latePaymentPenalty;
    }
    this.sumPayment = 0;
    this.monthlyPayment = this.balance;
}


Miscellaneous

Here:

    if (this.sumPayment &gt;= this.monthlyPayment) {
        this.paidInFullFlag = true;
    } else {
        this.paidInFullFlag = false;
    }


Since the condition is a boolean already, this could be simplified to:

    this.paidInFullFlag = this.sumPayment &gt;= this.monthlyPayment;




The comment here:

/*
 * Method adds interestCharged to balance and checks whether or not account
 * has been paid in full for month
 */


Should be a JavaDoc comment:

/**
 * Method adds interestCharged to balance and checks whether or not account
 * has been paid in full for month
 */


Very similar, but not exactly the same.