Пример #1
0
 public void removePlayer() {
   String playerName = con.readLine("Enter player's name: ");
   String teamName = con.readLine("Enter player's team: ");
   Team t = (Team) cache.get(encode(teamName));
   if (t != null) {
     t.removePlayer(playerName);
     cache.put(encode(teamName), t);
   } else {
     con.printf(msgTeamMissing, teamName);
   }
 }
Пример #2
0
  public static void main(String[] args) {
    Team baseballTeam = new Team(); // Creates a new Team object
    // The following print statement prints the menu
    System.out.println(
        "A) Add Player "
            + "\n"
            + "G) Get Player"
            + "\n"
            + "L) Get Leader in a stat"
            + "\n"
            + "R) Remove Player"
            + "\n"
            + "P) Print All Players"
            + "\n"
            + "S) Size"
            + "\n"
            + "H) Update Hits"
            + "\n"
            + "E) Update Errors"
            + "\n"
            + "Q) Quit ");
    Scanner scan = new Scanner(System.in);
    String menuOption = ""; // Creates a variable to hold user input
    while (!menuOption.equalsIgnoreCase(
        "Q")) // Loop will continue until user inputs "Q" for their menu option
    {
      System.out.print("\n" + "Select a menu option: "); // Prompts user for menu option
      menuOption = scan.nextLine();

      /**
       * If the selected option is "A", prompt user for name, hits, errors and position. Checks to
       * see if hits and errors are greater than or equal to zero. If condition not met, prompts
       * user for new input. Attempt to create a player using input. Throws an exception when team
       * is full or position is not within valid range.
       */
      if (menuOption.equalsIgnoreCase("A")) {
        System.out.print("\n" + "Enter the player's name: ");
        String name = scan.nextLine();
        System.out.print("Enter the number of hits: ");
        int hits = scan.nextInt();
        while (hits < 0) {
          System.out.print("Please enter a value greater than or equal to 0." + "\n");
          System.out.print("Enter the number of hits: ");
          hits = scan.nextInt();
          scan.nextLine();
        }
        System.out.print("Enter the number of errors: ");
        int errors = scan.nextInt();
        while (errors < 0) {
          System.out.println("Please enter a value greater than or equal to 0." + "\n");
          System.out.print("Enter the number of errors: ");
          errors = scan.nextInt();
          scan.nextLine();
        }
        System.out.print("Enter the position: ");
        int position = scan.nextInt();
        scan.nextLine();
        try {
          baseballTeam.addPlayer(new Player(name, hits, errors), position);
          System.out.println("Player added: " + baseballTeam.getPlayer(position));
        } catch (FullTeamException e) {
          System.out.println(e.getMessage());
        } catch (IllegalArgumentException e) {
          System.out.println(e.getMessage());
        }
      }

      /**
       * Prints the player in the position requested, prompts user for position. Throws exception
       * when position not within valid range.
       */
      if (menuOption.equalsIgnoreCase("G")) {
        System.out.print("Enter the position of the Player requested:");
        int position = scan.nextInt();
        scan.nextLine();
        try {
          System.out.println(baseballTeam.getPlayer(position));
        } catch (IllegalArgumentException e) {
          System.out.println(e.getMessage());
        }
      }

      /**
       * Prints the player who has the best score in a given statistic, prompts user for statistic
       * Throws exception when user requests stat other than "hits" or "errors" Throws exception
       * when there are no players in the Team.
       */
      if (menuOption.equalsIgnoreCase("L")) {
        System.out.print("Enter the stat:");
        String stat = scan.nextLine();
        try {
          if (stat.equalsIgnoreCase("hits"))
            System.out.println("Leader in hits: " + baseballTeam.getLeader(stat).toString());
          else System.out.println("Leader in errors: " + baseballTeam.getLeader(stat).toString());
        } catch (IllegalArgumentException e) {
          System.out.println(e.getMessage());
        } catch (NullPointerException e) {
          System.out.println("There are currently no players in the team.");
        }
      }

      /**
       * Removes a player from a given position. Prompts user for the position. Throws exception
       * when position requested is not in valid range.
       */
      if (menuOption.equalsIgnoreCase("R")) {
        System.out.print("Enter the position: ");
        int position = scan.nextInt();
        scan.nextLine();
        try {
          if (baseballTeam.getPlayer(position) == null)
            System.out.println("No player exists at that position");
          else {
            baseballTeam.removePlayer(position);
            System.out.println("Player removed at position " + position);
          }
        } catch (IllegalArgumentException e) {
          System.out.println(e.getMessage());
        }
      }

      /** Prints all the players in the team and their stats in a nice format. */
      if (menuOption.equalsIgnoreCase("P")) {
        System.out.print("\n");
        baseballTeam.PrintAllPlayers();
      }

      /** Prints the number of players currently in the team. */
      if (menuOption.equalsIgnoreCase("S")) {
        System.out.println("There are " + baseballTeam.size() + " player(s) in the current Team");
      }

      /**
       * Updates the number of hits of a player. Prompts user for player's name. Throws exception
       * when player nonexistent.
       */
      if (menuOption.equalsIgnoreCase("H")) {
        System.out.print("\n" + "Enter the player to update: ");
        String name = scan.nextLine();
        System.out.print("Enter the new number of hits:");
        int hits = scan.nextInt();
        scan.nextLine();
        while (hits < 0) {
          System.out.print("Please enter a value greater than or equal to 0");
          System.out.print("\n" + "Enter the new number of hits:");
          hits = scan.nextInt();
          scan.nextLine();
        }
        try {
          baseballTeam.getPlayerByName(name).setHits(hits);
          System.out.println("Updated " + name + " hits");
        } catch (NullPointerException e) {
          System.out.println("The player is not currently in the Team");
        }
      }

      /**
       * Updates the number of errors of a player. Prompts user for player's name. Throws exception
       * when player nonexistent.
       */
      if (menuOption.equalsIgnoreCase("E")) {
        System.out.print("\n" + "Enter the player to update: ");
        String name = scan.nextLine();
        System.out.print("Enter the new number of errors:");
        int errors = scan.nextInt();
        scan.nextLine();
        while (errors < 0) {
          System.out.print("Please enter a value greater than or equal to 0");
          System.out.print("\n" + "Enter the new number of errors:");
          errors = scan.nextInt();
          scan.nextLine();
        }

        try {
          baseballTeam.getPlayerByName(name).setErrors(errors);
          System.out.println("Updated " + name + " errors");
        } catch (NullPointerException e) {
          System.out.println("The player is not currently in the Team");
        }
      }
    }
    scan.close();
    System.out.println("Program terminated.");
  }
Пример #3
0
    void drawScreen(Team teamPassed) {

      if (teamPassed != null) {
        // We're editing a current team
        team = teamPassed;
        for (Player player : team.getPlayers()) {
          JPanel thisPlayer = player.playerPreview();
          JButton modify = new JButton(ResourceRetriever.getImage("edit.png", 16, 16));
          modify.setMargin(new Insets(0, 0, 0, 0));
          modify.addActionListener(
              (ActionEvent e) -> {
                new ModifyPlayerPopup(player, this, null);
              });
          JButton delete = new JButton(ResourceRetriever.getImage("X.png", 16, 16));
          delete.setMargin(new Insets(0, 0, 0, 0));
          delete.addActionListener(
              (ActionEvent e) -> {
                Object[] options = {"Remove Player from team", "Delete Player", "Cancel"};
                JFrame frame = new JFrame();
                int answer =
                    JOptionPane.showOptionDialog(
                        frame,
                        "Are you sure you would like to delete \"" + player.name + "?\"",
                        "Confirm Delete",
                        JOptionPane.YES_NO_OPTION,
                        JOptionPane.QUESTION_MESSAGE,
                        Global.QUESTIONPIC,
                        options,
                        options[2]);
                if (answer == 0) {
                  team.removePlayer(player);
                  refresh();
                } else if (answer == 1) {
                  // Teams without uniqueID's don't get added to players yet,
                  //   so the player needs to be manually removed.
                  if (team.globalIndex == -1) {
                    team.removePlayer(player);
                  }
                  player.delete();
                  refresh();
                }
              });
          thisPlayer.add(delete, "east");
          thisPlayer.add(modify, "east");
          thisPlayer.setBorder(BorderFactory.createLineBorder(Color.black));
          players.add(thisPlayer, "growx");
        }
        name.setText(team.name);
      } else {
        // This is a new Team
        team = new Team("New Team");
        name.setText(team.name);
        name.setFont(new Font("Ariel", Font.PLAIN, 20));
        name.select(0, name.getText().length());
      }

      players.setBorder(BorderFactory.createLineBorder(Color.black));

      south.add(addPlayers);
      south.add(new JLabel("Players"));
      south.add(players, "span, grow");
      south.add(cancel, "span, right, split 2");
      south.add(save, "span, right");

      Eve.add(name);
      Eve.add(south);
    }