2015-04-29 154 views
0

在我的EMR類中,我試圖在帶有2個變量的構造函數後增加患者計數器數據字段。我也試圖在用5個變量構造我的構造之後增加患者計數器數據字段。在構造函數中計數實例

在Main中,最終目標是創建5個新的患者記錄(我曾這樣做)並打印前後可用的患者記錄數。

我很確定我必須在我的EMR類中使用public static long numofpatients,但不知道該從哪裏去。

主要代碼:

package studenthealthservices; 

public class Studenthealthservices { 
    public static void main(String[] args) { 
     EMR p1 = new EMR("Colin", "10-22-74", "Strained Ankle", 99, 110, "hurt during football", "ankle brace"); 
     System.out.println(p1.toString()); 

     EMR p2 = new EMR("Anquan", "9-30-77", "stomach ache", 98, 120, "stress", "Tylenol"); 
     System.out.println(p2.toString()); 

     EMR p3 = new EMR("Buster", "3-27-1987", "Broken ankle", 99, 113, "Scott Cousins", "None"); 
     System.out.println(p3.toString()); 

     EMR P4 = new EMR("Frank The Tank", "4/1/89"); 

     EMR p5 = new EMR("Merton Hanks", "03-12-1968"); 
    } 
} 

EMR類代碼:

package studenthealthservices; 

public class EMR { 
    private String name; 
    private String dob; 
    private String rfv; 
    private double bodyt; 
    private double hr; 
    private String diag; 
    private String pmeds; 

    public void setName(String name) { 
     this.name = name; 
    } 

    public EMR(String name, String dob) { 
     this.name = name; 
     this.dob = dob; 
    } 

    public String getName() { 
     return name; 
    } 

    public EMR(String name, String dob, String rfv, double bodyt, double hr, String diag, String pmeds) { 
     this.name = name; 
     this.dob = dob; 
     this.rfv = rfv; 
     this.bodyt = bodyt; 
     this.hr = hr; 
     this.diag = diag; 
     this.pmeds = pmeds; 
    } 

    public String getDob() { 
     return dob; 
    } 

    public void setDob(String dob) { 
     this.dob = dob; 
    } 

    public String getRfv() { 
     return rfv; 
    } 

    public void setRfv(String rfv) { 
     this.rfv = rfv; 
    } 

    public double getBodyt() { 
     return bodyt; 
    } 

    public void setBodyt(double bodyt) { 
     this.bodyt = bodyt; 
    } 

    public double getHr() { 
     return hr; 
    } 

    public void setHr(double hr) { 
     this.hr = hr; 
    } 

    public String getDiag() { 
     return diag; 
    } 

    public void setDiag(String diag) { 
     this.diag = diag; 
    } 

    public String getPmeds() { 
     return pmeds; 
    } 

    public void setPmeds(String pmeds) { 
     this.pmeds = pmeds; 
    } 

    public void redFlags() { 
     String help = "get help!"; 
     if (bodyt >= 97.3 && bodyt <= 99.1) 
      this.bodyt = bodyt; 
     if (hr >= 60 && hr <= 100) 
      this.hr = hr; 
     else { 
      System.out.printf(help); 
     } 
    } 

    @Override 
    public String toString() { 
     return "\nname : " + this.name 
      + "\nDate of Birth: " + this.dob 
      + "\nReason for visit: " + this.rfv 
      + "\nBody Temperature: " + this.bodyt 
      + "\nHeart Rate " + this.hr 
      + "\nDiagnosis: " + this.diag 
      + "\nPrescribed Meds " + this.pmeds; 
    } 
} 
+0

爲什麼不跟蹤初始化EMR類對象的類中的患者數,在您的情況下,它將是Studenthealthservices類? – nullPointer

+0

你想要有一個櫃檯來記錄已經創建了多少個EMR?但是您需要EMR課程中的計數器而不是創建它的計數器? –

+0

每個EMR構造函數中的numofpatients ++? – Divers

回答

1

在這裏看到(簡體類):

package studenthealthservices; 

public class EMR 
{ 
    private static /*volatile*/ long numofpatients; // when using different threads, volatile is needed. 

    private String name; 
    private String dob; 

    public EMR(String name, String dob) { 
     numofpatients++; //increment here   
     this.name = name; 
     this.dob = dob; 
    } 

    public EMR(String name, String dob, String rfv, double bodyt, double hr, String diag, String pmeds) { 
     numofpatients++; //increment here   
     this.name = name; 
     this.dob = dob; 
     //simplified 
    } 

    public String getDob() { 
     return dob; 
    } 

    public void setDob(String dob) { 
     this.dob = dob; 
    } 

    @Override 
    public String toString() { 
     return "simplified"; 
    } 

    public static long getInstanceCount() 
    { 
     return numofpatients; 
    } 
} 
1

我會避免在構造函數中增加一個static變量,而是將該責任推送到StudentHealthServices課程中,如下所示:

public class StudentHealthServices { 
    private int numOfPatients; 

    public static void main(String[] args) { 
     StudentHealthServices services = new StudentHealthServices(); 
     EMR p1 = new EMR("Colin", "10-22-74", "Strained Ankle", 99, 110, "hurt during football", "ankle brace"); 
     services.add(p1); 

     EMR p2 = new EMR("Anquan", "9-30-77", "stomach ache", 98, 120, "stress", "Tylenol"); 
     services.add(p2); 

     EMR p3 = new EMR("Buster", "3-27-1987", "Broken ankle", 99, 113, "Scott Cousins", "None"); 
     services.add(p3); 

     EMR p4 = new EMR("Frank The Tank", "4/1/89"); 
     services.add(p4); 

     EMR p5 = new EMR("Merton Hanks", "03-12-1968"); 
     services.add(p5); 
    } 

    private void add(EMR emr) { 
     // maybe you want to store the patients in a list here? 
     System.out.println(emr); 
     System.out.println("there are now " + ++numOfPatients + " patients"); 
    } 
}