jeudi 1 octobre 2015

Is the java code for this method correct?

For an assignment, I am told to write two methods--incrementDay() and incrementMonth().

I was hoping you guys could tell me if the code/logic is correct? Also, I would like to know if my test is thorough enough?

Thanks in advance! I know this is a long post--I apologize, and I won't do this often.

Assignment:

"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."

Code:

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

Aucun commentaire:

Enregistrer un commentaire