/**
   * Sells a specified number of records Throws an exception if the record doesn't exist in the
   * store or if trying to sell more records than exist in the inventory
   *
   * @param record - the record to sell
   * @param quantity - the number of records to sell
   */
  public void sellItem(AudioFormat record, int quantity)
      throws NullPointerException, IllegalArgumentException {
    // System.out.println("Selling: " + record.toString() + ", quantity: " + quantity);
    InventoryItem item = null;
    for (int i = 0; i < this.listOfItems.length; i++) {
      item = this.listOfItems[i];

      // If it finds the record, break out of the loop
      if (record.equals(item.getRecord())) {
        break;
      }
    }

    // Handle the record not being in the inventory
    if (item == null) {
      throw new NullPointerException(
          "Error: there is no record of this type in stock "
              + "(record does not exist within the array).");
    }

    // Handle selling more items than the store has
    if (item.getQuantity() - quantity < 0) {
      throw new IllegalArgumentException(
          "Error: you cannot sell more records than you have "
              + "(# of records sold makes quantity smaller than 0).");
    }
    item.setQuantity(item.getQuantity() - quantity);
  }
  /**
   * Writes the contents of listOfItems to the file at the specified path Precondition - the file
   * must already exist
   *
   * @param path - the file to write to
   */
  public void writeToFile(String path) {
    PrintWriter writer = null;
    try {
      writer = new PrintWriter(path);

      // Iterates through every item in the store
      for (InventoryItem item : this.listOfItems) {
        DecimalFormat decimalFormat = new DecimalFormat("#0.00");

        // Uses StringBuilder instead of String concatenation to save memory in the loop
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(item.getRecord().toString());
        stringBuilder.append(",");
        stringBuilder.append(item.getQuantity());
        stringBuilder.append(",");

        // Ensures that the price has only two decimal places
        stringBuilder.append(decimalFormat.format(item.getPrice()));
        stringBuilder.append("\n");
        writer.write(stringBuilder.toString());
      }
    } catch (FileNotFoundException e) {
      System.out.println("Error transcribing output.");
      e.printStackTrace();
    } finally {
      writer.close();
    }
  }
  /**
   * Adds a specified number of records to the inventory and updates the price
   *
   * @param record - the record to add
   * @param quantity - the number of records to add
   * @param price - the price to sell the record at
   */
  public void addItem(AudioFormat record, int quantity, double price) {
    // System.out.println("Adding: " + record.toString() + ", quantity: " + quantity + ", price: " +
    // price);
    for (int i = 0; i < this.listOfItems.length; i++) {
      InventoryItem item = this.listOfItems[i];

      // If the record is found, set the price and add the quantity and end the method
      if (record.equals(item.getRecord())) {
        item.setPrice(price);
        item.setQuantity(item.getQuantity() + quantity);
        return;
      }
    }

    // If it's an entirely new record, duplicates the old array and increases its size by 1
    InventoryItem[] temp = new InventoryItem[this.listOfItems.length + 1];
    for (int i = 0; i < this.listOfItems.length; i++) {
      temp[i] = this.listOfItems[i];
    }
    this.listOfItems = temp;

    // Adds the new record to the end of the array
    this.listOfItems[this.listOfItems.length - 1] = new InventoryItem(record, quantity, price);
  }