-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBabyBirths.java
169 lines (124 loc) · 5.26 KB
/
BabyBirths.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
import edu.duke.*;
import org.apache.commons.csv.*;
import java.io.File;
public class BabyBirths {
private String projectDirectory = "E:/Coursera/us_babynames/";
public int getNumNamesByGenderAndYear(String gender, int year) {
FileResource fr = new FileResource(projectDirectory + "us_babynames_by_year/yob" + year + ".csv");
int numNames = 0;
for (CSVRecord rec : fr.getCSVParser(false)) {
if (rec.get(1).equals(gender)) {
numNames++;
}
}
return numNames;
}
public int getRankByNameAndYear(String name, int year, String gender) {
FileResource fr = new FileResource(projectDirectory + "us_babynames_by_year/yob" + year + ".csv");
int rank = 0;
for (CSVRecord rec : fr.getCSVParser(false)) {
if (rec.get(1).equals(gender)) {
rank++;
if (rec.get(0).equals(name)) {
return rank;
}
}
}
return -1;
}
public String getNameByRankAndYear(int rank, int year, String gender) {
FileResource fr = new FileResource(projectDirectory + "us_babynames_by_year/yob" + year + ".csv");
int currentRank = 0;
for (CSVRecord rec : fr.getCSVParser(false)) {
if (rec.get(1).equals(gender)) {
currentRank++;
if (currentRank == rank) {
return rec.get(0);
}
}
}
return "NO NAME.";
}
public void whatIsNameInYear(String name, int year, int newYear, String gender) {
int rank = getRankByNameAndYear(name, year, gender);
String newName = getNameByRankAndYear(rank, newYear, gender);
System.out.println(name + " born in " + year + " would be " + newName + " if born in " + newYear + ".");
}
public int yearOfHighestRank(String name, String gender) {
int highestRank = Integer.MAX_VALUE;
int yearWithHighestRank = -1;
DirectoryResource dr = new DirectoryResource();
for (File f : dr.selectedFiles()) {
String fileName = f.getName();
int year = Integer.parseInt(fileName.substring(3, 7));
int currentRank = getRankByNameAndYear(name, year, gender);
if (currentRank != -1 && currentRank < highestRank) {
highestRank = currentRank;
yearWithHighestRank = year;
}
}
return yearWithHighestRank;
}
public double getAverageRank(String name, String gender) {
double totalRank = 0.0;
int fileCount = 0;
DirectoryResource dr = new DirectoryResource();
for (File f : dr.selectedFiles()) {
String fileName = f.getName();
int year = Integer.parseInt(fileName.substring(3, 7));
int currentRank = getRankByNameAndYear(name, year, gender);
if (currentRank != -1) {
totalRank += currentRank;
fileCount++;
}
}
if (fileCount == 0) {
return -1.0;
}
return totalRank / fileCount;
}
public int getTotalBirthsRankedHigher(int year, String name, String gender) {
FileResource fr = new FileResource(projectDirectory + "us_babynames_by_year/yob" + year + ".csv");
int totalBirthsRankedHigher = 0;
for (CSVRecord rec : fr.getCSVParser(false)) {
if (rec.get(1).equals(gender) && rec.get(0).equals(name)) {
break;
}
if (rec.get(1).equals(gender)) {
totalBirthsRankedHigher += Integer.parseInt(rec.get(2));
}
}
return totalBirthsRankedHigher;
}
public static void main(String[] args) {
BabyBirths bb = new BabyBirths();
// Question 1
System.out.println("Question 1: " + bb.getNumNamesByGenderAndYear("F", 1900));
// Question 2
System.out.println("Question 2: " + bb.getNumNamesByGenderAndYear("M", 1905));
// Question 3
System.out.println("Question 3: " + bb.getRankByNameAndYear("Emily", 1960, "F"));
// Question 4
System.out.println("Question 4: " + bb.getRankByNameAndYear("Frank", 1971, "M"));
// Question 5
System.out.println("Question 5: " + bb.getNameByRankAndYear(350, 1980, "F"));
// Question 6
System.out.println("Question 6: " + bb.getNameByRankAndYear(450, 1982, "M"));
// Question 7
bb.whatIsNameInYear("Susan", 1972, 2014, "F");
// Question 8
bb.whatIsNameInYear("Owen", 1974, 2014, "M");
// Question 9
System.out.println("Question 9: " + bb.yearOfHighestRank("Genevieve", "F"));
// Question 10
System.out.println("Question 10: " + bb.yearOfHighestRank("Mich", "M"));
// Question 11
System.out.println("Question 11: " + String.format("%.2f", bb.getAverageRank("Susan", "F")));
// Question 12
System.out.println("Question 12: " + String.format("%.2f", bb.getAverageRank("Robert", "M")));
// Question 13
System.out.println("Question 13: " + bb.getTotalBirthsRankedHigher(1990, "Emily", "F"));
// Question 14
System.out.println("Question 14: " + bb.getTotalBirthsRankedHigher(1990, "Drew", "M"));
}
}