-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNextPageController
More file actions
193 lines (177 loc) · 7.6 KB
/
NextPageController
File metadata and controls
193 lines (177 loc) · 7.6 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package com.example.jproject;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.collections.ObservableList;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Optional;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;
import javafx.scene.control.TextInputDialog;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
public class NextPageController implements Initializable {
@FXML
public TableView<Logs> table;
@FXML
public TableColumn<Logs, Integer> LogID;
@FXML
public TableColumn<Logs, Integer> LogDate;
@FXML
public TableColumn<Logs, Integer> duration;
@FXML
public TableColumn<Logs, Integer> sets;
@FXML
public TableColumn<Logs, Integer> reps;
private static final String jdbcURL = "jdbc:postgresql://localhost:5432/postgres";
private static final String username = "postgres";
private static final String password = "shreya123";
public String username1;
public void setUsernameFromLogin(String username) {
username1 = username;
}
public void addNewRow(ActionEvent event) {
Logs newLog = captureUserInput();
table.getItems().add(newLog);
assert newLog != null;
insertLogIntoDatabase(newLog);
}
private Logs captureUserInput() {
TextInputDialog logDateDialog = new TextInputDialog();
logDateDialog.setTitle("New Row Input");
logDateDialog.setHeaderText("Enter data for the new row:");
logDateDialog.setContentText("Log Date:");
Optional<String> logDateResult = logDateDialog.showAndWait();
TextInputDialog durationDialog = new TextInputDialog();
durationDialog.setTitle("New Row Input");
durationDialog.setHeaderText("Enter data for the new row:");
durationDialog.setContentText("Duration:");
Optional<String> durationResult = durationDialog.showAndWait();
TextInputDialog setDialog = new TextInputDialog();
setDialog.setTitle("New Row Input");
setDialog.setHeaderText("Enter data for the new row:");
setDialog.setContentText("sets:");
Optional<String> SetsResult = setDialog.showAndWait();
TextInputDialog repDialog = new TextInputDialog();
repDialog.setTitle("New Row Input");
repDialog.setHeaderText("Enter data for the new row:");
repDialog.setContentText("reps:");
Optional<String> repsResult = repDialog.showAndWait();
if (logDateResult.isPresent() && durationResult.isPresent() && SetsResult.isPresent() && repsResult.isPresent()) {
String logDate = logDateResult.get();
int duration = Integer.parseInt(durationResult.get());
int sets = Integer.parseInt(SetsResult.get());
int reps = Integer.parseInt(repsResult.get());
Logs newLog = new Logs();
newLog.setLogDate(logDate);
newLog.setDuration(duration);
newLog.setSets(sets);
newLog.setReps(reps);
return newLog;
} else {
return null;
}
}
private void insertLogIntoDatabase(Logs log) {
Connection connection = null;
try {
connection = DriverManager.getConnection(jdbcURL, username, password);
connection.setAutoCommit(false);
int userId = getUserIdByUsername(connection, username1);
try (PreparedStatement statement = connection.prepareStatement(
"INSERT INTO exercise_logs (log_date, duration_minutes, sets, reps, user_id) VALUES (?, ?, ?, ?, ?)")) {
statement.setString(1, log.getLogDate());
statement.setInt(2, log.getDuration());
statement.setInt(3, log.getSets());
statement.setInt(4, log.getReps());
statement.setInt(5, userId);
int updatedRows = statement.executeUpdate();
if (updatedRows > 0) {
connection.commit();
}
} catch (SQLException e) {
e.printStackTrace();
connection.rollback();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private int getUserIdByUsername(Connection connection, String username) throws SQLException {
try (PreparedStatement statement = connection.prepareStatement(
"SELECT user_id FROM users WHERE username = ?")) {
statement.setString(1, username);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
return resultSet.getInt("user_id");
} else {
return -1;
}
}
}
@Override
public void initialize(URL location, ResourceBundle resources) {
LogID.setCellValueFactory(new PropertyValueFactory<>("LogID"));
LogDate.setCellValueFactory(new PropertyValueFactory<>("LogDate"));
duration.setCellValueFactory(new PropertyValueFactory<>("Duration"));
sets.setCellValueFactory(new PropertyValueFactory<>("sets"));
reps.setCellValueFactory(new PropertyValueFactory<>("reps"));
}
public void setDataInTable(ObservableList<Logs> data) {
table.setItems(data);
}
ObservableList<Logs> getDataFromDatabase(String username1) {
ObservableList<Logs> data = FXCollections.observableArrayList();
try (Connection connection = DriverManager.getConnection(jdbcURL, username, password);
PreparedStatement statement = connection.prepareStatement(
"SELECT log_id, log_date, duration_minutes, sets, reps FROM exercise_logs el INNER JOIN users u ON el.user_id = u.user_id WHERE u.username = ?")) {
statement.setString(1, username1);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
Logs log = new Logs();
log.setLogID(resultSet.getInt("log_id"));
log.setLogDate(resultSet.getString("log_date"));
log.setDuration(resultSet.getInt("duration_minutes"));
log.setSets(resultSet.getInt("sets"));
log.setReps(resultSet.getInt("reps"));
data.add(log);
}
} catch (SQLException e) {
e.printStackTrace();
}
return data;
}
public void fitnessGoals(ActionEvent event) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("fitnessGoals.fxml"));
Parent fitnessGoalsRoot = loader.load();
FitnessGoalsController fitnessGoalsController = loader.getController();
ObservableList<fitness> data = fitnessGoalsController.getDataFromDatabase1(username1);
fitnessGoalsController.setDataInTable(data);
fitnessGoalsController.setUsernameFromLogin(username1);
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
stage.setTitle("Fitness Goals");
Scene scene = new Scene(fitnessGoalsRoot);
stage.setScene(scene);
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
}