Back to Examples
package edu.iuk.BankProject1;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

//Jacob Morris
//Morris Programming

public class Bank {

  //Create an array of created accounts
  static ArrayList accountsList = new ArrayList();
  //Create a variable for customers
  static Customer currentCustomer;
  static File f = new File("Accounts.dat");

  public static void main(String[] args) {

    if (f.exists()) {

      try {

        ObjectInputStream input = new ObjectInputStream(new FileInputStream(f));
        accountsList = (ArrayList) input.readObject();
        input.close();

      } catch (Exception e) {

        System.out.println( e.getMessage() );
        e.printStackTrace();
        System.exit(0);

      }

    }

    //loop the bank program until the user enters 10;
    for(int i =0; i <1; i=i){

      //Display the bank menu
      System.out.println("Bank Menu\n" +
      "================================\n" +
      "1. Create Checking Account\n" +
      "2. Create Gold Account\n" +
      "3. Create Regular Account\n" +
      "4. Deposit\n" +
      "5. Withdraw\n" +
      "6. Display Account Information\n" +
      "7. Remove an Account\n" +
      "8. Apply End of Month Fees\n" +
      "9. Display Bank Statistics\n" +
      "10. Save and Exit");

      //receive user input
      Scanner input = new Scanner(System.in);
      System.out.print("\nPlease input your choice (1-10):");
      String selection = input.nextLine();

      //Call method related to the input selected by the user
      if(selection.equals("1")){

        createChecking();

      } else if(selection.equals("2")){

        createGold();

      } else if(selection.equals("3")) {

        createRegular();

      } else if(selection.equals("4")) {

        depositCurrency();

      } else if(selection.equals("5")){

        withdrawCurrency();

      } else if(selection.equals("6")){

        displayAccountInformation();

      } else if(selection.equals("7")) {

        removeAccount();

      } else if(selection.equals("8")) {

        applyFees();

      } else if(selection.equals("9")) {

        displayBankStatistics();

      } else if(selection.equals("10")) {

        endProgram();

      }

      //Ensure user makes a valid selection
      else {

        System.out.println("\nPlease make a valid selection!\n");

      }

    }

  }

  public static void createChecking(){

    //receive customer name
    Scanner name = new Scanner(System.in);
    System.out.print("Please input the customer name: ");
    String customerName = name.nextLine();

    //receive customer ID
    Scanner ID = new Scanner(System.in);
    System.out.print("Please input the customer ID: ");
    String customerID = ID.nextLine();

    //receive customer phone number
    Scanner phone = new Scanner(System.in);
    System.out.print("Please input the customer Phone Number: ");
    String customerPhone = phone.nextLine();

    //receive account number
    Scanner account = new Scanner(System.in);
    System.out.print("Please input the Account Number: ");
    String accountNumber = account.nextLine();

    //add new customer and new account to the arrays
    currentCustomer =new Customer(customerID, customerName, customerPhone);
    accountsList.add(new Checking(accountNumber, 0.0, currentCustomer));
    System.out.println("\nAccount Created Successfully!\n");

  }

  public static void createGold(){

    //receive customer name
    Scanner name = new Scanner(System.in);
    System.out.print("Please input the customer name: ");
    String customerName = name.nextLine();

    //receive customer ID
    Scanner ID = new Scanner(System.in);
    System.out.print("Please input the customer ID: ");
    String customerID = ID.nextLine();

    //receive customer phone number
    Scanner phone = new Scanner(System.in);
    System.out.print("Please input the customer Phone Number: ");
    String customerPhone = phone.nextLine();

    //receive account number
    Scanner account = new Scanner(System.in);
    System.out.print("Please input the Account Number: ");
    String accountNumber = account.nextLine();

    //add new customer and account to the array
    currentCustomer = new Customer(customerID, customerName, customerPhone);
    accountsList.add(new Gold(accountNumber, 0.0, currentCustomer));
    System.out.println("\nAccount Created Successfully!\n");

  }

