Advertisement
JulianJulianov

12.ObjectsAndClasses-Students

Mar 5th, 2020
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.69 KB | None | 0 0
  1. 12.ObjectsAndClasses-Students
  2. Write a program that receives a count of students - n and orders them by grade in descending order. Each student should have a First name (string), a Last name (string) and a grade (a floating-point number).
  3. Input
  4. • On the first line, you are going to receive n - the count of students
  5. • On the next n lines, you will be receiving the info about the students in the following format:
  6. "{first name} {second name} {grade}"
  7. Output
  8. • Print each student in the following format: "{first name} {second name}: {grade}"
  9. Example
  10. Input                                  Output
  11. 4
  12. Lakia Eason 3.90                       Rocco Erben: 6.00
  13. Prince Messing 5.49                    Prince Messing: 5.49
  14. Akiko Segers 4.85                      Akiko Segers: 4.85
  15. Rocco Erben 6.00                       Lakia Eason: 3.90
  16.  
  17.  
  18.  
  19.  
  20. using System;
  21. using System.Collections.Generic;
  22. using System.Linq;
  23. using System.Text;
  24. using System.Threading.Tasks;
  25.  
  26. namespace _4.Average_Grades
  27. {
  28.     class Student
  29.     {
  30.         public string FirstName { get; set; }
  31.         public string SecondName { get; set; }
  32.         public double Grade { get; set; }
  33.         public Student(string firstName, string secondName, double grade)
  34.         {
  35.             this.FirstName = firstName;
  36.             this.SecondName = secondName;
  37.             this.Grade = grade;
  38.         }
  39.         public double getGrade()
  40.         {
  41.             return this.Grade;
  42.         }
  43.  
  44.         public override string ToString()
  45.         {
  46.             return string.Format(this.FirstName, this.SecondName, this.Grade);
  47.         }
  48.     }
  49.  
  50.  
  51.     class Program
  52.     {
  53.         static void Main(string[] args)
  54.         {
  55.             List<Student> students = new List<Student>();
  56.             int n = int.Parse(Console.ReadLine());
  57.  
  58.             for (int i = 0; i < n; i++)
  59.             {
  60.                 string[] info = Console.ReadLine().Split(" ");
  61.                 string firstName = info[0];
  62.                 string secondName = info[1];
  63.                 double grade = double.Parse(info[2]);
  64.  
  65.                 Student student = new Student(firstName, secondName, grade);
  66.                 students.Add(student);
  67.  
  68.                 //Console.WriteLine($"{student.FirstName} {student.SecondName}: {student.Grade:f2}");
  69.  
  70.             }
  71.             students.OrderByDescending(t => t.Grade).ThenBy(t => t.FirstName).ToList();
  72.             List<Student> sortedStudents = students.OrderByDescending(t => t.Grade).ThenBy(t => t.FirstName).ThenBy(t => t.SecondName).ToList();
  73.  
  74.             foreach (Student t in sortedStudents)
  75.             {
  76.                 Console.WriteLine($"{t.FirstName} {t.SecondName}: {t.Grade:f2}");
  77.             }
  78.  
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement