Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 412
  • Last Modified:

Further question about allocting patient to doctor, ( CEHJ, i can post a ghost problem if you would like to solve the point dispute)


i have a further question..
Is there a way I can bring up a list of already entered doctors, when entering a doctors name while creating a new patient.
Or only be allowed to enter the name of a doctor which has been entered.

ps if you would like me to make up a ghost question for your points i will be happy to do so

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

import corejava.Console;

public class Patient
{
//attributes

private int ID;
private static int nextID = 1000;
private String name;
private String address;
private String Postcode;
private String Telephone;
private String DateOfBirth;
private String Allergies;
private String doc;


//connecting code to main method
public Patient(String doc, String n, String a, String p, String t, String d, String al){

    this.doc = doc;
    name = n;
    address = a;
    Postcode = p;
    Telephone = t;
    DateOfBirth = d;
    Allergies = al;
    ID = nextID;
    nextID++;
}

public void setdoc(String doc){
    this.doc = doc;
}

public String getName()
{
    return name;
}


public void printDetails(){
//prints out what the patient has entered

    System.out.println("\ndoc >>         " + doc);
    System.out.println("\nID >>          " + ID);
    System.out.println("\nName >>        " + name);
    System.out.println("\nAddress >>     " + address);
    System.out.println("\nPostcode >>    " + Postcode);
    System.out.println("\nTelephone >>   " + Telephone);
    System.out.println("\nDateOfBirth >> " + DateOfBirth);
    System.out.println("\nAllergies >>   " + Allergies);
   }
    public static int getNextID() {
        return nextID;



}
}
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////


import corejava.*;
import corejava.Console;

public class mainSurgery
{

storePatients sP;
private Patient p;

storeDoctors sD;
private doctor d;

public mainSurgery()
{
    sP = new storePatients(50);


    sD = new storeDoctors(10);

}

    public int showmenu()
    {

           int choice;
           System.out.println("");
           System.out.println("\t *********************  ");
           System.out.println("\t    FixFast Surgery  ");
           System.out.println("\t *********************  ");
           System.out.print("\n[1]Register a new patient");
           System.out.print("\n[2]Register a new Doctor");
           System.out.print("\n[3]Print all patients data");
           System.out.print("\n[4]Find a patient's details");
           System.out.print("\n[5]Remove a patient's details");
           System.out.print("\n[6]Print Doctors details");
           System.out.print("\n[7]Exit the program");
           System.out.println("");
         choice = Console.readInt("\nPlease select one of the above.");
              return choice;
}

public void addPatient()
{

    String name = Console.readString (" Enter your name ");
    String address = Console.readString (" Your full address ");
    String Postcode = Console.readString (" Correct postcode ");
    String Telephone = Console.readString (" Existing telephone number ");
    String DateOfBirth = Console.readString (" Your date of Birth ");
    String Allergies = Console.readString (" Do you suffer from allergies? ");
    String doc = Console.readString (" Please enter your doctor's name");


    Patient p = new Patient( doc, name, address, Postcode, Telephone, DateOfBirth, Allergies);
        System.out.println(" You have been successfully registered");
    sP.addPatient(p);

}

public void addDoctor()
{

    //int ID = Console.readInt (" Please enter your doctor ID  ");
    String name = Console.readString (" Enter your name oh wise one ");
    String address = Console.readString (" Your address ");
    String Postcode = Console.readString (" Your full Postcode ");
    String homeTelephone = Console.readString (" Home telephone No");
    String maximumPatients = Console.readString (" Maximum designated patients ");
    String currentPatients = Console.readString (" Current patients seeing ");


    doctor d = new doctor( name, address, Postcode, homeTelephone, maximumPatients,
    currentPatients);
    sD.addDoctor(d);

    }

public void printDetails()
{
    sP.printDetails();
}

public void printDoctors()
{
    sD.printDoctors();

}

    public void removePatient()
    {
            String name = Console.readString("Name of Patient? ");

            boolean success = sP.removePatient(name);
            if (success)
            {
                System.out.println("Patient deleted");
            }
            else
                System.out.println("Patient not found");
    }

   public void findPatient()
    {
            String name = Console.readString("What name? ");

            Patient p = sP.getPatient(name);
            if (p != null)
            {
                System.out.println("Found");
                p.printDetails();
            }
            else
            System.out.println("Patient has not been found");

    }


    public void chooseOption()
    {
        int choice;
        choice = showmenu();
        while(choice  !=7)
        {
            switch(choice)
            {
                case 1:
                    addPatient();
                    break;
                case 2:
                    addDoctor();
                    break;
                case 3:
                    printDetails();
                    break;
                case 4:
                    findPatient();
                    break;
                case 5:
                    removePatient();
                    break;
                case 6:
                    printDoctors();
                    break;

                default:
                    System.out.println("Your entry has been processed and is invalid. Please try again");

            }
            choice = showmenu();
        }
    }



public static void main(String[] args)
{
        mainSurgery si = new mainSurgery();
    si.chooseOption();

    System.out.print("Thankyou, I hope you have enjoyed your time here at FixFast");

 }
}
0
pigmentarts
Asked:
pigmentarts
  • 64
  • 40
  • 31
  • +2
1 Solution
 
CEHJCommented:
The correct way to solve the previous problem is to get the question reopened. Just post a 0 point q to COmmunity Support
0
 
TimYatesCommented:
And remember this time that there is a "Split Points" link above the comment box, so you can split the points between more than one expert

http://www.experts-exchange.com/Programming/Programming_Languages/Java/help.jsp#hi69
0
 
CEHJCommented:
The correct way to implement the functionality you want is to have your storeDoctors class implement toString and call that method
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
pigmentartsAuthor Commented:
heh, well it wasnt my question so i don't know what the title was :P
0
 
CEHJCommented:
But i see you have a printDoctors method in that class. You need to call that function
0
 
pigmentartsAuthor Commented:

This is my storeDoctors class as it stands,  do you mean i need a new method in there?   What is implement toString..

public class storeDoctors

{

    private doctor theDoctors[];
    private int numDoctors;

    private int currentDoctors;
    private int maximumDoctors;



   public storeDoctors(int capacity)
   {
    theDoctors = new doctor[capacity];
    currentDoctors = 0;
    maximumDoctors = capacity;
}


    public boolean addDoctor(doctor sD)
{
    if (currentDoctors < maximumDoctors)
    {
        theDoctors[currentDoctors] = sD;
        currentDoctors ++;
        return true;
    }
    else
        return false;
}

  public void printDoctors()
    {
        for(int i = 0; i < currentDoctors ; i++)
        {
            theDoctors[i].printDoctors() ;
        }

    }

}
0
 
pigmentartsAuthor Commented:
ah yes, I was thinking along those lines but couldn't put it all together in my head.... where do i call the printDoctors()  method
0
 
CEHJCommented:
>>do you mean i need a new method in there?  

No you don't. See my last comment - i didn't see you had a printDoctors method before posting. You just need to call it
0
 
zzynxSoftware engineerCommented:
>> Or only be allowed to enter the name of a doctor which has been entered.
If it was an app with a GUI using a combobox containing the existing doctors would be the only good approach.
But it isn't, is it?

0
 
mmuruganandamCommented:
think of map solution....


map.put(doctorname, doctor); // doctor is an object.  doctorname is string and key for the map


when the doctor name is entered...

check

if (map.containsKey(doctorname))
{
  d = (doctor)map.get(doctorname);
}
else
{
      map.put(doctorname, doc);
}


Regards,
Muruga
0
 
zzynxSoftware engineerCommented:
>> where do i call the printDoctors()  method
Whenever you want the user to see the available doctors ;)
0
 
CEHJCommented:
Presumably as the first (or second) statement in addPatient

System.out.println("Add a patient - here are the current available doctors:");
sd.printDoctors(); // or whatever the storeDoctors variable is
0
 
mmuruganandamCommented:
>> printDoctors()
might help you only in displaying...

That won't help you in avoiding duplicate to the list.
0
 
zzynxSoftware engineerCommented:
Good idea of murugan.

in your storeDoctors class
replace
       private doctor theDoctors[];
by the map

