I really need help filling out the rest of the code for new account, delete acco
ID: 3742593 • Letter: I
Question
I really need help filling out the rest of the code for new account, delete account, and withdrawal. The professor wants to see the deleted account in the output and source code so he told us to create a new account that does not exist and delete an account that already existed. I also do not know where is the source code located and will like for you if possible to screen shot and explain how did you find the source code. I am not a good programmer and am a begginer. I will really appreciate the help. I really need help filling out the rest of the code for new account, delete account, and withdrawal. The professor wants to see the deleted account in the output and source code so he told us to create a new account that does not exist and delete an account that already existed. I also do not know where is the source code located and will like for you if possible to screen shot and explain how did you find the source code. I am not a good programmer and am a begginer. I will really appreciate the help.
import java.io.*; import java.util.Scanner;
public class pgm00_01BankAccounts {
public static void main(String[] args) throws IOException { //constant definitions final int MAX_NUM = 50;
//variable declarations int[] acctNumArray = new int[MAX_NUM];//array of account numbers double[] balanceArray = new double[MAX_NUM];//array of balances int numAccts;//number of accounts char choice;//menu item selected boolean not_done = true;//loop control flag
// open input test cases file //File testFile = new File("/bc/cisc3115/pgms/chapter_00/prj00_01BankAccounts/mytestcases.txt"); File testFile = new File("mytestcases.txt"); //create Scanner object Scanner kybd = new Scanner(testFile); //Scanner kybd = new Scanner(System.in);
// open the output file //PrintWriter outFile = new PrintWriter("/bc/cisc3115/pgms/chapter_00/prj00_01BankAccounts/myoutput.txt"); PrintWriter outFile = new PrintWriter("myoutput.txt"); //PrintWriter outFile = new PrintWriter(System.out);
/* first part */ /* fill and print initial database */ numAccts = readAccts(acctNumArray,balanceArray,MAX_NUM); import java.io.*; import java.util.Scanner;
public class pgm00_01BankAccounts {
public static void main(String[] args) throws IOException { //constant definitions final int MAX_NUM = 50;
//variable declarations int[] acctNumArray = new int[MAX_NUM];//array of account numbers double[] balanceArray = new double[MAX_NUM];//array of balances int numAccts;//number of accounts char choice;//menu item selected boolean not_done = true;//loop control flag
// open input test cases file //File testFile = new File("/bc/cisc3115/pgms/chapter_00/prj00_01BankAccounts/mytestcases.txt"); File testFile = new File("mytestcases.txt"); //create Scanner object Scanner kybd = new Scanner(testFile); //Scanner kybd = new Scanner(System.in);
// open the output file //PrintWriter outFile = new PrintWriter("/bc/cisc3115/pgms/chapter_00/prj00_01BankAccounts/myoutput.txt"); PrintWriter outFile = new PrintWriter("myoutput.txt"); //PrintWriter outFile = new PrintWriter(System.out);
/* first part */ /* fill and print initial database */ numAccts = readAccts(acctNumArray,balanceArray,MAX_NUM); printAccts(acctNumArray,balanceArray,numAccts,outFile); /* second part */ /* prompts for a transaction and then */ /* call functions to process the requested transaction */ do { menu(); choice = kybd.next().charAt(0); switch(choice) { case 'q': case 'Q': not_done = false; printAccts(acctNumArray,balanceArray,numAccts,outFile); break; case 'b': case 'B': balance(acctNumArray,balanceArray,numAccts,outFile,kybd); break; case 'd': case 'D': deposit(acctNumArray,balanceArray,numAccts,outFile,kybd); break; case 'w': case 'W': withdrawal(acctNumArray,balanceArray,numAccts,outFile,kybd); break; case 'n': case 'N': numAccts = newAcct(acctNumArray,balanceArray,numAccts,outFile,kybd); break; case 'x': case 'X': numAccts = deleteAcct(acctNumArray,balanceArray,numAccts,outFile,kybd); break; default: outFile.println("Error: " + choice + " is an invalid selection - try again"); outFile.println(); outFile.flush(); break; } // give user a chance to look at output before printing menu //pause(kybd); } while (not_done); //close the output file outFile.close(); //close the test cases input file kybd.close(); System.out.println(); System.out.println("The program is terminating"); }
/* Method readAccts() * Input: * acctNumArray - reference to array of account numbers * balanceArray - reference to array of account balances * maxAccts - maximum number of active accounts allowed * Process: * Reads the initial database of accounts and balances * Output: * Fills in the initial account and balance arrays and returns the number of active accounts */ public static int readAccts(int[] acctNumArray, double[] balanceArray, int maxAccts) throws IOException { // open database input file //create File object //File dbFile = new File("/bc/cisc3115/pgms/chapter_00/prj00_01BankAccounts/myinput.txt"); File dbFile = new File("myinput.txt");
//create Scanner object Scanner sc = new Scanner(dbFile); int count = 0; //account number counter
while (sc.hasNext() && count < maxAccts) { acctNumArray[count] = sc.nextInt(); balanceArray[count] = sc.nextDouble(); count++; }
//close the input file sc.close(); //return the account number count return count; } printAccts(acctNumArray,balanceArray,numAccts,outFile); /* second part */ /* prompts for a transaction and then */ /* call functions to process the requested transaction */ do { menu(); choice = kybd.next().charAt(0); switch(choice) { case 'q': case 'Q': not_done = false; printAccts(acctNumArray,balanceArray,numAccts,outFile); break; case 'b': case 'B': balance(acctNumArray,balanceArray,numAccts,outFile,kybd); break; case 'd': case 'D': deposit(acctNumArray,balanceArray,numAccts,outFile,kybd); break; case 'w': case 'W': withdrawal(acctNumArray,balanceArray,numAccts,outFile,kybd); break; case 'n': case 'N': numAccts = newAcct(acctNumArray,balanceArray,numAccts,outFile,kybd); break; case 'x': case 'X': numAccts = deleteAcct(acctNumArray,balanceArray,numAccts,outFile,kybd); break; default: outFile.println("Error: " + choice + " is an invalid selection - try again"); outFile.println(); outFile.flush(); break; } // give user a chance to look at output before printing menu //pause(kybd); } while (not_done); //close the output file outFile.close(); //close the test cases input file kybd.close(); System.out.println(); System.out.println("The program is terminating"); }
/* Method readAccts() * Input: * acctNumArray - reference to array of account numbers * balanceArray - reference to array of account balances * maxAccts - maximum number of active accounts allowed * Process: * Reads the initial database of accounts and balances * Output: * Fills in the initial account and balance arrays and returns the number of active accounts */ public static int readAccts(int[] acctNumArray, double[] balanceArray, int maxAccts) throws IOException { // open database input file //create File object //File dbFile = new File("/bc/cisc3115/pgms/chapter_00/prj00_01BankAccounts/myinput.txt"); File dbFile = new File("myinput.txt");
//create Scanner object Scanner sc = new Scanner(dbFile); int count = 0; //account number counter
while (sc.hasNext() && count < maxAccts) { acctNumArray[count] = sc.nextInt(); balanceArray[count] = sc.nextDouble(); count++; }
//close the input file sc.close(); //return the account number count return count; } /* Method printAccts: * Input: * acctNumArray - array of account numbers * balanceArray - array of account balances * numAccts - number of active accounts * outFile - reference to the output file * Process: * Prints the database of accounts and balances * Output: * Prints the database of accounts and balances */ public static void printAccts(int[] acctNumArray, double[] balanceArray, int numAccts, PrintWriter outFile) { outFile.println(); outFile.println(" Database of Bank Accounts"); outFile.println(); outFile.println("Account Balance"); for (int index = 0; index < numAccts; index++) { outFile.printf("%7d $%7.2f", acctNumArray[index], balanceArray[index]); outFile.println(); } outFile.println();
//flush the output file outFile.flush(); }
/* Method menu() * Input: * none * Process: * Prints the menu of transaction choices * Output: * Prints the menu of transaction choices */ public static void menu() { System.out.println(); System.out.println("Select one of the following transactions:"); System.out.println(" ****************************"); System.out.println(" List of Choices "); System.out.println(" ****************************"); System.out.println(" W -- Withdrawal"); System.out.println(" D -- Deposit"); System.out.println(" N -- New Account"); System.out.println(" B -- Balance Inquiry"); System.out.println(" X -- Delete Account"); System.out.println(" Q -- Quit"); System.out.println(); System.out.print(" Enter your selection: "); } /* Method printAccts: * Input: * acctNumArray - array of account numbers * balanceArray - array of account balances * numAccts - number of active accounts * outFile - reference to the output file * Process: * Prints the database of accounts and balances * Output: * Prints the database of accounts and balances */ public static void printAccts(int[] acctNumArray, double[] balanceArray, int numAccts, PrintWriter outFile) { outFile.println(); outFile.println(" Database of Bank Accounts"); outFile.println(); outFile.println("Account Balance"); for (int index = 0; index < numAccts; index++) { outFile.printf("%7d $%7.2f", acctNumArray[index], balanceArray[index]); outFile.println(); } outFile.println();
//flush the output file outFile.flush(); }
/* Method menu() * Input: * none * Process: * Prints the menu of transaction choices * Output: * Prints the menu of transaction choices */ public static void menu() { System.out.println(); System.out.println("Select one of the following transactions:"); System.out.println(" ****************************"); System.out.println(" List of Choices "); System.out.println(" ****************************"); System.out.println(" W -- Withdrawal"); System.out.println(" D -- Deposit"); System.out.println(" N -- New Account"); System.out.println(" B -- Balance Inquiry"); System.out.println(" X -- Delete Account"); System.out.println(" Q -- Quit"); System.out.println(); System.out.print(" Enter your selection: "); } /* Method findAcct: * Input: * acctNumArray - array of account numbers * numAccts - number of active accounts * requestedAccount - requested account requested_number * Process: * Performs a linear search on the acctNunArray for the requested account * Output: * If found, the index of the requested account is returned * Otherwise, returns -1 */ public static int findAcct(int[] acctNumArray, int numAccts, int requestedAccount) { for (int index = 0; index < numAccts; index++) if (acctNumArray[index] == requestedAccount) return index; return -1; }
/* Method balance: * Input: * acctNumArray - array of account numbers * balanceArray - array of account balances * numAccts - number of active accounts * outFile - reference to output file * kybd - reference to the "test cases" input file * Process: * Prompts for the requested account * Calls findAcct() to see if the account exists * If the account exists, the balance is printed * Otherwise, an error message is printed * Output: * If the account exists, the balance is printed * Otherwise, an error message is printed */ public static void balance(int[] acctNumArray, double[] balanceArray, int numAccts, PrintWriter outFile, Scanner kybd) { int requestedAccount; int index;
System.out.println(); System.out.print("Enter the account number: ");//prompt for account number requestedAccount = kybd.nextInt();//read-in the account number //call findAcct to search if requestedAccount exists index = findAcct(acctNumArray, numAccts, requestedAccount); if (index == -1) //invalid account { outFile.println("Transaction Requested: Balance Inquiry"); outFile.println("Error: Account number " + requestedAccount + " does not exist"); } /* Method findAcct: * Input: * acctNumArray - array of account numbers * numAccts - number of active accounts * requestedAccount - requested account requested_number * Process: * Performs a linear search on the acctNunArray for the requested account * Output: * If found, the index of the requested account is returned * Otherwise, returns -1 */ public static int findAcct(int[] acctNumArray, int numAccts, int requestedAccount) { for (int index = 0; index < numAccts; index++) if (acctNumArray[index] == requestedAccount) return index; return -1; }
/* Method balance: * Input: * acctNumArray - array of account numbers * balanceArray - array of account balances * numAccts - number of active accounts * outFile - reference to output file * kybd - reference to the "test cases" input file * Process: * Prompts for the requested account * Calls findAcct() to see if the account exists * If the account exists, the balance is printed * Otherwise, an error message is printed * Output: * If the account exists, the balance is printed * Otherwise, an error message is printed */ public static void balance(int[] acctNumArray, double[] balanceArray, int numAccts, PrintWriter outFile, Scanner kybd) { int requestedAccount; int index;
System.out.println(); System.out.print("Enter the account number: ");//prompt for account number requestedAccount = kybd.nextInt();//read-in the account number //call findAcct to search if requestedAccount exists index = findAcct(acctNumArray, numAccts, requestedAccount); if (index == -1) //invalid account { outFile.println("Transaction Requested: Balance Inquiry"); outFile.println("Error: Account number " + requestedAccount + " does not exist"); } MacBook Air HWi: Bank Accounts corsists of a sumber and a belance. The user of the program (the seller) can create a new account, as well as perform deposits, withdrawals, and balance inquiries You have beom hired as a progrmmer by a major bank Your firns peoject is a small bunking tramsaction system Each account Initially, the sccount information of existing customers is to be read into a pair of parallel arrays-ome for account eumbers, the other for balances. The bank can handle up to MAX NUM accounns. Use the Solowing method to read in the data values publie statie int readAeots (intl) eetum, doublell balance, int maxaeete) This method fills uo the account mamber and balance arrays (up to maxAccts) and returns the actual number of accounts read in ater refered to as umdests) After initialiration, the main program prists the inicial database of acceents and balances, Use method primAeetsO described below The program then allows the user to select romm the folowieg mens of transactions Select one of the followiag D- Deposit N- New account B Balance x- Delete Account o-Quit Use the following method to produce the menu: public statie void menu) This methed only displays the mena. The main program tben prompts the user for a selection. You should verify that the user has typed in a valid selection (otherwise print out an emor message and repeat the prompt) Once the uscer has enteed a selection, one of the following methods should be called to perform the specifie transaction. At the end, before the user quits, the program prints the final contents of the account and balance arrays publie statie int findhcot (intt) acethum, int numacets, int account) : method returns the index of account in the acstNum aray if the account exists, and -1 if it doesn't. It is called by all the remaining methods publie statie vold vithdraval (intt) acetmun, doublet) balance, int nunketa) : This method prompts the user for an account number. If the account does not exist, it the user for the amount of the withdrawal If the account does not contain sufficient performm the transaction prints an error messge. Odherwise, it ashs Bunds, it prints an error message and does not pubie statie void deposit(intt) aoctkum, doublel) balance, int nunkeets) This method prompts the user for an account mumber. If the account does not exist, it prints an error message. Otherwise, it asks the user for the amount of the deposit. public statie int nexAeet (inttl acctum, doublet) balance, int numkecta): This method prompts the user for a new account number. If the account already exists, i prints an error message. Otherwise, it adds the account to the acctsum array with an initial balance of O. It returns the new number of accounts public statie void balance (ant!) acetum, doublet] balance, int numAcets) This method prompts the user for an account number. If the account does not exist, it prints an error message. Otherwise, it prints public static int deleteAcct (int1] acctNum, double[) balance, int numaccts) i 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 public static void printAcets(intl acctiun, doublel) balance, int numaccts) the account balance. balance, it prints an error message. Otherwise, it deletes the account. It returms the new number of accounts This method prists a table of the database of all customer information-account number and balance.
Explanation / Answer
import java.io.*;
import java.util.*;
// Class BankAccount definition
class BankAccount
{
// main method definition
public static void main(String[] args) throws IOException
{
//constant definitions
final int MAX_NUM = 50;
//variable declarations
int[] acctNumArray = new int[MAX_NUM];//array of account numbers
double[] balanceArray = new double[MAX_NUM];//array of balances
int numAccts;//number of accounts
char choice;//menu item selected
boolean not_done = true;//loop control flag
// open input test cases file
//File testFile = new File("account.txt");
//create Scanner object
Scanner kybd = new Scanner(System.in);
// open the output file
PrintWriter outFile = new PrintWriter("myoutput.txt");
/* first part */
/* fill and print initial database */
numAccts = readAccts(acctNumArray,balanceArray,MAX_NUM);
printAccts(acctNumArray,balanceArray,numAccts,outFile);
/* second part */
/* prompts for a transaction and then */
/* call functions to process the requested transaction */
do
{
// Calls the method to display menu
menu();
// Accepts user choice
choice = kybd.next().charAt(0);
switch(choice)
{
case 'q':
case 'Q':
not_done = false;
printAccts(acctNumArray,balanceArray,numAccts,outFile);
break;
case 'b':
case 'B':
balance(acctNumArray,balanceArray,numAccts,outFile,kybd);
break;
case 'd':
case 'D':
deposit(acctNumArray,balanceArray,numAccts,outFile,kybd);
break;
case 'w':
case 'W':
withdrawal(acctNumArray,balanceArray,numAccts,outFile,kybd);
break;
case 'n':
case 'N':
numAccts = newAcct(acctNumArray,balanceArray,numAccts,outFile,kybd);
break;
case 'x':
case 'X':
numAccts = deleteAcct(acctNumArray,balanceArray,numAccts,outFile,kybd);
break;
default:
outFile.println("Error: " + choice + " is an invalid selection - try again");
outFile.println();
outFile.flush();
break;
}// End of switch case
} while (not_done); // End of do - while loop
//close the output file
outFile.close();
//close the test cases input file
kybd.close();
System.out.println();
System.out.println("The program is terminating");
}// End of main method
/* Method deleteAcct()
* Input:
* acctNumArray - reference to array of account numbers
* balanceArray - reference to array of account balances
* maxAccts - maximum number of active accounts allowed
* outFile - reference to output file
* kybd - reference to the "test cases" input file
* Process:
* Accepts account number
* Validates account number
* Delete the account
* Updates number of accounts
* Output:
* Writes the transaction details in file
*/
public static int deleteAcct(int []acctNumArray,double [] balanceArray,int numAccts,PrintWriter outFile, Scanner kybd)
{
// Checks if number of accounts is zero (no account available)
if(numAccts == 0)
{
// Writes the error data to file
outFile.println("Transaction Requested: Delete Account");
outFile.println("Error: No account to created.");
// Display the error in console
System.out.print(" ERROR: No account to created.");
}// End of if condition
// Otherwise record available to delete
else
{
// Accepts the account number
System.out.print(" Enter account number: ");
int acc = kybd.nextInt();
// Calls the function to check the account number availability
int index = findAcct(acctNumArray, numAccts, acc);
// Checks if the index value is -1 returned by the function findAcct()
// Then account number not available
if(index == -1)
{
// Writes the error data to file
outFile.println("Transaction Requested: Delete Account");
outFile.println("Error: Account number not available" + acc);
// Display the error in console
System.out.print(" ERROR: Account number not available.");
}// End of if condition
// Otherwise account number available
else
{
// Loops from the account number index position till end of accounts
for(int x = index; x < numAccts; x++)
{
// Shifts each account number one index position left
acctNumArray[x] = acctNumArray[x + 1];
// Shifts each balance one index position left
balanceArray[x] = balanceArray[x + 1];
}// End of for loop
// Decrease the record counter by one
numAccts--;
}// End of inner else
}// End of outer else
// Returns number of accounts counter
return numAccts;
}// End of method
/* Method newAcct()
* Input:
* acctNumArray - reference to array of account numbers
* balanceArray - reference to array of account balances
* maxAccts - maximum number of active accounts allowed
* outFile - reference to output file
* kybd - reference to the "test cases" input file
* Process:
* Accepts account number
* Validates account number
* Accepts initial balance
* Creates a new account
* Updates number of accounts
* Output:
* Writes the transaction details in file
*/
public static int newAcct(int []acctNumArray,double [] balanceArray,int numAccts,PrintWriter outFile, Scanner kybd)
{
// Checks if number of account is equals to 49 then record cannot be added
if(numAccts == 49)
{
// Writes the error data to file
outFile.println("Transaction Requested: Create Account");
outFile.println("Error: No more account can be created.");
// Display the error in console
System.out.print(" ERROR: No more account can be created.");
}// End of if condition
// Otherwise account can be created
else
{
// Accepts account number from the user
System.out.print(" Enter account number: ");
int acc = kybd.nextInt();
// Validates account number
if(acc >= 100000 && acc <= 999999)
{
// Calls the function to check the account number availability
// Checks if the index value is -1 returned by the function findAcct()
// Then account number not available
if(findAcct(acctNumArray, numAccts, acc) == -1)
{
// Accepts the initial balance from the user
System.out.print(" Enter the initial balance $");
double amt = kybd.nextDouble();
// Assigns the account number entered by the user at numAccts index position of acctNumArray
acctNumArray[numAccts] = acc;
// Assigns the amount entered by the user at numAccts index position of balanceArray
balanceArray[numAccts] = amt;
// Increases the number of account counter by one
numAccts++;
}// End of inner if condition
// Otherwise account number already available
else
{
// Writes the error data to file
outFile.println("Transaction Requested: Create Account");
outFile.println("Error: Account number already exists." + acc);
// Display the error in console
System.out.print(" Error: Account number already exists.");
}// End of else
}// End of outer if condition
// Otherwise invalid account number entered by the user
else
{
// Writes the error data to file
outFile.println("Transaction Requested: Create Account");
outFile.println("Error: Invalid Account number " + acc);
// Display the error in console
System.out.print(" ERROR: Invalid Account Number");
}// End of inner else
}// End of outer else
// Returns number of account counter
return numAccts;
}// End of method
/* Method deposit()
* Input:
* acctNumArray - reference to array of account numbers
* balanceArray - reference to array of account balances
* maxAccts - maximum number of active accounts allowed
* outFile - reference to output file
* kybd - reference to the "test cases" input file
* Process:
* Accepts account number
* Validates account number
* Accepts deposit amount
* Validates deposit amount
* Updates balance
* Output:
* Writes the transaction details in file
*/
public static void deposit(int []acctNumArray, double []balanceArray, int numAccts, PrintWriter outFile, Scanner kybd)
{
// Accepts account number from the user
System.out.print("Enter the account number: ");
int requestedAccount = kybd.nextInt();
// Calls the function to check the account number availability
int index = findAcct(acctNumArray, numAccts, requestedAccount);
// Checks if the index value is -1 returned by the function findAcct()
// Then account number not available
if (index == -1) //invalid account
{
// Writes the error data to file
outFile.println("Transaction Requested: Deposit");
outFile.println("Error: Account number " + requestedAccount + " does not exist");
// Display the error in console
System.out.print(" Invalid Account Number");
}// End of if condition
// Otherwise account available
else
{
// Accepts deposit amount from the user
System.out.print("Enter the deposit amount: ");
double amount = kybd.nextDouble();
// Checks if the amount entered by the user is negative or zero
if(amount <= 0)
{
// Writes the error data to file
outFile.println("Transaction Requested: Deposit");
outFile.println("Error: Deposit Amount " + amount);
// Display the error in console
System.out.print("Invalid deposit amount (Cannot negative or zero)" + amount);
}// End of if condition
// Otherwise valid deposit amount
else
{
// Adds the deposit amount entered by the use with the balance at index position
balanceArray[index] += amount;
// Writes the success data to file
outFile.println("Transaction Requested: Deposit");
outFile.println("Deposit successfull.");
// Display the error in console
System.out.print("Deposit successful.");
}// End of inner else
}// End of outer else
}// End of method
/* Method withdrawal()
* Input:
* acctNumArray - reference to array of account numbers
* balanceArray - reference to array of account balances
* maxAccts - maximum number of active accounts allowed
* outFile - reference to output file
* kybd - reference to the "test cases" input file
* Process:
* Accepts account number
* Validates account number
* Accepts withdrawal amount
* Validates withdrawal amount
* Updates balance
* Output:
* Writes the transaction details in file
*/
public static void withdrawal(int []acctNumArray, double []balanceArray, int numAccts, PrintWriter outFile, Scanner kybd)
{
System.out.print("Enter the account number: ");
int requestedAccount = kybd.nextInt();
// Calls the function to check the account number availability
int index = findAcct(acctNumArray, numAccts, requestedAccount);
// Checks if the index value is -1 returned by the function findAcct()
// Then account number not available
if (index == -1) //invalid account
{
// Writes the error data to file
outFile.println("Transaction Requested: Withdrawal");
outFile.println("Error: Account number " + requestedAccount + " does not exist");
// Display the error in console
System.out.print(" Invalid account number: " + requestedAccount);
}// End of if condition
// Otherwise account is available
else
{
// Accepts withdrawal amount from the user
System.out.print("Enter the withdrawal amount: ");
double amount = kybd.nextDouble();
// Checks if the amount entered by the user is negative or zero
if(amount <= 0)
{
// Writes the error data to file
outFile.println("Transaction Requested: Withdrawal");
outFile.println("Error: Withdrawal Amount " + amount);
// Display the error in console
System.out.print(" Invalid Withdrawal Amount (cannot zero or negaitve): " + amount);
}// End of if condition
// Otherwise check if the amount entered by the user is greater than the current balance
else if(balanceArray[index] < amount)
{
// Writes the error data to file
outFile.println("Transaction Requested: Withdrawal");
outFile.println("Error: Insufficient Balance ");
// Display the error in console
System.out.print(" Insufficient balance: Current balance: " + balanceArray[index] +
" Withdrawal amount requested for " + amount);
}// End of else if
// Otherwise valid amount
else
{
// Subtracts the amount entered by the user from the current balance
balanceArray[index] -= amount;
// Writes the success data to file
outFile.println("Withdrawal Successful.");
// Display the success in console
System.out.print(" Withdrawal Successful.");
}// End of inner else
}// End of outer else
}// End of method
/* Method readAccts()
* Input:
* acctNumArray - reference to array of account numbers
* balanceArray - reference to array of account balances
* maxAccts - maximum number of active accounts allowed
* Process:
* Reads the initial database of accounts and balances
* Output:
* Fills in the initial account and balance arrays and returns the number of active accounts
*/
public static int readAccts(int[] acctNumArray, double[] balanceArray, int maxAccts) throws IOException
{
// open database input file
//create File object
File dbFile = new File("account.txt");
//create Scanner object
Scanner sc = new Scanner(dbFile);
int count = 0; //account number counter
// Loops till data availability and number of records less than maximum account
while (sc.hasNext() && count < maxAccts)
{
// Reads account number from file and stores it in counter index position of acctNumArray
acctNumArray[count] = sc.nextInt();
// Reads balance from file and stores it in counter index position of balanceArray
balanceArray[count] = sc.nextDouble();
// Increase the record counter by one
count++;
}// End of while loop
//close the input file
sc.close();
//return the account number count
return count;
}// End of method
/* Method printAccts:
* Input:
* acctNumArray - array of account numbers
* balanceArray - array of account balances
* numAccts - number of active accounts
* outFile - reference to the output file
* Process:
* Prints the database of accounts and balances
* Output:
* Prints the database of accounts and balances
*/
public static void printAccts(int[] acctNumArray, double[] balanceArray, int numAccts, PrintWriter outFile)
{
outFile.println();
outFile.println(" Database of Bank Accounts");
outFile.println();
outFile.println("Account Balance");
// Loops till number of accounts available
for (int index = 0; index < numAccts; index++)
{
// Writes the account number and balance information to file
outFile.printf("%7d $%7.2f", acctNumArray[index], balanceArray[index]);
outFile.println();
}// End of for loop
// Close the file
outFile.println();
//flush the output file
outFile.flush();
}// End of method
/* Method menu()
* Input:
* none
* Process:
* Prints the menu of transaction choices
* Output:
* Prints the menu of transaction choices
*/
public static void menu()
{
System.out.println();
// Displays menu
System.out.println("Select one of the following transactions:");
System.out.println(" ****************************");
System.out.println(" List of Choices ");
System.out.println(" ****************************");
System.out.println(" W -- Withdrawal");
System.out.println(" D -- Deposit");
System.out.println(" N -- New Account");
System.out.println(" B -- Balance Inquiry");
System.out.println(" X -- Delete Account");
System.out.println(" Q -- Quit");
System.out.println();
System.out.print(" Enter your selection: ");
}// End of method
/* Method findAcct:
* Input:
* acctNumArray - array of account numbers
* numAccts - number of active accounts
* requestedAccount - requested account requested_number
* Process:
* Performs a linear search on the acctNunArray for the requested account
* Output:
* If found, the index of the requested account is returned
* Otherwise, returns -1
*/
public static int findAcct(int[] acctNumArray, int numAccts, int requestedAccount)
{
// Loops till number of accounts
for (int index = 0; index < numAccts; index++)
// Checks if the account number at index position is equals to the parameter requestedAccount
if (acctNumArray[index] == requestedAccount)
// returns the found index position
return index;
// Otherwise returns -1 for not found
return -1;
}// End of method
/* Method balance:
* Input:
* acctNumArray - array of account numbers
* balanceArray - array of account balances
* numAccts - number of active accounts
* outFile - reference to output file
* kybd - reference to the "test cases" input file
* Process:
* Prompts for the requested account
* Calls findAcct() to see if the account exists
* If the account exists, the balance is printed
* Otherwise, an error message is printed
* Output:
* If the account exists, the balance is printed
* Otherwise, an error message is printed
*/
public static void balance(int[] acctNumArray, double[] balanceArray, int numAccts,
PrintWriter outFile, Scanner kybd)
{
int requestedAccount;
int index;
System.out.println();
System.out.print("Enter the account number: ");//prompt for account number
requestedAccount = kybd.nextInt();//read-in the account number
// Calls the function to check the account number availability
index = findAcct(acctNumArray, numAccts, requestedAccount);
// Checks if the index value is -1 returned by the function findAcct()
// Then account number not available
if (index == -1) //invalid account
{
// Writes the error data to file
outFile.println("Transaction Requested: Balance Inquiry");
outFile.println("Error: Account number " + requestedAccount + " does not exist");
// Display the error in console
System.out.println(" ERROR: Invalid Account Number: " + requestedAccount);
}// End of if condition
// Otherwise valid account number
else
// Display the data in console
System.out.println(" Account Number: " + acctNumArray[index]
+ " Balance $" + balanceArray[index]);
}// End of method
}// End of class
Sample Output:
Select one of the following transactions:
****************************
List of Choices
****************************
W -- Withdrawal
D -- Deposit
N -- New Account
B -- Balance Inquiry
X -- Delete Account
Q -- Quit
Enter your selection: b
Enter the account number: 100003
Account Number: 100003
Balance $32000.0
Select one of the following transactions:
****************************
List of Choices
****************************
W -- Withdrawal
D -- Deposit
N -- New Account
B -- Balance Inquiry
X -- Delete Account
Q -- Quit
Enter your selection: d
Enter the account number: 100003
Enter the deposit amount: 1000
Deposit successful.
Select one of the following transactions:
****************************
List of Choices
****************************
W -- Withdrawal
D -- Deposit
N -- New Account
B -- Balance Inquiry
X -- Delete Account
Q -- Quit
Enter your selection: b
Enter the account number: 100003
Account Number: 100003
Balance $33000.0
Select one of the following transactions:
****************************
List of Choices
****************************
W -- Withdrawal
D -- Deposit
N -- New Account
B -- Balance Inquiry
X -- Delete Account
Q -- Quit
Enter your selection: B
Enter the account number: 100001
Account Number: 100001
Balance $56000.0
Select one of the following transactions:
****************************
List of Choices
****************************
W -- Withdrawal
D -- Deposit
N -- New Account
B -- Balance Inquiry
X -- Delete Account
Q -- Quit
Enter your selection: w
Enter the account number: 100001
Enter the withdrawal amount: 500
Withdrawal Successful.
Select one of the following transactions:
****************************
List of Choices
****************************
W -- Withdrawal
D -- Deposit
N -- New Account
B -- Balance Inquiry
X -- Delete Account
Q -- Quit
Enter your selection: b
Enter the account number: 100001
Account Number: 100001
Balance $55500.0
Select one of the following transactions:
****************************
List of Choices
****************************
W -- Withdrawal
D -- Deposit
N -- New Account
B -- Balance Inquiry
X -- Delete Account
Q -- Quit
Enter your selection: n
Enter account number: 100005
account number: 100005
Enter the initial balance $3000
Select one of the following transactions:
****************************
List of Choices
****************************
W -- Withdrawal
D -- Deposit
N -- New Account
B -- Balance Inquiry
X -- Delete Account
Q -- Quit
Enter your selection: b
Enter the account number: 100005
Account Number: 100005
Balance $3000.0
Select one of the following transactions:
****************************
List of Choices
****************************
W -- Withdrawal
D -- Deposit
N -- New Account
B -- Balance Inquiry
X -- Delete Account
Q -- Quit
Enter your selection: x
Enter account number: 100003
Select one of the following transactions:
****************************
List of Choices
****************************
W -- Withdrawal
D -- Deposit
N -- New Account
B -- Balance Inquiry
X -- Delete Account
Q -- Quit
Enter your selection: b
Enter the account number: 100003
ERROR: Invalid Account Number: 100003
Select one of the following transactions:
****************************
List of Choices
****************************
W -- Withdrawal
D -- Deposit
N -- New Account
B -- Balance Inquiry
X -- Delete Account
Q -- Quit
Enter your selection: n
Enter account number: 11
account number: 11
ERROR: Invalid Account Number
Select one of the following transactions:
****************************
List of Choices
****************************
W -- Withdrawal
D -- Deposit
N -- New Account
B -- Balance Inquiry
X -- Delete Account
Q -- Quit
Enter your selection: n
Enter account number: 100005
account number: 100005
Error: Account number already exists.
Select one of the following transactions:
****************************
List of Choices
****************************
W -- Withdrawal
D -- Deposit
N -- New Account
B -- Balance Inquiry
X -- Delete Account
Q -- Quit
Enter your selection: d
Enter the account number: 100001
Enter the deposit amount: -10
Invalid deposit amount (Cannot negative or zero)-10.0
Select one of the following transactions:
****************************
List of Choices
****************************
W -- Withdrawal
D -- Deposit
N -- New Account
B -- Balance Inquiry
X -- Delete Account
Q -- Quit
Enter your selection: w
Enter the account number: 100005
Enter the withdrawal amount: -10
Invalid Withdrawal Amount (cannot zero or negaitve): -10.0
Select one of the following transactions:
****************************
List of Choices
****************************
W -- Withdrawal
D -- Deposit
N -- New Account
B -- Balance Inquiry
X -- Delete Account
Q -- Quit
Enter your selection: b
Enter the account number: 100005
Account Number: 100005
Balance $3000.0
Select one of the following transactions:
****************************
List of Choices
****************************
W -- Withdrawal
D -- Deposit
N -- New Account
B -- Balance Inquiry
X -- Delete Account
Q -- Quit
Enter your selection: w
Enter the account number: 100005
Enter the withdrawal amount: 8000
Insufficient balance:
Current balance: 3000.0
Withdrawal amount requested for 8000.0
Select one of the following transactions:
****************************
List of Choices
****************************
W -- Withdrawal
D -- Deposit
N -- New Account
B -- Balance Inquiry
X -- Delete Account
Q -- Quit
Enter your selection: w
Enter the account number: 1
Invalid account number: 1
Select one of the following transactions:
****************************
List of Choices
****************************
W -- Withdrawal
D -- Deposit
N -- New Account
B -- Balance Inquiry
X -- Delete Account
Q -- Quit
Enter your selection: q
The program is terminating
account.txt file contents
100000
12000
100001
56000
100002
42000
100003
32000
myoutput.txt file contents
Database of Bank Accounts
Account Balance
100000 $12000.00
100001 $56000.00
100002 $42000.00
100003 $32000.00
Transaction Requested: Deposit
Deposit successfull.
Withdrawal Successful.
Transaction Requested: Balance Inquiry
Error: Account number 100003 does not exist
Transaction Requested: Create Account
Error: Invalid Account number 11
Transaction Requested: Create Account
Error: Account number already exists.100005
Transaction Requested: Deposit
Error: Deposit Amount -10.0
Transaction Requested: Withdrawal
Error: Withdrawal Amount -10.0
Transaction Requested: Withdrawal
Error: Insufficient Balance
Transaction Requested: Withdrawal
Error: Account number 1 does not exist
Database of Bank Accounts
Account Balance
100000 $12000.00
100001 $55500.00
100002 $42000.00
100005 $3000.00