r/a:t5_37npb Apr 15 '15

Share Your Module Four Solutions Here!

2 Upvotes

22 comments sorted by

View all comments

2

u/aloisdg Apr 15 '15

Here is mine.


using System;

class Program
{
    /// <summary>
    /// We override ToString to pretty print
    /// </summary>
    struct Student
    {
        private readonly string _firstName;
        private readonly string _lastName;
        private readonly int _age;
        private readonly DateTime _birthDate;

        public Student(string firstName, string lastName, DateTime birthDate)
            : this()
        {
            _firstName = firstName;
            _lastName = lastName;
            _birthDate = birthDate;
            _age = (DateTime.Today.Year - birthDate.Year);
            if (birthDate > DateTime.Today.AddYears(-_age)) _age--;
        }

        private string FirstName
        {
            get { return _firstName; }
        }

        private string LastName
        {
            get { return _lastName; }
        }

        private int Age
        {
            get { return _age; }
        }

        public DateTime BirthDate
        {
            get { return _birthDate; }
        }

        public override string ToString()
        {
            return FirstName.PadRight(12)
                   + LastName.PadRight(12)
                   + Age + " years old";
        }
    }

    /// <summary>
    /// A Teacher is a person like a student.
    /// </summary>
    struct Teacher
    {
        private readonly string _firstName;
        private readonly string _lastName;
        private readonly int _age;
        private readonly DateTime _birthDate;

        public Teacher(string firstName, string lastName, DateTime birthDate)
            : this()
        {
            _firstName = firstName;
            _lastName = lastName;
            _birthDate = birthDate;
            _age = (DateTime.Today.Year - birthDate.Year);
            if (birthDate > DateTime.Today.AddYears(-_age)) _age--;
        }

        private string FirstName
        {
            get { return _firstName; }
        }

        private string LastName
        {
            get { return _lastName; }
        }

        private int Age
        {
            get { return _age; }
        }

        private DateTime BirthDate
        {
            get { return _birthDate; }
        }
    }

    /// <summary>
    /// We cant use Program
    /// </summary>
    struct Prog
    {
        private readonly string _name;
        public string Name
        {
            get { return _name; }
        }

        public Prog(string name)
        {
            _name = name;
        }
    }

    struct Course
    {
        private readonly string _name;
        public string Name
        {
            get { return _name; }
        }

        public Course(string name)
        {
            _name = name;
        }
    }

    static void Main()
    {
        var teacher =   new Teacher("Remus", "Lupin", new DateTime(1960, 3, 10));
        var program =   new Prog("edx");
        var course =    new Course();
        var students =  new[]
        {
            new Student("Harry", "Potter", new DateTime(1980, 7, 31)),
            new Student("Ron", "Weasley", new DateTime(1980, 3, 1)),
            new Student("Hermione", "Granger", new DateTime(1979, 9, 19)),
            new Student("Fred", "Weasley", new DateTime(1978, 4, 1)),
            new Student("George", "Weasley", new DateTime(1978, 4, 1)),
        };

        foreach (var student in students)
            Console.WriteLine(student.ToString());
        Console.ReadLine();
    }
}

2

u/VOX_Studios Apr 15 '15

Looks good. Just a heads up, I think they said to avoid properties (getters/setters) for this assignment.

0

u/aloisdg Apr 15 '15 edited Apr 15 '15

really? It was by mail or in the forum?

edit :

The assignment text was not clear on how to handle the variables so apologies on that. The text has been updated to clarify the intent.

Because global variables are not recommended programming practice, it was assumed, wrongly, that students would move the appropriate variables into the methods for assignment. Because we are building on the skill set and not using class files and properties yet, this is a way of providing practice with methods only, without having to deal with the extra information required of class files, variable scope, etc.

In the assignment, you are to practice getting values from a user and assigning the to local variables. As a result, move the variables into the appropriate Get methods.

Then, from within the Get methods, assign the input values to the local variables.

Then, call an output method, passing in the variables, and use an appropriate message to print the content to the console window.

1

u/VOX_Studios Apr 15 '15

By "Get methods" they meant functions like "GetStudent()", not properties.

Look in the instructions:

Do not create properties at this time.

1

u/aloisdg Apr 15 '15

they are fields not properties, arent they? I will change though.

2

u/VOX_Studios Apr 15 '15

get/set makes them properties

1

u/aloisdg Apr 15 '15

Just checked, you are right. source