• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

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
0
eugene007
Asked:
eugene007
  • 10
  • 9
  • 6
1 Solution
 
zzynxSoftware engineerCommented:
You can write your equals method as

public boolean equals(Object obj) {
   Procedure p = (Procedure) obj;
   return (getCode() == p.getCode());
}
0
 
eugene007Author Commented:
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.
0
 
zzynxSoftware engineerCommented:
>> 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?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
zzynxSoftware engineerCommented:
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?
0
 
eugene007Author Commented:
Does not seem to work.

You can view the code:

www.mutaiyas.com/db/medical.zip
0
 
zzynxSoftware engineerCommented:
>> Does not seem to work
You mean that if you enter a procedure-code that already exists, you don't get "Error. Duplicate."?
0
 
zzynxSoftware engineerCommented:
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?
0
 
eugene007Author Commented:
yes.
0
 
Mig-OCommented:
Be more precise. What does 'not seem to work' mean. It should work!
0
 
eugene007Author Commented:
nothing.
0
 
eugene007Author Commented:
no msg is being displayed.
0
 
zzynxSoftware engineerCommented:
Meaning that equals() method gets never called
Thinking....
0
 
eugene007Author Commented:
I think the problem is because my MedicalFacility class which is an abtract class of hosptal class has got an equals method.
0
 
zzynxSoftware engineerCommented:
I don't think so. We're talking about comparing instances of the class Procedure
0
 
Mig-OCommented:
Yu are sure, that the contains()-statement is reached? Try to put a System.out in front of it.
0
 
zzynxSoftware engineerCommented:
Can you please add this method to Procedure.java and try again:

          public int hashCode() {
             return getCode();
          }

Background:
http://www.javapractices.com/Topic17.cjp
http://www.javapractices.com/Topic28.cjp
0
 
eugene007Author Commented:
That is why the equals method in my procedure is not executed.
0
 
Mig-OCommented:
I don't know what you try here, but with me, it says "Error. Duplicate."
0
 
Mig-OCommented:

       - 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>:
0
 
Mig-OCommented:
If your screen just doesn't shows it, my solution is, to await a return after showing the error message :))
0
 
eugene007Author Commented:
I placed the following code in my contains method of the Hospital class and it displays.

System.out.println("Error");
0
 
zzynxSoftware engineerCommented:
Any luck with my suggestion?
0
 
eugene007Author Commented:
lol
0
 
Mig-OCommented:
Ups sorry, you updated the .zip ... i didn't notice :)
0
 
zzynxSoftware engineerCommented:
thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 10
  • 9
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now