Advertisement
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;
- using System.Threading.Tasks;
- using System.Globalization;
- namespace task_23
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<Student> MyListOfStudents = new List<Student>();
- for (int i = 0; ; i++)
- {
- Input inputLine = ReadInputLine();
- if (inputLine.CheckKeyWord)
- {
- break;
- }
- bool newUniqueStudent = true;
- Student NewStudent = GetNewStudentAndAddDate(inputLine.input);
- Student upgradedStudent = new Student();
- foreach (Student item in MyListOfStudents)
- {
- if (item.name == NewStudent.name)
- {
- upgradedStudent = AddAdditionalDatesToExistingStudent(item, NewStudent);
- newUniqueStudent = false;
- }
- }
- if (newUniqueStudent)
- {
- MyListOfStudents.Add(NewStudent);
- }
- else
- {
- for (int j = 0; j < MyListOfStudents.Count; j++)
- {
- if (MyListOfStudents.ElementAt(j).name == NewStudent.name)
- {
- MyListOfStudents.RemoveAt(j);
- MyListOfStudents.Add(upgradedStudent);
- }
- }
- }
- }
- for (int i = 0; ; i++)
- {
- Input inputLine = ReadInputLine();
- if (inputLine.CheckKeyWord)
- {
- MyListOfStudents = OrderListOfStudents(MyListOfStudents);
- PrintTheResult(MyListOfStudents);
- break;
- }
- bool newUniqueStudentComment = false;
- Student NewStudent = GetNewStudentAndAddComment(MyListOfStudents, inputLine.input);
- if (NewStudent.ListOfComments != null)
- {
- Student upgradedStudent = new Student();
- foreach (Student item in MyListOfStudents)
- {
- if (item.name == NewStudent.name)
- {
- upgradedStudent = AddAdditionalCommentsToExistingStudent(item, NewStudent);
- newUniqueStudentComment = true;
- }
- }
- if (newUniqueStudentComment)
- {
- for (int j = 0; j < MyListOfStudents.Count; j++)
- {
- if (MyListOfStudents.ElementAt(j).name == NewStudent.name)
- {
- MyListOfStudents.RemoveAt(j);
- break;
- }
- }
- MyListOfStudents.Add(upgradedStudent);
- }
- }
- }
- }
- static void PrintTheResult(List<Student> ListToPrint)
- {
- foreach (Student student in ListToPrint)
- {
- Console.WriteLine(student.name);
- Console.WriteLine("Comments:");
- if (student.ListOfComments != null)
- {
- foreach (string item in student.ListOfComments)
- {
- Console.WriteLine($"- {item}");
- }
- }
- Console.WriteLine("Dates attended:");
- Student orderedDatesInStudent = new Student();
- orderedDatesInStudent = SortDatesForStudent(student);
- foreach (DateTime item in orderedDatesInStudent.ListOfAttendanceDates)
- {
- Console.WriteLine($"-- {item.ToString("dd/MM/yyyy")}");
- }
- }
- }
- static List<Student> OrderListOfStudents(List<Student> ListToOrder)
- {
- List<Student> OrderedList = new List<Student>();
- OrderedList = ListToOrder.OrderBy(o => o.name).ToList();
- return OrderedList;
- }
- static Student AddAdditionalCommentsToExistingStudent(Student StudentWithDates, Student StudentWithComments)
- {
- Student upgradedStudent = new Student();
- upgradedStudent.name = StudentWithDates.name;
- upgradedStudent.ListOfAttendanceDates = StudentWithDates.ListOfAttendanceDates;
- upgradedStudent.ListOfComments = UpgradeListOfComments(StudentWithDates.ListOfComments, StudentWithComments.ListOfComments);
- return upgradedStudent;
- }
- static List<string> UpgradeListOfComments(List<string> oldList, List<string> newList)
- {
- List<string> UpgradedList = new List<string>();
- if (oldList != null)
- {
- UpgradedList = oldList;
- }
- for (int i = 0; i < newList.Count; i++)
- {
- UpgradedList.Add(newList.ElementAt(i));
- }
- return UpgradedList;
- }
- static Student GetNewStudentAndAddComment(List<Student> ListToCheck, string input)
- {
- List<string> inputLine = input.Split('-').ToList();
- Student NewStudent = new Student();
- for (int i = 0; i < ListToCheck.Count; i++)
- {
- if (ListToCheck.ElementAt(i).name == inputLine[0])
- {
- NewStudent.name = inputLine[0];
- List<string> CommentToAdd = new List<string>();
- for (int j = 1; j < inputLine.Count; j++)
- {
- if (!CommentToAdd.Contains(inputLine[j]))
- {
- CommentToAdd.Add(inputLine[j]);
- }
- }
- NewStudent.ListOfComments = CommentToAdd;
- return NewStudent;
- }
- }
- return NewStudent;
- }
- static Student SortDatesForStudent(Student studentToSortDate)
- {
- Student sortedStudent = new Student();
- sortedStudent.name = studentToSortDate.name;
- sortedStudent.ListOfAttendanceDates = studentToSortDate.ListOfAttendanceDates.OrderBy(o => o).ToList();
- return sortedStudent;
- }
- static Student AddAdditionalDatesToExistingStudent(Student existingStudent, Student newStudent)
- {
- Student upgradedStudent = new Student();
- upgradedStudent.name = existingStudent.name;
- upgradedStudent.ListOfAttendanceDates = existingStudent.ListOfAttendanceDates.Union(newStudent.ListOfAttendanceDates).ToList();
- return upgradedStudent;
- }
- static Student GetNewStudentAndAddDate(string input)
- {
- string[] inputLine = input.Split(' ', ',').ToArray();
- Student NewStudent = new Student();
- NewStudent.name = inputLine[0];
- List<DateTime> datesToAdd = new List<DateTime>();
- for (int i = 1; i < inputLine.Length; i++)
- {
- datesToAdd.Add(DateTime.ParseExact(inputLine[i], "dd/MM/yyyy", CultureInfo.InvariantCulture));
- }
- NewStudent.ListOfAttendanceDates = datesToAdd;
- return NewStudent;
- }
- static Input ReadInputLine()
- {
- Input inputLogic = new Input();
- string input = Console.ReadLine();
- bool WordForBreak = false;
- if (input == "end of dates" || input == "end of comments")
- {
- WordForBreak = true;
- }
- inputLogic.input = input;
- inputLogic.CheckKeyWord = WordForBreak;
- return inputLogic;
- }
- }
- public class Student
- {
- public string name { get; set; }
- public List<string> ListOfComments { get; set; }
- public List<DateTime> ListOfAttendanceDates { get; set; }
- public List<string> GetListOfComments(List<string> listOfComments)
- {
- return ListOfComments;
- }
- }
- public class Input
- {
- public bool CheckKeyWord { get; set; }
- public string input { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement