Sunday 20 August 2017

client.java examples of java

package ACP.Patient;

import javax.swing.JOptionPane;
import java.io.*;
//////////////////////////////////////////////////////
class Client
{
    public static void main(String[] fileName)
    {
        final int ARRAY_SIZE    = 50;
        Patient []patient    = new Patient[ARRAY_SIZE];
        int numPatients        = 0;
        String[] choices    = {"", "Add New Patient Record", "Update Patient Information", "Update Prescription & Disease History", "Delete Patient Record", "Search & View Patient Record By Patient ID", "Search & View Patient Record By Patient Name", "Search & View Patient Record By Patient Age", "Search & View Patient Record By Disease category", "Search & View Patient Record By Doctor Name", "Exit"};
        String[] Continue    = {"", "Yes", "No"};
        ObjectOutputStream oOS    = null;
        ObjectInputStream  oIS    = null;
    //////////////////////////////////////////////////////
        String choice        = null;
        String cont        = null;
        int i            = 0;                   
    /////////////////////////////////////////////////////
        if(fileName.length == 0)
        {
            JOptionPane.showMessageDialog(null, "File Name Not Sent From Command Line\nExecute Again With File Name", "Error", JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        }
    ////////////////////////////////////////////////////
        try
        {
            Patient temp = null;
            oIS         = new ObjectInputStream(new FileInputStream(fileName[0]));
            for(i = 0; i < ARRAY_SIZE; i++)
            {
                temp = (Patient) oIS.readObject();
                if(temp != null)
                {
                    patient[i] = temp;
                    numPatients++;
                }
            }
            oIS.close();
        }
        catch(ClassNotFoundException cNFE)
        {
            JOptionPane.showMessageDialog(null, "Error\n" + cNFE, "Class Not Found Exception in Reading Objects", JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        }
        catch(FileNotFoundException fNFE){}
        catch(EOFException eOFE){}
        catch(IOException iOE)
        {
            JOptionPane.showMessageDialog(null, "Error\n" + iOE, "I/O Exception in Reading Objects", JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, "Error\n" + e, "Exception in Reading Objects", JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        }

        Patient.readNextID();
    ///////////////////////////////////////////////////
        do
        {
            choice = (String) JOptionPane.showInputDialog(null, "What You Want To Do ?", "Main Menu", JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);
            while(choice.equals(choices[0]))
                choice = (String) JOptionPane.showInputDialog(null, "Field Cannot Be Empty\nWhat You Want To Do ?", "Main Menu", JOptionPane.ERROR_MESSAGE, null, choices, choices[0]);
       
            if(choice.equals(choices[1]))
            {
                if(numPatients < 0 || numPatients >= ARRAY_SIZE)
                    JOptionPane.showMessageDialog(null, "No Space for New patients :(", "Database Full", JOptionPane.WARNING_MESSAGE);
                else
                {
                    patient[numPatients] = new Patient();
                    numPatients++;
                }               
            }
            else if(choice.equals(choices[2]))
            {
                if(numPatients <= 0)
                    JOptionPane.showMessageDialog(null, "No Records Available", "Database Empty", JOptionPane.WARNING_MESSAGE);
                else
                    patient[0].updatePatientInfo(patient, numPatients);
            }   
            else if(choice.equals(choices[3]))
            {
                if(numPatients <= 0)
                    JOptionPane.showMessageDialog(null, "No Records Available", "Database Empty", JOptionPane.WARNING_MESSAGE);
                else
                    patient[0].updatePrescriptionDiseaseHistory(patient, numPatients);
            }
            else if(choice.equals(choices[4]))
            {
                if(numPatients <= 0)
                    JOptionPane.showMessageDialog(null, "No Records Available", "Database Empty", JOptionPane.WARNING_MESSAGE);
                else
                    numPatients = patient[0].deletePatientInfo(patient, numPatients);
            }
            else if(choice.equals(choices[5]))
            {
                if(numPatients <= 0)
                    JOptionPane.showMessageDialog(null, "No Records Available", "Database Empty", JOptionPane.WARNING_MESSAGE);
                else
                    patient[0].searchView_patientID(patient, numPatients);
            }
            else if(choice.equals(choices[6]))
            {
                if(numPatients <= 0)
                    JOptionPane.showMessageDialog(null, "No Records Available", "Database Empty", JOptionPane.WARNING_MESSAGE);
                else
                    patient[0].searchView_patientName(patient, numPatients);
            }
            else if(choice.equals(choices[7]))
            {
                if(numPatients <= 0)
                    JOptionPane.showMessageDialog(null, "No Records Available", "Database Empty", JOptionPane.WARNING_MESSAGE);
                else
                    patient[0].searchView_patientAge(patient, numPatients);
            }
            else if(choice.equals(choices[8]))
            {
                if(numPatients <= 0)
                    JOptionPane.showMessageDialog(null, "No Records Available", "Database Empty", JOptionPane.WARNING_MESSAGE);
                else
                    patient[0].searchView_diseaseCategory(patient, numPatients);
            }
            else if(choice.equals(choices[9]))
            {
                if(numPatients <= 0)
                    JOptionPane.showMessageDialog(null, "No Records Available", "Database Empty", JOptionPane.WARNING_MESSAGE);
                else
                    patient[0].searchView_doctorName(patient, numPatients);
            }
            else if(choice.equals(choices[10]))
            {
                JOptionPane.showMessageDialog(null, "Good Bye", "Exiting", JOptionPane.PLAIN_MESSAGE);
                               
                if(numPatients > 0)
                {
                    try                       
                    {
                        oOS = new ObjectOutputStream(new FileOutputStream(fileName[0]));
                        for(i = 0; i < ARRAY_SIZE; i++)
                            oOS.writeObject(patient[i]);
                        oOS.close();
                    }
                    catch(IOException iOE)
                    {
                        JOptionPane.showMessageDialog(null, "Error\n" + iOE, "I/O Exception in Writing Objects", JOptionPane.ERROR_MESSAGE);
                    }
                    catch(Exception e)
                    {
                        JOptionPane.showMessageDialog(null, "Error\n" + e, "Exception in Writing Objects", JOptionPane.ERROR_MESSAGE);
                    }

                    Patient.writeNextID();
                }
                System.exit(0);
            }
            cont = (String) JOptionPane.showInputDialog(null, "Want to go back to Main Menu ?", "Continue or Exit", JOptionPane.QUESTION_MESSAGE, null, Continue, Continue[0]);
            while(cont.equals(Continue[0]))
                cont = (String) JOptionPane.showInputDialog(null, "Field Cannot Be Empty\nWant to go back to Main Menu ?", "Continue or Exit", JOptionPane.ERROR_MESSAGE, null, Continue, Continue[0]);
        }
        while(cont.equals(Continue[1]));
    ///////////////////////////////////////////////////
        JOptionPane.showMessageDialog(null, "Good Bye", "Exiting", JOptionPane.PLAIN_MESSAGE);
       
        if(numPatients > 0)
        {
            try                       
            {
                oOS = new ObjectOutputStream(new FileOutputStream(fileName[0]));
                for(i = 0; i < ARRAY_SIZE; i++)
                    oOS.writeObject(patient[i]);
                oOS.close();
            }
            catch(IOException iOE)
            {
                JOptionPane.showMessageDialog(null, "Error\n" + iOE, "I/O Exception in Writing Objects", JOptionPane.ERROR_MESSAGE);
            }
            catch(Exception e)
            {
                JOptionPane.showMessageDialog(null, "Error\n" + e, "Exception in Writing Objects", JOptionPane.ERROR_MESSAGE);
            }
        }

        Patient.writeNextID();
    ///////////////////////////////////////////////////
    }
}

0 comments: