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

public class CardCompany {
	private String name;
	private int nextCardNumber;
	private Vector cards = new Vector();

	public CardCompany(String name, int firstCardNumber) {
		this.name = name;
		nextCardNumber = firstCardNumber;
	}

	CardCompany(DataInputStream in) throws IOException {
		name = in.readUTF();
		nextCardNumber = in.readInt();
		int numCards = in.readInt();

		for (int i=0; i < numCards; i++)
			cards.addElement(new Card(in));
	}

	void write(DataOutputStream out) throws IOException {
		out.writeUTF(name);
		out.writeInt(nextCardNumber);
		int numCards = cards.size();
		out.writeInt(numCards);

 		for (Enumeration e=getCardIterator(); e.hasMoreElements(); ) {
			Card c = (Card) e.nextElement();
			c.write(out);
		}
	}
	
	void save(String fname) throws IOException {
		OutputStream out = new FileOutputStream(fname);
		DataOutputStream dout = new DataOutputStream(out);
		write(dout);
		dout.close();
	}

	static CardCompany load(String fname) throws IOException {
		InputStream in = new FileInputStream(fname);
		DataInputStream din = new DataInputStream(in);
		CardCompany company = new CardCompany(din);
		din.close();
		return company;

	}

	public String getName() { return name; }

	void setName(String name) { this.name = name; }

	public Card issueCard(String name) {
		Card c = new Card(name, nextCardNumber++);
		cards.addElement(c);
		return c;
	}
	
	private Enumeration getCardIterator() { 
		return cards.elements();
	}
	
	public Card findCard(String name) {
		for (Enumeration e=getCardIterator(); e.hasMoreElements(); ) {
			Card c = (Card) e.nextElement();
			if (name.equals(c.getName()) ) 
				return c;
		}
		
		return null;
	}
	
	public Card findCard(int number) {
		for (Enumeration e=getCardIterator(); e.hasMoreElements(); ) {
			Card c = (Card) e.nextElement();
			if (number == c.getNumber()) 
				return c;
		}
		
		return null;
	}
	
	public Card purchase(int number, String item, double amount) {
		Card c = findCard(number);
		
		if (c != null)
			c.purchase(item, amount);
			
		return c;
	}
	
	public Card payment(int number, double amount) {
		Card c = findCard(number);
		
		if (c != null)
			c.payment(amount);
			
		return c;
	}
	
	public boolean cancelCard(int number) {
		Card c = findCard(number);
		
		if (c != null) {
			cards.removeElement(c);
			return true;
		}
		
		return false;
	}

	public double totalBalance() {
		double balance = 0.0;
		
		for (Enumeration e=getCardIterator(); e.hasMoreElements(); ) {
			Card c = (Card) e.nextElement();
			balance += c.getBalance();
		}
		
		return balance;
	}

	public void print() {
		System.out.println("Company: " + name);
		
		for (Enumeration e=getCardIterator(); e.hasMoreElements(); ) {
			Card c = (Card) e.nextElement();
			c.print();;
		}
	}

	public static void main(String arg[]) {
		CardCompany firstJava = new CardCompany("First Java Card", 20000);
		CardCompany indoExpress = new CardCompany("Indonesian Express", 30000);
		
		int maryNo = firstJava.issueCard("Mary Worth").getNumber();
		int joeNo = firstJava.issueCard("Joe Bazooka").getNumber();
		int dilbertNo = indoExpress.issueCard("Dilbert").getNumber();
		
		indoExpress.purchase(dilbertNo, "Game", 42.00);
		firstJava.purchase(joeNo, "Food", 33.42);
		indoExpress.purchase(dilbertNo, "Book", 49.95);
		firstJava.purchase(maryNo, "Tickets", 40.00);
		indoExpress.payment(dilbertNo, 25.00);
		
		indoExpress.print();
		firstJava.print();
	}
}
