Click here to Skip to main content
15,917,565 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Program.cs
C#
using LM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LM
{
    class Program
    {
        static void Main(string[] args)
        {
            var students = new List<student>();

            var newStudent1 = new Student();
            newStudent1.Id = 0;
            newStudent1.Name = "Bob";
            newStudent1.DateOfBirth = DateTime.Now;
            newStudent1.IsEnrolled = false;

            var newStudent2 = new Student(1, "Maria", DateTime.Now, true);

            var newStudent3 = new Student
            {
                Id = 2,
                Name = "John",
                DateOfBirth = DateTime.Now,
                IsEnrolled = false
            };

            students.Add(newStudent1);
            students.Add(newStudent2);
            students.Add(newStudent3);

            // ------------------------------------------

            var choice = "";
            while (choice != "0")
            {
                Console.WriteLine("Choose an option:");
                Console.WriteLine("1: List all students");
                Console.WriteLine("2: Add student");
                Console.WriteLine("3: Remove student");
                Console.WriteLine("0: Exit");
                Console.WriteLine("---------------------------------------------");

                choice = Console.ReadLine();

                switch (choice)
                {
                    case "1":
                        // List all students
                        foreach(var s in students)
                        {
                            Console.WriteLine(s.Name);
                        }
                        break;

                    case "2":
                        // Add student
                        break;

                    case "3":
                        // Remove student
                        break;

                }
            }
        }
    }
}


Student.cs
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LM
{
    class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime DateOfBirth { get; set; }
        public bool IsEnrolled { get; set; }
        public List<string> Courses { get; set; }

        public Student()
        {
        }

        public Student(int id, string name, DateTime dob, bool enrolled)
        {
            Id = id;
            Name = name;
            DateOfBirth = dob;
            IsEnrolled = enrolled;
        }

        public void Register(string course)
        {
            Courses.Add(course);
        }

        public void Drop(string course)
        {
            Courses.Remove(course);
        }
    }
}
Posted
Updated 21-Jan-15 16:56pm
v2
Comments
DamithSL 21-Jan-15 23:00pm    
what is the problem?
George Jonsson 21-Jan-15 23:13pm    
Is this your first homework in your programming class?
TheRealSteveJudge 22-Jan-15 10:45am    
@Member 11393003: Please have a look at solution 2. Is it helpful?

here is your solution:


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

namespace LM
{
    class Program
    {
        static void Main(string[] args)
        {
            var students = new List<Student>();

            var newStudent1 = new Student();
            newStudent1.Id = 0;
            newStudent1.Name = "Bob";
            newStudent1.DateOfBirth = DateTime.Now;
            newStudent1.IsEnrolled = false;

            var newStudent2 = new Student(1, "Maria", DateTime.Now, true);

            var newStudent3 = new Student
            {
                Id = 2,
                Name = "John",
                DateOfBirth = DateTime.Now,
                IsEnrolled = false
            };

            students.Add(newStudent1);
            students.Add(newStudent2);
            students.Add(newStudent3);

            // ------------------------------------------

            var choice = "";
            while (choice != "0")
            {
                Console.WriteLine("Choose an option:");
                Console.WriteLine("1: List all students");
                Console.WriteLine("2: Add student");
                Console.WriteLine("3: Remove student");
                Console.WriteLine("4: Get Details of any student");
                Console.WriteLine("0: Exit");
                Console.WriteLine("---------------------------------------------");

                choice = Console.ReadLine();

                switch (choice)
                {
                    case "1":
                        // List all students
                        foreach (var s in students)
                        {
                            Console.WriteLine(s.Name);
                        }
                        break;

                    case "2":
                        // Add student
                        break;

                    case "3":
                        // Remove student
                        break;

                    case "4":
                        // Details of student
                        int counter = 1;
                        foreach (var s in students)
                        {
                            Console.WriteLine(counter++ + ": "+s.Name);
                        }
                        Console.WriteLine("Enter your choice- ");
                        int option = Convert.ToInt32(Console.ReadLine());
                        counter = 1;
                        foreach (var s in students)
                        {
                            if (counter == option)
                            {
                                Console.WriteLine("Id = " + s.Id);
                                Console.WriteLine("Name = " + s.Name);
                                Console.WriteLine("DOB = " + s.DateOfBirth);
                                Console.WriteLine("Enrollement Status = " + s.IsEnrolled);
                                Console.WriteLine("\n\n");
                            }
                            counter++;
                        }
                        break;

                }
            }
        }
    }
}
 
Share this answer
 
Normally we do not do your homework.
But you showed us what you already achieved.
Program.cs
C#
using System;
using System.Collections.Generic;
using System.Linq;

namespace LM
{
    class Program
    {
        static void Main(string[] args)
        {
            var students = new List<Student>();

            var newStudent1 = new Student();
            newStudent1.Id = 0;
            newStudent1.Name = "Bob";
            newStudent1.DateOfBirth = DateTime.Now;
            newStudent1.IsEnrolled = false;
            newStudent1.Courses.Add("Mathematics");
            newStudent1.Courses.Add("Programming");

            var newStudent2 = new Student(1, "Maria", DateTime.Now, true);

            var newStudent3 = new Student
            {
                Id = 2,
                Name = "John",
                DateOfBirth = DateTime.Now,
                IsEnrolled = false
            };

            students.Add(newStudent1);
            students.Add(newStudent2);
            students.Add(newStudent3);

            // ------------------------------------------

            var choice = "";
            while (choice != "0")
            {
                Console.WriteLine("Choose an option:");
                Console.WriteLine("1: List all students");
                Console.WriteLine("2: Add student");
                Console.WriteLine("3: Remove student");
                Console.WriteLine("0: Exit");
                Console.WriteLine("---------------------------------------------");

                choice = Console.ReadLine();

                switch (choice)
                {
                    case "1":
                        // List all students
                        foreach (var s in students)
                        {
                            Console.WriteLine(s.Id + "\t" + s.Name);
                        }

                        Console.WriteLine("Type ID for details. x for Exit");

                        string detailsChoice = Console.ReadLine();

                        switch (choice)
                        {
                            case "x":
                                break;

                            default:
                                var results = from x in students
                                              where x.Id.ToString() == detailsChoice
                                              select x;

                                var selectedStudent = results.FirstOrDefault();

                                Console.WriteLine(selectedStudent.Id);
                                Console.WriteLine(selectedStudent.Name);
                                Console.WriteLine(selectedStudent.DateOfBirth.ToShortDateString());
                                Console.WriteLine(selectedStudent.IsEnrolled.ToString());
                                foreach(string course in selectedStudent.Courses)
                                {
                                    Console.WriteLine(course);
                                }
                                break;
                        }

                        break;

                    case "2":
                        // Add student
                        break;

                    case "3":
                        // Remove student
                        break;

                    default:
                        break;
                }
            }

        }
    }
}

Student.cs
C#
using System;
using System.Collections.Generic;

namespace LM
{
    class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime DateOfBirth { get; set; }
        public bool IsEnrolled { get; set; }
        public List<string> Courses { get; set; }

        public Student()
        {
            Courses = new List<string>();
        }

        public Student(int id, string name, DateTime dob, bool enrolled)
        {
            Id = id;
            Name = name;
            DateOfBirth = dob;
            IsEnrolled = enrolled;
        }

        public void Register(string course)
        {
            Courses.Add(course);
        }

        public void Drop(string course)
        {
            Courses.Remove(course);
        }
    }
}</string></string>


Let me also give some hints:
In Students.cs you should use
C#
Courses = new List<string>();</string>

Moreover the switch statement in Program.cs also needs a default value.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900