Question
How to make this program work? I want the output looks like this Sample Output: Transaction Type: Deposit Account Number: 9876 Amount to Deposit: $123.45
How to make this program work? I want the output looks like this
Sample Output: Transaction Type: Deposit Account Number: 9876 Amount to Deposit: $123.45 Old Balance: $300.50 New Balance: $423.95
input can be whatever.
import java.util.*; import java.io.*; // need to input files public class BankAccount { public static void main(String[] args) throws IOException { Scanner keyboard=new Scanner(System.in); PrintWriter pw = new PrintWriter("bankoutput.txt"); final int MAXSIZE = 50;
int []acctnum = new int[MAXSIZE]; int num_accts = 0, test = 0; char select; double []balance = new double[MAXSIZE]; num_accts=readAccts(acctnum,balance, MAXSIZE, pw);
do { printAccts( acctnum, balance, num_accts, pw); menu(); System.out.println("Make a selection"); select=keyboard.next().charAt(0); switch(select) { case 'W': case 'w': withdrawal( acctnum, balance, num_accts, keyboard, pw); break; case 'D': case 'd':deposit(acctnum, balance, num_accts, pw); break; case 'N': case 'n': newAcct(acctnum, balance,num_accts, keyboard, pw ); break; case 'B': case 'b':balance( acctnum, balance, num_accts, pw); break; case 'q': case 'Q': System.out.println("further transcation has been terminated"); break; case 'x' : case 'X' : deleteAcct( acctnum, balance, num_accts, pw); break; default: pw.println("wrong"); } }while(select !='Q' && select !='q'); }
public static int readAccts(int[] acctnum, double[] balance, int max_accts, PrintWriter pw) throws IOException{ int numaccounts=0;
File outputFile = new File("input.txt"); Scanner output = new Scanner(outputFile);
while(output.hasNext()) { acctnum[numaccounts]=output.nextInt(); balance[numaccounts]=output.nextDouble(); numaccounts++; }
output.close(); return numaccounts; }
public static void menu() { System.out.println("W - withdrawal"); System.out.println("D - deposit "); System.out.println("N - new accounts"); System.out.println("B - balance"); System.out.println("Q - quit"); System.out.println("x- delete"); }
public static int findAcct(int[] acctnum, int num_accts, int account) { for(int i=0; i public static void withdrawal(int[] acctnum, double[] balance, int num_accts, Scanner keyboard, PrintWriter pw) { int acct, pos; double withdraw; System.out.println("Enter your account number: "); acct=keyboard.nextInt(); pos = findAcct(acctnum, num_accts, acct); if(pos== -1) pw.println("account not found"); else { System.out.println("Enter withdraw ammount: "); Scanner keyboard1=new Scanner(System.in); withdraw=keyboard1.nextDouble(); if(withdraw> balance[pos]) pw.println("Not enough funds"); else { balance[pos] -= withdraw; pw.println("Your withdrawl successful"); pw.println("succeed "); } } } public static void deposit(int[] acctnum, double[] balance, int num_accts, PrintWriter pw) { int acct, pos; Scanner keyboard=new Scanner(System.in); // prompt user to enter account number System.out.println("Enter your account number: "); acct=keyboard.nextInt(); pos = findAcct(acctnum, num_accts, acct); if(pos== -1) pw.println("account not found"); else { // prompt user to enter amount to deposit System.out.println("Enter deposit ammount: "); double deposit1 = keyboard.nextDouble(); if(deposit1 < 0) // prompt user to enter postive amount pw.println("deposit amount must be a positive number" + ". Try again"); else { balance[pos] +=deposit1; pw.println("Your deposit successful"); pw.println("succeed "); } } } public static int newAcct(int[] acctnum, double[] balance, int num_accts, Scanner input, PrintWriter pw ) throws IOException{ int pos , acct; double bal; System.out.println("Welcome to our bank! Enter your new account"); acct=input.nextInt(); pos=findAcct(acctnum, num_accts, acct); if(pos>=0) pw.println("Sorry, account number " + acct + " is taken"); else { acctnum[num_accts]=acct; balance[num_accts]=0; num_accts++; pw.println("account creaed!"); } return num_accts; } public static void balance(int[] acctnum, double[] balance, int num_accts, PrintWriter pw) { int account, pos; double deposit; Scanner keyboard=new Scanner(System.in); //step one: prompt the user to enter their account number System.out.print("Enter account number : "); account = keyboard.nextInt(); //step 2: send the acctnum array, num_accts, AND account //to the findAcct method. Assign the method call to the variable pos. pos = findAcct(acctnum, num_accts, account); if (pos== -1) pw.println( "the account the user entered was not found"); else pw.println("Current balance $" + balance[pos]); } public static void printAccts(int[] acctnum, double[] balance, int num_accts, PrintWriter pw) { for(int i=0; i // enter an account you want to delete System.out.println("enter an account number you want to delete"); acct=keyboard.nextInt(); pos=findAcct(acctnum, num_accts, acct); if (pos==-1) pw.println("Accout number " +acct + " was not found"); else { num_accts --; for(int i = pos; i <= num_accts; i++) { acctnum[i] = acctnum [i + 1]; balance[i] = balance [i + 1]; } pw.println(acct + "accont was deleted "); } return num_accts; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started