Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 24.02 KB | None | 0 0
  1. /*
  2.  ============================================================================
  3.  Name: [Redacted]
  4.  Date: 2016/06/20
  5.  File name: Seating.jar
  6.  Description: This program sits families randomly designed for the [Redacted]
  7.  ============================================================================
  8.  */
  9.  
  10. package sample;
  11.  
  12. import javafx.application.Application;
  13. import javafx.geometry.Insets;
  14. import javafx.geometry.Pos;
  15. import javafx.scene.Scene;
  16. import javafx.scene.control.Button;
  17. import javafx.scene.control.Label;
  18. import javafx.scene.control.TextField;
  19. import javafx.scene.layout.*;
  20. import javafx.scene.paint.Color;
  21. import javafx.scene.shape.Rectangle;
  22. import javafx.scene.text.Font;
  23. import javafx.scene.text.FontWeight;
  24. import javafx.stage.Stage;
  25.  
  26. import java.io.*;
  27.  
  28. public class Main extends Application {
  29.   private final int NUMFAM = countFam(); //total number of families
  30.   private Family[] unseated = new Family[NUMFAM];
  31.   private Family[] readToHere = new Family[NUMFAM];
  32.   private Family[] seated = new Family[NUMFAM];
  33.   private final int NUMROWS = 28;
  34.   private final int NUMCOLS = 55;
  35.   private Seat[][] fat = new Seat[NUMROWS][NUMCOLS];
  36.   private GridPane gridPane = new GridPane();
  37.   private int seatCount;
  38.   private int tryCount = 0;
  39.   private Stage stage;
  40.   private TextField[] rowNames = new TextField[NUMROWS];
  41.   private String out;
  42.   private HBox top = new HBox();
  43.  
  44.   /**
  45.    * Counts the total number of families
  46.    *
  47.    * @return the number of families in Seat.txt
  48.    */
  49.   private static int countFam() {
  50.     String fileName = "Seat.txt";
  51.  
  52.     int out = 0;
  53.  
  54.     try {
  55.       FileReader fileReader = new FileReader(fileName);
  56.  
  57.       BufferedReader bufferedReader = new BufferedReader(fileReader);
  58.  
  59.       while (bufferedReader.readLine() != null) {
  60.         out++;
  61.       }
  62.  
  63.       bufferedReader.close();
  64.     } catch (FileNotFoundException ex) {
  65.       System.out.println("Unable to open file '" + fileName + "'");
  66.     } catch (IOException ex) {
  67.       System.out.println("Error reading file '" + fileName + "'");
  68.     }
  69.  
  70.     return out;
  71.   }
  72.  
  73.   /**
  74.    * Creates the window and sets up default FAT layout
  75.    *
  76.    * @param primaryStage the stage
  77.    */
  78.   public void start(Stage primaryStage) {
  79.     BorderPane root;
  80.     Scene scene;
  81.  
  82.     VBox rows = new VBox();
  83.     top.setStyle("-fx-background-color: #773333;");
  84.  
  85.     /* The default row names */
  86.     for (int i = 0; i < NUMROWS; i++) {
  87.       rowNames[i] = new TextField("0");
  88.       rowNames[i].setPrefWidth(39);
  89.       rowNames[i].setFont(Font.font("Verdana", FontWeight.NORMAL, 12.16));
  90.       if (i == 0) {
  91.         rowNames[i].setText("AA");
  92.       }
  93.       if (i == 1) {
  94.         rowNames[i].setText("A");
  95.       }
  96.       if (i == 2) {
  97.         rowNames[i].setText("B");
  98.       }
  99.       if (i == 3) {
  100.         rowNames[i].setText("C");
  101.       }
  102.       if (i == 4) {
  103.         rowNames[i].setText("D");
  104.       }
  105.       if (i == 5) {
  106.         rowNames[i].setText("E");
  107.       }
  108.       if (i == 6) {
  109.         rowNames[i].setText("F");
  110.       }
  111.       if (i == 7) {
  112.         rowNames[i].setText("G");
  113.       }
  114.       if (i == 8) {
  115.         rowNames[i].setText("H");
  116.       }
  117.       if (i == 9) {
  118.         rowNames[i].setText("J");
  119.       }
  120.       if (i == 10) {
  121.         rowNames[i].setText("K");
  122.       }
  123.       if (i == 11) {
  124.         rowNames[i].setText("L");
  125.       }
  126.       if (i == 12) {
  127.         rowNames[i].setText("M");
  128.       }
  129.       if (i == 13) {
  130.         rowNames[i].setText("N");
  131.       }
  132.       if (i == 15) {
  133.         rowNames[i].setText("PP");
  134.       }
  135.       if (i == 16) {
  136.         rowNames[i].setText("P");
  137.       }
  138.       if (i == 17) {
  139.         rowNames[i].setText("R");
  140.       }
  141.       if (i == 18) {
  142.         rowNames[i].setText("S");
  143.       }
  144.       if (i == 19) {
  145.         rowNames[i].setText("T");
  146.       }
  147.       if (i == 20) {
  148.         rowNames[i].setText("U");
  149.       }
  150.       if (i == 21) {
  151.         rowNames[i].setText("V");
  152.       }
  153.       if (i == 22) {
  154.         rowNames[i].setText("W");
  155.       }
  156.       if (i == 23) {
  157.         rowNames[i].setText("X");
  158.       }
  159.       if (i == 24) {
  160.         rowNames[i].setText("XX");
  161.       }
  162.       rows.getChildren().add(rowNames[i]);
  163.     }
  164.  
  165.     /* The default existing red seats in FAT */
  166.     for (int r = 0; r < NUMROWS; r++) {
  167.       for (int c = 0; c < NUMCOLS; c++) {
  168.         Rectangle rec = new Rectangle(24, 24, Color.rgb(204, 0, 0));
  169.         fat[r][c] = new Seat();
  170.         rec.setStroke(Color.BLACK);
  171.         changeRecColor(rec, r, c);
  172.         StackPane s = new StackPane();
  173.         s.getChildren().add(rec);
  174.         gridPane.add(s, c, r);
  175.         if (
  176.             r == 14 ||
  177.                 c == 0 ||
  178.                 c == 1 && r != 23 ||
  179.                 c == 2 && r != 23 ||
  180.                 c == 3 && (r != 22 && r != 23) ||
  181.                 c == 4 && (r != 20 && r != 21 && r != 22 && r != 23) ||
  182.                 c == 5 && (r != 12 && r != 13 && r != 18 && r != 19 && r != 20 && r != 21 && r != 22 && r != 23) ||
  183.                 c == 6 && (r != 10 && r != 11 && r != 12 && r != 13 && r != 16 && r != 17 && r != 18 && r != 19 && r != 20 && r != 21 && r != 22 && r != 23) ||
  184.                 c == 7 && (r != 8 && r != 9 && r != 10 && r != 11 && r != 12 && r != 13 && r != 16 && r != 17 && r != 18 && r != 19 && r != 20 && r != 21 && r != 22 && r != 23) ||
  185.                 c == 8 && (r == 0 || r == 1 || r == 2 || r == 3 || r == 4 || r == 5 || r == 24) ||
  186.                 c == 9 && (r == 0 || r == 1 || r == 2 || r == 3 || r == 24) ||
  187.                 c == 10 && (r == 1 || r == 24) ||
  188.                 (c == 11 || c == 12 || c == 13) && r == 24 ||
  189.                 (c == 14 || c == 15 || c == 16) && r == 24 ||
  190.                 c == 17 ||
  191.                 c == 18 ||
  192.                 c == 19 && (r != 0) ||
  193.                 c == 20 && (r == 15 || r == 23 || r == 24) ||
  194.                 c == 21 && (r == 23 || r == 24) ||
  195.                 (c == 22 || c == 23) && (r == 23 || r == 24) ||
  196.                 (c == 24 || c == 25 || c == 26 || c == 27 || c == 28 || c == 29 || c == 30) && (r == 23 || r == 24) ||
  197.                 (c == 31 || c == 32 || c == 33 || c == 34 || c == 35) && (r == 23 || r == 24) ||
  198.                 c == 36 && (r != 0 && r != 16 && r != 17 && r != 18 && r != 19 && r != 20 && r != 21 && r != 22) ||
  199.                 c == 37 && (r != 23) ||
  200.                 c == 38 && (r != 23) ||
  201.                 c == 39 && (r == 15 || r == 16 || r == 17 || r == 18 || r == 19 || r == 20 || r == 21 || r == 22 || r == 24) ||
  202.                 c == 40 && r == 24 ||
  203.                 (c == 41 || c == 42 || c == 43 || c == 44) && r == 24 ||
  204.                 c == 45 && (r == 1 || r == 24) ||
  205.                 c == 46 && (r < 14 && (r == 0 || r == 1 || r == 2 || r == 3) || r == 24) ||
  206.                 c == 47 && (r < 14 && (r == 0 || r == 1 || r == 2 || r == 3 || r == 4 || r == 5) || r == 24) ||
  207.                 c == 48 && (r != 8 && r != 9 && r != 10 && r != 11 && r != 12 && r != 13 && r != 15 && r != 16 && r != 17 && r != 18 && r != 19 && r != 20 && r != 21 && r != 22 && r != 23) ||
  208.                 c == 49 && (r != 10 && r != 11 && r != 12 && r != 13 && r != 16 && r != 17 && r != 18 && r != 19 && r != 20 && r != 21 && r != 22 && r != 23) ||
  209.                 c == 50 && (r != 12 && r != 13 && r != 16 && r != 17 && r != 18 && r != 19 && r != 20 && r != 21 && r != 22 && r != 23) ||
  210.                 c == 51 && (r < 14 || r > 14 && (r == 15 || r == 16 || r == 17 || r == 24)) ||
  211.                 c == 52 && (r != 20 && r != 21 && r != 22 && r != 23) ||
  212.                 c == 53 && (r != 22 && r != 23) ||
  213.                 c == 54 ||
  214.                 r > 24
  215.         ) {
  216.           rec.setFill(Color.WHITE);
  217.           fat[r][c] = null;
  218.         }
  219.       }
  220.     }
  221.  
  222.     stage = primaryStage;
  223.     root = new BorderPane();
  224.     scene = new Scene(root, 1450, 770);
  225.     root.setPadding(new Insets(15));
  226.     stage.setScene(scene);
  227.     stage.setTitle("Seating");
  228.     root.setCenter(gridPane);
  229.     root.setTop(top);
  230.     root.setLeft(rows);
  231.     Button sit = new Button("Sit");
  232.     sit.setFont(Font.font("Verdana", FontWeight.BOLD, 22));
  233.     sit.setOnMouseClicked(event -> {
  234.       Seating();
  235.     });
  236.     top.getChildren().add(sit);
  237.     top.setAlignment(Pos.CENTER);
  238.  
  239.     stage.setResizable(false);
  240.     stage.show();
  241.   }
  242.  
  243.   /**
  244.    * Private helper method to change the rectangle colour from white to red
  245.    * and vice versa
  246.    *
  247.    * @param rec the rectangel that needs to change colour
  248.    * @param r   the row of the rectangle in FAT
  249.    * @param c   the column of the rectangle in FAT
  250.    */
  251.   private void changeRecColor(Rectangle rec, int r, int c) {
  252.     rec.setOnMouseClicked(event -> {
  253.       if (rec.getFill().equals(Color.WHITE)) {
  254.         rec.setFill(Color.rgb(125, 0, 0));
  255.         fat[r][c] = new Seat();
  256.         seatCount++;
  257.       } else {
  258.         rec.setFill(Color.WHITE);
  259.         fat[r][c] = null;
  260.         seatCount--;
  261.       }
  262.     });
  263.   }
  264.  
  265.   /**
  266.    * The method that sits all the families down in the FAT and updates the window
  267.    */
  268.   private void Seating() {
  269.  
  270.     /* Read and set the number of people un each family */
  271.     String fileName = "Seat.txt";
  272.  
  273.     String line;
  274.     String famName;
  275.     int famNum;
  276.  
  277.     try {
  278.       FileReader fileReader = new FileReader(fileName);
  279.  
  280.       BufferedReader bufferedReader = new BufferedReader(fileReader);
  281.  
  282.       for (int i = 0; i < NUMFAM; i++) {
  283.         line = bufferedReader.readLine();
  284.         if (line != null) {
  285.           String[] pieces = line.split(";");
  286.           famName = pieces[0];
  287.           famNum = Integer.parseInt(pieces[1]);
  288.           readToHere[i] = new Family(i + 1, famNum, famName);
  289.         }
  290.       }
  291.  
  292.       bufferedReader.close();
  293.     } catch (FileNotFoundException ex) {
  294.       System.out.println("Unable to open file '" + fileName + "'");
  295.     } catch (IOException ex) {
  296.       System.out.println("Error reading file '" + fileName + "'");
  297.     }
  298.  
  299.     for (int i = 0; i < NUMFAM; i++) {
  300.       for (int j = 0; j < NUMFAM - 1; j++) {
  301.         if (readToHere[j].getNum() > readToHere[j + 1].getNum()) {
  302.           Family temp = readToHere[j];
  303.           readToHere[j] = readToHere[j + 1];
  304.           readToHere[j + 1] = temp;
  305.         }
  306.       }
  307.     }
  308.  
  309.     do {
  310.       tryCount++;
  311.       /* Set appropriate seats to null */
  312.       for (int r = 0; r < NUMROWS; r++) {
  313.         for (int c = 0; c < NUMCOLS; c++) {
  314.           if (fat[r][c] != null) {
  315.             fat[r][c].setFamily(null);
  316.           }
  317.         }
  318.       }
  319.  
  320.  
  321.       /* Name the rows and columns */
  322.       for (int r = 0; r < NUMROWS; r++) {
  323.         for (int c = 0; c < NUMCOLS; c++) {
  324.           if (fat[r][c] != null) {
  325.  
  326.             /* Naming the colums, only to be implemented like this in the case of the FAT where
  327.              * the ground floor has extra chairs on the left */
  328.             if (r < 14 && r != 0) {
  329.               fat[r][c].setCol(c);
  330.             } else {
  331.               fat[r][c].setCol(c + 1);
  332.             }
  333.  
  334.             fat[r][c].setRow(rowNames[r].getText());
  335.           }
  336.         }
  337.       }
  338.  
  339.       /* Read and set the number of people un each family */
  340.       for (int i = 0; i < NUMFAM; i++) {
  341.         unseated[i] = readToHere[i];
  342.       }
  343.  
  344.       /* Seating V2.1 */
  345.  
  346.       int famCount = 0;
  347.       int satCount = 0;
  348.  
  349.       System.out.println();
  350.       System.out.println();
  351.  
  352.       for (int r = 0; r < NUMROWS; r++) {
  353.         for (int c = 0; c < NUMCOLS; c++) {
  354.           int cur = 0;
  355.           boolean valid = false;
  356.           boolean possible = true;
  357.  
  358.           if (fat[r][c] != null && unseated[0] != null) {
  359.             for (int i = 0; i < unseated[0].getNum(); i++) {
  360.               if (c + i >= NUMCOLS || fat[r][c + i] == null) {
  361.                 possible = false;
  362.               }
  363.             }
  364.  
  365.             if (possible) {
  366.               while (!valid) {
  367.                 valid = true;
  368.                 cur = (int) (Math.random() * (NUMFAM - famCount));
  369.  
  370.                 for (int i = 0; i < unseated[cur].getNum(); i++) {
  371.                   if (c + i >= NUMCOLS || fat[r][c + i] == null) {
  372.                     valid = false;
  373.                   }
  374.                 }
  375.  
  376.               }
  377.  
  378.               satCount += unseated[cur].getNum();
  379.  
  380.               /* Sit the family down */
  381.               for (int j = 0; j < unseated[cur].getNum(); j++) {
  382.                 unseated[cur].sit(fat[r][c + j]);
  383.               }
  384.               seated[famCount] = unseated[cur];
  385.  
  386.               c += (unseated[cur].getNum() - 1);
  387.  
  388.               unseated[cur] = null;
  389.  
  390.               for (int j = cur; j < unseated.length - 1; j++) {
  391.                 unseated[j] = unseated[j + 1];
  392.               }
  393.  
  394.               unseated[unseated.length - 1] = null;
  395.  
  396.               famCount++;
  397.             }
  398.           }
  399.         }
  400.       }
  401.  
  402.       System.out.println("All Seated!");
  403.  
  404.       int emCount = 0;
  405.       int stdCount = 0;
  406.  
  407.       /* Print out the seating arrangement */
  408.       for (int r = 0; r < NUMROWS; r++) {
  409.         if (fat[r][16] != null)
  410.           System.out.printf("ROW: %2s||", fat[r][16].getRow());
  411.         else
  412.           System.out.print("ROW: XX||");
  413.         for (int c = 0; c < NUMCOLS; c++) {
  414.           if (fat[r][c] != null && fat[r][c].getFam() != null) {
  415.             System.out.printf("%3d", fat[r][c].getFam().getId());
  416.           } else if (fat[r][c] == null) {
  417.             System.out.print(" X ");
  418.           } else {
  419.             System.out.print(" E ");
  420.             emCount++;
  421.           }
  422.         }
  423.         System.out.println();
  424.       }
  425.  
  426.       System.out.println();
  427.       System.out.println();
  428.  
  429.       System.out.println("Standing fams: " + (NUMFAM - famCount));
  430.       System.out.println("Sitting people: " + satCount);
  431.       for (int i = 0; i < NUMFAM - famCount; i++) {
  432.         stdCount += unseated[i].getNum();
  433.       }
  434.       System.out.println("Standing people: " + stdCount);
  435.       System.out.println("Free Seats: " + emCount);
  436.       System.out.println("Total seats " + seatCount);
  437.       System.out.println("Number of tries " + tryCount);
  438.  
  439.     } while (unseated[0] != null);
  440.  
  441.     resetSeats();
  442.  
  443.     top.getChildren().clear();
  444.     Button output = new Button("Output");
  445.     output.setFont(Font.font("Verdana", FontWeight.BOLD, 22));
  446.     output.setOnMouseClicked(event -> {
  447.       outPut();
  448.     });
  449.     top.getChildren().add(output);
  450.  
  451.   }
  452.  
  453.   /**
  454.    * Refreshes the window to represent the actual FAT layout
  455.    */
  456.   private void resetSeats() {
  457.     gridPane.getChildren().clear();
  458.  
  459.     boolean colorChoose = true;
  460.  
  461.     for (int r = 0; r < NUMROWS; r++) {
  462.       for (int c = 0; c < NUMCOLS; c++) {
  463.         Rectangle rec = new Rectangle(24, 24, Color.WHITE);
  464.  
  465.         rec.setStroke(Color.BLACK);
  466.  
  467.         StackPane s = new StackPane();
  468.         if (fat[r][c] != null && fat[r][c].getFam() != null) {
  469.           if (colorChoose) {
  470.             rec.setFill(Color.rgb(204, 204, 204));
  471.           } else {
  472.             rec.setFill(Color.rgb(170, 170, 170));
  473.           }
  474.           if (fat[r][c + 1] == null || fat[r][c + 1].getFam() == null || fat[r][c].getFam().getId() != fat[r][c + 1].getFam().getId()) {
  475.             colorChoose = !colorChoose;
  476.           }
  477.           Family curFam = fat[r][c].getFam();
  478.           s.setOnMouseClicked(event -> {
  479.             switchColours(curFam);
  480.           });
  481.           s.getChildren().addAll(rec, new Label(fat[r][c].getFam().getId() + ""));
  482.         } else if (fat[r][c] != null && fat[r][c].getFam() == null) {
  483.           rec.setFill(Color.rgb(255, 0, 0));
  484.           s.getChildren().addAll(rec, new Label("E"));
  485.           int i = 0;
  486.           while (fat[r][c - i] != null && fat[r][c - i].getFam() == null) {
  487.             i++;
  488.           }
  489.           int numEmpt = i;
  490.           if (fat[r][c - i] != null && fat[r][c - i].getFam() != null) {
  491.             Family curFam = fat[r][c - i].getFam();
  492.             s.setOnMouseClicked(event -> {
  493.               switchColoursEmpt(curFam, numEmpt);
  494.             });
  495.           }
  496.         } else {
  497.           s.getChildren().add(rec);
  498.         }
  499.  
  500.         gridPane.add(s, c, r);
  501.       }
  502.     }
  503.   }
  504.  
  505.   /**
  506.    * Triggered on the selection of a family and will highlight all the families
  507.    * with the same number of people as family f
  508.    *
  509.    * @param f the family that was clicked on to trigger this method
  510.    */
  511.   private void switchColours(Family f) {
  512.     resetSeats();
  513.     int numPeople = f.getNum();
  514.     boolean colorChoose = true;
  515.     boolean rightSize;
  516.  
  517.     for (int r = 0; r < NUMROWS; r++) {
  518.       for (int c = 0; c < NUMCOLS; c++) {
  519.         rightSize = false;
  520.  
  521.         if (fat[r][c] != null) {
  522.           if (fat[r][c].getFam() != null && fat[r][c].getFam().getNum() == numPeople) {
  523.             rightSize = true;
  524.           }
  525.         }
  526.  
  527.         if (rightSize) {
  528.           Rectangle rec = new Rectangle(24, 24, Color.rgb(200, 50, 0));
  529.           if (colorChoose) {
  530.             rec.setFill(Color.rgb(50, 200, 0));
  531.           } else {
  532.             rec.setFill(Color.rgb(120, 200, 0));
  533.           }
  534.           if (fat[r][c].getFam().getId() == f.getId()) {
  535.             rec.setFill(Color.rgb(150, 0, 0));
  536.           }
  537.           if (fat[r][c + 1] == null || fat[r][c + 1].getFam() == null || fat[r][c].getFam().getId() != fat[r][c + 1].getFam().getId()) {
  538.             colorChoose = !colorChoose;
  539.           }
  540.           rec.setStroke(Color.BLACK);
  541.           StackPane s = new StackPane();
  542.           s.getChildren().addAll(rec, new Label(fat[r][c].getFam().getId() + ""));
  543.           Family o = fat[r][c].getFam();
  544.           s.setOnMouseClicked(event -> {
  545.             switchSpots(f, o);
  546.           });
  547.           gridPane.add(s, c, r);
  548.  
  549.         }
  550.       }
  551.     }
  552.   }
  553.  
  554.   /**
  555.    * switches the positions of family one and two in the FAT and calls resetSeats()
  556.    *
  557.    * @param o family one
  558.    * @param t family two
  559.    */
  560.   private void switchSpots(Family o, Family t) {
  561.     int rowO = 0;
  562.     int rowT = 0;
  563.     int colO = 0;
  564.     int colT = 0;
  565.     int count = 0;
  566.  
  567.     /* Figure out the FAT row and column of the two families */
  568.     while (rowO == 0 && count < NUMCOLS) {
  569.       for (int i = 0; i < NUMROWS; i++) {
  570.         if (fat[i][count] != null && fat[i][count].getRow().equals(o.getSeat().getRow())) {
  571.           rowO = i;
  572.         }
  573.       }
  574.       count++;
  575.     }
  576.  
  577.     count = 0;
  578.  
  579.     while (rowT == 0 && count < NUMCOLS) {
  580.       for (int i = 0; i < NUMROWS; i++) {
  581.         if (fat[i][count] != null && fat[i][count].getRow().equals(t.getSeat().getRow())) {
  582.           rowT = i;
  583.         }
  584.       }
  585.       count++;
  586.     }
  587.  
  588.     for (int i = 0; i < NUMCOLS; i++) {
  589.       if (fat[rowT][i] != null && fat[rowT][i].getCol() == t.getSeat().getCol()) {
  590.         colT = i;
  591.       }
  592.     }
  593.  
  594.     for (int i = 0; i < NUMCOLS; i++) {
  595.       if (fat[rowO][i] != null && fat[rowO][i].getCol() == o.getSeat().getCol()) {
  596.         colO = i;
  597.       }
  598.     }
  599.  
  600.     /* Sit the families down */
  601.     for (int i = o.getNum() - 1; i >= 0; i--) {
  602.       t.sit(fat[rowO][colO - i]);
  603.     }
  604.  
  605.     for (int i = t.getNum() - 1; i >= 0; i--) {
  606.       o.sit(fat[rowT][colT - i]);
  607.     }
  608.  
  609.     resetSeats();
  610.   }
  611.  
  612.   /**
  613.    * Similar to switchColours() except is only triggered when an empty seat is clicked
  614.    * highlights families of the same size as family f (the closest family on the left of the empty seat)
  615.    * plus the number of empty seats on the left of the triggered seat
  616.    *
  617.    * @param f       the closest family on the left of the empty seat
  618.    * @param numEmpt the extra number of empty seats
  619.    */
  620.   private void switchColoursEmpt(Family f, int numEmpt) {
  621.     resetSeats();
  622.     int numPeople = f.getNum();
  623.     boolean colorChoose = true;
  624.     boolean rightSize;
  625.  
  626.     for (int r = 0; r < NUMROWS; r++) {
  627.       for (int c = 0; c < NUMCOLS; c++) {
  628.         rightSize = false;
  629.  
  630.         if (fat[r][c] != null) {
  631.           if (fat[r][c].getFam() != null && (fat[r][c].getFam().getNum() == numPeople + numEmpt || fat[r][c].getFam().getId() == f.getId())) {
  632.             rightSize = true;
  633.           }
  634.         }
  635.  
  636.         if (rightSize) {
  637.           Rectangle rec = new Rectangle(24, 24, Color.rgb(200, 50, 0));
  638.           if (colorChoose) {
  639.             rec.setFill(Color.rgb(50, 200, 0));
  640.           } else {
  641.             rec.setFill(Color.rgb(120, 200, 0));
  642.           }
  643.           if (fat[r][c].getFam().getId() == f.getId()) {
  644.             rec.setFill(Color.rgb(150, 0, 0));
  645.           }
  646.           if (fat[r][c + 1] == null || fat[r][c + 1].getFam() == null || fat[r][c].getFam().getId() != fat[r][c + 1].getFam().getId()) {
  647.             colorChoose = !colorChoose;
  648.           }
  649.           rec.setStroke(Color.BLACK);
  650.           StackPane s = new StackPane();
  651.           s.getChildren().addAll(rec, new Label(fat[r][c].getFam().getId() + ""));
  652.           Family o = fat[r][c].getFam();
  653.           s.setOnMouseClicked(event -> {
  654.             switchSpots(f, o, numEmpt);
  655.           });
  656.           gridPane.add(s, c, r);
  657.  
  658.         }
  659.       }
  660.     }
  661.  
  662.   }
  663.  
  664.   /**
  665.    * Overloaded switchSpots() method except this one also takes into account the possiblity of
  666.    * empty seats
  667.    *
  668.    * @param o       family one
  669.    * @param t       family two
  670.    * @param numEmpt the number of empty seats to take into account
  671.    */
  672.   private void switchSpots(Family o, Family t, int numEmpt) {
  673.     if (o.getId() != t.getId()) {
  674.       int rowO = 0;
  675.       int rowT = 0;
  676.       int colO = 0;
  677.       int colT = 0;
  678.       int count = 0;
  679.  
  680.       while (rowO == 0 && count < NUMCOLS) {
  681.         for (int i = 0; i < NUMROWS; i++) {
  682.           if (fat[i][count] != null && fat[i][count].getRow().equals(o.getSeat().getRow())) {
  683.             rowO = i;
  684.           }
  685.         }
  686.         count++;
  687.       }
  688.  
  689.       count = 0;
  690.  
  691.       while (rowT == 0 && count < NUMCOLS) {
  692.         for (int i = 0; i < NUMROWS; i++) {
  693.           if (fat[i][count] != null && fat[i][count].getRow().equals(t.getSeat().getRow())) {
  694.             rowT = i;
  695.           }
  696.         }
  697.         count++;
  698.       }
  699.  
  700.       for (int i = 0; i < NUMCOLS; i++) {
  701.         if (fat[rowT][i] != null && fat[rowT][i].getCol() == t.getSeat().getCol()) {
  702.           colT = i;
  703.         }
  704.       }
  705.  
  706.       for (int i = 0; i < NUMCOLS; i++) {
  707.         if (fat[rowO][i] != null && fat[rowO][i].getCol() == o.getSeat().getCol()) {
  708.           colO = i;
  709.         }
  710.       }
  711.  
  712.  
  713.       for (int i = t.getNum() - 1 - numEmpt; i >= 0 - numEmpt; i--) {
  714.         t.sit(fat[rowO][colO - i]);
  715.       }
  716.  
  717.       for (int i = o.getNum() - 1 + numEmpt; i >= numEmpt; i--) {
  718.         o.sit(fat[rowT][colT - i]);
  719.       }
  720.  
  721.       for (int i = 0; i < numEmpt; i++) {
  722.         fat[rowT][colT - i].setFamily(null);
  723.       }
  724.  
  725.     }
  726.     resetSeats();
  727.  
  728.   }
  729.  
  730.   /**
  731.    * Outputs the current seats per each family onto out_seat.txt
  732.    */
  733.   private void outPut() {
  734.  
  735.     out = "out_seat.txt";
  736.  
  737.  
  738.     try {
  739.       FileWriter fileWriter = new FileWriter(out);
  740.  
  741.       BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
  742.  
  743.       /* Bubblesort the families in ID order */
  744.       for (int i = 0; i < seated.length - 1; i++) {
  745.         for (int j = 0; j < seated.length - 1; j++) {
  746.           if (seated[j + 1] != null) {
  747.             if (seated[j].getId() > seated[j + 1].getId()) {
  748.               Family temp = seated[j];
  749.               seated[j] = seated[j + 1];
  750.               seated[j + 1] = temp;
  751.             }
  752.           }
  753.         }
  754.       }
  755.  
  756.       /* Print out the ordered families and their seats onto a file */
  757.       for (int i = 0; i < unseated.length; i++) {
  758.         if (seated[i] != null && seated[i].getSeat() != null) {
  759.           bufferedWriter.write(seated[i].getName() + ";");
  760.           for (int j = 0; j < seated[i].getNum(); j++) {
  761.             bufferedWriter.write(seated[i].getSeat().getRow() + "" + (seated[i].getSeat().getCol() - j) + " ");
  762.           }
  763.  
  764.           bufferedWriter.newLine();
  765.         } else {
  766.           bufferedWriter.write("SEATLESS");
  767.           bufferedWriter.newLine();
  768.         }
  769.       }
  770.  
  771.       bufferedWriter.close();
  772.     } catch (IOException ex) {
  773.       System.out.println("Error writing to file '" + out + "'");
  774.     }
  775.  
  776.     //TODO: Add a search bar for a particular family id <-- extra feature
  777.     //TODO: Notice groups of empty seats <-- more important
  778.   }
  779.  
  780.   public static void main(String[] args) {
  781.     launch(args);
  782.   }
  783.  
  784. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement