Esempio n. 1
0
  /** Handles all user actions from buttons or Menubox */
  public void actionPerformed(ActionEvent e) {
    String menuString = e.getActionCommand();
    if (menuString.equals("Add")) { // If the user selects Add from the Menu box
      mainPanel.setVisible(false);
      addGUI();
    } else if (menuString.equals("Search")) { // If the User selects search from the menu box
      mainPanel.setVisible(false);
      searchGUI();
    } else if (menuString.equals("Quit")) { // If the User quits the program
      System.exit(0);
    } else if (menuString.equals("Reset")) { // The user will clear all the fields
      callnoT.setText("");
      authorsT.setText("");
      titleT.setText("");
      publisherT.setText("");
      yearT.setText("");
      organizationT.setText("");
      theText.setText("");
    } else if (menuString.equals(
        "Create")) { // User will add entry and will get back a message from librarySearch
      String call = callnoT.getText();
      String title = titleT.getText();
      String year = yearT.getText();
      String author = authorsT.getText();
      String publisher = publisherT.getText();
      String organization = organizationT.getText();

      theText.setText(
          LibrarySearch.main(
              flag, call, title, year, author, publisher, organization)); // Text returned

    } else if (menuString.equals("Clear")) { // Wipes all the fields in search
      callnoT.setText("");
      titleT.setText("");
      startYearT.setText("");
      endYearT.setText("");
      theText.setText("");
    } else if (menuString.equals("Query")) { // Runs a search
      String call = callnoT.getText();
      String title = titleT.getText();
      String year = startYearT.getText() + "-" + endYearT.getText();
      String author = null;
      String publisher = null;
      String organization = null;
      theText.setText(LibrarySearch.main(2, call, title, year, author, publisher, organization));
    }
  }
  public static void main(String[] args) {
    String in, out = null;

    /*ensure the user has supplied the correct command line arguments*/
    /*IMPORTANT NOTE: files must be in root directory for LoadRecords() to work*/
    if (args.length != 2) {
      System.out.println(
          "System Error: Incorrect Arguments\n"
              + "Requires form: java LibrarySearch <input file> <output file>\n"
              + "      OR\n"
              + "Form: java LibrarySearch <output file>");
      System.exit(0);
    }

    Scanner input = new Scanner(System.in);
    LibrarySearch library = new LibrarySearch();
    String command;

    /*determine user's desired operation, load records from the desired file*/
    if (args.length == 2) {
      in = args[0];
      out = args[1];
      library.LoadRecords(in);
    } else if (args.length == 1) {
      out = args[0];
    }

    /*run the remainder of the program using user input*/
    do {
      System.out.print("Enter add, search, or quit> ");
      command = input.nextLine();
      if (command.equalsIgnoreCase("add") || command.equalsIgnoreCase("a")) library.add(input);
      else if (command.equalsIgnoreCase("search") || command.equalsIgnoreCase("s"))
        library.search(input);
    } while (!command.equalsIgnoreCase("quit") && !command.equalsIgnoreCase("q"));
    library.WriteToFile(out);
  }