

/**
 * Manages a collection of <code>Book</code> objects.
 */
public class BookStore {

  /**
   * Stores all book objects.
   */
  private Book[] books;

  /**
   * Keeps track of the actual number of book objects stored.
   */
  private int totalbooks;

  /**
   * Keeps track of the total gross income of the BookStore.
   */
  private double gross;

  /**
   * Total number of books this bookstore supports.
   */
  private final static int MAXNUMOFBOOKS = 1000;

  /**
   * Constructor creates an empty <code>BookStore</code> object.
   */
  public BookStore() {
    books = new Book[MAXNUMOFBOOKS];
    totalbooks = 0;
    gross = 0.0;
  } // constructor

  /**
   * Adds a new book to this bookstore.
   * @param b the book to add
   */
  public void addNewBook(Book b) {
    if (totalbooks < books.length) {
      books[totalbooks] = b;
      totalbooks++;
    }
    else {
      System.out.println("\nBookStore: I cannot add a new book into stock.");
    }
  } // addNewBook


  /**
   * Adds a certain quantity of a book already in stock.
   * @param title this method matches the book using this parameter
   * @param quantity amount of books to add
   */
  public void addBookQuantity(String title, int quantity) {
    int i;
    // Search for the book...if found adjust the quantity.
    for (i=0; i<totalbooks; i++) {
      if ((books[i].getTitle()).equals(title)) {
        books[i].addQuantity(quantity);
	return;
      }
    }
    // Book is not found in the bookstore
    System.out.println("\nBookStore: I cannot increment the quantity of the book titled");
    System.out.println("'"+title+"' because it is not available in the bookstore.");
  } // addBookQuantity


  /**
   * Checks if at least a certain number of a particular book are in stock.
   * Note: You can use <code>inStock(title, 0)</code> to check if a book
   * with <code>title</code> exists.  In this way, you won't create
   * duplicate records of the same book.  Hint: This is useful when adding and
   * selling.
   * @param title matching based on title
   * @param quantity the desired quantity
   * @returns true if title exists with specified quantity; otherwise false
   */
  public boolean inStock(String title, int quantity) {
    int i;
    // Search for the book...if found, adjust the quantity.
    for (i=0; i<totalbooks; i++) {
      if ((books[i].getTitle()).equals(title)) {
        if (quantity <= books[i].getQuantity()) {
          return true;
        }
        else {
          return false;
        }
      } // if
    } // for
    // Book not in the BookStore.
    return false;
  } // inStock

  /**
   * Sells a particular number of a certain book. If successful (i.e. enough
   * books are in stock to sell), the quantity of the book is adjusted.
   * Otherwise, no books are sold.
   * @param title matching based on title
   * @param quantity the amount of books to sell
   * @returns true if successful; otherwise false
   */
  public boolean sellBook(String title, int quantity) {
    int i;
    boolean sellflag=false;
    // Checks to see if the books are in stock.
    boolean retval = inStock(title, quantity);
    // If so, completes the sale.
    if (retval) {
      for (i=0; i<totalbooks && !sellflag; i++) {
        if (title.equals(books[i].getTitle())) {
          books[i].subtractQuantity(quantity);
          gross += (books[i].getPrice()) * quantity;
          sellflag = true;
        }
      } // for
    } // if
    return retval;
  } // sellBook

  /**
   * Lists information about each book by calling <code>toString()</code> on
   * each book.
   */
  public void listBooks() {
    int i;
    // Print out all information.
    System.out.println("\nList of Books\n=============");
    for (i=0;i<totalbooks;i++) {
      System.out.println(books[i]);
    }
    System.out.println();

  } // listBooks

  /**
   * Lists the titles of the books by calling <code>getTitle()</code> on each
   * book.
   */
  public void listTitles() {
    int i;
    // Print out all information.
    System.out.println("\nTitles of Books\n===============");
    for (i=0;i<totalbooks;i++) {
      System.out.println(books[i].getTitle());
    }
    System.out.println();
  } // listTitles

  /**
   * Returns the gross income of this bookstore.
   * @returns gross income
   */
  public double getIncome() {
    return gross;
  } // getIncome

} // BookStore

