Vrijman
Vrijman

Reputation: 29

Should I create one list or several?

Basic console-interface app that allows user to add/search information based on the following classes:

College.cs (Program.cs) 
Person.cs 
Student.cs
Staff.cs
Lecturer.cs

Staff and Studentboth inherit functionality from Person

First step was to define Properties, Constructors for Person as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StudentApp
{
    class Person
    {

        //Auto-implemented properties
        public int PpsnNo { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string Address { get; set; }

        public string Phone { get; set; }

        public string Email { get; set; }


        //Constructors 
        public Person() { }
        public Person(int ppsn, string first, string last, string address, string phone, string email)
        {

            PpsnNo = ppsn;
            FirstName = first;
            LastName = last;
            Address = address;
            Phone = phone;
            Email = email;

        }

        //Method
        public override string ToString()
        {

        }

    }
}

Next, I have started by creating a List in the Student class, as follows (I know there are unnecessary/redundant Methods, it's really just a question of where to store the list) :

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ICollection_Example
{
    class Student :ICollection<Person>
    {
        //property
        private List<Person> StudentList;

        //Constructor
        public Person()
        {
            StudentList = new List<Person>();
        }

        #region interface Properties
        public int Count { get; }

        public bool IsReadOnly
        {
            get { return false; }
        }
        #endregion

        #region Interface Methods
        public void Add(Person book)
        {
            StudentList.Add(book);
        }

        public void Clear()
        {
            StudentList.Clear();
        }

        public bool Contains(Person book)
        {
            return StudentList.Contains(book);
        }
        public bool Remove(Book book)
        {
            return StudentList.Remove(book);
        }
        public void CopyTo(Person[] array, int index)
        {
            StudentList.CopyTo(array, index);
        }
        public IEnumerator<Person> GetEnumerator()
        {
            return StudentList.GetEnumerator();
        }

       IEnumerator IEnumerable.GetEnumerator()
        {
            return StudentList.GetEnumerator();
        }

        #endregion

        #region Student Methods
        public bool Remove(string ppsno)
        {
            bool flag = false;
            for(int i = 0; i < StudentList.Count && flag==false; i++)
            {
                if(StudentList[i].Ppsno == ppsno)
                {
                    StudentList.Remove(StudentList[i]);
                    flag = true;
                }
            }
            return flag;
        }

        #endregion
    }
}

Is it necessary to create such a list again for the Staff class or does it make more sense to create a single list to be stored elsewhere (possibly in Person?) and then referenced in the classes that inherit another class' functionality - in this case, would Student and Staff reference a list stored in Person?

Upvotes: 2

Views: 65

Answers (1)

Aaron M. Eshbach
Aaron M. Eshbach

Reputation: 6510

I think you are conflating the responsibility of an individual Student or Staff, which is to extend Person and add role-specific functionality, with the responsibility of the College (based on your list of classes), which is to house the various people and facilities and provide a means for them to interact. I would suggest having Student directly inherit from Person, and having your College class have multiple collections, one for each type of person. If you then want a list of all people in the college, you can add a method to the College class that returns IEnumerable<Person> and concatenates the collections of Students, Staff, etc.

Upvotes: 3

Related Questions