Ejemplo n.º 1
0
  public static void insertFilm(
      Scanner sc,
      ProductRepository pr,
      CategoryRepository cr,
      ActorRepository acr,
      AuthorRepository aur,
      StaffRepository sr,
      UserRepository ur)
      throws StorageException {
    boolean checkValidInfo = true;
    String productName = "";
    String description = "";
    String cost = "";
    String rrp = "";
    String rating = "";
    String agelimit = "";
    String release = "";
    String actorsChoise = "";
    String categoryChoise = "";

    List<String> categoriesToAdd;
    Set<Integer> addCategoriesWithoutDoubles;

    List<String> actorsToAdd;
    Set<Integer> addActorsWithoutDoubles;

    Set<Integer> listOfCategoryIds;

    do {
      do {
        checkValidInfo = false;

        System.out.println("Insert film name:");
        productName = sc.nextLine();
        System.out.println();

        if (Check.isNumeric(productName)) {
          checkValidInfo = true;
          System.out.println("You can't have a numeric film name");
          System.out.println();
        } else if (productName.equals("")) {
          checkValidInfo = true;
          System.out.println("You haven't inserted any film name yet");
          System.out.println();
        }
      } while (checkValidInfo);

      do {
        checkValidInfo = false;

        System.out.println("Insert description:");
        description = sc.nextLine();
        System.out.println();

        if (Check.isNumeric(description)) {
          checkValidInfo = true;
          System.out.println("You can't have a numeric film description");
          System.out.println();
        }
      } while (checkValidInfo);

      do {
        checkValidInfo = false;

        System.out.println("Insert cost:");
        cost = sc.nextLine();
        System.out.println();

        if (!Check.isNumeric(cost)) {
          checkValidInfo = true;
          System.out.println("You haven't used a numeric value for cost");
          System.out.println();
        }
      } while (checkValidInfo);

      do {
        checkValidInfo = false;

        System.out.println("Insert rrp:");
        rrp = sc.nextLine();
        System.out.println();

        if (!Check.isNumeric(rrp)) {
          checkValidInfo = true;
          System.out.println("You haven't used a numeric value for rrp");
          System.out.println();
        }
      } while (checkValidInfo);

      do {
        checkValidInfo = false;

        System.out.println("Insert rating:");
        rating = sc.nextLine();
        System.out.println();

        if (!Check.isNumeric(rating)) {
          checkValidInfo = true;
          System.out.println("You haven't used a numeric value for rating");
          System.out.println();
        }
      } while (checkValidInfo);

      do {
        checkValidInfo = false;

        System.out.println("Insert agelimit:");
        agelimit = sc.nextLine();
        System.out.println();

        if (!Check.isNumeric(agelimit)) {
          checkValidInfo = true;
          System.out.println("You haven't used a numeric value for agelimit");
          System.out.println();
        }
      } while (checkValidInfo);

      do {
        checkValidInfo = false;

        System.out.println("Insert release year:");
        release = sc.nextLine();
        System.out.println();

        if (!Check.isNumeric(release)) {
          checkValidInfo = true;
          System.out.println("You haven't used a numeric value for release year");
          System.out.println();
        }
      } while (checkValidInfo);

      do {
        checkValidInfo = false;

        for (Actor a : acr.getAllActors()) {
          System.out.println("ActorId: " + a.getActorId());
          System.out.println("Actor name: " + a.getFirstName() + " " + a.getSurName());
          System.out.println();
        }

        System.out.println("Insert actors in the film (with actorId):");
        actorsChoise = sc.nextLine();

        actorsToAdd = Arrays.asList(actorsChoise.split(","));
        addActorsWithoutDoubles = new TreeSet<>();

        for (String s : actorsToAdd) {
          if (!Check.isNumeric(s.trim())) {
            checkValidInfo = true;
            System.out.println();
            System.out.println("You have inserted invalid actorId");
            break;
          } else if (acr.getActorOnActorId(Integer.parseInt(s.trim())).size() == 0) {
            checkValidInfo = true;
            System.out.println();
            System.out.println("You have inserted an actorId that is not in the repository");
            break;
          }
          addActorsWithoutDoubles.add(Integer.parseInt(s.trim()));
        }

        System.out.println();
      } while (checkValidInfo);

      do {
        checkValidInfo = false;

        do {
          checkValidInfo = false;
          int counter = 0;

          do {
            for (Category c : cr.getAllCategories()) {
              counter++;
              System.out.println(counter + ": " + c.getCategoryName());
            }

            checkValidInfo = false;

            System.out.println();

            System.out.println("Insert the categories you want the film to be associated with:");
            categoryChoise = sc.nextLine();
            System.out.println();

            if (categoryChoise.equals("")) {
              checkValidInfo = true;
              System.out.println("You haven't inserted any category yet");
              System.out.println();
            }
          } while (checkValidInfo);

          categoriesToAdd = Arrays.asList(categoryChoise.split(","));
          addCategoriesWithoutDoubles = new TreeSet<>();

          for (String s : categoriesToAdd) {
            String tempS = s.trim();

            if (Check.isNumeric(tempS)) {
              int i = Integer.parseInt(tempS);
              addCategoriesWithoutDoubles.add(i - 1);
            } else {
              checkValidInfo = true;
              System.out.println(
                  "You inserted some invalid data when trying to generate categories");
              break;
            }
          }
        } while (checkValidInfo);

        categoryChoise = "";
        listOfCategoryIds = new TreeSet<>();

        for (int i : addCategoriesWithoutDoubles) {
          listOfCategoryIds.add(cr.getAllCategories().get(i).getCategoryId());
          categoryChoise += cr.getAllCategories().get(i).getCategoryName() + ", ";
        }

        categoryChoise = categoryChoise.substring(0, categoryChoise.length() - 2);

        actorsChoise = "";

        for (int i : addActorsWithoutDoubles) {
          actorsChoise +=
              acr.getActorOnActorId(i).get(0).getFirstName()
                  + " "
                  + acr.getActorOnActorId(i).get(0).getSurName()
                  + ", ";
        }

        actorsChoise = actorsChoise.substring(0, actorsChoise.length() - 2);
      } while (checkValidInfo);

      do {
        checkValidInfo = false;

        System.out.println("You have inserted following values to your new film:");
        System.out.println("Product name: " + productName);
        System.out.println("Description: " + description);
        System.out.println("Cost: " + cost);
        System.out.println("Rrp: " + rrp);
        System.out.println("Rating: " + rating);
        System.out.println("Agelimit: " + agelimit);
        System.out.println("Release: " + release);
        System.out.println("Actors: " + actorsChoise);
        System.out.println("Categories: " + categoryChoise);

        System.out.println();

        System.out.println("If you want to save this film, and exit to main menu, press Y.");
        System.out.println("If you want to save this film, and make another, press A.");
        System.out.println("If you don't want to save this film, but make another, press X,");
        System.out.println("If you don't want to save this film, and exit to main menu, press Z,");

        String choises = sc.nextLine();
        choises = choises.toLowerCase();

        if (choises.equals("y")) {
          checkValidInfo = false;

          double doubleCost = Double.parseDouble(cost);
          double doubleRrp = Double.parseDouble(rrp);
          int intRating = Integer.parseInt(rating);
          int intAgelimit = Integer.parseInt(agelimit);

          Film f = new Film();

          f.setProductName(productName);
          f.setDescription(description);
          f.setCost(doubleCost);
          f.setRrp(doubleRrp);
          f.setRating(intRating);
          f.setAgelimit(intAgelimit);
          f.setReleaseYear(release);

          pr.insertFilm(f, listOfCategoryIds, addActorsWithoutDoubles);

          System.out.println();
        } else if (choises.equals("a")) {
          checkValidInfo = true;

          double doubleCost = Double.parseDouble(cost);
          double doubleRrp = Double.parseDouble(rrp);
          int intRating = Integer.parseInt(rating);
          int intAgelimit = Integer.parseInt(agelimit);

          Film f = new Film();

          f.setProductName(productName);
          f.setDescription(description);
          f.setCost(doubleCost);
          f.setRrp(doubleRrp);
          f.setRating(intRating);
          f.setAgelimit(intAgelimit);
          f.setReleaseYear(release);

          pr.insertFilm(f, listOfCategoryIds, addActorsWithoutDoubles);

          System.out.println();

          break;
        } else if (choises.equals("x")) {
          checkValidInfo = true;

          System.out.println();

          break;
        } else if (choises.equals("z")) {
          checkValidInfo = false;

          System.out.println();
        } else {
          checkValidInfo = true;

          System.out.println("You haven't pressed Y, A, X or Z");
          System.out.println();
        }
      } while (checkValidInfo);
    } while (checkValidInfo);

    MainConsole.mainWindow(pr, cr, acr, aur, sr, ur);
  }