-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMenuParser.java
More file actions
197 lines (134 loc) · 6.09 KB
/
MenuParser.java
File metadata and controls
197 lines (134 loc) · 6.09 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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Base64;
import java.awt.Image;
import java.awt.Toolkit;
public class MenuParser {
public static void main(String[] args) {
String filename = "Panda_Express_Menu.txt";
ArrayList<MenuItem> menuItems = parseMenuFile(filename);
// send menuItems to server using socket programming
}
public static ArrayList<MenuItem> parseMenuFile(String filename) {
ArrayList<MenuItem> menuItems = new ArrayList<>();
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line = reader.readLine();
while (line != null) {
String[] element = line.split("\\|");
String food = element[0].trim();
String description = element[1].trim();
double price = Double.parseDouble(element[2].trim()); //parse a string to double
String picture = element[3].trim();//?????????????????????????????????????????
// Decode the base64-encoded picture data
byte[] imageData = Base64.getDecoder().decode(picture);
// Image image = Toolkit.getDefaultToolkit().createImage(imageData);
MenuItem item = new MenuItem(food, description, price, imageData);
menuItems.add(item);
line = reader.readLine();
// Create an ImageIcon from the decoded byte array
// ImageIcon imageIcon = new ImageIcon(imageData);
// // Display the ImageIcon in a JLabel
// JLabel pictureLabel = new JLabel(imageIcon);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return menuItems;
}
// public static void sendMenuToServer(ArrayList<MenuItem> menuItems) {
// try {
// Socket socket = new Socket(address, port);
// ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
// outputStream.writeObject(menuItems);
// outputStream.flush();
// socket.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
public static class MenuItem {
String food;
String description;
double price;
byte[] imageData;
public MenuItem(String itemName, String description, double price, byte[] imageData) {
this.food = itemName;
this.description = description;
this.price = price;
this.imageData = imageData;
} ;
}
}
// import java.io.BufferedReader;
// import java.io.IOException;
// import java.io.InputStream;
// import java.io.InputStreamReader;
// import java.nio.charset.StandardCharsets;
// import java.util.Arrays;
// public class MenuParser {
// public static void main(String[] args) {
// try {
// InputStream inputStream = MenuParser.class.getResourceAsStream("/Menu.json"); // load json file
// InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8); // ead the contents
// // of the input
// // stream as
// // characters using
// // the UTF-8 encoding
// // read the characters from the InputStreamReader
// // and append them to a StringBuilder to create a string representation of the
// // JSON data.
// BufferedReader bufferedReader = new BufferedReader(reader);
// StringBuilder stringBuilder = new StringBuilder();
// String line;
// while ((line = bufferedReader.readLine()) != null) {
// stringBuilder.append(line);
// }
// String json = stringBuilder.toString();
// Menu[] menus = parseMenuJson(json);
// // Do something with the menus array
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// /*
// * pass "json" string to the parseMenuJson method, which manually parses the
// * JSON data into an array of Menu
// * objects by splitting the JSON string into tokens, then splitting each token
// * into key-value pairs
// * and populating a new Menu object with the appropriate values.
// */
// private static Menu[] parseMenuJson(String json) {
// String[] tokens = json.split("[{}]");
// Menu[] menus = new Menu[tokens.length / 2];
// int i = 0;
// for (int j = 1; j < tokens.length; j += 2) {
// String[] props = tokens[j].split(",");
// Menu menu = new Menu();
// for (String prop : props) {
// String[] keyValue = prop.split(":");
// String key = keyValue[0].trim();
// String value = keyValue[1].trim();
// switch (key) {
// case "\"name\"":
// menu.setName(value.substring(1, value.length() - 1));
// break;
// case "\"price\"":
// menu.setPrice(Double.parseDouble(value));
// break;
// case "\"img\"":
// menu.setImg(value.substring(1, value.length() - 1));
// break;
// case "\"restaurant\"":
// menu.setRestaurant(value.substring(1, value.length() - 1));
// break;
// }
// }
// menus[i++] = menu;
// }
// return menus;
// }
// }