|
| 1 | +/********************************************************************************* |
| 2 | +* (Baby name popularity ranking) Use the data files from Programming * |
| 3 | +* Exercise 12.31 to write a program that enables the user to select a year, * |
| 4 | +* gender, and enter a name to display the ranking of the name for the selected * |
| 5 | +* year and gender, as shown in Figure 21.9. To achieve the best efficiency, * |
| 6 | +* create two arrays for boy’s names and girl’s names, respectively. Each array * |
| 7 | +* has 10 elements for 10 years. Each element is a map that stores a name and its * |
| 8 | +* ranking in a pair with the name as the key. Assume the data files are stored * |
| 9 | +* at www.cs.armstrong.edu/liang/data/babynamesranking2001.txt, . . . , and * |
| 10 | +* www.cs.armstrong.edu/liang/data/babynamesranking2010.txt. * |
| 11 | +*********************************************************************************/ |
| 12 | +importjavafx.stage.Stage; |
| 13 | +importjavafx.scene.Scene; |
| 14 | +importjavafx.scene.layout.GridPane; |
| 15 | +importjavafx.scene.layout.BorderPane; |
| 16 | +importjavafx.scene.control.Button; |
| 17 | +importjavafx.scene.control.Label; |
| 18 | +importjavafx.scene.control.ComboBox; |
| 19 | +importjavafx.scene.control.TextField; |
| 20 | +importjavafx.application.Application; |
| 21 | +importjavafx.geometry.Insets; |
| 22 | +importjavafx.geometry.Pos; |
| 23 | +importjava.util.*; |
| 24 | + |
| 25 | +publicclassExercise_21_11extendsApplication { |
| 26 | +finalintBOYS_NAMES =1; |
| 27 | +finalintGIRLS_NAMES =3; |
| 28 | +protectedComboBox<String>cboYear =newComboBox<>(); |
| 29 | +protectedComboBox<String>cboGender =newComboBox<>(); |
| 30 | +protectedTextFieldtfName =newTextField(); |
| 31 | +protectedButtonbtFindRanking =newButton("Find Ranking"); |
| 32 | +protectedLabellblResults =newLabel(""); |
| 33 | +protectedMap[]boys =getNames(BOYS_NAMES); |
| 34 | +protectedMap[]girls =getNames(GIRLS_NAMES); |
| 35 | + |
| 36 | +@Override// Override the start method in the Application class |
| 37 | +publicvoidstart(StageprimaryStage) { |
| 38 | + |
| 39 | +// Create and register handle |
| 40 | +btFindRanking.setOnAction(e ->displayRank()); |
| 41 | + |
| 42 | +// Create a Scene and place it in the stage |
| 43 | +Scenescene =newScene(getPane(),300,160); |
| 44 | +primaryStage.setTitle("Exercise_21_11");// Set the stage title |
| 45 | +primaryStage.setScene(scene);// Place the scene in the stage |
| 46 | +primaryStage.show();// Display the stage |
| 47 | +} |
| 48 | + |
| 49 | +/** Displays the ranking for the name of the selected year and gender */ |
| 50 | +privatevoiddisplayRank() { |
| 51 | +lblResults.setText(getGender() +" name " |
| 52 | ++tfName.getText() +" is ranked #" +getRank() |
| 53 | ++" in year " +cboYear.getValue()); |
| 54 | +} |
| 55 | + |
| 56 | +/* Returns selected gender of boy or girl */ |
| 57 | +privateStringgetGender() { |
| 58 | +returncboGender.getValue().equals("Male") ?"Boy" :"Girl"; |
| 59 | +} |
| 60 | + |
| 61 | +/** Returns the ranking for the name of the selected year and gender */ |
| 62 | +privateStringgetRank() { |
| 63 | +intyear =Integer.parseInt(cboYear.getValue()) -2001; |
| 64 | + |
| 65 | +if (cboGender.getValue().equals("Male")) { |
| 66 | +returnboys[year].get(tfName.getText()) +""; |
| 67 | +} |
| 68 | +else |
| 69 | +returngirls[year].get(tfName.getText()) +""; |
| 70 | +} |
| 71 | + |
| 72 | +/** Returns the ranking pane */ |
| 73 | +privateBorderPanegetPane() { |
| 74 | +// Add items to cboYear |
| 75 | +for (inti =2001;i <=2010;i++) |
| 76 | +cboYear.getItems().add(i +""); |
| 77 | + |
| 78 | +// Add items to cboGender |
| 79 | +cboGender.getItems().addAll("Male","Female"); |
| 80 | + |
| 81 | +// Create a grid pane |
| 82 | +GridPanegridPane =newGridPane(); |
| 83 | +gridPane.setVgap(5); |
| 84 | +gridPane.setPadding(newInsets(5,0,5,0)); |
| 85 | +gridPane.setAlignment(Pos.CENTER); |
| 86 | +gridPane.add(newLabel("Select a year: "),0,0); |
| 87 | +gridPane.add(cboYear,1,0); |
| 88 | +gridPane.add(newLabel("Boy or girl?: "),0,1); |
| 89 | +gridPane.add(cboGender,1,1); |
| 90 | +gridPane.add(newLabel("Enter a name: "),0,2); |
| 91 | +gridPane.add(tfName,1,2); |
| 92 | +gridPane.add(btFindRanking,1,3); |
| 93 | + |
| 94 | +// Create a border pane and place node in it |
| 95 | +BorderPanepane =newBorderPane(); |
| 96 | +pane.setCenter(gridPane); |
| 97 | +pane.setBottom(lblResults); |
| 98 | +pane.setAlignment(lblResults,Pos.CENTER); |
| 99 | + |
| 100 | +returnpane; |
| 101 | +} |
| 102 | + |
| 103 | +/** Returns an array of Maps storing ranks |
| 104 | + * to names of a specified gender */ |
| 105 | +privateMap[]getNames(intgender) { |
| 106 | +Map[]array =newMap[10]; |
| 107 | + |
| 108 | +for (intyear =2001,i =0;year <=2010 &&i <10;year++,i++) { |
| 109 | +Map<String,String>map =newHashMap<>(); |
| 110 | +// Read data from url |
| 111 | +try { |
| 112 | +java.net.URLurl =newjava.net.URL( |
| 113 | +"http://www.cs.armstrong.edu/liang/data/babynamesranking" |
| 114 | ++year +".txt"); |
| 115 | + |
| 116 | +// Create input file from url |
| 117 | +Scannerinput =newScanner(url.openStream()); |
| 118 | +while (input.hasNext()) { |
| 119 | +ArrayList<String>list =newArrayList<>(); |
| 120 | +for (intw =0;w <5;w++) { |
| 121 | +list.add(w,input.next()); |
| 122 | +} |
| 123 | +map.put(list.get(gender),list.get(0)); |
| 124 | +} |
| 125 | +} |
| 126 | +catch (java.net.MalformedURLExceptionex) { |
| 127 | +System.out.println("Invalid URL"); |
| 128 | +} |
| 129 | +catch (java.io.IOExceptionex) { |
| 130 | +System.out.println("I/0 Errors: no such file"); |
| 131 | +} |
| 132 | +array[i] =map; |
| 133 | +} |
| 134 | + |
| 135 | +returnarray; |
| 136 | +} |
| 137 | +} |