Hemework-Topic 7-Bank Accounts: You have been hired as a programmer by a major b
ID: 3875239 • Letter: H
Question
Hemework-Topic 7-Bank Accounts: You have been hired as a programmer by a major bank. Your Erst 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 readkccts (int!1 acctnum, double balance, int max accts) This method fills up the account number and balance arrays (up to max accts) and returns the actual number of accounts read in (later referred to as num accts). After initialization, the main program prints the initial database of accounts and balances described below. Use method printAccts The peogram then allows the uscr to sclect 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 methed to produce the menu: public static void nenu This method only displays the menu. The main program then prompts the user for a selection. You should verify that the uscr has typed in a valid selection (otherwise print out an error message and repeat the prompt) Onoe the user has entered 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 public static int findâcet (int[1 acetnum, int num accts, int account) This method returns the index of account in the aectnum array if the account exists, and -1 if it doesnt. It is called by all the remaining methods. public statie void withdrawal (intl acctnun, doublel balance, int num accta), 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 ansount of the withdrawal. If the account does not comtain sufficient funds, it prints an ernor message and does not perform the transaction pubic static void deposit (intll acctnun, doublel1 balance, int num accts) This method prompes 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[] acctnun, double[] balance, int num accts); This method prompts the user for a new account number. If the account already exists, it prints an crror message. Otherwise, it adds the account to the acctnum array with an initial balance of O. It returns the new number of accounts public static void balance (intl acctnum, doublet] balance, int num accts) 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 atatic void printAccts(intl acctnum, double balance, int num accta) prints all customer informationaccount number and balance.
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
/**
*
* @author manB
*/
public class Bank {
private int[] acctnum;
private double[] balance;
final int MAX_NUM = 100;
public static void menu() {
System.out.println("Select one of the following: "
+ " "
+ "W - Withdrawl "
+ "D - Deposit "
+ "N - New Account "
+ "B - Balacne "
+ "Q - Quit "
+ "Enter choice:");
}
public static int findAcct(int[] acctnum, int num_accts, int account){
for (int i = 0; i < num_accts; i++)
if (acctnum[i] == account)
return i;
return -1;
}
public static void withdrawl(int[] acctnum, double[] balance, int num_accts) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter account number:");
int account = Integer.parseInt(br.readLine());
int index = findAcct(acctnum, num_accts, account);
if (index == -1) {
System.out.println("Account "+ account +" not fount.");
return;
}
System.out.println("Enter amount to withdraw:");
double amount = Double.parseDouble(br.readLine());
if (amount > balance[index]) {
System.out.println("Account "+ account +" have insufficient funds.");
return;
}
balance[index] -= amount;
}
public static void deposit(int[] acctnum, double[] balance, int num_accts) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter account number:");
int account = Integer.parseInt(br.readLine());
int index = findAcct(acctnum, num_accts, account);
if (index == -1) {
System.out.println("Account "+ account +" not fount.");
return;
}
System.out.println("Enter amount to deposit:");
double amount = Double.parseDouble(br.readLine());
balance[index] += amount;
}
public static int newAcct(int[] acctnum, double[] balance, int num_accts) throws IOException{
if (acctnum.length == num_accts) {
System.out.println("Memory FULL. Cannot create new account");
return num_accts;
}
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter new account number:");
int account = Integer.parseInt(br.readLine());
int index = findAcct(acctnum, num_accts, account);
if (index != -1) {
System.out.println("Account "+ account +" already exists.");
return num_accts;
}
acctnum[num_accts] = account;
balance[num_accts] = 0;
return ++num_accts;
}
public static void balacne(int[] acctnum, double[] balance, int num_accts) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter account number:");
int account = Integer.parseInt(br.readLine());
int index = findAcct(acctnum, num_accts, account);
if (index == -1) {
System.out.println("Account "+ account +" not fount.");
return;
}
System.out.println("Present balance: " + balance[index]);
}
public static void printAccts(int[] acctnum, double[] balance, int num_accts) {
System.out.println("Account |Balacne");
for (int i = 0; i < num_accts; i++)
System.out.println(acctnum[i]+" |"+balance[i]);
}
public Bank() {
acctnum = new int[MAX_NUM];
balance = new double[MAX_NUM];
}
public static int readAccts(int[] acctnum, double[] balance, int max_accts) throws FileNotFoundException, IOException {
BufferedReader br = new BufferedReader(new FileReader("bank_records"));
String line;
int index = 0;
while ((line = br.readLine()) != null) {
Scanner sc = new Scanner(line);
acctnum[index] = sc.nextInt();
balance[index] = sc.nextDouble();
index++;
if (index == max_accts)
break;
}
return index;
}
}
here you go champ.... do you need the main method?
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.