elltyl325

BankAccount

Sep 19th, 2020
1,945
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package threads;
  2.  
  3. import java.util.logging.Level;
  4. import java.util.logging.Logger;
  5.  
  6. /**
  7.  * A class representing a bank account user.
  8.  * @author tcolburn
  9.  */
  10. public class BankAccountUser extends Thread {
  11.  
  12.     /**
  13.      * Creates a new bank account user.
  14.      * @param name the user's name
  15.      * @param account the account used
  16.      * @param transactions an array of integers representing deposits (positive)
  17.      * and withdrawals (negative)
  18.      */
  19.     public BankAccountUser(String name, BankAccount account, int[] transactions) {
  20.         super(name);
  21.         this.waiting = false;
  22.         this.account = account;
  23.         if(transactions == null){
  24.             transactions[0] = 0;
  25.         }
  26.         this.transactions = transactions;
  27.         transactionsRemaining = transactions.length;
  28.     }
  29.  
  30.  
  31.     public void changeWaitingStatus(boolean bool){
  32.         waiting = bool;
  33.     }
  34.     /**
  35.      * Getter for the number of transactions remaining for this user.
  36.      * @return the number of transactions remaining
  37.      */
  38.     public int getTransactionsRemaining() {
  39.         return transactionsRemaining;
  40.     }
  41.    
  42.   /*  public BankAccount getAccount(){
  43.         return account;
  44.     }
  45.    
  46.     /**
  47.      * Runs the transactions in a loop.
  48.      * When finished, a message is logged.
  49.      */
  50.     public void run() {
  51.        
  52.         for (int amount : transactions) {
  53.             if (amount > 0) {
  54.                 account.deposit(amount, this);
  55.             }
  56.             else if (amount < 0) {
  57.                 try {
  58.                     account.withdraw(Math.abs(amount), this);
  59.                 } catch (InterruptedException ex) {
  60.                     Logger.getLogger(BankAccountUser.class.getName()).log(Level.SEVERE, null, ex);
  61.                 }
  62.             }
  63.             else {
  64.                 // amount will not be zero
  65.             }
  66.             transactionsRemaining--;
  67.             try {
  68.                 Thread.sleep((int)(Math.random() * 100));
  69.             } catch (InterruptedException ex) {
  70.                 Logger.getLogger(BankAccountUser.class.getName()).log(Level.SEVERE, null, ex);
  71.             }
  72.         }
  73.     }
  74.    
  75.     private boolean waiting;
  76.     private final BankAccount account;
  77.     private final int[] transactions;
  78.     private int transactionsRemaining;
  79. }
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89. /*
  90.  * To change this license header, choose License Headers in Project Properties.
  91.  * To change this template file, choose Tools | Templates
  92.  * and open the template in the editor.
  93.  */
  94. package threads;
  95.  
  96. /**
  97.  *
  98.  *
  99.  */
  100. public class BankAccountRescuer extends BankAccountUser{
  101.  
  102.    
  103.     /**
  104.      *
  105.      * @param name
  106.      * @param account
  107.      * @param transactions
  108.      * @param user
  109.      */
  110.     public BankAccountRescuer(String name, BankAccount account, int[] transactions, BankAccountUser[] user) {
  111.         super(name, account, transactions = null);
  112.         this.users = user;
  113.         this.account = account;
  114.  
  115.     }
  116.    
  117.     @Override
  118.     public void run() {
  119.        
  120.         int trans = account.getTransactionsRemaining();
  121.     }
  122.    
  123.    
  124.    private BankAccount account;
  125.    private BankAccountUser[] users;
  126. }
  127.  
  128.  
RAW Paste Data