From 751dcec1a42393db82c9efc84e8a78c4218d4515 Mon Sep 17 00:00:00 2001 From: ahmada4 <ahmada4@illinois.edu> Date: Thu, 19 Nov 2020 22:41:22 -0600 Subject: [PATCH] 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. --- .../ncsu/csc/itrust/beans/PatientBean.java | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/iTrust/src/edu/ncsu/csc/itrust/beans/PatientBean.java b/iTrust/src/edu/ncsu/csc/itrust/beans/PatientBean.java index e2560d3..9dd89e9 100644 --- a/iTrust/src/edu/ncsu/csc/itrust/beans/PatientBean.java +++ b/iTrust/src/edu/ncsu/csc/itrust/beans/PatientBean.java @@ -46,7 +46,8 @@ public class PatientBean implements Serializable, Comparable<PatientBean> { private String creditCardType = ""; private String creditCardNumber = ""; // 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 causeOfDeath = ""; private String motherMID = "0"; @@ -61,6 +62,12 @@ public class PatientBean implements Serializable, Comparable<PatientBean> { private String spiritualPractices = ""; private String alternateName = ""; 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() { @@ -97,7 +104,7 @@ public class PatientBean implements Serializable, Comparable<PatientBean> { public Date getDateOfBirth() { try { - return new SimpleDateFormat("MM/dd/yyyy").parse(dateOfBirthStr); + return new SimpleDateFormat(dateFormat).parse(dateOfBirthStr); } catch (ParseException e) { return null; @@ -106,7 +113,7 @@ public class PatientBean implements Serializable, Comparable<PatientBean> { public Date getDateOfDeath() { try { - return new SimpleDateFormat("MM/dd/yyyy").parse(dateOfDeathStr); + return new SimpleDateFormat(dateFormat).parse(dateOfDeathStr); } catch (ParseException e) { return null; @@ -119,9 +126,10 @@ public class PatientBean implements Serializable, Comparable<PatientBean> { public int getAge() { try { - long ageInMs = System.currentTimeMillis() - - new SimpleDateFormat("MM/dd/yyyy").parse(dateOfBirthStr).getTime(); - long age = ageInMs / (1000L * 60L * 60L * 24L * 365L); + long currentTime = System.currentTimeMillis(); + long birthTime = new SimpleDateFormat(dateFormat).parse(dateOfBirthStr).getTime(); + long ageInMs = currentTime - birthTime; + long age = ageInMs / (milliseconds * seconds * minutes * hours * days); return (int) age; } catch (ParseException e) { @@ -135,9 +143,10 @@ public class PatientBean implements Serializable, Comparable<PatientBean> { long ageInMs; try { - ageInMs = System.currentTimeMillis() - - new SimpleDateFormat("MM/dd/yyyy").parse(dateOfBirthStr).getTime(); - age = ageInMs / (1000L * 60L * 60L * 24L); + long currentTime = System.currentTimeMillis(); + long birthTime = new SimpleDateFormat(dateFormat).parse(dateOfBirthStr).getTime(); + ageInMs = currentTime - birthTime; + age = ageInMs / (milliseconds * seconds * minutes * hours * days); } catch (ParseException e) { @@ -154,9 +163,10 @@ public class PatientBean implements Serializable, Comparable<PatientBean> { long ageInMs; try { - ageInMs = System.currentTimeMillis() - - new SimpleDateFormat("MM/dd/yyyy").parse(dateOfBirthStr).getTime(); - age = ageInMs / (1000L * 60L * 60L * 24L * 7L); + long currentTime = System.currentTimeMillis(); + long birthTime = new SimpleDateFormat(dateFormat).parse(dateOfBirthStr).getTime(); + ageInMs = currentTime - birthTime; + age = ageInMs / (milliseconds * seconds * minutes * hours * days); } catch (ParseException e) { -- GitLab