Results 1 to 16 of 16
Like Tree1Likes
  • 1 Post By Tolls

Thread: Mocking a hospital database - OOP.

  1. #1
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default Mocking a hospital database - OOP.

    Hello once again guys. One thing that really aggravates me if when you go to different hospitals or move from one area to another and your old doctors/local hospital has to send over all of your records to the new doctors/hospital.

    What I thought about doing is creating a program where the user (receptionist/nurse) can type into their system your name and ALL of your past medical history can be automatically accessed even from another hospitals database records.

    I have 3 classes at the moment.

    -HospitalOne
    -MainProgram
    -Array

    Firstly, I want to setup a database of patients with thier personal details (address/d.o.b/records). Then i'd like my mainprogram class to be able to access these records from the HospitalOne class from an array/ArrayList. Then I would like to do the same with another HospitalTwo class and make their respective records accessible by each individual hospital class. Here is what I have so far. Now it is done OOP style with the guidance of some youtube tuts but I'm still a bit unsure of how to create a method which will access the whole records of a patient once their name has been entered into the system.

    My question is:- What things do I need to consider when writing a method to access an arraylist/array in another class.

    Let me show you what I have down so far:

    MainProgram
    Java Code:
    import java.util.Scanner;
    import java.util.*;
    
    class MainProgram {
    
    	 public static void main(String[] args) {
    	 
    	     Scanner input = new Scanner(System.in);
    		 HospitalOne col = new HospitalOne();
    		 ArrayOne arrayDb = new ArrayOne();
    		 System.out.println("Enter the name of the patient: ");
    		 String tempP = input.nextLine();
    		 
    		 col.setPatient(tempP);
    		 col.details();
    		 col.searching();
    		 arrayDb.create();
         }
    }
    HospitalOne
    Java Code:
    import java.util.*;
    
    public class HospitalOne {
    
         private String patient;
    	 
    	 public void setPatient(String name) {
    	     patient = name;
    	 }
    	 public String getPatient() {
    	     return patient;
    	 }
         public void details() {
    	     System.out.printf("Patient: %s", getPatient());
    	 }
    	 public void searching() {
    	     System.out.println("");
    	     System.out.println("Searching Database...");
    	 }
    }
    ArrayOne
    Java Code:
    import java.util.*;
    
    public class ArrayOne {
    
         public static void main(String[] args) {
    
             create();
    	 }
    	 
    	 public static void create() {
    	 
    	     ArrayList<String> detailHistory = new ArrayList<String>();
    	 
    	     detailHistory.add(new String("Name: Stephen Myhill"));
    	     detailHistory.add("D.O.B: 21/05/80");
    	     detailHistory.add("Address: 22 Long Road, Compton, Chesterfield, UK");
    	     detailHistory.add("History/Details: Chronic Bronchitis, Muscular Spasm, Diabetes");
    	 
    	     System.out.println(detailHistory);
    	 
    	     for(int i=0;i<detailHistory.size();i++) {
                 System.out.println("ArrayList Element "+i+" :"+detailHistory.get(i));
             }
    	 }
    }

    Thank you for any guidance.
    [A!B]Java

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default Re: Mocking a hospital database - OOP.

    Your naming needs some work.

    First, each Hospital should be an instance of a Hospital class. It looks suspiciously like you are going to write a new class for each hospital.

    Second, your Array class looks wrong. That should be a Patient. The Patient would have all the needed attributes, not as a List of Strings, but as proper attributes of the relevant datatype. (Date of birth would be a Date for example).
    Fubarable likes this.

  3. #3
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default Re: Mocking a hospital database - OOP.

    Ah so rather than creating another class, which I was going to do, for example I should have one Hospital.class and create the hospitals as such:

    Java Code:
    Hospital hosp1 = new Hospital();
    Hospital hosp2 = new Hospital();
    Is this what you mean by an instance, it's got the same methods etc but you create different unique objects off them 'blueprints'??
    [A!B]Java

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default Re: Mocking a hospital database - OOP.

    That's it exactly.
    Indeed "blueprint" is often used to try and explain what a class represents.

  5. #5
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default Re: Mocking a hospital database - OOP.

    Would I create multiple arrays within the Array.class holding every single hospitals details (in separate ones of course) or would I do the same as I have done with the hospitals?
    [A!B]Java

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default Re: Mocking a hospital database - OOP.

    I'm not too sure what you're actually building here, so I can't say.
    But the Arrays class (as a name) doesn't really tell me what it's modelling.

    Though (re-reading your post) I suspect you mean "every single Patients details"...so, presuming the Arrays represents your database (in which case I would probably rename it PatientDatabase) then yes, it would hold all of them. Give it addPatient() and removePatient() methods, along with some sort of findPatient() method, and you'd have a pretty good basic DB setup to work with. Then have something else populate it, like your MainProgram (say a method, populateDatabase(), which will simply call addPatient() lots of times).

  7. #7
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default Re: Mocking a hospital database - OOP.

    I'm trying to look up the arraylists add method to see whether or not I can use it in my program but change it for my method. Am I wasting my time?
    [A!B]Java

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default Re: Mocking a hospital database - OOP.

    I wouldn't say you're wasting your time, but if you are mocking a database then you might want to build on the simple add and remove that an ArrayList provides.
    Your mocked Database would contain an ArrayList of Patients. That would allow you to add the search stuff easily. The add and remove would probably just call the equivalent on the ArrayList directly.

  9. #9
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default Re: Mocking a hospital database - OOP.

    I'm just currently looking into making an arraylist as I am relatively new to Java. I have now renamed the Array.class to PatientDatabase.class. When I try to compile, I get the following errors which I have never seen before:

    Java Code:
    C:\mywork>javac PatientDatabase.java
    Note: PatientDatabase.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    Am I missing something? I tried to read Oracle's tuts on AL but found them too complicated to understand so I went to another site where this is the supposedly 'correct' way to create an AL. Any thought?

    Thank you for your time,

    Regards,

    NM.
    Last edited by Nanomech; 09-07-2011 at 04:00 PM. Reason: Put complie errors inside code tags to ease readability
    [A!B]Java

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default Re: Mocking a hospital database - OOP.

    That's a warning, but it does (usually) imply you are not saying what the ArrayList should be holding:
    Java Code:
    ArrayList myList = new ArrayList();
    will give the above warning, whereas:
    Java Code:
    ArrayList<String> myList = new ArrayList<String>();
    won't.

    Though that's a gues since I can't see your code.

  11. #11
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default Re: Mocking a hospital database - OOP.

    Ah that's brilliant, yes I hadn't put the <String> at the end of the ArrayList definition. Going to try and complete a bit more on my own, starting with looking up the ArrayLists methods. Thanks again and i'll keep you updated.

    Regards,

    NM.
    [A!B]Java

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default Re: Mocking a hospital database - OOP.

    You had it in your original ArrayOne code:
    Java Code:
    ArrayList<String> detailHistory = new ArrayList<String>();
    so I assume you skipped it when you rewrote it.

  13. #13
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default Re: Mocking a hospital database - OOP.

    That's right I was messing around with them because I seemed to be getting errors left, right and center.

    Just sitting here looking at my code and a question popped into my head.

    If you have two classes, one of them getting data off a user, and passing the value into another classes method such as searchPatient, because you stated you wanted to go into that certain method, when that class is gone into does it go straight into that method or into the main method?

    I read that when using multiple classes, some do not need main method, but I believe my one does because I have an arraylist created. So when I pass the patients name into the database class does it go straight to the main method first before searching, or does it search for a name which potentially hasn't been created yet? Here is the relevant classes (please not that at the moment, I am only trying to get the functionality of being able to add to an AL from another class for the time being).

    MainProgram:

    Java Code:
    import java.util.Scanner;
    
    class MainProgram {
    
    	 public static void main(String[] args) {
    	 
    	     Scanner input = new Scanner(System.in);
    		 PatientDatabase pd = new PatientDatabase();
    		 PatientDatabase arrayDb = new PatientDatabase();
    		 System.out.println("Enter the name of the patient: ");
    		 String tempP = input.nextLine();
    		 
    		 pd.searchPatient(tempP);
    		 pd.details();
    		 pd.searchMsg();
         }
    }
    PatientDatabase:

    Java Code:
    import java.util.*;
    
    public class PatientDatabase {
         
         public void addPatient(String name) {
    	     pnd.add(name);
    	 }
    	 public void searchPatient(String search) {
    	     if (pnd.contains(search)) {
    		    details(); //bring up the patients details
    		 }
    	 }
    	 
    	 public void searchMsg() {
    	     System.out.println("");
    	     System.out.println("Searching Database...");
    	 }
    	 
    	 public void details() {
    	     
    	 }
    	 
    	 public static void main(String[] args) {
    	     
    		 ArrayList<String> pnd = new ArrayList<String>();
    		 
    		 pnd.add("Stephen Myhill");
    		 pnd.add("David Gilmour");
    		 pnd.add("James Carter");
    		 pnd.add("Jack Byrnes");
    		 pnd.add("Lloyd Christmas");	 
    	 }
    	   
    }
    Regards,

    NM
    Last edited by Nanomech; 09-07-2011 at 04:59 PM.
    [A!B]Java

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default Re: Mocking a hospital database - OOP.

    The main() method (public static void main(String[] args)) is simply the entry point for an application.
    It's what the JVM looks for in the class you tell it to run:
    java MyClass
    So in the above it will find the MyClass.class file and expect to see a main() method in there.

    So in yours only the first one will be used. The other one is redundant (unless you are somehow testing that class independently, but there are better things to use for that than fillig up all your classes with main() methods).

  15. #15
    Nanomech's Avatar
    Nanomech is offline Member
    Join Date
    Aug 2011
    Location
    England
    Posts
    71
    Rep Power
    0

    Default Re: Mocking a hospital database - OOP.

    Would that be a Constructor because I read that it is a way to pre-define things, and seeing as though there will always be patients in the database (unless in the unlikely event a hospital/doctors closes), should I create the array inside a Constructor in the PatientDatabase class?

    Regards,

    NM.
    [A!B]Java

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default Re: Mocking a hospital database - OOP.

    A constructor should setup your object for use.
    So you would create your (empty) array in one, though I just noticed your array is not actually an attribute of the class. You'll need to fix that.

    What you wouldn't do (except in rare, special cases) is actually populate it there. I would have something else populate it by calling the addPatient() method. Possibly in your MainProgram class. That way you can change the data held in there without changing the class code itself.

Similar Threads

  1. Relational Database or Object Database?
    By mattlindsay in forum New To Java
    Replies: 8
    Last Post: 09-24-2011, 06:44 PM
  2. Problem in mocking a class
    By nonhocapito in forum New To Java
    Replies: 0
    Last Post: 10-05-2010, 08:01 AM
  3. Mocking a class for Testing
    By hotinlavacoolinjava in forum New To Java
    Replies: 4
    Last Post: 09-22-2009, 10:23 AM
  4. Replies: 2
    Last Post: 02-11-2009, 04:43 AM
  5. Mocking static methods of class
    By Kat in forum New To Java
    Replies: 3
    Last Post: 11-08-2007, 12:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •