Rayan-Nadeem / -CombinedFinanceTracker

this is my SCD semester project
0 stars 0 forks source link

tracker update w mvc and swing java #2

Open Rayan-Nadeem opened 3 weeks ago

Rayan-Nadeem commented 3 weeks ago

/*

import javax.swing.; import javax.swing.table.DefaultTableModel; import java.awt.; import java.util.ArrayList; import java.util.List;

class Transaction { private String type; private String category; private double amount; private String date; private String description;

public Transaction(String type, String category, double amount, String date, String description) {
    this.type = type;
    this.category = category;
    this.amount = amount;
    this.date = date;
    this.description = description;
}

public String getType() { return type; }
public String getCategory() { return category; }
public double getAmount() { return amount; }
public String getDate() { return date; }
public String getDescription() { return description; }

}

class FinanceManager { private List transactions = new ArrayList<>();

public void addTransaction(Transaction transaction) {
    transactions.add(transaction);
}

public List<Transaction> getTransactions() {
    return transactions;
}

public double getTotalIncome() {
    return transactions.stream()
        .filter(t -> t.getType().equalsIgnoreCase("Income"))
        .mapToDouble(Transaction::getAmount)
        .sum();
}

public double getTotalExpenses() {
    return transactions.stream()
        .filter(t -> t.getType().equalsIgnoreCase("Expense"))
        .mapToDouble(Transaction::getAmount)
        .sum();
}

public double getBalance() {
    return getTotalIncome() - getTotalExpenses();
}

}

public class CombinedFinanceTracker { private FinanceManager financeManager = new FinanceManager(); private JFrame primaryFrame; private DefaultTableModel tableModel; private JLabel totalIncomeLabel; private JLabel totalExpenseLabel; private JLabel balanceLabel;

public static void main(String[] args) {
    SwingUtilities.invokeLater(CombinedFinanceTracker::new);
}

public CombinedFinanceTracker() {
    primaryFrame = new JFrame("Personal Finance Tracker");
    primaryFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    primaryFrame.setSize(800, 600);
    primaryFrame.setLocationRelativeTo(null);
    showHomePage();
    primaryFrame.setVisible(true);
}

private void showHomePage() {
    JPanel homePanel = new JPanel(new BorderLayout());
    JLabel welcomeLabel = new JLabel("Welcome to Your Personal Finance Tracker!", JLabel.CENTER);
    welcomeLabel.setFont(new Font("Arial", Font.BOLD, 20));
    welcomeLabel.setBorder(BorderFactory.createEmptyBorder(20, 0, 20, 0));
    homePanel.add(welcomeLabel, BorderLayout.NORTH);

    JButton addTransactionButton = new JButton("Add Transaction");
    JButton viewReportsButton = new JButton("View Reports");
    JButton settingsButton = new JButton("Settings");

    addTransactionButton.setFont(new Font("Arial", Font.PLAIN, 14));
    viewReportsButton.setFont(new Font("Arial", Font.PLAIN, 14));
    settingsButton.setFont(new Font("Arial", Font.PLAIN, 14));

    JPanel buttonPanel = new JPanel(new GridLayout(3, 1, 10, 10));
    buttonPanel.add(addTransactionButton);
    buttonPanel.add(viewReportsButton);
    buttonPanel.add(settingsButton);
    buttonPanel.setBorder(BorderFactory.createEmptyBorder(40, 100, 40, 100));

    homePanel.add(buttonPanel, BorderLayout.CENTER);
    primaryFrame.setContentPane(homePanel);

    addTransactionButton.addActionListener(e -> showFinanceTracker());
    viewReportsButton.addActionListener(e -> showMessage("View Reports feature not implemented yet."));
    settingsButton.addActionListener(e -> showMessage("Settings feature not implemented yet."));
}

private void showFinanceTracker() {
JPanel trackerPanel = new JPanel(new BorderLayout());
String[] columns = {"Type", "Category", "Amount", "Date", "Description"};
tableModel = new DefaultTableModel(columns, 0);
JTable transactionTable = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(transactionTable);
trackerPanel.add(scrollPane, BorderLayout.CENTER);

totalIncomeLabel = new JLabel("Total Income: $0.00");
totalExpenseLabel = new JLabel("Total Expenses: $0.00");
balanceLabel = new JLabel("Balance: $0.00");
JPanel statusPanel = new JPanel();
statusPanel.add(totalIncomeLabel);
statusPanel.add(totalExpenseLabel);
statusPanel.add(balanceLabel);
trackerPanel.add(statusPanel, BorderLayout.SOUTH);

JButton addButton = new JButton("Add Transaction");
JButton deleteButton = new JButton("Delete Transaction");
JButton backButton = new JButton("Back to Home");
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
buttonPanel.add(addButton);
buttonPanel.add(deleteButton);
buttonPanel.add(backButton);
trackerPanel.add(buttonPanel, BorderLayout.NORTH);

primaryFrame.setContentPane(trackerPanel);
primaryFrame.revalidate();

addButton.addActionListener(e -> showAddTransactionDialog());
deleteButton.addActionListener(e -> {
    int selectedRow = transactionTable.getSelectedRow();
    if (selectedRow != -1) {
        financeManager.getTransactions().remove(selectedRow);
        tableModel.removeRow(selectedRow);
        updateTotals();
    }
});
backButton.addActionListener(e -> showHomePage()); // Back to Home button action

}

private void showAddTransactionDialog() { JDialog dialog = new JDialog(primaryFrame, "Add Transaction", true); dialog.setSize(300, 300); dialog.setLayout(new GridBagLayout()); dialog.setLocationRelativeTo(primaryFrame);

GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;

dialog.add(new JLabel("Type:"), gbc);
gbc.gridy++;
JTextField typeField = new JTextField();
dialog.add(typeField, gbc);

gbc.gridy++;
dialog.add(new JLabel("Category:"), gbc);
gbc.gridy++;
JTextField categoryField = new JTextField();
dialog.add(categoryField, gbc);

gbc.gridy++;
dialog.add(new JLabel("Amount:"), gbc);
gbc.gridy++;
JTextField amountField = new JTextField();
dialog.add(amountField, gbc);

gbc.gridy++;
dialog.add(new JLabel("Date:"), gbc);
gbc.gridy++;
JTextField dateField = new JTextField();
dialog.add(dateField, gbc);

gbc.gridy++;
dialog.add(new JLabel("Description:"), gbc);
gbc.gridy++;
JTextField descriptionField = new JTextField();
dialog.add(descriptionField, gbc);

gbc.gridy++;
JButton addButton = new JButton("Add");
gbc.anchor = GridBagConstraints.CENTER;
dialog.add(addButton, gbc);

addButton.addActionListener(e -> {
    String type = typeField.getText();
    String category = categoryField.getText();
    double amount = Double.parseDouble(amountField.getText());
    String date = dateField.getText();
    String description = descriptionField.getText();

    Transaction transaction = new Transaction(type, category, amount, date, description);
    financeManager.addTransaction(transaction);
    tableModel.addRow(new Object[]{type, category, amount, date, description});
    updateTotals();
    dialog.dispose();
});

dialog.pack();
dialog.setVisible(true);

}

private void updateTotals() {
    totalIncomeLabel.setText("Total Income: $" + String.format("%.2f", financeManager.getTotalIncome()));
    totalExpenseLabel.setText("Total Expenses: $" + String.format("%.2f", financeManager.getTotalExpenses()));
    balanceLabel.setText("Balance: $" + String.format("%.2f", financeManager.getBalance()));
}

private void showMessage(String message) {
    JOptionPane.showMessageDialog(primaryFrame, message);
}

}