For this project, your goal is to improve on the Bank system that you wrote in Project 1. You will add a few new features and upgrade a few of the existing ones. For example, now the user will have an option to save all of the bank account information to a file. What this will mean is that now they will be able to load the same set of accounts on any other machine that can run this program.

The following are the changes you will make:

  • The Bank class have the following changes:
    • The accounts field will now become an ArrayList< BankAccount> object instead of a plain array. Because an instance of ArrayList takes care of its size and capacity automatically, the field numAccounts and the method grow() will not be needed anymore. The process of adding, removing, and retrieving BankAccount objects within the bank will have to be adjusted accordingly.
    • writeToFile(out) -- a new method that will take a PrintWriter as an argument and write all of the bank accounts to it. Note that this method will just repeatedly call writeToFile(out) method of the BankAccount class for each BankAccount item in the accounts array.
    • readFromFile(in) -- a new method that will take a Scanner as an argument and read all of the bank accounts from it. Note that this method will just repeatedly call readFromFile(in) method of the BankAccount class as long as there are lines remaining in the provided scanner.
  • The BankAccount class will have the following changes:
    • hashOf(pin) -- a new static method that will take a PIN as an argument and return a hashed version of it (see what the words "hash" / "hashing" mean further below). You can either try to write your own hashing function as an exercise (it does not have to be cryptographically secure) or use this one:

      public static String hashOf(String pin) {
      // Ignore if the PIN is invalid
      if ( pin == null || pin.length() != 4 ) {
      return null;
      }

      // Run a silly, cryptic algorithm
      String hash = "";
      for ( int i = 0; i < 4; ++i ) {
      int digit = (int)pin.charAt(i);
      hash += "" + (digit | digit%3);
      hash += "" + (((digit+i)%5) * digit) + i;
      hash += "" + digit%2;
      }

      // Return the hash
      return hash;
      }

      (Note that the method I am providing is silly and inefficient, but to an unexperienced attacker's eye, the hash will not reveal much information about the PIN).
    • instead of storing the user's PIN directly, this class will now store a hash of the user's pin calculated using hashOfmethod described above (i.e. the field pin will now be replaced by the field hashOfPIN). To accomodate for that, all of the methods involving PIN confirmation/entry will now have to use the hashOf method to first calculate the hash of the PIN before comparing it to the hashOfPIN stored.. Note that the method setPIN will now only set a new hash. Also note that the method getPIN() will now be replaced by getHashOfPIN().
    • writeToFile(out) -- a new method write the account information as one line in the provided file. All of the values should be separated by the vertical bar character(see example further below).
    • BankAccount(in) -- a new constructor that will accept a Scanner as an argument and read the account information from one line of the scanner. The format it should read is equivalent to the format used in writeToFile method.
  • The landingMenu method of BankMenu class will now display two additional menu options to allow the user to save/read the bank records to/from a file. The new menu will look like this:
    1.Log in
    2.Sign up
    3.Terminate an account
    4.Save Bank account list to a file
    5.Load Bank account list from a file
    6.Exit
  • The BankMenu class will now have two new methods that correspond to options 4 and 5 in the landing menu:
    • saveToFileMenu(bank) -- will prompt the user to enter the name of the file to save bank records to. Note: to avoid unnecessary complexity when dealing with files, you can assume that the user will enter a simple file name without any additional path information in it (e.g. the file name will be something like bank.txt or records.txt, but not something like ../bankAccounts/newFolder/bank.txt). Once the file name is retrieved, this method should create a PrintWriter object and call the saveToFile method of Bank class to perform the saving.
    • loadFromFileMenu(bank) -- will prompt the user to enter the name of the file to load from. Similarly to saveToFileMenu, you can assume that the user will enter a simple file name without any additional path information in it. This method should first check if the file actually exists, and if it doesn't, it should print an appropriate error message and return back to the landing menu. If the file does exist, this method should create a Scanner object and call loadFromFilemethod of Bank class to perform the loading.

Example of an output file

Let's suppose we have a bank account registered under the name "Sally Johnson" who lives in "101 Blank Street". She has a username "sally123" and her PIN hashes to 4919601511021151153215516531. Her account balance is $200,000. Then writing her account to the file will result in this line:

Sally|Johnson|101 Blank Street|sally123|4919601511021151153215516531|200000
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.