-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
279 lines (219 loc) · 9.98 KB
/
Main.java
File metadata and controls
279 lines (219 loc) · 9.98 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Modality;
import javafx.stage.Stage;
import java.util.HashMap;
import java.util.Map;
public class Main extends Application {
private final ExpenseRepository repo = new ExpenseRepository();
private Stage primaryStage; // We keep a reference to the main window
// Chart Colors
private final String[] PIE_COLORS = {
"#fdcb6e", "#ff7675", "#74b9ff", "#55efc4", "#a29bfe", "#fab1a0"
};
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
this.primaryStage = stage;
showLoginScreen(); // Start with the login screen
}
// ==========================================
// 1. THE LOGIN SCREEN (Gateway)
// ==========================================
private void showLoginScreen() {
VBox root = new VBox(20);
root.setAlignment(Pos.CENTER);
root.setPadding(new Insets(40));
Label lblTitle = new Label("Expense Tracker Login");
lblTitle.getStyleClass().add("header-label");
lblTitle.setStyle("-fx-font-size: 24px; -fx-text-fill: #6c5ce7;");
Button btnUser = new Button("Login as User");
Button btnAdmin = new Button("Login as Admin");
// Set actions to switch screens
btnUser.setOnAction(e -> showUserInterface());
// Instead of going straight to Admin, we ask for password first
btnAdmin.setOnAction(e -> askForAdminPassword());
root.getChildren().addAll(lblTitle, btnUser, btnAdmin);
Scene scene = new Scene(root, 400, 300);
// Load CSS safely
try {
scene.getStylesheets().add(getClass().getResource("styles.css").toExternalForm());
} catch (Exception e) { System.out.println("CSS not found!"); }
primaryStage.setTitle("Welcome");
primaryStage.setScene(scene);
primaryStage.show();
}
// ==========================================
// 2. THE USER INTERFACE (Clean & Pretty)
// ==========================================
private void showUserInterface() {
VBox root = new VBox(20);
root.setAlignment(Pos.CENTER);
root.setPadding(new Insets(40));
Label lblTitle = new Label("💰 My Expenses");
lblTitle.getStyleClass().add("header-label");
Button btnAdd = new Button("Add Expense");
Button btnChart = new Button("View Analytics");
Button btnLogout = new Button("Logout");
btnLogout.setStyle("-fx-background-color: #b2bec3;"); // Grey for logout
btnAdd.setOnAction(e -> showAddPopup());
btnChart.setOnAction(e -> showChartPopup());
btnLogout.setOnAction(e -> showLoginScreen()); // Go back
root.getChildren().addAll(lblTitle, btnAdd, btnChart, btnLogout);
Scene scene = new Scene(root, 400, 350);
scene.getStylesheets().add(getClass().getResource("styles.css").toExternalForm());
primaryStage.setTitle("User Mode");
primaryStage.setScene(scene);
}
// ==========================================
// 3. THE ADMIN INTERFACE (Table & Delete)
// ==========================================
private void showAdminInterface() {
BorderPane root = new BorderPane();
// --- Left: Table ---
TableView<Expense> table = new TableView<>();
TableColumn<Expense, String> colDesc = new TableColumn<>("Description");
colDesc.setCellValueFactory(new PropertyValueFactory<>("description"));
TableColumn<Expense, Double> colAmount = new TableColumn<>("Amount");
colAmount.setCellValueFactory(new PropertyValueFactory<>("amount"));
TableColumn<Expense, String> colCat = new TableColumn<>("Category");
colCat.setCellValueFactory(new PropertyValueFactory<>("category"));
table.getColumns().addAll(colDesc, colAmount, colCat);
table.setItems(FXCollections.observableArrayList(repo.getAll())); // Load Data
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
VBox leftBox = new VBox(10, new Label("Database Records"), table);
leftBox.setPadding(new Insets(10));
leftBox.setPrefWidth(350);
// --- Right: Controls ---
Button btnDelete = new Button("Delete Selected");
btnDelete.setStyle("-fx-background-color: #ff7675;");
Button btnLogout = new Button("Logout");
btnLogout.setStyle("-fx-background-color: #b2bec3;");
// Delete Logic
btnDelete.setOnAction(e -> {
Expense selected = table.getSelectionModel().getSelectedItem();
if (selected != null) {
repo.delete(selected);
table.setItems(FXCollections.observableArrayList(repo.getAll())); // Refresh
}
});
btnLogout.setOnAction(e -> showLoginScreen());
VBox rightBox = new VBox(20, new Label("Admin Controls"), btnDelete, btnLogout);
rightBox.setAlignment(Pos.CENTER);
rightBox.setPadding(new Insets(20));
root.setCenter(leftBox);
root.setRight(rightBox);
Scene scene = new Scene(root, 600, 400);
scene.getStylesheets().add(getClass().getResource("styles.css").toExternalForm());
primaryStage.setTitle("Admin Dashboard");
primaryStage.setScene(scene);
}
// ==========================================
// HELPERS (Popups & Charts from before)
// ==========================================
private void showAddPopup() {
Stage popup = new Stage();
popup.initModality(Modality.APPLICATION_MODAL);
popup.setTitle("Add New Expense");
TextField txtDesc = new TextField(); txtDesc.setPromptText("Description");
TextField txtAmount = new TextField(); txtAmount.setPromptText("Amount");
TextField txtCat = new TextField(); txtCat.setPromptText("Category");
Button btnSave = new Button("Save");
VBox layout = new VBox(10, new Label("New Entry"), txtDesc, txtAmount, txtCat, btnSave);
layout.setPadding(new Insets(20));
layout.setAlignment(Pos.CENTER);
btnSave.setOnAction(e -> {
try {
repo.add(new Expense(txtDesc.getText(), Double.parseDouble(txtAmount.getText()), txtCat.getText()));
popup.close();
} catch(Exception ex) { /* Ignore */ }
});
Scene scene = new Scene(layout, 300, 250);
scene.getStylesheets().add(getClass().getResource("styles.css").toExternalForm());
popup.setScene(scene);
popup.show();
}
private void showChartPopup() {
Stage chartStage = new Stage();
PieChart chart = new PieChart();
chart.setLabelsVisible(true);
chart.setLegendVisible(false);
Map<String, Double> catTotals = new HashMap<>();
double grandTotal = 0;
for (Expense e : repo.getAll()) {
catTotals.put(e.category, catTotals.getOrDefault(e.category, 0.0) + e.amount);
grandTotal += e.amount;
}
ObservableList<PieChart.Data> data = FXCollections.observableArrayList();
for (Map.Entry<String, Double> entry : catTotals.entrySet()) {
data.add(new PieChart.Data(entry.getKey(), entry.getValue()));
}
chart.setData(data);
// Donut Hole
Circle hole = new Circle(100);
hole.setFill(Color.web("#f4f4f9"));
Label totalLabel = new Label(String.format("Total\n$%.2f", grandTotal));
totalLabel.setStyle("-fx-font-size: 18px; -fx-font-weight: bold;");
StackPane stack = new StackPane(chart, hole, totalLabel);
Scene scene = new Scene(stack, 500, 500);
scene.getStylesheets().add(getClass().getResource("styles.css").toExternalForm());
chartStage.setScene(scene);
chartStage.show();
// Apply Colors
int i = 0;
for (PieChart.Data d : data) {
d.getNode().setStyle("-fx-pie-color: " + PIE_COLORS[i % PIE_COLORS.length] + ";");
i++;
}
}
// --- NEW: ADMIN PASSWORD POPUP ---
private void askForAdminPassword() {
Stage popup = new Stage();
popup.initModality(Modality.APPLICATION_MODAL); // Block other windows
popup.setTitle("Security Check");
Label lbl = new Label("Enter Admin PIN:");
lbl.setStyle("-fx-font-weight: bold; -fx-text-fill: #2d3436;");
// PasswordField hides the text with dots
PasswordField txtPass = new PasswordField();
txtPass.setPromptText("PIN");
txtPass.setStyle("-fx-max-width: 150px;");
Button btnLogin = new Button("Unlock");
Label lblError = new Label(); // Hidden error message
lblError.setTextFill(Color.RED);
// Action: Check the password
btnLogin.setOnAction(e -> {
String input = txtPass.getText();
// THE SECRET PASSWORD IS "1234"
if (input.equals("1234")) {
popup.close(); // Close the password window
showAdminInterface(); // Open the Admin Dashboard
} else {
lblError.setText("Wrong PIN! Try again.");
txtPass.clear();
}
});
VBox layout = new VBox(15, lbl, txtPass, lblError, btnLogin);
layout.setAlignment(Pos.CENTER);
layout.setPadding(new Insets(20));
Scene scene = new Scene(layout, 250, 200);
// Try to load CSS if available
try { scene.getStylesheets().add(getClass().getResource("styles.css").toExternalForm()); }
catch (Exception ex) { /* ignore */ }
popup.setScene(scene);
popup.show();
}
}