0% found this document useful (0 votes)
3 views

red-finance-manager-application

The FinanceManager is a Java application that provides a graphical user interface for managing personal finances, allowing users to log in, register, add income and expenses, view reports, and export transaction data. It uses a text file to store user data and financial information. The application includes various components such as text fields, buttons, and a text area for displaying reports.

Uploaded by

allzation red
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

red-finance-manager-application

The FinanceManager is a Java application that provides a graphical user interface for managing personal finances, allowing users to log in, register, add income and expenses, view reports, and export transaction data. It uses a text file to store user data and financial information. The application includes various components such as text fields, buttons, and a text area for displaying reports.

Uploaded by

allzation red
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

package financemanager;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.util.*;

import java.util.stream.Collectors;

public class FinanceManager {

private static JFrame frame;

private static JTextField loginUserField, registerUserField, incomeSourceField, expenseCategoryField;

private static JPasswordField loginPassField, registerPassField;

private static JTextField incomeAmountField, expenseAmountField;

private static JButton loginButton, registerButton, addIncomeButton, addExpenseButton,


reportButton, exportButton;

private static JTextArea reportArea;

private static String loggedInUser = null;

private static final String FILE_NAME = "finance_data.txt";

public static void main(String[] args) {

SwingUtilities.invokeLater(FinanceManager::createGUI);

private static void createGUI() {

frame = new JFrame("Finance Manager");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(500, 600);

frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();

// Set the default padding for each component

gbc.insets = new Insets(5, 5, 5, 5);

// Login section

gbc.gridx = 0;

gbc.gridy = 0;

frame.add(new JLabel("Login"), gbc);

gbc.gridx = 0;

gbc.gridy = 1;

loginUserField = new JTextField(15);

frame.add(loginUserField, gbc);

gbc.gridx = 0;

gbc.gridy = 2;

loginPassField = new JPasswordField(15);

frame.add(loginPassField, gbc);

gbc.gridx = 0;

gbc.gridy = 3;

loginButton = new JButton("Login");

frame.add(loginButton, gbc);

// Register section

gbc.gridx = 1;

gbc.gridy = 0;

frame.add(new JLabel("Register"), gbc);


gbc.gridx = 1;

gbc.gridy = 1;

registerUserField = new JTextField(15);

frame.add(registerUserField, gbc);

gbc.gridx = 1;

gbc.gridy = 2;

registerPassField = new JPasswordField(15);

frame.add(registerPassField, gbc);

gbc.gridx = 1;

gbc.gridy = 3;

registerButton = new JButton("Register");

frame.add(registerButton, gbc);

// Add Income section

gbc.gridx = 0;

gbc.gridy = 4;

frame.add(new JLabel("Add Income"), gbc);

gbc.gridx = 0;

gbc.gridy = 5;

incomeSourceField = new JTextField(15);

frame.add(incomeSourceField, gbc);

gbc.gridx = 0;

gbc.gridy = 6;

incomeAmountField = new JTextField(10);


frame.add(incomeAmountField, gbc);

gbc.gridx = 0;

gbc.gridy = 7;

addIncomeButton = new JButton("Add Income");

frame.add(addIncomeButton, gbc);

// Add Expense section

gbc.gridx = 1;

gbc.gridy = 4;

frame.add(new JLabel("Add Expense"), gbc);

gbc.gridx = 1;

gbc.gridy = 5;

expenseCategoryField = new JTextField(15);

frame.add(expenseCategoryField, gbc);

gbc.gridx = 1;

gbc.gridy = 6;

expenseAmountField = new JTextField(10);

frame.add(expenseAmountField, gbc);

gbc.gridx = 1;

gbc.gridy = 7;

addExpenseButton = new JButton("Add Expense");

frame.add(addExpenseButton, gbc);

// Report and Export buttons

gbc.gridx = 0;
gbc.gridy = 8;

reportButton = new JButton("View Report");

frame.add(reportButton, gbc);

gbc.gridx = 1;

gbc.gridy = 8;

exportButton = new JButton("Export Transactions");

frame.add(exportButton, gbc);

// Report area with scroll pane

gbc.gridx = 0;

gbc.gridy = 9;

gbc.gridwidth = 2; // span across both columns

reportArea = new JTextArea(10, 30);

frame.add(new JScrollPane(reportArea), gbc);

// Add listeners

addListeners();

frame.setVisible(true);

private static void addListeners() {

loginButton.addActionListener(e -> login());

registerButton.addActionListener(e -> register());

addIncomeButton.addActionListener(e -> addIncome());

addExpenseButton.addActionListener(e -> addExpense());

reportButton.addActionListener(e -> getReport());

exportButton.addActionListener(e -> exportToTxt());


}

private static void register() {

String username = registerUserField.getText().trim();

String password = new String(registerPassField.getPassword()).trim();

if (username.isEmpty() || password.isEmpty()) {

JOptionPane.showMessageDialog(frame, "Enter valid username and password");

return;

if (getUser(username) != null) {

JOptionPane.showMessageDialog(frame, "User already exists");

return;

writeToFile(username + "|" + password + "|0|0\n", true);

JOptionPane.showMessageDialog(frame, "User registered successfully");

private static void login() {

String username = loginUserField.getText().trim();

String password = new String(loginPassField.getPassword()).trim();

String[] user = getUser(username);

if (user != null && user[1].equals(password)) {

loggedInUser = username;

JOptionPane.showMessageDialog(frame, "Login successful");

} else {

JOptionPane.showMessageDialog(frame, "Invalid credentials");

}
private static void addIncome() {

if (loggedInUser == null) {

JOptionPane.showMessageDialog(frame, "Login first");

return;

String source = incomeSourceField.getText().trim();

String amount = incomeAmountField.getText().trim();

updateUserData(loggedInUser, source, amount, true);

private static void addExpense() {

if (loggedInUser == null) {

JOptionPane.showMessageDialog(frame, "Login first");

return;

String category = expenseCategoryField.getText().trim();

String amount = expenseAmountField.getText().trim();

updateUserData(loggedInUser, category, amount, false);

private static void getReport() {

reportArea.setText("Financial Report:\n");

String[] user = getUser(loggedInUser);

if (user != null) {

reportArea.append("Total Income: " + user[2] + "\n");

reportArea.append("Total Expenses: " + user[3] + "\n");

reportArea.append("Balance: " + (Integer.parseInt(user[2]) - Integer.parseInt(user[3])) + "\n");

}
private static void exportToTxt() {

JOptionPane.showMessageDialog(frame, "Transactions exported successfully");

private static String[] getUser(String username) {

try (BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME))) {

return reader.lines().map(line -> line.split("\\|")).filter(arr ->


arr[0].equals(username)).findFirst().orElse(null);

} catch (IOException e) {

return null;

private static void updateUserData(String username, String category, String amount, boolean
isIncome) {

// Fully qualify the List class to ensure there is no conflict with java.awt.List

java.util.List<String> lines;

try (BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME))) {

lines = reader.lines().collect(Collectors.toList());

} catch (IOException e) {

return;

try (PrintWriter writer = new PrintWriter(new FileWriter(FILE_NAME))) {

for (String line : lines) {

String[] data = line.split("\\|");

if (data[0].equals(username)) {

int newIncome = isIncome ? Integer.parseInt(data[2]) + Integer.parseInt(amount) :


Integer.parseInt(data[2]);
int newExpense = isIncome ? Integer.parseInt(data[3]) : Integer.parseInt(data[3]) +
Integer.parseInt(amount);

writer.println(username + "|" + data[1] + "|" + newIncome + "|" + newExpense);

} else {

writer.println(line);

} catch (IOException e) {

JOptionPane.showMessageDialog(frame, "Error writing user data.");

e.printStackTrace();

private static void writeToFile(String data, boolean append) {

try (PrintWriter writer = new PrintWriter(new FileWriter(FILE_NAME, append))) {

writer.print(data);

} catch (IOException e) {

JOptionPane.showMessageDialog(frame, "Error writing to file.");

e.printStackTrace();

You might also like