-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShoppingList.java
115 lines (87 loc) · 2.76 KB
/
ShoppingList.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
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
import java.util.*;
public class ShoppingList {
private String[] MyList;
private int counter;
private java.util.Scanner scan;
// declare storage for the list items
public ShoppingList() {
int counter = 0;
int MAXSIZE = 10;
String[] MyList = new String[MAXSIZE];
scan = new java.util.Scanner(System.in);
// initialize storage for list items
}
public void printList() {
System.out.println("Your shopping list:");
System.out.println(MyList);
// print the list
}
public void addToList(String item) {
for(int i = 0; i < MyList.length ; i++){
MyList[counter] = item;
counter++;
}
}
public void emptyList() {
// empty the list
}
public String getInput() {
System.out.print("Enter your item or command: ");
return scan.next().trim();
}
public void printWelcome() {
System.out.println("Welcome to the XYZ Shopping List Program.");
}
public void printThankYou() {
System.out.println("Thank you for using the XYZ Shopping List Program.");
}
public void printHelp() {
System.out.println("Here are the list of commands:");
System.out.println(" -p : Print the list");
System.out.println(" -e : Empty the list");
// System.out.println(" -r n : Remove the nth item from the list");
System.out.println(" -x : Exit the application");
System.out.println(" -h : Print this command list");
}
public void go() {
String input;
printWelcome();
printHelp();
input = getInput();
while( ! input.equals("-x")) {
/*switch(input) {
case "-h":
printHelp();
break;
case "-p":
printList();
break;
case "-x":
break;
case "-e":
emptyList();
break;
default:
addToList(input); */
//alternate (pre-Java 1.7) implementation
if(input.equals("-h")) {
printHelp();
} else if(input.equals("-p")) {
printList();
} else if(input.equals("-e")) {
emptyList();
} else {
addToList(input);
}
//*/
input = getInput();
}
printThankYou();
}
public static void main(String[] args) {
ShoppingList list;
list = new ShoppingList();
list.go();
list.printList();
}
}