Untitled

 avatar
unknown
plain_text
5 months ago
20 kB
3
Indexable
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
 */
package javaapplication9;

import javax.swing.*;
import java.awt.*;
import javax.swing.border.TitledBorder;
import java.text.SimpleDateFormat;
import java.util.Date;


public class JavaApplication9 {
    private static JFrame frame;
    private static JTextField maintenanceCostField;
    private static final DefaultListModel<String> maintenanceHistoryListModel = new DefaultListModel<>();
    private static final DefaultListModel<String> harvestHistoryListModel = new DefaultListModel<>();

    public static void main(String[] args) {
        showStartMenu();
    }

    private static void showStartMenu() {
        // Create the frame for the Start Menu
        frame = new JFrame("CALAMANSI FARM SYSTEM");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600); // Increased the size of the frame
        frame.setLayout(new BorderLayout());

        // Welcome panel
        JPanel welcomePanel = new JPanel();
        welcomePanel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(10, 0, 10, 0);
        JLabel welcomeLabel = new JLabel("Welcome", SwingConstants.CENTER);
        welcomeLabel.setFont(new Font("Arial", Font.BOLD, 20));
        welcomePanel.add(welcomeLabel, gbc);

        gbc.gridy++;
        JLabel toLabel = new JLabel("to", SwingConstants.CENTER);
        toLabel.setFont(new Font("Arial", Font.BOLD, 20));
        welcomePanel.add(toLabel, gbc);

        gbc.gridy++;
        JLabel systemLabel = new JLabel("Calamansi Farm System", SwingConstants.CENTER);
        systemLabel.setFont(new Font("Arial", Font.BOLD, 20));
        welcomePanel.add(systemLabel, gbc);

        frame.add(welcomePanel, BorderLayout.CENTER);

        // Main Menu Panel
        JPanel mainMenuPanel = new JPanel();
        JButton startButton = new JButton("Start");
        JButton exitButton = new JButton("Exit");
        startButton.setPreferredSize(new Dimension(250, 50));
        exitButton.setPreferredSize(new Dimension(250, 50));
        mainMenuPanel.add(startButton);
        mainMenuPanel.add(exitButton);
        frame.add(mainMenuPanel, BorderLayout.SOUTH);

        // Start button action
        startButton.addActionListener(e -> showCalamansiFarm());

        // Exit button action
        exitButton.addActionListener(e -> System.exit(0));

        frame.setVisible(true);
    }

    private static void showCalamansiFarm() {
        frame.getContentPane().removeAll();
        frame.setLayout(new BorderLayout());

        // Welcome panel
        JPanel welcomePanel = new JPanel();
        welcomePanel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(10, 0, 10, 0);
        JLabel welcomeLabel = new JLabel("Welcome", SwingConstants.CENTER);
        welcomeLabel.setFont(new Font("Arial", Font.BOLD, 20));
        welcomePanel.add(welcomeLabel, gbc);

        gbc.gridy++;
        JLabel toLabel = new JLabel("to", SwingConstants.CENTER);
        toLabel.setFont(new Font("Arial", Font.BOLD, 20));
        welcomePanel.add(toLabel, gbc);

        gbc.gridy++;
        JLabel systemLabel = new JLabel("Calamansi Farm System", SwingConstants.CENTER);
        systemLabel.setFont(new Font("Arial", Font.BOLD, 20));
        welcomePanel.add(systemLabel, gbc);

        frame.add(welcomePanel, BorderLayout.CENTER);

        // Main Menu Panel
        JPanel mainMenuPanel = new JPanel();
        JButton exitButton = new JButton("Exit");
        exitButton.setPreferredSize(new Dimension(250, 50));
        mainMenuPanel.add(exitButton);
        frame.add(mainMenuPanel, BorderLayout.SOUTH);

        // Tabbed Pane for Maintenance, Harvest, and History sections
        JTabbedPane tabbedPane = new JTabbedPane();
        frame.add(tabbedPane, BorderLayout.CENTER);

        // Centering the panels
        GridBagConstraints gbcTab = new GridBagConstraints();
        gbcTab.gridx = 0;
        gbcTab.gridy = 0;
        gbcTab.weightx = 1;
        gbcTab.weighty = 1;
        gbcTab.anchor = GridBagConstraints.CENTER;
        gbcTab.fill = GridBagConstraints.NONE;

        // Maintenance Tab
        JPanel maintenanceContainer = createSectionContainer("Maintenance", new Color(255, 240, 220));
        JPanel maintenancePanel = createMaintenancePanel();
        maintenanceContainer.setPreferredSize(new Dimension(600, 400)); // Increased the size of the container
        maintenanceContainer.add(maintenancePanel);
        JPanel maintenanceWrapper = new JPanel(new GridBagLayout());
        maintenanceWrapper.add(maintenanceContainer, gbcTab);
        tabbedPane.addTab("MAINTENANCE", maintenanceWrapper);

        // Harvest Tab
        JPanel harvestContainer = createSectionContainer("Harvest", new Color(220, 255, 220));
        JPanel harvestPanel = createHarvestPanel();
        harvestContainer.setPreferredSize(new Dimension(600, 400)); // Increased the size of the container
        harvestContainer.add(harvestPanel);
        JPanel harvestWrapper = new JPanel(new GridBagLayout());
        harvestWrapper.add(harvestContainer, gbcTab);
        tabbedPane.addTab("HARVEST", harvestWrapper);

        // History Tab
        JPanel historyContainer = createSectionContainer("History", new Color(220, 220, 255));
        JPanel historyPanel = createHistoryPanel();
        historyContainer.setPreferredSize(new Dimension(600, 400)); // Increased the size of the container
        historyContainer.add(historyPanel);
        JPanel historyWrapper = new JPanel(new GridBagLayout());
        historyWrapper.add(historyContainer, gbcTab);
        tabbedPane.addTab("HISTORY", historyWrapper);

        // Exit button action
        exitButton.addActionListener(e -> System.exit(0));

        frame.revalidate();
        frame.repaint();
    }

   private static JPanel createMaintenancePanel() {
    JPanel maintenancePanel = new JPanel();
    maintenancePanel.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.LINE_END;
    gbc.insets = new Insets(5, 5, 5, 5);

    // Fields and labels
    String[] labels = {"Date:", "Irrigation Cost:", "Fertilizer:", "Pesticide:", "Gas:", "Grass Cutting:"};
    JTextField[] fields = {new JTextField(10), new JTextField(6), new JTextField(6), new JTextField(6), new JTextField(6), new JTextField(6)};

    // Adding Date as a JSpinner (Date picker)
    JSpinner dateSpinner = new JSpinner(new SpinnerDateModel());
    dateSpinner.setEditor(new JSpinner.DateEditor(dateSpinner, "yyyy-MM-dd"));

    // Adding date field to the form
    JLabel dateLabel = new JLabel("Date:");
    dateLabel.setFont(new Font("Arial", Font.PLAIN, 16));
    gbc.gridx = 0;
    gbc.gridy = 0;
    maintenancePanel.add(createFieldPanel(dateLabel, dateSpinner), gbc);  // Add the date field correctly

    for (int i = 0; i < labels.length; i++) {
        JLabel label = new JLabel(labels[i]);
        label.setFont(new Font("Arial", Font.PLAIN, 16));
        fields[i].setFont(new Font("Arial", Font.PLAIN, 16));
        gbc.gridx = 0;
        gbc.gridy = i + 1; // Start from row 1 since the date field takes row 0
        maintenancePanel.add(createFieldPanel(label, fields[i]), gbc);
    }

    // Add Compute and Reset buttons
    JPanel buttonPanel = new JPanel();
    JButton computeButton = new JButton("Compute");
    JButton resetButton = new JButton("Reset");
    buttonPanel.add(computeButton);
    buttonPanel.add(resetButton);
    gbc.gridy++;
    gbc.anchor = GridBagConstraints.CENTER;
    maintenancePanel.add(buttonPanel, gbc);

    // Compute button action
    computeButton.addActionListener(e -> {
        String date = dateSpinner.getValue().toString(); // Use the JSpinner value for the date
        double irrigationCost = Double.parseDouble(fields[0].getText());
        double fertilizerCost = Double.parseDouble(fields[1].getText());
        double pesticideCost = Double.parseDouble(fields[2].getText());
        double gasCost = Double.parseDouble(fields[3].getText());
        double grassCuttingCost = Double.parseDouble(fields[4].getText());

        double totalMaintenanceCost = irrigationCost + fertilizerCost + pesticideCost + gasCost + grassCuttingCost;

        maintenanceCostField.setText(String.valueOf(totalMaintenanceCost));

        String receipt = "Maintenance Receipt:\n" +
                "---------------------------------\n" +
                "Date: " + date + "\n" +
                "Irrigation Cost: " + irrigationCost + "\n" +
                "Fertilizer Cost: " + fertilizerCost + "\n" +
                "Pesticide Cost: " + pesticideCost + "\n" +
                "Gas Cost: " + gasCost + "\n" +
                "Grass Cutting Cost: " + grassCuttingCost + "\n" +
                "---------------------------------\n" +
                "Total Maintenance Cost: " + totalMaintenanceCost + "\n" +
                "---------------------------------";

        JOptionPane.showMessageDialog(null, receipt);

        // Save to history
        maintenanceHistoryListModel.addElement("Maintenance - Date: " + date + ", Irrigation Cost: " + irrigationCost +
                ", Fertilizer Cost: " + fertilizerCost + ", Pesticide Cost: " + pesticideCost +
                ", Gas Cost: " + gasCost + ", Grass Cutting Cost: " + grassCuttingCost +
                ", Total Maintenance Cost: " + totalMaintenanceCost);
    });

    // Reset button action
    resetButton.addActionListener(e -> {
        for (JTextField field : fields) {
            field.setText("");
        }
        dateSpinner.setValue(new Date()); // Reset the date field to current date
    });

    return maintenancePanel;
}

private static JPanel createHarvestPanel() {
    JPanel harvestPanel = new JPanel();
    harvestPanel.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.LINE_END;
    gbc.insets = new Insets(5, 5, 5, 5);

    // Fields and labels
    String[] labels = {"Date:", "Price of Good Calamansi:", "Price of Reject Calamansi:", "Quantity of Good Calamansi:", "Quantity of Reject Calamansi:", "Salary of Farmers:", "Maintenance Cost:"};
    JTextField[] fields = {new JTextField(10), new JTextField(8), new JTextField(8), new JTextField(8), new JTextField(8), new JTextField(8), new JTextField(8)};

    // Adding Date as a JSpinner (Date picker)
    JSpinner dateSpinner = new JSpinner(new SpinnerDateModel());
    dateSpinner.setEditor(new JSpinner.DateEditor(dateSpinner, "yyyy-MM-dd"));

    // Adding date field directly to the panel
    JLabel dateLabel = new JLabel("Date:");
    dateLabel.setFont(new Font("Arial", Font.PLAIN, 16));
    gbc.gridx = 0;
    gbc.gridy = 0;
    harvestPanel.add(dateLabel, gbc);  // Add the date label correctly

    gbc.gridx = 1;
    harvestPanel.add(dateSpinner, gbc);  // Add the spinner field to the panel

    // Add the rest of the fields and labels
    for (int i = 0; i < labels.length; i++) {
        JLabel label = new JLabel(labels[i]);
        label.setFont(new Font("Arial", Font.PLAIN, 16));
        fields[i].setFont(new Font("Arial", Font.PLAIN, 16));
        gbc.gridx = 0;
        gbc.gridy = i + 1; // Start from row 1 since the date field takes row 0
        harvestPanel.add(label, gbc); // Add label

        gbc.gridx = 1;
        harvestPanel.add(fields[i], gbc); // Add the corresponding field
    }

    // Add Compute and Reset buttons
    JPanel buttonPanel = new JPanel();
    JButton computeButton = new JButton("Compute");
    JButton resetButton = new JButton("Reset");
    buttonPanel.add(computeButton);
    buttonPanel.add(resetButton);
    gbc.gridy++;
    gbc.anchor = GridBagConstraints.CENTER;
    harvestPanel.add(buttonPanel, gbc);

    // Compute button action
    computeButton.addActionListener(e -> {
        String date = dateSpinner.getValue().toString(); // Use the JSpinner value for the date
        double priceGood = Double.parseDouble(fields[0].getText());
        double priceReject = Double.parseDouble(fields[1].getText());
        double quantityGood = Double.parseDouble(fields[2].getText());
        double quantityReject = Double.parseDouble(fields[3].getText());
        double salary = Double.parseDouble(fields[4].getText());
        double maintenanceCost = Double.parseDouble(fields[5].getText());

        double totalIncome = (priceGood * quantityGood) + (priceReject * quantityReject);
        double totalExpenses = salary + maintenanceCost;
        double netProfit = totalIncome - totalExpenses;

        // Display result
        String receipt = "Harvest Receipt:\n" +
                "---------------------------------\n" +
                "Date: " + date + "\n" +
                "Total Income: " + totalIncome + "\n" +
                "Total Expenses: " + totalExpenses + "\n" +
                "Net Profit: " + netProfit + "\n" +
                "---------------------------------";

        JOptionPane.showMessageDialog(null, receipt);

        // Save to history
        harvestHistoryListModel.addElement("Harvest - Date: " + date + ", Total Income: " + totalIncome +
                ", Total Expenses: " + totalExpenses + ", Net Profit: " + netProfit);
    });

    // Reset button action
    resetButton.addActionListener(e -> {
        for (JTextField field : fields) {
            field.setText("");
        }
        dateSpinner.setValue(new Date()); // Reset the date field to current date
    });

    return harvestPanel;
}


private static JPanel createHistoryPanel() {
    JPanel historyPanel = new JPanel(new BorderLayout());

    JList<String> maintenanceHistoryList = new JList<>(maintenanceHistoryListModel);
    JList<String> harvestHistoryList = new JList<>(harvestHistoryListModel);

    JScrollPane maintenanceScrollPane = new JScrollPane(maintenanceHistoryList);
    JScrollPane harvestScrollPane = new JScrollPane(harvestHistoryList);

    JTabbedPane historyTabbedPane = new JTabbedPane();
    historyTabbedPane.addTab("Maintenance History", maintenanceScrollPane);
    historyTabbedPane.addTab("Harvest History", harvestScrollPane);

    historyPanel.add(historyTabbedPane, BorderLayout.CENTER);

    // Edit button
    JButton editButton = new JButton("Edit");
    historyPanel.add(editButton, BorderLayout.SOUTH);

    // Edit button action
    editButton.addActionListener(e -> {
        int maintenanceSelectedIndex = maintenanceHistoryList.getSelectedIndex();
        int harvestSelectedIndex = harvestHistoryList.getSelectedIndex();

        // Clear selections when switching tabs
        maintenanceHistoryList.clearSelection();
        harvestHistoryList.clearSelection();

        if (maintenanceSelectedIndex != -1) {
            String selectedValue = maintenanceHistoryListModel.getElementAt(maintenanceSelectedIndex);
            showEditDialog("Maintenance", selectedValue, maintenanceSelectedIndex);
        } else if (harvestSelectedIndex != -1) {
            String selectedValue = harvestHistoryListModel.getElementAt(harvestSelectedIndex);
            showEditDialog("Harvest", selectedValue, harvestSelectedIndex);
        } else {
            JOptionPane.showMessageDialog(null, "Please select an entry to edit.");
        }
    });

    return historyPanel;
}

