repl.it
@vedprad1/

GroceryList

Java

If you are bad at memorizing your grocery list, this program will allow you to easily access your list, add items, remove items, and more!

fork
loading
Files
  • Main.java
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.util.Scanner;
class Main {
  public static void main(String[] args) {
    Scanner keyBoard = new Scanner(System.in);
    String list[];
    list = new String[0];
    String temp[];
    System.out.println("Welcome to the Grocery List!");
    int i;
    int j;
    int input = 0;
    int terminate;
    int remove;
    String item;
    while (input != 5) {
      System.out.println("Press a number:\n1: View your grocery list.\n2: Add an item.\n3: Remove an item.\n4: Clear the grocery list.\n5: Exit this program.");
      input = keyBoard.nextInt();
      if (input == 1) {
        System.out.print("Your grocery list currently has: ");
        for (i = 0; i < list.length; i++) {
          System.out.print(list[i] + ", ");
        }
        System.out.println();
      }
      else if (input == 2) {
        temp = new String[list.length + 1];
        for (i = 0; i < list.length; i++){
        temp[i] = list[i];
        }
        list = temp;
        System.out.println("What would you like to add?");
        keyBoard.nextLine();
        item = keyBoard.nextLine();
        list[list.length - 1] = item;
      }
      else if (input == 3) {
        System.out.println("Your grocery list currently has: ");
        for (i = 0; i < list.length; i++) {
          j = i + 1;
          System.out.println(j + ": " + list[i]);
        }
        System.out.println("Find what number in your grocery list the item you want to remove is at (starting from one).");
        remove = keyBoard.nextInt();
        remove--;
        temp = new String[list.length - 1];
        for (i = 0; i < remove; i++) {
          temp[i] = list[i];
        }
        for (i = remove; i < temp.length; i++) {
          temp[i] = list[i+1];
        }
        list = temp;
      }
      else if (input == 4) {
        list = new String[0];
        System.out.println("Grocery List has been cleared!");
      }
      else {
        System.out.println("Are you sure you want to leave this program? You will lose all history. Press 0 to terminate the program, or press 1 to continue the program.");
        terminate = keyBoard.nextInt();
        if (terminate == 1) {
          input = 1;
        }
        else {
          System.out.println("Program terminated!");
        }
      }
    }
  }
}
?