diff --git a/ExpenseTrackerApp.java b/ExpenseTrackerApp.java new file mode 100644 index 0000000..a50ae06 --- /dev/null +++ b/ExpenseTrackerApp.java @@ -0,0 +1,214 @@ +import javax.swing.*; +import javax.swing.table.DefaultTableModel; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class ExpenseTrackerApp extends JFrame { + + private JTable expenseTable; + private DefaultTableModel tableModel; + private double totalExpenses = 0.0; + private Double budget = null; + + public ExpenseTrackerApp() { + setTitle("Expense Tracker"); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setSize(800, 600); + setLocationRelativeTo(null); + + // Main panel + JPanel mainPanel = new JPanel(new BorderLayout()); + + // Menu bar + JMenuBar menuBar = new JMenuBar(); + JMenu searchMenu = new JMenu("Search"); + JMenuItem searchByCategory = new JMenuItem("Search by Category"); + searchByCategory.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String category = JOptionPane.showInputDialog(null, "Enter category to search:"); + if (category != null && !category.isEmpty()) { + searchExpensesByCategory(category); + } + } + }); + searchMenu.add(searchByCategory); + menuBar.add(searchMenu); + setJMenuBar(menuBar); + + // Expense Input Panel + JPanel expenseInputPanel = new JPanel(new GridLayout(3, 2, 5, 5)); + expenseInputPanel.setBorder(BorderFactory.createTitledBorder("Add Expense")); + + JLabel amountLabel = new JLabel("Amount:"); + JTextField amountField = new JTextField(); + JLabel categoryLabel = new JLabel("Category:"); + JTextField categoryField = new JTextField(); + JLabel descriptionLabel = new JLabel("Description:"); + JTextField descriptionField = new JTextField(); + + // Add ActionListener to each text field to listen for Enter key press + amountField.addActionListener(new EnterListener(categoryField)); + categoryField.addActionListener(new EnterListener(descriptionField)); + descriptionField.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + addExpense(amountField.getText(), categoryField.getText(), descriptionField.getText()); + clearFields(amountField, categoryField, descriptionField); + } + }); + + expenseInputPanel.add(amountLabel); + expenseInputPanel.add(amountField); + expenseInputPanel.add(categoryLabel); + expenseInputPanel.add(categoryField); + expenseInputPanel.add(descriptionLabel); + expenseInputPanel.add(descriptionField); + + // Expense Table Panel + JPanel expenseTablePanel = new JPanel(new BorderLayout()); + expenseTablePanel.setBorder(BorderFactory.createTitledBorder("Expense List")); + + tableModel = new DefaultTableModel(new Object[]{"Amount", "Category", "Description"}, 0); + expenseTable = new JTable(tableModel); + JScrollPane scrollPane = new JScrollPane(expenseTable); + + expenseTablePanel.add(scrollPane, BorderLayout.CENTER); + + // Budget Panel + JPanel budgetPanel = new JPanel(new BorderLayout()); + budgetPanel.setBorder(BorderFactory.createTitledBorder("Budget")); + + JLabel budgetLabel = new JLabel("Enter Budget:"); + JTextField budgetField = new JTextField(10); + JButton setBudgetButton = new JButton("Set Budget"); + setBudgetButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + String budgetStr = budgetField.getText(); + if (isValidNumber(budgetStr)) { + budget = Double.parseDouble(budgetStr); + budgetLabel.setText("Budget: $" + budget); + budgetField.setText(""); + } else { + showError("Error: Please enter a valid budget."); + } + } + }); + + budgetPanel.add(budgetLabel, BorderLayout.NORTH); + budgetPanel.add(budgetField, BorderLayout.CENTER); + budgetPanel.add(setBudgetButton, BorderLayout.SOUTH); + + // Summary Panel + JPanel summaryPanel = new JPanel(new BorderLayout()); + summaryPanel.setBorder(BorderFactory.createTitledBorder("Summary")); + + JButton totalExpensesButton = new JButton("Total Expenses"); + totalExpensesButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + JOptionPane.showMessageDialog(null, "Total Expenses: $" + totalExpenses); + } + }); + + JButton budgetUtilizationButton = new JButton("Budget Utilization"); + budgetUtilizationButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (budget == null) { + showError("Error: Please set the budget first."); + } else { + double utilization = (totalExpenses / budget) * 100.0; + JOptionPane.showMessageDialog(null, "Budget Utilization: " + String.format("%.2f", utilization) + "%"); + } + } + }); + + summaryPanel.add(totalExpensesButton, BorderLayout.NORTH); + summaryPanel.add(budgetUtilizationButton, BorderLayout.SOUTH); + + mainPanel.add(expenseInputPanel, BorderLayout.NORTH); + mainPanel.add(expenseTablePanel, BorderLayout.CENTER); + mainPanel.add(budgetPanel, BorderLayout.WEST); + mainPanel.add(summaryPanel, BorderLayout.SOUTH); + + add(mainPanel); + } + + private static class EnterListener implements ActionListener { + private JTextField nextField; + + public EnterListener(JTextField nextField) { + this.nextField = nextField; + } + + @Override + public void actionPerformed(ActionEvent e) { + nextField.requestFocusInWindow(); + } + } + + private void addExpense(String amountStr, String category, String description) { + if (!isValidNumber(amountStr)) { + showError("Error: Please enter a valid amount."); + return; + } + + double amount = Double.parseDouble(amountStr); + totalExpenses += amount; + Object[] rowData = {String.format("$%.2f", amount), category, description}; + tableModel.addRow(rowData); + + if (budget != null && totalExpenses > budget) { + JOptionPane.showMessageDialog(null, "Warning: Total expenses have exceeded the budget!"); + } + } + + private void clearFields(JTextField... fields) { + for (JTextField field : fields) { + field.setText(""); + } + } + + private boolean isValidNumber(String str) { + try { + Double.parseDouble(str); + return true; + } catch (NumberFormatException e) { + return false; + } + } + + private void showError(String message) { + JOptionPane.showMessageDialog(null, message, "Input Error", JOptionPane.ERROR_MESSAGE); + } + + private void searchExpensesByCategory(String category) { + DefaultTableModel filteredModel = new DefaultTableModel(new Object[]{"Amount", "Category", "Description"}, 0); + for (int i = 0; i < tableModel.getRowCount(); i++) { + if (tableModel.getValueAt(i, 1).toString().equalsIgnoreCase(category)) { + filteredModel.addRow(new Object[]{tableModel.getValueAt(i, 0), tableModel.getValueAt(i, 1), tableModel.getValueAt(i, 2)}); + } + } + if (filteredModel.getRowCount() > 0) { + JTable filteredTable = new JTable(filteredModel); + JOptionPane.showMessageDialog(null, new JScrollPane(filteredTable), "Expenses for Category '" + category + "'", JOptionPane.PLAIN_MESSAGE); + } else { + JOptionPane.showMessageDialog(null, "No expenses found for Category '" + category + "'."); + } + } + + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> { + try { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } catch (Exception e) { + e.printStackTrace(); + } + ExpenseTrackerApp app = new ExpenseTrackerApp(); + app.setVisible(true); + }); + } +} diff --git a/README.md b/README.md index 1a0c609..27cf3a9 100644 --- a/README.md +++ b/README.md @@ -1 +1,45 @@ -JAVA/CPP repo for OPENHACK. +# Simple Text Editor + +This is a simple text editor application created using Java Swing. + +## Features + +- Create new text files. +- Open existing text files. +- Save text files. +- Print text files. +- Cut, copy, and paste text. +- Close files. + +## How to Run + +1. Ensure you have Java installed on your PC. If not, download and install Java from [here](https://www.java.com/en/download/). +2. Download the `editor.java` file from this repository. +3. Open Command Prompt (on Windows) or Terminal (on macOS/Linux). +4. Navigate to the directory where the `editor.java` file is located. +5. Compile the Java source file using the following command: + ```bash + javac editor.java + ``` +6. Run the compiled Java program using the following command: + ```bash + java editor + ``` + +## Usage + +- **Creating a New File**: Click on the "File" menu and select "New". +- **Opening an Existing File**: Click on the "File" menu and select "Open". +- **Saving a File**: Click on the "File" menu and select "Save". +- **Printing a File**: Click on the "File" menu and select "Print". +- **Editing Operations**: Use the "Edit" menu for cut, copy, and paste operations. +- **Cutting Text**: Select the text you want to cut, then click on the "Edit" menu and select "Cut". +- **Copying Text**: Select the text you want to copy, then click on the "Edit" menu and select "Copy". +- **Pasting Text**: Place the cursor where you want to paste the text, then click on the "Edit" menu and select "Paste". +- **Closing a File**: Click on the "File" menu and select "Close" to close the current file window. + +## Notes + +- This text editor supports editing multiple text files simultaneously. +- The application uses Java Swing for the graphical user interface. + diff --git a/READme(Expense Tracker Application).md b/READme(Expense Tracker Application).md new file mode 100644 index 0000000..11bff5d --- /dev/null +++ b/READme(Expense Tracker Application).md @@ -0,0 +1,42 @@ + +# Expense Tracker Application + +## Description +This is a simple expense tracker application with a graphical user interface (GUI) built using Java Swing. It allows users to input their expenses, categorize them, and track their spending. Users can also set a budget and get insights into their spending habits and financial health. + +![ETA](https://github.com/Shubham25104/OH24-CPP-Java/assets/117568702/9c0133cc-13a6-4867-9d5f-5f0276b04372) + +## Features +- Add expenses with amount, category, and description fields. +- Display the list of expenses with a total amount. +- Set a budget for expenses. +- Calculate and display total expenses. +- Calculate and display budget utilization. + +## Screenshots +[//]: # (You can add screenshots of the application here.) + +## Usage +1. Run the application. +2. Enter the budget in the provided text field and click "Set Budget". +3. Enter the amount, category, and description of the expense in the corresponding text fields. +4. Press Enter or click outside the text fields to add the expense to the list. +5. Click "Total Expenses" to view the total expenses. +6. Click "Budget Utilization" to view the budget utilization. + +## Dependencies +-Java Development Kit (JDK) +-Swing GUI library (included in JDK) + +## Notes +- This is a simple expense tracker application for educational purposes. +- It lacks advanced features like database integration, user authentication, etc. + +## How to Run +Compile and run the `ExpenseTrackerApp.java` file. + +```bash +javac ExpenseTrackerApp.java +java ExpenseTrackerApp + + diff --git a/Texteditor.java b/Texteditor.java new file mode 100644 index 0000000..e60bece --- /dev/null +++ b/Texteditor.java @@ -0,0 +1,268 @@ +// Java Program to create a text editor using java + +import javax.swing.*; +import java.io.*; +import java.awt.event.*; +import javax.swing.plaf.metal.*; + +class Texteditor extends JFrame implements ActionListener { + // Text component + + JTextArea t; + private JFrame[] frames; + private JTextArea[] textAreas; + private int numOpenFiles; + + // Frame + JFrame f; + + // Constructor + Texteditor() { + // Create a frame + f = new JFrame("editor"); + + try { + // Set metal look and feel + UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); + + // Set theme to ocean + MetalLookAndFeel.setCurrentTheme(new OceanTheme()); + } catch (Exception e) { + } + + // Text component + t = new JTextArea(); + // Initialize arrays + frames = new JFrame[10]; // Assuming a maximum of 10 open files + textAreas = new JTextArea[10]; + numOpenFiles = 0; + + // Create a menubar + JMenuBar mb = new JMenuBar(); + + // Create amenu for menu + JMenu m1 = new JMenu("File"); + + // Create menu items + JMenuItem mi1 = new JMenuItem("New"); + JMenuItem mi2 = new JMenuItem("Open"); + JMenuItem mi3 = new JMenuItem("Save"); + JMenuItem mi9 = new JMenuItem("Print"); + + // Add action listener + mi1.addActionListener(this); + mi2.addActionListener(this); + mi3.addActionListener(this); + mi9.addActionListener(this); + + m1.add(mi1); + m1.add(mi2); + m1.add(mi3); + m1.add(mi9); + + // Create amenu for menu + JMenu m2 = new JMenu("Edit"); + + // Create menu items + JMenuItem mi4 = new JMenuItem("cut"); + JMenuItem mi5 = new JMenuItem("copy"); + JMenuItem mi6 = new JMenuItem("paste"); + + // Add action listener + mi4.addActionListener(this); + mi5.addActionListener(this); + mi6.addActionListener(this); + + m2.add(mi4); + m2.add(mi5); + m2.add(mi6); + + JMenuItem mc = new JMenuItem("close"); + + mc.addActionListener(this); + + mb.add(m1); + mb.add(m2); + mb.add(mc); + + f.setJMenuBar(mb); + f.add(t); + f.setSize(500, 500); + f.show(); + } + + // If a button is pressed + public void actionPerformed(ActionEvent e) { + String s = e.getActionCommand(); + + if (s.equals("cut")) { + t.cut(); + } else if (s.equals("copy")) { + t.copy(); + } else if (s.equals("paste")) { + t.paste(); + } else if (s.equals("Save")) { + // Create an object of JFileChooser class + JFileChooser j = new JFileChooser("f:"); + + // Invoke the showsSaveDialog function to show the save dialog + int r = j.showSaveDialog(null); + + if (r == JFileChooser.APPROVE_OPTION) { + + // Set the label to the path of the selected directory + File fi = new File(j.getSelectedFile().getAbsolutePath()); + + try { + // Create a file writer + FileWriter wr = new FileWriter(fi, false); + + // Create buffered writer to write + BufferedWriter w = new BufferedWriter(wr); + + // Write + w.write(t.getText()); + + w.flush(); + w.close(); + } catch (Exception evt) { + JOptionPane.showMessageDialog(f, evt.getMessage()); + } + } // If the user cancelled the operation + else { + JOptionPane.showMessageDialog(f, "the user cancelled the operation"); + } + } else if (s.equals("Print")) { + try { + // print the file + t.print(); + } catch (Exception evt) { + JOptionPane.showMessageDialog(f, evt.getMessage()); + } + } else if (s.equals("Open")) { + // Create an object of JFileChooser class + JFileChooser j = new JFileChooser("f:"); + + // Invoke the showsOpenDialog function to show the save dialog + int r = j.showOpenDialog(null); + + // If the user selects a file + if (r == JFileChooser.APPROVE_OPTION) { + // Set the label to the path of the selected directory + File fi = new File(j.getSelectedFile().getAbsolutePath()); + + try { + // String + String s1 = "", sl = ""; + + // File reader + FileReader fr = new FileReader(fi); + + // Buffered reader + BufferedReader br = new BufferedReader(fr); + + // Initialize sl + sl = br.readLine(); + + // Take the input from the file + while ((s1 = br.readLine()) != null) { + sl = sl + "\n" + s1; + } + + // Set the text + t.setText(sl); + } catch (Exception evt) { + JOptionPane.showMessageDialog(f, evt.getMessage()); + } + } // If the user cancelled the operation + else { + JOptionPane.showMessageDialog(f, "the user cancelled the operation"); + } + } else if (s.equals("New")) { + t.setText(""); + createNewFile(); + } else if (s.equals("close")) { + f.setVisible(false); + } + } + + private void createNewFile() { + JFrame frame = new JFrame("Untitled-" + (numOpenFiles + 1)); + JTextArea textArea = new JTextArea(); + JScrollPane scrollPane = new JScrollPane(textArea); + frame.add(scrollPane); + + // Create a menubar + JMenuBar mb = new JMenuBar(); + + // Create a menu for File + JMenu m1 = new JMenu("File"); + + // Create menu items for File + JMenuItem mi1 = new JMenuItem("New"); + JMenuItem mi2 = new JMenuItem("Open"); + JMenuItem mi3 = new JMenuItem("Save"); + JMenuItem mi9 = new JMenuItem("Print"); + + // Add action listeners to menu items + mi1.addActionListener(this); + mi2.addActionListener(this); + mi3.addActionListener(this); + mi9.addActionListener(this); + + // Add menu items to the File menu + m1.add(mi1); + m1.add(mi2); + m1.add(mi3); + m1.add(mi9); + + // Create a menu for Edit + JMenu m2 = new JMenu("Edit"); + + // Create menu items for Edit + JMenuItem mi4 = new JMenuItem("cut"); + JMenuItem mi5 = new JMenuItem("copy"); + JMenuItem mi6 = new JMenuItem("paste"); + + // Add action listeners to menu items + mi4.addActionListener(this); + mi5.addActionListener(this); + mi6.addActionListener(this); + + // Add menu items to the Edit menu + m2.add(mi4); + m2.add(mi5); + m2.add(mi6); + + // Create a menu item for Close + JMenuItem mc = new JMenuItem("close"); + + // Add action listener to the Close menu item + mc.addActionListener(this); + + // Add File and Edit menus to the menubar + mb.add(m1); + mb.add(m2); + + // Add Close menu item to the menubar + mb.add(mc); + + // Set the menubar for the frame + frame.setJMenuBar(mb); + + // Set frame size and visibility + frame.setSize(500, 500); + frame.setVisible(true); + + // Store references to JFrame and JTextArea + frames[numOpenFiles] = frame; + textAreas[numOpenFiles] = textArea; + numOpenFiles++; +} + + + // Main class + public static void main(String args[]) { + Texteditor e = new Texteditor(); + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..eecb906 --- /dev/null +++ b/readme.md @@ -0,0 +1,46 @@ +![TE](https://github.com/Shubham25104/OH24-CPP-Java/assets/117568702/0cad3314-8e5f-4e73-b17c-cac3e380ebbc) +# Simple Text Editor + +This is a simple text editor application created using Java Swing. + +## Features + +- Create new text files. +- Open existing text files. +- Save text files. +- Print text files. +- Cut, copy, and paste text. +- Close files. + +## How to Run + +1. Ensure you have Java installed on your PC. If not, download and install Java from [here](https://www.java.com/en/download/). +2. Download the `editor.java` file from this repository. +3. Open Command Prompt (on Windows) or Terminal (on macOS/Linux). +4. Navigate to the directory where the `editor.java` file is located. +5. Compile the Java source file using the following command: + ```bash + javac editor.java + ``` +6. Run the compiled Java program using the following command: + ```bash + java editor + ``` + +## Usage + +- **Creating a New File**: Click on the "File" menu and select "New". +- **Opening an Existing File**: Click on the "File" menu and select "Open". +- **Saving a File**: Click on the "File" menu and select "Save". +- **Printing a File**: Click on the "File" menu and select "Print". +- **Editing Operations**: Use the "Edit" menu for cut, copy, and paste operations. +- **Cutting Text**: Select the text you want to cut, then click on the "Edit" menu and select "Cut". +- **Copying Text**: Select the text you want to copy, then click on the "Edit" menu and select "Copy". +- **Pasting Text**: Place the cursor where you want to paste the text, then click on the "Edit" menu and select "Paste". +- **Closing a File**: Click on the "File" menu and select "Close" to close the current file window. + +## Notes + +- This text editor supports editing multiple text files simultaneously. +- The application uses Java Swing for the graphical user interface. +