Skip to content

Instantly share code, notes, and snippets.

@miststudent2011
Forked from jimmykurian/SavingsAccount.java
Created August 3, 2016 09:20
Show Gist options
  • Save miststudent2011/f103bf067642a58a64e046c7d46a7b2a to your computer and use it in GitHub Desktop.
Save miststudent2011/f103bf067642a58a64e046c7d46a7b2a to your computer and use it in GitHub Desktop.
A Java program that creates a Bank Account with withdraw, deposit, and intrest functions. And a tester class, that tests the SavingsAccount class.
//SavingsAccount.java - Jimmy Kurian
public class SavingsAccount
{
private double balance;
private double interest;
public SavingsAccount()
{
balance = 0;
interest = 0;
}
public SavingsAccount(double initialBalance, double initialInterest)
{
balance = initialBalance;
interest = initialInterest;
}
public void deposit(double amount)
{
balance = balance + amount;
}
public void withdraw(double amount)
{
balance = balance - amount;
}
public void addInterest()
{
balance = balance + balance * interest;
}
public double getBalance()
{
return balance;
}
}
//SavingsAccountTester.java - Jimmy Kurian
public class SavingsAccountTester
{
public static void main(String[] args)
{
SavingsAccount jimmysSavings = new SavingsAccount(1000, 0.10);
jimmysSavings.withdraw(250);
jimmysSavings.deposit(400);
jimmysSavings.addInterest();
System.out.println(jimmysSavings.getBalance());
System.out.println("Expected: 1265.0");
//1000-250=750 => 750+400=1150 => 1150+1150*0.10=1265.0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment