Title: Simple Banking Record Learning objectives: 1. Use of functions 2. Use of
ID: 3885027 • Letter: T
Question
Title: Simple Banking Record
Learning objectives:
1. Use of functions
2. Use of loops and if statement
3. Use of arrays and pointers
Lab Objective: Part 1
Write a program that will be used by a bank clerk to create a client's account and will then allow the clerk to enter withdraws and deposits on that account. The program should offer the ability for the clerk to view the current balance on the account.
Sample run of the program:
At startup the clerk will get a menu:
MAIN MENU
==========
1. Add a client account
2. Apply transactions on the client account
3. View the balance
4. Exit
Enter your choice:_
If the clerk chooses option 1:
The program will ask to enter an account number and full name;
The program will then return to the main menu.
If the clerk chooses option 2:
The program must first check if the client exists. An appropriate error should display and the program goes back to the main menu.
If a client exists, the program will offer the choice of withdraw or deposit. The program must inspect the balance for overdraft.
If the clerk chooses option 3:
The program will display the account number, the full name and the balance.
Option: exit the program completely!
Part 2: A client can have more than one account. Typically a client would have a checking and a savings account. Rewrite another version of the previous program to accommodate for this feature.
Part 3: This new version of the program will have all of the above features in addition to the ability of applying them to several clients.
Part 4: Make the data persistent. In other words, save all client information for later retrieval.
Explanation / Answer
import java.util.Random;
public class BankOperation
{
public static void main(String[] args)
{
int[] initialBalance = {500, 800}; // The initial account balances.
int[] totalCredits = new int[initialBalance.length]; // Total credits.
int[] totalDebits = new int[initialBalance.length]; // Total debits.
int[] nCredits = new int[initialBalance.length]; // Number of credits.
int[] nDebits = new int[initialBalance.length]; // Number of debits.
int transactionCount = 20; // Number of debits and of credits.
// Create the bank and the clerks:
Bank theBank = new Bank(); // Create a bank.
Clerk clerk1 = new Clerk(theBank); // Create the first clerk.
Clerk clerk2 = new Clerk(theBank); // Create the second clerk.
// Create the accounts, and initialize total credits and debits:
Account[] accounts = new Account[initialBalance.length];
for(int i = 0; i < initialBalance.length; i++)
{
accounts[i] = new Account(i+1, initialBalance[i]); // Create accounts
totalCredits[i] = totalDebits[i] = 0;
nCredits[i] = nDebits[i] = 0;
}
// Create the threads for the clerks as daemon, and start them off:
Thread clerk1Thread = new Thread(clerk1);
Thread clerk2Thread = new Thread(clerk2);
clerk1Thread.setDaemon(true); // Set first as daemon.
clerk2Thread.setDaemon(true); // Set second as daemon.
clerk1Thread.start(); // Start the first.
clerk2Thread.start(); // Start the second.
// Generate transactions of each type and pass to the clerks:
Random rand = new Random(); // Random number generator
Transaction transaction; // Stores a transaction
int amount; // stores an amount of money
int select; // Selects an account
for(int i = 1; i <= transactionCount; i++)
{
// Generate a credit or debit at random:
if(rand.nextInt(2)==1)
{
// Generate a random account index for credit operation:
select = rand.nextInt(accounts.length);
amount = 50 + rand.nextInt(26); // Generate amount of $50 to $75
transaction = new Transaction(accounts[select], // Account
Transaction.CREDIT, // Credit transaction
amount); // of amount
totalCredits[select] += amount; // Keep total credit tally
nCredits[select]++;
clerk1.doTransaction(transaction);
}
else
{
// Generate a random account index for debit operation:
select = rand.nextInt(accounts.length);
amount = 30 + rand.nextInt(31); // Generate amount of $30 to $60.
transaction = new Transaction(accounts[select], // Account.
Transaction.DEBIT, // Debit transaction of amount.
amount);
totalDebits[select] += amount; // Keep total debit tally.
nDebits[select]++;
clerk2.doTransaction(transaction);
}
}
// Wait until both clerks are done:
clerk1.isBusy();
clerk2.isBusy();
// Now output the results:
for(int i = 0; i < accounts.length; i++)
System.out.println("Account Number:"+accounts[i].getAccountNumber()+" "+
"Number of credits : " + nCredits[i] + " " +
"Number of debits : " + nDebits[i] + " " +
"Original balance : $" + initialBalance[i] + " " +
"Total credits : $" + totalCredits[i] + " " +
"Total debits : $" + totalDebits[i] + " " +
"Final balance : $" + accounts[i].getBalance() + " " +
"Should be : $" + (initialBalance[i] + totalCredits[i] -
totalDebits[i]) + " ");
}
}
//***********************
//Bank.java
//***********************
class Bank
{
// Perform a transaction:
public void doTransaction(Transaction transaction)
{
switch(transaction.getTransactionType())
{
case Transaction.CREDIT:
synchronized(transaction.getAccount())
{
System.out.println("Start credit of " +
transaction.getAccount() + " amount: " +
transaction.getAmount());
// Get current balance:
int balance = transaction.getAccount().getBalance();
// Credits require require a lot of checks:
try
{
Thread.sleep(10); // wait() time reduced to speed things up.
}
catch(InterruptedException e)
{
System.out.println(e);
}
balance += transaction.getAmount(); // Increment the balance.
transaction.getAccount().setBalance(balance); // Restore account balance.
/*
System.out.println(" End credit of " +
transaction.getAccount() + " amount: " +
transaction.getAmount());
*/
break;
}
case Transaction.DEBIT:
synchronized(transaction.getAccount())
{
System.out.println("Start debit of " +
transaction.getAccount() + " amount: " +
transaction.getAmount());
// Get current balance
int balance = transaction.getAccount().getBalance();
// Debits require even more checks...
try
{
Thread.sleep(15); // wait()time reduced to speed things up.
}
catch(InterruptedException e)
{
System.out.println(e);
}
balance -= transaction.getAmount(); // Increment the balance.
transaction.getAccount().setBalance(balance); // Restore account balance.
/*
System.out.println(" End debit of " +
transaction.getAccount() + " amount: " +
transaction.getAmount());
*/
break;
}
default: // We should never get here.
System.out.println("Invalid transaction");
System.exit(1);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.