In: Computer Science
Create a C# application for SummerSchool that reads the file
Participants.
participant's data on the screen. Create a Participant class that
contains field
first name, last name, age, and course taken for the summer school
and To
fields of records in the file are separated with semicolon (). 3
marks)
E.g. of a record in the file Participant.txt:
123,John,Smith35:Math
using System;
using static System. Console;
using System. I0;
class SummerSchool
static void Main()
/7Your code as answer
Short Summary:
**************Please upvote the answer and appreciate our time.************
Source Code:
Participant.cs File:
namespace SummerSchoolDemo
{
class Participant
{
private int id;
private string firstName;
private string lastName;
private int age;
private string course;
/// <summary>
/// Constructor
/// </summary>
/// <param name="id"></param>
/// <param name="firstName"></param>
/// <param name="lastName"></param>
/// <param name="age"></param>
/// <param name="course"></param>
public Participant(int id, string firstName, string lastName, int age, string course)
{
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.course = course;
}
/// <summary>
/// ToString override
/// </summary>
/// <returns>Participants details with semicolon separated</returns>
public override string ToString()
{
return this.id + ";" + this.firstName + ";" + this.lastName + ";" + this.age + ";" + this.course;
}
}
}
SummerSchool.cs File:
using System;
using System.Collections.Generic;
using System.IO;
namespace SummerSchoolDemo
{
class SummerSchool
{
const string INPUT_FILE = "Input.txt";
static void Main(string[] args)
{
// List to store all the participants
List<Participant> participants = new List<Participant>();
try
{
// use StreamReader to read the file
StreamReader file = new StreamReader(INPUT_FILE);
string line;
// Read line by line till last line
while ((line = file.ReadLine()) != null)
{
// Split the line by ;
string[] tokens = line.Split(';');
if(tokens.Length == 5)
{
int id;
int.TryParse(tokens[0], out id);
string firstname = tokens[1];
string lastname = tokens[2];
int age;
int.TryParse(tokens[3], out age);
string course = tokens[4];
//Create new participant object and add it to list
participants.Add(new Participant(id, firstname, lastname, age, course));
}
}
// close the file after reading
file.Close();
// Go through the list and display the participants on console
foreach(Participant p in participants)
{
Console.WriteLine(p.ToString());
}
}
catch(Exception e)
{
// Display the exception occured
Console.WriteLine("File Read Error: " + e.Message);
}
Console.ReadKey();
}
}
}
Sample Run:
Input file:
**************************************************************************************
Feel free to rate the answer and comment your questions, if you have any.
Please up vote the answer and appreciate our time.
Happy Studying!!!
**************************************************************************************