Question

In: Computer Science

Create a C# application for SummerSchool that reads the file Participants. participant's data on the screen....

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

Solutions

Expert Solution

Short Summary:

  • Created participant and summerschool class as requested using C# - Console application.
  • SummerSchool reads the input text file content and creates new participants object for each line and stores in a generic list
  • Finally, iterates through the list and prints the participants on console application
  • When you use absolute path, place the Input file at project output directory. Else, modify the INPUT_FILE constant to full file path.

**************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!!!

**************************************************************************************


Related Solutions

C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
c++ language Create a file program that reads an int type Array size 10; the array...
c++ language Create a file program that reads an int type Array size 10; the array has already 10 numbers, but your job is to resize the array, copy old elements of array to the new one and make it user input and add an additional 5 slots in the array, and lastly do binary search based on user input. close the file.
Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching...
Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching pairs of vehicle models and their respective makes Separate out the individual make and model on each line of the file Add the vehicle make to one list, and the vehicle model to another list; such that they are in the same relative position in each list Prompt the user to enter a vehicle model Search the list containing the vehicle models for a...
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Assignment: Create data file consisting of integer, double or String values. Create unique Java application to...
Assignment: Create data file consisting of integer, double or String values. Create unique Java application to read all data from the file echoing the data to standard output. After all data has been read, display how many data were read. For example, if 10 integers were read, the application should display all 10 integers and at the end of the output, print "10 data values were read" My issue is displaying how many integers were read and how many strings...
(C++) Create a data file and name it "input.txt". manually save 10 integers into the file....
(C++) Create a data file and name it "input.txt". manually save 10 integers into the file. Write a program to read the data and calculate the average of events and odds, separately. Print out the average values.
C Programming Write a program in C that reads in a file, stores its contents as...
C Programming Write a program in C that reads in a file, stores its contents as a character array/pointer (char*) into an unsigned character array/pointer (unsigned char* message). Note: the input file can have one line or multiple lines and vary in length
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT