/**
   * Function in charge of getting currency value. Should be formatted as: user (currency) if
   * currency is not given, uses default currency.
   */
  private String checkCurrency(ArrayList<String> cmd) {
    String curr = null;
    String user;

    if (cmd.size() >= 3) user = cmd.get(2);
    else return "Invalid user";

    if (cmd.size() >= 4) curr = cmd.get(3);

    if (bot.getUserBase().getUser(user) == null) return "User does not exist in database.";

    if (curr == null) return bank.getValue(user).toString();

    return bank.getValue(user, curr).toString();
  }
  public void addAccount() {
    AddAccountDialog addaccount = new AddAccountDialog(this, "Add Account");

    Point loc = getLocation();
    addaccount.setLocation(loc.x + 50, loc.y + 50);
    addaccount.setModal(true);
    addaccount.setVisible(true);

    if (!addaccount.canceled()) {
      String number = null;
      try {
        number = bank.createAccount(addaccount.getOwnerName());
      } catch (Exception e) {
        error(e);
      }

      if (number == null) {
        JOptionPane.showMessageDialog(
            this, "Account could not be created", "Error", JOptionPane.ERROR_MESSAGE);
      } else {
        try {
          Account acc = bank.getAccount(number);
          accounts.put(number, acc);

          String str = addaccount.getBalance().trim();
          double amount;
          if (str.equals("")) amount = 0;
          else amount = Double.parseDouble(str);
          acc.deposit(amount);
        } catch (NumberFormatException e) {
          JOptionPane.showMessageDialog(this, "Illegal Format", "Error", JOptionPane.ERROR_MESSAGE);
        } catch (IllegalArgumentException e) {
          JOptionPane.showMessageDialog(
              this, "Illegal Argument", "Error", JOptionPane.ERROR_MESSAGE);
        } catch (InactiveException e) {
          JOptionPane.showMessageDialog(
              this, "Account is inactive", "Error", JOptionPane.ERROR_MESSAGE);
        } catch (Exception e) {
          error(e);
        }
        ignoreItemChanges = true;
        accountcombo.addItem(number);
        accountcombo.setSelectedItem(number);
        ignoreItemChanges = false;
        refreshDialog();
      }
    }
  }
 public void closeAccount() {
   String number = currentAccountNumber();
   if (number != null) {
     int res =
         JOptionPane.showConfirmDialog(
             this,
             "Really close account " + number + "?",
             "Confirm closing",
             JOptionPane.YES_NO_OPTION,
             JOptionPane.QUESTION_MESSAGE);
     if (res == 0) {
       try {
         boolean done = bank.closeAccount(number);
         if (done) {
           refreshDialog();
         } else {
           JOptionPane.showMessageDialog(
               this, "Account could not be closed", "Error", JOptionPane.ERROR_MESSAGE);
         }
       } catch (Exception e) {
         error(e);
       }
     }
   }
 }
Example #4
0
  @Override
  public void run() {

    while (!Thread.currentThread().isInterrupted()) {
      transactionCount++;
      int randomAmount = random.nextInt(1000);
      boolean randomDirection = random.nextBoolean();
      bank.transaction(randomAmount, randomDirection);
    }
  }
  private void refreshDialog() {
    String nr = currentAccountNumber();
    accountcombo.removeAllItems();
    if (bank != null) {
      try {
        Set<String> s = bank.getAccountNumbers();
        ArrayList<String> accnumbers = new ArrayList<String>(s);
        Collections.sort(accnumbers);
        ignoreItemChanges = true;
        for (String item : accnumbers) {
          accountcombo.addItem(item);
          if (item.equals(nr)) accountcombo.setSelectedItem(item);
        }
        ignoreItemChanges = false;

        // clean up local accounts map
        for (String key : s) {
          if (!accounts.containsKey(key)) {
            accounts.put(key, bank.getAccount(key));
          }
        }
        Iterator<String> it = accounts.keySet().iterator();
        while (it.hasNext()) {
          if (!s.contains(it.next())) it.remove();
        }

        int size = s.size();
        btn_deposit.setEnabled(size > 0);
        btn_withdraw.setEnabled(size > 0);
        btn_transfer.setEnabled(size > 1);
        item_close.setEnabled(size > 0);

        for (BankTest t : tests) {
          JMenuItem m = testMenuItems.get(t);
          m.setEnabled(t.isEnabled(size));
        }

        updateCustomerInfo();
      } catch (Exception e) {
        error(e);
      }
    }
  }
  public void transfer() {
    String number = currentAccountNumber();
    if (number != null) {
      try {
        Set<String> s = new HashSet<String>(accounts.keySet());
        s.remove(number);

        TransferDialog trans = new TransferDialog(this, "Transfer Money", number, s);
        Point loc = getLocation();
        trans.setLocation(loc.x + 50, loc.y + 50);
        trans.setModal(true);
        trans.setVisible(true);

        if (!trans.canceled()) {
          if (number.equals(trans.getAccountNumber())) {
            JOptionPane.showMessageDialog(
                this, "Both Accounts are the same!", "Error", JOptionPane.ERROR_MESSAGE);
          } else {
            try {
              double amount = Double.parseDouble(trans.getBalance());
              Account from = accounts.get(number);
              Account to = accounts.get(trans.getAccountNumber());
              bank.transfer(from, to, amount);

              // after transfer adjust value of displayed account
              fld_balance.setText(currencyFormat(from.getBalance()));

              JOptionPane.showMessageDialog(
                  this, "Transfer successfull", "Information", JOptionPane.INFORMATION_MESSAGE);
            } catch (NumberFormatException e) {
              JOptionPane.showMessageDialog(
                  this, "Illegal Balance", "Error", JOptionPane.ERROR_MESSAGE);
            } catch (IllegalArgumentException e) {
              JOptionPane.showMessageDialog(
                  this, "Illegal Argument", "Error", JOptionPane.ERROR_MESSAGE);
            } catch (InactiveException e) {
              JOptionPane.showMessageDialog(
                  this, "At least one account is inactive", "Error", JOptionPane.ERROR_MESSAGE);
            } catch (OverdrawException e) {
              JOptionPane.showMessageDialog(
                  this, "Account must not be overdrawn", "Error", JOptionPane.ERROR_MESSAGE);
            }
          }
        }
      } catch (Exception e) {
        error(e);
      }
    }
  }
Example #7
0
 /**
  * Fuehrt eine Ueberweiung aus. Die Ueberweisung ist nur moeglich, wenn sie durch das
  * Ausgangskonto (quelle) gedeckt ist. Es ist nicht erlaubt, negative Betraege anzugeben.
  *
  * @param quelle Kontonummer von der aus ueberwiesen wird
  * @param zielBank Bank zu der ueberwiesen wird
  * @param zielKonto Kontonummer zu der ueberwiesen wird
  * @param betrag Geldbetrag
  * @throws IllegalArgumentException bei falscher Eingabe
  */
 public void ueberweisen(int quelle, Bank zielBank, int zielKonto, double betrag) {
   getKonto(quelle).ueberweisen(betrag, zielBank.getKonto(zielKonto));
 }
 /**
  * Helper function to call in order to get
  *
  * @param cmd
  * @return
  */
 private String showAvailableOptions(ArrayList<String> cmd) {
   return "Currently Available Currencies are: " + bank.getConversion().toString();
 }