unsplash-image-3wPJxh-piRw.jpg

Java - Banking System

Banking System with input Validation - Java


This is a program written in Java to add bank accounts, modify them and perform their monthly calculations. Each account requires the account type, account number, account holder’s name, phone number, email, opening balance, fees for a checking account, minimum balance and interest rate for savings account.

This program uses an Array List to store Objects of the type “BankAccount” as defined by the class “BankAccount” which implements the interface “BankSimulator”. There are two different types of accounts, savings and checking, these are two different classes that extend the bank account class. Each account has an account holder that is an object of the class “Person”. The “Bank” class houses the array list and implements all the methods of the other classes to populate the array. The class “Validate” is used wherever input is needed to ensure the correct input is being used. Finally the main method houses the menu and calls on the “Bank” class to make it all work.

A video demonstrating the output and the code is shown below:

Source Code

Main Method
Person
Account Templates
Account Types
Bank
Validation
Exported from Notepad++
1 // Name: Mostapha Abdelaziz 2 // Class: CST8132 #303 3 // 4 // Assessment: Lab 6 5 // Description: Class for the main method 6 7 package Banking; 8 9 /** 10 * This class is for the main method. 11 * @author Mostapha Abdelaziz 12 * @version 1.0 13 * @since 1.8 14 */ 15 public class Lab6 { 16 /** 17 * Main method to call and run the code. 18 * @param String[] 19 */ 20 //main method 21 public static void main(String[] args) { 22 String menu = "q"; 23 Bank bank = new Bank(); 24 // welcome message 25 System.out.println("======= Welcome to the Banking Simulation Program =======\n"); 26 27 // loop to loop through program until quitting 28 do { 29 // menu and case structure to implement choice 30 System.out.print("a: Add new account \n" + "u: Update an account\n" + "d: Display an account\n" 31 + "p: Print all accounts\n" + "m: Run monthly update\n" + "q: Quit\n" + "\n" 32 + "Enter your option: "); 33 menu = Validate.iMenu("Invalid menu choice, try again : ", "a", "u", "d", "p", "m", "q"); 34 35 // switch for each option 36 switch (menu.toLowerCase()) { 37 case "a": 38 System.out.println(); 39 bank.addAccount(); 40 System.out.println(); 41 break; 42 case "u": 43 System.out.println(); 44 bank.updateAccount(); 45 System.out.println(); 46 break; 47 case "d": 48 System.out.println("\n"+bank.displayAccount()); 49 break; 50 case "p": 51 System.out.println(); 52 bank.printAccountDetails(); 53 System.out.println(); 54 break; 55 case "m": 56 System.out.println(); 57 bank.monthlyUpdate(); 58 System.out.println("\n"); 59 break; 60 } 61 62 } while (!menu.equalsIgnoreCase("q")); 63 64 // quit message 65 System.out.println("\nQuit: Successfully exited the program"); 66 } 67 68 } 69
Exported from Notepad++
1 // Name: Mostapha Abdelaziz 2 // Class: CST8132 #303 3 // 4 // Assessment: Lab 6 5 // Description: Class for the Person object to be used as the account holder 6 7 package Banking; 8 9 /** 10 * This class is for a person object to be used as the account holder. 11 * @author Mostapha Abdelaziz 12 * @version 1.0 13 * @since 1.8 14 */ 15 public class Person { 16 // instance variables 17 private String firstName; 18 private String lastName; 19 private String email; 20 private long phoneNumber; 21 22 /** 23 * No argument constructor to be able to create an object without parameters. 24 */ 25 //no argument constructor 26 public Person() { 27 28 } 29 30 // getter method for name 31 /** 32 * Method to access the private variables of first name and last name. 33 * @return return the first name and last name together of the Person. 34 */ 35 public String getName() { 36 String Name = firstName + " " + lastName; 37 return Name; 38 } 39 40 // getter method for email 41 /** 42 * This is a method to access the email address of the Person. 43 * @return return the email address of the Person. 44 */ 45 public String getEmail() { 46 return email; 47 } 48 49 // getter method for phone number 50 /** 51 * This is a method to access the phone number of the person. 52 * @return 53 */ 54 public long getPhoneNumber() { 55 return phoneNumber; 56 } 57 58 //setter method for first name 59 /** 60 * This is a method to set or change the first name of the person. 61 * @param firstName The desired first name to use. 62 */ 63 public void setFirstName(String firstName) { 64 this.firstName = firstName; 65 } 66 67 //setter method for last name 68 /** 69 * This is a method to set or change the last name of the person. 70 * @param lastName The desired last name to use. 71 */ 72 public void setLastName(String lastName) { 73 this.lastName = lastName; 74 } 75 76 //setter method for email 77 /** 78 * This method is for setting or changing the email address of the person. 79 * @param email The desired email to use. 80 */ 81 public void setEmail(String email) { 82 this.email = email; 83 } 84 85 //setter method for phone number 86 /** 87 * This method is to set or change the phone number of the person. 88 * @param phoneNumber The desired phone number to use. 89 */ 90 public void setPhoneNumber(long phoneNumber) { 91 this.phoneNumber = phoneNumber; 92 } 93 } 94
Exported from Notepad++
1 // Name: Mostapha Abdelaziz 2 // Class: CST8132 #303 3 // 4 // Assessment: Lab 6 5 // Description: Interface to be implemented by BankAccount 6 7 8 package Banking; 9 10 /** 11 * The banking interface to be implemented by the Bank Account class. 12 * @author Mostapha Abdelaziz 13 * @version 1.0 14 * @since 1.8 15 */ 16 public interface BankSimulator { 17 //abstract methods for implementation 18 /** 19 * Return a formatted string of all the account information. 20 * @return The formatted string of all information 21 */ 22 public String toString(); 23 24 /** 25 * Take input from the user and create a new Bank Account. 26 * @return A boolean value of whether it was successful or not. 27 */ 28 public boolean addBankAccount(); 29 30 /** 31 * Update the balance of the account based on a withdrawal or a deposit. 32 * @param The amount withdrawn or deposited. 33 */ 34 public void updateBalance(double change); 35 36 } 37
Exported from Notepad++
1 // Name: Mostapha Abdelaziz 2 // Class: CST8132 #303 3 // 4 // Assessment: Lab 6 5 // Description: Abstract class to be used as the basis for savings and chequing accounts 6 7 package Banking; 8 import java.text.DecimalFormat; 9 10 /** 11 * Bank account class to be extended by savings and chequing accounts. 12 * Contains the common attributes and behaviours. 13 * @author Mostapha Abdelaziz 14 * @version 1.0 15 * @since 1.8 16 */ 17 abstract class BankAccount implements BankSimulator { 18 // instance variables 19 /** The bank account number, up to 8 digits **/ 20 protected long accountNumber; 21 /** The account holder, an object of class Person **/ 22 protected Person accHolder = new Person(); 23 /** The balance of amount of funds available in the account **/ 24 protected double balance; 25 26 // no argument constructor 27 /** 28 * No argument constructor to create a bank account without parameters. 29 */ 30 BankAccount() { 31 32 } 33 34 35 //return the data of the account 36 /** 37 * Return a formatted string of all the account information. 38 * @return The formatted string of all information 39 */ 40 public String toString() { 41 //for formatting balance 42 DecimalFormat format = new DecimalFormat("0.##"); 43 44 //add all the details to one string 45 String details; 46 details = "AccountNumber: " +accountNumber+ " | Name: " +accHolder.getName()+ 47 " | Phone Number: " +accHolder.getPhoneNumber()+ 48 " | Email Address: " +accHolder.getEmail()+ 49 " | Balance: " +format.format(balance); 50 51 //return string 52 return details; 53 } 54 55 //method for adding a bank account to the array 56 /** 57 * Take input from the user and create a new Bank Account. 58 * @return A boolean value of whether it was successful or not. 59 */ 60 public boolean addBankAccount() { 61 // ask for and store input to create a new account 62 //account details 63 System.out.print("Enter account number (up to 8 digits) : "); 64 accountNumber = Validate.iLong("Invalid account Number, try again : " , 0, 99999999); 65 66 // account holder details 67 System.out.print("Enter first name of account holder : "); 68 accHolder.setFirstName(Validate.iString("Invalid Name, try again : ", "`","~","1","2","3","4","5", 69 "6","7","8","9","0","_","=","+","!","@","#","$","%","^","&","*","(",")",";",":","[", 70 "]","{","}","|","\\","\"",",",".","/","<",">","?")); 71 72 System.out.print("Enter last name of account holder : "); 73 accHolder.setLastName(Validate.iString("Invalid Name, try again : ", "`","~","1","2","3","4","5", 74 "6","7","8","9","0","_","=","+","!","@","#","$","%","^","&","*","(",")",";",":","[", 75 "]","{","}","|","\\","\"",",",".","/","<",">","?")); 76 77 System.out.print("Enter phone number of account holder : "); 78 accHolder.setPhoneNumber(Validate.iLong("Invalid Phone Number, try again : " 79 , 1000000000L, 9999999999L)); 80 81 System.out.print("Enter email of account holder : "); 82 accHolder.setEmail(Validate.iString()); 83 84 System.out.print("Enter opening balance of account holder : "); 85 balance = Validate.iDouble("Invalid balance, try again : ", 0); 86 87 // return boolean value to be used next lab 88 return false; 89 } 90 91 //withdraw or deposit from the account 92 /** 93 * Update the balance of the account based on a withdrawal or a deposit. 94 * @param The amount withdrawn or deposited. 95 */ 96 public void updateBalance(double changes) { 97 //add or subtract from current balance 98 balance = balance + changes; 99 } 100 101 //abstract to be used in sub classes 102 /** 103 * Perform the necessary calculations for any monthly fees or interest. 104 */ 105 public abstract void monthlyAccountUpdate(); 106 } 107
Exported from Notepad++
1 // Name: Mostapha Abdelaziz 2 // Class: CST8132 #303 3 // 4 // Assessment: Lab 6 5 // Description: Class for Chequing account, 6 // extend Bank account and adds chequing specific behaviours and attributes 7 8 package Banking; 9 10 /** 11 * A class for a chequing account, extends the Bank account and adds 12 * additional chequing specific functionality. 13 * @author Mostapha Abdelaziz 14 * @version 1.0 15 * @since 1.8 16 */ 17 public class CheckingAccount extends BankAccount { 18 // instance variable 19 private double fee; 20 21 // no argument constructor 22 CheckingAccount() { 23 } 24 25 // return the data of the account 26 /** 27 * Return a formatted string of all the account information. 28 * Uses the super method and adds on to it. 29 * @return The formatted string of all information 30 */ 31 public String toString() { 32 // add all the details to one string 33 String details; 34 details = super.toString() + " | Fee: " +String.format("%.1f", fee)+ 35 "\n"; 36 37 // return string 38 return details; 39 } 40 41 // add a new bank account 42 /** 43 * Take input from the user and create a new Bank Account. 44 * Uses the super method and adds on to it. 45 * @return A boolean value of whether it was successful or not 46 */ 47 public boolean addBankAccount() { 48 // ask for and store input to create a new account 49 // call super method for non checking specific information 50 super.addBankAccount(); 51 52 // checking specific information 53 System.out.print("Enter the Chequing Account's monthly fee : "); 54 fee = Validate.iDouble("Invalid fee amount, try again : ", 0); 55 56 // return boolean value to be used next lab 57 return false; 58 } 59 60 // perform monthly updates 61 /** 62 * Perform the necessary calculations for subtracting any fees from the balance. 63 */ 64 public void monthlyAccountUpdate() { 65 //if statement to determine if balance has enough for fees 66 if(balance >= fee) { 67 // subtract fee from balance 68 balance = balance - fee; 69 } else { 70 //display error message 71 System.out.println("Account " +accountNumber+ 72 " does not have enough balance to subtract the fees of " +fee); 73 } 74 } 75 76 } 77
Exported from Notepad++
1 // Name: Mostapha Abdelaziz 2 // Class: CST8132 #303 3 // 4 // Assessment: Lab 6 5 // Description: Class for Savings account, 6 // extend Bank account and adds savings specific behaviours and attributes 7 8 package Banking; 9 10 /** 11 * A class for a savings account, extends the Bank account and adds 12 * additional savings specific functionality. 13 * @author Mostapha Abdelaziz 14 * @version 1.0 15 * @since 1.8 16 */ 17 public class SavingsAccount extends BankAccount { 18 // instance variable 19 private double interestRate; 20 private double minBalance; 21 22 // no argument constructor 23 /** 24 * No argument constructor to be able to create an object without parameters. 25 */ 26 SavingsAccount() { 27 } 28 29 // return the data of the account 30 /** 31 * Return a formatted string of all the account information. 32 * Uses the super method and adds on to it. 33 * @return The formatted string of all information 34 */ 35 public String toString() { 36 // add all the details to one string 37 String details; 38 details = super.toString() + " | Minimum Balance: " + String.format("%.1f", minBalance) + " | Interest Rate: " 39 + String.format("%.1f", interestRate) + "\n"; 40 41 // return string 42 return details; 43 } 44 45 // add a new bank account 46 /** 47 * Take input from the user and create a new Bank Account. 48 * Uses the super method and adds on to it. 49 * @return A boolean value of whether it was successful or not 50 */ 51 public boolean addBankAccount() { 52 // ask for and store input to create a new account 53 // call super method for non savings specific information 54 super.addBankAccount(); 55 56 // savings specific information 57 System.out.print("Enter the Saving Account's minimum balance : "); 58 minBalance = Validate.iDouble("Invalid balance, try again : ", 0); 59 60 System.out.print("Enter the Saving Account's Yearly interest rate (should be a number in (0,1)) : "); 61 interestRate = Validate.iDouble("Invalid Interest Rate, try again : ", 0, 1); 62 63 // return boolean value to be used next lab 64 return false; 65 } 66 67 // perform monthly updates 68 /** 69 * Perform the necessary calculations for the monthly interest and add to the balance. 70 */ 71 public void monthlyAccountUpdate() { 72 // if statement to determine if balance is above minimum for interest 73 if (balance >= minBalance) { 74 // add interest, divided by 12 because it is a yearly rate and this is a monthly update 75 balance = ((balance * interestRate) / 12) + balance; 76 } else { 77 // display error message 78 System.out.println( 79 "Account " + accountNumber + " does not have the " + minBalance + " required to apply interest"); 80 } 81 82 } 83 84 } 85
Exported from Notepad++
1 // Name: Mostapha Abdelaziz 2 // Class: CST8132 #303 3 // 4 // Assessment: Lab 6 5 // Description: Class for an array of bank accounts, executes their functions as well 6 7 package Banking; 8 9 import java.util.ArrayList; 10 11 /** 12 * A class for the Bank, using an array list to keep track of multiple accounts. 13 * Uses the bank account methods to execute all necessary actions. 14 * @author Mostapha Abdelaziz 15 * @version 1.0 16 * @since 1.8 17 */ 18 public class Bank { 19 // instance variable 20 private ArrayList<BankAccount> accounts = new ArrayList<BankAccount>(); 21 22 // no argument constructor 23 /** 24 * Create a bank without any parameters. 25 */ 26 Bank() { 27 } 28 29 // check account type and add to array 30 /** 31 * Add an account to the array list and use their methods to collect the information. 32 * @return Whether adding an account was successful or not 33 */ 34 public boolean addAccount() { 35 boolean success = false; 36 // variable to store input 37 String type; 38 39 //header 40 System.out.println("Enter the details of account Holder " +(accounts.size()+1)+ 41 "\n=================================\n"); 42 43 // ask for account type 44 System.out.print("Please select the type of account you wish to add. \n" 45 + "Type (without quotations) 'c' for chequing account or 's' for savings account : "); 46 type = Validate.iMenu("Invalid choice, try again : ", "c", "s"); 47 48 //variable to store the bank account 49 BankAccount account; 50 // determine which array object needs to be created based on account type 51 if (type.equalsIgnoreCase("c")) { 52 // if they selected Checking type, create a checking 53 account = new CheckingAccount(); 54 accounts.add(account); 55 } else { 56 // if they selected savings type, create a savings 57 account = new SavingsAccount(); 58 accounts.add(account); 59 } 60 61 // call method to read info 62 account.addBankAccount(); 63 64 // success is true 65 success = true; 66 return success; 67 } 68 69 // find a specific account 70 /** 71 * Compare a provided account number with the account numbers in our bank 72 * @return The index value of which the account is stored, or -1 for an error. 73 */ 74 private int findAccount() { 75 long info; 76 int arrayIndex = -1; 77 78 // validate it is correct account format 79 info = Validate.iLong("Invalid account number, try again : ", 0, 99999999); 80 81 // check if we have this account or not 82 for (int i = 0; i < accounts.size(); i++) { 83 if (accounts.get(i).accountNumber == info) { 84 arrayIndex = i; 85 i = accounts.size(); 86 } 87 } 88 89 // return the index number or -1 90 return arrayIndex; 91 } 92 93 // use account number to display account 94 /** 95 * Uses the findAccount method to return a string of 96 * a specific accounts information or an error message. 97 * @return The string with an error message or account information. 98 */ 99 public String displayAccount() { 100 int arrayIndex = -1; 101 String info = null; 102 103 // ask for account number 104 System.out.print("Please enter an account number to display the information of : "); 105 // use findAccount method 106 arrayIndex = findAccount(); 107 108 // if the array was not found return error message, else return the information 109 if (arrayIndex == -1) { 110 info = "This account could not be found.\n"; 111 } else { 112 info = accounts.get(arrayIndex).toString(); 113 } 114 115 // return the string of error or account info 116 return info; 117 } 118 119 // print all account details 120 /** 121 * Uses a loop to print all the information of every account in our bank array. 122 */ 123 public void printAccountDetails() { 124 //Header 125 System.out.println("Banking System\n" 126 + "******************\n" 127 + "Number of Account Holders : "+accounts.size()+ 128 "\n"); 129 // print each index of the array 130 for (int i = 0; i < accounts.size(); i++) { 131 System.out.print(accounts.get(i).toString()); 132 } 133 } 134 135 // use account number to update an account 136 /** 137 * Asks for a withdrawal or deposit and updates the balance of the account accordingly. 138 */ 139 public void updateAccount() { 140 int arrayIndex = -1; 141 double changes; 142 143 // ask for account number 144 System.out.print("Please enter an account number to update the balance of : "); 145 // use findAccount method 146 arrayIndex = findAccount(); 147 148 // if the array was not found return error message, else ask for the changes and 149 // execute 150 if (arrayIndex == -1) { 151 System.out.println("This account could not be found."); 152 } else { 153 // ask for change 154 System.out.print("Enter the amount to deposit/withdraw " 155 + "(positive number to deposit, negative number to withdraw) \n"); 156 changes = Validate.iDouble("You do not have enough (" 157 +String.format("%.1f", accounts.get(arrayIndex).balance)+ 158 ") to withdraw that amount, try again : ", 159 (accounts.get(arrayIndex).balance * -1)); 160 // update account 161 accounts.get(arrayIndex).updateBalance(changes); 162 } 163 } 164 165 // process monthly update for all accounts 166 /** 167 * Perform all the necessary calculations for fees or interest on all accounts in our array. 168 */ 169 public void monthlyUpdate() { 170 // update each index of the array 171 for (int i = 0; i < accounts.size(); i++) { 172 accounts.get(i).monthlyAccountUpdate(); 173 } 174 } 175 176 }
Exported from Notepad++
1 // Name: Mostapha Abdelaziz 2 // Class: CST8132 #303 3 // 4 // Assessment: Lab 6 5 // Description: Class for validating input or other variables, 6 // reduce clutter in the rest of the code and provides ease in expansion of our services. 7 8 package Banking; 9 10 import java.util.Scanner; 11 12 /** 13 * A class for validating any variables, or input. 14 * @author Mostapha Abdelaziz 15 * @version 1.0 16 * @since 1.8 17 */ 18 public class Validate { 19 private static Scanner input = new Scanner(System.in); 20 21 // validate double values 22 /** 23 * Validate a input of a double value. Display a provided 24 * error message if it is not in the provided range. 25 * @param errorMessage The message to be displayed if the input is incorrect. 26 * @param minValue The minimum value needed for the double. 27 * @param maxValue The maximum value allowed for the double. 28 * @return The validated input double value. 29 */ 30 public static double iDouble(String errorMessage, double minValue, double maxValue) { 31 double doubleValue; 32 33 // loop to continue asking for input 34 do { 35 // validate it is a double 36 while (!input.hasNextDouble()) { 37 System.out.print(errorMessage); 38 input.next(); 39 } 40 // store the double 41 doubleValue = input.nextDouble(); 42 43 // validate it is within range if not print message 44 if (doubleValue < minValue || doubleValue > maxValue) { 45 System.out.print(errorMessage); 46 } 47 48 } while (doubleValue < minValue || doubleValue > maxValue); 49 50 // return validated input 51 return doubleValue; 52 } 53 54 // validate double values 55 /** 56 * Validate a input of a double value. Display a provided 57 * error message if it is not larger than the provided minimum. 58 * @param errorMessage The message to be displayed if the input is incorrect. 59 * @param minValue The minimum value allowed for the double. 60 * @return The validated input double value. 61 */ 62 public static double iDouble(String errorMessage, double minValue) { 63 double doubleValue; 64 65 // loop to continue asking for input 66 do { 67 // validate it is a double 68 while (!input.hasNextDouble()) { 69 System.out.print(errorMessage); 70 input.next(); 71 } 72 // store the double 73 doubleValue = input.nextDouble(); 74 75 // validate it is within range if not print message 76 if (doubleValue < minValue) { 77 System.out.print(errorMessage); 78 } 79 80 } while (doubleValue < minValue); 81 82 // return validated input 83 return doubleValue; 84 } 85 86 // validate long values 87 /** 88 * Validate a input of a long value. Display a provided 89 * error message if it is not larger than the provided minimum. 90 * @param errorMessage The message to be displayed if the input is incorrect. 91 * @param minValue The minimum value needed for the long. 92 * @param maxValue The maximum value allowed for the long. 93 * @return The validated input double value. 94 */ 95 public static long iLong(String errorMessage, long minValue, long maxValue) { 96 long longValue; 97 98 // loop to continue asking for input 99 do { 100 // validate it is a long 101 while (!input.hasNextLong()) { 102 System.out.print(errorMessage); 103 input.next(); 104 } 105 106 // store the long 107 longValue = input.nextLong(); 108 109 // validate it is within range if not print message 110 if (longValue < minValue || longValue > maxValue) { 111 System.out.print(errorMessage); 112 } 113 114 } while (longValue < minValue || longValue > maxValue); 115 116 // return validated input 117 return longValue; 118 } 119 120 // validate a menu choice 121 /** 122 * Validate menu options. Ensure they are one of the provided values. 123 * @param errorMessage The message to be displayed if the input is invalid. 124 * @param choices The acceptable menu options 125 * @return The validated input long. 126 */ 127 public static String iMenu(String errorMessage, String... choices) { 128 // variable for input and loop 129 String menuChoice; 130 boolean loop = true; 131 132 // loop to continue asking for input 133 do { 134 // store input 135 menuChoice = input.next(); 136 // loop to check input 137 for (int i = 0; i < choices.length; i++) { 138 if (menuChoice.equalsIgnoreCase(choices[i])) { 139 i = choices.length; 140 loop = false; 141 } 142 } 143 144 // if the input wasn't one of the valid choices 145 if (loop == true) { 146 System.out.print(errorMessage); 147 } 148 149 } while (loop == true); 150 return menuChoice; 151 } 152 153 //validate string 154 /** 155 * Take a string input. 156 * @return The string. 157 */ 158 public static String iString() { 159 //take input 160 String string; 161 string = input.next(); 162 163 return string; 164 } 165 166 // validate strings with characters not allowed 167 /** 168 * Validate that a input string does not have any provided illegal characters. 169 * @param errorMessage The message to be provided if the input is incorrect. 170 * @param illegalCharacters The characters that are not allowed in the input. 171 * @return The validated input string 172 */ 173 public static String iString(String errorMessage, CharSequence ...illegalCharacters) { 174 String name; 175 boolean loop = true; 176 177 //loop to continue asking 178 do { 179 //store input 180 name = input.next(); 181 182 //loop to check for illegal characters 183 for(int i = 0; i < illegalCharacters.length ; i++) { 184 if(name.contains(illegalCharacters[i])) { 185 System.out.print(errorMessage); 186 loop = true; 187 i = illegalCharacters.length; 188 } else { 189 loop = false; 190 } 191 } 192 }while(loop == true); 193 194 return name; 195 } 196 197 } 198