// Import statements go here.  For example,
// import java.awt.Color;
import java.util.ArrayList;
// import java.util.Random;

/**
 *  Lab (or Programming Project) X: Name of Lab or PP<br>
 *
 *  The <code>MainClassTemplate</code> class provides a main method
 *  for a program that does X.
 *
 *  A more detailed description goes here, if necessary.
 *
 *  @author [your name]
 *  @author [your partner's name]
 *  @author [with assistance from ... (including instructor/TAs)]
 *  @author [working side-by-side with ...]
 *  @version [last_modification_date]
 */
public class LoopPatterns2020
{
    /**
     *  The main function initiates execution of this program.
     *    @param    String[] args not used in this program
     *              (but main methods always need this parameter)
     **/
    public static void main(String[] args)
    {
        System.out.println ("Welcome to the Loop Demo.");

        // Read menu items from the file.
        String filename = "MenuItems.txt";
        MenuItemReader reader = new MenuItemReader(filename);
        ArrayList<MenuItem> itemList = new ArrayList<MenuItem>();
        String nextLine;
        for ( nextLine = reader.getNextLine(); nextLine != null;
        nextLine = reader.getNextLine() )
        {
            // Each line has the restaurant, menu item, and price for a menu item.
            MenuItem newItem = reader.convertToMenuItem(nextLine);
            itemList.add(newItem);
        }

    // We have filled itemList.  Let's see what the items are.
    System.out.println("Printing all the items:");
    for ( int i = 0; i < itemList.size(); i++ )
    {
        MenuItem oneItem = itemList.get(i);
        System.out.println("\t" + oneItem.toString());  // tab + item info
    }

    System.out.println("\nPrinting all the items again using a different loop:");
    for ( MenuItem oneItem : itemList )
    {
        System.out.println("\t" + oneItem.toString());  // tab + item info
    }

    // What would it cost to buy one of everything?
    double sum = 0.0;
    for ( MenuItem oneItem : itemList )
    {
        sum += oneItem.getPrice();
    }
    System.out.println("The cost of buying 1 of everything is " + sum + ".");

    // What is the average price of menu items in the list? (use sum from above)
    double average = sum / itemList.size();
    System.out.println("The average price of things in this list is " + average);


    // Find "Tomato Caprese" (but full name is longer, so use contains)
    // Look through all the items...
    for ( MenuItem oneItem : itemList )
    {
        // Is this the one (or one of several) we're looking for?
        String itemName = oneItem.getItemName();
        if ( itemName.contains("Tomato Caprese") )
        {
            // Use toString method to print full information
            System.out.println(itemName.toString());
        }
    }

    // Print all the items from Saffron.
    // But also, let's put them in a new list to see what that looks like.
    ArrayList<MenuItem> allFromSaffron = new ArrayList<MenuItem>();
    System.out.println("\nItems from Saffron: ");
    for ( MenuItem oneItem : itemList )
    {
        // Use .equals rather than == to compare strings!!!
        if ( oneItem.getRestaurant().equals("Saffron") )
        {
            System.out.println("\t" + oneItem.toString());
            allFromSaffron.add(oneItem);
        }
    }

    // How many rice dishes are in the overall list?
    int numRiceDishes = 0;
    for ( MenuItem oneItem : itemList )
    {
        if ( oneItem.getItemName().contains("Rice") )
        {
            numRiceDishes++;
        }
    }
    System.out.println("\nThere are " + numRiceDishes
        + " rice dishes in this list.");

    // How many items are less than $10?
    int count = 0;
    for ( MenuItem oneItem : itemList )
    {
        if ( oneItem.getPrice() < 10.00 )
        {
            count++;
        }
    }
    System.out.println ("\nThere are " + count + " items less than $10");

    // What is the average price of menu items from Saffron?
    int numFromSaffron = 0;
    double sumFromSaffron = 0.0;
    for ( MenuItem oneItem : itemList )
    {
        String rest = oneItem.getRestaurant();
        if ( rest.equals("Saffron") )
        {
            sumFromSaffron += oneItem.getPrice();
            numFromSaffron++;
        }
    }
    average = sumFromSaffron / numFromSaffron;
    System.out.println("\nThe average price of things from Saffron is " + average);
    // NOTE: we could have looped through the allFromSaffron list
    //       to get the sum and then divided by allFromSafron.size().

    // What is the price of Chinn Chinn's fried rice?
    for ( MenuItem oneItem : itemList )
    {
        if ( oneItem.getRestaurant().equals("Chinn Chinn") &&
             oneItem.getItemName().contains("Fried Rice") )
        {
            System.out.println("The price of fried rice at Chinn Chinn is "
                + oneItem.getPrice() + ".");
        }
    }

    // What is the most expensive item?
    // Make sure there is at least one item, or this doesn't make sense!
    if ( itemList.size() > 0 )
    {
        // Assume the first item is the most costly (at least so far).
        MenuItem costly = itemList.get(0);

        // Look through the list and see if any are more costly.
        for ( MenuItem oneItem : itemList )
        {
            if ( oneItem.getPrice() > costly.getPrice() )
            {
                // oneItem is more costly, so most costly seen (so far)
                costly = oneItem;
            }
        }
        // By the time we get here, costly is truly the most costly one.
        System.out.println("The most expensive thing in this list is "
            + costly.toString());
    }
    else
    {
        // Can't find most expensive in an empty list!
        System.out.println("There are no items in this list!");
    }

    // What is the most expensive item?
    // Invent a dummy "most costly item" that is less costly than anything.
    double costlyPrice = -1.0;   // Obviously everything will be more costly.
    String costlyItemName = "";  // Don't know item name yet.
    for ( MenuItem oneItem : itemList )
    {
        if ( oneItem.getPrice() > costlyPrice )
        {
            // oneItem is more costly, so most costly seen (so far)
            costlyPrice = oneItem.getPrice();
            costlyItemName = oneItem.getItemName();
        }
    }
    // By the time we get here, costlyItemName is truly the name of
    // the most costly item.
    System.out.println("The most expensive thing in this list is "
        + costlyItemName + " at $" + costlyPrice + ".");

    // What are the three most expensive items?  Create a list to put them in.
    ArrayList<MenuItem> mostExpensive = new ArrayList<MenuItem>();
    double upperBound = 100.00;   // set an initial upper bound higher than the real max
    for ( int i = 0; i < 3; i++ )
    {
        // Find the most expensive that is under the upperBound.  (Usual
        // "find max" algorithm except for the extra upperBound check.)
        double expensivePrice = -1.0;
        int numAtThisPrice = 0;
        for ( MenuItem anElement : itemList )
        {
            double thisPrice = anElement.getPrice();
            if ( thisPrice > expensivePrice && thisPrice < upperBound )
            {
                expensivePrice = thisPrice;
            }
        }
        // Now find all the elements that are at that price and add to list.
        for ( MenuItem anElement : itemList )
        {
            if ( anElement.getPrice() == expensivePrice )
            {
                mostExpensive.add(anElement);
                numAtThisPrice++;
            }
        }
        // Lower upperBound, so next time we don't find these same items again.
        upperBound = expensivePrice;

        // Tricky: i is about to go up by 1 in for loop, but maybe it
        // should go up by more than one?  Increase by 1 less than
        // numAtThisPrice so that when the loop increments i we will
        // actually have raised it by numAtThisPrice.
        if ( numAtThisPrice < 1)
            i += (numAtThisPrice - 1);
    }
    System.out.println("\nThe 3 most expensive menu items are: ");
    for ( MenuItem anElement : mostExpensive )
        System.out.println("\t" + anElement);
    System.out.println();

    }
}
