Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Omar Natour
- * 11/4/2016
- * Csc-220 Data Structures
- * Hw 6 Baby names
- * Create a program that orders and combines the amount of baby names from a database
- * Ojnatour0001@studnet.stcc.edu
- */
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Scanner;
- import javafx.application.Application;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.geometry.Insets;
- import javafx.geometry.Pos;
- import javafx.scene.Node;
- import javafx.scene.Scene;
- import javafx.scene.control.*;
- import javafx.scene.layout.*;
- import javafx.scene.text.Text;
- import javafx.stage.Stage;
- public class BabyNameMain extends Application {
- int start = 0;
- int end = 0;
- static int count = 0;
- public static void main(String[] args) {
- launch(args);
- }
- public void callData(int start, int end, int count1) {
- if (isValid(start, end, count1)) {
- tfStatus.setText("good data");
- String sFemale = "";
- String sMale = "";
- ArrayList<BabyName> alFemale = new ArrayList<BabyName>();
- ArrayList<BabyName> alMale = new ArrayList<BabyName>();
- ArrayList<BabyName> alFemale1 = new ArrayList<BabyName>();
- ArrayList<BabyName> alMale1 = new ArrayList<BabyName>();
- for (int h = start; h <= end; h++) {
- try {
- Scanner sc = new Scanner(new File("names/yob" + h + ".txt"));
- String[] tempName = new String[3];
- String temp = sc.nextLine();
- while (temp.matches("([a-zA-Z])+,(F),(\\d)+")) {
- tempName = temp.split(",");
- alFemale.add(new BabyName(tempName[0], tempName[1].charAt(0), Integer.parseInt(tempName[2])));
- temp = sc.nextLine();
- }
- while (temp.matches("([a-zA-Z])+,(M),(\\d)+")) {
- tempName = temp.split(",");
- alMale.add(new BabyName(tempName[0], tempName[1].charAt(0), Integer.parseInt(tempName[2])));
- if (sc.hasNextLine())
- temp = sc.nextLine();
- else
- break;
- }
- sc.close();
- } catch (FileNotFoundException e) {
- tfStatus.setText("File not found.");
- e.printStackTrace();
- } catch (IndexOutOfBoundsException e) {
- tfStatus.setText("Not that many names");
- e.printStackTrace();
- }
- alFemale1 = combine(alFemale);
- alMale1 = combine(alMale);
- }
- Collections.sort(alFemale);
- Collections.sort(alMale);
- for (int i = 0; i < count1; i++) {
- sFemale += (i + 1 + ". " + alFemale.get(i).toString()) + "\n";
- }
- for (int i = 0; i < count1; i++)
- sMale += (i + 1 + ". " + alMale.get(i).toString()) + "\n";
- taFemale.setText(sFemale);
- taMale.setText(sMale);
- } else
- tfStatus.setText("bad data");
- }
- public ArrayList<BabyName> combine(ArrayList<BabyName> a) {
- BabyName temp = null;
- ArrayList<BabyName> alFemale2 = new ArrayList<BabyName>();
- for (int j = 0; j < a.size(); j++) {
- for (int k = j + 1; k < a.size(); k++) {
- if ((a.get(j)).equals(a.get(k))) {
- temp = a.get(j).combine(a.get(k));
- a.remove(k);
- }
- }
- alFemale2.add(temp);
- }
- return alFemale2;
- }
- public boolean isValid(int start, int end, int count) {
- if (start < 1880 || start > 2015)
- return false;
- else if (end < 1880 || end > 2015 || end < start)
- return false;
- else if (count <= 0)
- return false;
- else
- return true;
- }
- //// GUI////////////////////////////////////////////////////////////////////
- BorderPane bPane = new BorderPane();
- TextField tfStatus = new TextField();
- TextArea taMale = new TextArea();
- TextArea taFemale = new TextArea();
- public void start(Stage primaryStage) {
- bPane.setTop(top());
- bPane.setCenter(center());
- bPane.setBottom(bottom());
- Scene sce = new Scene(bPane, 600, 500);
- primaryStage.setTitle("Baby Names");
- primaryStage.setScene(sce);
- primaryStage.show();
- primaryStage.setResizable(false);
- }
- private Node center() {
- HBox center = new HBox(10);
- VBox left = new VBox();
- VBox right = new VBox();
- Text tMale = new Text("Male Names");
- Text tFemale = new Text("Female Names");
- taMale.setPrefSize(285, 400);
- taFemale.setPrefSize(285, 400);
- taMale.setEditable(false);
- taFemale.setEditable(false);
- left.setPadding(new Insets(10, 10, 10, 10));
- right.setPadding(new Insets(10, 10, 10, 10));
- left.getChildren().addAll(tMale, taMale);
- right.getChildren().addAll(tFemale, taFemale);
- center.getChildren().addAll(left, right);
- return center;
- }
- public Pane top() {
- HBox top = new HBox(10);
- Text tStart = new Text("Starting year:");
- TextField tfStart = new TextField();
- Text tEnd = new Text("Ending year:");
- TextField tfEnd = new TextField();
- Text tCount = new Text("How many names:");
- TextField tfCount = new TextField();
- Button btGo = new Button("GO!");
- btGo.setPrefSize(55, 20);
- tfStart.setPrefColumnCount(4);
- tfEnd.setPrefColumnCount(4);
- tfCount.setPrefColumnCount(6);
- EventHandler<ActionEvent> Go = e -> {
- String sStart = tfStart.getText();
- String sEnd = tfEnd.getText();
- String sCount = tfCount.getText();
- if (sStart.matches("(\\d+)") && sEnd.matches("(\\d+)") && sCount.matches("(\\d+)"))
- callData(Integer.parseInt(sStart), Integer.parseInt(sEnd), Integer.parseInt(sCount));
- else if (sStart.matches("(\\d+)") && sEnd.matches("(\\d+)") && sCount.matches(""))
- callData(Integer.parseInt(sStart), Integer.parseInt(sEnd), 100);
- else if (sStart.matches("(\\d+)") && sEnd.matches("") && sCount.matches(""))
- callData(Integer.parseInt(sStart), Integer.parseInt(sStart), 100);
- else if (sStart.matches("(\\d+)") && sEnd.matches("") && sCount.matches("(\\d+)"))
- callData(Integer.parseInt(sStart), Integer.parseInt(sStart), Integer.parseInt(sCount));
- else
- tfStatus.setText("Invalid input.");
- };
- btGo.setOnAction(Go);
- tfStart.setOnAction(Go);
- tfEnd.setOnAction(Go);
- tfCount.setOnAction(Go);
- top.setPadding(new Insets(10, 10, 0, 10));
- top.getChildren().addAll(tStart, tfStart, tEnd, tfEnd, tCount, tfCount, btGo);
- return top;
- }
- public Pane bottom() {
- HBox bottom = new HBox(10);
- Text tStatus = new Text("Status:");
- tfStatus.setPrefColumnCount(20);
- bottom.setPadding(new Insets(0, 0, 10, 0));
- bottom.setAlignment(Pos.CENTER);
- bottom.getChildren().addAll(tStatus, tfStatus);
- return bottom;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement