Advertisement
Tsuki11

ContactList

Feb 21st, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.11 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.List;
  4. import java.util.Scanner;
  5.  
  6. public class ContactList {
  7.     public static void main(String[] args) {
  8.         Scanner scan = new Scanner(System.in);
  9.  
  10.         List<String> contactBook = getMyContacts(scan);
  11.  
  12.         String standard = "Print Normal";
  13.         String reverse = "Print Reversed";
  14.  
  15.         String input = scan.nextLine();
  16.         while (!standard.equals(input) && !reverse.equals(input)) {
  17.             String[] tokens = input.split("\\s+");
  18.             String cmdArg = tokens[0];
  19.             int index;
  20.             boolean isValid;
  21.             switch (cmdArg) {
  22.                 case "Add":
  23.                     String contact = tokens[1];
  24.                     index = Integer.parseInt(tokens[2]);
  25.                     isValid = checkExistence(index, contactBook);
  26.                     if (contactBook.contains(contact)) {
  27.                         if (isValid) {
  28.                             contactBook.add(index, contact);
  29.                         }
  30.                     } else {
  31.                         contactBook.add(contact);
  32.                     }
  33.                     break;
  34.                 case "Remove":
  35.                     index = Integer.parseInt(tokens[1]);
  36.                     isValid = checkExistence(index, contactBook);
  37.                     if (isValid) {
  38.                         contactBook.remove(index);
  39.                     }
  40.                     break;
  41.                 case "Export":
  42.                     index = Integer.parseInt(tokens[1]);
  43.                     int count = Integer.parseInt(tokens[2]);
  44.                     printWithParameters(index, count, contactBook);
  45.                     break;
  46.             }
  47.  
  48.             input = scan.nextLine();
  49.         }
  50.  
  51.         if (standard.equals(input)) {
  52.             printContactBook(contactBook);
  53.         } else {
  54.             Collections.reverse(contactBook);
  55.             printContactBook(contactBook);
  56.         }
  57.     }
  58.  
  59.     private static void printContactBook(List<String> contactBook) {
  60.         System.out.print("Contacts: ");
  61.         for (String s : contactBook) {
  62.             System.out.print(s + " ");
  63.         }
  64.     }
  65.  
  66.     private static void printWithParameters(int index, int count, List<String> contactBook) {
  67.         int counter = 0;
  68.         for (int i = index; i < contactBook.size(); i++) {
  69.             String currElement = contactBook.get(i);
  70.             System.out.print(currElement + " ");
  71.             counter++;
  72.             if (counter == count) {
  73.                 break;
  74.             }
  75.         }
  76.         System.out.println();
  77.     }
  78.  
  79.     private static boolean checkExistence(int contactOrIndex, List<String> list) {
  80.         if (contactOrIndex >= 0 && contactOrIndex < list.size()) {
  81.             return true;
  82.         }
  83.         return false;
  84.     }
  85.  
  86.     private static List<String> getMyContacts(Scanner scan) {
  87.         String[] input = scan.nextLine().split("\\s+");
  88.         List<String> list = new ArrayList<>();
  89.         for (String currentElement : input) {
  90.             list.add(currentElement);
  91.         }
  92.         return list;
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement