Advertisement
Guest User

Contact List

a guest
Oct 29th, 2019
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.14 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.concurrent.DelayQueue;
  3.  
  4. public class changeName {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         List<String> names = new ArrayList<>();
  9.         String [] parts = scanner.nextLine().split("\\s+");
  10.         for (int i = 0; i < parts.length; i++) {
  11.             names.add(parts[i]);
  12.             }
  13.         String command = scanner.next();
  14.          while(!command.equals("Print")){
  15.  
  16.              if(command.equals("Add")){
  17.                  command = scanner.next();
  18.                 if(!names.contains(command)){
  19.                     names.add(command);
  20.                 }
  21.                 else{
  22.                     int commandIndex = scanner.nextInt();
  23.                     if(commandIndex>0 && commandIndex < names.size()){
  24.                         names.add(commandIndex,command);
  25.                     }
  26.                 }
  27.              }
  28.  
  29.              else if(command.equals("Remove")) {
  30.                  int commandIndex = scanner.nextInt();
  31.                  if (commandIndex > 0 && commandIndex < names.size()) {
  32.                      names.remove(commandIndex);
  33.                  }
  34.              }
  35.  
  36.                  else if (command.equals("Export")) {
  37.                  int startIndex = scanner.nextInt();
  38.                  int count = scanner.nextInt();
  39.                  for (int i = startIndex; i < count ; i++) {
  40.                      if(count>names.size()){
  41.                          count = names.size();
  42.                      }
  43.                      System.out.print(names.get(i) + " ");
  44.  
  45.                  }
  46.                  System.out.println();
  47.  
  48.                  }
  49.              command = scanner.next();
  50.  
  51.              }
  52.  
  53.         command = scanner.next();
  54.          if(command.equals("Normal")){
  55.              System.out.print("Contacts: ");
  56.            System.out.print(String.join(" ", names));
  57.          }
  58.         else if(command.equals("Reversed")){
  59.              Collections.reverse(names);
  60.              System.out.print("Contacts: ");
  61.              System.out.print(String.join(" ", names));
  62.          }
  63.  
  64.        }
  65.  
  66.  
  67.  
  68.  
  69.          }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement