-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBabyBirths.java
More file actions
94 lines (77 loc) · 2.9 KB
/
BabyBirths.java
File metadata and controls
94 lines (77 loc) · 2.9 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
import edu.duke.*;
import org.apache.commons.csv.*;
import java.util.*;
public class BabyBirths {
public int countRows(CSVParser parser) {
int rowCount = 0;
for (CSVRecord record : parser) {
rowCount++;
}
return rowCount;
}
private int getBabyCount(FileResource fr, String targetName) {
CSVParser parser = fr.getCSVParser(false);
for (CSVRecord record : parser) {
String name = record.get(0);
if (name.equalsIgnoreCase(targetName)) {
return Integer.parseInt(record.get(2));
}
}
return -1;
}
public String popularityRankFinder(FileResource fr, String name) {
int targetCount = getBabyCount(fr, name);
if (targetCount == -1) {
return "Name not found";
}
int rank = 1;
CSVParser parser = fr.getCSVParser(false);
for (CSVRecord record : parser) {
String currentName = record.get(0);
int count = Integer.parseInt(record.get(2));
if (!currentName.equalsIgnoreCase(name) && count > targetCount) {
rank++;
}
}
return Integer.toString(rank);
}
public String getNameAtRank(FileResource fr, int targetRank) {
int currentRank = 1;
CSVParser parser = fr.getCSVParser(false);
for (CSVRecord record : parser) {
String name = record.get(0);
int count = Integer.parseInt(record.get(2));
if (currentRank == targetRank) {
return name;
}
currentRank++;
}
return "Rank not found";
}
public void testRank() {
FileResource fr = new FileResource();
Scanner scanner = new Scanner(System.in);
System.out.print("What is your name: ");
String name = scanner.nextLine();
String rank = popularityRankFinder(fr, name);
System.out.println("Rank of your name: " + rank);
}
public void findEquivalentNameInNewFile() {
Scanner scanner = new Scanner(System.in);
System.out.println("Select the original file:");
FileResource originalFile = new FileResource();
System.out.print("What is your name? ");
String name = scanner.nextLine();
String rankStr = popularityRankFinder(originalFile, name);
if (rankStr.equals("Name not found")) {
System.out.println("That name was not found in the original file.");
return;
}
int rank = Integer.parseInt(rankStr);
System.out.println(name + " is ranked #" + rank + " in the original file.");
System.out.println("Select the comparison file:");
FileResource newFile = new FileResource();
String newName = getNameAtRank(newFile, rank);
System.out.println("In the new file, the name at the same rank (" + rank + ") is: " + newName);
}
}