public ServerSolution() {
    accountMap = new HashMap<String, Account>();
    File file = new File(fileName);
    ObjectInputStream in = null;
    try {
      if (file.exists()) {
        System.out.println("Reading from file " + fileName + "...");
        in = new ObjectInputStream(new FileInputStream(file));

        Integer sizeI = (Integer) in.readObject();
        int size = sizeI.intValue();
        for (int i = 0; i < size; i++) {
          Account acc = (Account) in.readObject();
          if (acc != null) accountMap.put(acc.getName(), acc);
        }
      }
    } catch (Exception e) {
      System.out.println(e.getMessage());
      e.printStackTrace();
    } finally {
      if (in != null) {
        try {
          in.close();
        } catch (Throwable t) {
          t.printStackTrace();
        }
      }
    }
  }
Beispiel #2
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);
    }
  }
  private boolean newAccountFactory(String type, String name, float balance)
      throws IllegalArgumentException {

    Account acc;
    if ("Checking".equals(type)) {
      acc = new Checking(name, balance);

    } else if ("Savings".equals(type)) {
      acc = new Savings(name, balance);

    } else {
      throw new IllegalArgumentException("Bad account type:" + type);
    }
    try {
      accountMap.put(acc.getName(), acc);
    } catch (Exception exc) {
      return false;
    }
    return true;
  }