Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Presentation;
- import java.util.ArrayList;
- import java.util.List;
- import Domain.Match;
- import Domain.Team;
- import Logic.KRPLogic;
- import javafx.collections.FXCollections;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.geometry.Insets;
- import javafx.geometry.Pos;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.TableColumn;
- import javafx.scene.control.TableView;
- import javafx.scene.control.cell.PropertyValueFactory;
- import javafx.scene.layout.GridPane;
- import javafx.stage.Stage;
- public class ViewLeague {
- private Stage stage;
- private GridPane grid;
- public ViewLeague(Stage stage) {
- this.stage = stage;
- }
- @SuppressWarnings("unchecked")
- public void init() {
- stage.setTitle("View league");
- grid = new GridPane();
- grid.setAlignment(Pos.TOP_CENTER);
- grid.setHgap(10);
- grid.setVgap(10);
- grid.setPadding(new Insets(25, 25, 25, 25));
- // TableView
- TableView<Team> leagueTable = new TableView<>();
- addDataToLeagueTable(leagueTable);
- TableColumn<Team, String> stillingCol = new TableColumn<Team, String>("Stilling");
- TableColumn<Team, String> holdnavnCol = new TableColumn<Team, String>("Holdnavn");
- holdnavnCol.setCellValueFactory(new PropertyValueFactory<Team, String>("holdnavn"));
- TableColumn<Team, String> pointCol = new TableColumn<Team, String>("Points");
- pointCol.setCellValueFactory(new PropertyValueFactory<Team, String>("points"));
- leagueTable.setMinSize(800, 600);
- leagueTable.setEditable(true);
- leagueTable.getColumns().addAll(stillingCol, holdnavnCol, pointCol);
- grid.add(leagueTable, 0, 0);
- // Buttons
- Button tilbage = new Button("return");
- grid.add(tilbage, 0, 5);
- tilbage.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- Main view = new Main();
- view.start(stage);
- }
- });
- Button export = new Button("Export");
- grid.add(export, 0, 4);
- export.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- }
- });
- Scene viewleague = new Scene(grid, 1200, 800);
- stage.setScene(viewleague);
- stage.setFullScreen(false);
- stage.show();
- }
- private void addDataToLeagueTable(TableView<Team> leagueTable) {
- List<Team> teamList = new ArrayList<Team>();
- // Henter kampe
- List<Match> matchList = KRPLogic.getMatch();
- for (int i = 0; i < matchList.size(); i++) {
- Team team1 = new Team();
- int team1IDInList = getTeamIndexInList(teamList, matchList.get(i).getHjemmeholdId());
- if (team1IDInList != -1) {
- team1 = teamList.get(team1IDInList);
- }
- team1.setHoldnavn(matchList.get(i).getHjemmeholdNavn());
- team1.setId(matchList.get(i).getHjemmeholdId());
- Team team2 = new Team();
- int team2IDInList = getTeamIndexInList(teamList, matchList.get(i).getUdeholdId());
- if (team2IDInList != -1) {
- team2 = teamList.get(team2IDInList);
- }
- team2.setHoldnavn(matchList.get(i).getUdeholdNavn());
- team2.setId(matchList.get(i).getUdeholdId());
- KRPLogic krpLogic = new KRPLogic();
- int team1Goals = krpLogic.selectNumberGoalsInfo(matchList.get(i).getHjemmeholdId(),
- matchList.get(i).getId());
- int team2Goals = krpLogic.selectNumberGoalsInfo(matchList.get(i).getUdeholdId(), matchList.get(i).getId());
- if (team1Goals > team2Goals) {
- int currentPoints = team1.getPoints();
- team1.setPoints(currentPoints + 2);
- }
- if (team2Goals > team1Goals) {
- int currentPoints = team2.getPoints();
- team2.setPoints(currentPoints + 2);
- }
- if (team2Goals == team1Goals) {
- int currentPointsTeam2 = team2.getPoints();
- int currentPointsTeam1 = team1.getPoints();
- team2.setPoints(currentPointsTeam2 + 1);
- team1.setPoints(currentPointsTeam1 + 1);
- }
- if (team1IDInList == -1) {
- teamList.add(team1);
- }
- if (team2IDInList == -1) {
- teamList.add(team2);
- }
- }
- System.out.println("Antal hold i teamlist: " + teamList.size());
- for (int i = 0; i < teamList.size(); i++) {
- System.out.println("Holdnavn: " + teamList.get(i).getHoldnavn());
- System.out.println("Points: " + teamList.get(i).getPoints());
- }
- leagueTable.setItems(FXCollections.observableArrayList(teamList));
- }
- private int getTeamIndexInList(List<Team> teamList, String teamIDToGet) {
- for (int i = 0; i < teamList.size(); i++) {
- String currentTeamID = teamList.get(i).getId();
- boolean sameID = new String(currentTeamID).equals(teamIDToGet);
- if (sameID) {
- return i;
- }
- }
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement