import java.io.File;
import java.util.Scanner;

class BankAccountsApp {
	public static void main(String [] args) throws Exception {
		Scanner scanner = new Scanner(new File("bank_actions.text"));

		final int CAPACITY = 100;
		BankAccount [] accounts = new BankAccount[CAPACITY];
		int size = 0;

		while (scanner.hasNextInt()) {
			int id = scanner.nextInt();
			String action = scanner.next();
			int amount;

			BankAccount account = search(accounts, size, id);
			if (account == null) {
				account = new BankAccount(id);
				size = add(accounts, size, account, CAPACITY);
				System.out.println("Added " + account);
			}

			switch (action) {
				case "D":
					amount = scanner.nextInt();
					account.deposit(amount);
					System.out.println("After deposit of $" + amount + ": " + account);
					break;
				case "W":
					amount = scanner.nextInt();
					account.withdraw(amount);
					System.out.println("After withdrawal of $" + amount + ": " + account);
					break;
				default:
					System.out.println("*** Error *** unknown action: " + action);
			}
		}
		System.out.println();
		System.out.println("=== Accounts ===");
		print(accounts, size);
	}	

	public static BankAccount search(BankAccount [] accounts, int size, int id) {
		for (int i = 0; i < size; i++) 
			if (accounts[i].getId() == id) return accounts[i];
		return null;
	}

	public static int add(BankAccount [] accounts, int size, BankAccount account, int capacity) {
		if (size == capacity) {
			System.err.println("Capacity reached; make arrays larger");
			System.exit(1);
		}
		accounts[size] = account;
		return size+1;
	}
		
	public static void print(BankAccount [] accounts, int size) {
		for (int i = 0; i < size; i++)
			System.out.println(accounts[i].getId() + ": $" + accounts[i].getBalance());
	}

}
