Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
99 lines (85 loc) · 3.3 KB
/
Copy pathMain.java
File metadata and controls
99 lines (85 loc) · 3.3 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.juhuyoon;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
private static Scanner scanner = new Scanner(System.in);
private static GroceryList groceryList = new GroceryList();
public static void main(String[] args) {
boolean quit = false;
int choice = 0;
printInstructions();
while(!quit) {
System.out.println("Enter your choice: ");
choice = scanner.nextInt();
scanner.nextLine();
switch(choice) {
case 0:
printInstructions();
case 1:
groceryList.printGroceryList();
break;
case 2:
addItem();
break;
case 3:
modifyItem();
break;
case 4:
removeItem();
break;
case 5:
searchForItem();
break;
case 6:
processArrayList();
break;
case 7:
quit = true;
break;
}
}
}
public static void printInstructions() {
System.out.println("\nPress ");
System.out.println("\t 0 = To print choice options.");
System.out.println("\t 1 = To print the list of grocery items.");
System.out.println("\t 2 = To add an item to the list.");
System.out.println("\t 3 = To modify an item in the list.");
System.out.println("\t 4 = To remove an item from the list.");
System.out.println("\t 5 = To search for an item in the list.");
System.out.println("\t 6 = To quit the application.");
}
public static void addItem() {
System.out.print("Please enter the grocery item: ");
groceryList.addGroceryItem(scanner.nextLine());
}
public static void modifyItem() {
System.out.println("Current item name: ");
String itemNo = scanner.nextLine();
System.out.println("Enter new item: ");
String newItem = scanner.nextLine();
groceryList.modifyGroceryItem(itemNo, newItem);
}
public static void removeItem() {
System.out.println("Enter item name: ");
String itemNo = scanner.nextLine();
groceryList.removeGroceryItem(itemNo);
}
public static void searchForItem() {
System.out.println("Item to search for: ");
String searchItem = scanner.nextLine();
if(groceryList.onFile(searchItem)) {
System.out.println("Found " + searchItem + " in our grocery list" );
} else {
System.out.println(searchItem + " is not in the shopping list. ");
}
}
public static void processArrayList() {
ArrayList<String> newArray = new ArrayList<String>();
newArray.addAll(groceryList.getGroceryList()); //grocerylist content is added into the newArray. one method to do it.
ArrayList<String> nextArray = new ArrayList<String>(groceryList.getGroceryList()); //faster way to do it.
//to convert the arraylist BACK to an array.
String[] myArray = new String[groceryList.getGroceryList().size()];
myArray = groceryList.getGroceryList().toArray(myArray);
}
}