Hey guys since the java threads have been pretty slow lately I wanted to post my BankAccount Class that I have been working on in AP coding at my school! Right now the class can carry out some simple commands such as withdraw, deposit, add interest, and get balance. I'll be adding on to it throughout the year but I thought you guys might want it for yourselves to fool around with or w/e u crazy people do =D.
I used BlueJ to compile this. There are TWO CLASSES, one is the SavingsAccount class and one is the TESTER. You need to compile them both in order to atain results. In order to use this you need to understand some basic java, like using objects by calling their methods. I basically created the object for you, and I created the methods for withdrawing, depositing, etc... ENJOY =P
EDIT: SORRY i accadentally left in an initial 10% interest rate when I typed this in, along with a couple other mistakes...this should be fixed.
SavingsAccount Class:
THE TESTER CLASS:
The tester has just one method in there, but you can edit this. The Expected string was from a HW problem, same with the addInterest + getBalance.
Post questions if you have any!
I used BlueJ to compile this. There are TWO CLASSES, one is the SavingsAccount class and one is the TESTER. You need to compile them both in order to atain results. In order to use this you need to understand some basic java, like using objects by calling their methods. I basically created the object for you, and I created the methods for withdrawing, depositing, etc... ENJOY =P
EDIT: SORRY i accadentally left in an initial 10% interest rate when I typed this in, along with a couple other mistakes...this should be fixed.
SavingsAccount Class:
Code:
/**
* Write a description of class SavingsAccount here.
*
* @author (Mike Perugini)
* @version (10/3/10)
*/
public class SavingsAccount
{
/**
* Constructs a bank account with zero balance.
*/
private double balance;
public SavingsAccount()
{
balance = 0;
}
/**
* Constructs a bank account with a given balance.
* @param initialBalance the initial balance.
*/
public SavingsAccount(double initialBalance)
{
balance = initialBalance;
}
/**
* Deposites money into the bank account.
* @param amount the ammount to deposit.
*/
public void deposite(double amount)
{
double newBalance = balance + amount;
balance = newBalance;
}
/**
* Withdraws money from the bank account.
* @param amount the ammount to withdraw.
*/
public void withdraw(double amount)
{
double newBalance = balance - amount;
balance = newBalance;
}
/**Adds interest to bank account balance.
* @param amount to add interest.
*/
public void addInterest(double amount)
{
double interestRate = (amount);
double newBalance = balance + (balance * interestRate / 100);
balance = newBalance;
}
/**
* Gets the current balance of the bank account.
* @return the current balance
*/
public double getBalance()
{
return balance;
}
}
THE TESTER CLASS:
Code:
/**
* Write a description of class SavingsAccountTester here.
*
* @author (Mike Perugini)
* @version (10/3/10)
*/
public class SavingsAccountTester
{
public static void main(String[] args)
{
//Creates a new bank account.
SavingsAccount mikesAccount = new SavingsAccount(1000);
mikesAccount.addInterest(5);
double balance = mikesAccount.getBalance();
System.out.println(balance);
System.out.println("Expected: 1100.55");
}
}
The tester has just one method in there, but you can edit this. The Expected string was from a HW problem, same with the addInterest + getBalance.
Post questions if you have any!




