|
| 1 | +import java.lang.reflect.Array; |
| 2 | +import java.util.ArrayList; |
| 3 | +import java.util.Collections; |
| 4 | +import java.util.Scanner; |
| 5 | + |
| 6 | +public class ShoppingList |
| 7 | +{ |
| 8 | + private Scanner in = new Scanner(System.in); |
| 9 | + private int total = 0; |
| 10 | + private ArrayList <String> addToList = new ArrayList<>(); |
| 11 | + |
| 12 | + private void run() |
| 13 | + { |
| 14 | + ArrayList<String> myList = new ArrayList<>(); |
| 15 | + Scanner in = new Scanner(System.in); |
| 16 | + String command; |
| 17 | + |
| 18 | + |
| 19 | + do { |
| 20 | + System.out.println("Enter Add to add item, " + |
| 21 | + "Print to print list, " + |
| 22 | + "Remove to remove, " + |
| 23 | + "Clear to clear list, " + |
| 24 | + "Sort or Exit"); |
| 25 | + |
| 26 | + command = in.nextLine(); |
| 27 | + |
| 28 | + if(command.equalsIgnoreCase("Add")) |
| 29 | + { |
| 30 | + add(); |
| 31 | + //System.out.println(addToList.size()); |
| 32 | + } |
| 33 | + else if(command.equalsIgnoreCase("Remove")) |
| 34 | + { |
| 35 | + remove(); |
| 36 | + |
| 37 | + //System.out.println(addToList.size()); |
| 38 | + } |
| 39 | + else if(command.equalsIgnoreCase("Print")) |
| 40 | + { |
| 41 | + printList(addToList); |
| 42 | + } |
| 43 | + else if (command.equalsIgnoreCase("Clear")) |
| 44 | + { |
| 45 | + clear(); |
| 46 | + } |
| 47 | + else if (command.equalsIgnoreCase("Sort")) |
| 48 | + { |
| 49 | + sort(); |
| 50 | + } |
| 51 | + else |
| 52 | + { |
| 53 | + messageError(); |
| 54 | + } |
| 55 | + //System.out.println("The current total is: " + total); |
| 56 | + } |
| 57 | + while(!command.equalsIgnoreCase("exit")); |
| 58 | + { |
| 59 | + System.out.println("Now exiting..."); |
| 60 | + System.out.println("Goodbye"); |
| 61 | + |
| 62 | + } |
| 63 | + printList(addToList); |
| 64 | + |
| 65 | + |
| 66 | + } |
| 67 | + private void add() |
| 68 | + { |
| 69 | + System.out.print("Enter the item to be added: "); |
| 70 | + String item = in.nextLine(); |
| 71 | + addToList.add(item); |
| 72 | + System.out.println("Item added: " + item); |
| 73 | + } |
| 74 | + |
| 75 | + private void remove() |
| 76 | + { |
| 77 | + System.out.println("Enter the item number to removed: "); |
| 78 | + int number = in.nextInt(); |
| 79 | + in.nextLine(); |
| 80 | + addToList.remove(number); |
| 81 | + |
| 82 | + System.out.println("Item removed: " + number); |
| 83 | + } |
| 84 | + |
| 85 | + private void printList(ArrayList<String> print) |
| 86 | + { |
| 87 | + for(int i = 0; i < print.size(); i++) |
| 88 | + { |
| 89 | + System.out.println(i + ". " + print.get(i)); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private void clear() |
| 94 | + { |
| 95 | + addToList.clear(); |
| 96 | + } |
| 97 | + |
| 98 | + private void messageError() |
| 99 | + { |
| 100 | + System.out.println("Invalid entry. Please try again."); |
| 101 | + } |
| 102 | + |
| 103 | + private void sort() |
| 104 | + { |
| 105 | + Collections.sort(addToList); |
| 106 | + } |
| 107 | + |
| 108 | + public static void main(String[] args) |
| 109 | + { |
| 110 | + ShoppingList myShoppingList = new ShoppingList(); |
| 111 | + myShoppingList.run(); |
| 112 | + } |
| 113 | +} |
0 commit comments