Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Homework – Topic 7 – Bank Accounts: You have been hired as a programmer by a maj

ID: 3700303 • Letter: H

Question

Homework – Topic 7 – Bank Accounts:
You have been hired as a programmer by a major bank. Your first project is a small banking transaction system. Each account
consists of a number and a balance. The user of the program (the teller) can create a new account, as well as perform deposits,
withdrawals, and balance inquiries.
Initially, the account information of existing customers is to be read into a pair of parallel arrays--one for account numbers, the
other for balances. The bank can handle up to MAX_NUM accounts. Use the following method to read in the data values:
public static int readAccts(int[] acctNum, double[] balance, int maxAccts)
This method fills up the account number and balance arrays (up to maxAccts) and returns the actual number of accounts read in
(later referred to as numAccts).
After initialization, the main program prints the initial database of accounts and balances. Use method printAccts()
described below.
The program then allows the user to select from the following menu of transactions:
Select one of the following:
W - Withdrawal
D - Deposit
N - New account
B - Balance
Q – Quit
X – Delete Account – Extra Credit
Use the following method to produce the menu:
public static void menu()
This method only displays the menu. The main program then prompts the user for a selection. You should verify that the user
has typed in a valid selection (otherwise print out an error message and repeat the prompt).
Once the user has entered a selection, one of the following methods should be called to perform the specific transaction. At the
end, before the user quits, the program prints the final contents of the account and balance arrays.
public static int findAcct(int[] acctNum, int numAccts, int account);
This method returns the index of account in the acctNum array if the account exists, and -1 if it doesn't. It is called by all the
remaining methods.
public static void withdrawal(int[] acctNum, double[] balance, int numAccts);
This method prompts the user for the account number. If the account does not exist, it prints an error message. Otherwise, it asks
the user for the amount of the withdrawal. If the account does not contain sufficient funds, it prints an error message and does not
perform the transaction.
pubic static void deposit(int[] acctNum, double[] balance, int num_accts);
This method prompts the user for the account number. If the account does not exist, it prints an error message. Otherwise, it asks
the user for the amount of the deposit.
public static int newAcct(int[] acctNum, double[] balance, int numAccts);
This method prompts the user for a new account number. If the account already exists, it prints an error message. Otherwise, it
adds the account to the acctNum array with an initial balance of 0. It returns the new number of accounts.
public static void balance(int[] acctNum, double[] balance, int numAccts);
This method prompts the user for an account number. If the account does not exist, it prints an error message. Otherwise, it prints
the account balance.
public static void printAccts(int[] acctNum, double[] balance, int numAccts);
This method prints all customer information--account number and balance.
EXTRA CREDIT 1:
public static int deleteAcct(int[] acctNum, double[] balance, int numAccts);
This method prompts the user for an account number. If the account does not exist, it prints an error message. Otherwise, it
deletes the account. It returns the new number of accounts.
or
EXTRA CREDIT 2:
public static int deleteAcct(int[] acctNum, double[] balance, int numAccts);
This method prompts the user for an account number. If the account does not exist, or if the account exists but has a non-zero
balance, it prints an error message. Otherwise, it deletes the account. It returns the new number of accounts.
Notes:
1. All output must be file directed (you must add additional parameters to the methods as needed)
2. Only output must go to the file - not interactive prompts or menus (which go to the monitor).
3. No global variables are allowed
4. The program and all methods must be properly commented.
5. The program must be properly tested.
a. The initial database should consist of at least 7 accounts
b. The initial database should be printed to the file
c. Test at least 2 invalid menu selections
d. Test at least 2 balance inquiries:
i. valid account
ii. invalid account
e. Test at least 3 deposits:
i. valid account - valid deposit amount
ii. valid account - invalid deposit amount
iii. invalid account
f. Test at least 4 withdrawals:
i. valid account - valid withdrawal amount
ii. valid account - invalid withdrawal amount
iii. valid account - insufficient funds
iv. invalid account
g. Create at least 3 new accounts with an initial balance of 0.0
h, Test the creation of at least 1 invalid new account
i. Test several transactions on the new accounts (deposits, withdrawals, etc.)
j. Extra Credit: Delete at least two accounts (both must be old accounts that had no transactions)
State whether you are doing Extra Credit 1 or Extra Credit 2.
Extra Credit 1 is worth +1; Extra Credit 2 1s worth +2
k. quit and print the final database
Sample Output:
Transaction Type: Deposit
Account Number: 9876
Amount to Deposit: $123.45
Old Balance: $300.50
New Balance: $423.95

