@Override
 public void run() {
   for (; ; ) { // Forever
     bank.transfer(Util.random(-10, 10)); //   transfer money
     Util.pause(200, 300); //   then take a break
   }
 }
  /** Prompts user for info, then carry out the transfer operation in Bank. */
  private static void transfer() {
    String prompt = "Please enter the account number to transfer FROM: ";
    int transferFromAccount = (int) getChoice(prompt);

    prompt = "Please enter the account number to transfer TO: ";
    int transferToAccount = (int) getChoice(prompt);

    prompt = "Please enter the amount to transfer: ";
    double amount = getChoice(prompt);
    ;

    try {

      theBank.transfer(transferFromAccount, transferToAccount, amount);
      System.out.println("\nTransfer successful.");
      System.out.printf(
          "Balance of account #: %d is %.2f\n",
          transferFromAccount, theBank.getBalanceForAccount(transferFromAccount));
      System.out.printf(
          "Balance of account #: %d is %.2f\n\n",
          transferToAccount, theBank.getBalanceForAccount(transferToAccount));

    } catch (AccountDoesNotExistException e) {
      System.out.println("\nOne of the accounts doesn't exist in this bank.");
    } catch (NegativeMoneyException e) {
      System.out.println("\nAmount to transfer has to be >= 0");
    } catch (InsufficientFundsException e) {
      System.out.println("\nInsufficient funds in this account to transfer the given amount.");
    } catch (NotSameOwnerException e) {
      System.out.println("\nThese accounts don't belong to the same owner.");
    }
  }
Exemplo n.º 3
0
 public static void main(String[] args) {
   Bank bank = new Bank(NACCOUNTS, INITIAL_BALANCE);
   for (int i = 0; i < NACCOUNTS; i++) {
     int fromAccount = i;
     Runnable r =
         () -> {
           try {
             while (true) {
               int toAccount = (int) (bank.size() * Math.random());
               double amount = MAX_AMOUNT * Math.random();
               bank.transfer(fromAccount, toAccount, amount);
               Thread.sleep((int) (DELAY * Math.random()));
             }
           } catch (InterruptedException e) {
           }
         };
     Thread t = new Thread(r);
     t.start();
   }
 }