0

I'm trying to create a system that allows you to create a hospital ward and then add patients with personal details to each ward. The intention is being able to read all information from a single text/csv file.

I currently have the code to create patients and add their details but creating a ward and assigning patients to the ward is beyond my grasp. My lecturer suggested using an array of Patients as a property in the Ward class but its beyond my comprehension at the moment. A typical Patient would be written in my textfile as ''JoeBloggs,28,03/03/1989,8'' And the below code sorts the attributes etc.

Here is my current code to create patients:

 private void btnLoad_Click(object sender, RoutedEventArgs e)
    {
        //read files from file
        string[] patientList = File.ReadAllLines("MyList.txt");
        string[] patientArray = new string[4];
        string patientLong, name;
        Patient patient;
        int age, blood;
        DateTime dob;
        .

        for (int i = 0; i < patientList.Length; i++)
        {
            patientLong = patientList[i];//one long string with commas eg johnmurphy,22,12/9/1998,6
            patientArray = patientLong.Split(',');//split array according to comma
            name = patientArray[0];
            age = Convert.ToInt32(patientArray[1]);
            dob = DateTime.Parse(patientArray[2]);
            blood = Convert.ToInt32(patientArray[3]);


            patient = new Patient(name,age,dob,blood);
            bookList.Add(patient);

        }

       // lbxExpenses.ItemsSource = null;
        lstPatients.ItemsSource = patientList;
    }

And class Ward

class Ward
{
    public string Name { get; set; }

    public Patient [] Patient { get; set; }//array of patients as a property?

    public Ward(string name,int patient )
    {
        Name = name;
        Patient = new int[i];//currently giving error
    }

    public string GetWardDetails()
    {
        return string.Format("{0}", Name);
    }
}
Ger Mc
  • 630
  • 3
  • 11
  • 22
  • 1
    `Patient = new int[i];` is wrong with `public Patient [] Patient { get; set; }`. You have to instantiate an array of `Patient` objects. Also `i` does not exist in the current context. The correct way to do this would be to use a `List` to which you can dynamically add patient objects. (i.e use `System.Collections.Generic` namespace). Have a look at `https://www.dotnetperls.com/list` if you want to use it. The other possibility would be to allocate an array of fixed length, say, 10 patients, which you could do with `Patients = new Patient[10];` (also rename the prop) – Maximilian Gerhardt Mar 06 '17 at 10:02
  • Ok, I think I know what you mean but I dont quite understand.Im currently using System.Collections.ObjectModel in my mainwaindow. – Ger Mc Mar 06 '17 at 10:05
  • 1
    Can you be more specific what you try to archieve? Is it about __creating__ a ward and __adding__ patients or is it about reading the CSV-File with predefined relations and __displaying__ the ward with associated patients? – grek40 Mar 06 '17 at 10:07
  • Ideally I should be able to read predefined testdata from CSV but also be able to add data to the CSV. I'm ok with adding data to a CSV file in my program but just trying to grasp how I can create a Ward object and then create many Patient objects inside each Ward is troubling me. This is how I want it to work : Create Ward1 --> Create Patient A, Create PatientB etcetc CreateWard2--> PatientC etc – Ger Mc Mar 06 '17 at 10:15
  • Anyways, **what is the question**? Are you asking what the lecturer meant by "but creating a ward and assigning patients to the ward"? – quetzalcoatl Mar 06 '17 at 10:24
  • No, I'm trying to establish how Id create a Ward object insieof which i can create many Patient objects with everything being stored in a CSV file. I I'm thinking the format of the CSV would be similar to : EmergencyWard,JoeBloggs,28,10/03/1989,bloodtypeA – Ger Mc Mar 06 '17 at 10:30
  • I'm voting to close, because the question is to broad. You shouldn't have a combined question about displaying, creating and turning into CSV. If you are just interested in one part, make sure to remove the other aspects from the question. For example, work with some predefined data in code. – grek40 Mar 06 '17 at 10:37

1 Answers1

2

The solution isn't to use an array, but instead, to use a generic list.

class Ward
{
    public string Name { get; set; }

    public List<Patient> Patients { get; set; }//array of patients as a property?

    public Ward(string name)
    {
        Name = name;
        Patients = new List<Patient>();
    }

    public string GetWardDetails()
    {
        return string.Format("{0}", Name);
    }
}

Now, when you would like to add or remove a Patient from the ward, you would do this:

Ward ward = new Ward("Emergency");
ward.Patients.Add(/*a valid patient object*/);

If you wanted to remove

ward.Patients.Remove(/*An already-existing patient object in the ward*/);

Some more about Lists can be found here. The problem is that arrays don't resize, while Lists do. So when you're reading a file, you can add new patients to the ward whenever you feel like. Also when you're instantiating a new object, you want to instantiate that object as its type. When you did = new int[0], you were telling it to assign type Patient to be an int, when it does not have the fields/properties in it to possibly be of type int. That's not to say you'll never assign one type to a different one, but you'll generally only do that if you want to achieve polymorphism.

If you wished to continue to use an array, you would have to do something along the lines of this:

class Ward
{
    public string Name { get; set; }

    public Patient[] Patients { get; private set; }

    public Ward(string name, IEnumerable<Patient> Patients)
    {
        Name = name;
        this.Patients = Patients.ToArray();
    }

    public Ward(string name, Patient[] Patients)
    {
        Name = name;
        Patients = Patients;
    }

    public string GetWardDetails()
    {
        return string.Format("{0}", Name);
    }
}

Cheers mate :)

Community
  • 1
  • 1
Rachael Dawn
  • 819
  • 6
  • 13
  • Shouldn't you maybe abstract the "adding" and "removing" of patients into a method in the Ward class? In case at some point he decides to change from Lists to something different... – Enrique Moreno Tent Mar 06 '17 at 10:17
  • You could, most certainly, but that's a decision he'd have to make. When you do that though, you'd be best off setting Patients to = { get; private set; } – Rachael Dawn Mar 06 '17 at 10:18
  • Why? What is the point of setting accessors? – Enrique Moreno Tent Mar 06 '17 at 10:21
  • He's using WPF, which means he may like to use it as a data source, binding it to a DependencyProperty or something of the like. – Rachael Dawn Mar 06 '17 at 10:23
  • Regarding WPF, I'd recommend `ObservableCollection` instead of `List` – grek40 Mar 06 '17 at 10:35
  • If i were to use an array how would it be done? Having the list be a pre defined size is in the criteria for this assignment and my lecturer specifically showed us that we should connect the Ward and patient class using array like in my original post. he didn't elaborate beyond the public Patient [] Patient { get; set; } property part however. – Ger Mc Mar 06 '17 at 10:38
  • Updated code for your comment. But, when it comes to pre-defined sizes, are you wanting the Ward to only be able to support X number of Patient objects, total, and add/remove them on the go? – Rachael Dawn Mar 06 '17 at 10:42