2
\$\begingroup\$

the purpose of this class file is to design a Saving account that stores a saving account's annual interest rate and balance. I have not wrote the test program yet, seeing that I believe I made a few mistakes with my class file. Also, at the end of classfile, I hid a method because I didn't understand how to word that method to add the interest rate to the balance.

Here are some additional information that could help you understand where I was going.

Name of class :SavingAccount Class

The Constructor should accept the amount of the savings account's starting balance.

Methods:
-Subtracting the amound of withdrawal.
-Adding the amound of a deposit.
-Adding the amount of monthly interest to the balance.
Other information:
- The annual rate is 5% (.05)
-monthly interest = Annual interest rate divided by 12.
- Getting the monthly interest rate added to balance: Add the monthly rate to the   balance. With the new result, add to the balance.

Class File

 public class SavingsAccount
{

private double balance;
private double startBalance;
private double AnnualRate = 0.5;
private double MonthlyInterest;
private double withdrawl;
 private double NewWithdrawl;
 private double deposit;
 private double NewDeposit;


public SavingsAccount()
{
balance = 0.0;
}

public SavingsAccount(double startBalance)
{
balance = startBalance;
}

public double getAnnualRate()
{
return AnnualRate;
}

public double MonthlyInterest()
{
MonthlyInterest= AnnualRate/12;
return MonthlyInterest;
}

public double getWithdrawl()
{
NewWithdrawl = balance - withdrawl;
return NewWithdrawl;
}
public double deposit()
{
NewDeposit = balance + deposit;
return NewDeposit;
}

/* ( The objecttive is to "Add the amount of monthly interest to the balance", not sure        where this was going)

public  getMonthlyInterestRate()
{
MonthlyBalance = MonthlyInterest x balance;
NewMonthlyBalance = MonthlyBalance + balance;
return NewMonthlyBalance;
 }
*/


}

Thanks for going through what I wrote, I hope I didn't make a huge mistake with my class file.

\$\endgroup\$
1

2 Answers 2

4
\$\begingroup\$

Please indent your code properly. Java normally uses a modified K&R style like this:

public class Whatever {
    public function int waitWhat() {
        return 42;
    }
}

Also read the Java Naming Conventions.

  • Classes are UpperCamelCase
  • Functions and Variables are lowerCamelCase

Consider using JavaDoc to document your code.


Sometimes your functions are prefixed with "get" if they return something, sometimes not, make up your mind!


You're using an odd method of caching values. On the one side you're caching values in private variables, on the other you always recalculate them.


There are quite a few unused variables in there. Don't add variables because "I might use them later on", only add things you actually use right now.


This is what a cleaned up version might look like. This misses problems like that annualRate can not be set from the outside, or that you should not use double for money, or that documentation is missing, or that safety-checks are missing...

public class SavingsAccount {

    private double balance = 0.0;
    private double annualRate = 0.5;

    public SavingsAccount() {
    }

    public SavingsAccount(double balance) {
        this.balance = balance;
    }

    public double deposit(double amount) {
        balance += amount;
        return balance;
    }    

    public double getAnnualRate() {
        return annualRate;
    }

    public double getBalance() {
        return balance;
    }

    public double getMonthlyInterest() {
        return annualRate / 12;
    }

    public double withdraw(double amount) {
        // Don't forget safety-checks if the customer
        // is actually able to withdraw that amount!
        balance -= amount;
        return balance;
    }
}
\$\endgroup\$
5
\$\begingroup\$
  • First and most important remark : do not use double or float to represent money. Use BigDecimal instead. And make a Money class that combines the amount with the currency. edit : it's also possible to use long or int, if you always calculate in cents, this would make a Money class even more useful, to hide away that implementation detail.
  • Think about rounding and precision. How many fractional digits can an annual rate have, how many can a monthly rate have...? How will you round if needed?
  • Don't hard code the values of the rates. If there's anything banks like to change it is their rates. :)
  • I would find it very odd if the date of the withdrawals and deposits did not matter in calculating the interests.
  • I'm not sure how NewWithdrawl and NewDeposit are supposed to get their values, from the posted code. As is these fields don't seem to make much sense, it feels like they want to be parameters of methods yet to be defined.
  • getWithdrawl() (sic) seems to do the same sort of operation as deposit(), yet the former is named as a getter method (while it isn't one) and the latter is not. Preferably they'd be named consistently.
\$\endgroup\$
2
  • \$\begingroup\$ Please note that only calculating in cents may not be enough in banking applications. Sometimes you have to calculate in tenth or hundredth of a cent (especially if you use long or int). \$\endgroup\$
    – Uwe Plonus
    Commented Jul 11, 2013 at 6:22
  • \$\begingroup\$ @UwePlonus hence the phrase : if you always calculate in cents :) \$\endgroup\$
    – bowmore
    Commented Jul 11, 2013 at 6:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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