Part 1 Build an Account class. The Account class should have 3 Properties: balan
ID: 3883757 • Letter: P
Question
Part 1
Build an Account class. The Account class should have 3 Properties: balance, owner and acctNo. The Account class should have 2 constructors, one that takes all 3 properties and one that takes no data (the default constructor). The Account class should have set and get methods as well as deposit and withdraw methods. Note that the Account class has no main() method.
Part 2
Create tester class called AccountTester to test out this class. The main() method will create an instance of the Account class and call it’s methods to test it. In particular the following tests should be done:
a) Create an Account with an initial balance of 200 using the multi-arg constructor. Use the widthdraw() method to withdraw 100 then display the balance.
b) Using the same Account object, call the deposit() method and deposit 50 and then display the balance.
c) Using the same Account object, use the withdraw() method to withdraw 500 and display the balance.
d) Using the setBalance() method on the Account object and set the balance to -100, then display the balance. What happens in your withdraw test if the balance goes below 0? Or if the setBalance() method is passed a negative number? In both cases the balance goes negative. Well, that is really an error, we don't want that. So we will need these methods to throw exceptions instead of creating a negative balance.
Part 3
Build an InsufficientFundsException class. Have this class extend from the Exception class. Also modify your Account class. Make it so that when the withdraw() method or the setBalance() method attempts to set the balance below zero, the InsufficientFundsException will be thrown. The AccountTester will, of course, also have to be changed to use a try/catch block anytime it calls the withdraw() or setBalance() methods catching the InsufficientFundsException and print out an appropriate message. Now rerun the AccountTester. What happens now?
Test Run
Here is what the output from running the AccountTester should look like after completing part 2.
Account object created with balance of 200
Withdrawing 100.
Balance after withdrawal of 100 is: 100
Depositing 50 Balance after deposit of 50 is: 150
Withdrawing 500
Error: Overdrawn
Balance is: 150
Setting Balance to -100
Error: Overdrawn
Balance is: 150
Explanation / Answer
Hi let me know if you need more information:-
=====================================
AccountTester.java
======================
public class AccountTester {
public static void main(String[] args) {
Account account = new Account("", 200, 12345678);
System.out.println("Account object created with balance of " + account.getBalance());
int withdraw = 100;
try {
account.withdraw(withdraw);
} catch (InsufficientFundsException e) {
System.out.println("Error: Overdrawn");
System.out.println("Balance is: " + account.getBalance());
}
System.out.println("Withdrawing " + withdraw);
System.out.println("Balance after withdrawal of " + withdraw + " is:" + account.getBalance());
int deposite = 50;
account.deposit(deposite);
System.out.println(
"Depositing " + deposite + " Balance after deposit of " + deposite + " is:" + account.getBalance());
withdraw = 500;
System.out.println("Withdrawing "+withdraw);
try {
account.withdraw(withdraw);
} catch (InsufficientFundsException e) {
System.out.println("Error: Overdrawn");
System.out.println("Balance is: " + account.getBalance());
}
int setBalance = -100;
System.out.println("Setting Balance to " + setBalance);
try {
account.setBalance(setBalance);
} catch (InsufficientFundsException e) {
System.out.println("Error: Overdrawn");
System.out.println("Balance is: " + account.getBalance());
}
}
}
============================
Account.java
===================
public class Account {
private String owner;
private int balance;
private int accNo;
public Account() {
super();
}
public Account(String owner, int balance, int accNo) {
super();
this.owner = owner;
this.balance = balance;
this.accNo = accNo;
}
public void deposit(int balance) {
this.balance += balance;
}
public void withdraw(int balance) throws InsufficientFundsException {
if (this.balance < balance)
throw new InsufficientFundsException();
this.balance -= balance;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) throws InsufficientFundsException {
if (balance < 0)
throw new InsufficientFundsException();
this.balance = balance;
}
public int getAccNo() {
return accNo;
}
public void setAccNo(int accNo) {
this.accNo = accNo;
}
}
==================================
InsufficientFundsException.java
=================================
public class InsufficientFundsException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
}
================================
OUTPUT:-
====================================
Account object created with balance of 200
Withdrawing 100
Balance after withdrawal of 100 is:100
Depositing 50 Balance after deposit of 50 is:150
Withdrawing 500
Error: Overdrawn
Balance is: 150
Setting Balance to -100
Error: Overdrawn
Balance is: 150
======================
Thanks