/**
   * Edits the marketing note on a machine.
   *
   * @param The new note to place in the machine.
   */
  private void changeMarketingNote(String newNote)
      throws NegativeInputException, NullDateException {
    // String word = "";
    // String rest = "";
    // if (newNote.contains(" "))
    // {
    // int index = newNote.indexOf(' ');
    // word = newNote.substring(0, index);
    // rest = newNote.substring(index + 2);
    // }
    // // System.out.println(newNote);
    // if (word.equals("Notes"))
    // {
    String temp[] = newNote.trim().split("\\n");
    String all = "";
    for (int i = 0; i < temp.length; ++i) {
      String tempString = temp[i].trim();
      if (!tempString.equals("")) {
        all += temp[i].trim() + "|";
      }

      // System.out.println(temp[i]);
    }

    if (numOfSelected.length == 1) {
      manager.editMarketingNote(numOfSelected[0], all, Calendar.getInstance());
    } else {
      for (int i = 0; i < numOfSelected.length; ++i) {
        int ID = numOfSelected[i];
        String temp1[] = getMarketingNote(ID).trim().split("\\n");
        String databaseText = "";
        for (int i1 = 0; i1 < temp1.length; ++i1) {
          String tempString = temp1[i1].trim();
          if (!tempString.equals("")) {
            databaseText += temp1[i1].trim() + "|";
          }
        }
        // System.out.println(all);

        databaseText += all;

        manager.editMarketingNote(ID, databaseText, Calendar.getInstance());
      }
    }
    // }
    // else
    // {
    // manager.editMarketingNote(ID, newNote, Calendar.getInstance());
    // }

  }
  /**
   * Gets the date of when the note was edited.
   *
   * @param flag
   * @return The date of the note waas edited.
   */
  private String getNoteDate(int flag, int ID) {
    String dateString = "";
    Calendar date = null;
    if (flag == 1) {
      date = manager.getRestockerDate(ID);

    } else {
      date = manager.getMarketingDate(ID);
    }

    if (date == null) {
      return "no date"; // needed when no note is written yet
    }
    int day = date.get(Calendar.DATE);
    int month = date.get(Calendar.MONTH);
    int year = date.get(Calendar.YEAR);
    String monthString = new DateFormatSymbols().getMonths()[month];
    dateString += monthString + " " + day + ", " + year;
    return dateString;
  }
  /**
   * gets the marketing note from the manager
   *
   * @return note returns the marketing note for the vending machine
   */
  private String getMarketingNote(int ID) {
    Scanner noteScan = new Scanner(manager.readMarketingNote(ID));
    String curLine;
    String all = "";
    while (noteScan.hasNext()) {
      curLine = noteScan.nextLine();
      if (curLine.contains("Notes")) {
        curLine = curLine.substring(8);

        String temp[] = curLine.split("\\|");

        for (int i = 0; i < temp.length; ++i) {
          all += temp[i].trim() + "\n";
          // System.out.println(temp[i]);
        }

        return all.trim();
      }
    }
    return manager.readMarketingNote(ID);
  }
  /**
   * <CODE> MarketingGUI</CODE> is the graphical interface for the marketing portion of the system.
   * It shows statistics for all of the machines available or for individual machines. Their is also
   * a marketing graph available to show graphical statistics of the system.
   */
  public MarketingGUI(MarketingManager manager) {
    // setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    this.manager = manager;
    numberOfMachines = manager.getNumberOfVendingMachines();

    // setSize(600, 400);
    // mainLayout.removeAll();
    // setSize(600, 400);
    // sets the title
    setTitle("Marketing Interface for machine ID ");
    setResizable(true);
    setSize(1000, 700);
    globalVendScreen();
    add(mainLayout);
    setVisible(true);
  }
  private Object[][] getStock(int ID) throws OutOfBoundsStockException {

    thisVend = manager.getVend(ID);
    Object stock[][] = new Object[thisVend.getTotalStock().size()][6];
    int i = 0;
    for (Map.Entry<XYPair, Integer> itemPair : thisVend.getTotalStock().entrySet()) {
      System.out.println(itemPair.getValue());
      stock[i][2] = thisVend.getAtXYZ(itemPair.getKey().x, itemPair.getKey().y, 0).getName();
      stock[i][0] = itemPair.getKey().x + 1;
      stock[i][1] = itemPair.getKey().y + 1;
      stock[i][3] = itemPair.getValue();
      stock[i][4] = thisVend.getAtXYZ(itemPair.getKey().x, itemPair.getKey().y, 0).getPrice();
      stock[i][5] = (int) stock[i][3] * (double) stock[i][4];
      ++i;
    }

    sort2DArray(stock);
    return stock;
  }