code
code
*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import java.io.*;
import java.nio.file.*;
// this
// constant
loadUsersFromFile();
SwingUtilities.invokeLater(() -> {
createAndShowGUI();
});
}
try {
if (!Files.exists(Paths.get(USER_DATABASE))) {
return; // File doesn't exist yet
}
out.println(user.toStringForFile());
out.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Error saving user data: " +
e.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
}
}
mainFrame.add(cardPanel);
mainFrame.setVisible(true);
}
cardPanel.add(panel, "Login");
}
private static void createSignUpPanel() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 10, 5, 10);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
users.add(user);
saveUserToFile(user); // Save the new user to the file
cardPanel.add(panel, "SignUp");
}
String[] options = {
"1. Add User Details",
"2. Add Medication Details",
"3. Add Medical History",
"4. Remove",
"5. Edit",
"6. View Profiles & History", // Updated label
"7. Exit"
};
for (String option : options) {
JButton button = new JButton(option);
button.addActionListener(e -> {
String choice = option.substring(0, 1);
switch (choice) {
case "1" -> showAddUserDetailsDialog();
case "2" -> showAddMedicationDialog();
case "3" -> showAddMedicalHistoryDialog();
case "4" -> showRemoveMenuDialog();
case "5" -> showEditMenuDialog();
case "6" -> showProfilesDialog();
case "7" -> cardLayout.show(cardPanel, "MainMenu");
}
});
panel.add(button, gbc);
}
cardPanel.add(panel, "AdminMenu");
}
cardPanel.add(panel, "UserMenu");
}
if (user == null)
return;
// Allergies
JPanel allergyPanel = new JPanel();
allergyPanel.add(new JLabel("Add Allergy:"));
JTextField allergyField = new JTextField(15);
allergyPanel.add(allergyField);
JComboBox<String> severityCombo = new JComboBox<>(new String[] { "mild",
"severe" });
allergyPanel.add(severityCombo);
JButton addAllergyButton = new JButton("Add");
addAllergyButton.addActionListener(e -> {
if (!allergyField.getText().isEmpty()) {
user.allergies.add(allergyField.getText() + " (" +
severityCombo.getSelectedItem() + ")");
allergyField.setText("");
JOptionPane.showMessageDialog(panel, "Allergy added
successfully!");
}
});
allergyPanel.add(addAllergyButton);
panel.add(allergyPanel);
// Medical Conditions
JPanel conditionPanel = new JPanel();
conditionPanel.add(new JLabel("Add Medical Condition:"));
JTextField conditionField = new JTextField(15);
conditionPanel.add(conditionField);
JButton addConditionButton = new JButton("Add");
addConditionButton.addActionListener(e -> {
if (!conditionField.getText().isEmpty()) {
user.medicalConditions.add(conditionField.getText());
conditionField.setText("");
JOptionPane.showMessageDialog(panel, "Medical condition added
successfully!");
}
});
conditionPanel.add(addConditionButton);
panel.add(conditionPanel);
if (user == null)
return;
if (result == JOptionPane.OK_OPTION) {
if (!medField.getText().isEmpty() && !dosageField.getText().isEmpty()
&& !frequencyField.getText().isEmpty() && !
durationField.getText().isEmpty()) {
user.medications.add(new String[] {
medField.getText(),
dosageField.getText(),
frequencyField.getText(),
durationField.getText()
});
JOptionPane.showMessageDialog(mainFrame, "Medication added
successfully!");
}
}
}
if (user == null)
return;
if (result == JOptionPane.OK_OPTION) {
if (!historyField.getText().isEmpty() && !
dateField.getText().isEmpty()) {
user.medicalHistory.add(new String[] { historyField.getText(),
dateField.getText() });
JOptionPane.showMessageDialog(mainFrame, "Medical history added
successfully!");
}
}
}
String[] options = {
"1. User Account",
"2. User Detail",
"3. User Medication",
"4. User Medical History",
"5. Back"
};
if (user == null)
return;
if (confirm == JOptionPane.YES_OPTION) {
users.remove(user);
JOptionPane.showMessageDialog(mainFrame, "User account
removed.");
}
}
case "2" -> removeUserDetail(user);
case "3" -> removeUserMedication(user);
case "4" -> removeUserMedicalHistory(user);
}
}
if (choice.equals("Allergies")) {
if (user.allergies.isEmpty()) {
JOptionPane.showMessageDialog(mainFrame, "No allergies to remove.",
"Info",
JOptionPane.INFORMATION_MESSAGE);
return;
}
if (allergy != null) {
user.allergies.remove(allergy);
JOptionPane.showMessageDialog(mainFrame, "Allergy removed.");
}
} else {
if (user.medicalConditions.isEmpty()) {
JOptionPane.showMessageDialog(mainFrame, "No medical conditions to
remove.", "Info",
JOptionPane.INFORMATION_MESSAGE);
return;
}
if (condition != null) {
user.medicalConditions.remove(condition);
JOptionPane.showMessageDialog(mainFrame, "Medical condition
removed.");
}
}
}
if (choice != null) {
int index = -1;
for (int i = 0; i < medStrings.length; i++) {
if (medStrings[i].equals(choice)) {
index = i;
break;
}
}
if (index != -1) {
user.medications.remove(index);
JOptionPane.showMessageDialog(mainFrame, "Medication removed.");
}
}
}
if (choice != null) {
int index = -1;
for (int i = 0; i < historyStrings.length; i++) {
if (historyStrings[i].equals(choice)) {
index = i;
break;
}
}
if (index != -1) {
user.medicalHistory.remove(index);
JOptionPane.showMessageDialog(mainFrame, "Medical history
removed.");
}
}
}
String[] options = {
"1. User Detail",
"2. User Medication",
"3. User Medical History",
"4. Back"
};
if (user == null)
return;
if (choice == null)
return;
if (choice.equals("Allergies")) {
if (user.allergies.isEmpty()) {
JOptionPane.showMessageDialog(mainFrame, "No allergies to edit.",
"Info",
JOptionPane.INFORMATION_MESSAGE);
return;
}
if (allergy != null) {
int index = user.allergies.indexOf(allergy);
String newAllergy = JOptionPane.showInputDialog(
mainFrame,
"Enter new allergy:",
allergy);
if (condition != null) {
int index = user.medicalConditions.indexOf(condition);
String newCondition = JOptionPane.showInputDialog(
mainFrame,
"Enter new medical condition:",
condition);
if (choice != null) {
int index = -1;
for (int i = 0; i < medStrings.length; i++) {
if (medStrings[i].equals(choice)) {
index = i;
break;
}
}
if (index != -1) {
String[] med = user.medications.get(index);
if (result == JOptionPane.OK_OPTION) {
if (!medField.getText().isEmpty() && !
dosageField.getText().isEmpty()
&& !frequencyField.getText().isEmpty() && !
durationField.getText().isEmpty()) {
user.medications.set(index, new String[] {
medField.getText(),
dosageField.getText(),
frequencyField.getText(),
durationField.getText()
});
JOptionPane.showMessageDialog(mainFrame, "Medication
updated successfully!");
}
}
}
}
}
if (choice != null) {
int index = -1;
for (int i = 0; i < historyStrings.length; i++) {
if (historyStrings[i].equals(choice)) {
index = i;
break;
}
}
if (index != -1) {
String[] history = user.medicalHistory.get(index);
if (result == JOptionPane.OK_OPTION) {
if (!historyField.getText().isEmpty() && !
dateField.getText().isEmpty()) {
user.medicalHistory.set(index, new String[]
{ historyField.getText(), dateField.getText() });
JOptionPane.showMessageDialog(mainFrame, "Medical history
updated successfully!");
}
}
}
}
}
if (user == null)
return;
// Profile tab
JPanel profilePanel = new JPanel(new BorderLayout());
JTextArea profileText = new JTextArea();
profileText.setEditable(false);
profileText.append("Name: " + user.name + "\n");
profileText.append("Age: " + user.age + "\n");
profileText.append("Birthday: " + user.birthday + "\n");
profileText.append("Civil Status: " + user.civilStatus + "\n");
profileText.append("Blood Type: " + user.bloodType + "\n");
profileText.append("Contact Number: " + user.contactNumber + "\n");
profileText.append("Emergency Contact: " + user.emergencyContactNumber + "\
n");
profileText.append("Allergies: " + user.allergies + "\n");
profileText.append("Medical Conditions: " + user.medicalConditions + "\n");
profilePanel.add(new JScrollPane(profileText), BorderLayout.CENTER);
tabbedPane.addTab("Profile", profilePanel);
JOptionPane.showMessageDialog(mainFrame, tabbedPane,
"User Profile - " + user.name, JOptionPane.PLAIN_MESSAGE);
}
if (currentUser.medicalHistory.isEmpty()) {
historyText.append("No medical history available.");
} else {
historyText.append("Your Medical History:\n\n");
for (String[] history : currentUser.medicalHistory) {
historyText.append("• " + history[0] + "\n");
historyText.append(" Occurred: " + history[1] + "\n\n");
}
}
if (result == JOptionPane.OK_OPTION) {
String newUsername = newUsernameField.getText().trim();
String confirmUsername = confirmUsernameField.getText().trim();
if (!newUsername.equals(confirmUsername)) {
JOptionPane.showMessageDialog(mainFrame, "Usernames don't match!",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}
if (newUsername.isEmpty()) {
JOptionPane.showMessageDialog(mainFrame, "Username cannot be
empty!", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
currentUser.username = newUsername;
updateDatabaseFile();
JOptionPane.showMessageDialog(mainFrame, "Username updated
successfully!");
}
}
if (result == JOptionPane.OK_OPTION) {
String newPassword = new String(newPasswordField.getPassword()).trim();
String confirmPassword = new
String(confirmPasswordField.getPassword()).trim();
if (!newPassword.equals(confirmPassword)) {
JOptionPane.showMessageDialog(mainFrame, "Passwords don't match!",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}
if (newPassword.length() != 12) {
JOptionPane.showMessageDialog(mainFrame, "Password must be 12
digits!", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
currentUser.password = newPassword;
updateDatabaseFile();
JOptionPane.showMessageDialog(mainFrame, "Password updated
successfully!");
}
}
@Override
public String toString() {
return name + " (" + username + ")";
}
return user;
} catch (Exception e) {
return null;
}
}