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

This is C++ Create a program that reads an HTML file and converts it to plain...
This is C++ Create a program that reads an HTML file and converts it to plain text. Console: HTML Converter Grocery List * Eggs * Milk * Butter Specifications: The HTML file named groceries.html contains these HTML tags: <h1>Grocery List</h1> <ul> <li>Eggs</li> <li>Milk</li> <li>Butter</li> </ul> When the program starts, it should read the contents of the file, remove the HTML tags, remove any spaces to the left of the tags, add asterisks (*) before the list items, and display the...
How to do in C++ HTML Converter Create a program that reads an HTML file and...
How to do in C++ HTML Converter Create a program that reads an HTML file and converts it to plain text. Console HTML Converter Grocery List * Eggs * Milk * Butter Specifications Your instructor should provide an HTML file named groceries.html that contains these HTML tags: <h1>Grocery List</h1> <ul>     <li>Eggs</li>     <li>Milk</li>     <li>Butter</li> </ul> When the program starts, it should read the contents of the file, remove the HTML tags, remove any spaces to the left of the tags, add...
Create a C Program that reads a file and replaces every other letter with an asterisk....
Create a C Program that reads a file and replaces every other letter with an asterisk. The first integer 'num' is the number of lines. Include an "Error" Message and exit the program if:    The wrong name for the input/output file is given    The input/output file can not be opened input.txt 7 Never Have We Ever Played A Game output.txt 7 N*v*r *a*e W* *v*r *l*y*d * G*m*
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...
Shopping Cart App (C#) Please create a C# Console (.NET Framework) application which reads from the...
Shopping Cart App (C#) Please create a C# Console (.NET Framework) application which reads from the user, the price of items in their shopping "cart". Be sure to tell the user how to stop (or ask them if they want to stop) entering prices. Items can be priced any positive number greater than zero. When the user has entered the prices for all items in their "cart", calculate the subtotal (the sum of all item prices). Next, check if the...
c# Create a console application that protects an XML file, such as the following example. Note...
c# Create a console application that protects an XML file, such as the following example. Note that the customer's credit card number and password are currently stored in clear text. The credit card must be encrypted so that it can be decrypted and used later, and the password must be salted and hashed: <?xml version="1.0" encoding="utf-8" ?> <customers> <customer> <name>Bob Smith</name> <creditcard>1234-5678-9012-3456</creditcard> <password>Pa$$w0rd</password> </customer> </customers>
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT