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

- I need \" try - catch for my switch menu ! Also , how to keep the all history

ID: 3769810 • Letter: #

Question

- I need " try - catch for my switch menu !

Also , how to keep the all history of transaction ? ! So option 5 suppose to show all the accounts history !

--------------------------------------------------------------------------------------------------

package bankapp;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Calendar;

public class BankApp
{
public static void main(String[] args)

    {
    Scanner s = new Scanner ( System.in);
  
    // create a calendar
    Calendar cal = Calendar.getInstance();
  

    Bank myBank = new Bank();
  
    int month = 0, day = 0;

    int user_choice = 2;

    do
{
    // display menu to user

   // ask user for his chice and validate it ( make sure it is between 1 and 6)

    System.out.println("         WELCOME TO YOUR BANK");
    System.out.println(" LET'S START WITH OPENING NEW ACCOUNT, ");
    System.out.println(" Today is " + cal.getTime());
    System.out.println(" --------------------------------------");
    System.out.println();
    System.out.println("1) Open a new bank account");
    System.out.println("2) Deposit to a bank account");
    System.out.println("3) Withdraw bank account");
    System.out.println("4) Print short account information");
    System.out.println("5) Print the detailed account information including last transaction");
    System.out.println("6) To see your Balance after 60 days ");
    System.out.println("7) Quit");
    System.out.println();
    System.out.println(" Enter choice [1-6]: ");
  
  

user_choice = s.nextInt();

    switch ( user_choice)

        {

case 1:

    System.out.println("Enter a customer name");

    ; String cn = s.next();

    System.out.println(" Enter an opening balance");

    double d = s.nextDouble();

    System.out.println(" (1)Saving account. (2) Checking account.(3) Extended Saving account." );

    int t = s.nextInt();
  
    System.out.println(" Congragulation !!   You have created a New account ");
    System.out.println("     YOUR NEW ACCOUNT NUMBER IS : " + myBank.openNewAccount(cn, d,t));
    System.out.println("The day of Opening new account is today : " + cal.getTime());
    System.out.println("");
    System.out.println("");
  

    break;

case 2:

    System.out.println(" Enter an ACCOUNT number");

    int an = s.nextInt();

    System.out.println(" Enter a DEPOSIT amount ");

    double da = s.nextDouble();

    myBank.depositTo(an, da);
  
    System.out.println("Today : " + cal.getTime());
  
    System.out.println("------------------------------------------------");

    break;

case 3:

    System.out.println(" Enter an ACCOUNT number");

    int acn = s.nextInt();

    System.out.println(" Enter a WITHDRAW amount ");

    double wa = s.nextDouble();

    myBank.withdrawFrom(acn, wa);
    System.out.println("Today : " + cal.getTime());
  
    System.out.println("------------------------------------------------");

    break;

case 4:

    System.out.println(" Enter an ACCOUNT number");

    int anum = s.nextInt();

    myBank.printTransactionInfo(anum);
    System.out.println("Today : " + cal.getTime());
  
    System.out.println("------------------------------------------------");

    break;

case 5:

    System.out.println(" Enter an ACCOUNT number");

    anum = s.nextInt();

    myBank.printTransactionInfo(anum);
    System.out.println("Today : " + cal.getTime());
  
    System.out.println("------------------------------------------------");

    break;
case 6:
    System.out.println(" Enter an ACCOUNT number");
    anum = s.nextInt();
    myBank.printTransactionInfo(anum);
    System.out.println(" Your money after 60 days ");
    cal.add(Calendar.DATE, 60);

    System.out.println ( " 60 days later "+ cal.getTime());
  
    break;
    default: System.out.println(" Invalid option. Please try again.");
  
    return;
        /**
         * Catch ( Exception e)
         * {
         * System.out.println("Exception ")
         * input.nextLine();
         */
      
            }
      }
        while ( user_choice !='6');
    }

}

Explanation / Answer

Maintaing history in a hasmap for each customer. See for history variable. Added two methods to add and print history for each customer

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Calendar;

public class BankApp {
  
  
   public static void main(String[] args)

