-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWirelessNetworkList.java
245 lines (212 loc) · 6.99 KB
/
WirelessNetworkList.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
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
import java.util.Arrays;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;
import java.util.Scanner;
/** WirelessNetworkList.
* @author Adam Bostwick
* @version 11.18.19
*/
public class WirelessNetworkList
{
private WirelessNetwork[] networkList;
private String[] invalidList;
/** Constructor.
*
*/
public WirelessNetworkList()
{
networkList = new WirelessNetwork[0];
invalidList = new String[0];
}
/** getWirelessNetworksArray.
* @return true
*/
public WirelessNetwork[] getWirelessNetworksArray()
{
return networkList;
}
/** getInvalidRecordsArray.
* @return true
*/
public String[] getInvalidRecordsArray()
{
return invalidList;
}
/** addWirelessNetwork.
* @param newNetworkIn input for new network
*/
public void addWirelessNetwork(WirelessNetwork newNetworkIn)
{
networkList = Arrays.copyOf(networkList, networkList.length + 1);
networkList[networkList.length - 1] = newNetworkIn;
}
/** addInvalidRecord.
* @param invalidIn input for invalid
* @throws FileNotFoundException true
*/
public void addInvalidRecord(String invalidIn)
{
invalidList = Arrays.copyOf(invalidList, invalidList.length + 1);
invalidList[invalidList.length - 1] = invalidIn;
}
/** readFile.
* @param fileNameIn input for file name
* throws InvalidCategoryException true
* @throws FileNotFoundException true
* @throws NoSuchElementException true
* @throws NumberFormatException true
*/
public void readFile(String fileNameIn)
throws /*InvalidCategoryException,*/ FileNotFoundException,
NumberFormatException, NoSuchElementException
{
File fileToRead = new File(fileNameIn);
Scanner fileReader = new Scanner(fileToRead);
//fileReader.useDelimiter(",|\\n");
Scanner toParts = null;
String line = "";
char category;
String name;
double bandwidth;
double fixedCost;
double modemCost;
double time;
double dataLimit;
String invalid;
while (fileReader.hasNext())
{
try
{
line = fileReader.nextLine();
toParts = new Scanner(line);
toParts.useDelimiter(",");
category = toParts.next().trim().toUpperCase().charAt(0);
name = toParts.next().trim();
bandwidth = Double.parseDouble(toParts.next().trim());
fixedCost = Double.parseDouble(toParts.next().trim());
switch(category)
{
case 'W':
modemCost =
Double.parseDouble(toParts.next().trim());
this.addWirelessNetwork(
new WiFi(name, bandwidth, fixedCost, modemCost));
break;
case 'C':
time = Double.parseDouble(toParts.next().trim());
dataLimit = Double.parseDouble(toParts.next().trim());
this.addWirelessNetwork(new Cellular(name,
bandwidth, fixedCost, time, dataLimit));
break;
case 'L':
time = Double.parseDouble(toParts.next().trim());
dataLimit = Double.parseDouble(toParts.next().trim());
this.addWirelessNetwork(new LTE(name,
bandwidth, fixedCost, time, dataLimit));
break;
case 'F':
time = Double.parseDouble(toParts.next().trim());
dataLimit = Double.parseDouble(toParts.next().trim());
this.addWirelessNetwork(new FiveG(name,
bandwidth, fixedCost, time, dataLimit));
break;
default:
//this.addInvalidRecord(line);
throw new InvalidCategoryException("" + category);
//break;
}
}
catch (InvalidCategoryException ice)
{
invalid = line + "\n" + ice;
addInvalidRecord(invalid);
}
catch (NumberFormatException nfe)
{
invalid = line + "\n" + nfe;
addInvalidRecord(invalid);
}
catch (NoSuchElementException nsee)
{
invalid = line + "\n" + nsee + ": For missing input data";
addInvalidRecord(invalid);
}
}
fileReader.close();
}
/** generateReport.
* @return true
*/
public String generateReport()
{
String output = "-------------------------------\n"
+ "Monthly Wireless Network Report";
output += "\n-------------------------------\n";
for (int i = 0; i < networkList.length; i++)
{
output += networkList[i].toString() + "\n\n";
}
return output;
}
/** generateReportByName.
* @return true
*/
public String generateReportByName()
{
Arrays.sort(networkList);
String output = "-----------------------------------------\n"
+ "Monthly Wireless Network Report (by Name)";
output += "\n-----------------------------------------\n";
for (int i = 0; i < networkList.length; i++)
{
output += networkList[i].toString() + "\n\n";
}
return output;
}
/** generateReportByBandwidth.
* @return true
*/
public String generateReportByBandwidth()
{
Arrays.sort(networkList, new BandwidthComparator());
String output = "----------------------------------------------\n"
+ "Monthly Wireless Network Report (by Bandwidth)";
output += "\n----------------------------------------------\n";
for (int i = 0; i < networkList.length; i++)
{
output += networkList[i].toString() + "\n\n";
}
return output;
}
/** generateReportByMonthlyCost.
* @return true
*/
public String generateReportByMonthlyCost()
{
Arrays.sort(networkList, new MonthlyCostComparator());
//Arrays.sort(networkList, Collections.reverseOrder());
String output = "-------------------------------------------------\n"
+ "Monthly Wireless Network Report (by Monthly Cost)";
output += "\n-------------------------------------------------\n";
for (int i = 0; i < networkList.length; i++)
{
output += networkList[i].toString() + "\n\n";
}
return output;
}
/** generateInvalidRecordsReport.
* @return true
*/
public String generateInvalidRecordsReport()
{
String output = "----------------------\n"
+ "Invalid Records Report";
output += "\n----------------------\n";
for (int i = 0; i < invalidList.length; i++)
{
output += invalidList[i].toString() + "\n\n";
}
return output;
}
}