Affichage des articles dont le libellé est Free Java Projects Source Code. Afficher tous les articles
Affichage des articles dont le libellé est Free Java Projects Source Code. Afficher tous les articles

Java Expenses And Incomes Tracker App Source Code


How To Make an Expenses And Incomes Tracker Project Using NetBeans

Java Expenses And Incomes Tracker App Source Code




In this Java Tutorial we will see How to Make a Simple Expenses And Incomes Tracker  (Calculator) App Using  Jframe JPanels, JTextFields, JComboBox, JTable and JButton  In Java NetBeans .





Project Source Code:

1 - Create the ExpenseIncomeEntry Class:


package expense_income_tracker;

/**
 *
 * @author 1BestCsharp
 */

   /*
 * ExpenseIncomeEntry represents a single entry for expenses and incomes.
 * Each entry contains a date, description, amount, and type (expense or income).
 */

public class ExpenseIncomeEntry {

    private String date;
    private String description;
    private double amount;
    private String type; // The type of the entry (expense or income).
    
    
    public ExpenseIncomeEntry(String date, String description, double amount, String type)
    {
        this.date = date;
        this.description = description;
        this.amount = amount;
        this.type = type;
    }
    
    public String getDate(){ return date;}
    public String getDescription(){ return description;}
    public double getAmount(){ return amount;}
    public String getType(){ return type;}
}


2 - Create the ExpenseIncomeTableModel Class:


package expense_income_tracker;

import java.util.List;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;


/**
 *
 * @author 1BestCsharp
 */
public class ExpenseIncomeTableModel extends AbstractTableModel {

    // List to store the entries (rows) in the table
    private final List<ExpenseIncomeEntry> entries;
    // Column names for the table
    private final String[] columnNames = {"Date","Description","Amount","Type"};
    
    /**
     * Constructor to initialize the table model.
     */
    public ExpenseIncomeTableModel(){
        entries = new ArrayList<>();
    }
            

    /**
     * Add a new entry to the table.
     *
     * @param entry The expense or income entry to add.
     */
    public void addEntry(ExpenseIncomeEntry entry){
        entries.add(entry);
        // Notify the table that a new row has been inserted
        fireTableRowsInserted(entries.size()-1, entries.size()-1);
    }
    
    
    @Override
    public int getRowCount() { return entries.size(); }

    @Override
    public int getColumnCount() { return columnNames.length;}

    @Override
    public String getColumnName(int column){ return columnNames[column]; }
    
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        
        ExpenseIncomeEntry entry = entries.get(rowIndex);
        
        // Return the value for the cell based on the column index
        switch(columnIndex){
            case 0: return entry.getDate();
            case 1: return entry.getDescription();
            case 2: return entry.getAmount();
            case 3: return entry.getType();
            default: return null;
        }
        
        
    }

}


3 - Create the ExpensesIncomesTracker Class (JFrame):


package expense_income_tracker;

import com.formdev.flatlaf.FlatDarkLaf;
import javax.swing.*;
import java.awt.*;

/**
 *
 * @author 1BestCsharp
 */
// The ExpensesIncomesTracker class extends JFrame to create the main application window.
public class ExpensesIncomesTracker extends JFrame {
    
    private final ExpenseIncomeTableModel tableModel;
    private final JTable table;
    private final JTextField dateField;
    private final JTextField descriptionField;
    private final JTextField amountField;
    private final JComboBox<String> typeCombobox;
    private final JButton addButton;
    private final JLabel balanceLabel;
    private double balance; // The current balance based on the added expenses and incomes.
    
