-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathgrocery application in java
More file actions
215 lines (151 loc) · 4.74 KB
/
grocery application in java
File metadata and controls
215 lines (151 loc) · 4.74 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//This is our Main class. inside this class, we will implement our GroceryList class.
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;
}
}
System.out.println("*********************************************************\r");
ArrayList<String> myToDoList = new ArrayList<String>();
myToDoList.add("Write an articel");
myToDoList.add("Make a Youtube Video");
myToDoList.add("Make a salad for dinner");
System.out.println(myToDoList.size());
myToDoList.set(0, "Dont forget things");
myToDoList.set(1, "Buy milk and orange juice");
myToDoList.remove(0);
for(int i = 0; i < myToDoList.size(); i++) {
System.out.println("Item No " + (i) + " - "+ myToDoList.get(i));
}
}
public static void printInstructions() {
System.out.println("\nPress");
System.out.println("\t 0 - To Print The Instructions ");
System.out.println("\t 1 - To Print GroceryList ");
System.out.println("\t 2 - To Add Item ");
System.out.println("\t 3 - To Modify item ");
System.out.println("\t 4 - To Remove Item ");
System.out.println("\t 5 - To Search for an Item ");
System.out.println("\t 6 - To Process Array List. ");
System.out.println("\t 7 - To Not Continue");
}
public static void addItem() {
System.out.print("Add : Please enter the grocery Item : ");
groceryList.addGroceryItem(scanner.nextLine());
}
public static void modifyItem() {
System.out.print("Modify : Enter the Item: ");
String curItem = scanner.nextLine();
System.out.print("Modify : Enter the Replacement Item");
String newItem = scanner.nextLine();
groceryList.modifyGroceryItem(curItem, newItem);
}
public static void removeItem() {
System.out.print("Remove : Enter the Item: ");
String itemNo = scanner.nextLine();
groceryList.removeItem(itemNo);
System.out.print("msg: The Item has been removed");
groceryList.printGroceryList();
}
public static void searchForItem() {
System.out.print("Item to search for: ");
String searchItem = scanner.nextLine();
if(groceryList.onFile(searchItem)) {
System.out.println("Item " + searchItem + " is on the list");
}else{
System.out.println(searchItem + " is not in the list");
}
}
public static void processArrayList() {
ArrayList<String> newArrayList = new ArrayList<String>();
newArrayList.addAll(groceryList.getGroceryList());
ArrayList<String> anotherArrayList = new ArrayList<>(groceryList.getGroceryList());
String[] myArray = new String[groceryList.getGroceryList().size()];
myArray = groceryList.getGroceryList().toArray(myArray);
}
}
}
//Here is our Grocerylist Class with all the functionality.
//File : GroceryList.java
import java.util.ArrayList;
public class GroceryList {
private ArrayList<String> groceryList = new ArrayList<String>();
public ArrayList<String> getGroceryList() {
return groceryList;
}
public void setGroceryList(ArrayList<String> groceryList) {
this.groceryList = groceryList;
}
public void addGroceryItem(String item) {
groceryList.add(item);
}
public void printGroceryList() {
System.out.println("You Have "+ groceryList.size() + " item in your grocery list");
for(int i = 0; i < groceryList.size(); i ++) {
System.out.println((i+1) +" . "+groceryList.get(i));
}
}
public void modifyGroceryItem(String currentItem, String newItem) {
int position = findItem(currentItem);
if(position >= 0) {
modifyGroceryItem(position , newItem);
}
}
private void modifyGroceryItem(int position , String item) {
groceryList.set(position, item);
System.out.println("Grocery Item " + (item) + " has been modified");
}
public void removeItem(String item) {
int position = findItem(item);
if(position >= 0) {
removeItem(position);
}
}
private void removeItem(int position) {
groceryList.remove(position);
}
private int findItem(String searchItem) {
return groceryList.indexOf(searchItem);
}
public boolean onFile(String item) {
int position = findItem(item);
if(position >= 0) {
return true;
}
return false;
}
}