Explanation / Answer

import java.io.File;

import java.io.FileReader;

import java.io.BufferedReader;

import java.util.Scanner;

/*

ACCOUNT DATABASE FILE NAME accounts.txt

Format is as follows

Account number and balance separated by a space

123 20.0

234 300.20

456 2929.00

678 10000.00

*/

class Bank

{

public static int readAccts(int[] acctNum,double[]balance,int maxAccts)

{

int numAccts = 0;

try

{

File f = new File("accounts.txt"); //opening file

FileReader fr = new FileReader(f);//filereader and bufferedReader to read file contents

BufferedReader br = new BufferedReader(fr);

String line = br.readLine();

int acNum; //to store i_th account number and balance

double bal;

String values[] = new String[2]; //string array to store account number and balance

while(line!=null) //Until file is read completed or an exception occured

{

values = line.trim().split(" "); //trim the line to remove leading whitesapces and split by " "

acNum = Integer.parseInt(values[0]); //convert acNum which is in string to Int

bal = Double.parseDouble(values[1]); //Convert balance which is in string to Dobule

acctNum[numAccts] = acNum; //store account number and balance in respective arrays

balance[numAccts] = bal;

numAccts++; //increament count of number of accounts

line = br.readLine(); //read nextLine

}

}

catch(Exception e)

{

System.out.println("Error Occured while reading file");

System.out.println("Number of A/C read till now = "+numAccts+" continuing program");

}

//fills balence and account number arrays and check for maxAccts violation

//returns current total number of accounts

printAccts(acctNum,balance,numAccts);

return numAccts;

}

public static void printAccts(int[] acctNum,double[]balance,int numAccts)

{

int i;

//prints account number and balence

if(numAccts == 0)

{

System.out.println("No Accounts to print");

}

for(i = 0;i<numAccts;i++)

{

System.out.println((i+1)+". Account Number = "+acctNum[i]+" Balance = "+balance[i]);

}

}

public static void menu(int[] acctNum,double[]balance,int maxAccts)

{

String choice = "0";

Scanner in = new Scanner(System.in);

int numAccts = readAccts(acctNum,balance,maxAccts);

while(true)

{

System.out.println("Enter :- W - Withdrawal");

System.out.println("D - Deposit");

System.out.println("N - New account");

System.out.println("B - Balance");

System.out.println("Q - Quit");

System.out.println("X - Delete Account");

System.out.println("P - Print Accounts");

System.out.print("Enter Your Choice : ");

choice = in.nextLine();

if(choice.equalsIgnoreCase("W"))

{

withdrawl(acctNum,balance,numAccts);

}

else if(choice.equalsIgnoreCase("D"))

{

deposit(acctNum,balance,numAccts);

}

else if(choice.equalsIgnoreCase("N"))

{

numAccts = newAcct(acctNum,balance,numAccts,maxAccts);

}

else if(choice.equalsIgnoreCase("B"))

{

balance(acctNum,balance,numAccts);

}

else if(choice.equalsIgnoreCase("X"))

{

numAccts = deleteAcct(acctNum,balance,numAccts);

}

else if(choice.equalsIgnoreCase("Q"))

{

printAccts(acctNum,balance,numAccts);

break;

}

else if(choice.equalsIgnoreCase("P"))

{

printAccts(acctNum,balance,numAccts);

}

else

{

System.out.println("Invalid Input Try Again!!");

}

}

}

public static int findAcct(int[] acctNum,int numAccts,int account)

{

int i;

int pos = -1; //finds passed account number is in acctNum array returns pos else -1

for(i=0;i<numAccts;i++)

{

if(acctNum[i]==account)

{

pos = i;

break;

}

}

return pos;

}

public static void withdrawl(int[]acctNum,double[] balance,int numAccts)

{

//prompts user for account number

Scanner in = new Scanner(System.in);

System.out.print("Enter Account Number : ");

int acNum = in.nextInt();

int pos = findAcct(acctNum,numAccts,acNum); //finds account number position in array

if(pos == -1)

{

System.out.println("Account Number is not found");//if account number is not found

}

else

{

System.out.println("Enter Withdrawl amount : ");//if a/c num found asks withdrawl amount

double bal = in.nextDouble();

if(balance[pos]>= bal) //if enterd balance is greater than available balance prints Insufficient funds

{

balance[pos]-=bal; //else removes entered balance from available balance

System.out.println("Last Withdrawl is Successful");

}

else

{

System.out.println("Insufficient funds");

}

}

}

public static void deposit(int[] acctNum,double[] balance,int numAccts)

{

//prompts user for account number

Scanner in = new Scanner(System.in);

System.out.print("Enter Account Number : ");

int acNum = in.nextInt();

int pos = findAcct(acctNum,numAccts,acNum);

if(pos == -1)

{

System.out.println("Account Number is not found");

}

else

{

System.out.println("Enter Deposit amount : ");

double bal = in.nextDouble(); //if account number is found asks deposit amount and adds to the account holder balance

balance[pos]+=bal;

System.out.println("Last Deposit is Successfull");

}

}

public static int newAcct(int[] acctNum,double[] balance,int numAccts,int maxAccts)

{

//prmpts user for a new account number

//set initial balance of new account to 0

if(numAccts<maxAccts)

{

Scanner in = new Scanner(System.in);

System.out.print("Enter Account Number : ");

int acNum = in.nextInt();

int pos = findAcct(acctNum,numAccts,acNum);

if(pos != -1) // if pos == -1 then that account number is not available

{

System.out.println("Account Number Already Available");

}

else

{

acctNum[numAccts] = acNum; //insert account number into array and increment numAccts

balance[numAccts] = 0.0;

numAccts++;

System.out.println("New Account is Created");

}

}

else

{

System.out.println("Total Accounts reached maximum limit");

}

return numAccts;

}

public static void balance(int[] acctNum,double[] balance,int numAccts)

{

//prompts user for a account number

Scanner in = new Scanner(System.in);

System.out.print("Enter Account Number : ");

int acNum = in.nextInt();

int pos = findAcct(acctNum,numAccts,acNum);

if(pos == -1)

{

System.out.println("Account Number Entered is not available");

}

else

{

System.out.println("Balance = "+balance[pos]); //prints balance of account if entered a/c num is available

}

}

public static int deleteAcct(int[] acctNum,double[] balance,int numAccts)

{

//prompts user for a account number

if(numAccts>0)

{

Scanner in = new Scanner(System.in);

System.out.print("Enter Account Number : ");

int acNum = in.nextInt();

int pos = findAcct(acctNum,numAccts,acNum);

if(pos == -1)

{

System.out.println("Account Number Entered is not available");

}

else

{

//if account number to delete is correct

//replaces the entered account number and balance with next account number and balance

//and all are swift to forward

int i;

for(i=pos;i+1<numAccts;i++)

{

acctNum[i] = acctNum[i+1];

balance[i] = balance[i+1];

}

numAccts--;

}

}

else

{

System.out.println("No Accounts to remove");

}

return numAccts;//returns num of accounts

}

public static void main(String args[])

{

int maxAccts = 10;

int[] acctNum = new int[maxAccts];

double balance[] = new double[maxAccts];

menu(acctNum,balance,maxAccts);

}

}