Exercises using JUnit and array of objects 1. Money and Account exercises 2. Design and implement BankTest and Bank. Acc

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Exercises using JUnit and array of objects 1. Money and Account exercises 2. Design and implement BankTest and Bank. Acc

Post by answerhappygod »

Exercises Using Junit And Array Of Objects 1 Money And Account Exercises 2 Design And Implement Banktest And Bank Acc 1
Exercises Using Junit And Array Of Objects 1 Money And Account Exercises 2 Design And Implement Banktest And Bank Acc 1 (641.62 KiB) Viewed 55 times
=======
======
public class Account
{
private String name;
private String id;
private Money balance = new Money(0, 0);
//constructor for Account wiht attributes like name ,
id and balance
public Account(String nameInput, String idInput,
Money balanceInput)
{
this.name = nameInput;
this.id = idInput;
this.balance = balanceInput;
}
public Account(Account other)
{
this.name = other.name;
this.id = other.id;
this.balance = other.balance;
}
//deposit method will add money to taccount
public void deposit(Money other)
{
if(other.compareTo(new Money (0, 0)) ==
0 || other.compareTo(new Money (0, 0)) == 1)
{
this.balance =
this.balance.add(other);
}
}
//withdraw will withdraw the money from
account
public void withdraw(Money other)
{
if(other.compareTo(this.balance) == 0
|| other.compareTo(this.balance) == -1)
{
this.balance =
this.balance.subtract(other);
}
}
//method to transfer the money
public void transfer(Account other, Money cash)
{
this.balance =
this.balance.add(cash);
other.balance =
other.balance.subtract(cash);
}
//to string method
public String toString()
{
return "" + this.name + ", " + this.id
+ ", " + this.balance.toString();
}
//method to return the ID
public String returnID()
{
return new String(id);
}
//boolean equal method to check if attributed of
two accounts are equal or not.
public boolean equals(Account other)
{
if(this.name.equals(other.name)
&& this.id.equals(other.id) &&
this.balance.equals(other.balance))
{
return true;
}
return false;
}
}
======
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class AccountTest
{
private Account account;
public AccountTest()
{
}
@Before
public void setUp()
{
account = new Account("Name", "001",
new Money (0, 0));
}
@After
public void tearDown()
{
account = null;
}
// first test: is money deposited in
account?
@Test
public void testDeposit()
{
Money depositCash = new Money (5,
0);
Account expectedResult = new
Account("Name", "001", depositCash);
account.deposit(depositCash);
assertTrue ("Error in testDeposit",
account.equals(expectedResult));
}
// test: money withdrawn
@Test
public void testWithdraw()
{
Money withdrawCash = new Money (5,
0);
Account account2 = new Account("Name",
"001", new Money (10, 0));
Account expectedResult = new
Account("Name", "001", new Money (5, 0));
account2.withdraw(withdrawCash);
assertTrue ("Error in testWithdraw",
account2.equals(expectedResult));
}
// test: money transfer
@Test
public void testTransfer()
{
Money transferCash = new Money (5,
0);
Account account2 = new Account("Name",
"001", new Money (12, 0));
account.transfer(account2,
transferCash);
Account expectedResult1 = new
Account("Name", "001", new Money (5, 0));
Account expectedResult2 = new
Account("Name", "001", new Money (7, 0));
assertTrue ("Error in testTransfer",
account.equals(expectedResult1));
assertTrue ("Error in testTransfer",
account2.equals(expectedResult2));
}
// test to string
@Test
public void testToString()
{
String expectedResult = "Name, 001,
$0.00";
String actualResult =
account.toString();
assertTrue ("Error in testToString",
expectedResult.equals(actualResult));
}
// equality test
@Test
public void testEquals()
{
Account account2 = new Account("Name",
"001", new Money (0, 0));
assertTrue ("Error in testEquals",
account.equals(account2));
Account account3 = new
Account("Nam", "001", new Money (0, 0));
Account account4 = new Account("Name",
"00", new Money (0, 0));
Account account5 = new Account("Name",
"001", new Money (10, 0));
assertFalse ("Error in testEquals",
account.equals(account3));
assertFalse ("Error in testEquals",
account.equals(account4));
assertFalse ("Error in testEquals",
account.equals(account5));
}
}
Exercises using JUnit and array of objects 1. Money and Account exercises 2. Design and implement BankTest and Bank. According to the test-first approach, a test case should be written before the code that makes that test pass. Document each method before implementing. For the ADT Bank, include operations for adding a new account to its array of accounts, depositing an amount to a given account (according to its id number), and withdrawing an amount from a given account (according to its id number). Given an account id number, the search method should return the account (in the array accounts) that has that id number. The constructor takes in a string representing the name of the bank, allocates storage for the array (each element of which is a reference to an Account object), and initializes the number of accounts to 0. a Reuse, when possible, methods of the ADT Account as well as methods of the ADT Bank. For example, the deposit and withdraw methods of the ADT Bank can use the search method of ADT Bank as a helper method to first find the account to update within the array of accounts; once the account is found, it can then be updated via a call to the deposit or withdraw method of the ADT Account. Bank nameOfBank : String accounts : Account[] |- numOfAccounts : int + Bank (String) + addAccount (Account) + search (String) : Account // assuming id is a String; see description above. + deposit (String, Money) + withdraw (String, Money) + toString(): String
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply