Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  3.  
  4. public class StaticArrayList_Main {
  5.  
  6. public static void main(String[] args) {
  7.  
  8. StaticArrayList_Methods shoppingList = new StaticArrayList_Methods();
  9. Scanner sc = new Scanner(System.in);
  10.  
  11. while (true) {
  12. System.out.println();
  13. System.out.println("For ADD choose 1, for REMOVE choose 2, for END choose 3");
  14. int choice = sc.nextInt();
  15.  
  16. if (choice == 1) {
  17. System.out.println("The element you want to add:");
  18. String n = sc.next();
  19.  
  20. if (shoppingList.contains(n) == false) {
  21. shoppingList.add(n);
  22.  
  23. System.out.println();
  24. System.out.println("Now your list contains:");
  25. getList(shoppingList);
  26.  
  27. continue;
  28.  
  29. } else {
  30. System.out.println("The element is already in the list");
  31. continue;
  32. }
  33. }
  34.  
  35. if (choice == 2) {
  36. System.out.println("The element you want to remove:");
  37. String n = sc.next();
  38.  
  39. if (shoppingList.contains(n)) {
  40. shoppingList.remove(shoppingList.indexOf(n));
  41.  
  42. System.out.println();
  43. System.out.println("Now your list contains:");
  44. getList(shoppingList);
  45.  
  46. continue;
  47.  
  48. } else {
  49. System.out.println("No element to remove");
  50. continue;
  51. }
  52. }
  53.  
  54. if (choice == 3) {
  55. System.out.println();
  56. System.out.println("Now your list contains:");
  57. getList(shoppingList);
  58.  
  59. System.out.println("Program ended");
  60. break;
  61. }
  62.  
  63. } // end while
  64.  
  65. sc.close();
  66.  
  67. } // end MAIN
  68.  
  69. static void getList(StaticArrayList_Methods shoppingList) {
  70. for (int i = 0; i < shoppingList.getLength(); i++) {
  71. System.out.println(shoppingList.elementAt(i));
  72. }
  73. }
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement