// Arup Guha
// 12/5/2015, 12/12/2015
// Solution to Junior Knights Grocery Problem

import java.util.*;

public class grocery {

	public static void main(String[] args) {

		// Set everything up.
		Scanner stdin = new Scanner(System.in);
		double sales = 0;
		ArrayList<item> stock = new ArrayList<item>();

		// Get the user's first choice.
		menu();
		int choice = stdin.nextInt();

		// Keep on going until we quit.
		while (choice != 7) {

			// Stocking this item.
			if (choice == 1) {
				System.out.println("Please enter the name of the item to add to the store.");
				String name = stdin.next();
				System.out.println("What is the cost of this item?");
				double cost = stdin.nextDouble();
				stock.add(new item(name, cost));
			}

			// Buying an item.
			else if (choice == 2) {

				// Print out the stock.
				System.out.println("Which item number do you want to buy?");
				for (int i=0; i<stock.size(); i++) {
					System.out.println((i+1)+". "+stock.get(i));
				}

				// Process the buy.
				int number = stdin.nextInt();

				// Invalid item so don't remove it.
				if (number < 1 || number > stock.size()) {
					System.out.println("Sorry that is not a valid item.");
				}

				// Buy it!
				else {
					item bought = stock.remove(number-1);
					sales += bought.getCost();
				}
			}

			else if (choice == 3) {

				// Print out the stock.
				System.out.println("Which item number do you want to discount?");
				for (int i=0; i<stock.size(); i++) {
					System.out.println((i+1)+". "+stock.get(i));
				}

				// Process the discount
				int number = stdin.nextInt();

				// Invalid item so don't remove it.
				if (number < 1 || number > stock.size()) {
					System.out.println("Sorry that is not a valid item.");
				}

				// Discount it!
				else {

					// Get the percentage.
					System.out.println("What percentage do you want to discount the item?");
					int perc = stdin.nextInt();

					// Oops - invalid discount percentage.
					if (perc < 0 || perc >= 100)
						System.out.println("Sorry, that is an invalid discount for any item.");

					// Process the discount.
					else
						stock.get(number-1).discount(perc);
				}

			}

			// Just print the stock.
			else if (choice == 4) {
				System.out.println("Here is everything in stock:");
				for (int i=0; i<stock.size(); i++) {
					System.out.println((i+1)+". "+stock.get(i));
				}
			}

			// Search for an item.
			else if (choice == 5) {

				// Get the item name.
				System.out.println("Which item to you want to search for?");
				String item = stdin.next();

				// Alternate way to loop through items.
				boolean found = false;
				for (item thisItem : stock) {
					if (thisItem.getName().equals(item)) {
						System.out.println("The item is in stock! It costs "+thisItem.getCost());
						found = true;
						break;
					}
				}

				// We never found it, sorry!
				if (!found)
					System.out.println("Sorry, we don't have that item in stock.");
			}

			// Print total sales.
			else if (choice == 6) {
				System.out.println("The total sales so far is $"+sales);
			}

			// Error message for menu choice.
			else if (choice != 7) {
				System.out.println("Sorry that's not a valid menu choice.");
			}

			// Get next choice.
			menu();
			choice = stdin.nextInt();
		}
	}

	// Just prints out a menu of choices.
	public static void menu() {
		System.out.println("Which option would you like?");
		System.out.println("1. Stock an item.");
		System.out.println("2. Sell an item.");
		System.out.println("3. Discount an item.");
		System.out.println("4. Print everything in stock.");
		System.out.println("5. Search for an item.");
		System.out.println("6. Print out the total sales thus far.");
		System.out.println("7. Quit");
	}
}

