Advertisement
Omar_Natour

Natour, O. 11/4/16 Csc-220 BabyNamesMain

Nov 5th, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.36 KB | None | 0 0
  1.  
  2. /*
  3.  * Omar Natour
  4.  * 11/4/2016
  5.  * Csc-220 Data Structures
  6.  * Hw 6 Baby names
  7.  * Create a program that orders and combines the amount of baby names from a database
  8.  * Ojnatour0001@studnet.stcc.edu
  9.  */
  10.  
  11. import java.io.File;
  12. import java.io.FileNotFoundException;
  13. import java.util.ArrayList;
  14. import java.util.Collections;
  15. import java.util.Scanner;
  16. import javafx.application.Application;
  17. import javafx.event.ActionEvent;
  18. import javafx.event.EventHandler;
  19. import javafx.geometry.Insets;
  20. import javafx.geometry.Pos;
  21. import javafx.scene.Node;
  22. import javafx.scene.Scene;
  23. import javafx.scene.control.*;
  24. import javafx.scene.layout.*;
  25. import javafx.scene.text.Text;
  26. import javafx.stage.Stage;
  27.  
  28. public class BabyNameMain extends Application {
  29.  
  30.     int start = 0;
  31.     int end = 0;
  32.     static int count = 0;
  33.  
  34.     public static void main(String[] args) {
  35.  
  36.         launch(args);
  37.     }
  38.  
  39.     public void callData(int start, int end, int count1) {
  40.  
  41.         if (isValid(start, end, count1)) {
  42.             tfStatus.setText("good data");
  43.  
  44.             String sFemale = "";
  45.             String sMale = "";
  46.  
  47.             ArrayList<BabyName> alFemale = new ArrayList<BabyName>();
  48.             ArrayList<BabyName> alMale = new ArrayList<BabyName>();
  49.             ArrayList<BabyName> alFemale1 = new ArrayList<BabyName>();
  50.             ArrayList<BabyName> alMale1 = new ArrayList<BabyName>();
  51.  
  52.             for (int h = start; h <= end; h++) {
  53.                 try {
  54.                     Scanner sc = new Scanner(new File("names/yob" + h + ".txt"));
  55.  
  56.                     String[] tempName = new String[3];
  57.  
  58.                     String temp = sc.nextLine();
  59.  
  60.                     while (temp.matches("([a-zA-Z])+,(F),(\\d)+")) {
  61.                         tempName = temp.split(",");
  62.  
  63.                         alFemale.add(new BabyName(tempName[0], tempName[1].charAt(0), Integer.parseInt(tempName[2])));
  64.  
  65.                         temp = sc.nextLine();
  66.  
  67.                     }
  68.                     while (temp.matches("([a-zA-Z])+,(M),(\\d)+")) {
  69.                         tempName = temp.split(",");
  70.  
  71.                         alMale.add(new BabyName(tempName[0], tempName[1].charAt(0), Integer.parseInt(tempName[2])));
  72.  
  73.                         if (sc.hasNextLine())
  74.                             temp = sc.nextLine();
  75.                         else
  76.                             break;
  77.                     }
  78.                     sc.close();
  79.  
  80.                 } catch (FileNotFoundException e) {
  81.                     tfStatus.setText("File not found.");
  82.                     e.printStackTrace();
  83.  
  84.                 } catch (IndexOutOfBoundsException e) {
  85.                     tfStatus.setText("Not that many names");
  86.                     e.printStackTrace();
  87.                 }
  88.                 alFemale1 = combine(alFemale);
  89.                 alMale1 = combine(alMale);
  90.             }
  91.  
  92.             Collections.sort(alFemale);
  93.             Collections.sort(alMale);
  94.  
  95.             for (int i = 0; i < count1; i++) {
  96.                 sFemale += (i + 1 + ". " + alFemale.get(i).toString()) + "\n";
  97.  
  98.             }
  99.  
  100.             for (int i = 0; i < count1; i++)
  101.                 sMale += (i + 1 + ". " + alMale.get(i).toString()) + "\n";
  102.  
  103.             taFemale.setText(sFemale);
  104.             taMale.setText(sMale);
  105.  
  106.         } else
  107.             tfStatus.setText("bad data");
  108.  
  109.     }
  110.  
  111.     public ArrayList<BabyName> combine(ArrayList<BabyName> a) {
  112.  
  113.         BabyName temp = null;
  114.         ArrayList<BabyName> alFemale2 = new ArrayList<BabyName>();
  115.  
  116.         for (int j = 0; j < a.size(); j++) {
  117.             for (int k = j + 1; k < a.size(); k++) {
  118.                 if ((a.get(j)).equals(a.get(k))) {
  119.                     temp = a.get(j).combine(a.get(k));
  120.                     a.remove(k);
  121.                 }
  122.             }
  123.             alFemale2.add(temp);
  124.         }
  125.  
  126.         return alFemale2;
  127.     }
  128.  
  129.     public boolean isValid(int start, int end, int count) {
  130.  
  131.         if (start < 1880 || start > 2015)
  132.             return false;
  133.         else if (end < 1880 || end > 2015 || end < start)
  134.             return false;
  135.         else if (count <= 0)
  136.             return false;
  137.         else
  138.             return true;
  139.     }
  140.  
  141.     //// GUI////////////////////////////////////////////////////////////////////
  142.  
  143.     BorderPane bPane = new BorderPane();
  144.     TextField tfStatus = new TextField();
  145.     TextArea taMale = new TextArea();
  146.     TextArea taFemale = new TextArea();
  147.  
  148.     public void start(Stage primaryStage) {
  149.  
  150.         bPane.setTop(top());
  151.         bPane.setCenter(center());
  152.         bPane.setBottom(bottom());
  153.  
  154.         Scene sce = new Scene(bPane, 600, 500);
  155.  
  156.         primaryStage.setTitle("Baby Names");
  157.         primaryStage.setScene(sce);
  158.         primaryStage.show();
  159.         primaryStage.setResizable(false);
  160.  
  161.     }
  162.  
  163.     private Node center() {
  164.  
  165.         HBox center = new HBox(10);
  166.         VBox left = new VBox();
  167.         VBox right = new VBox();
  168.  
  169.         Text tMale = new Text("Male Names");
  170.         Text tFemale = new Text("Female Names");
  171.  
  172.         taMale.setPrefSize(285, 400);
  173.         taFemale.setPrefSize(285, 400);
  174.         taMale.setEditable(false);
  175.         taFemale.setEditable(false);
  176.  
  177.         left.setPadding(new Insets(10, 10, 10, 10));
  178.         right.setPadding(new Insets(10, 10, 10, 10));
  179.  
  180.         left.getChildren().addAll(tMale, taMale);
  181.         right.getChildren().addAll(tFemale, taFemale);
  182.  
  183.         center.getChildren().addAll(left, right);
  184.  
  185.         return center;
  186.     }
  187.  
  188.     public Pane top() {
  189.  
  190.         HBox top = new HBox(10);
  191.  
  192.         Text tStart = new Text("Starting year:");
  193.         TextField tfStart = new TextField();
  194.         Text tEnd = new Text("Ending year:");
  195.         TextField tfEnd = new TextField();
  196.         Text tCount = new Text("How many names:");
  197.         TextField tfCount = new TextField();
  198.         Button btGo = new Button("GO!");
  199.  
  200.         btGo.setPrefSize(55, 20);
  201.         tfStart.setPrefColumnCount(4);
  202.         tfEnd.setPrefColumnCount(4);
  203.         tfCount.setPrefColumnCount(6);
  204.  
  205.         EventHandler<ActionEvent> Go = e -> {
  206.             String sStart = tfStart.getText();
  207.             String sEnd = tfEnd.getText();
  208.             String sCount = tfCount.getText();
  209.  
  210.             if (sStart.matches("(\\d+)") && sEnd.matches("(\\d+)") && sCount.matches("(\\d+)"))
  211.                 callData(Integer.parseInt(sStart), Integer.parseInt(sEnd), Integer.parseInt(sCount));
  212.             else if (sStart.matches("(\\d+)") && sEnd.matches("(\\d+)") && sCount.matches(""))
  213.                 callData(Integer.parseInt(sStart), Integer.parseInt(sEnd), 100);
  214.             else if (sStart.matches("(\\d+)") && sEnd.matches("") && sCount.matches(""))
  215.                 callData(Integer.parseInt(sStart), Integer.parseInt(sStart), 100);
  216.             else if (sStart.matches("(\\d+)") && sEnd.matches("") && sCount.matches("(\\d+)"))
  217.                 callData(Integer.parseInt(sStart), Integer.parseInt(sStart), Integer.parseInt(sCount));
  218.             else
  219.                 tfStatus.setText("Invalid input.");
  220.  
  221.         };
  222.  
  223.         btGo.setOnAction(Go);
  224.         tfStart.setOnAction(Go);
  225.         tfEnd.setOnAction(Go);
  226.         tfCount.setOnAction(Go);
  227.  
  228.         top.setPadding(new Insets(10, 10, 0, 10));
  229.  
  230.         top.getChildren().addAll(tStart, tfStart, tEnd, tfEnd, tCount, tfCount, btGo);
  231.  
  232.         return top;
  233.  
  234.     }
  235.  
  236.     public Pane bottom() {
  237.         HBox bottom = new HBox(10);
  238.  
  239.         Text tStatus = new Text("Status:");
  240.  
  241.         tfStatus.setPrefColumnCount(20);
  242.  
  243.         bottom.setPadding(new Insets(0, 0, 10, 0));
  244.         bottom.setAlignment(Pos.CENTER);
  245.  
  246.         bottom.getChildren().addAll(tStatus, tfStatus);
  247.  
  248.         return bottom;
  249.     }
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement