Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- import java.util.List;
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- List<String> numbers = Arrays.stream(sc.nextLine().split("\\s")).collect(Collectors.toList());
- String command = sc.nextLine();
- String[] tokens = command.split("\\s");
- while(!tokens[0].equals("Print")) {
- switch(tokens[0]) {
- case "Add":
- String contact = tokens[1];
- int index = Integer.parseInt(tokens[2]);
- if (numbers.indexOf(contact) == -1) {
- numbers.add(contact);
- } else if (index >= 0 && index < numbers.size()) {
- numbers.add(index, contact);
- }
- break;
- case "Remove":
- int index2 = Integer.parseInt(tokens[1]);
- if (index2 >= 0 && index2 < numbers.size()) {
- numbers.remove(index2);
- }
- break;
- case "Export":
- int startIndex = Integer.parseInt(tokens[1]);
- int count = Integer.parseInt(tokens[2]);
- if (startIndex > -1) {
- if (startIndex + count < numbers.size()) {
- for (int i = startIndex; i < startIndex + count; i++) {
- System.out.print(numbers.get(i) + " ");
- }
- // System.out.println();
- } else if (startIndex + count >= numbers.size()) {
- for (int i = startIndex; i < numbers.size(); i++) {
- System.out.print(numbers.get(i) + " ");
- }
- }
- System.out.println();
- }
- break;
- }
- command = sc.nextLine();
- tokens = command.split(" ");
- }
- if(tokens[0].equals("Print")) {
- if (tokens[1].equals("Normal")) {
- System.out.print("Contacts: ");
- System.out.print(String.join((" "), numbers));
- } else if (tokens[1].equals("Reversed")) {
- Collections.reverse(numbers);
- System.out.print("Contacts: ");
- System.out.print(String.join((" "), numbers));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement