Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4943b17

Browse files
Chap 21, Ex: 13
1 parent b6eecc4 commit 4943b17

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

‎ch_21/Exercise12_13.java‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package ch_21;
2+
3+
import java.io.IOException;
4+
import java.net.URL;
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.Scanner;
8+
9+
/**
10+
* *21.13 (Baby name popularity ranking) Revise Programming Exercise 21.11 to prompt
11+
* the user to enter year, gender, and name and display the ranking for the name.
12+
* Prompt the user to enter another inquiry or exit the program. Here is a sample
13+
* run:
14+
* Enter the year: 2010
15+
* Enter the gender: M
16+
* Enter the name: Javier
17+
* Boy name Javier is ranked #190 in year 2010
18+
* Enter another inquiry? Y
19+
* Enter the year: 2001
20+
* Enter the gender: F
21+
* Enter the name: Emily
22+
* Girl name Emily is ranked #1 in year 2001
23+
* Enter another inquiry? N
24+
*/
25+
public class Exercise12_13 {
26+
static ArrayList<HashMap<String, String>> boysNames = new ArrayList<>();
27+
static ArrayList<HashMap<String, String>> girlsNames = new ArrayList<>();
28+
29+
static final String BABY_NAMES_BASE_URL = "https://liveexample.pearsoncmg.com/data/babynameranking";
30+
static String[] babyNamesHttpPaths;
31+
static final int BOY = 1;
32+
static final int GIRL = 3;
33+
String selectedYear;
34+
String selectedGender;
35+
String selectedName;
36+
37+
static {
38+
babyNamesHttpPaths = new String[10];
39+
// Resolve url paths for txt files
40+
for (int i = 0, year = 2001; i < 10; year++, i++) {
41+
babyNamesHttpPaths[i] = BABY_NAMES_BASE_URL + year + ".txt";
42+
}
43+
}
44+
45+
public static void main(String[] args) {
46+
Exercise12_13 context = new Exercise12_13();
47+
Scanner in = new Scanner(System.in);
48+
try {
49+
context.populateRankings(boysNames, BOY);
50+
context.populateRankings(girlsNames, GIRL);
51+
for (; ; ) {
52+
System.out.print("\nEnter the year: ");
53+
context.selectedYear = in.next().trim();
54+
55+
System.out.print("\nEnter the gender (M or F):");
56+
context.selectedGender = in.next().trim();
57+
58+
System.out.print("\nEnter a name: ");
59+
context.selectedName = in.next().trim();
60+
String result = context.getSelectedRank(boysNames, girlsNames);
61+
if (result.startsWith("Invalid")) {
62+
System.out.println(result);
63+
} else {
64+
System.out.println((context.selectedGender.equalsIgnoreCase("M") ? "Boy" : "Girl") + " name "
65+
+ context.selectedName + " is ranked #" + result + " in year " + context.selectedYear);
66+
67+
}
68+
System.out.print("\nEnter another inquiry?(Y or N) ");
69+
String loop = in.next().trim();
70+
if (loop.equalsIgnoreCase("Y")) {
71+
continue;
72+
}
73+
System.out.println("GoodBye");
74+
break;
75+
}
76+
77+
} catch (IOException e) {
78+
System.out.println("Exception occurred while reading data files..");
79+
System.out.println("Exiting the program....");
80+
System.exit(1);
81+
82+
}
83+
84+
}
85+
86+
private void populateRankings(ArrayList<HashMap<String, String>> names,
87+
int gender) throws IOException {
88+
for (int i = 0; i < babyNamesHttpPaths.length; i++) {
89+
URL dataUrl = new URL(babyNamesHttpPaths[i]);
90+
Scanner urlScanner = new Scanner(dataUrl.openStream());
91+
HashMap<String, String> map = new HashMap<>();
92+
93+
while (urlScanner.hasNextLine()) {
94+
String[] line = urlScanner.nextLine().split("\\s+"); // Split line ex: 1 Jacob 32,550 Emily 25,057
95+
String ranking = line[0].trim();
96+
map.put(line[gender].trim(), ranking);
97+
}
98+
names.add(map);
99+
urlScanner.close();
100+
}
101+
}
102+
103+
private String getSelectedRank(ArrayList<HashMap<String, String>> boysNames,
104+
ArrayList<HashMap<String, String>> girlsNames) {
105+
if (selectedName == null || selectedName.isEmpty() ||
106+
selectedGender == null || selectedGender.isEmpty() ||
107+
selectedYear == null || selectedYear.isEmpty()) {
108+
return "Invalid: Name, Gender, and Year must not be null or empty.";
109+
}
110+
int mapIndex = Integer.parseInt(selectedYear.substring(2)) - 1; // Convert selected year to list index
111+
String rank = "";
112+
if (selectedGender.equalsIgnoreCase("M")) {
113+
HashMap<String, String> rankings = boysNames.get(mapIndex);
114+
rank = rankings.get(selectedName);
115+
} else if (selectedGender.equalsIgnoreCase("F")) {
116+
HashMap<String, String> rankings = girlsNames.get(mapIndex);
117+
rank = rankings.get(selectedName);
118+
}
119+
120+
return rank;
121+
}
122+
123+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /