Mohammadjo123

java / 1

Jan 21st, 2020
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Collections;
  3. import java.util.List;
  4. import java.util.Scanner;
  5. import java.util.stream.Collectors;
  6.  
  7. public class FroggySquad {
  8. public static void main(String[] args) {
  9. Scanner scanner = new Scanner(System.in);
  10.  
  11. List<String> frogs = Arrays.stream(scanner.nextLine().split(" ")).collect(Collectors.toList());
  12.  
  13. while (true) {
  14. String[] tokens = scanner.nextLine().split(" ");
  15. String command = tokens[0];
  16. switch (command) {
  17. case "Join": {
  18. String name = tokens[1];
  19. frogs.add(name);
  20. }
  21. break;
  22. case "Jump": {
  23. String name = tokens[1];
  24. int index = Integer.parseInt(tokens[2]);
  25. if (isIndexValid(frogs, index)) {
  26. frogs.add(index, name);
  27. }
  28. }
  29. break;
  30. case "Dive": {
  31. int index = Integer.parseInt(tokens[1]);
  32. if (isIndexValid(frogs, index)) {
  33. frogs.remove(index);
  34. }
  35. }
  36. break;
  37. case "First": {
  38. int count = Integer.parseInt(tokens[1]);
  39. System.out.println(frogs.stream().limit(count).collect(Collectors.joining(" ")));
  40. }
  41. break;
  42. case "Last": {
  43. int count = Integer.parseInt(tokens[1]);
  44. System.out.println(frogs.stream().skip(Math.max(frogs.size() - count, 0)).collect(Collectors.joining(" ")));
  45. }
  46. break;
  47. case "Print": {
  48. String subCommand = tokens[1];
  49. if (subCommand.equals("Reversed")) {
  50. Collections.reverse(frogs);
  51. }
  52. System.out.println("Frogs: " + String.join(" ", frogs));
  53. }
  54. return;
  55. }
  56. }
  57. }
  58.  
  59. private static boolean isIndexValid(List<String> frogs, int index) {
  60. return index >= 0 && index < frogs.size();
  61. }
  62. }
Add Comment
Please, Sign In to add comment