Read through this lab once before doing anything so you know what to expect. Acc
ID: 668136 • Letter: R
Question
Read through this lab once before doing anything so you know what to expect.
Account
-acctNo: int
-owner: String
-balance: int
+Account()
+Accoun(acctNo: int, owner: String, balance: int)
+deposit(amount: int): void
Withdraw(amount: int): void
2.) In NetBeans create a 'Unitexception' 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?
Account
-acctNo: int
-owner: String
-balance: int
+Account()
+Accoun(acctNo: int, owner: String, balance: int)
+deposit(amount: int): void
Withdraw(amount: int): void
Explanation / Answer
AccountTest.java
//package main;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
//import org.junit.*;
public class AccountTest {
Account ac;
//@Before
public void setUp() throws Exception {
ac = new Account(1122, 20000, .045);
}
//@After
public void tearDown() throws Exception {
ac = null;
}
//@Test
public final void testID() throws
InsufficientFundsException {
ac.getID();
assertEquals((long)ac.getID(),(long)1122);
}
//@Test
public final void testBalance() throws
InsufficientFundsException {
ac.getBalance();
assertEquals((long)ac.getBalance(),
(long)20000);
}
//@Test
public final void testannualInterestRate() throws
InsufficientFundsException {
ac.getAnnualInterestrate();
assertEquals((long)ac.getAnnualInterestrate
(),(long).045);
}
//@Test
public final void testMonthlyInterestRate() throws
InsufficientFundsException {
ac.getMonthlyInterestRate();
assertEquals((long)ac.getMonthlyInterestRate
(),(long).00375);
}
//@Test
public final void testDate() throws
InsufficientFundsException {
ac.getDateCreated();
}
//@Test(expected=InsufficientFundsException.class)
public final void testWithdraw() throws
InsufficientFundsException {
ac.deposit(2500);
ac.withdraw(3000);
assertEquals((long)ac.getBalance(),
(long)19500);
ac.withdraw(50000);
}
}
/*
@Test
public final void testWithdraw() {
fail("Not yet implemented"); // TODO
}
@Test
public final void testGetBalance() {
fail("Not yet implemented"); // TODO
}
@Test
public final void testGetNumber() {
fail("Not yet implemented"); // TODO
}
*/
Account.java
//package main;
import java.util.*;
public class Account {
private int id=0;
private double balance = 0;
private double annualInterestRate=0;
private Date dateCreated;
public Account()
{
this.id=0;
this.balance=0;
this.annualInterestRate=0;
this.dateCreated= new Date();
}
public Account(int id, double balance, double
annualInterestRate)
{
this.id=id;
this.balance=balance;
this.annualInterestRate=annualInterestRate;
this.dateCreated=new Date();
}
public void setID(int id) {
this.id=id;
}
public int getID() {
return this.id;
}
public void setBalance(double balance) {
this.balance=balance;
}
public double getBalance(){
return this.balance;
}
public void setAnnualInterestrate(double
annualInterestRate) {
this.annualInterestRate=annualInterestRate;
}
public double getAnnualInterestrate() {
return this.annualInterestRate;
}
public Date getDateCreated(){
return this.dateCreated;
}
public double getMonthlyInterestRate() {
return (this.annualInterestRate)/12;
}
public void withdraw(double amount) throws
InsufficientFundsException
{
if(amount <= balance)
{
balance -= amount;
}
else
{
double needs = amount - balance;
throw new InsufficientFundsException
(needs);
}
System.out.println("After withdrawing "+
amount + ", balance in accounts is: " + this.balance + ".");
}
public void deposit(double amount)
{
this.balance += amount;
System.out.println("After depositing "+ amount
+ ", balance in account is: " + this.balance + ".");
}
public static void main(String args[])
{
Account ac= new Account(1122,20000,4.5);
try{
ac.withdraw(2500);
}catch(InsufficientFundsException e)
{
ac.deposit(3000);
System.out.println("Balance in Account
" + ac.getID() + " is: " + ac.getBalance() + ".");
System.out.println("Monthly Interest
is: "+ (ac.getMonthlyInterestRate() * ac.getBalance()) / 100 +
".");
System.out.println("Account created
on: " + ac.getDateCreated() + ".");
}
}
}
InsufficientFundsException.java
//package main;
import java.io.*;
public class InsufficientFundsException extends Exception
{
private double amount;
/**
* Create an instance of the
InsufficientFundsException class, to be thrown to the caller
* @param amount
*/
public InsufficientFundsException(double amount)
{
this.amount = amount;
}
public double getAmount()
{
return amount;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.