Whenever adding a doctor, put it in the map. (key= doctor's name)
Whenever the user enters a doctor's name you can check - as he said - with map.contains()
0
 
CEHJCommented:
To cause the least disruption to your code, i'd suggest you put a containsDoctor method in your class storeDoctors
0
 
mmuruganandamCommented:
public class mainSurgery
{
  Map doctorsMap = new HashMap();

----
----
----

public boolean addDoctor(doctor sD)
{
    if (currentDoctors < maximumDoctors)
    {
        String doctorName = sD.getDoctorName();
       
        if (map.containsKey(doctorName) == fals)
        {
           map.put(doctorName, sD);
           currentDoctors ++;
           return true;
        }
    }
    else
        return false;
}
0
 
mmuruganandamCommented:
simple and elegant is the map strategy for a beginner.
0
 
CEHJCommented:
There is already a container for Doctors. Adding another simply confuses the code. Either

a. redesign it
b. use the less invasive solution i mentioned
0
 
mmuruganandamCommented:
doctor object does contain another doctor object...

I mean there is no self composition.  one of the doctor object you have to send all the other doctors and check for the same.

That would be code than this map contains approach.
0
 
zzynxSoftware engineerCommented:
It is also better because
- you have no limit to the number of doctors you can have.
- you don't have to specify a max # of doctors you can have

public class storeDoctors {

    private HashMap theDoctors;

   public storeDoctors(int capacity) {
      theDoctors = new HashMap(capacity);
   }

   public boolean addDoctor(doctor sD) {
     theDoctors.put(sD.getName(), sD);
   }

   public void printDoctors() {
    Iterator it = theDoctors.iterator();
    while (it.hasNext()) {
         System.out.println( ((doctor)it.next()).toString() );
    }
}

public boolean doctorAlreadyKnown(doctor sD) {
     return theDoctors.contains(sD.getName());
}

}
0
 
mmuruganandamCommented:
A correction...

public boolean addDoctor(doctor sD) {
    if (doctorAlreadyKnown(sD))
          theDoctors.put(sD.getName(), sD);
   }


0
 
pigmentartsAuthor Commented:
If it was an app with a GUI using a combobox containing the existing doctors would be the only good approach.
But it isn't, is it?

lol no, dont think it is
0
 
mmuruganandamCommented:
oops...

public boolean addDoctor(doctor sD) {
    if (doctorAlreadyKnown(sD) == false)
          theDoctors.put(sD.getName(), sD);
   }
0
 
zzynxSoftware engineerCommented:
>>  you don't have to specify a max # of doctors you can have

   public storeDoctors(int capacity) {
      theDoctors = new HashMap(capacity);
   }

should be

   public storeDoctors() {
      theDoctors = new HashMap();
   }

And then you see that storeDoctors is indeed just a HashMap
0
 
CEHJCommented:
>>It is also better because ...

This is true. If you go down the redesign route, this is a good approach. Make sure it doesn't break your code elsewhere.
0
 
mmuruganandamCommented:
All the changes he is going to make is only in "storeDoctors", so there won't be any problem and/or break-up.
0
 
zzynxSoftware engineerCommented:
>>public boolean addDoctor(doctor sD) {
>>    if (doctorAlreadyKnown(sD) == false)
>>          theDoctors.put(sD.getName(), sD);
>>   }

Is not really needed, since a map doesn't allow double by nature.
It just overwrites the entry with the new doctor.
But, indeed... maybe you don't want that...
0
 
mmuruganandamCommented:
pigmentarts,

Whenever you are satisfied with more than one answer, accept one answer and accept other valuable comments as assisted answer if you really think that is worth.

If there are two different ways and both solves the purpose you can go with split points...

0
 
mmuruganandamCommented:
That is right, he might have given a wrong values except the name.  So it is better to verify the existence and put it to the map.

0
 
pigmentartsAuthor Commented:
Hmmm ok, I'm a little confused.  

and yes simple is the way for me.  forgetting about duplicate doctors..

When entering a patients data...name -  sick patient

please choose your doctor from (alfred, spock, Hyde) -  alfred


could i have something like this, where i call up any doctors that i have entered into the array
0
 
CEHJCommented:
If you want to go down the route of retaining your original array, just put this method in storeDoctors:

public boolean containsDoctor(doctor d) {
      return Arrays.asList(doctors).contains(d);
}
0
 
CEHJCommented:
>>could i have something like this, where i call up any doctors that i have entered into the array

That answer i gave you way back - print them in addPatient
0
 
mmuruganandamCommented:
In the map, you can easily get the doctor


public doctor getDoctor(String doctorName)
{
   return (doctor)map.get(doctorName);
}


CEHJ,  the reference would be always different

>> Arrays.asList(doctors).contains(d);
This won't work until he writes his own hashCode and/or comparable implementation
0
 
pigmentartsAuthor Commented:
This looked quite interesting mmurugan....
I'm thinking I cant just swap this for the addDoctor method i have... :)


public boolean addDoctor(doctor sD)
{
    if (currentDoctors < maximumDoctors)
    {
        String doctorName = sD.getDoctorName();
       
        if (map.containsKey(doctorName) == fals)
        {
           map.put(doctorName, sD);
           currentDoctors ++;
           return true;
        }
    }
    else
        return false;
}
0
 
pigmentartsAuthor Commented:
I'm getting worried about this point splitting business now

CEHJ, you are saying i create a new method  

public boolean containsDoctor(doctor d) {
     return Arrays.asList(doctors).contains(d);
}

in storeDoctor

and just call it in main Surgery..where?
0
 
CEHJCommented:
>>
CEHJ,  the reference would be always different

>> Arrays.asList(doctors).contains(d);
This won't work until he writes his own hashCode and/or comparable implementation
>>

That must be done anyway, for ANY collection class implementation to work
0
 
CEHJCommented:
>>I'm getting worried about this point splitting business now

Don't get worried!

Just first of all can you clarify *exactly* what you want to achieve, as certain things

>>...  forgetting about duplicate doctors..

are a little hazy.


0
 
zzynxSoftware engineerCommented:
I see that in your addPatient() func you just have the doctor's name.
In that case you should have in storeDoctors:

public boolean isDoctorKnown(String name) {
     return theDoctors.contains(name);
}

Then - to answer your original Q - you could write

public void addPatient()
{

    String name = Console.readString (" Enter your name ");
    String address = Console.readString (" Your full address ");
    String Postcode = Console.readString (" Correct postcode ");
    String Telephone = Console.readString (" Existing telephone number ");
    String DateOfBirth = Console.readString (" Your date of Birth ");
    String Allergies = Console.readString (" Do you suffer from allergies? ");
    do {
      String doc = Console.readString (" Please enter your doctor's name");
      if ( !sD.isDoctorKnown(doc) )
        System.out.println(" That doctor is unknown.");
    } while ( !sD.isDoctorKnown(doc) );

    Patient p = new Patient( doc, name, address, Postcode, Telephone, DateOfBirth, Allergies);
        System.out.println(" You have been successfully registered");
    sP.addPatient(p);
}
0
 
mmuruganandamCommented:
I won't prefer that anyway, because of the compare complications...
0
 
mmuruganandamCommented:
method that i and zzynx given is perfectly achieves your requirement...
0
 
zzynxSoftware engineerCommented:
>> please choose your doctor from (alfred, spock, Hyde) -  alfred
>> could i have something like this, where i call up any doctors that i have entered into the array

I would stick to the map approach. (why cf. above)
Then
"please choose your doctor from (alfred, spock, Hyde) -  alfred"

is as simple as changing printDoctors() a little bit

public void printDoctors() {
    Iterator it = theDoctors.iterator();
    System.out.print("(");
    while (it.hasNext())
         System.out.print( ((doctor)it.next()).toString() + "," );
    System.out.print(")");
}
0
 
pigmentartsAuthor Commented:
I see that in your addPatient() func you just have the doctor's name.
In that case you should have in storeDoctors:


ok this looks good...just add a method to storeDoctor and add to my addPatient menu.

ok with your final approval i will give this a go.... PLese code!! don't break on me now!!!

Where do you guys do this from?  home or in a big building somewhere?
0
 
pigmentartsAuthor Commented:
I dont understand the map thing tho ! 8-()
0
 
zzynxSoftware engineerCommented:
>> method that i and zzynx given is perfectly achieves your requirement...
I agree (of course)  
;°)
0
 
CEHJCommented:
>>I won't prefer that anyway, because of the compare complications...

mmuruganandam, the point about correct behaviour in collections classes is a relatively advanced one that i was hoping to bring up later, but since you have already, i hope YOU are aware that the correct equals/hashCode implementations are necessary anyway *including Maps*. If you are aware - why are you bringing it up as a reason for not using the container classes that are already in the code?
0
 
pigmentartsAuthor Commented:
woah, dont stray from your 5:31AM post   my your up early...i'm all over here in uk
0
 
pigmentartsAuthor Commented:
is that why everyones a bit tetcy...go to bed!!
0
 
zzynxSoftware engineerCommented:
>> I dont understand the map thing tho ! 8-()

As murugan, said just replace your storeDoctors class (comment it out) with ours and see what happens...

>> ok with your final approval i will give this a go
Good.
Listening...
0
 
mmuruganandamCommented:
the map is going to have the doctorName as key.  So there is no need to worry about the contains part.

0
 
pigmentartsAuthor Commented:
public boolean isDoctorKnown(String name) {
     return theDoctors.contains(name);
}

error variable theDoctors not found,  i'll type it now to save time when i dont fix it
0
 
pigmentartsAuthor Commented:
ahh ok,,, should i have typed that in storeDoctors or storePatients
0
 
zzynxSoftware engineerCommented:
Yes, that's another function of your storeDoctors class
0
 
mmuruganandamCommented:
change your addDoctor method in storeDoctors...
0
 
pigmentartsAuthor Commented:
ahh lol i changed the patient one..ang on
0
 
pigmentartsAuthor Commented:
the one you gave me was for the patient, should i just change it for the doctor
0
 
pigmentartsAuthor Commented:

huh? I thought i was doing this

I see that in your addPatient() func you just have the doctor's name.
In that case you should have in storeDoctors:

public boolean isDoctorKnown(String name) {
     return theDoctors.contains(name);
}

Then - to answer your original Q - you could write

public void addPatient()
{

    String name = Console.readString (" Enter your name ");
    String address = Console.readString (" Your full address ");
    String Postcode = Console.readString (" Correct postcode ");
    String Telephone = Console.readString (" Existing telephone number ");
    String DateOfBirth = Console.readString (" Your date of Birth ");
    String Allergies = Console.readString (" Do you suffer from allergies? ");
    do {
      String doc = Console.readString (" Please enter your doctor's name");
      if ( !sD.isDoctorKnown(doc) )
        System.out.println(" That doctor is unknown.");
    } while ( !sD.isDoctorKnown(doc) );

    Patient p = new Patient( doc, name, address, Postcode, Telephone, DateOfBirth, Allergies);
        System.out.println(" You have been successfully registered");
    sP.addPatient(p);
}
 
0
 
CEHJCommented:
pigmentarts, just to clarify my position on this one:

1. i have given you the way/place  to print your doctors
2. i agree with redesigning your container of doctors if that's acceptable - i'm merely trying to stop spurious reasons being given for not doing it with the existing one. When you use collections classes with your own, your own should implement equals and hashCode
0
 
zzynxSoftware engineerCommented:
>> the one you gave me was for the patient,
those changes in addPatient()?
Yes.

Don't change anything for adding a doctor. That's OK. (if you use the new storeDoctors class)
0
 
mmuruganandamCommented:
>> i have given you the way/place  to print your doctors

he has to bring up the doctor in place when he enters the patient.  Just printing is not enough for this requirement.

even the array also now been suggested by you.  that is a new one within the code.  since map is going to take the key as doctor name there is no extra effort put in for compare.
0
 
pigmentartsAuthor Commented:
:D
0
 
pigmentartsAuthor Commented:
Sorry CEHJ i'm getting a little overwhelmed here, i'm understanding things then hearing stuff about hash and
0
 
pigmentartsAuthor Commented:
My 5.50 post,   following this i get errors

varibale doc not found and contains(java.lang.String) not found in class doctor.

Do these question ans sessions usually go on this long or am i special....
0
 
zzynxSoftware engineerCommented:
>> I'm getting a little overwhelmed here
Just code our suggestions at your ease and let us know your problems ;)
0
 
CEHJCommented:
>>Just printing is not enough for this requirement.

Do you imagine i'm not aware of that?

>>even the array also now been suggested by you...that is a new one within the code

!? So what is this then?:

>>private doctor theDoctors[];
0
 
pigmentartsAuthor Commented:

ok thx,

well this is what i've done  in main method

public void addPatient()
{

    String name = Console.readString (" Enter your name ");
    String address = Console.readString (" Your full address ");
    String Postcode = Console.readString (" Correct postcode ");
    String Telephone = Console.readString (" Existing telephone number ");
    String DateOfBirth = Console.readString (" Your date of Birth ");
    String Allergies = Console.readString (" Do you suffer from allergies? ");
    do {
      String doc = Console.readString (" Please enter your doctor's name");
      if ( !sD.isDoctorKnown(doc) )
        System.out.println(" That doctor is unknown.");
    } while ( !sD.isDoctorKnown(doc) );

    Patient p = new Patient( doc, name, address, Postcode, Telephone, DateOfBirth, Allergies);
        System.out.println(" You have been successfully registered");
    sP.addPatient(p);
}

and in my storeDoctor class i have

public class storeDoctors

{

    private doctor theDoctors[];
    private int numDoctors;

    private int currentDoctors;
    private int maximumDoctors;



   public storeDoctors(int capacity)
   {
    theDoctors = new doctor[capacity];
    currentDoctors = 0;
    maximumDoctors = capacity;
}

public boolean isDoctorKnown(String name) {
     return theDoctors.contains(name);
}

    public boolean addDoctor(doctor sD)
{
    if (currentDoctors < maximumDoctors)
0
 
mmuruganandamCommented:
No more fights CEHJ... totally tired...

pigmentarts,   Can you please put your code along with all the suggested solution and tell us the status....
0
 
zzynxSoftware engineerCommented:
>> Do these question ans sessions usually go on this long or am i special....
As long as needed. :)

>> contains(java.lang.String) not found in class doctor.
????

In your class storeDoctors you should add

public boolean isDoctorKnown(String name) {
     return theDoctors.contains(name);
}

>> varibale doc not found
???

do {
      String doc = Console.readString (" Please enter your doctor's name: "); //<<< here it is
      if ( !sD.isDoctorKnown(doc) )
        System.out.println(" That doctor is unknown.");
    } while ( !sD.isDoctorKnown(doc) );

Can you post your complete storeDoctors class you have + addPatient()?
0
 
mmuruganandamCommented:
my weekend starts late... ;)
0
 
zzynxSoftware engineerCommented:
That's not the storeDoctors class that I suggested!
Where's that HashMap?
???? 8-O
0
 
pigmentartsAuthor Commented:
public class storeDoctors

{

    private doctor theDoctors[];
    private int numDoctors;

    private int currentDoctors;
    private int maximumDoctors;



   public storeDoctors(int capacity)
   {
    theDoctors = new doctor[capacity];
    currentDoctors = 0;
    maximumDoctors = capacity;
}

public boolean isDoctorKnown(String name) {
     return theDoctors.contains(name);
}


    public boolean addDoctor(doctor sD)
{
    if (currentDoctors < maximumDoctors)
    {
        theDoctors[currentDoctors] = sD;
        currentDoctors ++;
        return true;
    }
    else
        return false;
}

  public void printDoctors()
    {
        for(int i = 0; i < currentDoctors ; i++)
        {
            theDoctors[i].printDoctors() ;
        }

    }

}

lolol  you crazy maaan, but i like you
0
 
pigmentartsAuthor Commented:
haha, i told you i didnt know about the hash map
0
 
pigmentartsAuthor Commented:
where does this go

{
      String doc = Console.readString (" Please enter your doctor's name: "); //<<< here it is
      if ( !sD.isDoctorKnown(doc) )
        System.out.println(" That doctor is unknown.");
    } while ( !sD.isDoctorKnown(doc) );
0
 
zzynxSoftware engineerCommented:
Try this

public class storeDoctors {

    private HashMap theDoctors;

   public storeDoctors() {
      theDoctors = new HashMap();
   }

   public boolean addDoctor(doctor sD) {
       theDoctors.put(sD.getName(), sD);  // <<< I suppose your doctor class has a getName() function?
   }

   public void printDoctors() {
    Iterator it = theDoctors.iterator();
    while (it.hasNext()) {
         System.out.println( ((doctor)it.next()).toString() );
    }
}

public boolean doctorAlreadyKnown(doctor sD) {
     return theDoctors.contains(sD.getName());
}

public boolean isDoctorKnown(String name) {
    return theDoctors.contains(name);
}

}
0
 
pigmentartsAuthor Commented:
or does none of it work without the hash maps
0
 
pigmentartsAuthor Commented:
this is now my addPatient in mainSurgery


public void addPatient()
{

    String name = Console.readString (" Enter your name ");
    String address = Console.readString (" Your full address ");
    String Postcode = Console.readString (" Correct postcode ");
    String Telephone = Console.readString (" Existing telephone number ");
    String DateOfBirth = Console.readString (" Your date of Birth ");
    String Allergies = Console.readString (" Do you suffer from allergies? ");
    String doc = Console.readString (" Please enter your doctor's name: "); //<<< here it is
      if ( !sD.isDoctorKnown(doc) )
        System.out.println(" That doctor is unknown.");
    } while ( !sD.isDoctorKnown(doc) );



    Patient p = new Patient( doc, name, address, Postcode, Telephone, DateOfBirth, Allergies);
        System.out.println(" You have been successfully registered");
    sP.addPatient(p);

}
0
 
mmuruganandamCommented:
this one has to work...
0
 
zzynxSoftware engineerCommented:
replace your current storeDoctors class with that one.
0
 
pigmentartsAuthor Commented:
taking out the {  mistake above i now have the error

method contains(java.lang.String) not found in class doctor[] at line 21, col 24
0
 
pigmentartsAuthor Commented:
replace my storeDoctors class?   with what one
0
 
zzynxSoftware engineerCommented:
addPatient() is OK

Btw: Is showing all valid doctor names really an option? What if there are 20 or 30?
0
 
pigmentartsAuthor Commented:
ah ok, just seen another post...so many :P   one min
0
 
zzynxSoftware engineerCommented:
>> with what one?
THIS:

public class storeDoctors {

    private HashMap theDoctors;

   public storeDoctors() {
      theDoctors = new HashMap();
   }

   public boolean addDoctor(doctor sD) {
       theDoctors.put(sD.getName(), sD);  // <<< I suppose your doctor class has a getName() function?
   }

   public void printDoctors() {
    Iterator it = theDoctors.iterator();
    while (it.hasNext()) {
         System.out.println( ((doctor)it.next()).toString() );
    }
}

public boolean doctorAlreadyKnown(doctor sD) {
     return theDoctors.contains(sD.getName());
}

public boolean isDoctorKnown(String name) {
    return theDoctors.contains(name);
}

}
0
 
pigmentartsAuthor Commented:
lol yeah it has a get name function
0
 
pigmentartsAuthor Commented:
ok hash map not found in sD    at 2 lines

and iterator not found in sD
0
 
mmuruganandamCommented:
import java.util.*;
0
 
CEHJCommented:
Perhaps you can see how sometimes, even if not the best 'textbook solution', simply adding one method to a class is a lot easier ;-)
0
 
pigmentartsAuthor Commented:
i put that in sD...i still get the same errors, oh wait, no....

errors are      method iterator() not found in java.util.HashMap
                     methos contains(java.lang.String) not found in.....(above)
0
 
pigmentartsAuthor Commented:
i thought i was just adding one method to my sD class..?
0
 
mmuruganandamCommented:
map.keySet().iterator()

containsKey(
0
 
pigmentartsAuthor Commented:
who what where..
0
 
pigmentartsAuthor Commented:
import java.util.*;   in   storeDoctor class?
0
 
mmuruganandamCommented:
replace the iterator() to keySet().iterator()

replace the contains with containsKey
0
 
mmuruganandamCommented:
>> import java.util.*;   in   storeDoctor class?

The class in which you are using HashMap and Iterator
0
 
zzynxSoftware engineerCommented:
Sorry my fault indeed:

public class storeDoctors {

    private HashMap theDoctors;

   public storeDoctors() {
      theDoctors = new HashMap();
   }

   public boolean addDoctor(doctor sD) {
       theDoctors.put(sD.getName(), sD);  // <<< I suppose your doctor class has a getName() function?
   }

   public void printDoctors() {
    Iterator it = theDoctors.keySet().iterator();
    while (it.hasNext()) {
         System.out.println( (String)it.next() );  // Print out the names
    }
}

public boolean doctorAlreadyKnown(doctor sD) {
     return theDoctors.containsKey(sD.getName());
}

public boolean isDoctorKnown(String name) {
    return theDoctors.containsKey(name);
}

}
0
 
mmuruganandamCommented:
public boolean doctorAlreadyKnown(doctor sD) {
     return theDoctors.containsKey(sD.getName());
}

public boolean isDoctorKnown(String name) {
    return theDoctors.containsKey(name);
}

This can be further abstracted as


public boolean doctorAlreadyKnown(doctor sD) {
     return isDoctorKnown(sD.getName());
}

public boolean isDoctorKnown(String name) {
    return theDoctors.containsKey(name);
}
0
 
pigmentartsAuthor Commented:
getting 'contains'  error..i dont think i've called it anywhere


import java.util.*;
public class storeDoctors {

    private HashMap theDoctors;

   public storeDoctors() {
      theDoctors = new HashMap();
   }

   public boolean addDoctor(doctor sD) {
       theDoctors.put(sD.getName(), sD);
   }

   public void printDoctors() {
    Iterator it = theDoctors.keySet().iterator()     //////this right??
;
    while (it.hasNext()) {
         System.out.println( ((doctor)it.next()).toString() );
    }
}

public boolean doctorAlreadyKnown(doctor sD) {
     return theDoctors.contains(sD.getName());
}

public boolean isDoctorKnown(String name) {
    return theDoctors.contains(name);
}

}


0
 
mmuruganandamCommented:
public boolean doctorAlreadyKnown(doctor sD) {
     return isDoctorKnown(sD.getName());
}

public boolean isDoctorKnown(String name) {
    return theDoctors.containsKey(name);
}
0
 
zzynxSoftware engineerCommented:
>> Iterator it = theDoctors.keySet().iterator()     //////this right??

Yes it is. theDoctors.keySet() returns all the keys in the map, .iterator() creates an iterator over them.

>> getting 'contains'  error..i dont think i've called it anywhere

Oh no????
public boolean doctorAlreadyKnown(doctor sD) {
     return theDoctors.contains(sD.getName());     // <<<<<<<<<<<<<<<<<<<
}

public boolean isDoctorKnown(String name) {
    return theDoctors.contains(name); /// <<<<<<<<<<<<<<<<<<<<<<<<<<<<
}
0
 
pigmentartsAuthor Commented:
print the names of my doctors...

ach,
still getting errors about HashMap and Iterator   not found in storeDoctors
0
 
zzynxSoftware engineerCommented:
>> still getting errors about HashMap and Iterator   not found in storeDoctors
Come on... Can't believe it.

import java.util.*; in your storeDoctors class
0
 
pigmentartsAuthor Commented:
still have those errors..not it doesn't get to stage one of loading  /cry
0
 
mmuruganandamCommented:
in storeDoctor

import java.util.*;

It should be in the top of the storeDoctors class.
0
 
pigmentartsAuthor Commented:
public boolean doctorAlreadyKnown(doctor sD) {
     return theDoctors.contains(sD.getName());     // <<<<<<<<<<<<<<<<<<<error     'contains'
}

public boolean isDoctorKnown(String name) {
    return theDoctors.contains(name); /// <<<<<<<<<<<<<<<<<<<<<<<<<<<< error  'contains'
}
0
 
pigmentartsAuthor Commented:
import java.util.*;
public class storeDoctors {

    private HashMap theDoctors;

   public storeDoctors() {
      theDoctors = new HashMap();
   }

   public boolean addDoctor(doctor sD) {
       theDoctors.put(sD.getName(), sD);  // <<< I suppose your doctor class has a getName() function?
   }

   public void printDoctors() {
    Iterator it = theDoctors.keySet().iterator();
    while (it.hasNext()) {
         System.out.println( (String)it.next() );  // Print out the names
    }
}

public boolean doctorAlreadyKnown(doctor sD) {
     return theDoctors.contains(sD.getName());
}

public boolean isDoctorKnown(String name) {
    return theDoctors.contains(name);
}

}
0
 
zzynxSoftware engineerCommented:
contains() SHOULD BE containsKey()
0
 
pigmentartsAuthor Commented:
System.out.println( (String it.next() alfred, spock));  // Print out the names ------ my doctor names??
0
 
pigmentartsAuthor Commented:
ahhhhhhhhhhhhhhhhhhhhhhhh  come on!!!
0
 
pigmentartsAuthor Commented:
not that it works yet, one min :)
0
 
mmuruganandamCommented:
import java.util.*;
public class storeDoctors {

    private HashMap theDoctors;

   public storeDoctors() {
      theDoctors = new HashMap();
   }


   public boolean addDoctor(doctor sD) {
       theDoctors.put(sD.getName(), sD);  
// <<< the method that returns the doctor name in doctor class sD.getName().. change that
   }

   public void printDoctors() {
    Iterator it = theDoctors.keySet().iterator();
    while (it.hasNext()) {
         System.out.println( (String)it.next() );  // Print out the names
    }
}

public boolean doctorAlreadyKnown(doctor sD) {
     return isDoctorKnown(sD.getName());
}

public boolean isDoctorKnown(String name) {
    return theDoctors.containsKey(name);
}

}
0
 
zzynxSoftware engineerCommented:
Phew... what a mess
;°))
0
 
pigmentartsAuthor Commented:
public boolean isDoctorKnown(String name) {
    return theDoctors.containsKey(name);

or

public boolean isDoctorKnown(String name) {
    return theDoctors.containsKey()(name);

                                                l
                                                l
0
 
pigmentartsAuthor Commented:
phew is right...think how i feel
0
 
zzynxSoftware engineerCommented:
The first
0
 
mmuruganandamCommented:
Good luck man... i am leaving.. it is already too late for me....
0
 
zzynxSoftware engineerCommented:
you ask the map if it contains the name (being the key)

A hashmap maps a key to an object

Key  (name)          Object
--------------------------------------
McEnroe   ==>     doctor McEnroe's doctor object
Johnson   ==>      docor Johnson's doctor object
...


So, you call the containsKey() with as parameter the name
0
 
pigmentartsAuthor Commented:
ok, we maybe there..nearly

i get an error about this so that good.
// <<< the method that returns the doctor name in doctor class sD.getName().. change that

so i change  in doctor class:

public String getName()
{
    return name;
}

to  
public String sD.getName()
{
    return name;
}

0
 
pigmentartsAuthor Commented:
getting a ';' error

public String sD.getName()
0
 
zzynxSoftware engineerCommented:
???????????

Show me your doctor class.

0
 
mmuruganandamCommented:
that may work..
0
 
pigmentartsAuthor Commented:
i kinda get it of get it, kinda like an array
0
 
zzynxSoftware engineerCommented:
your doctor class should return the doctor's name. As simple as what.

public String getName() {
    return name;
}

is OK (if name is the variable you store the doctor's name in)
0
 
pigmentartsAuthor Commented:
thx mmuruganandam, thx for your wisdom...you should sleep like a baby now :)
0
 
pigmentartsAuthor Commented:
import corejava.Console;

public class doctor
{
//attributes
private String name;
private String address;
private String Postcode;
private String homeTelephone;
private String maximumPatients;
private String currentPatients;

public doctor(String n, String a, String p, String h, String m, String c)
{
    name = n;
    address = a;
    homeTelephone = h;
    maximumPatients = m;
    currentPatients = c;

}

public String sD.getName()
{
    return name;
}

public void printDoctors(){
//print doctor details
    System.out.println("\nName >>             " + name);
    System.out.println("\nAddress >>          " + address);
    System.out.println("\nPostcode >>         " + Postcode);
    System.out.println("\nhomeTelephone >>    " + homeTelephone);
    System.out.println("\nmaximumPatients >>  " + maximumPatients);
    System.out.println("\ncurrentPatients >>  " + currentPatients);


}
}


0
 
mmuruganandamCommented:
import corejava.Console;

public class doctor
{
//attributes
private String name;
private String address;
private String Postcode;
private String homeTelephone;
private String maximumPatients;
private String currentPatients;

public doctor(String n, String a, String p, String h, String m, String c)
{
    name = n;
    address = a;
    homeTelephone = h;
    maximumPatients = m;
    currentPatients = c;

}

public String getName()
{
    return name;
}

public void printDoctors(){
//print doctor details
    System.out.println("\nName >>             " + name);
    System.out.println("\nAddress >>          " + address);
    System.out.println("\nPostcode >>         " + Postcode);
    System.out.println("\nhomeTelephone >>    " + homeTelephone);
    System.out.println("\nmaximumPatients >>  " + maximumPatients);
    System.out.println("\ncurrentPatients >>  " + currentPatients);


}
}
0
 
mmuruganandamCommented:
use the given doctor class
0
 
zzynxSoftware engineerCommented:
As I told already...


What the hell does

public String sD.getName() {
}

mean? You define a function!
0
 
pigmentartsAuthor Commented:
public boolean addDoctor(doctor sD) {   -- method does not return a value

you made a comment saying  // <<< the method that returns the doctor name in doctor class sD.getName().. change that

thats why i changed     getName()   to   sD.getName
0
 
zzynxSoftware engineerCommented:
why did you change

public String getName()

into

public String sD.getName()

?


P.S. You're developing in java for how many hours?
;))))
Just a joke, just a joke.
0
 
pigmentartsAuthor Commented:
wondered when i'd start pissin people off :)

problem is when i start using people answers i stop thinking for myself..
0
 
pigmentartsAuthor Commented:
lolol, to be honest i'd say i've been developing java for about 2 days now :)  in essence anyway
0
 
pigmentartsAuthor Commented:
because i thought u told me tt change it !!
0
 
zzynxSoftware engineerCommented:
FYI:

public boolean addDoctor(doctor sD) {
       theDoctors.put(sD.getName(), sD);      // put the object sD in the map with as key sD.getName()
}

sD stands for a doctor object, not a storeDoctors object, bad name.

For readability reasons better use good variable names:

public boolean addDoctor(doctor doc) {
       theDoctors.put(doc.getName(), doc);
}



0
 
pigmentartsAuthor Commented:
ah i see what u were saying now... me thinks
0
 
zzynxSoftware engineerCommented:
>>wondered when i'd start pissin people off :)
It was a joke

>>problem is when i start using people answers i stop thinking for myself..
Don't ever do that!

>> lolol, to be honest i'd say i've been developing java for about 2 days now :)
That's about 16 hours then ;°)))

>> because i thought u told me tt change it !!
It was murugan's comment
0
 
pigmentartsAuthor Commented:
I though sD stood formy storeDoctors class :P  so i could link my array to it
0
 
pigmentartsAuthor Commented:
ahh hehheh, yeah but you've gotta be like...
Has this guy actually studied java??
0
 
zzynxSoftware engineerCommented:
Funny remark: "arts" in pigmentarts means doctor in Dutch.
0
 
zzynxSoftware engineerCommented:
I'm developing in java every day. (since > 1 year)
0
 
pigmentartsAuthor Commented:
Both

 public boolean addDoctor(doctor doc) {
       theDoctors.put(doc.getName(), doc);  gives error of method does not return a value

and

public boolean addDoctor(doctor sD) {
       theDoctors.put(sD.getName(), sD);

give same error of method not returning a value

ok i have a frind interpreting :)   she says i should be putting      this in my mainSurgery  interface
0
 
pigmentartsAuthor Commented:
no way, coincedence??  or fate  :)
0
 
zzynxSoftware engineerCommented:
My fault again.

addDoctor() shouldn't return a boolean.

define it as

public void addDoctor( .......... )
          ^
--------+



...typing all those code by head...
0
 
zzynxSoftware engineerCommented:
"void" means the function doesn't return anything
0
 
pigmentartsAuthor Commented:
Aww no...now its sayin    constructor storedoctors(int) not found in class storeDoctor

sD = new storeDoctors(10);    from mainSurgery
0
 
zzynxSoftware engineerCommented:
Change that in

sD = new storeDoctors();

you don't have to pass a limit to your constructor.
(I think this should be the last one...)
0
 
pigmentartsAuthor Commented:
II cant mix hash map and array.... can i?
0
 
pigmentartsAuthor Commented:
wooohooooooooooooooooooo!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
0
 
pigmentartsAuthor Commented:
yeeeeeeeeeeeeehaaaaaaaaaaaaaa!!!!!!!!!!!!!!!!!  thx man.. your a top man!

Have a good life man, hope to speak to you again
0
 
pigmentartsAuthor Commented:
oh crap one thing.. how do i get  it so when i enter a non existant docotr it will go back to thew start of menu
0
 
zzynxSoftware engineerCommented:
No, no, no. Forget about arrays.

- An array is limited by its initial size, a HashMap can grow as much as needed
- A hashmap takes care of doubles by nature, an array doesn't
- pretty easy to look up by nature
- ...
0
 
zzynxSoftware engineerCommented:
>> how do i get  it so when i enter a non existant docotr it will go back to thew start of menu
Remove the loop in addPatient() and return when needed.

public void addPatient()
{

    String name = Console.readString (" Enter your name ");
    String address = Console.readString (" Your full address ");
    String Postcode = Console.readString (" Correct postcode ");
    String Telephone = Console.readString (" Existing telephone number ");
    String DateOfBirth = Console.readString (" Your date of Birth ");
    String Allergies = Console.readString (" Do you suffer from allergies? ");
    String doc = Console.readString (" Please enter your doctor's name");
    if ( !sD.isDoctorKnown(doc) ) {
        System.out.println(" That doctor is unknown.");
        return;
    }

    Patient p = new Patient( doc, name, address, Postcode, Telephone, DateOfBirth, Allergies);
        System.out.println(" You have been successfully registered");
    sP.addPatient(p);
}

>> your a top man!
Hoping for top points too.
;°)
0
 
pigmentartsAuthor Commented:
if i could give u more i would mate, cya round. have a good life
0
 
zzynxSoftware engineerCommented:
Thanks for accepting

>> if i could give u more i would mate
That's enough. You make my weekend ;)

>>cya round
Certainly. Till the next Q.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 64
  • 40
  • 31
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now