Advertisement
IvanNikolov2217

rad 5

May 19th, 2022
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 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 BusinessLayer;
  7.  
  8. namespace Client
  9. {
  10. class Program
  11. {
  12. static Dictionary<Type, object> recievedOperation = new Dictionary<Type, object>();
  13. static Dictionary<Type, object> recievedData = new Dictionary<Type, object>();
  14. static BinaryMessage operationToSend = new BinaryMessage();
  15. static BinaryMessage dataToSend = new BinaryMessage();
  16.  
  17. static void Main(string[] args)
  18. {
  19. try
  20. {
  21. ClientManager.InitializeClient();
  22.  
  23. do
  24. {
  25. ShowMenu();
  26.  
  27. if (!ClientManager.CommunicationIsActive)
  28. {
  29. break;
  30. }
  31.  
  32. } while (ClientManager.ContinueCommunicating());
  33.  
  34. EndCommunication();
  35.  
  36. Console.WriteLine("Press any key to close the client API!");
  37. Console.ReadKey();
  38. }
  39. catch (Exception ex)
  40. {
  41. Console.WriteLine(ex.Message);
  42. Console.WriteLine("Press any key to close the program");
  43. Console.ReadKey();
  44. }
  45. finally
  46. {
  47. ClientManager.CloseConnection();
  48. }
  49. }
  50.  
  51. private static void ShowMenu()
  52. {
  53. Console.WriteLine(Environment.NewLine);
  54. Console.WriteLine("Select option:");
  55. Console.WriteLine("1) Create Student");
  56. Console.WriteLine("2) Create Classroom");
  57. Console.WriteLine("3) View Students");
  58. Console.WriteLine("4) View Classrooms");
  59. Console.WriteLine("5) Exit");
  60.  
  61. Console.Write("Your choice: ");
  62. int choice = Convert.ToInt32(Console.ReadLine());
  63.  
  64. switch (choice)
  65. {
  66. case 1: CreateStudent(); break;
  67. case 2: CreateClassroom(); break;
  68. case 3: ViewStudents(); break;
  69. case 4: ViewClassrooms(); break;
  70. case 5: ClientManager.CommunicationIsActive = false; break;
  71. default: throw new ArgumentException("Invalid option!");
  72. }
  73. }
  74.  
  75. private static void CreateStudent()
  76. {
  77. Console.Write("Name: ");
  78. string name = Console.ReadLine();
  79.  
  80. Console.Write("Age: ");
  81. int age = Convert.ToInt32(Console.ReadLine());
  82.  
  83. Console.Write("Points: ");
  84. List<int> points = Console.ReadLine().Split(' ').Select(int.Parse).ToList();
  85.  
  86. Student student = new Student(name, age ,points);
  87.  
  88. operationToSend = TransformDataManager.Serialize(1);
  89. dataToSend = TransformDataManager.Serialize(student);
  90.  
  91. ClientManager.SendMessage(operationToSend);
  92. ClientManager.SendMessage(dataToSend);
  93. }
  94.  
  95. private static void CreateClassroom()
  96. {
  97. Console.Write("Name: ");
  98. string name = Console.ReadLine();
  99.  
  100. Console.Write("Subject: ");
  101. string subject = Console.ReadLine();
  102.  
  103.  
  104.  
  105. Classroom classroom= new Classroom(name, subject);
  106.  
  107. operationToSend = TransformDataManager.Serialize(2);
  108. dataToSend = TransformDataManager.Serialize(classroom);
  109.  
  110. ClientManager.SendMessage(operationToSend);
  111. ClientManager.SendMessage(dataToSend);
  112. }
  113.  
  114. private static void ViewStudents()
  115. {
  116. operationToSend = TransformDataManager.Serialize(3);
  117. dataToSend = TransformDataManager.Serialize(string.Empty);
  118.  
  119. ClientManager.SendMessage(operationToSend);
  120. ClientManager.SendMessage(dataToSend);
  121.  
  122. recievedData = ClientManager.WaitForMessage();
  123.  
  124. List<Student> students = recievedData[typeof(List<Student>)] as List<Student>;
  125.  
  126. Console.WriteLine(Environment.NewLine + "Students Information:");
  127.  
  128. foreach (Student student in students)
  129. {
  130. Console.WriteLine("ID: {0}", student.ID);
  131. Console.WriteLine("Name: {0} # Age: {1} # Points: {2}", student.Name, student.Age , string.Join(", ", student.Points));
  132. }
  133. Console.WriteLine();
  134. }
  135.  
  136. private static void ViewClassrooms()
  137. {
  138. operationToSend = TransformDataManager.Serialize(4);
  139. dataToSend = TransformDataManager.Serialize(string.Empty);
  140.  
  141. ClientManager.SendMessage(operationToSend);
  142. ClientManager.SendMessage(dataToSend);
  143.  
  144. recievedData = ClientManager.WaitForMessage();
  145.  
  146. List<Classroom> classrooms = recievedData[typeof(List<Classroom>)] as List<Classroom>;
  147.  
  148. Console.WriteLine(Environment.NewLine + "Classroom Information:");
  149.  
  150. foreach (Classroom classroom in classrooms)
  151. {
  152. Console.WriteLine("ID: {0}", classroom.ID);
  153. Console.WriteLine("Name: {0} # Subject: {1}", classroom.Name, classroom.Subject);
  154. }
  155. Console.WriteLine();
  156. }
  157.  
  158. public static void EndCommunication()
  159. {
  160. operationToSend = TransformDataManager.Serialize(5);
  161. dataToSend = TransformDataManager.Serialize(string.Empty);
  162.  
  163. ClientManager.SendMessage(operationToSend);
  164. ClientManager.SendMessage(dataToSend);
  165. }
  166.  
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement