// Account.java

import java.util.*;
/*
 (provided code)
 Simple, synchronized Account class encapsulates
 a balance and a transaction count. Instead of deposit()
 and withdraw(), client send apply(transaction).
*/
class Account {
    private int id;
    public int balance;
    private int transactions;
    private Bank bank;
    
    public Account(Bank bank, int id, int balance) {
        this.bank = bank;
        this.id = id;
        this.balance = balance;
        transactions = 0;
    }
    
    public synchronized int getBalance() {
        return balance;
    }
    
    public synchronized int getTransactions() {
        return transactions;
    }
    
    /*
     Applies the given transaction to the account --
     subtracting for the 'from' acct and adding for
     the 'to' acct. Client should send this to
     both the accounts of a transaction.
    */
    public synchronized void apply(Transaction trans) {
        if (trans.to == id) balance += trans.amount;
        if (trans.from == id) balance -= trans.amount;

        if (id!=trans.to && id!=trans.from) {
            throw new RuntimeException("Bad account apply");
        }

        transactions++;

    }

    public String toString() {
        return("acct:" + id + " bal:" + balance + " trans:" + transactions);
    }

}
