// Arup Guha
// 1/16/2010
// Written to test out several Card objects.

import java.util.*;

public class TestCredit {

	// I went ahead and made an objects, but students don't have to.
	private CreditCard[] myCards;
	int numCards;
	
	// Is 10 enough credit cards?
	public TestCredit() {
		myCards = new CreditCard[10];
		numCards = 0;
	}
	
	// Just string together the String representation of each card.
	public String toString() {
		String tmp = "";
		for (int i=0; i<numCards; i++)
			tmp = tmp+i+". "+myCards[i]+"\n\n";
		return tmp;
	}
	
	public static void main(String[] args) {
		
		// Get user's first choice.
		Scanner stdin = new Scanner(System.in);
		int answer;
		menu();
		answer = stdin.nextInt();
		
		// Create my TestCredit object.
		TestCredit mine = new TestCredit();
		
		// Keep going until they quit.
		while (answer != 6) {
			
			// Execute the appropriate option.
			if (answer == 1) 
				mine.makeCard();
			else if (answer == 2) 
				mine.makePurchase();
			else if (answer == 3) 
				mine.payBill();
			else if (answer == 4)
				mine.nextYear();
			else if (answer == 5)
				System.out.println(mine);
			else if (answer != 6)
				System.out.println("Not a valid menu choice. Try again.");
				
			// Get next.
			menu();
			answer = stdin.nextInt();
		}
	}
	
	// Prints the menu.
	public static void menu() {
		System.out.println("Please select a choice.");
		System.out.println("1. Create a credit card.");
		System.out.println("2. Make a purchase.");
		System.out.println("3. Pay a credit card bill.");
		System.out.println("4. Advance to the next year.");
		System.out.println("5. Print out the status of all credit cards.");
		System.out.println("6. Quit.");
	}
	
	// Creates a card.
	public boolean makeCard() {
		
		// Get this out of the way.
		if (numCards == myCards.length) {
			System.out.println("Sorry you are at your limit of cards.");
			System.out.println("No card can be added.");
			return false;
		}
		
		// Get user info.
		Scanner stdin = new Scanner(System.in);
		int answer;

		System.out.println("What card do you want? (1)Regular, (2)Gold");
		answer = stdin.nextInt();
		System.out.println("What is your first name?");
		String first = stdin.next();
		System.out.println("What is your last name?");
		String last = stdin.next();
		System.out.println("What is the current year?");
		int year = stdin.nextInt();
		
		// Add this card to the collection.
		if (answer == 1) 
			myCards[numCards] = new CreditCard(first,last,year);
		else
			myCards[numCards] = new GoldCreditCard(first,last,year);
			
		// Adjust the number of cards in the collection.
		numCards++;
		return true;
	}
	
	// Executes a purchase.
	public void makePurchase() {
		
		// Check default case.
		if (numCards == 0) {
			System.out.println("Sorry, you have no cards with which to make a purchase.");
			return;
		}
		
		// Get card to use.
		Scanner stdin = new Scanner(System.in);
		System.out.println("Which card do you want to use(0-"+(numCards-1)+")?");
		int choice = stdin.nextInt();
		
		// Just set choice to be a valid one, the primary card, if necessary.
		if (choice >= numCards || choice < 0) choice = 0;
		
		// Get purchase amount.
		System.out.println("How much is your purchase?");
		double amount = stdin.nextDouble();
		
		// Print out accordingly.
		if (myCards[choice].charge(amount))
			System.out.println("Congrats, your purchase has been charged.");
		else
			System.out.println("Sorry you don't have the necessary credit limit for this purchase.");
		
	}
	
	public void payBill() {
		
		// Check default case.
		if (numCards == 0) {
			System.out.println("Sorry, you have no cards to pay off.");
			return;
		}
		
		// Get which card.
		Scanner stdin = new Scanner(System.in);
		System.out.println("Which card do you want to make a payment for(0-"+(numCards-1)+")?");
		int choice = stdin.nextInt();
		
		// Just set choice to be a valid one, the primary card, if necessary.
		if (choice >= numCards || choice < 0) choice = 0;
		
		// This seemed to be the best place to put this!
		myCards[choice].addInterest();
		
		// Get payment and state how much they actually paid off.
		System.out.println("How much do you want to pay?");
		double amount = stdin.nextDouble();
		double actual = myCards[choice].payoff(amount);
		System.out.println("Great, you paid off $"+actual);
	}
	
	// Advances all cards.
	public void nextYear() {
		for (int i=0; i<numCards; i++)
			myCards[i].advanceYear();
	}
}