UML-Diagram-Bank java

A Simple Collection Class

“A Simple Collection Class” is a practical example of how to use Collection classes in Java.

First of all, let’s have a look at the UML Diagram:

The EasyScanner Class


import java.util.*;

public class EasyScanner
{
public static int nextInt()
{
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
return i;
}

public static double nextDouble()
{
Scanner sc = new Scanner(System.in);
double d = sc.nextDouble();
return d;
}

public static String nextString()
{
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
return s;
}

public static char nextChar()
{
Scanner sc = new Scanner(System.in);
char c = sc.next().charAt(0);
return c;
}
}

The BankAccount Class


public class BankAccount
{
// the attributes
private String accountNumber;
private String accountName;
private double balance;

// the methods

// the constructor
public BankAccount(String numberIn, String nameIn)
{
accountNumber = numberIn;
accountName = nameIn;
balance = 0;
}

// methods to read the attributes
public String getAccountName()
{
return accountName;
}

public String getAccountNumber()
{
return accountNumber;
}

public double getBalance()
{
return balance;
}

// methods to deposit and withdraw money
public void deposit(double amountIn)
{
balance = balance + amountIn;
}
public boolean withdraw(double amountIn)
{
if(amountIn > balance)
{
return false;
}
else
{
balance = balance - amountIn;
return true;
}
}
}

The Bank Class:


public class Bank
{
// attributes
private BankAccount[] list; // to hold the accounts
private int total; // to keep track of the number of accounts in the list

// methods
// the constructor

public Bank(int sizeIn)
{
// size array with parameter
list = new BankAccount[sizeIn];
total = 0;
}

// helper method to find the index of a specified account
private int search(String accountNumberIn)
{
for(int i = 0; i < total; i++)
{
BankAccount tempAccount = list[i]; // find the account at index i
String tempNumber = tempAccount.getAccountNumber(); // get account number
if(tempNumber.equals(accountNumberIn)) // if this is the account we are looking for
{
return i; // return the index
}
}
return -999;
}

// return the total number of accounts in the list
public int getTotal()
{
return total;
}

// check if the list is empty
public boolean isEmpty()
{
if (total == 0)
{
return true; // list is empty
}
else
{
return false; // list is not empty
}
}

// check if the list is full
public boolean isFull()
{
if (total == list.length)
{
return true; // list is full
}
else
{
return false; // list is empty
}
}

// add an item to the array
public boolean add(BankAccount accountIn)
{
if(search(accountIn.getAccountNumber()) == -999){
if (!isFull()) // check if list is full
{
list[total] = accountIn; // add item
total++; // increment total
return true; // indicate success
}
else
{
return false; // indicate failure
}
}
else {
return false;
}

}

// return an account at a particular place in the list
public BankAccount getItem(int positionIn)
{
if(positionIn < 1 || positionIn > total)
{
return null; // indicate invalid index
}
else
{
return list[positionIn - 1];
}
}

// return an account with a particular account number
public BankAccount getItem(String accountNumberIn)
{
int index;
index = search(accountNumberIn);
if(index == -999)
{
return null; // indicate invalid index
}
else
{
return list[index];
}
}

// deposit money in a specified account
public boolean depositMoney(String accountNumberIn, double amountIn)
{
int index = search(accountNumberIn);
if(index == -999) // there was no such account number
{
return false; // indicate failure
}
else
{
list[index].deposit(amountIn);
return true; // indicate success
}
}

// withdraw money from a specified account
public boolean withdrawMoney(String accountNumberIn, double amountIn)
{
boolean okToWithdraw;
int index = search(accountNumberIn);
if(index == -999) // there was no such account number
{
return false; // indicate failure due to incorrect account number
}
else
{
okToWithdraw = list[index].withdraw(amountIn); // store boolean return value
return okToWithdraw; // false if insufficient funds and true if withdraw successful
}
}

// remove an account
public boolean remove(String numberIn)
{
int index = search(numberIn); // find index of account
if(index == -999) // if no such account
{
return false; // remove was unsuccessful
}
else
{ // overwrite items by shifting other items along
for(int i = index; i< = total-2; i++)
{
list[i] = list[i+1];
}

total--; // decrement total number of accounts
return true; // remove was successful

}
}
}

