static void Main() { var studentMaria = new Student("Maria", "Petrova","985112", 3, Specialty.BiologyTeacher, University.SofiaUneversity, Faculty.Biology); var studentPesho = new Student("Pesho", "Peshev", "159876", 4, Specialty.SoftwareEngineer, University.Technical, Faculty.Electronics); var otherPesho = studentPesho.Clone() as Student; Console.WriteLine("Compare Pesho and otherPesho:"); Console.WriteLine("Equal: "+ studentPesho.Equals(otherPesho)); Console.Write("==: "); Console.WriteLine( studentPesho == otherPesho); Console.Write("!=: "); Console.WriteLine(studentPesho != otherPesho); Console.WriteLine(); Console.WriteLine("Compare Pesho and Maria:"); Console.WriteLine("Equal: " + studentPesho.Equals(studentMaria)); Console.Write("==: "); Console.WriteLine(studentPesho == studentMaria); Console.Write("!=: "); Console.WriteLine(studentPesho != studentMaria); Console.WriteLine(); otherPesho.SSN = "546549"; Console.WriteLine("Original Pesho:\n" +studentPesho.ToString()); Console.WriteLine("Cloned Pesho:\n"+otherPesho.ToString()); Console.WriteLine("Compairing Pesho and otherPesho " +studentPesho.CompareTo(otherPesho)); Console.WriteLine("Compare Pesho and otherPesho:"); //Console.WriteLine("Equal: " + studentPesho.Equals(otherPesho)); Console.Write("==: "); //Console.WriteLine(studentPesho == otherPesho); Console.Write("!=: "); //Console.WriteLine(studentPesho != otherPesho); Console.WriteLine(); }
static void Main(string[] args) { Student[] studentsArray = new Student[5] { new Student("Kristian","Bojadjiev",25), new Student("Antoniq","Petrova",19), new Student("Viktor","Kynchelov",23), new Student("Viktor","Zashev",31), new Student("Psiho","Sum",15) }; Console.WriteLine("----Selected First before Last----"); Student[] selected = FirstBeforeLast(studentsArray); foreach (var item in selected) { Console.WriteLine(item.FirstName+" "+ item.LasttName); } Console.WriteLine("----Selected Age between 18 and 24----"); string[] selectedAge = AgeBetween18and24(studentsArray); foreach (var item in selectedAge) { Console.WriteLine(item); } Console.WriteLine("----Ordered by names Lambda----"); Student[] orderedLambda = OrderedByNamesLambda(studentsArray); foreach (var item in orderedLambda) { Console.WriteLine(item.FirstName + " " + item.LasttName); } Console.WriteLine("----Ordered by names LINQ----"); Student[] orderedLINQ = OrderedByNames(studentsArray); foreach (var item in orderedLINQ) { Console.WriteLine(item.FirstName + " " + item.LasttName); } }
static void Main() { //09. Create a List<Student> with sample students. Select only the students that are from group number 2. //• Use LINQ query. Order the students by FirstName. List<Student> students = new List<Student>(); List<byte> marks = new List<byte>() {5, 6, 5, 6}; Student student = new Student("Anna", "Zdravlkova", "4457", "0256475475", "*****@*****.**", 4, marks); students.Add(student); marks = new List<byte>() { 6, 6, 6, 6 }; student = new Student("Rosen", "Nilolaev", "5858", "024581234", "*****@*****.**", 2, marks); students.Add(student); marks = new List<byte>() { 6, 6, 4, 6 }; student = new Student("Angel", "Todorov", "6001", "0899735641", "*****@*****.**", 2, marks); students.Add(student); marks = new List<byte>() { 6, 6, 4, 6 }; student = new Student("Zdravka", "Dobreva", "4357", "0899345678", "*****@*****.**", 3, marks); students.Add(student); marks = new List<byte>() { 6, 6, 6, 6 }; student = new Student("Dimana", "Todorova", "5941", "0899778641", "*****@*****.**", 2, marks); students.Add(student); Console.WriteLine("Students:"); PrintStudents(students); ////Select only the students that are from group number 2. ////• Use LINQ query. Order the students by FirstName. var studentsGr2 = StudentServices.GetStudentsByGroup(students, 2); Console.WriteLine("9. Students in group number 2 (ordered by FirstName)(LINQ):"); PrintStudents(studentsGr2); //10. Implement the previous using the same query expressed with extension methods. var studentsGr2Ext = StudentServices.GetStudentsByGroupExt(students, 2); Console.WriteLine("10. Students in group number 2 (ordered by FirstName) (extension methods):"); PrintStudents(studentsGr2Ext); //11.Extract all students that have email in abv.bg . //• Use string methods and LINQ. string domain = "abv.bg"; var studentsAbv = StudentServices.GetStudentsByEmailDomain(students, domain); Console.WriteLine("11. Students that have email in abv.bg:"); PrintStudents(studentsAbv); //12. Extract all students with phones in Sofia. //• Use LINQ. string phoneCode = "02"; var studentsPhoneCode02 = StudentServices.GetStudentsByPhoneCode(students, phoneCode); Console.WriteLine("12. Students students with phones in Sofia.:"); PrintStudents(studentsPhoneCode02); }
static void Main() { var student1 = new Student("Slim", "Marshal", "Shady", 666666666, "Detroit", "+35988888888", "*****@*****.**", 3, Specialties.Psychology, Universities.SofiaUniversity, Faculties.Europeistics); Console.WriteLine(student1.GetHashCode()); var student2 = new Student("Doctor", "Young", "Dre", 666222666, "Detroit", "+35988888888", "*****@*****.**", 4, Specialties.Phylosophy, Universities.TechnicalUniversity, Faculties.Languages); Console.WriteLine(student2.GetHashCode()); Console.WriteLine(); Console.WriteLine(student1); Console.WriteLine(); Console.WriteLine(student2); Console.WriteLine(); Console.WriteLine("student1 == student2 : {0}", student1 == student2); Console.WriteLine("student1 != student2 : {0}", student1 != student2); Console.WriteLine("student1.Equals(student1) : {0}", student1.Equals(student1)); Console.WriteLine("student1.Equals(student2) : {0}", student1.Equals(student2)); Console.WriteLine(); var person1 = new Person("Slim Shady", 42); var person2 = new Person("Doctor Dre"); Console.WriteLine(person1); Console.WriteLine(person2); }
private static IEnumerable<Student> firstNameBeforeLast(Student[] students) { var studentsWithFirstNameBeforeLastName = from student in students where student.FirstName.CompareTo(student.LastName) < 0 select student; return studentsWithFirstNameBeforeLastName; }
static void Main() { //whatever you want to test here Student stud1 = new Student("Pesho", "Peshev", 123); Student stud2 = new Student("Pesho", "Peshev", 123); Console.WriteLine(stud1.CompareTo(stud2)); }
private static void Main() { var peter = new Student("Peter", "Ivanov", "17.03.1992", "From Sofia"); var stella = new Student("Stella", "Markova", "03.11.1993", "From Vidin, gamer, high results"); Console.WriteLine("{0} older than {1} -> {2}", peter.FirstName, stella.FirstName, peter.IsOlderThan(stella)); }
static string[] AgeBetween18and24(Student[] inArray) { var selected = from student in inArray where (student.Age > 18 && student.Age < 24) select (student.FirstName + " " + student.LasttName); return selected.ToArray(); }
public static Student[] FindStudentsWhichFirstNameIsBeforeLast(Student[] students) { Student[] result = students .Where(student => String.Compare(student.FirstName, student.LastName, StringComparison.InvariantCultureIgnoreCase) < 0) .ToArray(); return result; }
// This is the method required by the task description static Student[] FilterStudents(Student[] students) { var list = (from student in students where student.FirstName.CompareTo(student.LastName) < 0 select student); // LINQ query return list.ToArray(); // Return the result as a Student[] array }
public static Student[] StudentsBetween18and24(Student[] students) { var result = from student in students where student.age >= 18 && student.age <= 24 select student; return result.ToArray<Student>(); }
static Student[] OrderedByNames(Student[] inArray) { var ordered = from student in inArray orderby student.FirstName descending, student.LasttName descending select student; return ordered.ToArray(); }
static void Main(string[] args) { Student st = new Student("Pesho", "Goshov", "Ivanov", "9110111245", "Bobovdol, Slaveikov st 10", "0888-88-88-88", "*****@*****.**", "3", Universities.UNWE, Faculties.Informatics, Specialties.BusinessInformatics); Console.WriteLine(st); Student st2 = st.Clone(); Console.WriteLine(st2); }
public void RemoveStudent(Student student) { if (student == null) { throw new ArgumentNullException("Student cannot be null"); } this.Students.Remove(student); }
static Student[] FirstBeforeLast(Student[] inArray) { var selected = from student in inArray where student.FirstName.CompareTo(student.LasttName) < 0 select student; return selected.ToArray(); }
public static Student[] StudentFirstNameBeforeLast(Student[] students) { var result = from student in students where student.firstName.CompareTo(student.lastName) < 0 select student; return result.ToArray<Student>(); }
public static void SortByFirstAndLastName(Student[] students) { var result = students.OrderByDescending(x => x.firstName) .ThenByDescending(y => y.lastName); foreach (var student in result) { Console.WriteLine(student.firstName + " " + student.lastName); } }
public static void SortByAge(Student[] students) { var result = from student in students where (student.age >= 18 && student.age <= 24) select student; foreach (var studnet in result) { Console.WriteLine(studnet.firstName + " " + studnet.lastName + " " + studnet.age); } }
public static void SortNames(Student[] students) { var result = from student in students where (student.firstName.ToLower().ToCharArray(0, 1))[0] < (student.lastName.ToLower().ToCharArray(0, 1))[0] select student; foreach (var studnet in result) { Console.WriteLine(studnet.firstName + " " + studnet.lastName); } }
public static void SortByFirstAndLastNameLINQ(Student[] students) { var result = from student in students orderby student.firstName descending, student.lastName descending select student; foreach (var student in result) { Console.WriteLine(student.firstName + " " + student.lastName); } }
static void Main(string[] args) { Student[] students = new Student[] {new Student("Alfa", "Betov", 24), new Student("Boris", "Aleksandrov", 28), new Student("Dimana", "Nikolova", 20), new Student("Nikola", "Dimov", 19), new Student("Dimo", "Dimov", 18) }; Console.WriteLine("Students list:"); Console.WriteLine(StudentsHelper.StudentsToStirng(students)); Console.WriteLine(); //Write a method that from a given array of students finds all students whose first name is before its last name alphabetically. Student[] studentsFirstIsBeforeLast = StudentsHelper.FindStudentsWhichFirstNameIsBeforeLast(students); Console.WriteLine("Students whose first name is before its last name alphabetically"); Console.WriteLine(StudentsHelper.StudentsToStirng(studentsFirstIsBeforeLast)); Console.WriteLine(); //Write a LINQ query that finds the first name and last name of all students with age is between 18 and 24. Console.WriteLine("First name and last name of all students with age between 18 and 24"); var studentsBetuween18and24Names = students.Where(student => student.Age >= 18 && student.Age <= 24) .Select(student => new { student.FirstName, student.LastName }); foreach (var item in studentsBetuween18and24Names) { Console.WriteLine(item); } Console.WriteLine(); //Using the extension methods OrderBy() and ThenBy() //with lambda expressions sort the students by first name and last name in descending order. //Rewrite the same with LINQ. Console.WriteLine("students sorted by first name and last name in descending order(lambda expressions):"); var orderedStudents = students.OrderByDescending(student => student.FirstName).ThenByDescending(student => student.LastName); foreach (var item in orderedStudents) { Console.WriteLine(item); } Console.WriteLine("students sorted by first name and last name in descending order(LINQ):"); var orderedStudentsLINQ = from student in students orderby student.FirstName descending, student.LastName descending select student; foreach (var item in orderedStudentsLINQ) { Console.WriteLine(item); } }
static void Main() { Student[] students = new Student[] {new Student("Ivan","Ivanov",19) , new Student("Petur", "Stamatov" , 21), new Student("Petur", "Ivanov" , 17), new Student("Iva" ,"Avramova" , 24), new Student ("Greta","Petrova" , 26)}; //task 3 var FirstNameBeforeLastNameAlphabetically = //thumbs up for the variable name :D from student in students where student.FirstName.CompareTo(student.LastName) < 0 select student; Console.WriteLine("First Name is before Last Name alphabetically : "); foreach (var item in FirstNameBeforeLastNameAlphabetically) { Console.WriteLine(item.FirstName + " " + item.LastName); } //task 4 Console.WriteLine("Students age is between 18 and 24 : "); var StudentsWithAgeBetween18and24 = from student in students where student.Age >= 18 && student.Age <= 24 select student; foreach (var item in StudentsWithAgeBetween18and24) { Console.WriteLine(item.FirstName + " " + item.LastName); } //task 5 Console.WriteLine("Sorted by FirstName and Last Name Lambda : "); var SortedStudentsFirstNameLastNameDescendingLambda = students.OrderByDescending(x => x.FirstName).ThenByDescending(x => x.LastName); foreach (var item in SortedStudentsFirstNameLastNameDescendingLambda) { Console.WriteLine(item.FirstName + " " + item.LastName); } Console.WriteLine("Sorted by FirstName and Last Name Linq : "); var SortedStudentsFirstNameLastNameDescendingLINQ = from student in students orderby student.FirstName descending, student.LastName descending select student; foreach (var item in SortedStudentsFirstNameLastNameDescendingLINQ) { Console.WriteLine(item.FirstName + " " + item.LastName); } }
public static void Main() { // I'm using American settings, that's why the month goes first when parsing to DateTime Student peter = new Student("Peter", "Ivanov", "Sofia", DateTime.Parse("03.17.1992")); Student stella = new Student("Stella", "Markova", "Vidin", DateTime.Parse("11.03.1993")); stella.AdditionalInfo = "gamer, high results"; Console.WriteLine( "{0} older than {1} -> {2}", peter.FirstName, stella.FirstName, peter.IsOlderThan(stella)); }
//Method that finds all students whose first name is before its last name alphabetically public static string FirstBeforeLastName(Student[] studentsArr) { var resultStudentsArr = from student in studentsArr where student.FirstName.CompareTo(student.LastName) < 0 select student; StringBuilder foundStudents = new StringBuilder(); foreach (var foundStudent in resultStudentsArr) { foundStudents.Append(foundStudent.FirstName); foundStudents.Append(" "); foundStudents.AppendLine(foundStudent.LastName); } return foundStudents.ToString(); }
public static string StudentsToStirng(Student[] students) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < students.Length; i++) { sb.Append(students[i]); if (i < students.Length - 1) { sb.Append('\r'); sb.Append('\n'); } } return sb.ToString(); }
public static void Main() { var students = new SortedDictionary<string, SortedDictionary<string, Student>>(); using (var sr = new StreamReader(@"..\..\students.txt")) { var line = sr.ReadLine(); while (line != null) { var values = line .Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries) .Select(v => v.Trim()) .ToArray(); var student = new Student(values[0], values[1]); if (!students.ContainsKey(values[2])) { students.Add(values[2], new SortedDictionary<string, Student>()); } if (!students[values[2]].ContainsKey(student.ToString())) { students[values[2]].Add(student.ToString(), student); } line = sr.ReadLine(); } } foreach (var course in students) { Console.Write("{0}: ", course.Key); List<string> studentsInCourse = new List<string>(); foreach (var student in course.Value) { studentsInCourse.Add(student.Value.FirstName + " " + student.Value.LastName); } Console.WriteLine(string.Join(", ", studentsInCourse)); } }
static void Main() { Student student1 = new Student("Pesho", "Pehsev", "Peshev", 12345, "Sofia", 89765223, "*****@*****.**", University.SU, Faculty.finance, Specialty.finance); Student student2 = new Student("Pesho", "Peshev", "Peshev", 54331, "Sofia", 7432487, "*****@*****.**", University.TU, Faculty.IT, Specialty.IT); Console.WriteLine(student1); Console.WriteLine(); Console.WriteLine(student2); Console.WriteLine(student1 != student2); Console.WriteLine(student1 == student2); Console.WriteLine(student1.GetHashCode()); Console.WriteLine(); Student student3 = (Student)student1.Clone(); Console.WriteLine(student3); Console.WriteLine(student1.ComparerTo(student2)); Console.WriteLine(student1.ComparerTo(student3)); }
public void AddStudent(Student student) { if (student == null) { throw new ArgumentNullException("Student cannot be null"); } if (this.Students.Count + 1 > MaxStudentsNumbers) { throw new InvalidOperationException(string.Format("Students in a course cannot be more than {0}", MaxStudentsNumbers)); } if (this.Students.Contains(student)) { throw new InvalidOperationException("This course already contains this student"); } this.Students.Add(student); }
public void AddStudent(Student student) { if (student == null) { throw new ArgumentNullException("Student cannot be null"); } if (this.Students.Contains(student)) { throw new InvalidOperationException("This school already contains this student"); } if (this.Students.Any(st => st.Number == student.Number)) { throw new InvalidOperationException("Student with this number has already been added"); } this.Students.Add(student); }
private static ICollection<Student> ReadFile(string filePath) { char[] separators = new char[] { ' ', '|' }; var students = new List<Student>(); using (var streamReader = new StreamReader(filePath)) { string line; while ((line = streamReader.ReadLine()) != null) { var studentInfo = line.Split(separators, StringSplitOptions.RemoveEmptyEntries); if (studentInfo.Length == 3) { var firstName = studentInfo[0]; var lastName = studentInfo[1]; var courseName = studentInfo[2]; var student = new Student(firstName, lastName, courseName); students.Add(student); } } } return students; }
public Student GetXML() { Student sd = new Student(); sd.InputStudent(); FileStream stream = new FileStream("students.xml", FileMode.Open); XmlTextReader xmlReader=new XmlTextReader(stream);; xmlReader.WhitespaceHandling = WhitespaceHandling.None; // var xmlReader = new XmlDocument(); //xmlReader.Load("students.xml"); while (xmlReader.Read()) { if (xmlReader.Name.Equals("Age")) { xmlReader.Read(); if (xmlReader.NodeType == XmlNodeType.Text) { sd.age = Convert.ToInt32(xmlReader.Value); Console.WriteLine(sd.age); } //string q; // q = xmlReader.Value; // sd.age = Convert.ToInt32(q.Split(new char[] { ' ','\n','\t','\r' }, StringSplitOptions.RemoveEmptyEntries)); } if (xmlReader.Name.Equals("firstName")) { // xmlReader.Read(); sd.firstName = xmlReader.Value; Console.WriteLine(sd.firstName); } if (xmlReader.Name.Equals("lastName")) { xmlReader.Read(); sd.lastName = xmlReader.Value; } if (xmlReader.Name.Equals("surName")) { xmlReader.Read(); sd.surName = xmlReader.Value; } if (xmlReader.Name.Equals("group")) { xmlReader.Read(); sd.group = xmlReader.Value; } if (xmlReader.Name.Equals("Profile")) { xmlReader.Read(); if (xmlReader.Value== "Developer") sd.profile = 1; if (xmlReader.Value == "Admin") sd.profile = 2; if (xmlReader.Value == "Desiner") sd.profile = 3; } if (xmlReader.Name.Equals("Marks")) { string s; xmlReader.Read(); s = xmlReader.Value; string[] smarks= s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (sd.profile == 1) { sd.mark[0] = new int[smarks.Length]; for (int i = 0; i < smarks.Length; i++) { sd.mark[0][i] =Convert.ToInt32 (smarks[i]); } } if (sd.profile == 2) { sd.mark[1] = new int[smarks.Length]; for (int i = 0; i < smarks.Length; i++) { sd.mark[1][i] = Convert.ToInt32(smarks[i]); } } if (sd.profile == 3) { sd.mark[2] = new int[smarks.Length]; for (int i = 0; i < smarks.Length; i++) { sd.mark[2][i] = Convert.ToInt32(smarks[i]); } } } } // Console.WriteLine(sd.age); sd.InputInXml(); xmlReader.Close(); sd.PrintStudent(); return sd; }
public void AddXML() { Student sd = new Student(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("stusentts.xml"); XmlElement xmlRoot = xmlDoc.DocumentElement; Console.WriteLine("Student Add:\t"); XmlElement StudElem = xmlDoc.CreateElement("Student"); XmlElement firstName = xmlDoc.CreateElement("firstName"); XmlElement lastName = xmlDoc.CreateElement("lastName"); XmlElement surName = xmlDoc.CreateElement("surName"); XmlElement Age = xmlDoc.CreateElement("Age"); XmlElement group = xmlDoc.CreateElement("group"); XmlElement profile = xmlDoc.CreateElement("Profile"); XmlElement marks = xmlDoc.CreateElement("Marks"); XmlText firstName1 = xmlDoc.CreateTextNode(sd.firstName); XmlText lastName1 = xmlDoc.CreateTextNode(sd.lastName); XmlText surName1 = xmlDoc.CreateTextNode(sd.surName); XmlText Age1 = xmlDoc.CreateTextNode(Convert.ToString(sd.age)); XmlText group1 = xmlDoc.CreateTextNode(sd.group); XmlText profile1 = xmlDoc.CreateTextNode(""); XmlText marks1 = xmlDoc.CreateTextNode(""); if (sd.profile == 1) { profile1 = xmlDoc.CreateTextNode("Developer"); string []temp=null; for (int i = 0; i < sd.mark[0].Length; i++)//!!!!!!!!!!!!!!!!!!!!!!!! { temp[i] = Convert.ToString( sd.mark[0][i]); } } if (sd.profile == 2) { profile1 = xmlDoc.CreateTextNode("Admin"); } if (sd.profile == 3) { profile1 = xmlDoc.CreateTextNode("Desiner"); } Console.Write("input number of book:\t"); XmlText IDText = xmlDoc.CreateTextNode(string.Format("{0}", Convert.ToInt32(Console.ReadLine()))); Console.Write("input title of book:\t"); XmlText titleText = xmlDoc.CreateTextNode(Console.ReadLine()); Console.Write("input currency:\t"); XmlText currencyText = xmlDoc.CreateTextNode(Console.ReadLine()); Console.Write("input price of book:\t"); XmlText priceText = xmlDoc.CreateTextNode(string.Format("{0}", Convert.ToDouble(Console.ReadLine()))); priceElem.AppendChild(priceText); currencyAttr.AppendChild(currencyText); priceElem.Attributes.Append(currencyAttr); titleElem.AppendChild(titleText); bookElem.AppendChild(titleElem); bookElem.AppendChild(priceElem); IDAttr.AppendChild(IDText); bookElem.Attributes.Append(IDAttr); xmlRoot.AppendChild(bookElem); Console.WriteLine("Book added"); xmlDoc.Save("books.xml"); }