Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have a program to do for my advanced Java Class and I am having a hard time with it. I need to add a

I have a program to do for my advanced Java Class and I am having a hard time with it. I need to add a GUI to a bank program. I dont understand GUIs that well and would really appreciate any help getting my bank program started. I need to have four different windows with it. The Instructions are as follows:

You should have a single Transaction window that essentially acts as an ATM. There will be a text field where you type in the account number. (Well ignore your PIN.) Once an account number is entered, the user should be able to press a button, Load Account. At this point, the account number, the type of account (Savings or Checking) and the current balance should be displayed. You may do this using Text or TextArea controls, but make certain it looks nice. If the account is not found, the user should be informed. (Note this may mean you will need to search for both checking and saving accounts, depending on your Bank. The Bank project that is part of this assignment has a findAccountByNum method that will return either type.) Once the account information is displayed, the user should be able to type in an amount and press either a Deposit or Withdraw button. The field to type in the amount and the two buttons should be disabled until the account has been selected; similarly, once an account has been successfully selected, the account number text field and the Load Account button should be disabled. When the deposit or withdrawal is made, the new balance for the account should replace the old one. If the transaction fails, notify the user. The user should be able to perform as many deposits and withdrawals as desired without entering a new account number. At any point after the user has been shown their account they should be able to press a Done button. The application will clear all account specific information, and disable the amount field and the Deposit and Withdraw buttons. The window should then be able to accept a new account number.

Note 1: all of the above can be done in a single window. The only exception is when the user is informed of an error or when the program thanks them for their business; then you may use an Alert window.

Note 2: Enabling and disabling the controls can be tedious, consider doing this in a method.

For the following additions, you must first create a new initial window. Each of the additions that follow will require that you add the specified controls to this initial window. You may do any combination of the additions with the exception that you must do either A1 or A2, but not both.

(A) Worth 2 OR 7 points

You must do one, but only one, of these two options. Note, for both A1 and A2, the initial window should not allow a user to open multiple accounts, i.e. the user should not be able to select more than one account at a time. If an account is already being shown, that window will be raised showing the current account (i.e. the account already being displayed in the Transaction window). If it is not, the window will be brought up with the newly selected account active. There should be only one Transaction window object ever created by the program. That window will be populated as needed.

(A1) Worth 3 points

To the Startup window, add a button that brings up the Transaction window described above. Make certain there is only one instance as described in the above paragraph.

(A2) Worth up to 7 points

To the Startup window add a ComboBox that lists all of the accounts as well as a Show Account button. Once the user has selected an account, the Show Account button should open a separate Transaction window displaying information for the selected account. The window that is opened should be just like the Transaction window described above, with these exceptions: there should be no way to select a new account number (which means the text field and the Load Account button should be removed). Also, the Done button should dismiss the window. Make certain there is only one instance as described in the above paragraph.

(B) Worth up to 9 Points

Add a new button Open Account button to the Startup window to create an account. Pressing this button will display a new Account Creation window that asks for a type (radio button), account number and opening balance. This window should have OK and Cancel buttons. When the user hits OK, the new account will be opened in the Bank and the window will be dismissed; hitting the Cancel button will dismiss the window. Once created (make certain you check whether the account was actually added; display an Alert if it is not) the new account should appear in your ComboBox that shows all of the account numbers. Essentially, you want to reload the array list. Make certain you get the list from the Bank. (Do not save the account number and add it directly.) You should allow the creation of only a single account at one time. If the Create Account window is already showing, raise it to the front so the user can see it.

(C) Worth up to 8 Points

Add a Transfer Funds button to the Startup window that brings up a Transfer window. This button will display a window with a means to select the two and from accounts, a place to enter the amount, and some sort of OK button. If the transfer is unsuccessful, the user will be notified and the window will remain. If it successful, the window will be dismissed. You may allow multiple Transfer windows just as you did Transaction windows. Changes in the accounts affected by the transfer do not need to show up in the Transaction window if it happens to be open, but if that window is closed and reopened, the changes will be seen.

The Program we started with is:

Account Class:

