-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrontEnd.java
More file actions
194 lines (167 loc) · 5.77 KB
/
FrontEnd.java
File metadata and controls
194 lines (167 loc) · 5.77 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
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class FrontEnd {
private boolean run;
private Scanner input;
public FrontEnd()
{
run = false;
input = new Scanner(System.in);
}
public void startProgram(Shop shop){
run = true;
System.out.println("Welcome to ShopManager! Please enter the filename where your supplier information is stored:");
try {
takeFileInput(shop);
runFrontEnd(shop);
} catch (IOException e){
System.out.println("There was a problem with the file name entered or a failure in creating the orders.txt file. Exiting program...");
System.exit(0);
}
}
public void takeFileInput(Shop shop) throws IOException
{
String fileName = input.nextLine();
shop.addSuppliersFromFile(fileName);
System.out.println("Now please enter the filename where your inventory information is stored:");
fileName = input.nextLine();
shop.addInvFromFile(fileName);
System.out.println("Successfully loaded suppliers and inventory and created orders.txt file.\n");
}
public void runFrontEnd(Shop shop) {
try{
mainMenu(shop);
} catch(Exception e) {
System.out.println("Input error, restarting...");
String clear = input.nextLine();
runFrontEnd(shop);
}
}
public void mainMenu(Shop shop) throws Exception
{
while(run) {
switch(menu()) {
case 1:
shopSell(shop);
break;
case 2:
shopView(shop);
break;
case 3:
shopSearch(shop);
break;
case 4:
quitProg(shop);
break;
default:
otherOption();
break;
}
}
}
public int menu () throws Exception
{
System.out.println("What would you like to do?\n" +
"-Press 1 and Enter if a sale has been made.\n" +
"-Press 2 and Enter to view all items and their quantities.\n" +
"-Press 3 and Enter to search for an item to see its info & quantity.\n" +
"-Press 4 and Enter to quit the program");
int a = input.nextInt();
String clear = input.nextLine();
return a;
}
public void shopSell(Shop s)throws Exception
{
try {
System.out.println("Please enter the item ID and quantity sold.");
int id = input.nextInt();
int quan = input.nextInt();
String clear = input.nextLine();
int success = s.sell(id, quan);
shopSellResult(success);
pressKey();
}
catch(FileNotFoundException e){
System.out.println("Problem writing to orders.txt file. Quitting...");
System.exit(0);
}
}
public void shopSellResult(int success)
{
if (success==1)
System.out.println("Successfully sold item and removed quantity!");
else if (success==2)
System.out.println("Item does not exist!");
else
System.out.println("You don't have enough quantity of this item to sell that amount!");
}
public void shopView(Shop s)
{
System.out.println("Here is a list of all items and their information:");
s.printInventory();
pressKey();
}
public void shopSearch(Shop s) throws Exception{
int option;
do {
System.out.println("Press 1 and Enter to search by item ID or Press 2 and Enter to search by item name");
option = input.nextInt();
String clear = input.nextLine();
if (option != 1 && option != 2)
System.out.println("Invalid character entered, please try again.");
else
break;
} while (true);
shopSearchDo(option, s);
}
public void shopSearchDo(int option, Shop s) throws Exception
{
if (option == 1)
findItemShopID(s);
else
findItemShopDesc(s);
pressKey();
}
public void findItemShopID(Shop s) throws Exception
{
System.out.println("Enter item ID");
int id = input.nextInt();
String clear = input.nextLine();
System.out.println(s.getItemInfo(id));
}
public void findItemShopDesc(Shop s) throws Exception
{
System.out.println("Enter item name");
String name = input.nextLine();
System.out.println(s.getItemInfo(name));
}
public void quitProg(Shop s)throws FileNotFoundException
{
s.closeOrderFile();
System.out.println("Quitting ShopMaster...");
run = false;
}
public void otherOption()
{
System.out.println("An invalid input was entered, please try again.");
pressKey();
}
public void pressKey()
{
System.out.println( "\n----------------------------------------------------\n" +
" Press Enter to continue \n" +
"------------------------------------------------------");
String clear = input.nextLine();
}
public static void main(String[] args)
{
ArrayList<Supplier> suppliers = new ArrayList<Supplier>();
ArrayList<Order> orders = new ArrayList<Order>();
Inventory inventory = new Inventory();
Shop s = new Shop (inventory, suppliers, orders);
FrontEnd fe = new FrontEnd();
fe.startProgram(s);
}
}