Advertisement
Guest User

Java ContactList

a guest
Nov 1st, 2019
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5.  
  6. public class Contacts {
  7. public static void main(String[] args) {
  8. Scanner scanner = new Scanner(System.in);
  9.  
  10. List <String> contacts = Arrays.stream(scanner.nextLine().split("\\s+")).collect(Collectors.toList());
  11. List <String> printListExport = new ArrayList<>();
  12.  
  13. String [] commands = scanner.nextLine().split("\\s+");
  14.  
  15. while (!commands[0].equals("Print")) {
  16.  
  17. switch (commands[0]) {
  18. case "Add":
  19. String name = commands [1];
  20. int index = Integer.parseInt(commands[2]);
  21. if (!contacts.contains(name)) {
  22.  
  23. contacts.add(commands[1]);
  24. } else if (0 <= index && index < contacts.size()){
  25. contacts.add(index, name);
  26. }
  27.  
  28. break;
  29. case "Remove":
  30. int indexRemove = Integer.parseInt(commands[1]);
  31. if (0 <= indexRemove && indexRemove < contacts.size()) {
  32. contacts.remove(indexRemove);
  33. }
  34. break;
  35. case "Export":
  36. int startIndex = Integer.parseInt(commands[1]);
  37. int count = Integer.parseInt(commands[2]);
  38. count += startIndex;
  39.  
  40. if (0<= startIndex && startIndex < contacts.size() && count > 0) {
  41.  
  42. if (count > contacts.size()) {
  43. count = contacts.size();
  44. }
  45. for (int i = startIndex; i < count; i++) {
  46. printListExport.add(contacts.get(i));
  47. }
  48. System.out.println(String.join(" ", printListExport));
  49. }
  50. printListExport.clear();
  51. break;
  52. default:
  53. }
  54. commands = scanner.nextLine().split(" ");
  55. }
  56.  
  57. if (commands[1].equals("Normal")) {
  58. Collections.sort(contacts);
  59. System.out.println("Contacts: " + String.join(" ", contacts));
  60. }
  61. if (commands[1].equals("Reversed")) {
  62. Collections.reverse(contacts);
  63. System.out.println("Contacts: " + String.join(" ", contacts));
  64. }
  65.  
  66.  
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement