How to Create a Savings Account Class With Java
When creating a class to model the information for a savings account in Java, a number of special considerations come into play. The most important of these is a subtle weakness in the Java implementation of the float and double primitive data types that causes them to occasionally return slightly inaccurate answers to arithmetic operations. These inaccuracies, under most circumstances, would be trivial. However, when dealing with records of currency, where small inaccuracies can add up to dramatic, real world consequences over time, they can become serious.
Instructions
-
-
1
Create a SavingsAccount class. If you are using a specialized Java IDE, there is probably an option to create skeleton classes automatically for you in the File menu. Otherwise, simply create a file named "SavingsAccount.java" and write the following info in it:
/**
* This class represents information common to all savings accounts.
*/
public class SavingsAccount {// Everything else in the tutorial will be written in this space!
}
-
2
Declare the class fields. At the minimum, you will probably want to store an account number, the current balance, a list of account holders, and a record of all transactions on the account. Write the following inside the class:
/**
* The account number.
*/
String accountNumber;/**
* The current balance. Never use floats or doubles for currency! There are inaccuracies in float and double arithmetic.
*/
private BigDecimal balance;/**
* This stores a list of account holder names as a string. An alternative might be to create an AccountHolder class, and store that here.
*/
ArrayList<String> accountHolders = new ArrayList<String>();/**
* A history of transactions performed upon the account. Again, an alternative would be to create a Transaction class to hold more information than the amount of the transaction
*/
private ArrayList<BigDecimal> transactions = new ArrayList<BigDecimal>();Documenting your work is important to code readability and future maintenance, so don't neglect your JavaDoc comments.
One very important thing you should notice is that balance and transactions are not represented as floats or doubles, but rather with the class BigDecimal. BigDecimal is a slower and more memory-intensive method of storing floating point numbers. However, it lacks the small inaccuracies of the float and double primitives. Since you are dealing with currency, you should prefer accuracy in operations to the small gains provided by float and double in speed and memory consumption.
-
-
3
Create a constructor. In most classes, you'll want to overload your constructor; that is, you'll want to have more than one method for building a new account instance. You can decide for yourself what sorts of constructors will suit your needs, but at the minimum, there should be a constructor for creating a new account given an account number and a starting balance, so add the following method to your class:
/**
* Create a new account
* @param accountNumber the new account's number
* @param balance the account's start balance
*/
SavingsAccount(String accountNumber, BigDecimal balance) {
this.accountNumber = accountNumber;
this.balance = balance;
} -
4
Create a balance "getter" and a performTransaction method. It is standard Object Oriented Programming practice to create getters and setters for all fields in a class. However, this is inappropriate for the balance field. While you should certainly allow the balance to be viewed, you do not want arbitrary manipulations to the balance. Rather, you want all changes to the balance to occur in the form of a transaction. Therefore, add the following two methods to your class.
/**
* @return the current balance
*/
BigDecimal getBalance() {
return balance;
}/**
* Performs a transaction on the account.
* @param amount how much to withdraw/deposit? Withdraws should be negative. Deposits should be positive.
* @return True of successful. False if unsuccessful. An alternative to using a boolean here would be to create a "TransactionFailedException" class that could provide the user more information about the reasons for the failure.
*/
boolean performTransaction(BigDecimal amount) {
if (amount.compareTo(BigDecimal.ZERO) == -1) {
// This is a withdraw
if (amount.abs().compareTo(balance) == 1) {
// The withdraw amount is greater than the balance.
return false;
} else {
// There is enough money to cover the withdraw. Modify the balance and log the transaction.
balance = balance.add(amount);
transactions.add(amount);
return true;
}
} else {
// This is a deposit. Add to the balance and log the transaction.
balance = balance.add(amount);
transactions.add(amount);
return true;
}
}This provides your savings account class with the minimum it needs to function, though there are still enhancements that can be made to it. Account holder and transactions should be their own classes, rather than simple Strings and BigDecimals, since you might want to store more information about a transaction than simply the amount involved.
-
1