public class Account { static public enum AccountType { CHECKING, SAVINGS } //type of object array list will hold inside arrows private ArrayList transactions = new ArrayList<>(); private AccountType accountType; private double balance = 0; private final String accountNumber;

public double getBalance() { return balance; }

public AccountType getAccountType() { return accountType; }

public String getAccountNumber() { return accountNumber; }

public Account(String accountNumber, double balance, AccountType type) { this.accountNumber = accountNumber; this.balance = balance; this.accountType = type; Transaction o = new Transaction(Transaction.Type.OPEN_ACCNT, balance, balance); transactions.add(o); }

public boolean deposit(double amount) { balance += amount; Transaction t = new Transaction(Transaction.Type.DEPOSIT,amount, balance); transactions.add(t); return true; }

public boolean withdraw(double amount) { if (amount <= balance) { balance -= amount; Transaction w =new Transaction(Transaction.Type.WITHDRAW, amount, balance); transactions.add(w); return true; } else { return false; } } public boolean serviceFee(double amount) { if (balance >= amount) { balance-=amount; Transaction s = new Transaction(Transaction.Type.SERV_FEE, amount, balance); transactions.add(s); } return true; } public List getTransactions() { return Collections.unmodifiableList(transactions); } }

Bank Class:

public class Bank { private static Bank instance = new Bank();

private ArrayList accounts = new ArrayList<>();

/** * Gets the Singleton Bank instance * @return Returns the singleton Bank instance */ public static Bank getInstance() { return instance; }

/** * Open a new savings account and place it in the list of bank accounts. * * @param accntNum the number of the new account * @param initialBal the initial balance * @return Returns true if an account is created; false if the account already exists or the balance is invalid */ public boolean openSavingsAccount(String accntNum, double initialBal) { if (findAccountByNum(accntNum) != null || initialBal < 0) { return false; }

SavingsAccount savings = new SavingsAccount(accntNum, initialBal);

return accounts.add(savings); }

/** * Open a new checking account and place it in the list of bank accounts. * * @param accntNum the number of the new account * @param initialBal the initial balance * @return Returns true if an account is created; false if the account already exists or the balance is invalid */ public boolean openCheckingAccount(String accntNum, double initialBal, double minBalance) { if (findAccountByNum(accntNum) != null || initialBal < 0) { return false; }

CheckingAccount checking = new CheckingAccount(accntNum, initialBal);

return accounts.add(checking); } /** * Finds the account specified by the given account number * @param accntNum the number of the account to be found * @return Returns the account matching the number if found; null if the account is not found */ public Account findAccountByNum(String accntNum) { Account acnt = null; Optional match = accounts.stream().filter(e -> e.getAccountNumber().equals(accntNum)).findFirst(); if (match.isPresent()) { acnt = match.get(); } return acnt; }

/** * Transfers the specified amount from the fromAccount to the toAccount. This method can fail if either * of the account numbers is invalid, or if the fromAccount has insufficient funds to make the transfer. * @param fromAccountNum The account number of the account from which the money is to be withdrawn. * @param toAccountNum The account number of the account to which the money is to be deposited. * @param amount The amount to be transfered. * @return Returns true if the transfer was successful, false otherwise */ public boolean makeTransfer(String fromAccountNum, String toAccountNum, double amount) { Account fromAccnt; Account toAccnt;

fromAccnt = findAccountByNum(fromAccountNum); toAccnt = findAccountByNum(toAccountNum);

if (fromAccnt == null || toAccnt == null) { return false; }

if (fromAccnt.withdraw(amount)) { toAccnt.deposit(amount); return true; } else { return false; } } /** * Pulls all of the account numbers from the accounts and returns them as a list of strings. * @return The list of account numbers. */ public List getAllAccountNumbers() { ArrayList accountNums = new ArrayList<>(); accounts.stream().forEach(e -> accountNums.add(e.getAccountNumber())); return accountNums; } /** * Loads the transactions from the specified comma separated values file. The format of the file is as follows: * O,num,type,amount * D,num,type,amount * W,num,type,amount * T,from,to,amount * @param filePath Path to the file containing the transactions * @throws FileNotFoundException */ public void loadTransactions(String filePath) throws FileNotFoundException { Scanner input; input = new Scanner(new File(filePath));

while (input.hasNext()) { String line = input.nextLine(); // creates an string array called fields and populates each item // splitting by comma. String[] fields = line.split(","); // System.out.println("number of fields: " + fields.length); // first field and first character switch (fields[0].charAt(0)) { case 'O': case 'o': { double minBalance = 0; // open a new account String accntNum = fields[1]; String type = fields[2]; double initialBalance = Double.parseDouble(fields[3]); if (fields.length == 5) { minBalance = Double.parseDouble(fields[4]); }

createAccount(accntNum, type, initialBalance, minBalance); } break; case 'D': case 'd': { // deposit into an account String accntNum = fields[1]; String type = fields[2]; double amount = Double.parseDouble(fields[3]);

Account account = findAccountByNum(accntNum); account.deposit(amount);

} break; case 'W': case 'w': { String accntNum = fields[1]; String type = fields[2]; double amount = Double.parseDouble(fields[3]); Account account = findAccountByNum(accntNum); account.withdraw(amount); } break; case 'T': case 't': { String fromAccount = fields[1]; String toAccount = fields[2]; double amount = Double.parseDouble(fields[3]); makeTransfer(fromAccount, toAccount, amount); } break; default: { System.out.println("Does not meet requirements");

}

} } input.close(); }