  public static void createRegular(){

    //receive customer name
    Scanner name = new Scanner(System.in);
    System.out.print("Please input the customer name: ");
    String customerName = name.nextLine();

    //receive customer ID
    Scanner ID = new Scanner(System.in);
    System.out.print("Please input the customer ID: ");
    String customerID = ID.nextLine();

    //receive customer phone number
    Scanner phone = new Scanner(System.in);
    System.out.print("Please input the customer Phone Number: ");
    String customerPhone = phone.nextLine();

    //receive account number
    Scanner account = new Scanner(System.in);
    System.out.print("Please input the Account Number: ");
    String accountNumber = account.nextLine();

    //add new customer and account to the array
    currentCustomer = new Customer(customerID, customerName, customerPhone);
    accountsList.add(new Regular(accountNumber, 0.0, currentCustomer));
    System.out.println("\nAccount Created Successfully!\n");

  }

  public static void depositCurrency(){

    //receive account number
    Scanner account = new Scanner(System.in);
    System.out.print("Please input the Account Number: ");
    String accountNumber = account.nextLine();

    //receive deposit amount
    try{

      //No accounts are made yet
      if(accountsList.size() == 0) {

        System.out.println("\nAccount Not Found!\n");

      } else{

        //find account with received account number
        for(int i1=0; i1
          if(accountsList.get(i1).getAccountNumber().equals(accountNumber)) {

            //receive deposit amount
            Scanner amount = new Scanner(System.in);
            System.out.print("Please input the amount: ");
            double depositAmount = amount.nextDouble();

            //deposit amount must be a positive integer
            if (depositAmount >=0){

              //deposit
              accountsList.get(i1).depositCurrency(depositAmount);
              System.out.println("\nAccount Updated Successfully!\n New Account Balance: " + accountsList.get(i1).getAccountBalance() + "\n");

              break;

            } else {

              System.out.println("\nPlease enter a valid amount!\n");
              break;

            }

          }

          //account is not found
          else if(i1 + 1 == accountsList.size()) {

            System.out.println("\nAccount Not Found!\n");
            break;

          }

        }

      }

      //invalid number entered for deposit amount.
    } catch (InputMismatchException a){

      System.out.println("\nPlease enter a valid amount!\n");

    }

  }

  public static void withdrawCurrency(){

    //receive account number
    Scanner account = new Scanner(System.in);
    System.out.print("Please input the Account Number: ");
    String accountNumber = account.nextLine();

    try{

    //withdraw amount must be a positive integer
    //No accounts are made yet
    if(accountsList.size() == 0) {

      System.out.println("\nAccount Not Found!\n");

    } else{

      //find account with received account number
      for(int i1=0; i1
        if(accountsList.get(i1).getAccountNumber().equals(accountNumber)) {

          //receive withdraw amount
          Scanner amount = new Scanner(System.in);
          System.out.print("Please input the amount: ");
          double withdrawalAmount = amount.nextDouble();

          if(withdrawalAmount>=0) {

            //withdrawal
            accountsList.get(i1).withdrawalCurrency(withdrawalAmount);
            System.out.println("\nAccount Updated Successfully!\n New Account Balance: " + accountsList.get(i1).getAccountBalance() + "\n");
break;

          } else {

            System.out.println("\nPlease enter a valid Amount!\n");
          }

        }

        //account is not found
        else if(i1 + 1 == accountsList.size()) {

          System.out.println("\nAccount Not Found!\n");
          break;

        }

      }

    }

    //invalid number entered for withdraw amount.
    } catch (InputMismatchException a){

      System.out.println("\nPlease enter a valid amount!\n");

    }

  }

  public static void displayAccountInformation(){

    //receive account number
    Scanner account = new Scanner(System.in);
    System.out.print("Please input the Account Number: ");
    String accountNumber = account.nextLine();

    //No accounts made yet
    if(accountsList.size()==0) {

      System.out.println("\nAccount Not Found!\n");

    } else {

      //find account with received account number
      for(int i1=0; i1
        if(accountsList.get(i1).getAccountNumber().equals(accountNumber)) {

          //display information for the account
          System.out.println(accountsList.get(i1).displayAccountInformation() + "\n");
          break;

        }
        //account not found
        else if(i1+1 == accountsList.size()) {

          System.out.println("\nAccount Not Found!\n");
          break;

        }

      }

    }

  }

  public static void removeAccount(){

    //receive account number
    Scanner account = new Scanner(System.in);
    System.out.print("Please input the Account Number: ");
    String accountNumber = account.nextLine();

    //No accounts made yet
    if(accountsList.size()==0) {

      System.out.println("\nAccount Not Found!\n");
    } else {

      //find account with received account number
      for(int i1=0; i1
        if(accountsList.get(i1).getAccountNumber().equals(accountNumber)) {

          //remove the account from the array
          accountsList.remove(i1);
          System.out.println("\nAccount successfully removed!\n");
          break;

        }
        //account not found
        else if(i1+1 == accountsList.size()) {

          System.out.println("\nAccount Not Found!\n");
          break;

        }

      }

    }

  }

  public static void applyFees(){

    //apply fees to all accounts in the array
    //This function should be used once every end of month and will apply interest to the Regular and Gold accounts
    //and deduct transaction fees from Checking accounts whenever applicable
    for(int i1=0; i1
      //checking account fees
      if(accountsList.get(i1).getClass()== Checking.class) {

        ((Checking)accountsList.get(i1)).addFees();

      }
      //Gold account fees
      else if(accountsList.get(i1).getClass()== Gold.class) {

        ((Gold)accountsList.get(i1)).addGoldInterest();

      }
      //Regular account fees
        else if(accountsList.get(i1).getClass()== Regular.class) {

          ((Regular)accountsList.get(i1)).addRegularInterest();

        }
    }

    System.out.println("\nFees Successfully Applied!\n");

  }
  public static void displayBankStatistics(){

    //this feature will display a simple report for bank
    //administrators that include the total sum of all accounts in the
    //bank, number of zero-balance accounts, average balance of accounts, and the
    //account with largest balance.

    //declare and Initialize variables for bank information
    double accountTotal =0;
    double accountAverage =0;
    int emptyAccounts = 0;
    double largestAccount = 0;
    String largestOwner = "";

    for(int i1=0; i1
      //add all account balances together, and put them in variable accountTotal
      accountTotal += accountsList.get(i1).getAccountBalance();

      //add number of empty accounts, and put them in variable emptyAccounts
      if(accountsList.get(i1).getAccountBalance() == 0.0) {

        emptyAccounts += 1;

      }

      //Find largest account in the array
      if(accountsList.get(i1).getAccountBalance() > largestAccount) {

        //variable largestAccount holds the largest account balance
        largestAccount = accountsList.get(i1).getAccountBalance();

        //variable largestOwner holds the owner name of the largest account
        largestOwner = accountsList.get(i1).getCustomer().getCustomerName();

      }

    }

    //variable accountAverage takes the total of all accounts, and divides it by number of accounts.
    accountAverage = (accountTotal / accountsList.size());

    //Display bank statistics
    System.out.println("\nBank Statistics:\n" +
    "==========================\n" +
    "Total Bank Balance: " + accountTotal +
    "\nAverage Balance: " + accountAverage +
    "\nNumber of Empty Accounts: " + emptyAccounts +
    "\nLargest Account Owner: " + largestOwner + "\n");

  }

  public static void endProgram(){

    try {

      ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(f));
      output.writeObject(accountsList);
      output.close();

    } catch (Exception e) {

      System.out.println( e.getMessage() );
      e.printStackTrace();
      System.exit(0);

    }

    //End the program.
    System.exit(0);

  }

}
Back to Examples

Startup Discount
Small Business?
Contact us for an Enormous Discount!