2

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?

Vrijman
  • 29
  • 4
  • Possible duplicate of [Why not inherit from List?](https://stackoverflow.com/questions/21692193/why-not-inherit-from-listt) – Sinatr Feb 08 '18 at 13:17
  • 1
    `Student` is not a `List`. Rather add a property `List` to `College`. – Sinatr Feb 08 '18 at 13:18
  • Wait what? `class Student :ICollection`and `private List StudentList;` ? Is you student definition actually taking in account multiple personality? – Drag and Drop Feb 08 '18 at 13:21
  • I think you should use the [Liskov substitution principle](https://en.wikipedia.org/wiki/Liskov_substitution_principle) of SOLID here. – Alexander Feb 08 '18 at 17:58

1 Answers1

3

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.

Aaron M. Eshbach
  • 6,380
  • 12
  • 22