Untitled

mail@pastecode.io avatar
unknown
plain_text
17 days ago
7.9 kB
3
Indexable
Never
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDate;
import java.util.LinkedList;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

// Class representing health data
class HealthData {
    String date;
    int steps;
    int waterIntake;
    int sleepHours;
    int meals;

    public HealthData(String date, int steps, int waterIntake, int sleepHours, int meals) {
        this.date = date;
        this.steps = steps;
        this.waterIntake = waterIntake;
        this.sleepHours = sleepHours;
        this.meals = meals;
    }

    public String[] toArray() {
        return new String[] {date, String.valueOf(steps), String.valueOf(waterIntake),
                             String.valueOf(sleepHours), String.valueOf(meals)};
    }
}

public class HealthTrackerGUI extends JFrame {
    LinkedList<HealthData> healthList = new LinkedList<>();
    DefaultTableModel model;
    
    
    public HealthTrackerGUI() {
        setTitle("Health Tracker");
        setSize(1366, 768);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        
        // Layout and Components
        setLayout(new BorderLayout());
        
        // Panel for input fields
        JPanel inputPanel = new JPanel();
        inputPanel.setLayout(new GridLayout(5, 2));
        
        // Input fields
        JLabel dateLabel = new JLabel("Date (YYYY-MM-DD):");
        JTextField dateField = new JTextField();
        dateField.setText(LocalDate.now().toString());
        JLabel stepsLabel = new JLabel("Steps:");
        JTextField stepsField = new JTextField();
        JLabel waterLabel = new JLabel("Water Intake (ml):");
        JTextField waterField = new JTextField();
        JLabel sleepLabel = new JLabel("Sleep (hours):");
        JTextField sleepField = new JTextField();
        JLabel mealsLabel = new JLabel("Meals:");
        JTextField mealsField = new JTextField();
        
        inputPanel.add(dateLabel);
        inputPanel.add(dateField);
        inputPanel.add(stepsLabel);
        inputPanel.add(stepsField);
        inputPanel.add(waterLabel);
        inputPanel.add(waterField);
        inputPanel.add(sleepLabel);
        inputPanel.add(sleepField);
        inputPanel.add(mealsLabel);
        inputPanel.add(mealsField);
        
        // Add input panel to the top of the frame
        add(inputPanel, BorderLayout.NORTH);
        
        // Table to display health records
        String[] columns = {"Date", "Steps", "Water Intake (ml)", "Sleep (hours)", "Meals"};
        model = new DefaultTableModel(columns, 0);
        JTable table = new JTable(model);
        JScrollPane tablePane = new JScrollPane(table);
        add(tablePane, BorderLayout.CENTER);
        
        // Buttons for actions
        JPanel buttonPanel = new JPanel();
        JButton addButton = new JButton("Add Record");
        JButton editButton = new JButton("Edit Record");
        JButton deleteButton = new JButton("Delete Record");
        JButton avgStepsButton = new JButton("Average Steps");
        JButton avgWaterButton = new JButton("Average Water Intake");
        
        buttonPanel.add(addButton);
        buttonPanel.add(editButton);
        buttonPanel.add(deleteButton);
        buttonPanel.add(avgStepsButton);
        buttonPanel.add(avgWaterButton);
        
        add(buttonPanel, BorderLayout.SOUTH);
        
        // Event Listeners for buttons
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String date = dateField.getText();
                int steps = Integer.parseInt(stepsField.getText());
                int waterIntake = Integer.parseInt(waterField.getText());
                int sleepHours = Integer.parseInt(sleepField.getText());
                int meals = Integer.parseInt(mealsField.getText());
                
                HealthData newData = new HealthData(date, steps, waterIntake, sleepHours, meals);
                healthList.add(newData);
                model.addRow(newData.toArray());
                clearFields();
            }
        });
        
        editButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int selectedRow = table.getSelectedRow();
                if (selectedRow >= 0) {
                    HealthData data = healthList.get(selectedRow);
                    data.steps = Integer.parseInt(stepsField.getText());
                    data.waterIntake = Integer.parseInt(waterField.getText());
                    data.sleepHours = Integer.parseInt(sleepField.getText());
                    data.meals = Integer.parseInt(mealsField.getText());
                    
                    model.setValueAt(data.steps, selectedRow, 1);
                    model.setValueAt(data.waterIntake, selectedRow, 2);
                    model.setValueAt(data.sleepHours, selectedRow, 3);
                    model.setValueAt(data.meals, selectedRow, 4);
                    clearFields();
                } else {
                    JOptionPane.showMessageDialog(null, "Please select a row to edit.");
                }
            }
        });
        
        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int selectedRow = table.getSelectedRow();
                if (selectedRow >= 0) {
                    healthList.remove(selectedRow);
                    model.removeRow(selectedRow);
                } else {
                    JOptionPane.showMessageDialog(null, "Please select a row to delete.");
                }
            }
        });
        
        avgStepsButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (healthList.isEmpty()) {
                    JOptionPane.showMessageDialog(null, "No records to calculate.");
                } else {
                    int totalSteps = 0;
                    for (HealthData data : healthList) {
                        totalSteps += data.steps;
                    }
                    JOptionPane.showMessageDialog(null, "Average Steps: " + totalSteps / healthList.size());
                }
            }
        });
        
        avgWaterButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (healthList.isEmpty()) {
                    JOptionPane.showMessageDialog(null, "No records to calculate.");
                } else {
                    int totalWater = 0;
                    for (HealthData data : healthList) {
                        totalWater += data.waterIntake;
                    }
                    JOptionPane.showMessageDialog(null, "Average Water Intake (ml): " + totalWater / healthList.size());
                }
            }
        });
    }
    
    // Method to clear the input fields after an action
    private void clearFields() {
        for (Component c : getContentPane().getComponents()) {
            if (c instanceof JPanel) {
                for (Component field : ((JPanel) c).getComponents()) {
                    if (field instanceof JTextField) {
                        ((JTextField) field).setText("");
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        HealthTrackerGUI gui = new HealthTrackerGUI();
        gui.setVisible(true);
    }
}
Leave a Comment