The Program Tester:


public class BankProgram
{
public static void main(String[] args)
{
char choice;
int size;
System.out.print("Maximum number of accounts? ");
size = EasyScanner.nextInt();
// create Bank object
Bank myBank = new Bank(size);

// offer menu
do
{
System.out.println();
System.out.println("1. Create new account");
System.out.println("2. Remove an account");
System.out.println("3. Deposit money");
System.out.println("4. Withdraw money");
System.out.println("5. Check account details");
System.out.println("6. Display all the account");
System.out.println("7. Quit");
System.out.println();
System.out.print("Enter choice [1-6]: ");

// get choice
choice = EasyScanner.nextChar();
System.out.println();

// process menu options
switch (choice)
{
case '1': option1(myBank);
break;
case '2': option2(myBank);
break;
case '3': option3(myBank);
break;
case '4': option4(myBank);
break;
case '5': option5(myBank);
break;
case '6': option6(myBank);
case '7': break;
default: System.out.println("Invalid entry");
}
}
while (choice != '7');
}

// add account
static void option1(Bank bankIn)
{
// get details from user
System.out.print("Enter account number: ");
String number = EasyScanner.nextString();
System.out.print("Enter account name: ");
String name = EasyScanner.nextString();
// create new account
BankAccount account = new BankAccount(number, name);
// add account to list
boolean ok = bankIn.add(account); if (!ok)
{
System.out.println("The list is full or the account number already exist");
}
else
{
System.out.println("Account created");
}
}

// remove account
static void option2(Bank bankIn)
{
// get account number of account to remove
System.out.print("Enter account number: ");
String number = EasyScanner.nextString();
// delete item if it exists
boolean ok = bankIn.remove(number); if (!ok)
{
System.out.println("No such account number");
}
else
{
System.out.println("Account removed");
}
}

// deposit money in an account
static void option3(Bank bankIn)
{
// get details from user
System.out.print("Enter account number: ");
String number = EasyScanner.nextString();
System.out.print("Enter amount to deposit: ");
double amount = EasyScanner.nextDouble();
boolean ok = bankIn.depositMoney(number, amount); // attempt to deposit
if (!ok)
{
System.out.println("No such account number");
}
else
{
System.out.println("Money deposited");
}
}

// withdraw money from an account
static void option4(Bank bankIn)
{
// get details from user
System.out.print("Enter account number: ");
String number = EasyScanner.nextString();
System.out.print("Enter amount to withdraw: ");
double amount = EasyScanner.nextDouble();
boolean ok = bankIn.withdrawMoney(number, amount); // attempt to withdraw
if (!ok)
{
System.out.println("No such account number or insufficient funds");
}
else
{
System.out.println("Money withdrawn");
}
}

// check account details
static void option5(Bank bankIn)
{
// get details from user
System.out.print("Enter account number ");
String number = EasyScanner.nextString();
BankAccount account = bankIn.getItem(number);
if (account == null)
{
System.out.println("No such account number");
}
else
{
System.out.println("Account number: " + account.getAccountNumber());
System.out.println("Account name: " + account.getAccountName());
System.out.println("Balance: " + account.getBalance());
System.out.println();
}
}
static void option6(Bank bankIn) {
for (int i=1; i< = bankIn.getTotal();i++)
{
System.out.println("The account number for account "+ i +" is: ");
System.out.println(bankIn.getItem(i).getAccountNumber());
System.out.println("The account name for account "+ i +" is: ");
System.out.println(bankIn.getItem(i).getAccountName());
}
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *