import java.util.*;
import java.io.*;

public class Card {
	private String name;
	private int number;
	private Vector trans = new Vector();

	Card(String name, int number) {
		this.name = name;
		this.number = number;
	}

	Card(DataInputStream in) throws IOException {
		name = in.readUTF();
		number = in.readInt();
		int numTrans = in.readInt();

		for (int i=0; i < numTrans; i++)
			trans.addElement(new Transaction(in));
	}

	void write(DataOutputStream out) throws IOException {
		out.writeUTF(name);
		out.writeInt(number);

		int numTrans = trans.size();
		out.writeInt(numTrans);
		for(Enumeration e=getTransactionIterator(); e.hasMoreElements(); ) {
			Transaction t = (Transaction) e.nextElement();
 			t.write(out);
		}
	}

	public String getName() { return name; }

	public int getNumber() { return number; }

	void payment(double amount) {
		purchase("PAYMENT", -amount);
	}

	void purchase(String item, double amount) {
		Transaction t = new Transaction(item, amount);
		trans.addElement(t);
	}
	
	Enumeration getTransactionIterator() { 
		return trans.elements();
	}

	public double getBalance() {
		double balance = 0.0;

		for(Enumeration e=getTransactionIterator(); e.hasMoreElements(); ) {
			Transaction t = (Transaction) e.nextElement();
			balance += t.getAmount();
		}

		return balance;
	}

	void print() {
		System.out.println("----------------------");
		System.out.println("Card #" + number);
		System.out.println("Card Holder: " + name);
		if (trans.size() == 0)
			System.out.println("No current transactions");
		else {
			for(Enumeration e=getTransactionIterator(); e.hasMoreElements(); ) {
				Transaction t = (Transaction) e.nextElement();
				System.out.println("\t" + t);
			}

		}

		System.out.println("Total balance: " + getBalance());
	}

	public static void main(String arg[]) {
		Card peter = new Card("Peter Parker", 55443322);
		peter.purchase("Java Book", 49.95);
		peter.purchase("Visual Cafe", 99.99);
		peter.payment(50.00);

		Card arther = new Card("Clark Kent", 22446688);
		arther.purchase("Computer", 3500.00);
		arther.purchase("Printer", 349.95);

		peter.print();
		arther.print();
	}
}
