Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Work2
- {
- class Student
- {
- public string name;
- public DateTime birthDate;
- public string course;
- public int grade;
- public Student(string name, DateTime birthDate, string course, int grade)
- {
- this.name = name;
- this.birthDate = birthDate;
- this.course = course;
- this.grade = grade;
- }
- }
- }
- using System;
- using System.Collections;
- namespace Work2
- {
- class Program
- {
- static ArrayList list = new ArrayList();
- static void Main(string[] args)
- {
- DateTime bd;
- string name, course;
- int grade, choice;
- do
- {
- Console.Write("Name: ");
- name = Console.ReadLine();
- Console.Write("Date of birth: ");
- bd = Convert.ToDateTime(Console.ReadLine());
- Console.Write("Choose course:\t C# Programming...1\t Software Development...2 ");
- if (Console.ReadLine().Equals("1")) course = "C# Programming";
- else course = "Software Development";
- Console.Write("Grade: ");
- grade = Convert.ToInt32(Console.ReadLine());
- Student student = new Student(name, bd, course, grade);
- list.Add(student);
- Console.WriteLine("Add a new student?\tYes...1 \t No...0");
- } while (Convert.ToInt32(Console.ReadLine()) != 0);
- do
- {
- Console.WriteLine("\nSelect an action:\nFind a student...1\nDisplay C# Programming course students...2\nDisplay the list...3\nRemove a student...4\nExit...0");
- choice = Convert.ToInt32(Console.ReadLine());
- switch (choice)
- {
- case 1:
- Console.Write("Enter name: ");
- if (isOnList(Console.ReadLine())) Console.WriteLine("The student is on the list.");
- else Console.WriteLine("The student is not on the list!");
- break;
- case 2:
- Console.WriteLine("C# Programming course students: ");
- DisplayCSStudents();
- break;
- case 3:
- DisplayAll();
- break;
- case 4:
- Console.Write("Enter name: ");
- name = Console.ReadLine();
- Remove(name);
- break;
- }
- } while(choice != 0);
- }
- static bool isOnList(string name)
- {
- bool found = false;
- foreach (Student student in list)
- {
- if (student.name.Equals(name))
- {
- found = true;
- break;
- }
- }
- return found;
- }
- static void DisplayCSStudents()
- {
- foreach (Student student in list)
- {
- if (student.course.Equals("C# Programming"))
- {
- Console.WriteLine("Name: " + student.name);
- Console.WriteLine("Grade: " + student.grade + "\n");
- }
- }
- }
- static void Sort()
- {
- string[] names = new string[list.Count];
- int i = 0; //index in string array
- int lIndex; //index in list
- Student s;
- foreach (Student student in list) //copying of names from the arraylist
- {
- names[i] = student.name;
- i++;
- }
- Array.Sort(names); //sorting of names
- i = 0;
- for (lIndex = 0; lIndex < list.Count; lIndex++ ) //sorting of objects in the arraylist
- {
- s = (Student)list[lIndex];
- while (!s.name.Equals(names[i])) i++;
- s = (Student)list[i];
- list[i] = list[lIndex];
- list[lIndex] = s;
- i = 0;
- }
- }
- static void DisplayAll()
- {
- Sort();
- Console.WriteLine("Students in the list: ");
- foreach (Student student in list)
- {
- Console.WriteLine("Name: " + student.name);
- Console.WriteLine("Date of birth: " + student.birthDate);
- Console.WriteLine("Course: " + student.course);
- Console.WriteLine("Grade: " + student.grade + "\n");
- }
- }
- static void Remove(string name)
- {
- if (!isOnList(name)) Console.WriteLine("The student is not on the list!");
- else
- {
- foreach (Student student in list)
- {
- if (student.name.Equals(name))
- {
- list.Remove(student);
- break;
- }
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment