Пример #1
0
  public void withdraw(String withdrawAmount, String accountNum) {
    // actually performs the withdrawal service
    double withdrawal = Double.parseDouble(withdrawAmount);
    boolean accountFound = customer.removeMoney(withdrawal, accountNum);
    if (accountFound == true) {
      // creates the log file
      double logAmount = -withdrawal;
      timestamp = date.getTime();
      String customerID = customer.returnID();
      AdminLog newTransaction =
          new AdminLog(timestamp, transaction_counter, customerID, accountNum, logAmount);
      adminLogs.add(newTransaction);

      transaction_counter++;
      add_interest();
      String infoMessage = customer.builderToString();
      jf.displayInfo(infoMessage);

      try {
        saveFile(cust, DBfile);
        saveFile(adminLogs, logFile);
      } catch (IOException e) {
        e.printStackTrace();
      }
    } else {
      jf.errorMessage(
          "We cannot find that account number in our database"); // pops up an error message if the
                                                                 // account was not found
    }
    customer = null; // resets the customer and account after each transaction
    pin = null;
  }
Пример #2
0
  public void printAllAccounts(String customerID) {
    // Sorts the customer based on ID, then prints the accounts information

    // change the withdrawal funcitons and deposit functions too
    ajf.dispose();
    ajf = new AdminRunningFrame(this);

    Collections.sort(cust, Customer.CompareIDs);
    String searchString = ajf.getCustomerID();

    StringBuilder stringResults = new StringBuilder();
    String header = header();
    stringResults.append(header);
    for (Customer customer : cust) {
      String id = customer.returnID();
      String name = customer.getName().toUpperCase();
      String pin = customer.returnPin();
      if (searchString.equals(id)) {
        ArrayList<Account> accounts = customer.getAccounts();
        if (!accounts.isEmpty()) {
          for (Account account : accounts) {
            if (account.checkActive()) {
              String accountNumber = account.returnNumber();
              double balance = account.returnBalance();
              String balanceAsString = formatter.format(balance);
              String customerInfo = printAdminInfo(name, id, accountNumber, pin, balanceAsString);
              stringResults.append(customerInfo);
            }
          }
        }
      }
    }
    String resultsAsString = stringResults.toString();
    ajf.printInfo(resultsAsString);
  }
Пример #3
0
  public void deposit(String accountNum, String depositAmount) {
    // actually deposits the appropriate amount into the account

    double deposit =
        Double.parseDouble(
            depositAmount); // the balance in the Account class is saved as a double so it must be
                            // converted here
    boolean accountFound = customer.addMoney(deposit, accountNum);

    if (accountFound == true) {
      // create the log file
      timestamp = date.getTime();
      String customerID = customer.returnID();
      AdminLog newTransaction =
          new AdminLog(timestamp, transaction_counter, customerID, accountNum, deposit);
      adminLogs.add(newTransaction);

      transaction_counter++;
      add_interest();
      String infoMessage = customer.builderToString();
      jf.displayInfo(infoMessage);
      try {
        saveFile(cust, DBfile);
        saveFile(adminLogs, logFile);
      } catch (IOException e) {
        e.printStackTrace();
      }
    } else {
      jf.errorMessage(
          "We cannot find that account number in our database"); // pops up an error message if the
                                                                 // account was not found
    }
    customer = null; // resets the customer and account after each transaction
    pin = null;
  }
Пример #4
0
  public void printHighestBalance() {
    // prints the accounts from highest amount to lowest, with those who have empty accounts last
    ajf.dispose();
    ajf = new AdminRunningFrame(this);

    StringBuilder stringResults = new StringBuilder();
    String header = header();
    stringResults.append(header);

    if (!cust.isEmpty()) {
      ArrayList<Account> allAccounts = new ArrayList<Account>();
      ArrayList<Customer> CustomersNoAccounts = new ArrayList<Customer>();
      for (Customer c : cust) {
        ArrayList<Account> accounts = c.getAccounts();
        if (!accounts.isEmpty()) {
          // Concatenates all the accounts together for easy sorting
          allAccounts.addAll(accounts);
        }
        // Adds customers without accounts to a separate list to be printed at the end of all the
        // others
        else {
          CustomersNoAccounts.add(c);
        }
      }
      if (!allAccounts.isEmpty()) {
        Collections.sort(allAccounts, Account.CompareBalances);
        for (Account a : allAccounts) {
          if (a.checkActive()) {
            String id = a.getID();
            String name = a.getName().toUpperCase();
            String pin = a.getPin();
            String accountNumber = a.returnNumber();
            double balance = a.returnBalance();
            String balanceAsString = formatter.format(balance);
            String customerInfo = printAdminInfo(name, id, accountNumber, pin, balanceAsString);
            stringResults.append(customerInfo);
          }
        }
      }
      if (!CustomersNoAccounts.isEmpty()) {
        Collections.sort(CustomersNoAccounts, Customer.CompareName);
        for (Customer c : CustomersNoAccounts) {
          String id = c.returnID();
          String name = c.getName().toUpperCase();
          String pin = c.returnPin();
          String noAccounts = String.format("%-20s %-9s %-10s\n", name, id, pin);
          stringResults.append(noAccounts);
        }
      }
      String resultsAsString = stringResults.toString();
      ajf.printInfo(resultsAsString);
    }
  }
Пример #5
0
 public void model_printAllAccounts() {
   ArrayList<String> cusIDs = new ArrayList<String>(100);
   cusIDs.add("Accounts:");
   Collections.sort(cust, Customer.CompareIDs);
   for (Customer c : cust) {
     String id = c.returnID();
     cusIDs.add(id);
   }
   if (ajf != null) {
     ajf.dispose();
   }
   ajf = new AdminRunningFrame(this);
   ajf.showOneCustomerScreen(cusIDs);
 }
Пример #6
0
 public void create_account(String name, String newPin) {
   // Actually creates the customer account
   Customer customer = new Customer(starting_customer_number, newPin, name);
   cust.add(customer);
   String newCusID = customer.returnID();
   String idString = String.format("Your new Customer ID is: %s\n", newCusID);
   transaction_counter++;
   add_interest();
   try {
     saveFile(cust, DBfile);
   } catch (IOException e) {
     e.printStackTrace();
   }
   jf.displayInfo(idString);
 }
Пример #7
0
  public void transfer(String transferAmount, String accountFrom, String accountTo) {
    // actually transfers the model
    double transferDouble = Double.parseDouble(transferAmount);
    boolean accountFromFound = customer.removeMoney(transferDouble, accountFrom);
    boolean accountToFound = customer.addMoney(transferDouble, accountTo);
    if (accountFromFound == true && accountToFound == true) {
      // creates the log file
      double logAmount = -transferDouble;
      timestamp = date.getTime();
      String customerID = customer.returnID();

      AdminLog firstTransaction =
          new AdminLog(timestamp, transaction_counter, customerID, accountFrom, transferDouble);

      timestamp = date.getTime();
      AdminLog secondTransaction =
          new AdminLog(timestamp, transaction_counter, customerID, accountTo, logAmount);
      adminLogs.add(firstTransaction);
      adminLogs.add(secondTransaction);

      for (AdminLog a : adminLogs) {
        long timestampe = a.getTimestamp();
      }

      transaction_counter++;
      add_interest();
      String infoMessage = customer.transferMessage(accountFrom, accountTo, transferDouble);
      jf.displayInfo(infoMessage);
      try {
        saveFile(cust, DBfile);
        saveFile(adminLogs, logFile);
      } catch (IOException e) {
        e.printStackTrace();
      }
    } else {
      jf.errorMessage(
          "We cannot find that account number in our database"); // pops up an error message if the
                                                                 // account was not found
    }
    customer = null; // resets the customer and account after each transaction
    pin = null;
  }
Пример #8
0
  public void printByCustomerName() {
    // Prints the database info by customer's name in ascending order
    Collections.sort(cust, Customer.CompareName);
    ajf.dispose();
    ajf = new AdminRunningFrame(this);

    StringBuilder stringResults = new StringBuilder();
    String header = header();
    stringResults.append(header);

    if (!cust.isEmpty()) {
      for (Customer customer : cust) {
        String id = customer.returnID();
        String name = customer.getName().toUpperCase();
        String pin = customer.returnPin();
        ArrayList<Account> accounts = customer.getAccounts();
        if (!accounts.isEmpty()) {
          for (Account account : accounts) {
            if (account.checkActive()) {
              String accountNumber = account.returnNumber();
              double balance = account.returnBalance();
              String balanceAsString = formatter.format(balance);
              String customerInfo = printAdminInfo(name, id, accountNumber, pin, balanceAsString);
              stringResults.append(customerInfo);
            }
          }

        } else {
          // Still prints customers who have created customer accounts but not set up any
          // checking/savings
          String noAccounts = String.format("%-20s %-9s %-10s\n", name, id, pin);
          stringResults.append(noAccounts);
        }
      }
    }
    String resultsAsString = stringResults.toString();
    ajf.printInfo(resultsAsString);
  }
Пример #9
0
 public void add_interest() {
   if ((transaction_counter % 5) == 0) {
     for (Customer c : cust) {
       ArrayList<Tuple> accountsAddedTo = new ArrayList<>(100);
       accountsAddedTo = c.addInterestToSavings(interest_rate);
       timestamp = date.getTime();
       if (!accountsAddedTo.isEmpty()) {
         String customerID = c.returnID();
         for (Tuple tuple : accountsAddedTo) {
           String accountNumber = tuple.getFirst();
           double logAmount = tuple.getSecond();
           AdminLog newTransaction =
               new AdminLog(timestamp, transaction_counter, customerID, accountNumber, logAmount);
           adminLogs.add(newTransaction);
         }
       }
       try {
         saveFile(cust, DBfile);
       } catch (IOException e) {
         e.printStackTrace();
       }
     }
   }
 }
Пример #10
0
  private void validate_info(String newUserID, String newPin) {
    // validates that the customer has put in the correct Customer name and PIN
    if (newPin == null || newPin.length() != 4) {
      // returns the Customer object right away if the pin number is wrong, which will cause
      // the functions defined below to just pass through without doing anything
      pin = newPin;
    } else {
      // Validate user information
      for (Customer c : cust) {
        // Checks to see if the customer is already in the database
        String possibleCustID = c.returnID();

        if (possibleCustID.equals(newUserID)) {
          found = true;
          String possibleCustPIN = c.returnPin(); // Sets possible customer PIN for validation
          if (possibleCustPIN.equals(newPin)) {
            customer = c;
            pin = newPin;
          }
          pin =
              newPin; // so if customer ends up being null but the pin still has value, then the pin
                      // was incorrect
        }
      }
    }

    if (!found) { // calls the correct warning message if  either the customer or pin is not correct
      if (pin == null) {
        jf.errorMessage("That customer is not in our database.");
      } else {
        jf.errorMessage(
            "Invalid PIN number."); // if the customer has not been found but the pin has been set
                                    // that means the pin was invalid
      }
    }
  }
Пример #11
0
  public Atm() // constructor
        // sets the customer array to that found in the file if the file exists
      {
    try {
      // Set cross-platform Java L&F (also called "Metal")
      UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
    } catch (UnsupportedLookAndFeelException e) {
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } catch (InstantiationException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    }

    if (logFile.exists()) {
      try {
        adminLogs = returnLog("p2.log");
        if (!adminLogs.isEmpty()) {
          // there may be customer data but perhaps no transactions yet.
          ArrayList<Integer> allTransactions = new ArrayList<>(100);
          for (AdminLog a : adminLogs) {

            int transaction = a.getTransactionID();
            ;
            allTransactions.add(transaction);
          }
          transaction_counter = Collections.max(allTransactions) + 1;
        }
      } catch (ClassNotFoundException | IOException e) {
        e.printStackTrace();
      }
    }

    if (DBfile.exists()) {
      try {

        cust = returnSavedData("p2.dat");

        // sets the starting ID to the max of all the IDs stored in the file.
        allIDs = new ArrayList<Integer>(100);
        ArrayList<Integer> allAccounts = new ArrayList<>();
        for (Customer customer : cust) {
          String idString = customer.returnID();
          int id = Integer.parseInt(idString);
          allIDs.add(id);
          int accountNumber = customer.returnMaxAccount();
          allAccounts.add(accountNumber);
        }
        // checks to see if the customer has even made any accounts, if not, max accounts will be
        // set at 1001
        if (starting_account_number != 1) {
          starting_account_number = Collections.max(allAccounts) + 1;
        }
        starting_customer_number = Collections.max(allIDs) + 1;

      } catch (ClassNotFoundException | IOException e) {
        e.printStackTrace();
      }
    } else {
      cust = new ArrayList<>(100);
      // creates the file if it does not exist
      try {
        saveFile(cust, DBfile);
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    interest_rate = 5;
  }