     // Constructor to initialize the application and set up the form.
    public ExpensesIncomesTracker(){
     
        try{
            // Apply the FlatDarkLaf look and feel for a modern and flat appearance.
            UIManager.setLookAndFeel(new FlatDarkLaf());
        }
        catch(Exception ex){
            System.err.println("Failed to Set FlatDarkLaf LookAndFeel");
        }
        
        // Custom color schemes for specific Swing components.
        UIManager.put("TextField.foreground", Color.WHITE);
        UIManager.put("TextField.background", Color.DARK_GRAY);
        UIManager.put("TextField.caretForeground", Color.RED);
        UIManager.put("ComboBox.foreground", Color.YELLOW);
        UIManager.put("ComboBox.selectionForeground", Color.WHITE);
        UIManager.put("ComboBox.selectionBackground", Color.BLACK);
        UIManager.put("Button.foreground", Color.WHITE);
        UIManager.put("Button.background", Color.ORANGE);
        UIManager.put("Label.foreground", Color.WHITE);
        
        // Set the default font for the entire application
        Font customFont = new Font("Arial", Font.PLAIN, 18);
        UIManager.put("Label.font", customFont);
        UIManager.put("TextField.font", customFont);
        UIManager.put("ComboBox.font", customFont);
        UIManager.put("Button.font", customFont);
        
        
        // Initialize the table model and balance variable.
        balance = 0.0;
        tableModel = new ExpenseIncomeTableModel();
        
        // Create a JTable and set up a scroll pane to display the data.
        table = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(table);
        table.setFillsViewportHeight(true);
        
        // Create input fields and components for adding new entries.
        dateField = new JTextField(10);
        descriptionField = new JTextField(20);
        amountField = new JTextField(10);
        typeCombobox = new JComboBox<>(new String[]{"Expense","Income"});
        
        // Attach an ActionListener to the "Add" button to handle new entry addition.
        addButton = new JButton("Add");
        addButton.addActionListener(e -> addEntry());
        balanceLabel = new JLabel("Balance: $" + balance);
        
        // Create input panel to arrange input components.
        JPanel inputPanel = new JPanel();
        inputPanel.add(new JLabel("Date"));
        inputPanel.add(dateField);
        
        inputPanel.add(new JLabel("Description"));
        inputPanel.add(descriptionField);
        
        inputPanel.add(new JLabel("Amount"));
        inputPanel.add(amountField);
        
        inputPanel.add(new JLabel("Type"));
        inputPanel.add(typeCombobox);
        
        inputPanel.add(addButton);
        
        // Create bottom panel to display the balance.
        JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        bottomPanel.add(balanceLabel);
        setLayout(new BorderLayout());
        
        // Set the layout of the main frame and add components to appropriate positions.
        add(inputPanel, BorderLayout.NORTH);
        add(scrollPane, BorderLayout.CENTER);
        add(bottomPanel, BorderLayout.SOUTH);
        
        // Set the title, default close operation, and visibility of the main frame.
        setTitle("Expanses And Incomes Tracker");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
        
    }
    
    
    // Method to handle adding new entries to the table.
    private void addEntry()
    {
        // Get input values from input fields.
        String date = dateField.getText();
        String description = descriptionField.getText();
        String amountStr = amountField.getText();
        String type = (String)typeCombobox.getSelectedItem();
        double amount;
        
        // Validate input values.
        // you can the description and date to the validation
        if(amountStr.isEmpty())
        {
            JOptionPane.showMessageDialog(this, "Enter the Amount", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        try
        {
            amount = Double.parseDouble(amountStr);
        }
        catch(NumberFormatException ex)
        {
            JOptionPane.showMessageDialog(this, "Invalid Amount Format", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        // Convert expenses to negative values.
        if(type.equals("Expense"))
        {
            amount *= -1;
        }
        
        // Create a new entry and add it to the table.
        ExpenseIncomeEntry entry = new ExpenseIncomeEntry(date, description, amount, type);
        tableModel.addEntry(entry);
        
        // Update the balance and display the new balance.
        balance += amount;
        balanceLabel.setText("Balance: $"+balance);
        
        // Clear input fields for the next entry.
        clearInputFields();
    }
    
    
    // Method to clear input fields.
    private void clearInputFields()
    {
        dateField.setText("");
        descriptionField.setText("");
        amountField.setText("");
        typeCombobox.setSelectedIndex(0);
    }
    
}


4 - Create the Main Class:

package expense_income_tracker;

/**
 *
 * @author 1BestCsharp
 */
public class Expense_Income_Tracker {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        new ExpensesIncomesTracker().setLocationRelativeTo(null);
        
    }

}

                                           
////// OUTPUT : 

Java Expenses And Incomes Tracker App

Java Expenses And Incomes Tracker



download the source code




More Java Projects:





Free Java Projects Source Code

Download 12 Java Project Source Code

Free Java Projects Source Code

here's a list of free java projects source code + video tutorial.

this list contains java games, mini apps, form designs.

if you want to get premium java projects source code click HERE .


 1 - JAVA Quiz App Project Source Code 

JAVA Quiz App Project Source Code

How to Make a Simple Quiz App Using  Jframe JPanels and JRadioButtons In Java NetBeans.


 2 - Java Calculator Project Source Code 

Java Calculator Project Source Code

How To Make A Calculator With Swith  To Do The Basic Operations (+, -, /, *) Using Java And Netbeans Editor.


 3 - Java Login And Register Form Design In One Window Source Code 

Java Login And Register Form Design In One Window Source Code

How To Design A Login Form And A Register Form In One Jframe Using JPanels In Java NetBeans .


 4 - Java Tic Tac Toe Game Source Code 

Java Tic Tac Toe Game Source Code

How To Build A Tic-Tac-Toe Game With Replay And Get The Winner And Change Winning Boxes Color Using Java And Netbeans Editor.


 5 - Java Design Login And Dashboard Form 

Java Design Login And Dashboard Form

How To Design A Login Form And A Dashboard Form In Java NetBeans.


 6 - Java Rock Paper Scissors Game Source Code 

Java Rock Paper Scissors Game Source Code

How to Make a Simple Rock, Paper, Scissors Game Using Jframe JPanels and Jlabels In Java NetBeans.


 7 - Java Login & Register Form With MySQL DataBase Source Code 

Java Login & Register Form With MySQL DataBase Source Code

How To Make a Design a Login Form And a Register Form, plus how to connect those two form with mysql database so the user can signup and signin.


 8 - Java Glass Bridge Game From Squid Game Source Code 

Java Glass Bridge Game From Squid Game Source Code

How to Make The Glass Stepping Stones Game or the glass bridge game From Squid Game Using JPanels and Jlabels In Java NetBeans .


 9 - Java Login & Menu Form Design Source Code 

Java Login & Menu Form Design Source Code

How To Design A Login Form And A Drop-Down Menu Form In Java NetBeans .


 10 - Java Guess The Word Game Source Code 

Java Guess The Word Game Source Code

How to Make a Simple Word Guessing Game App In Java NetBeans..


 11 - Java Login & Register Form Design Source Code (2) 

Java Login & Register Form Design Source Code (2)

how to design a login and register form in java netbeans and connect these two forms with mysql database.


 12 - Java Login & Register Form With Text File Source Code 

Java Login & Register Form With Text File Source Code

How To Design a Login And Register Form + how to use those two form with a text file so the user can signup and signin.


 13 - Java Drawing Applicatiom Source Code 

Java Drawing Applicatiom Source Code

How To Create a simple drawing application with basic features.






if you want to download Premium Java Projects source code, click on the download button below