Question

java code ============= public class BankAccount { private String accountID; private double balance; /** Constructs a...

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

  • BankAccount Class
    • Constructing a Bank Account with zero Balance.
    • Constructing a Bank Account with Initial Balance greater than zero.
    • Setting AccountID (AccountID should only accept a string of length 4 with first letter as a character followed by 3 integers, Eg., "A001", is a valid AccountID).
    • Getting AccountID.
    • Depositing an amount in Bank.
    • Withdrawing an amount from bank.
    • Getting balance amount.
0 0
Add a comment Improve this question Transcribed image text
Answer #1


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.

Add a comment
Know the answer?
Add Answer to:
java code ============= public class BankAccount { private String accountID; private double balance; /** Constructs a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • java code ========= public class BankAccount { private String accountID; private double balance; /** Constructs a...

    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...

  • Declare an interface Filter as follows: public interface Filter {boolean accept (Object x); } Modify the...

    Declare an interface Filter as follows: public interface Filter {boolean accept (Object x); } Modify the implementation of the DataSet class in Section 10.4 to use both a Measurer and a Filter object. Only objects that the filter accepts should be processed. Demonstrate your modification by processing a collection of bank accounts, filtering out all accounts with balances less than $1,000. Please use the code provided and fill in what is needed. BankAccount.java /**    A bank account has a...

  • Write a Java class called BankAccount (Parts of the code is given below), which has two...

    Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double), two constructors and five methods getName(), getBalance(), deposit (double amount), withdraw(double amount) and toString(). The first constructor should initialize name to null and balance to 0. The second constructor initializes name and balance to the parameters passed. deposit method deposits the amount to the account causing the current balance to increase, withdraw method withdraws the amount causing the...

  • Write a Java class called BankAccount (Parts of the code is given below), which has two...

    Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double), two constructors and five methods getName(), getBalance(), deposit (double amount), withdraw(double amount) and toString(). The first constructor should initialize name to null and balance to 0. The second constructor initializes name and balance to the parameters passed. deposit method deposits the amount to the account causing the current balance to increase, withdraw method withdraws the amount causing the...

  • Implement a class Portfolio. This class has two objects, checking and savings, of the type BankAccount....

    Implement a class Portfolio. This class has two objects, checking and savings, of the type BankAccount. Implement four methods: • public void deposit(double amount, String account) • public void withdraw(double amount, String account) • public void transfer(double amount, String account) • public double getBalance(String account) Here the account string is "S" or "C". For the deposit or withdrawal, it indicates which account is affected. For a transfer, it indicates the account from which the money is taken; the money is...

  • C++ Please create the class named BankAccount with the following properties below: // The BankAccount class...

    C++ Please create the class named BankAccount with the following properties below: // The BankAccount class sets up a clients account (name & ID), keeps track of a user's available balance. // It also keeps track of how many transactions (deposits and/or withdrawals) are made. public class BankAccount { Private String name private String id; private double balance; private int numTransactions; // Please define method definitions for Accessors and Mutator functions below Private member variables string getName(); // No set...

  • [JAVA] < Instruction> You are given two classes, BankAccount and ManageAccounts ManageAccounts is called a Driver...

    [JAVA] < Instruction> You are given two classes, BankAccount and ManageAccounts ManageAccounts is called a Driver class that uses BankAccount. associate a member to BankAcount that shows the Date and Time that Accounts is created. You may use a class Date. To find API on class Date, search for Date.java. Make sure to include date information to the method toString(). you must follow the instruction and Upload two java files. BankAccount.java and ManageAccounts.java. -------------------------------------------------------------------------- A Bank Account Class File Account.java...

  • TASK 1 Create a new class called CheckingAccount that extends BankAccount. It should contain a static...

    TASK 1 Create a new class called CheckingAccount that extends BankAccount. It should contain a static constant FEE that represents the cost of clearing onecheck. Set it equal to 15 cents. Write a constructor that takes a name and an initial amount as parameters. Itshould call the constructor for the superclass. It should initializeaccountNumber to be the current value in accountNumber concatenatedwith -10 (All checking accounts at this bank are identified by the extension -10). There can be only one...

  • The current code I have is the following: package uml; public class uml {        public...

    The current code I have is the following: package uml; public class uml {        public static void main(String[] args) {              // TODO Auto-generated method stub        } } class Account { private String accountID; public Account(String accountID) { this.accountID = accountID; } public String getAccountID() { return accountID; } public void setAccountID(String accountID) { this.accountID = accountID; } @Override public String toString() { return "Account [accountID=" + accountID + "]"; } } class SuppliesAccount extends Account { private...

  • Write a unit test to test the following class. Using Java : //Test only the debit...

    Write a unit test to test the following class. Using Java : //Test only the debit method in the BankAccount. : import java.util.*; public class BankAccount { private String customerName; private double balance; private boolean frozen = false; private BankAccount() { } public BankAccount(String Name, double balance) { customerName = Name; this.balance = balance; } public String getCustomerName() { return customerName; } public double getBalance() { return balance; } public void setDebit(double amount) throws Exception { if (frozen) { throw...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT