In: Computer Science
java code
============
public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the Account ID * @return the accountID */ public String getAccountID() { return accountID; } /** * Sets the Account ID * @param accountID */ public void setAccountID(String accountID) { this.accountID = accountID; } /** Deposits money into the bank account. @param amount the amount to deposit */ public void deposit(double amount) { balance += amount; } /** Withdraws money from the bank account. @param amount the amount to withdraw @return true if allowed to withdraw */ public boolean withdraw(double amount) { boolean isAllowed = balance >= amount; if (isAllowed) balance -= amount; return isAllowed; } /** Gets the current balance of the bank account. @return the current balance */ public double getBalance() { return balance; } }
==========================
You must write tests for the following:
Unit Testing with JUnit 5
You must write tests for the following (which may include Custom Exceptions):
Tests are written such that any deposit that is made greater than 10000 is not accepted.
Tests are written such that balance in the BankAccount does not go below 0.
Care should be taken for above tests at the time of Initial Deposit and at the time of Withdrawal and future Deposits.
Tests should be written such that the Bank AccountID only accepts a string of length 4 with first letter as a character followed by 3 integers, Eg., "A001", is a valid AccountID.
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import org.junit.Assert; import org.junit.Before; import org.junit.Test; public class BankAccountTest { BankAccount b; @Before public void setup() { b = new BankAccount("A123"); } @Test public void testBadDeposit() { double bal = b.getBalance(); try { b.deposit(10001); Assert.fail(); // If we reached here, it means no exception came. } catch(Exception e) { // we are good here. } // the balance should remain same, Because more than 10000 should not be accepted. assertEquals(bal, b.getBalance(), 0.000001); } @Test public void testLowBalance() { double bal = b.getBalance(); assertFalse(b.withdraw(bal + 100)); // the balance should remain same, Because it can not be negative. assertEquals(bal, b.getBalance(), 0.000001); } @Test public void testValidId() { String ids[] = new String[] {"123", "A", null, "", "AAAA", "a12"}; for(String x: ids) { try { BankAccount ba = new BankAccount(x); Assert.fail(); // If we reached here, it means no exception came. } catch(Exception e) { // we are good here. } } } @Test public void testBalance() { String ids[] = new String[] {"123", "A", null, "", "AAAA", "a12"}; for(String x: ids) { try { BankAccount ba = new BankAccount(x); Assert.fail(); // If we reached here, it means no exception came. } catch(Exception e) { // we are good here. } } } @Test public void testInitialization() { try { BankAccount ba = new BankAccount(-2, "A123"); Assert.fail(); // If we reached here, it means no exception came. } catch(Exception e) { // we are good here. } try { BankAccount ba = new BankAccount(12000, "A123"); Assert.fail(); // If we reached here, it means no exception came. } catch(Exception e) { // we are good here. } } } class BankAccount { private String accountID; private double balance; private void validateId(String accountID) { if(accountID == null || accountID.length() != 4 || !Character.isAlphabetic(accountID.charAt(0)) || !Character.isDigit(accountID.charAt(1)) || !Character.isDigit(accountID.charAt(2)) || !Character.isDigit(accountID.charAt(3))) { throw new IllegalArgumentException("Invalid accountId"); } } /** * Constructs a bank account with a zero balance * * @param accountID - ID of the Account */ public BankAccount(String accountID) { validateId(accountID); balance = 0; this.accountID = accountID; } /** * Constructs a bank account with a given balance * * @param initialBalance the initial balance * @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { validateId(accountID); if(initialBalance > 10000 || initialBalance < 0) { throw new IllegalArgumentException("Invalid Balance"); } this.accountID = accountID; balance = initialBalance; } /** * Returns the Account ID * * @return the accountID */ public String getAccountID() { return accountID; } /** * Sets the Account ID * * @param accountID */ public void setAccountID(String accountID) { this.accountID = accountID; } /** * Deposits money into the bank account. * * @param amount the amount to deposit */ public void deposit(double amount) { if(amount > 10000) { throw new IllegalArgumentException("Deposit amount can not be more than 10000"); } balance += amount; } /** * Withdraws money from the bank account. * * @param amount the amount to withdraw * @return true if allowed to withdraw */ public boolean withdraw(double amount) { boolean isAllowed = balance >= amount; if (isAllowed) balance -= amount; return isAllowed; } /** * Gets the current balance of the bank account. * * @return the current balance */ public double getBalance() { return balance; } }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.