-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplitterApp.java
More file actions
104 lines (88 loc) · 3.58 KB
/
Copy pathSplitterApp.java
File metadata and controls
104 lines (88 loc) · 3.58 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
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import java.io.*;
import java.util.*;
public class SplitterApp extends Application {
private ListView<String> listView = new ListView<>();
private List<Expense> expenses = new ArrayList<>();
private static final String FILE_PATH = "expenses.txt";
@Override
public void start(Stage stage) {
stage.setTitle("Offline Expense Splitter");
TextField nameField = new TextField();
nameField.setPromptText("Enter person name");
TextField amountField = new TextField();
amountField.setPromptText("Enter amount");
Button addBtn = new Button("Add Expense");
Button splitBtn = new Button("Split Equally");
Button saveBtn = new Button("Save");
HBox inputBox = new HBox(10, nameField, amountField, addBtn);
VBox layout = new VBox(15, inputBox, splitBtn, saveBtn, listView);
layout.setStyle("-fx-padding: 15; -fx-background-color: #f4f4f4;");
addBtn.setOnAction(e -> {
try {
String name = nameField.getText();
double amount = Double.parseDouble(amountField.getText());
Expense exp = new Expense(name, amount);
expenses.add(exp);
listView.getItems().add(exp.toString());
nameField.clear();
amountField.clear();
} catch (Exception ex) {
showAlert("Error", "Please enter valid data!");
}
});
splitBtn.setOnAction(e -> {
if (expenses.isEmpty()) {
showAlert("No Data Found", "Add some expenses first!");
return;
}
double total = expenses.stream().mapToDouble(Expense::getAmount).sum();
double share = total / expenses.size();
listView.getItems().add("\nTotal: ₹" + total + " | Each Pays: ₹" + String.format("%.2f", share));
});
saveBtn.setOnAction(e -> saveExpenses());
loadExpenses();
Scene scene = new Scene(layout, 420, 400);
stage.setScene(scene);
stage.show();
}
private void saveExpenses() {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH))) {
for (Expense exp : expenses) {
writer.write(exp.getName() + "," + exp.getAmount());
writer.newLine();
}
showAlert("Saved", "Expenses saved offline!");
} catch (IOException e) {
showAlert("Error", "Failed to save expenses!");
}
}
private void loadExpenses() {
File file = new File(FILE_PATH);
if (!file.exists()) return;
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 2) {
Expense exp = new Expense(parts[0], Double.parseDouble(parts[1]));
expenses.add(exp);
listView.getItems().add(exp.toString());
}
}
} catch (IOException e) {
showAlert("Error", "Failed to load expenses!");
}
}
private void showAlert(String title, String message) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
}