In NetBeans create a project. 3.) Build an Account class. The Account class shou
ID: 3879448 • Letter: I
Question
In NetBeans create a project.
3.) 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 in the order shown 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. See the UML diagram (note the getters and setters are assumed in the diagram). The method signatures need to look like the ones in the diagram.
4.) Create a JUnit tester to test out this class.
5.) Substitute the provided AccountTest.java class for the tester you created.
6.) 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 this setBalance method attempts to set the balance below zero, the InsufficientFundsException will be thrown.
7.) Now in your JUnit tester modify the withdraw() test method that will attempt to withdraw more than the current balance has available. Change the setBalance() test method too. What happens now?
8.) Lastly modify your JUnit test for overdrawing the account so that it catches exceptions, and catch the InsufficientFundsException. Retest. What happens now?
Explanation / Answer
Below is your program. Let me know if you have any issue: -
Account.java
public class Account {
private int acctNo;
private String owner;
private int balance;
public Account() {
acctNo = 0;
owner = "";
balance = 0;
}
public Account(int no, String own, int bal) {
this.acctNo = no;
this.owner = own;
this.balance = bal;
}
public int getAcctNo() {
return acctNo;
}
public void setAcctNo(int no) {
this.acctNo = no;
}
public String getOwner() {
return owner;
}
public void setOwner(String own) {
this.owner = own;
}
public int getBalance() {
return balance;
}
public void setBalance(int bal) {
this.balance = bal;
}
public void withdraw(int amt) throws InsufficientFundsException {
if (!(amt == 0 && balance == 0) && amt <= balance) {
balance -= amt;
} else {
int newBalance = amt - balance;
throw new InsufficientFundsException(newBalance);
}
}
public void deposit(int amt) {
this.balance += amt;
}
public static void main(String args[]) {
Account ac = new Account(1234, "david", 15000);
try {
ac.withdraw(1500);
} catch (InsufficientFundsException e) {
System.out.println("Account number: " + ac.getAcctNo());
System.out.println("owner: " + ac.getOwner());
System.out.println("Balance is :" + ac.getBalance());
}
}
}
InsufficientFundsException.java
public class InsufficientFundsException extends Exception {
private final int amount;
public InsufficientFundsException(int amt) {
this.amount = amt;
}
public int getAmt() {
return amount;
}
}
AccountTest.java
import static org.junit.Assert.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class AccountTest {
public AccountTest() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Test
public void testAcctNo() throws InsufficientFundsException {
Account instance = new Account();
int id = 0;
int number = instance.getAcctNo();
assertEquals(id, number);
}
@Test
public void testBalance() throws InsufficientFundsException {
Account instance = new Account();
int expResult = 0;
int result = instance.getBalance();
assertEquals(expResult, result);
}
@Test(expected = InsufficientFundsException.class)
public void testWithdraw() throws InsufficientFundsException {
int amount = 0;
Account instance = new Account();
instance.withdraw(amount);
int balance = instance.getBalance();
assertEquals(-amount, balance);
}
@Test
public void testDeposit() throws InsufficientFundsException {
int amount = 0;
Account instance = new Account();
instance.deposit(amount);
int balance = instance.getBalance();
assertEquals(amount, balance);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.