Skip to content
Snippets Groups Projects
Commit 751dcec1 authored by ahmada4's avatar ahmada4
Browse files

Three code smells identified and fixed. 1- multiple occurrences of the same...

Three code smells identified and fixed. 1- multiple occurrences of the same string literal "MM/dd/yyyy". 2- ageInMs calculation longer than 100 lines. 3- age conversion uses multiple magic numbers.
parent c0a5d37e
No related branches found
No related tags found
No related merge requests found
...@@ -46,7 +46,8 @@ public class PatientBean implements Serializable, Comparable<PatientBean> { ...@@ -46,7 +46,8 @@ public class PatientBean implements Serializable, Comparable<PatientBean> {
private String creditCardType = ""; private String creditCardType = "";
private String creditCardNumber = ""; private String creditCardNumber = "";
// Topical Health Information // Topical Health Information
private String dateOfBirthStr = new SimpleDateFormat("MM/dd/yyyy").format(new Date()); private String dateFormat = "MM/dd/yyyy";
private String dateOfBirthStr = new SimpleDateFormat(dateFormat).format(new Date());
private String dateOfDeathStr = ""; private String dateOfDeathStr = "";
private String causeOfDeath = ""; private String causeOfDeath = "";
private String motherMID = "0"; private String motherMID = "0";
...@@ -61,6 +62,12 @@ public class PatientBean implements Serializable, Comparable<PatientBean> { ...@@ -61,6 +62,12 @@ public class PatientBean implements Serializable, Comparable<PatientBean> {
private String spiritualPractices = ""; private String spiritualPractices = "";
private String alternateName = ""; private String alternateName = "";
private String dateOfDeactivationStr = ""; private String dateOfDeactivationStr = "";
// Age calculations
private long milliseconds = 1000;
private long seconds = 60;
private long minutes = 60;
private long hours = 24;
private long days = 365;
public BloodType getBloodType() { public BloodType getBloodType() {
...@@ -97,7 +104,7 @@ public class PatientBean implements Serializable, Comparable<PatientBean> { ...@@ -97,7 +104,7 @@ public class PatientBean implements Serializable, Comparable<PatientBean> {
public Date getDateOfBirth() { public Date getDateOfBirth() {
try { try {
return new SimpleDateFormat("MM/dd/yyyy").parse(dateOfBirthStr); return new SimpleDateFormat(dateFormat).parse(dateOfBirthStr);
} catch (ParseException e) { } catch (ParseException e) {
return null; return null;
...@@ -106,7 +113,7 @@ public class PatientBean implements Serializable, Comparable<PatientBean> { ...@@ -106,7 +113,7 @@ public class PatientBean implements Serializable, Comparable<PatientBean> {
public Date getDateOfDeath() { public Date getDateOfDeath() {
try { try {
return new SimpleDateFormat("MM/dd/yyyy").parse(dateOfDeathStr); return new SimpleDateFormat(dateFormat).parse(dateOfDeathStr);
} catch (ParseException e) { } catch (ParseException e) {
return null; return null;
...@@ -119,9 +126,10 @@ public class PatientBean implements Serializable, Comparable<PatientBean> { ...@@ -119,9 +126,10 @@ public class PatientBean implements Serializable, Comparable<PatientBean> {
public int getAge() { public int getAge() {
try { try {
long ageInMs = System.currentTimeMillis() long currentTime = System.currentTimeMillis();
- new SimpleDateFormat("MM/dd/yyyy").parse(dateOfBirthStr).getTime(); long birthTime = new SimpleDateFormat(dateFormat).parse(dateOfBirthStr).getTime();
long age = ageInMs / (1000L * 60L * 60L * 24L * 365L); long ageInMs = currentTime - birthTime;
long age = ageInMs / (milliseconds * seconds * minutes * hours * days);
return (int) age; return (int) age;
} catch (ParseException e) { } catch (ParseException e) {
...@@ -135,9 +143,10 @@ public class PatientBean implements Serializable, Comparable<PatientBean> { ...@@ -135,9 +143,10 @@ public class PatientBean implements Serializable, Comparable<PatientBean> {
long ageInMs; long ageInMs;
try { try {
ageInMs = System.currentTimeMillis() long currentTime = System.currentTimeMillis();
- new SimpleDateFormat("MM/dd/yyyy").parse(dateOfBirthStr).getTime(); long birthTime = new SimpleDateFormat(dateFormat).parse(dateOfBirthStr).getTime();
age = ageInMs / (1000L * 60L * 60L * 24L); ageInMs = currentTime - birthTime;
age = ageInMs / (milliseconds * seconds * minutes * hours * days);
} }
catch (ParseException e) { catch (ParseException e) {
...@@ -154,9 +163,10 @@ public class PatientBean implements Serializable, Comparable<PatientBean> { ...@@ -154,9 +163,10 @@ public class PatientBean implements Serializable, Comparable<PatientBean> {
long ageInMs; long ageInMs;
try { try {
ageInMs = System.currentTimeMillis() long currentTime = System.currentTimeMillis();
- new SimpleDateFormat("MM/dd/yyyy").parse(dateOfBirthStr).getTime(); long birthTime = new SimpleDateFormat(dateFormat).parse(dateOfBirthStr).getTime();
age = ageInMs / (1000L * 60L * 60L * 24L * 7L); ageInMs = currentTime - birthTime;
age = ageInMs / (milliseconds * seconds * minutes * hours * days);
} }
catch (ParseException e) { catch (ParseException e) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment