Question
It's that time of the semester - you're working diligently to finish the last pieces of your team project (probably in other classes as well!).
It's that time of the semester - you're working diligently to finish the last pieces of your team project (probably in other classes as well!). At a time when logic errors abound, put away the GUI building and take a closer look at JUnit testing a very simple Java program.
Getting Started
To begin this lab, create a new Java project named abc123-lab7, and create the following packages and classes:
bank.Account.java
bank.Bank.java
This lab does not deal with synchronized or multithreading. Correct solutions to this lab may not be thread-safe.
Banks & Accounts
Bank.java will define functionality for a bank. Banks must have a name and a set of accounts. Use a java.util.Set implementation of your choosing for this data structure, and the Account.java implementation for the accounts stored. Bank.java must have a single constructor to properly initialize banks. In addition, there should be an overloaded method called addAccount which can add Account objects to the bank given either a name for the account, or both a name for the account and a starting balance. This method will create an Account object and add it to the set stored in the Bank object, nothing will be returned. Creating a new object would be as follows: Bank bank = new Bank("Gringotts");
Account.java will define functionality for an account. Accounts must have a name associated with them, an account number, and a balance. As described above, an Account object can be defined using either all three of these (name, number, and balance) or only with the name and number. (This indicates the constructor must be overloaded - one for each of these cases.) If a starting balance is not provided, it will be assumed to be 0. There should be getters and setters for all of these three variables. Account.java must have two additional methods: deposit and withdraw. deposit will be an object method that takes in an amount of money and adds the amount to the balance of an account. Deposits should inherently only the positive values. This method will return the new balance of the account. withdraw will be an object method that takes in an amount of money and reduces the balance by that amount. Withdrawls should inherently only be positive values. Withdrawls larger than the current balance should not be permitted. This method will return the new balance of the account. Creating a new object would be as follows: Account account1 = new Account("Amy Smith", 2202); or Account account2 = new Account("Bob Smith", 1584, 100.0);
JUnit Testing
Create a JUnit 4 Test Case, called AccountTest.java. This class will test only the functionality in the Account class, defined above. Implement the following methods:
setUp() - Method runs before each test is run.
testInit() - Test the constructors.
testValidDeposit() - Test depositing a valid amount (i.e. positive amount) into the account.
testInvalidDeposit() - Test depositing an invalid amount (i.e. negative amount) into the account.
testValidWithdrawl() - Test withdrawing a valid amount (i.e. positive amount) from the account.
testInvalidWithdrawl() - Test withdrawing an invalid amount (i.e. negative amount) from the account.
testOverdraft() - Test withdrawing more than the balance from the account.
No test methods should be created for the getters or setters in the Account class. Each method should have the appropriate JUnit annotation. Overdrafts should not be permitted, instead a message must be presented to the user. For each test listed, consider the preconditions and postconditions of the method in testing. Consider any input (parameters), logical requirements (described in the class descriptions above), and outputs (returned values). What could go wrong when the method is called? Equally important, what could be wrong after the method is called? In the process of implementing these tests, you should run them and verify they are passing. If a test does not pass, first assess the test (is it set up correctly?), then verify your implementation meets the requirements (see Account.java above). A completed submission for this lab will correctly assess all test cases, and running AccountTest.java will result in a pass (green bar in the JUnit view).
HERE IS THE CODE I HAVE FOR THE ACCOUNT.JAVA, BANK.JAVA, AND ACCOUNTTEST.JAVA
IM NOT GETTING THE GREEN PASS FOR THE JUNIT TEST PLEASE HELP!!!! CORRECT MY CODE!
//Bank.java
package bank;
import java.util.HashSet;
import java.util.Set;
public class Bank {
@SuppressWarnings("unused")
private String bankName;
private Set
public Bank(String name) {
this.bankName = name;
this.setOfAccounts = new HashSet
}
public void addAccount(String accountName) {
this.setOfAccounts.add(new Account(accountName, 0)) ;
public void addAccount(String accountName, double openingBalance) {
this.setOfAccounts.add(new Account(accountName, 0, openingBalance)) ;
}
}
//Account.java
package bank;
public class Account {
private String accountName;
private long accountNumber;
private double balance;
public Account (String name, long number, double balance) {
this.accountName = name;
this.accountNumber = number;
this.balance = balance;
}
public Account( String accountName, long accountNumber) {
this.accountName = accountName;
this.accountNumber = accountNumber;
this.balance =0;
}
public String getAccountName () {
return accountName;
}
public void setAccountName(String name) {
this.accountName = name;
}
public long getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(long accountNumber) {
this.accountNumber =accountNumber;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double deposit(double amount) {
if(amount > 0)
this.setBalance(this.getBalance()+amount);
return this.getBalance();
}
public double withdraw( double amount) {
if (amount > 0){
double currentBalance = this.getBalance();
double balanceAfterWithdrawal = currentBalance - amount;
if(balanceAfterWithdrawal >0)
this.setBalance(this.getBalance()- amount) ;
}
return this.getBalance();
}
}
//AccountTest.java
package bank;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
@SuppressWarnings("deprecation")
public class AccountTest {
static Account account1;
static Account account2;
@BeforeClass
public static void setUp(){
account1 = new Account("First", 1111, 1000.0);
account1 = new Account("Second",2222); }
@Test
public void testInit(){
Assert.assertNotNull(account1); Assert.assertNotNull(account2); }
@Test
public void tesDepositPositive(){
Assert.assertEquals(1010.0, account1.deposit(10)); }
@Test
public void testDepositNegative(){
Assert.assertEquals(1010.0, account1.deposit(-10)); }
@Test
public void testWithdrawPositive(){
Assert.assertEquals(1000.0, account1.withdraw(10)); }
@Test
public void testWithdrawNegative(){
Assert.assertEquals(1000.0, account1.withdraw(-10)); }
@Test public void testWithdrawOverdraft(){
Assert.assertEquals(0.0, account2.deposit(-10)); }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started