Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class StaticArrayList_Main {
- public static void main(String[] args) {
- StaticArrayList_Methods shoppingList = new StaticArrayList_Methods();
- Scanner sc = new Scanner(System.in);
- while (true) {
- System.out.println();
- System.out.println("For ADD choose 1, for REMOVE choose 2, for END choose 3");
- int choice = sc.nextInt();
- if (choice == 1) {
- System.out.println("The element you want to add:");
- String n = sc.next();
- if (shoppingList.contains(n) == false) {
- shoppingList.add(n);
- System.out.println();
- System.out.println("Now your list contains:");
- getList(shoppingList);
- continue;
- } else {
- System.out.println("The element is already in the list");
- continue;
- }
- }
- if (choice == 2) {
- System.out.println("The element you want to remove:");
- String n = sc.next();
- if (shoppingList.contains(n)) {
- shoppingList.remove(shoppingList.indexOf(n));
- System.out.println();
- System.out.println("Now your list contains:");
- getList(shoppingList);
- continue;
- } else {
- System.out.println("No element to remove");
- continue;
- }
- }
- if (choice == 3) {
- System.out.println();
- System.out.println("Now your list contains:");
- getList(shoppingList);
- System.out.println("Program ended");
- break;
- }
- } // end while
- sc.close();
- } // end MAIN
- static void getList(StaticArrayList_Methods shoppingList) {
- for (int i = 0; i < shoppingList.getLength(); i++) {
- System.out.println(shoppingList.elementAt(i));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement