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

Create a Bank Account class that contains four instance variables: an account nu

ID: 3728078 • Letter: C

Question

Create a Bank Account class that contains four instance variables: an account number (int), a last name (String), a first name (String) and the balance (double). It should also have methods you think appropriate as well as the requisite accessor and mutator methods and necessary constructors.

Design a tester program that uses a Random Access File (RAF) to store (writes to and reads out) the current information for each Bank Account object. When working with Random Access Files, you must give each value a fixed size that is sufficiently large. As a result, every record (object) will have the same size. Integers obviously are 4 bytes; doubles 8 bytes and Strings are of a fixed byte size of your choosing.

The tester should ask the user to enter an account number and an amount to deposit. If the account does not currently exist in the RAF, it should generate an account number and get the user’s first and last name. Each account number must be unique. It should then write out that account number’s data to the RAF. If the account already exists, the deposit will be added to the current balance. After adding the deposit, the user should be informed of the account’s new balance. The user can have multiple accounts or could be a bank teller entering deposits from the Business Drop Box. Hence, a user should be asked if they want to make another deposit(s).

When there are at least 10 accounts in the RAF, use a loop to read out the current information for all account numbers. PrintScreen your results with all the data for the four instance variables of the BankAccount object for all accounts.

At this point, the program should also allow the user to append more new accounts to the file and add additional deposits to at least three of the existing accounts. Use some of the Random Access File methods to locate an object and print it out, determine the size of the file as well as interrogate the file to verify or check a customer’s name or a balance and edit the account record if necessary. PrintScreen again after you have added more accounts, added new deposits to existing accounts and used some of the RAF methods as suggested above.

Explanation / Answer

BankAccount.java


public class bankAccount {
   private int accountNumber;
   private double balance;
   private String firstname;
   private String lastName;
  
   public bankAccount(int accountNumber, double balance, String firstname,
           String lastName) {
       this.accountNumber = accountNumber;
       this.balance = balance;
       this.firstname = firstname;
       this.lastName = lastName;
   }
  
   public bankAccount() {
      
   }

   public int getAccountNumber() {
       return accountNumber;
   }

   public void setAccountNumber(int accountNumber) {
       this.accountNumber = accountNumber;
   }

   public double getBalance() {
       return balance;
   }

   public void setBalance(double balance) {
       this.balance = balance;
   }

   public String getFirstname() {
       return firstname;
   }

   public void setFirstname(String firstname) {
       this.firstname = firstname;
   }

   public String getLastName() {
       return lastName;
   }

   public void setLastName(String lastName) {
       this.lastName = lastName;
   }
  
}

BankAccountTest.java

import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Scanner;


public class BankAccountTest {
  
   public static int getAccountNumberIndex(int index){
       return index;
   }
  
   public static int getBalanceIndex(int index){
       return index + 6;
   }
  
   public static int getFirstNameIndex(int index){
       return index + 16;
   }
  
   public static int getLastNameIndex(int index){
       return index + 38;
   }
  
   public static int getNextLineIndex(int index){
       return index + 60;
   }
  
   public static int getIndex(RandomAccessFile fp, int accountNumber) throws IOException{
       int curIndex = 0;
       while(fp.length() > curIndex){
           fp.seek(curIndex);
           if(fp.readInt() == accountNumber){
               return curIndex;
           }
           curIndex = getNextLineIndex(curIndex);
       }
       return -1;
   }
  
   public static void appendAccount(RandomAccessFile fp, int bankAccount, double balance) throws IOException{
       Scanner in = new Scanner(System.in);
       System.out.print("FirstName: ");
       String firstName = in.next();
       int length = firstName.length();
       for(int i = 0; i < 20 - length; ++i){
           firstName = firstName + ' ';
       }
       System.out.print("LastName: ");
       String lastName = in.next();
       length = lastName.length();
       for(int i = 0; i < 20 - length; ++i){
           lastName = lastName + ' ';
       }
       fp.seek(fp.length());
       fp.writeInt(bankAccount);
       fp.writeChar(' ');
       fp.writeDouble(balance);
       fp.writeChar(' ');
       fp.writeBytes(firstName);
       fp.writeChar(' ');
       fp.writeBytes(lastName);
       fp.writeChar(' ');
   }
  
   public static void display(RandomAccessFile fp) throws IOException{
       fp.seek(0);
       int curIndex = 0;
       while(fp.length() > curIndex){
           fp.seek(curIndex);
           System.out.println("Account Number: " + fp.readInt());
           fp.seek(getBalanceIndex(curIndex));
           System.out.println("Balance: " + fp.readDouble());
           fp.seek(getFirstNameIndex(curIndex));
           byte[] b = new byte[20];
           fp.readFully(b);
           System.out.print("Firstname: ");
           for(int i = 0; i < 20; ++i){
               System.out.print((char)b[i]);
           }
           fp.seek(getLastNameIndex(curIndex));
           fp.readFully(b);
           System.out.println();
           System.out.print("Lastname: ");
           for(int i = 0; i < 20; ++i){
               System.out.print((char)b[i]);
           }
           System.out.println();
           curIndex = getNextLineIndex(curIndex);
           System.out.println();
       }
       System.out.println();
   }
  
   public static void main(String args[]) throws IOException{
       Scanner in = new Scanner(System.in);
       System.out.print("Enter file name: ");
       String fileName = in.next();
       RandomAccessFile fp = new RandomAccessFile(fileName, "rws");
       while(true){
           System.out.println("Enter an account number(-1 to Exit): ");
           int accountNumber = in.nextInt();
           if(accountNumber == -1){
               break;
           }
           else{
               int index = getIndex(fp, accountNumber);
               System.out.print("Deposit: ");
               double balance = in.nextDouble();
               if(index == -1){
                   appendAccount(fp, accountNumber, balance);
               }
               else{
                   index = getBalanceIndex(index);
                   fp.seek(index);
                   double curBalance = fp.readDouble();
                   fp.seek(index);
                   fp.writeDouble(balance + curBalance);
               }
           }
       }
       display(fp);
       fp.close();
   }
}