Student Information Classes and Objects



Student Information

This is an exmaple of a C# program that uses multiple classes to collect and display student information to the console.

Here's the 2 classes!

createstudent.cs

                            using System;

                            namespace StudentInformation
                            {
                                class CreateStudents
                                {
                                    static void Main(string[] args)
                                    {
                                        Student[] students = new Student[2];
                                        int x;
                                        int id;
                                        string name;
                                        double gpa;
                                        for ( x = 0; x < students.Length; x++)
                                        {
                                            GetData(out id, out name, out gpa);
                                            students[x] = new Student(id, name, gpa);
                                        }
                                        Array.Sort(students);
                                        Console.WriteLine("Sorted List:");
                                        for (x = 0; x < students.Length; x++)
                                        {
                                            Display(students[x]);
                                        }
                                    }

                                    public static void GetData(out int id, out string name, out double gpa)
                                    {
                                        string inString;
                                        Console.Write("Please enter student ID Number >> ");
                                        inString = Console.ReadLine();
                                        int.TryParse(inString, out id);
                                        Console.Write("Please enter last name for " + "student {0} >> ", id);
                                        name = Console.ReadLine();
                                        Console.Write("Please enter grade point average >> ");
                                        inString = Console.ReadLine();
                                        double.TryParse(inString, out gpa);
                                    }

                                    public static void Display(Student stu)
                                    {
                                        Console.WriteLine("{0,5} {1,-10}{2,6}", stu.IdNumber, stu.LastName, stu.GradePointAverage.ToString("F1"));
                                        Console.ReadLine();
                                    }
                                }
                            }
                        

student.cs

                            using System;
                            namespace StudentInformation
                            {
                                class Student : IComparable
                                {
                                    private int idNumber;
                                    private string lastName;
                                    private double gradePointAverage;

                                    public const double HIGHEST_GPA = 5.0;
                                    public const double LOWEST_GPA = 0.0;

                                    public int IdNumber
                                    {
                                        get
                                        {
                                            return idNumber;
                                        }
                                        set
                                        {
                                            idNumber = value;
                                        }
                                    }
                                    public string LastName
                                    {
                                        get
                                        {
                                            return lastName;
                                        }
                                        set
                                        {
                                            lastName = value;
                                        }
                                    }
                                    public double GradePointAverage
                                    {
                                        get
                                        {
                                            return gradePointAverage;
                                        }
                                        set
                                        {
                                            if (value >= LOWEST_GPA && value <= HIGHEST_GPA)
                                            {
                                                gradePointAverage = value;
                                            }
                                            else
                                            {
                                                gradePointAverage = LOWEST_GPA;
                                            }
                                        }
                                    }
                                    public Student(int id, string name, double gpa)
                                    {
                                        IdNumber = id;
                                        LastName = name;
                                        GradePointAverage = gpa;
                                    }
                                    public Student()
                                        : this(0, "XXX", 0.0)
                                    {

                                    }
                                    int IComparable.CompareTo(Object o)
                                    {
                                        int returnVal;
                                        Student temp = (Student)o;
                                        if (this.IdNumber > temp.IdNumber)
                                            returnVal = 1;
                                        else
                                            if (this.IdNumber < temp.IdNumber)
                                                returnVal = -1;
                                            else
                                                returnVal = 0;
                                        return returnVal;
                                    }
                                }
                            }