Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* This program allows the user to create an account (provided the
- bank has space), deposit money in the account, and
- withdraw money from the account. The bank has only space for
- five accounts, and each account holder's name must be unique.
- */
- import javax.swing.*; // Imported to use JOptionPane
- import java.io.*;
- import java.util.*;
- public class SimpleAccountTeller
- {
- public static boolean hasCustomer(SimpleAccount[] bank, String bap)
- {
- boolean customerExists = false;// No customer exists with given name.
- for(int index = 0; index < bank.length; index++)
- {
- if(bank[index]!= null && bap.equalsIgnoreCase(bank[index].getOwner()))
- {
- customerExists = true;
- }
- }
- return customerExists; // Return user's status.
- }
- /* This method returns true if the bank is at full capacity
- and false if the banl is not full and new customers can be added.
- */
- public static boolean bankIsFull(SimpleAccount[] bank)
- {
- boolean full = true;
- // Searches the bank for any available space in the form of
- // null values.
- for(SimpleAccount bap: bank)
- {
- if(bap == null)
- {
- full = false;
- }
- }
- return full;// Returns the true/false status of bank's capacity.
- }
- /* This method adds customers to the bank if there is space
- and if the customer does not already exist.
- */
- public static void addCustomer(SimpleAccount[] bank, SimpleAccount newCus)
- {
- for(int index = 0; index < bank.length; index++)
- {
- if(bank[index]== null && !hasCustomer(bank, newCus.getOwner()))
- {
- bank[index] = newCus;
- }
- }
- }
- /* This method returns the index of a customer's account
- in the bank array.
- */
- public static int getCustomerIndex(SimpleAccount[] bank, String bap)
- {
- int iCustomer = 0; // Initializes customer index to 0.
- // Searches the entire array, finds the location of the bank account,
- // and resets the customer's acount index to the found index.
- for(int index = 0; index < bank.length; index++)
- {
- if(bank[index]!= null && bap.equalsIgnoreCase(bank[index].getOwner()))
- {
- iCustomer = index;
- }
- }
- return iCustomer;// Returns the customer's account index
- }
- // Saves customer data in customers array to file.
- public static void save(SimpleAccount[] bank, String givenFile)throws IOException
- {
- String file = givenFile;
- FileWriter outfile = new FileWriter(file);
- PrintWriter out = new PrintWriter(outfile);
- for(SimpleAccount a : bank)
- {
- if(a != null)
- {
- out.println(a.getOwner() + "," + a.getBalance());
- }
- }
- out.close();
- }
- /* Reads customer data from file to start bank session and returns array of
- BankAccountP... objects */
- public static SimpleAccount[] startUp(SimpleAccount[] bank,
- String givenFile)throws IOException
- {
- String file = givenFile;
- String content = "", givenName = "";
- double givenDeposit = 0;
- int space = -1; // index of space between owner name and balance
- SimpleAccount[] newBank = new SimpleAccount[bank.length];
- // Catch IO errors when opening file
- try
- {
- // if the file is found do this block of code
- FileReader freader = new FileReader(file);
- Scanner fileScanner = new Scanner(freader);
- int i =0;
- // Create array of BankAccountP... objects from file data
- // (What if there is no file?)
- while(fileScanner.hasNext() && i < bank.length)
- {
- content = fileScanner.nextLine();
- space = content.indexOf(",");
- givenName = content.substring(0, space);
- givenDeposit = Double.parseDouble(content.substring(space+1));
- SimpleAccount b = new SimpleAccount(givenName, givenDeposit);
- newBank[i] = b;
- i++;
- }
- }
- catch(IOException e)
- {
- JOptionPane.showMessageDialog(null, "Bank Now Open");
- }
- return newBank;
- }
- // main method: Creates Bank (which = array of customers)
- public static void main(String[] args)
- {
- final int BANK_SIZE = 5; // Maximum bank size
- SimpleAccount[] customers
- = new SimpleAccount[BANK_SIZE]; //Initializes maximum bank
- //size to 5 customers
- String response = ""; // Holds the user's input
- String name = ""; // Holds the user's name
- double iDeposit = 0.0; // Holds user's deposit amount
- double iWithdraw = 0.0; // Holds customer's withdraw amount
- int iTemp = 0; // Holds index of customer's account
- String menu = "Press o to open an account.\n" +
- "Press d to deposit.\n" +
- "Press w to withdraw.\n" +
- "Press q to exit.\n"; // The menu display
- //Shows the menu and takes in input as long as the user does not quit.
- do
- {
- for(int j = 0; j < customers.length; j++)
- {
- try
- {
- customers[j] = startUp(customers, "CustomerData.txt")[j];
- }
- catch(IOException e)
- {
- // Do nothing, just continue with program
- }
- }
- response = JOptionPane.showInputDialog(menu);
- // If the user chooses to open an account
- if(response.equalsIgnoreCase("O"))
- {
- // If the bank is already full
- if(bankIsFull(customers))
- {
- JOptionPane.showMessageDialog(null,"Unfortunately, the bank has already\n"
- + " reached the maximum number of accounts allowed.");
- }
- // Else, if the bank has available account space
- else
- {
- // Gets the user's name and intial deposit amount
- name = JOptionPane.showInputDialog("Please input your name:");
- iDeposit = Double.parseDouble(JOptionPane.showInputDialog
- ("Initial Deposit:"));
- // If the deposit is positive and the customer does not
- // already have an account
- if(iDeposit > 0 && !hasCustomer(customers, name))
- {
- // Creates a new bank account,
- // shows the customer's account status,
- // and adds the account to the bank.
- SimpleAccount nAccount = new SimpleAccount(name, iDeposit);
- JOptionPane.showMessageDialog(null, nAccount);
- addCustomer(customers, nAccount);
- }
- // If the deposit is 0 and
- else if (iDeposit == 0 && !hasCustomer(customers, name))
- {
- SimpleAccount nAccount = new SimpleAccount(name);
- addCustomer(customers, nAccount);
- }
- else
- {
- JOptionPane.showMessageDialog(null, "Cannot deposit: "
- + "Negative deposit or no account!");
- }
- }
- }// End of 'O'
- // If user chooses to make a deposit
- else if(response.equalsIgnoreCase("D"))
- {
- // Gets the user's name
- name = JOptionPane.showInputDialog("Please input your name:");
- // If the user is a customer
- if(hasCustomer(customers, name))
- {
- // asks how much the user wants to deposit.
- iDeposit = Double.parseDouble(JOptionPane.showInputDialog("How much " +
- "would you like to deposit?"));
- //If the deposit is greater than 0.
- if(iDeposit > 0)
- {
- // adjusts the customer's account and displays the status.
- iTemp = getCustomerIndex(customers, name);
- customers[iTemp].deposit(iDeposit); customers[iTemp].incrementDeposits();
- JOptionPane.showMessageDialog(null, customers[iTemp]);
- }
- // If the deposit is not greater than 0
- else
- {
- JOptionPane.showMessageDialog(null, "Cannot deposit.");
- }
- }
- // If the user in not a customer
- else
- {
- String noAccount = "Error! You do not have an account with us.\n" +
- " Press ok to return to the menu and open an account.";
- JOptionPane.showMessageDialog(null, noAccount);
- }
- }
- // If the user chooses to withdraw money from an account
- else if(response.equalsIgnoreCase("W"))
- {
- // gets the user's name.
- name = JOptionPane.showInputDialog("Please input your name:");
- // If the user is a customer
- if(hasCustomer(customers, name))
- {
- // gets withdraw amount,
- iWithdraw = Double.parseDouble(JOptionPane.showInputDialog("How much "
- + "would you like to withdraw?"));
- iTemp = getCustomerIndex(customers, name);
- // checks the withdraw amount, adjusts the customer's account.
- if(iWithdraw >= 0 && iWithdraw <= customers[iTemp].getBalance())
- {
- customers[iTemp].withdraw(iWithdraw); customers[iTemp].incrementWithdrawals();
- JOptionPane.showMessageDialog(null, customers[iTemp]);
- }
- // if an overdraft will occur or the withdraw amount is negative
- else
- {
- JOptionPane.showMessageDialog(null, "Overdraft/Negative Withdraw Warning!\n" +
- "Cannot withdraw amount.");
- }
- }
- // If the user does not have an account with the bank
- else
- {
- String noAccount = "Error! You do not have an account with us.\n" +
- " Press ok to return to the menu and open an account.";
- JOptionPane.showMessageDialog(null, noAccount);
- }
- }
- }
- while(!response.equalsIgnoreCase("Q"));// Repeat cycle until the user quits.
- // Save the data from this session to a text file
- try
- {
- save(customers, "CustomerData.txt");
- }
- catch (IOException e)
- {
- JOptionPane.showMessageDialog(null, "File not Found.");
- }
- System.exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment