Advertisement
gelatofu

LabExer3-4_Program.cs

Mar 29th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using MyDLL;
  8.  
  9. namespace StudentData
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             try
  16.             {
  17.                 Student stud = new Student();
  18.                 int choice, studnumber;
  19.                 string coursecode;
  20.                
  21.                 Console.WriteLine("====================");
  22.                 Console.WriteLine("======= MENU =======");
  23.                 Console.WriteLine("====================");
  24.                 Console.WriteLine("[1] Add Student\n[2] Display All\n[3] Display Student\n[4] Display Course");
  25.                 Console.WriteLine("====================");
  26.                 Console.Write("Enter your choice: ");
  27.  
  28.                 choice = Convert.ToInt32(Console.ReadLine());
  29.                 switch (choice)
  30.                 {
  31.                   case 1: EnrollStudent(stud);
  32.                         break;
  33.                   case 2: DisplayStudentProfile();
  34.                         break;
  35.                   case 3: Console.WriteLine("Enter student number: ");
  36.                           studnumber = Convert.ToInt32(Console.ReadLine());
  37.                         break;
  38.                   case 4: Console.WriteLine("Enter course code: ");
  39.                           coursecode = Convert.ToString(Console.ReadLine());
  40.                         break;
  41.                   default: Console.WriteLine("Invalid input. Please enter from numbers 1-4 only.");
  42.                         break;
  43.                  }
  44.             }
  45.             catch (FormatException e)
  46.             {
  47.                 Console.WriteLine(e.Message);
  48.             }
  49.            
  50.  
  51.             Console.Read();
  52.         }
  53.  
  54.         static void EnrollStudent(Student s)
  55.         {
  56.             Console.Clear();
  57.             try
  58.             {
  59.                 string path = @"D:\MCL\StudentsData\";
  60.                 path = string.Concat(path, ".txt");
  61.  
  62.                 using (StreamWriter sw = new StreamWriter(path))
  63.                 {
  64.                     Console.Write("Enter student number: ");
  65.                     s.StudNum = Convert.ToInt32(Console.ReadLine());
  66.                     sw.WriteLine(s.StudNum);
  67.  
  68.                     Console.Write("Enter full name of student: ");
  69.                     s.FullName = Console.ReadLine();
  70.                     sw.WriteLine(s.FullName);
  71.  
  72.                     Console.Write("Enter student program: ");
  73.                     s.StudProgram = Console.ReadLine();
  74.                     sw.WriteLine(s.StudProgram);
  75.  
  76.                     Console.Write("Enter father's name: ");
  77.                     s.FatherName = Console.ReadLine();
  78.                     sw.WriteLine(s.FatherName);
  79.  
  80.                     Console.Write("Enter mother's name: ");
  81.                     s.MotherName = Console.ReadLine();
  82.                     sw.WriteLine(s.MotherName);
  83.  
  84.                     Console.Write("Enter birthdate [yyyymmdd]: ");
  85.                     s.BirthDate = Convert.ToInt32(Console.ReadLine());
  86.                     sw.WriteLine(s.BirthDate);
  87.  
  88.                     for (int i = 0; i < s.StudCourse.Length; i++)
  89.                     {
  90.                         Console.Write("Enter Course {0}: ", i + 1);
  91.                         s.StudCourse[i] = Console.ReadLine();
  92.                         sw.WriteLine(s.StudCourse[i]);
  93.                     }
  94.  
  95.                     sw.Close();
  96.                 }
  97.             }
  98.             catch (IOException e)
  99.             {
  100.                 Console.WriteLine(e.Message);
  101.             }
  102.         }
  103.  
  104.         public static void DisplayStudentProfile()
  105.         {
  106.             try
  107.             {
  108.                 string path = @"D:\MCL\StudentsData\";
  109.                 path = string.Concat(path, ".txt");
  110.  
  111.                 string[] stud = File.ReadAllLines(path);
  112.                 Student s = new Student();
  113.  
  114.                 s.FullName = (string)stud.GetValue(0);
  115.                 for (int i = 0; i < stud.Length - 6; i++)
  116.                 {
  117.                     s.StudCourse[i] = (string)stud.GetValue(i + 6);
  118.                 }
  119.  
  120.                 foreach (string data in stud)
  121.                 {
  122.                     Console.WriteLine(data);
  123.                 }
  124.             }
  125.             catch (IOException e)
  126.             {
  127.                 Console.WriteLine(e.Message);
  128.             }
  129.             catch (IndexOutOfRangeException e)
  130.             {
  131.                 Console.WriteLine(e.Message);
  132.             }
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement