package hospitalclient;

public class Person
{
    static int idnumber= 1500;
    private String name;
    private String telephoneno;
    private char gender;
    private String nationality;

    //No Parameter constructor
    public Person()
    {
        name="unassigned";
        telephoneno = "";
        gender = 'M';
        nationality = "Saudi";
    }

    public int getid()
    {
        return idnumber;
    }

    // Accessor Methods
    public String getname()
    {
        return name;
    }

    public String gettelephoneno()
    {
        return telephoneno;
    }

    public char getgender()
    {
        return gender;
    }

    public String getnationality()
    {
        return nationality;
    }


    //Mutator Methods
    public void setname(String Name)
    {
        this.name = Name;
    }

    public void settelephoneno(String telephoneno)
    {
        this.telephoneno = telephoneno;
    }

    public void setgender(char gender)
    {
       this.gender = gender; 
    }

    public void setnationality(String nationality)
    {
        this.nationality = nationality;
    }

    //Method to print Information
    public String tostring()
    {
        return "Name is" + this.name + "\nId number is" + getid() + 
               "\ntelephoneno is" + this.telephoneno + "\nGender is" + gender +
               "\nNationalty is " + this.nationality;

    }


}




/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hospitalclient;

import java.util.Date;

/**
 *
 * @author Home
 */
public class Patient extends Person
{
    private String illnesName;
    private Date conslationdate;
    private boolean appointment;
    private Doctor doctor;



    public Patient()
    {
        super();
        this.illnesName = "";
        this.appointment = false;

    }

    public Patient(Date conslationdate,Doctor doctor)
    {
        this.conslationdate = conslationdate;
        this.doctor = doctor;
    }

    /*public double getdoctorfees()
    {
        return doctor;
    }
    */

    public String getillnesname()
    {
        return illnesName;
    }

    public void setillnesname(String illnesname)
    {
        this.illnesName = illnesname;
    }

    /*public double consultantcharges()
    {

    }

    */

    public Date getconsultationdate()
    {
        return conslationdate;
    }

    public void setconsultationdate(Date date)
    {
        this.conslationdate = date;
    }

    public boolean isassigned()
    {
        return appointment = true;
    }

    public void setassigned(boolean appointment)
    {
        this.appointment = appointment;
    }

    @Override
    public String tostring()
    {
        return super.tostring();
    }
}






/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hospitalclient;

/**
 *
 * @author Home
 */
public class Doctor extends Person
{
   private String Specalization;
   private double consultationfees;
   private boolean assignedpatient;
   private Room room;


   public Doctor(Room r)
   {
       super();
       this.room = r;
   }

   public String getspecaliztion()
   {
       return Specalization;
   }

   public void setspecalization(String Specalization)
   {
       this.Specalization = Specalization;
   }

   public double getconsulationfees()
   {
       return consultationfees;
   }

   public void setconsulationfees(double consultationfees)
   {
       this.consultationfees = consultationfees;
   }

   public boolean isassigned()
   {
       return assignedpatient= false;
   }

   public void setassigned(boolean assignedpatient)
   {
       this.assignedpatient = assignedpatient;
   }

   @Override
   public String tostring()
   {
       return super.tostring() + "Specalization is" + this.Specalization 
                               + "consultationfees is" + this.consultationfees
                               + "assignedpatient IS" + this.assignedpatient;

   }


}




/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hospitalclient;
import java.util.Date;
import java.util.Scanner;

/**
 *
 * @author Home
 */
public class HospitalClient {

    public static void main(String[] args)
    {
        while(true)
        {
        int choice;
        System.out.println("--------------------------------------\n" +
                            "Welcome To Kau Hospital System\n" +
                           "--------------------------------------\n" +
                           "1. Register Doctor Details\n" +
                           "2. Register Patient Details\n" +
                           "3. Add Appointment\n" +
                           "4. Register Rooms\n" +
                           "5. Allocate Rooms to Doctors\n" +
                           "6. Bill Indivisual Paient Records\n" +  
                           "7. Know Doctor Consultation fees \n" +
                           "8. Know Doctor Availability \n" +      
                           "9. Exit from KAU Hospital Management System\n\n" +
                           "Enter Your Chocie");


        Scanner input = new Scanner(System.in);
        choice = input.nextInt();

        if(choice == 1)
          {
               for(int i = 0; i<10;i++)
               {
                   Room r= new Room();
                   Doctor dc = new Doctor(r);

                   System.out.print("Enter Doctor Name --> ");
                   String name = input.next();
                   dc.setname(name);

                   System.out.print("Enter DoctorTelephone No --> ");
                   String telephonenum = input.next();
                   dc.settelephoneno(telephonenum);

                   System.out.print("Enter DoctorGender --> ");
                   String Gender = input.next();
                   char g = Gender.charAt(0);
                   dc.setgender(g);

                   System.out.print("Enter Doctor Nationality --> ");
                   String nationalty = input.next();
                   dc.setnationality(nationalty);

                   System.out.print("Enter Doctor Specialization --> ");
                   String specalization = input.next();
                   dc.setspecalization(specalization);

                   System.out.print("Enter Doctor Consultation Fee –-> ");
                   double ConsultationFee = input.nextInt();
                   dc.setconsulationfees(ConsultationFee);

                   System.out.print("Do you want to register another Doctor in the System (y /n) -->");
                   String answer = input.next();
                   char letter = answer.charAt(0);

             if(letter == 'Y' || letter == 'y')
               {
                   i=0;
               }

             else
               {
                   i=10;
               }
          }

          }

         if(choice == 2)
          {
             for(int i = 0; i<10;i++)
               {
                   Room r = new Room();
                   Doctor dc = new Doctor(r);
                   Date date = new Date();
                   Patient patient = new Patient(date,dc);

                   System.out.print("Enter Patient Name -->");
                   String name = input.next();
                   patient.setname(name);


                   System.out.print("Enter Patient Telephone no -->");
                   String Telephoneno = input.next();
                   patient.settelephoneno(Telephoneno);

                   System.out.print("Enter Patient Gender -->");
                   String Gender = input.next();
                   char g = Gender.charAt(0);
                   patient.setgender(g);

                   System.out.print("Enter Patient Nationality --> ");
                   String nationalty = input.next();
                   patient.setnationality(nationalty);

                   System.out.print("Enter Patient illnes --> ");
                   String illnes = input.next();
                   patient.setillnesname(illnes);

                   System.out.print("Patient Consultation date -->");
                   patient.setconsultationdate(date);
                   System.out.println(patient.getconsultationdate());

                   System.out.print("\nDo you want to register another Patient in the System (y /n) -->");
                   String answer = input.next();
                   char letter = answer.charAt(0);

               if(letter == 'Y' || letter == 'y')
                  {
                        i=0;
                       System.out.println(patient.getname());
                  }
               else
                  {
                      i=10;
                  }
             }
        }

        if(choice == 3)
          {
             Patient patient;
                patient = new Patient();
             System.out.println("To give appointment, please enter index of the patient from the list ");
             System.out.println("--------------Registered Patient List -----------------");
             System.out.println("Patient --> " + patient.getname() + " index");
          }

        if(choice == 4)
          {
              for(int i = 0; i<10;i++)
               {
            Room r = new Room();
            System.out.print("Enter Room Number -- > ");
            int room = input.nextInt();
            r.setroomno(room);
            System.out.print("Enter Floor --> ");
            String floor = input.next();
            r.setfloorname(floor);
            System.out.print("\nDo you want to register another Patient in the System (y /n) -->");
            String answer = input.next();
            char letter = answer.charAt(0);

               if(letter == 'Y' || letter == 'y')
                  {
                        i=0;

                  }
               else
                  {
                      i=10;
                  }

             }
          }   

        if(choice == 5)
          {

          }

        if(choice == 6)
          {

          }

        if(choice == 7)
          {
                Room r = new Room();
                Doctor dc = new Doctor(r);
                System.out.println("Doctor Name --> " + dc.getname() + "\nNationality --> "  + dc.getnationality() + 
                                   "\nSpecialization --> " +  dc.getspecaliztion() + "\nConsultation Fee –-> " + dc.getconsulationfees());

          }

        if(choice == 8)
          {

          }

        if(choice == 9)
          {
              System.exit(0);
          }

        }
    }
}

my problem in main when i enter choice 2 and enter name and other information i save them in setname method
in choice 3 when i call getname method it says unassigned which is the default value but it's supposed to say the name that i entered can you tell me how to fix this

Recommended Answers

All 2 Replies

Try doing some debugging by adding some println statements that show the values of the variables as they are changed.

Make sure that there is only one object being used. For example are you calling the set method for one object and calling the get method with another object.

The scope of a name is only within the immediate surrounding {} brackets. Eg the Patient variable declared on line 335 has its scope from 331 to 376. As soon as you go outside that scope the variable is discarded.
So although you create a patient on lines 335 onwards that patient is discarded before you go round the loop again. Simiularly on 380/381 - you create new patient there and discard it soon after.
You need a patient variable (or array?) declared where all the parts of your code can access it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.