// Arup Guha
// 3/21/06
// A partial implementation of the Bookstore class for the COP 3330 in-class
// activity on 3/21/06. So far, there is limited functionality. A 
// Bookstore object can be created, and stock can be added, or doubled.
// Items can be sold, but right now there is no Customer class and the
// method of selling is difficult (through sku number only.)

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

public class Bookstore {

  // Instance variables to keep track of the stock and gross revenue.
  private ArrayList<MediaItem> stock;
  private double grossrevenue;

  // Create an empty store.
  public Bookstore() {
    stock = new ArrayList<MediaItem>();
    grossrevenue = 0;
  }

  // Create a new store and stock it with the items in the file pointed to
  // by fin.
  public Bookstore(Scanner fin) {
    grossrevenue = 0;
    addStock(fin);    
  }

  // Add the items in the file pointed to by fin into the current object.
  public void addStock(Scanner fin) {

    // Get the number of items in this file.
    int num = fin.nextInt();

    for (int i=0; i<num; i++) {

      // Read in all the common information.
      String type = fin.next();
      double price = fin.nextDouble();
      int sku = fin.nextInt();
      String author = fin.next();

      // Read in the extra information for a CD, and add it.
      if (type.equals("CD")) {
        String album = fin.next();
        stock.add(new CD(author,album,sku,price));
      }

      // Read in the extra information for a Book, and add it.
      else if (type.equals("Book")) {
        String title = fin.next();
        int numpages = fin.nextInt();
        stock.add(new Book(author,title,sku,price,numpages));
      }
    }

    fin.close();
  }

  // Adds a copy of each item in the current object to itself. (So the
  // current object will contain twice as many items right after this
  // method completes than it did right before it started.
  public void doubleStock() {

    ArrayList<MediaItem> temp = new ArrayList<MediaItem>();

    // Iterate through each item in the stock, and add it to a temp stock.
    for (MediaItem m: stock) {

      // Check if it's a Book and add a Book object.
      if (m instanceof Book)
        temp.add(((Book)m).clone());

      // Check if it's a CD and add a CD object.
      else if (m instanceof CD) 
        temp.add(((CD)m).clone());

    }

    // Iterate again, and add each item in the temp stock back into stock.
    for (MediaItem m: temp)
      stock.add(m);

  }

  // Prints all the items in the current object that cost value or less.
  public void printUnderCost(double value) {

    System.out.println("Here are all the items that cost "+value+" or less.");

    // Iterate through each item, printing the appropriate ones.
    for (MediaItem m: stock)
      if (m.underCost(value))
        System.out.println(m);
    System.out.println();
  }

  // Sell the item with the sku number passed in as a parameter and return
  // this item. If no such item is in stock, return null.
  public MediaItem sell(int sku) {

    // Loop through the stock, looking for the appropriate item.
    for (MediaItem m: stock) {

      // Check for the right sku.
      if (m.getSku() == sku) {

        // Add to the gross revenue of the Bookstore.
        grossrevenue += m.getPrice();

        // Remove the item from the store.
        MediaItem retval = m;
	int index = stock.lastIndexOf(m);
        stock.remove(index);

        // Return the item.
        return retval;
      }    
    }
    return null; // No item found!
  }

  // This method discounts each CD in the current object by rate.
  public void discountCDs(double rate) {

    // Go through each item in the store.
    for (MediaItem m: stock) 

      // Only discount CDs!!!
      if (m instanceof CD)
        m.discount(rate);
  }
  

  // A small main to run some tests.
  public static void main(String[] args) throws IOException {

    // Create an empty bookstore.
    Bookstore b = new Bookstore();
    Scanner fin = new Scanner(new File("stock.in"));

    // Add some items to the new bookstore.
    b.addStock(fin);

    // Get a new copy of each item in the bookstore and add it.
    b.doubleStock();

    // Print out all items in the store that is $15 or less.
    b.printUnderCost(15);

    // Sell the item with sku number 9812, the Dan Brown book.
    b.sell(9812);

    // Discount CDs 20%.
    b.discountCDs(.20);

    // Print out all items in the bookstore that are $20 or less.
    b.printUnderCost(20);

  }
}
