-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
113 lines (91 loc) · 3.58 KB
/
Copy pathMain.java
File metadata and controls
113 lines (91 loc) · 3.58 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package Data_Structures.Arrays.Lists;
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();
break;
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.print("Current item name: ");
String itemNo = scanner.nextLine();
System.out.print("Enter replacement item: ");
String newItem = scanner.nextLine();
//we are starting the count from 1, so we are deducting 1 to able to modify the correct element
groceryList.modifyGroceryItem(itemNo, newItem);
}
public static void removeItem() {
System.out.print("Enter item name: ");
String itemNo = scanner.nextLine();
scanner.nextLine();
groceryList.removeGroceryItem(itemNo);
}
public static void searchForItem() {
System.out.print("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() {
//1 way to copy an array
ArrayList<String> newArray = new ArrayList<String>();
newArray.addAll(groceryList.getGroceryList());
//2 another way of copying an away
ArrayList<String> nextArray = new ArrayList<String>(groceryList.getGroceryList());
//returning an array list of strings, converting to an array, updateed it into a var and passing it to the toArray method
String[] myArray = new String[groceryList.getGroceryList().size()];
//will contain the contents of the array
myArray = groceryList.getGroceryList().toArray(myArray);
}
}