/*
  Revise Programming Exercise 21.11 to prompt the user to enter year, gender,
  and name and display the ranking for the name. Prompt the user to enter
  another inquiry or exit the program.
*/

import java.util.Scanner;
import java.io.IOException;

public class E21_13 {
  public static void main(String[] args) throws IOException {
    BabyNames bn = new BabyNames();

    Scanner input = new Scanner(System.in);
    do {
      System.out.print("Enter the year: ");
      int year = input.nextInt();

      if (year < 2001 || year > 2010) {
        System.out.println("Year must be between 2001 and 2010");
        continue;
      }

      int index = getIndexForYear(year); // get the array index for year

      System.out.print("Enter the gender (M/F): ");
      String gender = input.next().toLowerCase();

      if (!gender.equals("m") && !gender.equals("f")) {
        System.out.println("Please enter either M or F for gender");
        continue;
      }

      System.out.print("Enter the name: ");
      String name = input.next();

      if (gender.equals("m")) {
        if (bn.getBoyData().get(index).get(name) != null) {
          String rank = bn.getBoyData().get(index).get(name);
          System.out.println("Boy name " + name + " is ranked #" + rank +
            " in year " + year);
        } else {
          System.out.println(name + " not found");
        }
      } else if (gender.equals("f")) {
        if (bn.getGirlData().get(index).get(name) != null) {
          String rank = bn.getGirlData().get(index).get(name);
          System.out.println("Girl name " + name + " is ranked #" + rank +
            " in year " + year);
        } else {
          System.out.println(name + " not found");
        }
      }

      System.out.print("Enter another inquiry? (N to exit): ");
      if (input.next().toLowerCase().equals("n")) {
        System.exit(1);
      }

    } while (true);
  }

  public static int getIndexForYear(int year) {
    for (int i = 0, j = 2001; i < 10; i++, j++) {
      if (j == year) { return i; }
    }
    return -1; // shouldn't happen
  }
}

@@ -636,82 +636,8 @@
  %7D%0A%0A
-      int index = getIndexForYear(year); // get the array index for year%0A%0A
     
@@ -976,353 +976,427 @@
     
-if (gender.equals(%22m%22)) %7B%0A        if (bn.getBoyData().get(index).get(name) != null) %7B%0A          String rank = bn.getBoyData().get(index).get(name);%0A          System.out.println(%22Boy name %22 + name + %22 is ranked #%22 + rank +%0A            %22
+printRanking(bn, year, gender, name);%0A%0A      System.out.print(%22Enter another inquiry? (N to exit): %22);%0A      if (input.next().toLowerCase().equals(%22n%22)) %7B%0A        System.exit(1);%0A      %7D%0A%0A    %7D while (true);%0A  %7D%0A%0A  private static void printRanking(%0A    BabyNames bn,
  in
+t
  year
- %22 + year);%0A        %7D else %7B%0A          System.out.println(name + %22 not found%22);%0A        %7D%0A      %7D else
+, String gender, String name) %7B%0A    int index = getIndexForYear(year);%0A    String rank = %22%22;%0A    StringBuilder result = new StringBuilder();%0A   
  if 
@@ -1403,33 +1403,33 @@
 (gender.equals(%22
-f
+m
 %22)) %7B%0A        if
@@ -1428,24 +1428,24 @@
     
-  if (
+rank = 
 bn.get
-Girl
+Boy
 Data
@@ -1471,36 +1471,57 @@
 ame)
- != null) %7B%0A          String
+;%0A      result.append(%22Boy %22);%0A    %7D else %7B%0A     
  ran
@@ -1573,131 +1573,62 @@
     
-    System.out.println(%22Girl name %22 + name + %22 is ranked #%22 + rank +%0A            %22 in year %22 + year);%0A        %7D else %7B%0A    
+result.append(%22Girl %22);%0A    %7D%0A    if (rank == null) %7B%0A
     
@@ -1652,16 +1652,13 @@
 tln(
-n
+%22N
 ame
- + %22
  not
@@ -1663,16 +1663,17 @@
 ot found
+ 
 %22);%0A    
@@ -1678,187 +1678,131 @@
     
-  %7D%0A 
+return;%0A
     
- %7D%0A
+%7D
 %0A    
-  System.out.print(%22Enter another inquiry? (N to exit): %22);%0A      if (input.next().toLowerCase().equals(%22n%22)) %7B%0A   
+result.append(%22name %22 + name + %22 is ranked #%22 + rank + %22 in year %22 + year);%0A
     
- 
 System.
-exit(1);%0A      %7D%0A%0A    %7D while (true
+out.println(result
 );%0A 
@@ -1804,29 +1804,30 @@
 t);%0A  %7D%0A%0A  p
-ublic
+rivate
  static int 

Refactor result printing into new method