Question
Bank Account Encryption Program - Java Using the BankSimulator, BankAccount and BankData classes, change them so that there is a GUI front end and that
Bank Account Encryption Program - Java
Using the BankSimulator, BankAccount and BankData classes, change them so that there is a GUI front end and that the Account Number and Balance are encrypted and stored. The encryption should be a simple algorithm.
Specifications
1. Account Number label and field
2. Balance label and field
a. Format as a currency field
b. User cannot change.
3. Deposit Amount label and field
4. Deposit Button - Deposits the amount entered
5. Clear Button Clears all fields.
6. Exit Button Terminates the program.
7. Account Number and Balance are encrypted and stored. The encryption should be a simple algorithm.
Error Checking
1) A numeric amount in the Deposit Amount field when the Deposit Button is pressed.
2) try-catch blocks where appropriate.
*******Bank Simulator******
public class BankSimulator { public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); BankData data = new BankData(); try { data.open("bank.dat");
boolean done = false; while (!done) { System.out.print("Account number: "); int accountNumber = in.nextInt(); System.out.print("Amount to deposit: "); double amount = in.nextDouble();
int position = data.find(accountNumber); BankAccount account; if (position >= 0) { account = data.read(position); account.deposit(amount); System.out.println("New balance: " + account.getBalance()); } else // Add account { account = new BankAccount(accountNumber, amount); position = data.size(); System.out.println("Adding new account."); } data.write(position, account);
System.out.print("Done? (Y/N) "); String input = in.next(); if (input.equalsIgnoreCase("Y")) { done = true; } } } finally { data.close(); } System.out.println(" Thank you for using the Bank Simulator. Good Bye."); } }
*******BankData*******
public class BankData { private RandomAccessFile file;
public static final int INT_SIZE = 4; public static final int DOUBLE_SIZE = 8; public static final int RECORD_SIZE = INT_SIZE + DOUBLE_SIZE;
/** Constructs a BankData object that is not associated with a file. */ public BankData() { file = null; }
/** Opens the data file. @param filename the name of the file containing bank account information */ public void open(String filename) throws IOException { if (file != null) { file.close(); } file = new RandomAccessFile(filename, "rw"); }
/** Gets the number of accounts in the file. @return the number of accounts */ public int size() throws IOException { return (int) (file.length() / RECORD_SIZE); }
/** Closes the data file. */ public void close() throws IOException { if (file != null) { file.close(); } file = null; }
/** Reads a bank account record. @param n the index of the account in the data file @return a bank account object initialized with the file data */ public BankAccount read(int n) throws IOException { file.seek(n * RECORD_SIZE); int accountNumber = file.readInt(); double balance = file.readDouble(); return new BankAccount(accountNumber, balance); }
/** Finds the position of a bank account with a given number. @param accountNumber the number to find @return the position of the account with the given number, or -1 if there is no such account */ public int find(int accountNumber) throws IOException { for (int i = 0; i < size(); i++) { file.seek(i * RECORD_SIZE); int a = file.readInt(); if (a == accountNumber) { return i; } // Found a match } return -1; // No match in the entire file }
/** Writes a bank account record to the data file. @param n the index of the account in the data file @param account the account to write */ public void write(int n, BankAccount account) throws IOException { file.seek(n * RECORD_SIZE); file.writeInt(account.getAccountNumber()); file.writeDouble(account.getBalance()); } }
**********BankAccount***********
public class BankAccount { private int accountNumber; private double balance;
/** Constructs a bank account with a zero balance. @param anAccountNumber the account number for this account */ public BankAccount(int anAccountNumber) { accountNumber = anAccountNumber; balance = 0; }
/** Constructs a bank account with a given balance. @param anAccountNumber the account number for this account @param initialBalance the initial balance */ public BankAccount(int anAccountNumber, double initialBalance) { accountNumber = anAccountNumber; balance = initialBalance; }
/** Gets the account number of this bank account. @return the account number */ public int getAccountNumber() { return accountNumber; }
/** Deposits money into the bank account. @param amount the amount to deposit */ public void deposit(double amount) { double newBalance = balance + amount; balance = newBalance; }
/** Withdraws money from the bank account. @param amount the amount to withdraw */ public void withdraw(double amount) { double newBalance = balance - amount; balance = newBalance; }
/** Gets the current balance of the bank account. @return the current balance */ public double getBalance() { return balance; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started