-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExpense_main
More file actions
94 lines (79 loc) · 3.12 KB
/
Expense_main
File metadata and controls
94 lines (79 loc) · 3.12 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
import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
interface ExpenseDataReceiver {
void receiveExpense(String expense, String category, String date, String description);
}
class Expense_main implements ExpenseDataReceiver {
private DefaultTableModel tableModel;
private JTextField inField;
private double income = 0;
private double totalExpenses = 0;
public static void main(String args[]) {
new Expense_main().initUI();
}
public void initUI() {
JFrame frame = new JFrame("Personal Expense Manager");
frame.setSize(900, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.setLocationRelativeTo(null);
JLabel titleLabel = new JLabel("Personal Expense Manager");
titleLabel.setFont(new Font("Arial", Font.BOLD, 24));
titleLabel.setBounds(250, 20, 400, 30);
frame.add(titleLabel);
JButton addExpenseButton = new JButton("Add Expense");
addExpenseButton.setBounds(50, 70, 130, 30);
frame.add(addExpenseButton);
addExpenseButton.addActionListener(e -> {
try {
if (income == 0) {
income = Double.parseDouble(inField.getText());
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(frame, "Please enter a valid income first.");
return;
}
new AddingFrame(this);
});
JButton expenseReportButton = new JButton("Expense Report");
expenseReportButton.setBounds(190, 70, 150, 30);
expenseReportButton.setBackground(Color.GREEN);
frame.add(expenseReportButton);
JLabel inLabel = new JLabel("Income: ");
inLabel.setFont(new Font("Arial", Font.BOLD, 24));
inLabel.setBounds(500, 70, 150, 25);
frame.add(inLabel);
inField = new JTextField();
inField.setBounds(600, 70, 120, 30);
frame.add(inField);
String[] columns = {"Expense", "Balance", "Category", "Date", "Description", "Amount"};
tableModel = new DefaultTableModel(columns, 0);
JTable table = new JTable(tableModel);
JTableHeader header = table.getTableHeader();
header.setReorderingAllowed(false);
table.getTableHeader().setResizingAllowed(false);
table.setShowGrid(true);
table.setGridColor(Color.GRAY);
table.setDefaultEditor(Object.class, null);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(50, 130, 780, 400);
frame.add(scrollPane);
frame.setVisible(true);
}
public void receiveExpense(String expense, String category, String date, String description) {
double amount = Double.parseDouble(expense);
totalExpenses += amount;
double balance = income - totalExpenses;
String[] row = {
expense,
String.format("%.2f", balance),
category,
date,
description,
expense
};
tableModel.addRow(row);
}
}