private static void showEditDialog(String type, String value, int index) {
    // Create a new editPanel each time to ensure it's fresh
    JPanel editPanel = new JPanel(new GridLayout(0, 2));
    JTextField dateField = new JTextField(10);
    editPanel.add(new JLabel("Date:"));
    editPanel.add(dateField);

    String[] fields = value.split(", ");
    dateField.setText(fields[0].split(": ")[1]);

    if (type.equals("Maintenance")) {
        JTextField irrigationField = new JTextField(fields[1].split(": ")[1]);
        JTextField fertilizerField = new JTextField(fields[2].split(": ")[1]);
        JTextField pesticideField = new JTextField(fields[3].split(": ")[1]);
        JTextField gasField = new JTextField(fields[4].split(": ")[1]);
        JTextField grassCuttingField = new JTextField(fields[5].split(": ")[1]);

        editPanel.add(new JLabel("Irrigation Cost:"));
        editPanel.add(irrigationField);
        editPanel.add(new JLabel("Fertilizer Cost:"));
        editPanel.add(fertilizerField);
        editPanel.add(new JLabel("Pesticide Cost:"));
        editPanel.add(pesticideField);
        editPanel.add(new JLabel("Gas Cost:"));
        editPanel.add(gasField);
        editPanel.add(new JLabel("Grass Cutting Cost:"));
        editPanel.add(grassCuttingField);

        int result = JOptionPane.showConfirmDialog(null, editPanel, "Edit Maintenance Record", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            String updatedValue = String.format("Date: %s, Irrigation Cost: %s, Fertilizer Cost: %s, Pesticide Cost: %s, Gas Cost: %s, Grass Cutting Cost: %s",
                    dateField.getText(), irrigationField.getText(), fertilizerField.getText(), pesticideField.getText(), gasField.getText(), grassCuttingField.getText());
            maintenanceHistoryListModel.set(index, updatedValue);
        }
    } else if (type.equals("Harvest")) {
        JTextField priceGoodField = new JTextField(fields[1].split(": ")[1]);
        JTextField priceRejectField = new JTextField(fields[2].split(": ")[1]);
        JTextField qtyGoodField = new JTextField(fields[3].split(": ")[1]);
        JTextField qtyRejectField = new JTextField(fields[4].split(": ")[1]);
        JTextField salaryField = new JTextField(fields[5].split(": ")[1]);
        JTextField maintenanceCostField = new JTextField(fields[6].split(": ")[1]);

        editPanel.add(new JLabel("Price of Good Calamansi:"));
        editPanel.add(priceGoodField);
        editPanel.add(new JLabel("Price of Reject Calamansi:"));
        editPanel.add(priceRejectField);
        editPanel.add(new JLabel("Quantity of Good Calamansi:"));
        editPanel.add(qtyGoodField);
        editPanel.add(new JLabel("Quantity of Reject Calamansi:"));
        editPanel.add(qtyRejectField);
        editPanel.add(new JLabel("Salary of Farmers:"));
        editPanel.add(salaryField);
        editPanel.add(new JLabel("Maintenance Cost:"));
        editPanel.add(maintenanceCostField);

        int result = JOptionPane.showConfirmDialog(null, editPanel, "Edit Harvest Record", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            String updatedValue = String.format("Date: %s, Price of Good Calamansi: %s, Price of Reject Calamansi: %s, Quantity of Good Calamansi: %s, Quantity of Reject Calamansi: %s, Salary of Farmers: %s, Maintenance Cost: %s",
                    dateField.getText(), priceGoodField.getText(), priceRejectField.getText(), qtyGoodField.getText(), qtyRejectField.getText(), salaryField.getText(), maintenanceCostField.getText());
            harvestHistoryListModel.set(index, updatedValue);
        }
    } else {
        JOptionPane.showMessageDialog(null, "Invalid record type selected.");
    }
}



    private static JPanel createFieldPanel(JLabel label, JTextField field) {
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout(FlowLayout.LEFT));
        panel.add(label);
        panel.add(field);
        return panel;
    }

    private static JPanel createSectionContainer(String title, Color color) {
        JPanel container = new JPanel();
        container.setLayout(new BorderLayout());
        container.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createLineBorder(Color.BLACK),
                title, TitledBorder.LEFT, TitledBorder.TOP));
        container.setBackground(color);
        return container;
    }
}
Editor is loading...
Leave a Comment