public void InsertTrainer() { Console.WriteLine("Type a first name between 1 and 50 characters:"); string fname = ConsoleRead.SetVarchar(50, 1); Console.WriteLine("Type a last name between 1 and 50 characters:"); string sname = ConsoleRead.SetVarchar(50, 1); string sub; Console.WriteLine("If you want to set subject for this trainer, press Y. Otherwise no subject will be inserted."); string answer = Console.ReadLine().ToLower(); if (answer == "y" || answer == "yes") { Console.WriteLine("Type subject:"); sub = ConsoleRead.SetVarchar(50); } else { sub = null; } Trainer tr = new Trainer() { firstName = fname, lastName = sname, subject = sub }; dbContext.Trainers.Add(tr); dbContext.SaveChanges(); }
public void InsertStudent() { Console.WriteLine("Type a first name between 1 and 50 characters:"); string fname = ConsoleRead.SetVarchar(50, 1); Console.WriteLine("Type a last name between 1 and 50 characters:"); string sname = ConsoleRead.SetVarchar(50, 1); Console.WriteLine("Type a valid date of birth. The date must be before today:"); Date birthdate = ConsoleRead.SetDate(); double?fees; Console.WriteLine("If you want to set tuition fees for this student, press Y. Otherwise no fees will be inserted."); string answer = Console.ReadLine().ToLower(); if (answer == "y" || answer == "yes") { Console.WriteLine("Type tuition fees for the student:"); fees = ConsoleRead.SetDouble(true); } else { fees = null; } Student std = new Student() { firstName = fname, lastName = sname, dateOfBirth = birthdate, tuitionFees = (decimal?)fees }; dbContext.Students.Add(std); dbContext.SaveChanges(); }
public void InsertAssignmentPerStudentPerCourse() { //Όλα τα assignments. Φυσικά τα θέλω >0 το πλήθος. var queryAss = from c in dbContext.Assignments select c; if (!dbContext.Assignments.Any()) { Console.WriteLine("No assignments to connect to courses."); return; } //Όλα τα μαθήματα. Φυσικά τα θέλω >0 το πλήθος. var queryCr = from c in dbContext.Courses select c; if (!dbContext.Courses.Any()) { Console.WriteLine("No courses to assign the trainers to."); return; } //Τυπώνω assignments και μαθήματα για να ξέρει ο χρήστης αυτά που έχει στη διάθεση του. Console.WriteLine("The available assignments are the following:"); foreach (var assInfo in queryAss) { Console.WriteLine("{0}. Title: {1}.", assInfo.id, assInfo.title); } Console.WriteLine(); Console.WriteLine("The available courses are the following:"); foreach (var crInfo in queryCr) { Console.WriteLine("{0}: {1}", crInfo.id, crInfo.title); } Console.WriteLine(); Console.WriteLine("Type a valid assignment id and a valid course id to assign that assignment to that course. Take note that:\n" + "The two ID ust be given in that order.\n" + "The two ID must be separated by one or more spaces.\n" + "In case the assignment is already given at that course, no changes will occur."); string inputa, inputb; if (!ConsoleRead.ReadTwoIntegers(out inputa, out inputb)) { Console.WriteLine("Wrong input."); return; } //Η ReadTwoIntegers έχει εξασφαλίσει ότι το Parse πορεί να συμβεί int ida = int.Parse(inputa); int idb = int.Parse(inputb); /*Ορίζω και αρχικοποιώ μια μεταβλητή assignment και μαθήματος. Είμαι σίγουρος ότι κι οι δυο πίνακες έχουν * τουλάχιστον ένα στοιχείο οπότε τους αρχικοποιώ με το πρώτο - δεν έχει σημασία, απλά για να μη χτυπάει παρακάτω * ο κώδικας - καθώς οι τιμές τους στη πραγματικότητα δίνονται εντός if.*/ Assignment wAss = queryAss.First(); Course wCr = queryCr.First(); //Ελέγχει αν το id assignment είναι υπαρκτό bool found = false; foreach (var assInfo in queryAss) { if (assInfo.id == ida) { found = !found; wAss = assInfo; break; } } if (!found) { Console.WriteLine("Given assignment id doesn't exist."); return; } //Ελέγχει αν το id μαθήματος είναι υπαρκτό found = false; foreach (var crInfo in queryCr) { if (crInfo.id == idb) { found = !found; wCr = crInfo; break; } } if (!found) { Console.WriteLine("Given course id doesn't exist."); return; } //Διατρέχω το πίνακα Course_has_Assignment για ήδη υπάρχουσα αντιστοίχηση. if (dbContext.Course_has_Assignment.Any(a => a.Assignment_id == ida && a.Course_id == idb)) { Console.WriteLine("Given assignment already belongs to given course."); return; } //Αντιστοιχώ assignment-μάθημα στη περίπτωση που όλα πήγαν καλά. Course_has_Assignment cr_assNew = new Course_has_Assignment() { Assignment_id = ida, Course_id = idb }; dbContext.Course_has_Assignment.Add(cr_assNew); cr_assNew.Course = wCr; cr_assNew.Assignment = wAss; //Τώρα θα αναθέσω σε όλους τους μαθητές του μαθήματος το assignment αυτό. foreach (var std in wCr.Students) { Student_gets_Assignment i = new Student_gets_Assignment() { Student_id = std.id, Course_id = cr_assNew.Course_id, Assignment_id = cr_assNew.Assignment_id, //Ο μαθητής δεν έχει ακόμη παραδώσει το assignment για να πάρει βαθμούς totalMark = null, oralMark = null }; dbContext.Student_gets_Assignment.Add(i); i.Student = std; i.Course_has_Assignment = cr_assNew; } dbContext.SaveChanges(); }
public void InsertTrainerPerCourse() { //Όλοι οι εκπαιδευτές. Φυσικά τους θέλω >0 το πλήθος. var queryTr = from c in dbContext.Trainers select c; if (!dbContext.Trainers.Any()) { Console.WriteLine("No trainers to connect to courses."); return; } //Όλα τα μαθήματα. Φυσικά τα θέλω >0 το πλήθος. var queryCr = from c in dbContext.Courses select c; if (!dbContext.Courses.Any()) { Console.WriteLine("No courses to assign the trainers to."); return; } //Τυπώνω εκπαιδευτές και μαθήματα για να ξέρει ο χρήστης αυτά που έχει στη διάθεση του. Console.WriteLine("The available trainers are the following:"); foreach (var trInfo in queryTr) { Console.WriteLine("{0}: {1} {2}", trInfo.id, trInfo.firstName, trInfo.lastName); } Console.WriteLine(); Console.WriteLine("The available courses are the following:"); foreach (var crInfo in queryCr) { Console.WriteLine("{0}: {1}", crInfo.id, crInfo.title); } Console.WriteLine(); Console.WriteLine("Type a valid trainer id and a valid course id to assign that trainer to that course. Take note that:\n" + "The two ID ust be given in that order.\n" + "The two ID must be separated by one or more spaces.\n" + "In case the trainer already teaches at that course, no changes will occur."); string inputa, inputb; if (!ConsoleRead.ReadTwoIntegers(out inputa, out inputb)) { Console.WriteLine("Wrong input."); return; } //Η ReadTwoIntegers έχει εξασφαλίσει ότι το Parse πορεί να συμβεί int ida = int.Parse(inputa); int idb = int.Parse(inputb); /*Ορίζω και αρχικοποιώ μια μεταβλητή εκπαιδευτή και μαθήματος. Είμαι σίγουρος ότι κι οι δυο πίνακες έχουν * τουλάχιστον ένα στοιχείο οπότε τους αρχικοποιώ με το πρώτο - δεν έχει σημασία, απλά για να μη χτυπάει παρακάτω * ο κώδικας - καθώς οι τιμές τους στη πραγματικότητα δίνονται εντός if.*/ Trainer wTr = queryTr.First(); Course wCr = queryCr.First(); //Ελέγχει αν το id εκπαιδευτή είναι υπαρκτό bool found = false; foreach (var trInfo in queryTr) { if (trInfo.id == ida) { found = !found; wTr = trInfo; break; } } if (!found) { Console.WriteLine("Wrong input."); return; } //Ελέγχει αν το id μαθήματος είναι υπαρκτό found = false; foreach (var crInfo in queryCr) { if (crInfo.id == idb) { found = !found; wCr = crInfo; break; } } if (!found) { Console.WriteLine("Wrong input."); return; } //Διατρέχω τα μαθήματα του δωσμένου εκπαιδευτή για ήδη υπάρχουσα αντιστοίχηση. foreach (var crInfo in wTr.Courses) { if (crInfo.id == idb) { Console.WriteLine("That trainer is already teaching at given course."); return; } } //Αντιστοιχώ εκπαιδευτή-μάθημα στη περίπτωση που όλα πήγαν καλά. wTr.Courses.Add(wCr); //wCr.Trainers.Add(wTr); //το ανάποδο γίνεται αυτόματα(;) dbContext.SaveChanges(); }
public void InsertCourse() { string _title = ConsoleRead.SetVarchar(50); string _stream; Console.WriteLine("If you want to set stream for this course, press Y. Otherwise no stream will be inserted."); string answer = Console.ReadLine().ToLower(); if (answer == "y" || answer == "yes") { Console.WriteLine("Type stream:"); _stream = ConsoleRead.SetVarchar(50); } else { _stream = null; } string _type; Console.WriteLine("If you want to set type for this course, press Y. Otherwise no type will be inserted."); answer = Console.ReadLine().ToLower(); if (answer == "y" || answer == "yes") { Console.WriteLine("Type type:"); _type = ConsoleRead.SetVarchar(50); } else { _type = null; } Date?start; Console.WriteLine("If you want to set start date for this course, press Y. Otherwise no start date will be inserted."); answer = Console.ReadLine().ToLower(); if (answer == "y" || answer == "yes") { Console.WriteLine("Type start date. Note that you will have to assign a future date:"); start = ConsoleRead.SetDate(true); } else { start = null; } Date?end; Console.WriteLine("If you want to set end date for this course, press Y. Otherwise no end date will be inserted."); answer = Console.ReadLine().ToLower(); if (answer == "y" || answer == "yes") { Console.WriteLine("Type end date. Note that you will have to assign a future date:"); end = ConsoleRead.SetDate(true); } else { end = null; } Course cr = new Course() { title = _title, stream = _stream, type = _type, start_date = start, end_date = end }; dbContext.Courses.Add(cr); dbContext.SaveChanges(); }
public void InsertAssignment() { string _title; Console.WriteLine("If you want to set title for this assignment, press Y. Otherwise no title will be inserted."); string answer = Console.ReadLine().ToLower(); if (answer == "y" || answer == "yes") { Console.WriteLine("Type title:"); _title = ConsoleRead.SetVarchar(50); } else { _title = null; } string desc; Console.WriteLine("If you want to set description for this assignment, press Y. Otherwise no description will be inserted."); answer = Console.ReadLine().ToLower(); if (answer == "y" || answer == "yes") { Console.WriteLine("Type description:"); desc = ConsoleRead.SetVarchar(500); } else { desc = null; } Date?subdt; Console.WriteLine("If you want to set submission deadline for this assignment, press Y. Otherwise no deadline will be inserted."); answer = Console.ReadLine().ToLower(); if (answer == "y" || answer == "yes") { Console.WriteLine("Type deadline date and time. The date and time must be after the current:"); subdt = ConsoleRead.SetDateTime(); } else { subdt = null; } int?oral; Console.WriteLine("If you want to set maximum oral mark for this assignment, press Y. Otherwise no oral mark will be inserted."); answer = Console.ReadLine().ToLower(); if (answer == "y" || answer == "yes") { Console.WriteLine("Type maximum possible oral mark:"); oral = ConsoleRead.SetInt(true); } else { oral = null; } int?total; Console.WriteLine("If you want to set maximum total mark for this assignment, press Y. Otherwise no total mark will be inserted."); answer = Console.ReadLine().ToLower(); if (answer == "y" || answer == "yes") { Console.WriteLine("Type maximum possible total mark:"); total = ConsoleRead.SetInt(true); } else { total = null; } Assignment ass = new Assignment() { title = _title, description = desc.Substring(0, 49), subDateTime = subdt, oralMark = oral, totalMark = total }; dbContext.Assignments.Add(ass); dbContext.SaveChanges(); }