Skip to content

Instantly share code, notes, and snippets.

@QuanSai
Created November 13, 2012 09:46
Show Gist options
  • Save QuanSai/4064931 to your computer and use it in GitHub Desktop.
Save QuanSai/4064931 to your computer and use it in GitHub Desktop.
A small, simple demonstration of an ATM in Java I wrote. Demonstrates classes, methods, and exceptions. Also demonstrates implementation and testing.
package bankaccounttest;
public class BankAccount
{
private double balance;
//////Constructors//////
public BankAccount() //no-arg constructor
{
this.balance = 0.0;
}
public BankAccount(double b) //arg constructor
{
try
{
this.balance = b;
if(b < 0)
{
throw new IllegalArgumentException(Double.toString(b)); //throw an exception if the specified account amount is negative
}
}
catch(IllegalArgumentException e)
{
System.out.println(e);
}
}
//////Accessors//////
public double getBalance() //accessor; retrieves the account balance
{
return balance;
}
//////Mutators//////
public void deposit(double amount)
{
try
{
this.balance += amount;
if(amount < 0)
{
throw new IllegalArgumentException(Double.toString(amount));
}
}
catch(IllegalArgumentException e)
{
System.out.println(e);
}
}
public void withdraw(double amount)
{
try
{
if(amount > this.balance)
{
throw new InsufficientFundsException(Double.toString(amount));
}
if(amount < 0)
{
throw new IllegalArgumentException(Double.toString(amount));
}
else
{
this.balance -= amount;
}
}
catch(IllegalArgumentException | InsufficientFundsException e)
{
}
}
//////Meta Data//////
@Override
public String toString()
{
String meta = "Funds: " + this.getBalance();
return meta;
}
////////////////////////
public void transfer(double amount, BankAccount other)
{
if(amount <= this.balance)
{
this.withdraw(amount);
other.deposit(amount);
}
else
{
System.out.println("Couldn't transfer due to insufficient funds.");
}
}
}
package bankaccounttest;
public class BankAccountTest
{
public static void main(String[] args)
{
//create two BankAccount objects; one has the default balance (0.0);
// the other has a $500.0 balance.
BankAccount account_a = new BankAccount(), account_b = new BankAccount(500);
//Test the getBalance() method on each account
System.out.println("Account A: $" + account_a.getBalance());
System.out.println("Account B: $" + account_b.getBalance());
//test the deposit() method
account_a.deposit(65);
System.out.println(account_a.toString()); //test the toString() method
//transfer a legal amount from account_a to account_b
account_a.transfer(60, account_b);
System.out.println("A: " + account_a.toString());
System.out.println("B: " + account_b.toString());
//attempt to transfer an illegal amount (greater than balance)
// from account_a to account_b
//Should print out an error statement.
account_a.transfer(100, account_b);
System.out.println("A: " + account_a.toString());
System.out.println("B: " + account_b.toString());
//attempt to transfer one more legal amount to make sure everything's fine
// after error.
account_a.transfer(2, account_b);
System.out.println("A: " + account_a.toString());
System.out.println("B: " + account_b.toString());
}//end main()
}//end BankAccountTest
package bankaccounttest;
public class InsufficientFundsException extends Exception
{
public InsufficientFundsException(String message)
{
super(message);
}
public InsufficientFundsException(String message, Throwable throwable)
{
super(message, throwable);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment