import java.util.Scanner;
public class ExceptionTester {
public static void main(String[] args){
int initialBalance, deposit, withdraw;
Scanner input = new Scanner(System.in);//init. scanner input
BankAccount bank = null;
System.out.println("Welcome to BankAccount Exception Tester by: Leo Gorokhovsky");
System.out.println("Please enter your Initial Balance");
try
{
initialBalance= input.nextInt();
bank = new BankAccount(initialBalance);
}
catch (IllegalArgumentException exception)
{
System.out.println("Sorry, you cannot have a negitive balance");
}
System.out.println("How much money would you like to deposit?");
try
{
deposit = input.nextInt();
bank.deposit(deposit);
System.out.println("Your current balance is: "+bank.getBalance());
}
catch (IllegalArgumentException exception2)
{
System.out.println("Sorry, you cannot deposit a negitive balance");
}
System.out.println("How much money would you like to withdraw?");
try
{
withdraw = input.nextInt();
bank.withdraw(withdraw);
System.out.println("Your current balance is: "+bank.getBalance());
}
catch (IllegalArgumentException exception3)
{
System.out.println("Sorry, you cannot withdraw a negitive value");
}
catch (IllegalArgumentException exception4)
{
System.out.println("Sorry, you cannot withdraw more than you have in your account");
}
}
}
/**
A bank account has a balance that can be changed by
deposits and withdrawals.
*/
public class BankAccount implements Measurable
{
/**
Constructs a bank account with a zero balance.
*/
public BankAccount()
{
balance = 0;
}
/**
Constructs a bank account with a given balance.
Editted for IllegalArgumentException Error Handling
*/
public BankAccount(double initialBalance)
{
if (initialBalance < 0){
IllegalArgumentException exception = new IllegalArgumentException("You must enter a positive ammount");
throw exception;
}
balance = initialBalance;
}
/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
public void deposit(double amount)
{
if (amount < 0){
IllegalArgumentException exception2 = new IllegalArgumentException("You must enter a positive ammount");
throw exception2;
}
double newBalance = balance + amount;
balance = newBalance;
}
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
{
if (amount < 0){
IllegalArgumentException exception3 = new IllegalArgumentException("You must enter a positive ammount");
throw exception3;
}
if (amount > balance){
IllegalArgumentException exception4 = new IllegalArgumentException("You cannot withdraw more than you have");
throw exception4;
}
double newBalance = balance - amount;
balance = newBalance;
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
return balance;
}
public double getMeasure()
{
return balance;
}
private double balance;
}