Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 05.ObjectAndClasses-Students
- Define a class Student, which holds the following information about students: first name, last name, age and hometown.
- Read a list of students until you receive the "end" command. After that, you will receive a name of a city. Print only students, which are from the given city in the following format: "{firstName} {lastName} is {age} years old.".
- Examples
- Input Output
- John Smith 15 Sofia John Smith is 15 years old.
- Peter Ivanov 14 Plovdiv Linda Bridge is 16 years old.
- Linda Bridge 16 Sofia
- Simon Stone 12 Varna
- end
- Sofia
- Anthony Taylor 15 Chicago Anthony Taylor is 15 years old.
- David Anderson 16 Washington Jack Lewis is 14 years old.
- Jack Lewis 14 Chicago David Lee is 14 years old.
- David Lee 14 Chicago
- end
- Chicago
- Solution
- Define a class student with the following properties: FirstName, LastName, Age and City.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _05Students
- {
- class Program
- {
- static void Main(string[] args)
- {
- var allStudents = new List<Student>();
- var command = "";
- while ((command = Console.ReadLine()) != "end")
- {
- var array = command.Split();
- var firstNameStudent = array[0];
- var lastNameStudent = array[1];
- var ageStudent = array[2];
- var homeTown = array[3];
- var createObjectStudent = new Student();
- createObjectStudent.FirstName = firstNameStudent;
- createObjectStudent.LastName = lastNameStudent;
- createObjectStudent.Age = ageStudent;
- createObjectStudent.HomeTown = homeTown;
- allStudents.Add(createObjectStudent);
- }
- var town = Console.ReadLine();
- foreach (var student in allStudents)
- {
- if (student.HomeTown == town)
- {
- Console.WriteLine($"{student.FirstName} {student.LastName} is {student.Age} years old.");
- }
- }
- }
- }
- class Student
- {
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public string Age { get; set; }
- public string HomeTown { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment