2
\$\begingroup\$

I am new to Java programming and this is my first assignment. I have worked tirelessly to make sure everything works. What I am looking for is to see if there is any better way I can optimize or better organize the code. This is for my better understanding so I can build another program for my next assignment.

The program needs to lookup customer information via a customer ID and print relevant information pertaining to that ID (I've created temp customers). Also, it should be able to list all customers, whether existing or created newly. It needs to allow creation of new customers and a maximum of 5 bank accounts per user. New accounts can be added until the max of 5 is reached.

import java.util.*;
import java.util.InputMismatchException; // for error checking

public class BankSystem{

   public static void main(String[] args){

      Date date = new Date();                 // Create Date Object for TimeStamp 
      Scanner input = new Scanner(System.in); // Initializes Scanner for User Input
      String selection;                        // Variable for menu selection
      boolean controlinput;                    // variable for error control

      MyBank customer = new MyBank();        // Initialzie customer objects


      System.out.println("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" );                         
      System.out.println("  Date: " + date.toString());
      System.out.println("\n  Welcome to The Banking System\n");
      System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" );                         

      System.out.println("Please make a Selection:\n\n" 
         + "1. Search by Customer ID \n2. List All Customers \n3. Add Customer \n4. Add Account(s)\n5. Exit"); 

      // Displays Menu for user choice // Error checking included   
      while (input.hasNext()) {

         selection = input.next(); 

         switch (selection) {
            //Search for Individual Customer via Customer ID  w/ error check      
            case "1" : controlinput=true;   
                       do {
                          try{
                             System.out.println("SELECTED --> Search for Customer\n");
                             System.out.println("Please Enter 3-Digit Customer ID #: ");
                             int entry = input.nextInt();
                             customer.printAccountInfo(entry);
                             break;
                          }
                          catch (InputMismatchException ex) {
                             System.out.println("\n\n////////// Incorrect input ////////// \n\n");
                             input.nextLine(); // Discard input
                          }
                       } while (controlinput);
                       break;
            // Displays all customers to screen w/ error check
            case "2" : System.out.println("SELECTED --> List all Customers"); 
                       customer.printAccountInfoAll(); // Call method to print
                       break;
            // Add Customer w/ error check
            case "3" :  controlinput=true;
                         do {
                            try{
                               System.out.println("SELECTED --> Add Customer");
                               System.out.println("Enter the customers First Name: ");
                               String x1 = input.next(); 
                               System.out.println("Enter the customer Last Name");
                               String x2 = input.next();
                               System.out.println("Enter the customers Email Address");
                               String x3 = input.next();
                               System.out.println("Enter a customers phone number including dashes(-)");
                               String x4 = input.next();
                               System.out.println("Create 4 Digit Pin (Cannot Begin with 0)");
                               int x5 = input.nextInt(); 
                                  while (x5<1000 || x5>10000){ // error checking pin
                                     System.out.println("Only 4 Numbers (Cannot Begin with 0)");
                                     x5 = input.nextInt();
                                  }

                               customer.openNewAccount(x1,x2,x3,x4,x5); // Call Constructor for create new Customer
                               break;
                            }
                           catch (InputMismatchException ex) {
                             System.out.println("\n\n////////// Incorrect input ////////// \n\n");
                              input.nextLine(); // Discard input
                             }
                        } while (controlinput);
                        break;
            // Add account to existing customer w/ error check
            case "4" :  controlinput=true;
               do {
                  try{
                     System.out.println("SELECTED --> Add Account");
                     System.out.println("Please Enter 3-Digit Customer ID #: ");
                     int entry = input.nextInt();
                     System.out.println("Please Choose Account type:\n1. Savings\n2. Checking\n3. Money Market"
                                                 + "\n4. Deposit Box");
                     int type = input.nextInt();
                     System.out.println("Please Enter Opening Balance for account");
                     double bal = input.nextDouble();
                     customer.addNewAccount(entry,type,bal);
                     break;
                  }

                  catch (InputMismatchException ex) {
                     System.out.println("\n\n////////// Incorrect input ////////// \n\n");
                     input.nextLine(); // Discard input
                  }
               } while (controlinput);
               break;

            // exit program selection                
            case "5" : System.out.println("Exiting..."); System.exit(0);

            //default for invalid selection or program continuation
            default : System.out.println("\nInvalid Selection. Please Try Again."); 
         } // End  Switch  Selection  


         System.out.println("\nPlease make a Selection:\n\n" 
            + "1. Search by Customer ID \n2. List All Customers \n3. Add Customer \n4. Add Account(s)\n5. Exit");  

      }  // End While Loop

   } // END OF MAIN METHOD

} // END OF MAIN CLASS "BankSystem"

Second File:

public class MyBank{

   static CustInfo customer[]; //declared static to be avabile throughout the system


   public MyBank(){

      customer = new CustInfo[30]; // creates 30 customer objects

      for (int i = 0; i<customer.length;i++){ 
         customer[i] = new CustInfo();
      }  

      // Generate 5 Customers and a mix of accounts per Customer   
      openNewAccount("Moe","Ram","Mr@gmail.com","456-262-4780", 1111);       
      customer[0].custAcct[0].createAccount("savings", 500.25);
      customer[0].custAcct[1].createAccount("checking", 20.15);
      customer[0].custAcct[2].createAccount("checking", 30.84);
      customer[0].custAcct[3].createAccount("money market", 50);
      customer[0].custAcct[4].createAccount("deposit box", 0);
      openNewAccount("Betty","Cook","betty@gmail.com","862-245-4780", 2222);
      customer[1].custAcct[0].createAccount("savings", 5000.25);
      customer[1].custAcct[1].createAccount("savings", 2000.15);
      customer[1].custAcct[2].createAccount("checking", 3000.84);
      openNewAccount("Mike","Eggs","mike@gmail.com","547-262-5785", 3333);
      customer[2].custAcct[0].createAccount("checking", 10000.11);
      customer[2].custAcct[1].createAccount("checking", 32000.84);
      openNewAccount("Sally","Pancake","sally@gmail.com","542-445-4780", 4444);
      customer[3].custAcct[3].createAccount("money market", 50);
      customer[3].custAcct[4].createAccount("deposit box", 0);
      openNewAccount("James","Apple","james@gmail.com","964-258-4780", 5555);
      customer[4].custAcct[4].createAccount("deposit box", 0);



   }   

   // Print Selected Customer
   // Prints the account number, the customer information and the balance of the bank accounts 
   public void printAccountInfo(int entry){
      for (int i=0;i<customer.length;i++){
         if (entry == customer[i].custID){
            System.out.println("-----------  CUSTOMER INFORMATION  -----------");
            System.out.println("> CustomerID\t" + customer[i].custID);
            System.out.println("> First Name\t" + customer[i].firstName);
            System.out.println("> Last Name \t" + customer[i].lastName);
            System.out.println("> Email     \t" + customer[i].email);
            System.out.println("> Phone     \t" + customer[i].phone);
            System.out.println("> Pin #     \t" + customer[i].pin);
            System.out.println("----------------------------------------------");
            System.out.println("-----------  ACCOUNT INFORMATION  ------------");
            System.out.println("Total Accounts: " + MyBank.customer[i].custAcct[0].getAcctTally(i));
            for (int k = 0; k<5;k++){
               if (customer[i].custAcct[k].active == true) {
                  System.out.println("\nAccount Type: " + customer[i].custAcct[k].accttype);
                  System.out.println("Account Number: " + customer[i].custAcct[k].acctnum);
                  System.out.println("Balance: " + customer[i].custAcct[k].balance);
               }  
            }
            System.out.println("----------------------------------------------");
            return;
         }

      }
      System.out.println("-------------------------------------------");                
      System.out.println("\tAccount number not found!");
      System.out.println("-------------------------------------------");
   }  

   // Print All Customers //
   // Prints the account number, the customer information and the balance of the bank accounts 

   public void printAccountInfoAll(){
      for (int i=0; i<customer.length; i++){
         if (customer[i].custID!=0){
            System.out.println("-----------  CUSTOMER INFORMATION  -----------");
            System.out.println("> CustomerID\t" + customer[i].custID);
            System.out.println("> First Name\t" + customer[i].firstName);
            System.out.println("> Last Name \t" + customer[i].lastName);
            System.out.println("> Email     \t" + customer[i].email);
            System.out.println("> Phone     \t" + customer[i].phone);
            System.out.println("> Pin #     \t" + customer[i].pin);
            System.out.println("-------------------------------------------");
            System.out.println("-----------  ACCOUNT INFORMATION  ------------");
            System.out.println("Total Accounts: " + MyBank.customer[i].custAcct[0].getAcctTally(i));
            for (int k = 0; k<5;k++){
               if (customer[i].custAcct[k].active == true) {
                  System.out.println("\nAccount Type: " + customer[i].custAcct[k].accttype);
                  System.out.println("Account Number: " + customer[i].custAcct[k].acctnum);
                  System.out.println("Balance: " + customer[i].custAcct[k].balance);
               }  
            }
            System.out.println("----------------------------------------------\n");

         }
      }
   }                                            

    // Method to Add NEW Customer //              
   public void openNewAccount(String firstName, String lastName, String email, String phone, int pin) {

      CustInfo newcust = new CustInfo(firstName, lastName, email, phone, pin);

      for (int i=0; i<customer.length; i++){
         if (customer[i].custID == 0){
            customer[i] = newcust;
            if (customer[0].custID == 100) customer[i].custID = customer[i-1].custID +1;
            else customer[i].custID = 100; 
            System.out.println(">>Account Created with ID #: " + customer[i].custID);
            break;
         }

      } 
   }  


   // method to add new accounts to new or existing customer
   // error checking included for max accounts a customer can have
   // error checking for customer not in system
   public void addNewAccount(int entry, int type1, double bal){

      String type2="";
      switch(type1) {

         case 1: type2 = "Savings"; break;
         case 2: type2 = "Checking"; break;
         case 3: type2 = "Money Market"; break;
         case 4: type2 = "Deposit Box"; break;
      }

      for (int i=0;i<customer.length;i++){
            if (entry == customer[i].custID){
               for (int k = 0; k<5;k++){
                  if (customer[i].custAcct[k].active == true) continue;
                  if (customer[i].custAcct[k].active == false) {
                     customer[i].custAcct[k].createAccount(type2,bal);
                     System.out.println(">>>" + type2 + " account added to Customer ID: "
                                         + customer[i].custID);
                     return;
                  }  

                }         
             } 
         }


         System.out.println(">>> MAX ACCOUNTS REACHED or  ACCOUNT NOT FOUND"); return;

   }   



}  /////////////////    END CLASS: //////  MyBank    /////////////////


class CustInfo {

   // variables used for the creation of a customer object and array of accounts
   int custID; 
   String firstName;
   String lastName;
   String email; 
   String phone;
   int pin;
   Account custAcct[] = new Account[5];


   public CustInfo(){

   } 


   // New Customer Constructor // 
   public CustInfo(String firstName, String lastName, String email, String phone, int pin){

      this.custID = custID;          // New Customer Constructor
      this.firstName = firstName;
      this.lastName = lastName;
      this.email = email;
      this.phone = phone;
      this.pin = pin;

      for (int i = 0; i<custAcct.length;i++){
         this.custAcct[i] = new Account();  
      }

   }




}  /////////////////    END CLASS: //////  CustInfo    /////////////////


class Account{

   // variables for creating and managing accounts
   boolean active;
   String accttype;
   int acctnum;
   double balance;
   int accttally;

   // new account constructor
   public Account(){

      active = false;
      accttype = "none";
      acctnum = ((int)(Math.random()*10000000)); // generate unique account numbers per account

   }

   // method to create new accoutnt
   public void createAccount(String accttype, double balance){

   this.active = true;
   if      (accttype.equalsIgnoreCase("savings")) this.accttype = "Savings";
   else if (accttype.equalsIgnoreCase("checking")) this.accttype = "Checking";
   else if (accttype.equalsIgnoreCase("money market")) this.accttype = "Money Market";
   else if (accttype.equalsIgnoreCase("deposit box")) this.accttype = "Deposit Box";
   else    this.accttype = "none";
   this.balance = balance;
   this.accttally= accttally+1;
   } 

   // getter for counting total number of active accounts per customer
   public int getAcctTally(int i){

    int total=0;
    for (int k=0;k<5;k++){
        total += MyBank.customer[i].custAcct[k].accttally;                     
     }

   return total;
   }

}  /////////////////    END CLASS: //////  Accounts    /////////////////
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.