   {
  
       //To maintain history
       HashMap<Integer,ArrayList<String>> history= new HashMap<Integer,ArrayList<String>>();
      
      
       Scanner s = new Scanner(System.in);

       // create a calendar
       Calendar cal = Calendar.getInstance();

       Bank myBank = new Bank();

       int month = 0, day = 0;

       int user_choice = 2;
       String hstr = null;

       do {
           // display menu to user

           // ask user for his chice and validate it ( make sure it is between
           // 1 and 6)

           System.out.println("         WELCOME TO YOUR BANK");
           System.out.println(" LET'S START WITH OPENING NEW ACCOUNT, ");
           System.out.println(" Today is " + cal.getTime());
           System.out.println(" --------------------------------------");
           System.out.println();
           System.out.println("1) Open a new bank account");
           System.out.println("2) Deposit to a bank account");
           System.out.println("3) Withdraw bank account");
           System.out.println("4) Print short account information");
           System.out
                   .println("5) Print the detailed account information including last transaction");
           System.out.println("6) To see your Balance after 60 days ");
           System.out.println("7) Quit");
           System.out.println();
           System.out.println(" Enter choice [1-6]: ");

           user_choice = s.nextInt();
           try {

               switch (user_choice)

               {

               case 1:

                   System.out.println("Enter a customer name");

                   ;
                   String cn = s.next();

                   System.out.println(" Enter an opening balance");

                   double d = s.nextDouble();

                   System.out
                           .println(" (1)Saving account. (2) Checking account.(3) Extended Saving account.");

                   int t = s.nextInt();

                   System.out
                           .println(" Congragulation !!   You have created a New account ");
                  
                   int accNo = myBank.openNewAccount(cn, d, t);
                   System.out.println("     YOUR NEW ACCOUNT NUMBER IS : "
                           +accNo );
                  
                   Date time = cal.getTime();
                   System.out
                           .println("The day of Opening new account is today : "
                                   + time);
                   System.out.println("");
                   System.out.println("");
                  
                   //Adding history
                   hstr = time.toString()+" # New Account Created for "+cn+", Account type: "+(t == 1 ? "Saving account" : (t == 2 ? "Checking account" : "Extended Saving account") );
                   BankApp.add(history, accNo, hstr);
                   break;
                  
                  

               case 2:

                   System.out.println(" Enter an ACCOUNT number");

                   int an = s.nextInt();

                   System.out.println(" Enter a DEPOSIT amount ");

                   double da = s.nextDouble();

                   myBank.depositTo(an, da);

                   System.out.println("Today : " + cal.getTime());

                   System.out
                           .println("------------------------------------------------");
                  
                   //Adding history
                   hstr = cal.getTime().toString()+" # Amount Depoistied: "+da;
                   BankApp.add(history, an, hstr);
                  

                   break;

               case 3:

                   System.out.println(" Enter an ACCOUNT number");

                   int acn = s.nextInt();

                   System.out.println(" Enter a WITHDRAW amount ");

                   double wa = s.nextDouble();

                   myBank.withdrawFrom(acn, wa);
                   System.out.println("Today : " + cal.getTime());

                   System.out
                           .println("------------------------------------------------");
                   //Adding history
                   hstr = cal.getTime().toString()+" # Amount Withdrawn: "+wa;
                   BankApp.add(history, acn, hstr);
                  

                   break;

               case 4:

                   System.out.println(" Enter an ACCOUNT number");

                   int anum = s.nextInt();

                   myBank.printTransactionInfo(anum);
                   System.out.println("Today : " + cal.getTime());

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

                   //Adding history
                   hstr = cal.getTime().toString()+" # Printed short account information ";
                   BankApp.add(history, anum, hstr);
                   break;

               case 5:

                   System.out.println(" Enter an ACCOUNT number");

                   anum = s.nextInt();

                   //myBank.printTransactionInfo(anum);
                   //detail information
                   BankApp.printHistory(history, anum);
                  
                   System.out.println("Today : " + cal.getTime());

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

                   hstr = cal.getTime().toString()+" # Printed detailed account information ";
                   BankApp.add(history, anum, hstr);
                  
                  
                   break;
               case 6:
                   System.out.println(" Enter an ACCOUNT number");
                   anum = s.nextInt();
                   myBank.printTransactionInfo(anum);
                   System.out.println(" Your money after 60 days ");
                   cal.add(Calendar.DATE, 60);

                   System.out.println(" 60 days later " + cal.getTime());

                   hstr = cal.getTime().toString()+" # Checked balance after 60 days ";
                   BankApp.add(history, anum, hstr);
                  
                  
                   break;
               default:
                   System.out.println(" Invalid option. Please try again.");

                   return;
                   /**
                   * Catch ( Exception e) { System.out.println("Exception ")
                   * input.nextLine();
                   */

               }
           } catch (Exception exc) {
               System.out.println(" Error: " + exc.getMessage());
           }
       } while (user_choice != '6');

   }

   public static void add(HashMap<Integer,ArrayList<String>> history, int accNo, String log) {
       ArrayList<String> data = history.get(accNo);
       if(data == null)
           data = new ArrayList<String>();
       data.add(log);
   }
  
   public static void printHistory(HashMap<Integer,ArrayList<String>> history, int accNo) {
       ArrayList<String> data = history.get(accNo);
       if(data != null) {
           for(int i = data.size()-1; i > 0; i--)
               System.out.println(data.get(i));
       } else
           System.out.println("No history found");
      
   }
}