Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you help me rearrange my code by incorporating switch I'm not really sure how to do it and it makes the code look cleaner.

Can you help me rearrange my code by incorporating switch I'm not really sure how to do it and it makes the code look cleaner. Can you help me do it but still give the same output? Also kindly add an in-line comment on what changes and rearrangement you've done so I can understand what's going on.

import java.util.ArrayList;

import java.util.Scanner;

public class Bank {

/** * Add and read bank information to the user. * @param arg A string, double and int array containing * the command line arguments. * @exception Any exception * @return an arraylsit of bank customers */ // method to display the menu static void menu() { System.out.println(" 1. Add a bank customer"); System.out.println("2. Remove a bank customer"); System.out.println("3. Print all bank customers"); System.out.println("4. Quit"); }

// method to add a customer in sorted order of account number into an array

// list. returns true if addition is successful, else false

static boolean addCustomer(ArrayList customers, BankCustomer newCustomer) { // looping and finding the proper position to add for (int i = 0; i < customers.size(); i++) { // checking if new customer can be added to index i if (newCustomer.getAcctNumber() < customers.get(i).getAcctNumber()) { // adding to index i customers.add(i, newCustomer); return true; // success } else if (newCustomer.getAcctNumber() == customers.get(i) .getAcctNumber()) { // duplicate found, returning false return false; } } // adding to the end, if still not added customers.add(newCustomer); return true; // success }

// method to remove a customer by account number, if exists

static boolean removeCustomer(ArrayList customers, int accountNum) { for (int i = 0; i < customers.size(); i++) { if (customers.get(i).getAcctNumber() == accountNum) { // found, removing current customer customers.remove(i); return true; // found and removed } } return false; // not found }

// method to print customers

static void printCustomers(ArrayList customers) { // displaying heading System.out.printf("%-15s %-15s %-15s ", "Customer Name", "Account Number", "Account Balance"); // looping and printing each customer's name, acc num and balance for (BankCustomer c : customers) { System.out.printf("%-15s %-15d $%-14.2f ", c.getName(), c.getAcctNumber(), c.getBalance()); } System.out.println("----------------------------------------------- "); }

// main method

public static void main(String[] args) { // creating an array list of BankCustomer ArrayList accounts = new ArrayList(); // scanner to read user input Scanner scanner = new Scanner(System.in); int ch = 0; // looping until ch is 4 do { // displaying menu menu(); // inside try with multiple catches block, getting and processing // user input try { System.out.print("Enter your choice: "); //reading choice ch = Integer.parseInt(scanner.nextLine()); System.out.println(); if (ch == 1) { //reading details for bank customer System.out.print("Enter name: "); String name = scanner.nextLine(); System.out.print("Enter account number: "); int accNum = Integer.parseInt(scanner.nextLine()); System.out.print("Enter balance: "); double balance = Double.parseDouble(scanner.nextLine()); //creating a customer, will throw BCException if any detail is invalid BankCustomer cust = new BankCustomer(accNum, balance, name); //if no exception occurred, adding to the list and displaying the result if (addCustomer(accounts, cust)) { System.out.println("Success!"); } else { System.out .println("Customer with same account number already exists!"); } } else if (ch == 2) { //fetching an account number, removing customer System.out.print("Enter account number: "); int accNum = Integer.parseInt(scanner.nextLine()); if (removeCustomer(accounts, accNum)) { System.out.println("Success!"); } else { System.out .println("Customer with given account number does not exist!"); } } else if (ch == 3) { //printing customers printCustomers(accounts); } } catch (BCException e) { //BCException is occurred System.out.println(e.getMessage()); } catch (Exception e) { //exception due to non numeric input System.out.println("Invalid input!"); } } while (ch != 4); }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Informix Database Administrators Survival Guide

Authors: Joe Lumbley

1st Edition

0131243144, 978-0131243149

More Books

Students also viewed these Databases questions