我可以在C#中的数组列表中记录2件事吗?

时间:2015-10-15 20:16:08

标签: c# arrays class arraylist

对于我的作业,我不得不在C#中创建一个程序,该程序有一个名为" patient"的类,如下:

class patient
{
    private string name;
    private int age;
    private double weight;
    private double height;

    public patient()
    {
        name = "";
        age = 0;
        weight = 0;
        height = 0;
    }

    public patient(string newName, int newAge, double newWeight, double newHeight)
    {
        name = newName;
        age = newAge;
        weight = newWeight;
        height = newHeight;
    }

    public double bmi()
    {
        return weight / Math.Pow(height, 2);
    }

    public bool obese()
    {
        if (bmi() > 27 && age < 40)
            return true;
        else if (bmi() > 30 && age >= 40)
            return true;
        else
            return false;
    }

    public void printDetails()
    {
        Console.WriteLine("Name: " + name);
        Console.WriteLine("Age: " + age);
        Console.WriteLine("Weight: " + weight + "kg");
        Console.WriteLine("Height: " + height + "m");
        Console.WriteLine("BMI: " + bmi());
        if (obese())
            Console.WriteLine("Patient is obese");
        else
            Console.WriteLine("Patient is not obese.");
    }

问题的最后部分说: 编写一种方法,将最近输入的患者及其肥胖症诊断记录到ArrayList中。它应记录最近的五个条目及其诊断。

阵列列表不能是多维的,但问题是让我记录肥胖症诊断和实际患者。

我曾考虑将对象存储在数组列表中,但我不确定这是否是问题所在。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

我会这样做:

// you will have a history of Records
// a Record contains the Patient + obesity result
public class Record 
{
   public Patient Patient {get; private set;} 
   public bool ObesityResult {get; private set; }
   public Record(Patient patient, bool obesityResult) 
   {
       this.Patient = patient; 
       this.ObesityResult = obesityResult; // save the obese result
   }
}


// now this class will handle the history.
public class RecordHistory 
{
    private ArrayList history; 

    public void Add(Patient patient) 
    { 
        var record = new Record(patient, patient.obese());  // pass the obesity result
        history.Add(patient);  // DO some magic here to keep only 5
    }

   public ArrayList GetHistory() 
   {
      return history;
   }
}

请不要理解你应该如何做现实生活中的例子。这只是一项家庭作业。