private void createAccount(String accntNum, String type, double initialBalance, double minBalance) { switch (type.charAt(0)) { case 's': case 'S': { openSavingsAccount(accntNum, initialBalance); } break;

case 'c': case 'C': { openCheckingAccount(accntNum, initialBalance, minBalance); } break; } } }

CheckingAccount Class:

public class CheckingAccount extends Account { private final double MIN_BALANCE = 100.00; private final double SERV_FEE = .13;

public CheckingAccount(String number, double initialBalance) { super(number, initialBalance,Account.AccountType.CHECKING); }

@Override public boolean withdraw(double amount) { boolean result = super.withdraw(amount); if (result) { if (super.getBalance() < MIN_BALANCE) { super.serviceFee(SERV_FEE); } }

return result; } }

Main Class:

public class MainClass { static Scanner console = new Scanner(System.in);

public void start(Stage primaryStage) throws Exception { BankUI ui = new BankUI(primaryStage); ui.show(); } public static void main(String[] args) throws FileNotFoundException { Bank b = Bank.getInstance(); b.loadTransactions("Transactions.csv"); customerInput(); }

private static void customerInput() { String accountNumber = ""; System.out.println("Welcome to Johnson's Inn Security Bank."); System.out.println("When you have finished using the program, enter quit to exit the program"); Bank bank = Bank.getInstance();

while (!accountNumber.equalsIgnoreCase("quit")) { System.out.println("Please enter your account number to continue."); accountNumber = console.nextLine();

Account account = bank.findAccountByNum(accountNumber);

String action = "";

if (account != null) { System.out.printf("%s account number: %s%n", account.getAccountType() == Account.AccountType.CHECKING ? "Checking" : "Savings", account.getAccountNumber());

System.out.println("(D)eposit/n"); System.out.println("(W)ithdraw/n"); System.out.println("(S)how Balance/n"); action = console.nextLine();

switch (action.charAt(0)) { case 'D': case 'd': {

double amount = 0; System.out.println("Please enter an amount to deposit."); amount = console.nextDouble(); console.nextLine(); account.deposit(amount); } break; case 'W': case 'w': { double amount = 0; System.out.println("Please enter an amount to withdraw"); amount = console.nextDouble(); console.nextLine(); account.withdraw(amount);

} break; case 'S': case 's': { double balance = account.getBalance(); System.out.printf("Your current balance: %.2f ", balance); } break; default: { System.out.println("Please enter a character for the action you wish to perform.");

}

}

}

} } }

SavingsAccount Class:

public class SavingsAccount extends Account { private final int MAX_WITHDRAWALS=7; private int numWithdrawals=0; private final double SERV_FEE=.23; public SavingsAccount(String number, double initialBalance) { super(number, initialBalance, Account.AccountType.SAVINGS); } @Override public boolean withdraw(double amount) { boolean result=false; result=super.withdraw(amount); if(result) { numWithdrawals++; if(numWithdrawals>MAX_WITHDRAWALS) { super.serviceFee(SERV_FEE); } } return result; }

public void resetWithdrawalCount() { numWithdrawals=0; } }

Transaction Class:

public class Transaction { static public enum Type { OPEN_ACCNT, DEPOSIT, WITHDRAW, SERV_FEE } private Type transType; private double transAmount=0; private double transBalance=0; public Transaction (Type type, double amount, double balance) { transType= type; transAmount=amount; transBalance=balance; }

public Type getType() { return transType; }

public double getTransAmount() { return transAmount; } public double getBalance() { return transBalance; } }

Any help getting this started would be great.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Time Series Databases New Ways To Store And Access Data

Authors: Ted Dunning, Ellen Friedman

1st Edition

1491914726, 978-1491914724

More Books

Students also viewed these Databases questions

Question

What is the basis for Security Concerns in Cloud Computing?

Answered: 1 week ago

Question

Describe the three main Cloud Computing Environments.

Answered: 1 week ago