Sunday 20 August 2017

pateint.java java examples

package ACP.Patient;

import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.io.*;

class Patient implements Serializable
{
    private String patientName;
    private String fatherName;
    private String diseaseCategory;
    private String patientCNIC;
    private String doctorName;
    private String prescription;
    private String diseaseHistory;
    private Date   dateOfBirth;
    private int    patientID;
   
    private static int nextID = 0;
//////////-----------------------------Constructor-----------------------------//////////
    Patient()
    {
        this.setPatientInfo();
    }
//////////-----------------------Method setPatientInfo------------------------//////////
    void setPatientInfo()
    {
        String[] diseaseCategories = {"", "Heart", "ENT", "Diabetic", "Brain", "Orthopedic", "General"};
        String[] doctors       = {"", "Dr. Faridullah Khan(Cardiologist)", "Dr. Muhammad Tariq(ENT Specialist)", "Dr. Muhammad Faisal(Diabetes Specialist)", "Dr. Ejaz Ahmed(Neurologist)", "Dr. Ali Akhtar(Orthopedic Surgeon)", "Dr. Rauf Niazi(General Surgeon)"};
        Date currentDate        = new Date();
        DateFormat df           = new SimpleDateFormat("dd/MM/yyyy");
        String tempPrescription       = null;
              //////////////////////////////////////////////////////////////////////////
        this.patientName = JOptionPane.showInputDialog("Enter Patient's Name:");
        while(this.patientName.length()== 0)
            this.patientName = JOptionPane.showInputDialog("Field Cannot Be Empty\nEnter Patient's Name:");
              //////////////////////////////////////////////////////////////////////////
        this.fatherName = JOptionPane.showInputDialog("Enter Father's Name:");
        while(this.fatherName.length() == 0)
            this.fatherName = JOptionPane.showInputDialog("Field Cannot Be Empty\nEnter Father's Name:");
              //////////////////////////////////////////////////////////////////////////
        String dOB = JOptionPane.showInputDialog(null, "Enter Patient's Date of Birth:", "dd/MM/yyyy");
        while(true)
        {
            try
            {
                this.dateOfBirth = df.parse(dOB);
                break;
            }
            catch (ParseException e)
            {
                dOB = JOptionPane.showInputDialog(null, "Invalid Input. Enter Again\n\nEnter Patient's Date of Birth:", "dd/MM/yyyy");
            }
        }
              //////////////////////////////////////////////////////////////////////////
        this.patientCNIC = JOptionPane.showInputDialog(null, "Enter Patient's CNIC:", "xxxxx-xxxxxxx-x");
        while(this.patientCNIC.length() == 0 || this.patientCNIC.equals("xxxxx-xxxxxxx-x"))
            this.patientCNIC = JOptionPane.showInputDialog("Field Cannot Be Empty\nEnter Patient's CNIC:", "xxxxx-xxxxxxx-x");
              //////////////////////////////////////////////////////////////////////////
        this.diseaseCategory = (String) JOptionPane.showInputDialog(null, "Select Patient's Disease Category:", null, JOptionPane.QUESTION_MESSAGE, null, diseaseCategories, diseaseCategories[0]);
        while(this.diseaseCategory.equals(diseaseCategories[0]))
            this.diseaseCategory = (String) JOptionPane.showInputDialog(null, "Field Cannot Be Empty\nSelect Patient's Disease Category:", null, JOptionPane.ERROR_MESSAGE, null, diseaseCategories, diseaseCategories[0]);
              //////////////////////////////////////////////////////////////////////////
        this.doctorName = (String) JOptionPane.showInputDialog(null, "Select Doctor's Name:", null, JOptionPane.QUESTION_MESSAGE, null, doctors, doctors[0]);
        while(this.doctorName.equals(doctors[0]))
            this.doctorName    = (String) JOptionPane.showInputDialog(null, "Field Cannot Be Empty\nSelect Doctor's Name:", null, JOptionPane.ERROR_MESSAGE, null, doctors, doctors[0]);
          //////////////////////////////////////////////////////////////////////////
        tempPrescription = JOptionPane.showInputDialog("Enter Prescription:");
        while(tempPrescription.length() == 0)
            tempPrescription = JOptionPane.showInputDialog("Field Cannot Be Empty\nEnter Prescription:");
        this.prescription   = df.format(currentDate) + "\t" + tempPrescription;
              //////////////////////////////////////////////////////////////////////////
        this.diseaseHistory = df.format(currentDate) + "\t" + this.doctorName + "\t" + tempPrescription;
              //////////////////////////////////////////////////////////////////////////
        if(nextID != 0)
            this.patientID = nextID++;
        else
        {
            nextID         = 9001;
            this.patientID = nextID++;
        }
              //////////////////////////////////////////////////////////////////////////
        JOptionPane.showMessageDialog(null, new JTextArea("Patient ID:\t" + this.patientID + "\nName:\t" + this.patientName + "\nFather Name:\t" + this.fatherName + "\nDate of Birth:\t" + df.format(this.dateOfBirth) + "\nPatient CNIC:\t" + this.patientCNIC + "\nCategory:\t" + this.diseaseCategory + "\nDoctor Name:\t" + this.doctorName + "\nPrescription:\t" + this.prescription + "\nHistory:\t" + this.diseaseHistory), "Patient Added", JOptionPane.INFORMATION_MESSAGE);
    }
//////////----------------------Method updatePatientInfo----------------------//////////
    void updatePatientInfo(Patient[] p, int arraySearchSize)
    {
        String[] doctors = {"", "Dr. Faridullah Khan(Cardiologist)", "Dr. Muhammad Tariq(ENT Specialist)", "Dr. Muhammad Faisal(Diabetes Specialist)", "Dr. Ejaz Ahmed(Neurologist)", "Dr. Ali Akhtar(Orthopedic Surgeon)", "Dr. Rauf Niazi(General Surgeon)"};
        String input     = JOptionPane.showInputDialog("Enter Patient's ID(a number) Whose Information You Want to Update:");
        while(input.length() == 0)
            input    = JOptionPane.showInputDialog("Field Cannot Be Empty\nEnter Patient's ID(a number) Whose Information You Want to Update:");
       
        int iDtoSearch;
        while(true)
        {
            try
            {
                iDtoSearch = Integer.parseInt(input);
                break;
            }
            catch (NumberFormatException nFE)
            {
                input = JOptionPane.showInputDialog("Please Enter a Number\nEnter Patient's ID(a number) Whose Information You Want to Update:");
            }
        }
       
        int foundAtIndex = searchIDs_Index(p, arraySearchSize, iDtoSearch);
        if(foundAtIndex == -1)
            JOptionPane.showMessageDialog(null, "No Patient Found with ID '" + iDtoSearch + "'", "Invalid ID", JOptionPane.ERROR_MESSAGE);
        else
        {
            input = (String) JOptionPane.showInputDialog(null, "Select Doctor's Name:", null, JOptionPane.QUESTION_MESSAGE, null, doctors, doctors[0]);
            while(input.length() == 0)
                input = (String) JOptionPane.showInputDialog(null, "Field Cannot Be Empty\nSelect Doctor's Name:", null, JOptionPane.QUESTION_MESSAGE, null, doctors, doctors[0]);
            p[foundAtIndex].doctorName = input;
            showPatientInfo(p, foundAtIndex);
        }
    }
//////////--------------Method updatePrescriptionDiseaseHistory--------------//////////
    void updatePrescriptionDiseaseHistory(Patient[] p, int arraySearchSize)
    {
        Date currentDate = new Date();
        DateFormat df    = new SimpleDateFormat("dd/MM/yyyy");
        String input     = JOptionPane.showInputDialog("Enter Patient's ID(a number) Whose Prescription You Want to Update:");
        while(input.length() == 0)
            input    = JOptionPane.showInputDialog("Field Cannot Be Empty\nEnter Patient's ID(a number) Whose Prescription You Want to Update:");
       
        int iDtoSearch;
        while(true)
        {
            try
            {
                iDtoSearch = Integer.parseInt(input);
                break;
            }
            catch (NumberFormatException nFE)
            {
                input = JOptionPane.showInputDialog("Please Enter a Number\nEnter Patient's ID(a number) Whose Prescription You Want to Update:");
            }
        }
        int foundAtIndex = searchIDs_Index(p, arraySearchSize, iDtoSearch);
        String newPrescription;
        if(foundAtIndex == -1)
            JOptionPane.showMessageDialog(null, "No Patient Found with ID '" + iDtoSearch + "'", "Invalid ID", JOptionPane.ERROR_MESSAGE);
        else
        {
            newPrescription = JOptionPane.showInputDialog("Enter New Prescription:");
            while(newPrescription.length() == 0)
                newPrescription = (String) JOptionPane.showInputDialog("Field Cannot Be Empty\nEnter New Prescription:");
            p[foundAtIndex].prescription   = p[foundAtIndex].prescription + "\n\t" + df.format(currentDate) + "\t" + newPrescription;
            p[foundAtIndex].diseaseHistory = p[foundAtIndex].diseaseHistory + "\n\t" + df.format(currentDate) + "\t" + p[foundAtIndex].doctorName + "\t" + newPrescription;
             showPatientInfo(p, foundAtIndex);
        }
    }
//////////---------------------Method deletePatientInfo----------------------//////////
    int deletePatientInfo(Patient[] p, int arraySearchSize)
    {
        String input  = JOptionPane.showInputDialog("Enter Patient's ID(a number) Whose Record You Want to Delete:");
        while(input.length() == 0)
            input = JOptionPane.showInputDialog("Field Cannot Be Empty\nEnter Patient's ID(a number) Whose Record You Want to Delete:");
       
        int iDtoSearch;
        while(true)
        {
            try
            {
                iDtoSearch = Integer.parseInt(input);
                break;
            }
            catch (NumberFormatException nFE)
            {
                input = JOptionPane.showInputDialog("Please Enter a Number\nEnter Patient's ID(a number) Whose Record You Want to Delete:");
            }
        }
        int foundAtIndex = searchIDs_Index(p, arraySearchSize, iDtoSearch);
        if(foundAtIndex == -1)
            JOptionPane.showMessageDialog(null, "No Patient Found with ID '" + iDtoSearch + "'", "Invalid ID", JOptionPane.ERROR_MESSAGE);
        else
            for(int j = foundAtIndex; j < arraySearchSize; j++)
            {
                if(j == arraySearchSize - 1)
                {
                    JOptionPane.showMessageDialog(null, "Record of Patient ID " + iDtoSearch + " is Deleted", "Record Deleted", JOptionPane.INFORMATION_MESSAGE);
                    p[j] = null;
                    return (--arraySearchSize);
                }
                else
                    p[j] = p[j+1];
            }
        return arraySearchSize;
    }
//////////---------------------Method searchView_patientID--------------------//////////
    void searchView_patientID(Patient[] p, int arraySearchSize)
    {
        String input  = JOptionPane.showInputDialog("Enter Patient's ID(a number) Whose Record You Want to Search:");
        while(input.length() == 0)
            input = JOptionPane.showInputDialog("Field Cannot Be Empty\nEnter Patient's ID(a number) Whose Record You Want to Search:");
       
        int iDtoSearch;
        while(true)
        {
            try
            {
                iDtoSearch = Integer.parseInt(input);
                break;
            }
            catch (NumberFormatException nFE)
            {
                input = JOptionPane.showInputDialog("Please Enter a Number\nEnter Patient's ID(a number) Whose Record You Want to Search:");
            }
        }
        int foundAtIndex = searchIDs_Index(p, arraySearchSize, iDtoSearch);
        if(foundAtIndex == -1)
            JOptionPane.showMessageDialog(null, "No Patient Found with ID '" + iDtoSearch + "'", "Invalid ID", JOptionPane.ERROR_MESSAGE);
        else
            showPatientInfo(p, foundAtIndex);
    }
//////////--------------------Method searchView_patientName------------------//////////
    void searchView_patientName(Patient[] p, int arraySearchSize)
    {
        DateFormat df  = new SimpleDateFormat("dd/MM/yyyy");
        int totalFound = 0;
        String name    = JOptionPane.showInputDialog("Enter Patient's Name Whose Record You Want to Search:");
        while(name.length() == 0)
            name   = JOptionPane.showInputDialog("Field Cannot Be Empty\nEnter Patient's Name Whose Record You Want to Search:");
        for(int i = 0; i <= arraySearchSize; i++)
        {
            if(i == arraySearchSize && totalFound == 0)
                JOptionPane.showMessageDialog(null, "No Patient Found with Name '" + name + "'", "Record Not Found", JOptionPane.ERROR_MESSAGE);
            else if(i == arraySearchSize)
                break;
            else if(p[i].patientName.equals(name))
            {
                showPatientInfo(p, i);
                totalFound++;
            }
        }
    }
//////////-------------------Method searchView_patientAge--------------------//////////
    void searchView_patientAge(Patient[] p, int arraySearchSize)
    {
        Date currentDate = new Date();
        int totalFound   = 0;
        String input     = JOptionPane.showInputDialog("Enter Patient's Age(a number) Whose Record You Want to Search:");
        while(input.length() == 0)
            input    = JOptionPane.showInputDialog("Field Cannot Be Empty\nEnter Patient's Age(a number) Whose Record You Want to Search:");
       
        int AgetoSearch;
        while(true)
        {
            try
            {
                AgetoSearch = Integer.parseInt(input);
                break;
            }
            catch (NumberFormatException nFE)
            {
                input = JOptionPane.showInputDialog("Please Enter a Number\nEnter Patient's Age(a number) Whose Record You Want to Search:");
            }
        }

        int age;
        int curMonth;
        int birthMonth;
        int curDate;
        int birthDate;

        for(int i = 0; i <= arraySearchSize; i++)
        {
            if(i == arraySearchSize && totalFound == 0)
                JOptionPane.showMessageDialog(null, "No Patient Found with Age '" + AgetoSearch + "'", "Record Not Found", JOptionPane.ERROR_MESSAGE);
            else if(i == arraySearchSize)
                break;
            else
            {
                age       = currentDate.getYear() - p[i].dateOfBirth.getYear();
               
                curMonth   = currentDate.getMonth();
                birthMonth = p[i].dateOfBirth.getMonth();
                curDate       = currentDate.getDate();
                birthDate  = p[i].dateOfBirth.getDate();

                if(curMonth < birthMonth || (curMonth == birthMonth && curDate < birthDate))
                    age--;

                if(AgetoSearch == age)
                {
                    showPatientInfo(p, i);
                    totalFound++;
                }
            }
        }
    }
//////////----------------Method searchView_diseaseCategory------------------//////////
    void searchView_diseaseCategory(Patient[] p, int arraySearchSize)
    {
        String[] diseaseCategories = {"", "Heart", "ENT", "Diabetic", "Brain", "Orthopedic", "General"};
        int totalFound               = 0;
        String categoryToSearch    = (String) JOptionPane.showInputDialog(null, "Select Patient's Disease Category Whose Record You Want to Search:", null, JOptionPane.QUESTION_MESSAGE, null, diseaseCategories, diseaseCategories[0]);
        while(categoryToSearch.equals(diseaseCategories[0]))
            categoryToSearch   = (String) JOptionPane.showInputDialog(null, "Field Cannot Be Empty\nSelect Patient's Disease Category Whose Record You Want to Search:", null, JOptionPane.ERROR_MESSAGE, null, diseaseCategories, diseaseCategories[0]);
       
        for(int i = 0; i <= arraySearchSize; i++)
        {
            if(i == arraySearchSize && totalFound == 0)
                JOptionPane.showMessageDialog(null, "No Patient Found in '" + categoryToSearch + "' Category", "Record Not Found", JOptionPane.ERROR_MESSAGE);
            else if(i == arraySearchSize)
                break;
            else if(p[i].diseaseCategory.equals(categoryToSearch))
            {
                showPatientInfo(p, i);
                totalFound++;           
            }
        }
    }
//////////-----------------Method searchView_doctorName---------------------//////////
    void searchView_doctorName(Patient[] p, int arraySearchSize)
    {
        String[] doctors = {"", "Dr. Faridullah Khan(Cardiologist)", "Dr. Muhammad Tariq(ENT Specialist)", "Dr. Muhammad Faisal(Diabetes Specialist)", "Dr. Ejaz Ahmed(Neurologist)", "Dr. Ali Akhtar(Orthopedic Surgeon)", "Dr. Rauf Niazi(General Surgeon)"};
        int totalFound   = 0;
        String docName     = (String) JOptionPane.showInputDialog(null, "Select Patient's Doctor Name Whose Record You Want to Search:", null, JOptionPane.QUESTION_MESSAGE, null, doctors, doctors[0]);
        while(docName.equals(doctors[0]))
            docName     = (String) JOptionPane.showInputDialog(null, "Field Cannot Be Empty\nSelect Patient's Doctor Name Whose Record You Want to Search:", null, JOptionPane.ERROR_MESSAGE, null, doctors, doctors[0]);
       
        for(int i = 0; i <= arraySearchSize; i++)
        {
            if(i == arraySearchSize && totalFound == 0)
                JOptionPane.showMessageDialog(null, "No Patient is Treated by '" + docName + "'", "Record Not Found", JOptionPane.ERROR_MESSAGE);
            else if(i == arraySearchSize)
                break;
            else if(p[i].doctorName.equals(docName))
            {
                showPatientInfo(p, i);
                totalFound++;           
            }
        }
    }
      //_____________________________________________________________
    private int searchIDs_Index(Patient[] p, int arraySearchSize, int iDtoSearch)
    {
        for(int i = 0; i <= arraySearchSize; i++)
        {
            if(i == arraySearchSize)
                return -1;
            else if(p[i].patientID == iDtoSearch)
                return i;
        }
        return -1;
    }
      //_____________________________________________________________
    private void showPatientInfo(Patient[] p, int indexToShow)
    {
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        JOptionPane.showMessageDialog(null, new JTextArea("Patient ID:\t" + p[indexToShow].patientID + "\nName:\t" + p[indexToShow].patientName + "\nFather Name:\t" + p[indexToShow].fatherName + "\nDate of Birth:\t" + df.format(p[indexToShow].dateOfBirth) + "\nPatient CNIC:\t" + p[indexToShow].patientCNIC + "\nCategory:\t" + p[indexToShow].diseaseCategory + "\nDoctor Name:\t" + p[indexToShow].doctorName + "\nPrescription:\t" + p[indexToShow].prescription + "\nHistory:\t" + p[indexToShow].diseaseHistory), "Patient Information", JOptionPane.INFORMATION_MESSAGE);
    }
      //_____________________________________________________________
    static void readNextID()
    {
        String iDFileName = "ACP\\NextID.txt";
        try
        {
            DataInputStream dIS = new DataInputStream(new FileInputStream(iDFileName));
            Patient.nextID        = (int) dIS.readInt();
            dIS.close();   
        }
        catch(FileNotFoundException fNFE){}
        catch(IOException iOE)
        {
            JOptionPane.showMessageDialog(null, "Error\n" + iOE, "I/O Exception in Reading NextId", JOptionPane.ERROR_MESSAGE);
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, "Error\n" + e, "Exception in Reading NextId", JOptionPane.ERROR_MESSAGE);
        }
    }
      //_____________________________________________________________
    static void writeNextID()
    {
        String iDFileName = "ACP\\NextID.txt";
        try
        {
            DataOutputStream dOS = new DataOutputStream(new FileOutputStream(iDFileName));
            dOS.writeInt(Patient.nextID);
            dOS.close();
        }
        catch(IOException iOE)
        {
            JOptionPane.showMessageDialog(null, "Error\n" + iOE, "I/O Exception in Writing NextId", JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, "Error\n" + e, "Exception in Writing NextID", JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        }   
    }
}

1 comment:

  1. Maybe Github would be the place to post your source code.

    If it was a decent post about linked lists that actually had plain text explaining what it's doing and why it would be of use.

    Currently it just looks like a bit of meaningless copy and paste from some homework sheet.

    There are not even comments in the code. Very unprofessional, very amature.

    ReplyDelete