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

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

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

Post 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() { }
}
===
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply