Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CheckingAccount
- {
- private double myBalance;
- private String myAccountNumber;
- public CheckingAccount()
- {
- myBalance = 0;
- myAccountNumber= "NEW";
- }
- public CheckingAccount(double initialBalance, String acctNum)
- {
- if(initialBalance < 0)
- throw new IllegalArgumentException("negative initial balance");
- myBalance = initialBalance;
- myAccountNumber = acctNum;
- }
- public double getBalance()
- {
- return myBalance;
- }
- public void deposit(double amount)
- {
- if(amount < 0)
- throw new IllegalArgumentException("negative amount");
- myBalance = myBalance + amount;
- }
- public void withdraw(double amount)
- {
- if(amount < 0 || amount > myBalance)
- throw new IllegalArgumentException("illegal amount");
- myBalance = myBalance - amount;
- }
- }
- import chn.util.*;
- public class CheckingTester
- {
- public static void main(String[] args)
- {
- CheckingAccount checking;
- ConsoleIO console = new ConsoleIO();
- double startBal=0;
- double dep = 0;
- double withdraw = 0;
- /*final double INTEREST_RATE = 2.5;
- double interest;
- interest = checking.getBalance() * INTEREST_RATE / 100;
- checking.deposit(interest);
- System.out.println("balance after year 1 is $" + checking.getBalance());
- interest = checking.getBalance() * INTEREST_RATE / 100;
- checking.deposit(interest);
- System.out.println("Balance after year 2 is $" + checking.getBalance());
- */
- System.out.println("");
- System.out.println("ErrorFreeChecking Test");
- System.out.print("Please enter the starting balance----> ");
- startBal = console.readDouble();
- try
- {
- checking = new CheckingAccount(startBal, "A123");
- }
- catch(IllegalArgumentException e)
- {
- System.out.println("Negative balance amount");
- System.out.println("");
- }
- System.out.println("");
- System.out.print("Please enter amount to deposit---->");
- dep = console.readDouble();
- try
- {
- checking.deposit(dep);
- }
- catch(IllegalArgumentException e)
- {
- System.out.println("negative amount");
- System.out.println("");
- }
- System.out.println("");
- System.out.print("Please enter amount to withdraw----->");
- withdraw = console.readDouble();
- checking.withdraw(withdraw);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment