Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class FroggySquad {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<String> frogsName = new ArrayList<>(Arrays.asList(scanner.nextLine().split(" ")));
- String input = scanner.nextLine();
- while (true) {
- String[] tokens = input.split(" ");
- String command = tokens[0];
- if (command.equals("Print")) {
- String typeOfPrint = tokens[1];
- printResult(frogsName, typeOfPrint);
- break;
- }
- switch (command) {
- case "Join": {
- String name = tokens[1];
- if (!frogsName.contains(name)) {
- frogsName.add(name);
- }
- }
- break;
- case "Jump": {
- String name = tokens[1];
- int index = Integer.parseInt(tokens[2]);
- if (isValidIndex(frogsName, index)) {
- frogsName.add(index, name);
- }
- break;
- }
- case "Dive": {
- int index = Integer.parseInt(tokens[1]);
- if (isValidIndex(frogsName, index)) {
- frogsName.remove(index);
- }
- }
- break;
- case "First":
- case "Last":
- int count = Integer.parseInt(tokens[1]);
- firstLastPrint(frogsName, command, count);
- break;
- }
- input = scanner.nextLine();
- }
- }
- private static void firstLastPrint(List<String> frogsName, String command, int count) {
- if (command.equals("First")) {
- if (count > frogsName.size()) {
- count = frogsName.size();
- }
- for (int i = 0; i < count; i++) {
- System.out.print(frogsName.get(i) + " ");
- }
- } else {
- int startCount = frogsName.size()-count;
- if (count > frogsName.size()) {
- startCount = 0;
- }
- for (int i = startCount; i < frogsName.size(); i++) {
- System.out.print(frogsName.get(i) + " ");
- }
- }
- System.out.println();
- }
- private static void printResult(List<String> frogsName, String typeOfPrint) {
- if (typeOfPrint.equals("Reversed")) {
- Collections.reverse(frogsName);
- }
- System.out.print("Frogs: ");
- System.out.println(String.join(" ", frogsName));
- }
- private static boolean isValidIndex(List<String> frogsName, int index) {
- boolean isValid = false;
- if (index >= 0 && index < frogsName.size()) {
- isValid = true;
- }
- return isValid;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement