import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.*;
import java.util.List;
public class MedicalDiagnosisSystem {
private final Map<String, Disease> diseaseDatabase = new HashMap<>();
private JFrame frame;
private JTextArea symptomInput;
private JTextArea resultArea;
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new MedicalDiagnosisSystem().initializeSystem());
}
private void initializeSystem() {
initializeDiseases();
createAndShowGUI();
}
private void initializeDiseases() {
diseaseDatabase.put("Common Cold", new Disease("Common Cold",
Arrays.asList("cough", "sneezing", "runny nose", "sore throat"),
"1. Get plenty of rest\n2. Drink fluids\n3. Over-the-counter cold medicine"));
diseaseDatabase.put("Flu (Influenza)", new Disease("Flu (Influenza)",
Arrays.asList("fever", "body aches", "headache", "cough", "fatigue"),
"1. Antiviral medication\n2. Rest\n3. Stay hydrated\n4. See doctor if symptoms worsen"));
diseaseDatabase.put("Allergies", new Disease("Allergies",
Arrays.asList("sneezing", "itchy eyes", "runny nose", "rash"),
"1. Antihistamines\n2. Avoid allergens\n3. Nasal sprays"));
diseaseDatabase.put("COVID-19", new Disease("COVID-19",
Arrays.asList("fever", "cough", "shortness of breath", "loss of taste/smell"),
"1. Self-isolate immediately\n2. Get tested\n3. Monitor symptoms\n4. Seek emergency care for severe symptoms"));
}
private void createAndShowGUI() {
frame = new JFrame("Medical Diagnosis System");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
mainPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
JLabel headerLabel = new JLabel("Medical Diagnosis System", JLabel.CENTER);
headerLabel.setFont(new Font("Arial", Font.BOLD, 20));
mainPanel.add(headerLabel, BorderLayout.NORTH);
JPanel inputPanel = new JPanel(new BorderLayout(5, 5));
JLabel symptomLabel = new JLabel("Enter your symptoms (comma-separated):");
symptomInput = new JTextArea(3, 30);
symptomInput.setLineWrap(true);
symptomInput.setWrapStyleWord(true);
JScrollPane inputScroll = new JScrollPane(symptomInput);
JLabel exampleLabel = new JLabel("Example: cough, fever, headache");
exampleLabel.setForeground(Color.GRAY);
inputPanel.add(symptomLabel, BorderLayout.NORTH);
inputPanel.add(inputScroll, BorderLayout.CENTER);
inputPanel.add(exampleLabel, BorderLayout.SOUTH);
JPanel resultPanel = new JPanel(new BorderLayout(5, 5));
JLabel resultLabel = new JLabel("Diagnosis Results:");
resultArea = new JTextArea(10, 30);
resultArea.setEditable(false);
resultArea.setLineWrap(true);
resultArea.setWrapStyleWord(true);
JScrollPane resultScroll = new JScrollPane(resultArea);
resultPanel.add(resultLabel, BorderLayout.NORTH);
resultPanel.add(resultScroll, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
JButton diagnoseButton = new JButton("Diagnose");
diagnoseButton.setPreferredSize(new Dimension(120, 35));
diagnoseButton.addActionListener(this::performDiagnosis);
JButton clearButton = new JButton("Clear");
clearButton.setPreferredSize(new Dimension(120, 35));
clearButton.addActionListener(e -> {
symptomInput.setText("");
resultArea.setText("");
});
buttonPanel.add(diagnoseButton);
buttonPanel.add(clearButton);
mainPanel.add(inputPanel, BorderLayout.NORTH);
mainPanel.add(resultPanel, BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
frame.add(mainPanel);
frame.setVisible(true);
}
private void performDiagnosis(ActionEvent e) {
String input = symptomInput.getText().trim().toLowerCase();
if (input.isEmpty()) {
JOptionPane.showMessageDialog(frame, "Please enter at least one symptom",
"Input Error", JOptionPane.WARNING_MESSAGE);
return;
}
List<String> symptoms = Arrays.asList(input.split("\\s*,\\s*"));
DiagnosisResult result = diagnose(symptoms);
StringBuilder output = new StringBuilder();
if (result.getPossibleDiseases().isEmpty()) {