patternjavaMinor
Simple object oriented design for a patient system
Viewed 0 times
simpledesignsystemfororientedpatientobject
Problem
I want to model this scenario in object oriented language:
Please review my code and tell me:
```
import java.util.Date;
import java.util.*;
import java.text.*;
class Patient
{
private String name;
private String streetAdress1;
private String phoneNumber;
private String streetAdress2;
private String zipCode; //I really dont need to preform any numerical operations.
private List mcList;
public Patient(String name) //Public for constructor.
{
this.name = name;
mcList = new ArrayList();
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
//All setter and getters
public void addMedicalCondition(MedicalCondition mc)
{
mcList.add(mc);
}
public void deleteMedicalCondition(MedicalCondition mc)
{
mcList.remove(mc);
}
public List getMedicalConditions()
{
return mcList;
}
public String toString(){
return name;
}
}
class MedicalCondition
{
private String nameOfCondition;
Date dateOfReport;
public MedicalCondition(String nameOfCondition,Date dateOfReport)
{
this.nameOfCondition = nameOfCondition;
this.dateOfReport = dateOfReport;
}
public String toString()
{
return nameOfCondition;
}
}
public class HelloWorld{
public static void main(String args[]) throws ParseException{
Patient p1 = new Patient("Mike");
DateFormat format = new SimpleDateFormat("mm/dd/yyyy");
- A hospital has number of patients
- A patient can have zero or more medical conditions
- A medical condition is identified by name and date
Please review my code and tell me:
- What can I do to incorporate object oriented fundamentals like reusability and extensibility?
- Do I need to implement an
equalsmethod in theMedicalConditionclass?
- Do I need to initialize all the instance variables in constructor?
```
import java.util.Date;
import java.util.*;
import java.text.*;
class Patient
{
private String name;
private String streetAdress1;
private String phoneNumber;
private String streetAdress2;
private String zipCode; //I really dont need to preform any numerical operations.
private List mcList;
public Patient(String name) //Public for constructor.
{
this.name = name;
mcList = new ArrayList();
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
//All setter and getters
public void addMedicalCondition(MedicalCondition mc)
{
mcList.add(mc);
}
public void deleteMedicalCondition(MedicalCondition mc)
{
mcList.remove(mc);
}
public List getMedicalConditions()
{
return mcList;
}
public String toString(){
return name;
}
}
class MedicalCondition
{
private String nameOfCondition;
Date dateOfReport;
public MedicalCondition(String nameOfCondition,Date dateOfReport)
{
this.nameOfCondition = nameOfCondition;
this.dateOfReport = dateOfReport;
}
public String toString()
{
return nameOfCondition;
}
}
public class HelloWorld{
public static void main(String args[]) throws ParseException{
Patient p1 = new Patient("Mike");
DateFormat format = new SimpleDateFormat("mm/dd/yyyy");
Solution
Bracing style
You started off with Allman-style bracing approach, but switched to the Java convention for
Medical condition dates
Your medical conditions carry a start date, but should they also have an end date? Or are we only talking about incurable medical conditions here? While we're at it, Java 8 has new 'Time' APIs with a different (and better) approach to modeling chronology, so you may want to use the newer
OK, now I get it... you delete medical conditions when they are cured. If you need to preserve medical history, you now know what you can do too...
Throwing
I understand
Do I need to implement an
Your call. :) The longer answer is, what does it mean when a medical condition is-equal to another? Same condition and date? How helpful will such an equivalence be? To 'group' patients by the same medical conditions? If you don't have a strong case for this, you probably wouldn't want to implement it first.
Adding medical conditions
In this regard, I will recommend your existing approach over @Amr Ayman's answer. This is mainly because your
Actually, on this note, you can also consider making your
You started off with Allman-style bracing approach, but switched to the Java convention for
HelloWorld and its main() method. Regardless of the styles you choose, please be consistent on this front. :)Medical condition dates
Your medical conditions carry a start date, but should they also have an end date? Or are we only talking about incurable medical conditions here? While we're at it, Java 8 has new 'Time' APIs with a different (and better) approach to modeling chronology, so you may want to use the newer
LocalDate class.OK, now I get it... you delete medical conditions when they are cured. If you need to preserve medical history, you now know what you can do too...
Throwing
Exceptionspublic static void main(String args[]) throws ParseExceptionI understand
HelloWorld is something like a test class to see your implementation in action, but still try to avoid doing something like this, especially when it's your main() method. One wrong input will simply cause your test class to quit with a stacktrace. It will be better if you have a method to handle the parsing and any possible errors. For example, if you want to default to the current date in case of parsing errors:private static LocalDate parse(String date) {
// using the new time APIs
try {
return DateTimeFormatter.ISO_LOCAL_DATE.parse(date, LocalDate::from);
} catch (DateTimeParseException e) {
return LocalDate.now();
}
}equals() implementationDo I need to implement an
equals method in the MedicalCondition class?Your call. :) The longer answer is, what does it mean when a medical condition is-equal to another? Same condition and date? How helpful will such an equivalence be? To 'group' patients by the same medical conditions? If you don't have a strong case for this, you probably wouldn't want to implement it first.
Adding medical conditions
In this regard, I will recommend your existing approach over @Amr Ayman's answer. This is mainly because your
Patient class should know what it means to add a medical condition, instead of letting callers of your Patient instances 'figure that out'. A better refinement is to wrap the instance returned by getMedicalConditions() with a Collections.unmodifiableList(): this will prevent callers of the method to modify the list's contents directly, such as wiping them outright.Actually, on this note, you can also consider making your
MedicalCondition class immutable, and I shall leave that as an exercise for the healthy reader...Code Snippets
public static void main(String args[]) throws ParseExceptionprivate static LocalDate parse(String date) {
// using the new time APIs
try {
return DateTimeFormatter.ISO_LOCAL_DATE.parse(date, LocalDate::from);
} catch (DateTimeParseException e) {
return LocalDate.now();
}
}Context
StackExchange Code Review Q#102612, answer score: 2
Revisions (0)
No revisions yet.