Page 1 of 1

import java.util.Arrays; public class Bank { public static void main(String[] args) { BankTest acc[] = new BankTest[5];

Posted: Fri May 20, 2022 12:35 pm
by answerhappygod
import java.util.Arrays;
public class Bank
{
public static void main(String[] args)
{
BankTest acc[] = new BankTest[5];
acc[0] = new BankTest("11582932");
acc[1] = new BankTest("35820483");
acc[2] = new BankTest("19302839");
acc[3] = new BankTest("28424883");
acc[4] = new BankTest("67420189");
Bank1 bank1 = new Bank1("Dime Bank", acc, 5);
BankTest newAccount = new BankTest("67420189");
bank1.addAccount(newAccount);
bank1.deposit("35820483", 78);
bank1.withdraw("67420189", 3000);
if (bank1.search("67420189") != null) {
BankTest searchResult = bank1.search("67420189");
System.out.println("Account found.");
} else {
System.out.println("Account does not exists.");
}
System.out.println(bank1.toString());
}
}
class Bank1{
private String nameOfBank;
private BankTest arr[];
private int numOfAccount;
public Bank1(String nameOfBank, BankTest arr[], int
numOfAccount) { // constructor
this.nameOfBank = nameOfBank;
this.arr = arr; // initialising the class variables
this.numOfAccount = numOfAccount;
}
public void addAccount(BankTest acc) { // to add a account
arr = Arrays.copyOf(arr, arr.length + 1); // here i am creating
a new array of length increased by 1
// using the copyof method then assigning it to arr only
arr[numOfAccount++] = acc; // then assigning the new account to
that index
}
public BankTest search(String id) { // to search
for (int i = 0; i < arr.length; i++) { // iterating the
array
if (arr.id.equals(id)) { // if the array id matches then
returning the array
return arr;
}
}
return null; // else returning null
}
public void deposit(String id, int Money) { // to deposit
BankTest ac = search(id); // here using the class method search
to first the search the account then
if (ac != null)
{ // if the result is not null, then using the deposit method of
the account to
// deposit
ac.deposit(Money);
}
else
{
System.out.println("Account does not exist"); // else printing
account does not exist
}
}
public void withdraw(String id, int Money) { // to withdraw
BankTest ac = search(id); // again using the search method to
first search the account, then
if (ac != null) { // if not null then
ac.withdraw(Money); // using the withdraw method of the account,
depositing the moey
} else {
System.out.println("Account does not exist"); // else printing
does not exist
}
}
public String toString() { // returing the name and number of
accounts in the bank
return nameOfBank + " " + numOfAccount;
}
}
====
public interface BankInterface {
void addAccount(BankTest acc) ;
BankTest search(String id) ;
void deposit(String id, int Money);
void withdraw(String id, int Money) ;
}
====
import org.junit.Test;
class TestBank2 {
String id;
public TestBank2(String id) {
this.id = id;
}
// callng methods that have accounts
public void deposit(int amount) {
System.out.println(" Account id used to deposit the money is:
" + id);
}
public void withdraw(int money) {
System.out.println("Account id which was used to withdraw money is:
" + id);
}
@Test
public void testDeposit() { }
}
===