Link to home
Start Free TrialLog in
Avatar of eugene007
eugene007

asked on

contains

This code can be found in the MedicalConsole.java

if(((Hospital)m).contains(p))
{
    System.out.println("\n\n*************");
    System.out.println("Error. Duplicate.");
    System.out.println("*************\n\n");        
}
else
{
    ((Hospital)m).add(p);
}



This code can be found in Hospital.java

public boolean contains(Procedure p)
{
    if(getProcedures().contains(p))
    {
           return true;
    }
    else
    {
           return false;
    }
}




This code can be found in the procedure.java


public boolean equals(Object obj)
{
   if(this==obj)
   {
           return true;
   }
          
   Procedure p = (Procedure) obj;
                 
   if (getCode() == p.getCode())
   {
        return true;
   }
   else
   {
        return false;
    }      
}



The code in procedure.java does not seem to work.  
getCode()  will return an integer value. Therefore
the code in my MedicalConsole.java will not display
an error msg if there is a duplicate code.

How do I solve this problem.

Your help is kindly appreciated.

Regards

Eugene
Avatar of zzynx
zzynx
Flag of Belgium image

You can write your equals method as

public boolean equals(Object obj) {
   Procedure p = (Procedure) obj;
   return (getCode() == p.getCode());
}
Avatar of eugene007
eugene007

ASKER

If my code is a string type then I can do as bellow.

public boolean equals(Object obj)
{
   if(this==obj)
   {
           return true;
   }
         
   Procedure p = (Procedure) obj;
   return getCode().equalsIgnoreCase(p.getCode());
}

But in my case the code is an interger value.
>> getCode()  will return an integer value. Therefore
>> the code in my MedicalConsole.java will not display
>> an error msg if there is a duplicate code.

Therefore?
1) I don't like the

   if(this==obj)
   {
           return true;
   }

part in your equals() method

>> But in my case the code is an interger value
2) So what?

>>    return (getCode() == p.getCode());
will return true if both integers are the same and false otherwise.
Isn't that what you want?
Does not seem to work.

You can view the code:

www.mutaiyas.com/db/medical.zip
>> Does not seem to work
You mean that if you enter a procedure-code that already exists, you don't get "Error. Duplicate."?
Time for some extra debugging information:

Try running with this:

      public boolean equals(Object obj) {
              Procedure p = (Procedure) obj;
              System.out.println("This code=" + getCode() + " - other code=" + p.getCode());  // <<<<<<<<
              return (getCode() == p.getCode());    
      }

What do you get?
yes.
Be more precise. What does 'not seem to work' mean. It should work!
nothing.
no msg is being displayed.
Meaning that equals() method gets never called
Thinking....
I think the problem is because my MedicalFacility class which is an abtract class of hosptal class has got an equals method.
I don't think so. We're talking about comparing instances of the class Procedure
Yu are sure, that the contains()-statement is reached? Try to put a System.out in front of it.
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That is why the equals method in my procedure is not executed.
I don't know what you try here, but with me, it says "Error. Duplicate."

       - HEALTH SERVICE MANAGEMENT SYSTEM -



____________________________________________________
                       MAIN MENU                    
Please choose a number from the menu and            
press <ENTER>                                      
|                                                  |
1. Add                                              
2. Delete                                          
3. Modify                                          
4. List                                            
5. Present                                          
6. Operate                                          
7. Exit                                            


Please type your selection then press <ENTER>:
1


____________________________________________________
                    ADD SUB MENU                    
Please choose a number from the menu and            
press <ENTER>                                      
|                                                  |
1. Add Medical Facility                            
2. Add Patient                                      
3. Add Procedure                                    
4. Main Menu                                        


Please type your selection then press <ENTER>:      
1
Enter the medical facility name
Hans
Select hospital or health clinic (h/c)
h
Enter the probAdmid
23


____________________________________________________
                       MAIN MENU                    
Please choose a number from the menu and            
press <ENTER>                                      
|                                                  |
1. Add                                              
2. Delete                                          
3. Modify                                          
4. List                                            
5. Present                                          
6. Operate                                          
7. Exit                                            


Please type your selection then press <ENTER>:
1


____________________________________________________
                    ADD SUB MENU                    
Please choose a number from the menu and            
press <ENTER>                                      
|                                                  |
1. Add Medical Facility                            
2. Add Patient                                      
3. Add Procedure                                    
4. Main Menu                                        


Please type your selection then press <ENTER>:      
3
Enter the medical facility name
23


******************************
The medical facility is not valid.
******************************




******************************************
The procedure can only be added to a hospital.
******************************************




____________________________________________________
                       MAIN MENU                    
Please choose a number from the menu and            
press <ENTER>                                      
|                                                  |
1. Add                                              
2. Delete                                          
3. Modify                                          
4. List                                            
5. Present                                          
6. Operate                                          
7. Exit                                            


Please type your selection then press <ENTER>:
1


____________________________________________________
                    ADD SUB MENU                    
Please choose a number from the menu and            
press <ENTER>                                      
|                                                  |
1. Add Medical Facility                            
2. Add Patient                                      
3. Add Procedure                                    
4. Main Menu                                        


Please type your selection then press <ENTER>:      
3
Enter the medical facility name
Hans
Enter the procedure name
Peter
Enter the procedure code
1
Enter the procedure description
sadsafafa
Select elective or non elective (e/n)
e
Enter the procedure cost
34


____________________________________________________
                       MAIN MENU                    
Please choose a number from the menu and            
press <ENTER>                                      
|                                                  |
1. Add                                              
2. Delete                                          
3. Modify                                          
4. List                                            
5. Present                                          
6. Operate                                          
7. Exit                                            


Please type your selection then press <ENTER>:
1


____________________________________________________
                    ADD SUB MENU                    
Please choose a number from the menu and            
press <ENTER>                                      
|                                                  |
1. Add Medical Facility                            
2. Add Patient                                      
3. Add Procedure                                    
4. Main Menu                                        


Please type your selection then press <ENTER>:      
3
Enter the medical facility name
Hans
Enter the procedure name
werwrwrwerwe
Enter the procedure code
1
Enter the procedure description
afdfsdfsf
Select elective or non elective (e/n)
e
Enter the procedure cost
34


*************
Error. Duplicate.
*************




____________________________________________________
                       MAIN MENU                    
Please choose a number from the menu and            
press <ENTER>                                      
|                                                  |
1. Add                                              
2. Delete                                          
3. Modify                                          
4. List                                            
5. Present                                          
6. Operate                                          
7. Exit                                            


Please type your selection then press <ENTER>:
If your screen just doesn't shows it, my solution is, to await a return after showing the error message :))
I placed the following code in my contains method of the Hospital class and it displays.

System.out.println("Error");
Any luck with my suggestion?
lol
Ups sorry, you updated the .zip ... i didn't notice :)
thanks