-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNames.java
431 lines (356 loc) · 12.9 KB
/
Names.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// Answers to Analysis Questions
// One interesting trend was how the presence of celebrity figures can often sway the popularity of a name. For instance, for Courtney, we see that between 1960 and 2000, the name moved 528 ranks (determined through a method I added). Though there are many reasons for this, it is likely that the emergence of actress Courtney Love and singer Courtney Cox influenced the popularity of this name.
import java.io.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
/**
* Names class looks at NameRecord objects and creates a variety of methods (like printing rank, printing trends in popularity, and more).
*
* @author Dhruv Patel
* @version 12.15.2021
*/
public class Names
{
// instance variables
private Scanner scanThrough;
private File newFile;
private ArrayList<NameRecord> arrayList;
/**
* Constructor for objects of class Names
*
* @throws FileNotFoundException
*/
public Names() throws FileNotFoundException
{
// initialise instance variables
String line = "";
newFile = new File("Names.txt");
scanThrough = new Scanner(newFile); // scans through the file
arrayList = new ArrayList<NameRecord>();
while (scanThrough.hasNextLine()){
line = scanThrough.nextLine();
NameRecord a = new NameRecord(line);
arrayList.add(a);
}
}
/**
* Method that will be accessed by other methods - finds the NameRecord object for the specific name
*
* @param name
* @return null
*/
public NameRecord getRecordByName(String name) {
for (NameRecord i : arrayList) {
if (name.equalsIgnoreCase(i.getName())) {
return i;
}
}
return null;
}
/**
* Method to print the rank of a name in every decade
*
* @param name
* @return name
*/
public ArrayList printRank(String name) {
ArrayList<String> array = new ArrayList<>();
NameRecord nr = this.getRecordByName(name);
if (nr == null) {
JFrame frame = new JFrame();
JOptionPane.showMessageDialog(frame,"Name not found","Name error", JOptionPane.ERROR_MESSAGE);
} // if the name is not found in the data file, print this message
for (int i = 0; i < 11; i++) {
int year = 1900 + 10 * i;
array.add("The popularity in " + year + " was " + nr.getData(i) + "\n");
} // for each decade, print the popularity
return array;
}
/**
* Method to print the names that have been ranked in every decade
*
* @return str
* @throws IOException
*/
public String allDecades() throws IOException {
String str = "";
for (NameRecord i : arrayList) {
int count = 0;
for (int t = 0; t < 11; t++) {
if (i.getData(t) == 0) {
count += 1;
}
}
if (count == 0) {
str += i.getName() + "\n";
}
}
str = str.trim();
return str;
}
/**
* Method to print the name that has been ranked in only one decade
*
* @return str
* @throws IOException
*/
public String oneDecades() throws IOException {
ArrayList<String> array = new ArrayList<>();
String str = "";
for (NameRecord i : arrayList) {
int count = 0;
for (int t = 0; t<11; t++) {
if (i.getData(t) != 0) {
count += 1;
}
}
if (count == 1) {
array.add(i.getName());
if (array.size() != 0) {
str += i.getName() + "\n";
}
}
}
str = str.trim();
return str;
}
/**
* Method to print the rank of a name at a specific decade
*
* @return output
* @param name
* @param decade
*/
public String rankName(String name, int decade) {
int index = (decade - 1900) / 10;
if (index < 0 || index > 10) {
JFrame frame = new JFrame();
JOptionPane.showMessageDialog(frame,"Cannot be calculated","Decade error",JOptionPane.ERROR_MESSAGE);
} // if the provided decade is not in the range given in the data set, return an error
NameRecord nr = this.getRecordByName(name);
if (nr == null) {
JFrame frame = new JFrame();
JOptionPane.showMessageDialog(frame,"Name not found","Name error",JOptionPane.ERROR_MESSAGE);
}
String output = "" + nr.getData(index);
if (output.equals("0")) {
output = "Name is not ranked (after the 1000th most popular name)";
} // if the rank is 0, print statement saying it is after the 1000th most popular name
return output;
}
/**
* Method to print the names whose popularity has consistently increased
*
* @return str
*/
public String popularityIncreasing() {
String str = "";
for (NameRecord i : arrayList) {
int count = 0;
int max = i.getData(0);
for (int t = 1; t<11; t++) {
if (i.getData(t) > max) {
max = i.getData(t);
count += 1;
}
}
if (count == 10) {
str += i.getName() + "\n";
}
}
str = str.trim();
return str;
}
/**
* Method to print the names whose popularity has consistently decreased
*
* NOTE: This is a method personally added (not required by directions)
*
* @return str
*/
public String popularityDecreasing()
{
String str = "";
for (NameRecord i : arrayList) {
int count = 0;
int min = i.getData(0);
for (int t = 1; t<11; t++) {
if (i.getData(t) < min) {
min = i.getData(t);
count += 1;
}
}
if (count == 10) {
str += i.getName() + "\n";
}
}
str = str.trim();
return str;
}
/**
* Method to determine if one name was more popular than another in a specific decade
*
* NOTE: This is a method personally added (not required by directions)
*
* @param name1
* @param name2
* @param decade
* @return output
*/
public String isOneNameMorePopular(String name1, String name2, int decade) // this is a method personally added (not required by directions) throws Exception throws Exception throws Exception
{
String output = "";
if (name1.equalsIgnoreCase(name2)) {
JFrame frame = new JFrame();
JOptionPane.showMessageDialog(frame,"Please input different names","Name error",JOptionPane.ERROR_MESSAGE);
}
int modifiedYear = (decade-1900)/10;
if (modifiedYear > 10 || modifiedYear < 0) {
JFrame frame = new JFrame();
JOptionPane.showMessageDialog(frame,"Cannot be calculated","Decade error",JOptionPane.ERROR_MESSAGE);
} // if the inputted decade is not between 1900 and 2000, print error message
NameRecord nr = this.getRecordByName(name1);
if (nr == null) {
JFrame frame = new JFrame();
JOptionPane.showMessageDialog(frame,"Name not found","Name error",JOptionPane.ERROR_MESSAGE);
} // if name not found, print error message
int popYear = nr.getData(modifiedYear);
NameRecord nr1 = this.getRecordByName(name2);
if (nr1 == null) {
JFrame frame = new JFrame();
JOptionPane.showMessageDialog(frame,"Name not found","Name error",JOptionPane.ERROR_MESSAGE);
} // if name not found, print error message
int popYear1 = nr1.getData(modifiedYear);
if (popYear < popYear1) {
output = name1 + " was more popular than " + name2 + " in " + decade;
}
else if (popYear > popYear1) {
output = name2 + " was more popular than " + name1 + " in " + decade;
}
else if (popYear == popYear1) {
output = "The popularity was equal";
}
return output;
}
/**
* Method to determine the most popular name in a specified decade
*
* NOTE: This is a method personally added (not required by directions)
*
* @param decade
* @return str
*/
public String mostPopularName(int decade) {
String str = "";
int index = (decade - 1900) / 10;
if (index > 10 || index < 0) {
JFrame frame = new JFrame();
JOptionPane.showMessageDialog(frame,"Cannot be calculated","Decade error",JOptionPane.ERROR_MESSAGE);
}
for (NameRecord i : arrayList) {
if (i.getData(index) == 1) {
str += i.getName() + "\n";
}
}
return str;
}
/**
* Method to determine the least popular name in a specified decade
*
* NOTE: This is a method personally added (not required by directions)
*
* @param decade
* @return str
*/
public String leastPopularName(int decade) {
String str = "";
int index = (decade - 1900) / 10;
if (index > 10 || index < 0) {
JFrame frame = new JFrame();
JOptionPane.showMessageDialog(frame,"Cannot be calculated","Decade error", JOptionPane.ERROR_MESSAGE);
}
for (NameRecord i : arrayList) {
if (i.getData(index) == 999) {
str += i.getName() + "\n";
}
}
return str;
}
/**
* Method to print the name at a specificied rank in any decade
*
* NOTE: This is a method personally added (not required by directions)
*
* @param position
* @param decade
* @return str
*/
public String positionReturnName(int position, int decade) {
String str = "";
int index = (decade - 1900) / 10; // modify the inputted decade to work with the indices set in NameRecord
if (index > 10 || index < 0) {
JFrame frame = new JFrame();
JOptionPane.showMessageDialog(frame,"Cannot Be Calculated","Decade Error",JOptionPane.ERROR_MESSAGE);
} // if the decade input is not between 1900 and 2000, print error message.
if (position <= 0 || position >= 1000) {
JFrame frame = new JFrame();
JOptionPane.showMessageDialog(frame,"Inaccurate Position","Position Error",JOptionPane.ERROR_MESSAGE);
} // if a rank is not between 1-999, print error message.
for (NameRecord i: arrayList) {
if (position != 0) { // if the position is 0, do not run this statement
if (i.getData(index) == position) {
str = i.getName();
}
}
}
return str;
}
/**
* Method to determine the change in popularity (number of ranks moved)
*
* NOTE: This is a method personally added (not required by directions)
*
* @param name
* @param decade1
* @param decade2
* @return str
*/
public String changeInPopularity (String name, int decade1, int decade2) {
String str = "";
int index1 = (decade1-1900) / 10;
int index2 = (decade2-1900) / 10;
if ((index1 > 10 || index1 < 0) || (index2 > 10 || index2 < 0)) {
JFrame frame = new JFrame();
JOptionPane.showMessageDialog(frame,"Cannot Be Calculated","Decade Error",JOptionPane.ERROR_MESSAGE);
}
if (decade1 == decade2) {
JFrame frame = new JFrame();
JOptionPane.showMessageDialog(frame,"Decades cannot be the same","Decade Error",JOptionPane.ERROR_MESSAGE);
}
NameRecord nr = this.getRecordByName(name);
int pop1 = nr.getData(index1);
if (name == null) {
JFrame frame = new JFrame();
JOptionPane.showMessageDialog(frame,"Name not found","Name error",JOptionPane.ERROR_MESSAGE);
}
int pop2 = nr.getData(index2);
if (pop2 == 0) {
pop2 = 1000;
}
int change = pop2-pop1;
if(decade1 != decade2) {
if (pop2 == 1000) {
str = "The popularity decreased in popularity by at least " + change + " positions";
}
else if (change < 0) {
str = "The popularity grew in popularity " + Math.abs(change) + " positions";
}
else if (change >= 0) {
str = "The popularity decreased in popularity by " + change + " positions";
}
}
return str;
}
//NOTE: Tweleve total method --- six are personally created and not required by directions.
}