/**
  * Takes the barcode for a product and adds it to the checkout
  *
  * @param input The barcode for the product as a string
  * @return True if the product was added, false if it failed
  */
 public static boolean addToCart(String input) {
   String tempBarCode = "-1";
   if (input != null && !input.equals("")) {
     tempBarCode = input; // disallows the user from entering nothing or clicking cancel.
   } else if ((input == null) || ("".equals(input))) {
     return false;
   }
   String adding = itemDatabase.getEntryName(tempBarCode).orElse("ERROR");
   if (adding != null && !adding.equals("ERROR")) {
     System.out.println(tempBarCode + "\n" + adding);
     checkOuts.addProduct(tempBarCode, adding); // otherwise, add the product as normal.
     return true;
   }
   return false;
 }
Beispiel #2
0
  private void run() {
    /**
     * Class Interface: Method run Preconditions: Interface constructor has been run Postconditions:
     * the program will have run and all user interactions will have been acted upon. If ended
     * correctly, the program will write the productDatabase to productDatabase.txt. The program
     * will end.
     */
    setUIFont(new javax.swing.plaf.FontUIResource("Serif", Font.PLAIN, 25));
    // create the variables that will be used throughout the program
    String tempName; // names of things which we will be entering and reusing
    long tempProductPrice; // the above but as a price
    String tempInput; // what will be storing any input for testing and conversion
    long tempBarCode;
    int quantity; // the bar code and number of each item that we have.
    int personNumber = -1; // the position of the person in the database
    int productNumber; // as above but for products
    int q1, q2; // test question integer for menus.
    int i, error, added; // minor variables which will be used in multiple parts of the program.
    error = 0; // setting up error
    boolean admin = false; // how the program knows that it is in admin mode
    boolean sameUser = true; // How the program knows that it is serving the same user.
    boolean first = true;
    int another = 0; // whether another item will be added
    final JPasswordField passwordField =
        new JPasswordField(10); // box to take passwords from the user

    // Import the productDatabase
    q2 = 0;
    error = productDatabase.readDatabase("productDatabase.txt"); // read in the product database
    if (error == -1) { // tell the user there was an error reading the above
      System.out.println("There was an error reading the productDatabase");
    } else { // tell the user the above went swimingly.
      System.out.printf("I have imported %d products\n", error);
    }
    error = personDatabase.readDatabase("personDatabase.txt"); // as above for the person database
    if (error == -1) {
      System.out.println("There was an error reading the personDatabase");
    } else {
      System.out.printf("I have imported %d people\n", error);
    }
    while (true) // perminantly loop through this code.
    {

      tempBarCode = getPMKeyS();
      personNumber =
          personDatabase.findPerson(
              tempBarCode); // convert this integer to the person number in the databate.
      sameUser = true; // tells the program that a user is logged in.
      first = true;
      if (-2 == personNumber) { // checks whether that user is an admin
        String passWd = "";
        passWd = getPassWd(true);
        if (passWd != null) {
          passWd = getSecurePassword(passWd);
        }
        if (passWd != null
            && !"".equals(passWd)
            && passWd.equals(personDatabase.getPersonName(-2))) {
          admin = true; // the above conversion will return -2 for all admins. This will enact that.
          sameUser = false; // skip the normal user interface for non admin personnel.
          passWd = null;
        } else if (passWd != null) {
          JOptionPane.showMessageDialog(
              null, "Password incorrect", "Error", JOptionPane.ERROR_MESSAGE);
          admin = false;
          sameUser = false;
          passWd = null;
        } else {
          admin = false;
          sameUser = false;
        }
      }
      while (sameUser) { // avoids people having to re-enter their PMKeyS to get to the shopping
        // cart if they stuff something up.
        while (!admin) {
          //	tempInput = JOptionPane.showInputDialog("Hello " +
          // personDatabase.getPersonUser(personNumber) + "\nEnter the bar code of the product you
          // would like");
          tempInput =
              showInputDialog(
                  "<html>Hello "
                      + personDatabase.getPersonUser(personNumber, true)
                      + "<br>Enter the bar code of the product you would like</html>",
                  "what would you like",
                  JOptionPane.QUESTION_MESSAGE,
                  JOptionPane.OK_CANCEL_OPTION,
                  new String[0],
                  false);
          if (tempInput != null && !tempInput.equals("") && isLong(tempInput)) {
            tempBarCode =
                Long.parseLong(
                    tempInput); // disallows the user from entering nothing or clicking cancel.
          } else if ((tempInput == null && !first) || ("".equals(tempInput) && !first)) {
            // if canceled and not on the first run of adding items.
            another =
                JOptionPane.showConfirmDialog(
                    null,
                    "You are purchasing "
                        + checkOuts.getCheckOut(1)
                        + "\nWould you like to add more items?"
                        + "\nHitting no will buy this cart",
                    "Cart",
                    JOptionPane.YES_NO_CANCEL_OPTION);
            if (another == JOptionPane.NO_OPTION) break; // let the user buy already.
            else if (another == JOptionPane.CANCEL_OPTION) {
              checkOuts = new CheckOut();
              sameUser = false;
              break;
            } else if (another == JOptionPane.YES_OPTION) continue;
          } else { // what to do if the user does the above on the first run.
            sameUser = false;
            another = 1;
            // checkOuts = new CheckOut();
            break;
          }
          if (tempBarCode == personDatabase.getBarCode(personNumber)) {
            JOptionPane.showMessageDialog(
                null,
                "Little early in your career to start selling yourself isn't it?\n"
                    + "This incident has been reported",
                "Attempted prostitution",
                JOptionPane.ERROR_MESSAGE);
            continue;
          }
          productNumber =
              productDatabase.findProduct(
                  tempBarCode); // Now that we have done the error checking, convert the barcode to
          // a position in the database
          if (productNumber == -1) { // -1 is output by the above on error
            if (tempBarCode == personDatabase.getBarCode(personNumber)) {
              JOptionPane.showMessageDialog(
                  null,
                  "Little early in your career to start selling yourself isn't it?\n"
                      + "This incident has been reported",
                  "Attempted prostitution",
                  JOptionPane.ERROR_MESSAGE);
            } else
              JOptionPane.showMessageDialog(
                  null,
                  "That product does not exist, please try again.",
                  "Error",
                  JOptionPane.ERROR_MESSAGE);
            continue;
          }

          int checkProduct; // create this for use below
          tempInput = productDatabase.getProduct(productNumber);
          checkProduct =
              checkOuts.productEqualTo(
                  tempInput); // check that the product was not entered into the database before.
          // If it was, just add the quantity to the one in the database
          if (checkProduct != -1) {
            checkOuts.addQuantity(checkProduct, 1);
          } else
            checkOuts.addProduct(
                checkOuts.emptyProduct(),
                productDatabase.getProductRef(productNumber),
                1); // otherwise, add the product as normal.
          // another = JOptionPane.showConfirmDialog(null, "Would you like to add another item?",
          // "Continue", JOptionPane.YES_NO_OPTION);
          another =
              JOptionPane.showConfirmDialog(
                  null,
                  "You are purchasing "
                      + checkOuts.getCheckOut(1)
                      + "\nWould you like to add more items?"
                      + "\nHitting no will buy this cart",
                  "Cart",
                  JOptionPane.YES_NO_CANCEL_OPTION);
          if (another == JOptionPane.NO_OPTION) break; // let the user buy already.
          else if (another == JOptionPane.CANCEL_OPTION) {
            checkOuts = new CheckOut();
            sameUser = false;
            break;
          } else first = false; // make the add another product.
        }
        if (!admin && sameUser) {
          buyProducts(personNumber, checkOuts.getPrice());
          sameUser = false; // reset to enter PMKeyS
          JOptionPane.showMessageDialog(
              null, "Thank You for coming to TOC", "Thanks", JOptionPane.INFORMATION_MESSAGE);
        }
      }
      while (admin) {
        tempInput = showAdminMenu();
        if (tempInput == null || tempInput.length() < 1) {
          admin = false;
          break;
        } else if (tempInput.equals("add products")) {
          int done = 0;
          addToDatabase("product");
        } else if (tempInput.equals("remove products")) {
          error = 1;
          tempInput =
              showInputDialog(
                  "Enter the bar code of the item you would like to delete",
                  "Barcode",
                  JOptionPane.QUESTION_MESSAGE,
                  JOptionPane.OK_CANCEL_OPTION,
                  new String[0],
                  false);
          if (!isLong(tempInput)) continue; // check the input
          tempBarCode = Long.parseLong(tempInput);
          q2 = productDatabase.findProduct(tempBarCode);
          if (q2 != -1) {
            error = productDatabase.delProduct(q2); // recieve a one value on an error
            productDatabase.writeOutDatabase("productDatabase.txt");
          }
          if (error != 1 && q2 != 0) {
            JOptionPane.showMessageDialog(
                null,
                "The product that you asked for has been deleted.",
                "Success",
                JOptionPane.INFORMATION_MESSAGE);
          }
          if (error == 1) {
            JOptionPane.showMessageDialog(
                null,
                "The product that you asked for could not be found, please try again",
                "Error",
                JOptionPane.ERROR_MESSAGE);
            q2 = 0;
          }
        } else if (tempInput.equals("add people")) {
          addToDatabase("person");
        } else if (tempInput.equals("remove people")) {
          while (true) {
            error = 1;
            tempInput =
                showInputDialog(
                    "Enter the PMKeyS of the person you would like to delete",
                    "PMKeyS",
                    JOptionPane.QUESTION_MESSAGE,
                    JOptionPane.OK_CANCEL_OPTION,
                    new String[0],
                    false);
            if (tempInput == null) break;
            else if (!tempInput.equals("")
                && (tempInput.charAt(0) == 'c'
                    || tempInput.charAt(0) == 'n'
                    || tempInput.charAt(0) == 'C'
                    || tempInput.charAt(0) == 'N')) {
              tempInput = tempInput.substring(1);
            }
            if (!isLong(tempInput)) continue;
            tempBarCode = Long.parseLong(tempInput);
            q2 = personDatabase.findPerson(tempBarCode);
            if (q2 != -1 && tempBarCode != 7000000) {
              error = personDatabase.delPerson(q2); // recieve a one value on an error
              q2++; // confirmation output error
              personDatabase.writeOutDatabase("personDatabase.txt");
            }
            if (error != 1 && q2 != 0) {
              JOptionPane.showMessageDialog(
                  null,
                  "The person that you asked for has been deleted.",
                  "Success",
                  JOptionPane.INFORMATION_MESSAGE);
              break;
            }
            if (error == 1) {
              JOptionPane.showMessageDialog(
                  null,
                  "The person that you asked for could not be found, please try again",
                  "Error",
                  JOptionPane.ERROR_MESSAGE);
              q2 = 0;
            }
          }
        } else if (tempInput.equals("save product database")) {
          error = 0;
          error = productDatabase.adminWriteOutDatabase("adminProductDatabase.txt");
          if (error == 0) { // Inform the user of the write out.
            JOptionPane.showMessageDialog(
                null,
                "Your Database has been written to adminProductDatabase.txt",
                "Success",
                JOptionPane.INFORMATION_MESSAGE);
          } else { // inform the user of an error in writing the productDatabase to disk.
            JOptionPane.showMessageDialog(
                null,
                "Your productDatabase has not been written to disk as there was an error finding productDatabase.txt",
                "Error",
                JOptionPane.ERROR_MESSAGE);
          }
        } else if (tempInput.equals("save person database")) {
          error = 0;
          error = personDatabase.adminWriteOutDatabase("adminPersonDatabase.txt");
          if (error == 0) { // Inform the user of the write out.
            JOptionPane.showMessageDialog(
                null,
                "Your Database has been written to adminPersonDatabase.txt",
                "Success",
                JOptionPane.INFORMATION_MESSAGE);
          } else { // inform the user of an error in writing the productDatabase to disk.
            JOptionPane.showMessageDialog(
                null,
                "Your productDatabase has not been written to disk as there was an error finding personDatabase.txt",
                "Error",
                JOptionPane.ERROR_MESSAGE);
          }
        } else if (tempInput.equals("change product")) {
          int tempNumber = 0; // the number of the product in the database
          double tempPriceDouble = 0;
          tempInput =
              showInputDialog(
                  "Enter the bar code of the product you would like to edit",
                  "Barcode",
                  JOptionPane.INFORMATION_MESSAGE,
                  JOptionPane.OK_CANCEL_OPTION,
                  new String[0],
                  false);
          if (!isLong(tempInput)) {
            continue;
          }
          productNumber =
              productDatabase.findProduct(
                  Long.parseLong(tempInput)); // find the position of the product in the database
          if (productNumber == -1) {
            JOptionPane.showMessageDialog(
                null,
                "The product that you asked for does not exist",
                "Error",
                JOptionPane.ERROR_MESSAGE);
            continue;
          }
          // get the new detials of the product.
          tempInput =
              showInputDialog(
                  "Enter the new name for this product",
                  "Name",
                  JOptionPane.QUESTION_MESSAGE,
                  JOptionPane.OK_CANCEL_OPTION,
                  new String[0],
                  false);
          if (tempInput == null || tempInput.length() < 1) continue;
          tempName = tempInput;
          tempInput =
              showInputDialog(
                  "Enter the new item Price witout the dollar sign",
                  "Price",
                  JOptionPane.QUESTION_MESSAGE,
                  JOptionPane.OK_CANCEL_OPTION,
                  new String[0],
                  false);
          if (!isDouble(tempInput)) continue;
          tempPriceDouble = Double.parseDouble(tempInput);
          tempPriceDouble *= 100;
          tempProductPrice = (long) tempPriceDouble;
          tempInput =
              showInputDialog(
                  "Enter the new barCode of the product",
                  "Barcode",
                  JOptionPane.QUESTION_MESSAGE,
                  JOptionPane.OK_CANCEL_OPTION,
                  new String[0],
                  false);
          if (!isLong(tempInput)) continue;
          tempBarCode = Long.parseLong(tempInput);
          tempNumber =
              productDatabase.getNumber(
                  productNumber); // returns the number of this product in stock.
          productDatabase.delProduct(productNumber);
          added =
              productDatabase.setDatabaseProduct(
                  productDatabase.emptyProduct(), tempName, tempProductPrice, tempBarCode);
          if (added == 0) {
            JOptionPane.showMessageDialog(
                null,
                "Your product has been changed in the database",
                "Success",
                JOptionPane.INFORMATION_MESSAGE);
            productDatabase.setNumber(productDatabase.findProduct(tempBarCode), tempNumber);
          } else { // output when the user tries to edit the product to one that already exists.
            JOptionPane.showMessageDialog(
                null,
                "The new data that you tried to use for this product is already in use in the productDatabase",
                "Error",
                JOptionPane.ERROR_MESSAGE);
          }
          productDatabase.writeOutDatabase("productDatabase.txt");
        } else if (tempInput.equals("reset bills")) {
          another = 1;
          another =
              JOptionPane.showConfirmDialog(
                  null,
                  "Are you sure that you would like to reset the TOC bills?",
                  "Are you sure?",
                  JOptionPane.YES_NO_OPTION);
          if (another == 0) {
            personDatabase.resetBills(); // reset the bills
            personDatabase.writeOutDatabase("personDatabase.txt"); // save the new database to file
            JOptionPane.showMessageDialog(
                null, "Bills reset", "Success", JOptionPane.INFORMATION_MESSAGE);
          } else {
            JOptionPane.showMessageDialog(
                null, "Bills have not been reset.", "Error", JOptionPane.ERROR_MESSAGE);
          }
        } else if (tempInput.equals("print the person database to the screen")) {
          printDatabase("Person");
        } else if (tempInput.equals("print the product database to the screen")) { // see above
          printDatabase("Product");
        } else if (tempInput.equals("save databases to USB")) {
          error = 0;
          error = personDatabase.adminWriteOutDatabase("adminPersonDatabase.txt");
          error = productDatabase.adminWriteOutDatabase("adminProductDatabase.txt");
          if (error != 0)
            JOptionPane.showMessageDialog(
                null, "Could not copy the database", "Error", JOptionPane.ERROR_MESSAGE);
          try {
            ProcessBuilder pb = new ProcessBuilder("Copy.sh");
            Process p = pb.start();
            if (error == 0)
              JOptionPane.showMessageDialog(
                  null, "Database copied", "Success", JOptionPane.INFORMATION_MESSAGE);
          } catch (java.io.IOException e) {
            JOptionPane.showMessageDialog(
                null, "Could not copy the database", "Error", JOptionPane.ERROR_MESSAGE);
          }

        } else if (tempInput.equals("Enter stock counts (bulk)")) {
          int tempNumber = 0;
          for (i = 0;
              productDatabase.productExists(i);
              i++) { // for each product ask for the new number of items you have.
            tempInput =
                showInputDialog(
                    "You have "
                        + productDatabase.getNumber(i)
                        + " "
                        + productDatabase.getProductName(i)
                        + " left from last stocktake\n Including these, how many do you have now?",
                    "Stock Count",
                    JOptionPane.QUESTION_MESSAGE,
                    JOptionPane.OK_CANCEL_OPTION,
                    new String[0],
                    false);
            if (!isInteger(tempInput)) continue;
            tempNumber = Integer.parseInt(tempInput);
            productDatabase.setNumber(i, tempNumber);
          }
          productDatabase.writeOutDatabase("productDatabase.txt");
        } else if (tempInput.equals("Enter stock count (individual)")) {
          int tempNumber = 0;
          tempInput =
              showInputDialog(
                  "Enter the bar code of the product you would like to set",
                  "Barcode",
                  JOptionPane.QUESTION_MESSAGE,
                  JOptionPane.OK_CANCEL_OPTION,
                  new String[0],
                  false);
          if (!isLong(tempInput)) continue;
          tempBarCode = Long.parseLong(tempInput);
          productNumber =
              productDatabase.findProduct(
                  tempBarCode); // corrilate the product number with that in the database
          if (productNumber == -1) {
            JOptionPane.showMessageDialog(
                null,
                "The product that you asked for does not exist",
                "Error",
                JOptionPane.ERROR_MESSAGE);
            continue;
          }
          tempInput =
              showInputDialog(
                  "You had "
                      + productDatabase.getNumber(productNumber)
                      + " "
                      + productDatabase.getProductName(productNumber)
                      + " left from last stocktake\n Inclunding these, how many do you have now?",
                  "Stock Counts",
                  JOptionPane.QUESTION_MESSAGE,
                  JOptionPane.OK_CANCEL_OPTION,
                  new String[0],
                  false);
          // ask the user how many they had, and how many they now have
          if (!isInteger(tempInput)) continue;
          tempNumber = Integer.parseInt(tempInput);
          productDatabase.setNumber(
              productNumber, tempNumber); // enter all this into the database and write it out.
          productDatabase.writeOutDatabase("productDatabase.txt");
        } else if (tempInput.equals("change password")) {
          String passWd = "";
          passWd = getPassWd(true);
          String newPassWd = "";
          if (passWd != null) {
            passWd = getSecurePassword(passWd);
          }
          if (passWd != null
              && !"".equals(passWd)
              && passWd.equals(personDatabase.getPersonName(-2))) {
            passWd = getPassWd(true);
            newPassWd = getPassWd(false);
            if (passWd != null
                && newPassWd != null
                && !passWd.equals("")
                && !newPassWd.equals("")
                && passWd.equals(newPassWd)) {
              passWd = getSecurePassword(passWd);
              personDatabase.setAdminPassword(passWd);
              personDatabase.writeOutDatabase("personDatabase.txt");
              JOptionPane.showMessageDialog(
                  null, "Success, Password changed", "Success", JOptionPane.INFORMATION_MESSAGE);
              passwordField.setText("");
            } else {
              JOptionPane.showMessageDialog(
                  null, "Password incorrect", "Error", JOptionPane.ERROR_MESSAGE);
            }
          }

        } else if (tempInput.equals("close the program")) {
          personDatabase.writeOutDatabase(
              "personDatabase.txt"); // write any somehow missed changes out and then exit
          productDatabase.writeOutDatabase("productDatabase.txt");
          System.exit(0);
        } else {
          admin = false; // somehow picked an option which doesn't exist or simpily pressed cancel.
          break;
        }
      }
    }
  }