Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class FroggySquad {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6.  
  7. List<String> frogsName = new ArrayList<>(Arrays.asList(scanner.nextLine().split(" ")));
  8.  
  9. String input = scanner.nextLine();
  10.  
  11. while (true) {
  12. String[] tokens = input.split(" ");
  13. String command = tokens[0];
  14. if (command.equals("Print")) {
  15. String typeOfPrint = tokens[1];
  16. printResult(frogsName, typeOfPrint);
  17. break;
  18. }
  19. switch (command) {
  20.  
  21. case "Join": {
  22. String name = tokens[1];
  23. if (!frogsName.contains(name)) {
  24. frogsName.add(name);
  25. }
  26. }
  27. break;
  28. case "Jump": {
  29. String name = tokens[1];
  30. int index = Integer.parseInt(tokens[2]);
  31. if (isValidIndex(frogsName, index)) {
  32. frogsName.add(index, name);
  33. }
  34. break;
  35. }
  36. case "Dive": {
  37. int index = Integer.parseInt(tokens[1]);
  38. if (isValidIndex(frogsName, index)) {
  39. frogsName.remove(index);
  40. }
  41. }
  42. break;
  43. case "First":
  44. case "Last":
  45. int count = Integer.parseInt(tokens[1]);
  46. firstLastPrint(frogsName, command, count);
  47. break;
  48. }
  49.  
  50.  
  51. input = scanner.nextLine();
  52. }
  53.  
  54.  
  55. }
  56.  
  57. private static void firstLastPrint(List<String> frogsName, String command, int count) {
  58. if (command.equals("First")) {
  59. if (count > frogsName.size()) {
  60. count = frogsName.size();
  61. }
  62. for (int i = 0; i < count; i++) {
  63. System.out.print(frogsName.get(i) + " ");
  64. }
  65. } else {
  66. int startCount = frogsName.size()-count;
  67. if (count > frogsName.size()) {
  68. startCount = 0;
  69. }
  70. for (int i = startCount; i < frogsName.size(); i++) {
  71. System.out.print(frogsName.get(i) + " ");
  72. }
  73. }
  74. System.out.println();
  75. }
  76.  
  77. private static void printResult(List<String> frogsName, String typeOfPrint) {
  78. if (typeOfPrint.equals("Reversed")) {
  79. Collections.reverse(frogsName);
  80.  
  81. }
  82. System.out.print("Frogs: ");
  83. System.out.println(String.join(" ", frogsName));
  84. }
  85.  
  86. private static boolean isValidIndex(List<String> frogsName, int index) {
  87. boolean isValid = false;
  88. if (index >= 0 && index < frogsName.size()) {
  89. isValid = true;
  90. }
  91. return isValid;
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement