summaryrefslogtreecommitdiff
path: root/Java-Kompendium/kap15/src/main/java/Marks.java
blob: 6ec6c06f6a1d3f3389c79ec693ad80afd23d4e51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import java.sql.*;

public class Marks extends Application {
    private GridPane grid;
    private Text status;
    private Connection connection;

    @Override
    public void start(Stage stage) {
        this.grid = new GridPane();
        this.createStatus();
        this.createTable();
        this.createForm();
        this.createSearch();

        this.grid.setVgap(5);
        this.grid.setHgap(5);
        this.grid.setAlignment(Pos.CENTER);

        var scene = new Scene(this.grid, 500, 400);

        stage.setScene(scene);
        stage.setTitle("Noten von Schülern");
        stage.show();
    }

    private void createStatus() {
        this.status = new Text();

        this.grid.add(this.status, 0, 0, 2, 1);
    }

    private void createSearch() {
        var field = new TextField();
        var button = new Button("Suchen");
        var line = new Separator();
        var table = new TableView<Mark>();

        try {
            var handler = new SearchAction(this.connection, this.status, field);
            table.setItems(handler.getMarks());

            var subjectColumn = new TableColumn<Mark, String>("Fach");
            subjectColumn.setCellValueFactory(
                new PropertyValueFactory<>("subject" /* handler.getMarks().get(0).subjectProperty().getName() */)
            );

            var markColumn = new TableColumn<Mark, Integer>("Note");
            markColumn.setCellValueFactory(
                new PropertyValueFactory<>("mark" /* handler.getMarks().get(0).markProperty().getName() */)
            );
            table.getColumns().setAll(subjectColumn, markColumn);

            button.setOnAction(handler);
        } catch (SQLException exception) {
            this.status.setText(exception.getMessage());
        }
        line.setMinSize(10, 10);

        this.grid.add(line, 0, 6, 2, 1);
        this.grid.add(button, 0, 7);
        this.grid.add(field, 1, 7);
        this.grid.add(table, 0, 8, 2, 1);
    }

    private void createForm() {
        var nameField = new TextField();
        var nameLabel = new Label("Name");
        var englishField = new TextField();
        var englishLabel = new Label("Englisch");
        var germanField = new TextField();
        var germanLabel = new Label("Deutsch");
        var mathField = new TextField();
        var mathLabel = new Label("Mathematik");
        var saveButton = new Button("Speichern");

        try {
            var action = new SaveAction(this.connection, nameField, englishField, germanField, mathField, this.status);
            saveButton.setOnAction(action);
        } catch (SQLException exception) {
            this.status.setText(exception.getMessage());
        }

        this.grid.add(nameLabel, 0, 1);
        this.grid.add(nameField, 1, 1);
        this.grid.add(englishLabel, 0, 2);
        this.grid.add(englishField, 1, 2);
        this.grid.add(germanLabel, 0, 3);
        this.grid.add(germanField, 1, 3);
        this.grid.add(mathLabel, 0, 4);
        this.grid.add(mathField, 1, 4);
        this.grid.add(saveButton, 1, 5);
    }

    private void createTable() {
        try {
            var host = "jdbc:hsqldb:mem:mitarbeiter;shutdown=true";
            this.connection = DriverManager.getConnection(host);
            var statement = connection.createStatement();

            statement.executeUpdate("""
                CREATE TABLE IF NOT EXISTS marks (
                    mark_id identity PRIMARY KEY,
                    name VARCHAR,
                    english integer,
                    german integer,
                    math integer
                )
            """);
        } catch (SQLException exception) {
            this.status.setText(exception.getMessage());
        }
    }
}