-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExpenseTracker
126 lines (108 loc) · 4.3 KB
/
ExpenseTracker
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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class ExpenseTracker extends JFrame {
private List<Expense> expenses;
private JTextArea expenseTextArea;
private JTextField amountField;
private JButton addButton;
private static final String DATA_FILE = "expenses.txt";
public ExpenseTracker() {
setTitle("Expense Tracker");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel mainPanel = new JPanel(new BorderLayout());
expenseTextArea = new JTextArea();
expenseTextArea.setEditable(false);
mainPanel.add(new JScrollPane(expenseTextArea), BorderLayout.CENTER);
JPanel inputPanel = new JPanel(new FlowLayout());
JLabel amountLabel = new JLabel("Enter Amount:");
inputPanel.add(amountLabel);
amountField = new JTextField(10);
inputPanel.add(amountField);
addButton = new JButton("Add Expense");
addButton.addActionListener(new AddButtonListener());
inputPanel.add(addButton);
mainPanel.add(inputPanel, BorderLayout.SOUTH);
loadExpenses();
updateExpenseTextArea();
add(mainPanel);
setVisible(true);
}
private void loadExpenses() {
expenses = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(DATA_FILE))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
double amount = Double.parseDouble(parts[0]);
long timestamp = Long.parseLong(parts[1]);
expenses.add(new Expense(amount, new Date(timestamp)));
}
} catch (IOException | NumberFormatException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Error loading expenses: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void saveExpenses() {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(DATA_FILE))) {
for (Expense expense : expenses) {
writer.write(expense.getAmount() + "," + expense.getTimestamp().getTime());
writer.newLine();
}
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Error saving expenses: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void updateExpenseTextArea() {
expenseTextArea.setText("");
for (Expense expense : expenses) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
expenseTextArea.append("$" + expense.getAmount() + " on " + dateFormat.format(expense.getTimestamp()) + "\n");
}
}
private class AddButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String amountStr = amountField.getText().trim();
if (!amountStr.isEmpty()) {
try {
double amount = Double.parseDouble(amountStr);
expenses.add(new Expense(amount, new Date()));
saveExpenses();
updateExpenseTextArea();
amountField.setText("");
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(ExpenseTracker.this, "Invalid amount format", "Error", JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(ExpenseTracker.this, "Please enter an amount", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(ExpenseTracker::new);
}
}
class Expense {
private double amount;
private Date timestamp;
public Expense(double amount, Date timestamp) {
this.amount = amount;
this.timestamp = timestamp;
}
public double getAmount() {
return amount;
}
public Date getTimestamp() {
return timestamp;
}
}