0

So I want to find the oldest person in a school class. The age is calculated according to his personal code. For example, 120499-12345, where the first part is the date and the first number after the "-" can be 1 or 2,depending on when did person born(before 2000 - "1", after 2000 - "2"). Personal code is a string type and I used substrings to get the year from code and the 1 or 2 to calculate the age. And the problem is that I don't really understand how to find the oldest person.

        public void Oldest()
        {

            int age = 0;

                foreach (Student b in students)// students is an array of Student class
                {
                    string sub1 = b.pers_code.Substring(4, 2);
                    string sub2 = b.pers_code.Substring(7, 1);
                    int type = 0;
                    type = Convert.ToInt32(sub2);
                    int year = 0;
                    year = Convert.ToInt32(sub1);




                    if (type == 2)
                    {
                        age = 18 - year;


                    }

                    else
                    {
                        age = 2018 - (year + 1900);

                    }


                }    
        }
5
  • So the 120499-12345 is meaning that the person was born at 04 December 1999? Commented Apr 2, 2018 at 11:19
  • 1
    year is always zero in your code! Explain by a complete example how to get the age for a string code! Commented Apr 2, 2018 at 11:22
  • I assume 'gads' means 'age' in some langugae and we see an incomplete translation. Commented Apr 2, 2018 at 11:28
  • Makes no sense to me. Why are you picking up the 99? VTC Commented Apr 2, 2018 at 11:40
  • 120499 is the date. What is the significance of this date? Without a clear description of how you go from 120499 to an age, nobody can help you. Commented Apr 2, 2018 at 14:50

1 Answer 1

3

I don't really understand how to find the oldest person

Sure, there is too much going on in your method. Instead of trying to fix it, you can take a different approach and improve the general design first.


You can split your i'm-responsible-for-everything method into single-focused functions:

DateTime GetBirthDate(string personCode) { ... }

TimeSpan GetAge(DateTime birthDate) { ... }

Now you can test this logic separately.

Come to think of it, it actually seems the Student's responsibility to know his or her age.

class Student
{
    ...
    // instead of computing, you can set these once in the constructor
    public DateTime BirthDay => GetBirthDay(this.PersonalCode);
    public TimeSpan Age => GetAge(this.BirthDay);
    ...
    private TimeSpan GetAge(DateTime birthDate) { ... }
}

And then you can assemble your simple and testable building blocks into a larger solution

var oldest = students.OrderByDescending(s => s.Age).FirstOrDefault();
var maxAge = students.Max(s => s.Age);

It all got much clearer, not to mention that we're now able to easily find other stats -- average age, top-10 youngest students etc.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.