Advertisement
Guest User

ViewLeague.java

a guest
Jan 23rd, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.58 KB | None | 0 0
  1. package Presentation;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import Domain.Match;
  7. import Domain.Team;
  8. import Logic.KRPLogic;
  9. import javafx.collections.FXCollections;
  10. import javafx.event.ActionEvent;
  11. import javafx.event.EventHandler;
  12. import javafx.geometry.Insets;
  13. import javafx.geometry.Pos;
  14. import javafx.scene.Scene;
  15. import javafx.scene.control.Button;
  16. import javafx.scene.control.TableColumn;
  17. import javafx.scene.control.TableView;
  18. import javafx.scene.control.cell.PropertyValueFactory;
  19. import javafx.scene.layout.GridPane;
  20. import javafx.stage.Stage;
  21.  
  22. public class ViewLeague {
  23.     private Stage stage;
  24.     private GridPane grid;
  25.  
  26.     public ViewLeague(Stage stage) {
  27.         this.stage = stage;
  28.  
  29.     }
  30.  
  31.     @SuppressWarnings("unchecked")
  32.     public void init() {
  33.         stage.setTitle("View league");
  34.         grid = new GridPane();
  35.         grid.setAlignment(Pos.TOP_CENTER);
  36.         grid.setHgap(10);
  37.         grid.setVgap(10);
  38.         grid.setPadding(new Insets(25, 25, 25, 25));
  39.  
  40.         // TableView
  41.         TableView<Team> leagueTable = new TableView<>();
  42.  
  43.         addDataToLeagueTable(leagueTable);
  44.  
  45.         TableColumn<Team, String> stillingCol = new TableColumn<Team, String>("Stilling");
  46.  
  47.         TableColumn<Team, String> holdnavnCol = new TableColumn<Team, String>("Holdnavn");
  48.         holdnavnCol.setCellValueFactory(new PropertyValueFactory<Team, String>("holdnavn"));
  49.  
  50.         TableColumn<Team, String> pointCol = new TableColumn<Team, String>("Points");
  51.         pointCol.setCellValueFactory(new PropertyValueFactory<Team, String>("points"));
  52.  
  53.         leagueTable.setMinSize(800, 600);
  54.         leagueTable.setEditable(true);
  55.  
  56.         leagueTable.getColumns().addAll(stillingCol, holdnavnCol, pointCol);
  57.         grid.add(leagueTable, 0, 0);
  58.  
  59.         // Buttons
  60.         Button tilbage = new Button("return");
  61.         grid.add(tilbage, 0, 5);
  62.         tilbage.setOnAction(new EventHandler<ActionEvent>() {
  63.             @Override
  64.             public void handle(ActionEvent event) {
  65.                 Main view = new Main();
  66.                 view.start(stage);
  67.             }
  68.         });
  69.  
  70.         Button export = new Button("Export");
  71.         grid.add(export, 0, 4);
  72.         export.setOnAction(new EventHandler<ActionEvent>() {
  73.             @Override
  74.             public void handle(ActionEvent event) {
  75.  
  76.             }
  77.         });
  78.  
  79.         Scene viewleague = new Scene(grid, 1200, 800);
  80.         stage.setScene(viewleague);
  81.         stage.setFullScreen(false);
  82.         stage.show();
  83.  
  84.     }
  85.  
  86.     private void addDataToLeagueTable(TableView<Team> leagueTable) {
  87.         List<Team> teamList = new ArrayList<Team>();
  88.  
  89.         // Henter kampe
  90.         List<Match> matchList = KRPLogic.getMatch();
  91.  
  92.         for (int i = 0; i < matchList.size(); i++) {
  93.  
  94.             Team team1 = new Team();
  95.             int team1IDInList = getTeamIndexInList(teamList, matchList.get(i).getHjemmeholdId());
  96.             if (team1IDInList != -1) {
  97.                 team1 = teamList.get(team1IDInList);
  98.             }
  99.             team1.setHoldnavn(matchList.get(i).getHjemmeholdNavn());
  100.             team1.setId(matchList.get(i).getHjemmeholdId());
  101.             Team team2 = new Team();
  102.             int team2IDInList = getTeamIndexInList(teamList, matchList.get(i).getUdeholdId());
  103.             if (team2IDInList != -1) {
  104.                 team2 = teamList.get(team2IDInList);
  105.             }
  106.             team2.setHoldnavn(matchList.get(i).getUdeholdNavn());
  107.             team2.setId(matchList.get(i).getUdeholdId());
  108.             KRPLogic krpLogic = new KRPLogic();
  109.             int team1Goals = krpLogic.selectNumberGoalsInfo(matchList.get(i).getHjemmeholdId(),
  110.                     matchList.get(i).getId());
  111.             int team2Goals = krpLogic.selectNumberGoalsInfo(matchList.get(i).getUdeholdId(), matchList.get(i).getId());
  112.  
  113.             if (team1Goals > team2Goals) {
  114.                 int currentPoints = team1.getPoints();
  115.                 team1.setPoints(currentPoints + 2);
  116.             }
  117.             if (team2Goals > team1Goals) {
  118.                 int currentPoints = team2.getPoints();
  119.                 team2.setPoints(currentPoints + 2);
  120.             }
  121.             if (team2Goals == team1Goals) {
  122.                 int currentPointsTeam2 = team2.getPoints();
  123.                 int currentPointsTeam1 = team1.getPoints();
  124.                 team2.setPoints(currentPointsTeam2 + 1);
  125.                 team1.setPoints(currentPointsTeam1 + 1);
  126.  
  127.             }
  128.  
  129.             if (team1IDInList == -1) {
  130.                 teamList.add(team1);
  131.             }
  132.             if (team2IDInList == -1) {
  133.                 teamList.add(team2);
  134.             }
  135.  
  136.         }
  137.  
  138.         System.out.println("Antal hold i teamlist: " + teamList.size());
  139.         for (int i = 0; i < teamList.size(); i++) {
  140.             System.out.println("Holdnavn: " + teamList.get(i).getHoldnavn());
  141.             System.out.println("Points: " + teamList.get(i).getPoints());
  142.         }
  143.  
  144.         leagueTable.setItems(FXCollections.observableArrayList(teamList));
  145.     }
  146.  
  147.     private int getTeamIndexInList(List<Team> teamList, String teamIDToGet) {
  148.         for (int i = 0; i < teamList.size(); i++) {
  149.  
  150.             String currentTeamID = teamList.get(i).getId();
  151.             boolean sameID = new String(currentTeamID).equals(teamIDToGet);
  152.             if (sameID) {
  153.                 return i;
  154.             }
  155.         }
  156.         return -1;
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement