import type.lang.*;
import type.lib.*;

public class DemoInstanceof
{
   public static void main(String[] args)
   {
		// -----------------------
		// check for correct usage
		// -----------------------

		if (args.length != 1)
		{
			IO.println("-------------------------------------------");
			IO.println("Usage: java DemoInstanceof file\n");
			IO.println("where 'file' is a credit card database file");
			IO.println("(see 'creditcards2.txt' for example file)");
			IO.println("-------------------------------------------");
			System.exit(0);
		}

		// -----------------
		// load the database
		// -----------------

		IO.print("Loading the database... ");
		GlobalCredit gc = new GlobalCredit();		
		UniReader ur = new UniReader(args[0]);

		String s;
		CreditCard cc = null;

		while ((s = ur.readLine()) != null)
		{
			// skip empty lines or lines beginning with '#' (comments)
			if (s.length() == 0 || s.charAt(0) == '#') 
				continue;

			int number = Integer.parseInt(s);
			String type = ur.readLine();
			String name = ur.readLine();
			double limit = ur.readDouble();
			double balance = ur.readDouble();

			// declare and instantiate the credit/reward card
			if (type.equals("ordinary"))
				cc = new CreditCard(number, name, limit);
			else if (type.equals("reward"))
				cc = new RewardCard(number, name, limit);
			else
			{
				IO.println("Unknown card type");
				System.exit(0);
			}

			if (!cc.charge(balance))
			{
				IO.println("Charge exceeds limit!");
				System.exit(0);
			}
			gc.add(cc);
		}
		IO.println("Done!");

		// -------------
		// traverse demo
		// -------------

		IO.println("Traverse demo... ");

		CreditCard c;
		IO.print("NUMBER", "15L");
		IO.print("TYPE", "15L");
		IO.print("NAME", "15L");
		IO.print("LIMIT", "10");
		IO.print("     ");
		IO.println("BALANCE", "10");
		
		for (c = gc.getFirst(); c != null; c = gc.getNext())
		{
			IO.print(c.getNumber(), "15L");
			if (c instanceof RewardCard)
				IO.print("Reward", "15L");
			else
				IO.print("Ordinary", "15L");
			IO.print(c.getName(), "15L");
			IO.print("$" + c.getLimit(), "10.0");
			IO.print("     ");
			IO.println("$" + c.getBalance(), "10.2");
		}
		IO.println("Done!");
   }
}

