-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain1.java
More file actions
259 lines (239 loc) · 10.6 KB
/
Copy pathMain1.java
File metadata and controls
259 lines (239 loc) · 10.6 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package test1;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.File;
import java.io.IOException;
import java.util.*;
public class Main1 {
private static final int goodsIdIndex = 0;
private static final int goodsNameIndex = 1;
private static final int belongingRegionIndex = 2;
private static final int sendingRegionIndex = 3;
private static final int goodsTypeIndex = 4;
private static final int customerLevelIndex = 5;
private static final int receiveDateIndex = 6;
private static List<String> belongingRegionList = new ArrayList<>();
private static ArrayList<Goods> goodsList = new ArrayList<>();
public static void main(String[] args) {
System.out.println("------------欢迎来到物流管理系统-----------");
System.out.println("读取数据中...");
// 需要解析的excel文件
File file = new File("src/resources/data.xls");
try {
readDataFromFile(file);
while (true) {
System.out.println("----------请选择地区----------");
for (int i = 0; i < belongingRegionList.size(); i++) {
System.out.println(i + 1 + "." + belongingRegionList.get(i));
}
System.out.println();
System.out.println("0.退出系统");
try {
Scanner input = new Scanner(System.in);
int inputValue = input.nextInt();
if (inputValue == 0) {
System.out.println("--------------系统登出-------------");
return;
} else if (inputValue < belongingRegionList.size()) {
regionSelector(inputValue - 1);
} else {
System.out.println("输入错误!请重新输入!");
}
} catch (Exception e) {
System.out.println("输入错误!请重新输入!");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static void readDataFromFile(File file) throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook(FileUtils.openInputStream(file));
// 读取默认第一个工作表sheet
HSSFSheet sheet = workbook.getSheetAt(0);
int lastRowNum = sheet.getLastRowNum();
Set<String> belongingRegionSet = new HashSet<>();
for (int i = 1; i <= lastRowNum; i++) {
HSSFRow row = sheet.getRow(i);
HSSFCell cell0 = row.getCell(goodsIdIndex);
HSSFCell cell1 = row.getCell(goodsNameIndex);
HSSFCell cell2 = row.getCell(belongingRegionIndex);
HSSFCell cell3 = row.getCell(sendingRegionIndex);
HSSFCell cell4 = row.getCell(goodsTypeIndex);
HSSFCell cell5 = row.getCell(customerLevelIndex);
HSSFCell cell6 = row.getCell(receiveDateIndex);
int goodsId = Integer.parseInt(cell0.getStringCellValue());
String goodsName = cell1.getStringCellValue();
String belongingRegion = cell2.getStringCellValue();
belongingRegionSet.add(belongingRegion);
String sendingRegion = cell3.getStringCellValue();
int goodsType = Integer.parseInt(cell4.getStringCellValue());
int clientGrade = Integer.parseInt(cell5.getStringCellValue());
String receiveDate = cell6.getStringCellValue();
Goods goods = new Goods(goodsId, goodsName, belongingRegion, sendingRegion, goodsType, clientGrade, receiveDate);
goodsList.add(goods);
}
belongingRegionList.addAll(belongingRegionSet);
}
private static void regionSelector(int belongingRegionIndex) {
String belongRegion = belongingRegionList.get(belongingRegionIndex);
System.out.println("----------地区" + belongRegion + "----------");
while (true) {
System.out.println("1.按照优先级别排序物品");
System.out.println("2.价格最小物流方案");
System.out.println("3.时间最短物流方案");
System.out.println("4.综合物流最优方案");
System.out.println("5.航空物流方案");
System.out.println();
System.out.println("0.返回上一页");
Scanner input = new Scanner(System.in);
int inputValue;
try {
inputValue = input.nextInt();
switch (inputValue) {
case 0:
return;
case 1:
prioritySort(belongRegion);
break;
case 2:
projectSeletor(belongRegion, 1);
break;
case 3:
projectSeletor(belongRegion, 2);
break;
case 4:
projectSeletor(belongRegion, 3);
break;
case 5:
projectSeletor(belongRegion, 4);
break;
default:
System.out.println("输入错误!请重新输入!");
break;
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("输入错误!请重新输入!");
}
}
}
private static void projectSeletor(String belongingRegion, int projectId) {
while (true) {
System.out.println("1.按优先级输出该方案的物品");
System.out.println("2.输出最短路径");
System.out.println("3.根据物品ID发货");
System.out.println();
System.out.println("0.返回上一页");
Scanner input = new Scanner(System.in);
int inputValue;
try {
inputValue = input.nextInt();
switch (inputValue) {
case 0:
return;
case 1:
printGoodsOfThisProject(belongingRegion, projectId);
break;
case 2:
minStep(belongingRegionList.indexOf(belongingRegion), -1, projectId);
break;
case 3:
sendGoods(belongingRegion, projectId);
break;
default:
System.out.println("输入错误!请重新输入!");
break;
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("输入错误!请重新输入!");
}
}
}
private static void sendGoods(String belongingRegion, int projectId) {
while (true) {
System.out.println("请输入物品ID发货,输入0返回上一层:");
Scanner input = new Scanner(System.in);
int inputValue;
try {
inputValue = input.nextInt();
switch (inputValue) {
case 0:
return;
default:
goodsList.stream()
.filter(goods -> goods.belongingArea.equals(belongingRegion))
.filter(goods -> goods.type == projectId)
.filter(goods -> goods.ID == inputValue)
.forEach(el -> {
System.out.println(el);
minStep(belongingRegionList.indexOf(belongingRegion), belongingRegionList.indexOf(el.sendingArea), projectId);
});
break;
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("输入错误!请重新输入!");
}
}
}
// if end == -1, then print all. else print specified route
private static void minStep(int start, int end, int projectId) {
File file = new File("src/resources/regionDistance.xls");
try {
int[][] Graph = new int[belongingRegionList.size()][belongingRegionList.size()];
HSSFWorkbook workbook = new HSSFWorkbook(FileUtils.openInputStream(file));
// 读取默认第一个工作表sheet
HSSFSheet sheet = workbook.getSheetAt(projectId - 1);
for (int i = 1; i < belongingRegionList.size() + 1; i++) {
HSSFRow row = sheet.getRow(i);
// 获取当前行最后单元格列号
for (int j = 1; j < belongingRegionList.size() + 1; j++) {
HSSFCell cell = row.getCell(j);
int value = (int) cell.getNumericCellValue();
Graph[i - 1][j - 1] = value;
}
}
Dijkstra.minStep(Graph, start, end, belongingRegionList);
System.out.println();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void printGoodsOfThisProject(String belongingRegion, int goodsType) {
// NodeTree root = new NodeTree(goodsList.get(0)); 用树形结构存储,用根节点
goodsList.stream()
.filter(goods -> goods.belongingArea.equals(belongingRegion))
.filter(goods -> goods.type == goodsType)
.sorted((o1, o2) -> {
double diff = o1.priority - o2.priority;
if (diff == 0) {
return 0;
}
return diff > 0 ? -1 : 1;
})
.forEach(System.out::println);
// .forEach(el-> {
// root.insert(root, el); //插入元素
// });
System.out.println();
// root.preOrder(root); //后向遍历
}
private static void prioritySort(String belongingRegion) {
goodsList.stream()
.filter(goods -> goods.belongingArea.equals(belongingRegion))
.sorted((o1, o2) -> {
double diff = o1.priority - o2.priority;
if (diff == 0) {
return 0;
}
return diff > 0 ? -1 : 1;
})
.forEach(System.out::println);
System.out.println();
}
}