3
\$\begingroup\$

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 >= this.monthlyPayment)
    {
      this.paidInFullFlag = true;
    }
    else 
    {
      this.paidInFullFlag = false;
    }

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

Test:

> CreditCardAccount c = new CreditCardAccount()
> c.setCreditLimit(100)
> c.setInterestRate(.08)
> c.setLatePaymentPenalty(3)
> c.setMinMonthlyPayment(20)
> c.charge(30)
true
> c.getBalance()
30.0
> c.payment(5) //pay 5 dollars towards balance
> c.incrementDay() 
> c.incrementMonth()
> 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
> c.payment(5)
> c.charge(20)
true
> c.getBalance()
40.0
> c.incrementMonth() //late payment penalty is charged
> c.getBalance() 
43.0
> c.incrementDay()
> c.getBalance()
43.0
> c.incrementMonth()
> c.getBalance() //interest AND late payment penalty is added
46.00942465753425
\$\endgroup\$
1
\$\begingroup\$

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 >= this.monthlyPayment) {
        this.paidInFullFlag = true;
    } else {
        this.paidInFullFlag = false;
    }

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

Miscellaneous

Here:

    if (this.sumPayment >= 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 >= 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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.