The document describes a C# program that searches for names in two text files containing lists of the most popular boy and girl names from 2000-2009. The program loads the names into lists from the text files, then allows the user to search for a boy's name, girl's name, or both. It will display whether the searched names were among the most popular or not.
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
The problem is called Name Search and it has to be in C# 6. .pdf
1. The problem is called "Name Search" and it has to be in C#
6. Name Search
In the Chap07 folder of the Student Sample Programs, you will find the following
files:
• GirlNames.txt—This file contains a list of the 200 most popular names given to
girls born in the United States from 2000 through 2009.
• BoyNames.txt—This file contains a list of the 200 most popular names given to
boys born in the United States from 2000 through 2009.
Create an application that reads the contents of the two files into two separate
arrays or Lists. The user should be able to enter a boy’s name, a girl’s name, or
both, and the application should display
Solution
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace NameSearch
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//Intitialize the form.
}
2. private void btnSearch_Click(object sender, EventArgs e)
{//On clicking the search button..
string fileName;//fileName variable
string fileName2;//second filename variable
string searchName;//search name variable from user input
string searchName2;//second search name variable from user input
string nameType;//type of name(boy/girl)
try//try to..
{
if (txtBoysInput.Text != "" || txtGirlsInput.Text != "")
//If either of the textboxes are not empty..
{
if (txtBoysInput.Text != null && txtGirlsInput.Text == "")
{
//if the boys text box is not empty
fileName = "BoyNames.txt";
nameType = "boy's";
//set the file name, and the name type to reflect the masculine gender
searchName = txtBoysInput.Text;
//set the search name variable to the boy's contents.
parseSearchSingleFile(fileName, searchName, nameType);
//Parse the file into a list, and search it.
}
else if (txtGirlsInput.Text != null && txtBoysInput.Text == "")
{//if the girls textbox is not empty and the boys is empty..
fileName = "GirlNames.txt";
nameType = "girl's";
//set the file name, and the name type to reflect the feminine gender
searchName = txtGirlsInput.Text;
//set the search name to the user's input
parseSearchSingleFile(fileName, searchName, nameType);
//parse the file into a list, and search the list.
}
else if (txtGirlsInput.Text != null && txtBoysInput.Text != null)
{
fileName = "GirlNames.txt";
3. searchName = txtBoysInput.Text;
fileName2 = "BoyNames.txt";
searchName2 = txtGirlsInput.Text;
//set the file names, and collect the user input for the search names.
parseSearchBothFiles(fileName, fileName2, searchName, searchName2);
//parse the files, and search both of them.
}
}
else
{
MessageBox.Show("Enter a name, please.");
//Show the messagebox for empty textboxes. AKA prompt for user entry.
}
}
catch (Exception ex)
{
MessageBox.Show("Error Code: " + ex);
//if something goes wrong, show the error code, please.
}
}
private void parseSearchSingleFile(string fileName, string searchName, string nameType)
{
List names = new List();
//create a list array of names
using (var reader = new StreamReader(fileName))
{
while (reader.Peek() >= 0)
//if there's more than 0 lines left..
names.Add(reader.ReadLine());
//add the line to the list.
}
if (searchName != "")
{//if the searchname is not empty
if (names.Contains(searchName))
{//and the list contains the search name..
4. MessageBox.Show("The name " + searchName + "is among the most popular " +
nameType + " names!");
}//Tell us about it.
else
{//otherwise..
MessageBox.Show("The name was not among the most popular " + nameType +
" names.");
}//Tell us it isn't in the list.
}
else
{
MessageBox.Show("Enter a name, please.");
}//if it is empty, prompt for user entry.
}
private void parseSearchBothFiles(string fileName, string fileName2, string searchName,
string searchName2)
{
List girlsNames = new List();
//create a girls name list
using (var girlsReader = new StreamReader(fileName))
{
while (girlsReader.Peek() >= 0)//If there's more than 0 valid lines
girlsNames.Add(girlsReader.ReadLine());//add the line to the list
}
if (searchName != "" && searchName2 != "")
{//if both search names aren't empty..
List boysNames = new List();
//create a list of boy's names
using (var boysReader = new StreamReader(fileName2))
{
while (boysReader.Peek() >= 0)//while there are more than 0 lines left
boysNames.Add(boysReader.ReadLine());
}
if (girlsNames.Contains(searchName2) && boysNames.Contains(searchName))
{//if both names are on the list
MessageBox.Show("The name " + searchName2 + " was among the most popular
5. girl's names, and the name " + searchName + "was among the most popular boy's names!");
}//tell us about it
else if (boysNames.Contains(searchName))
{//if just the boy's name is on the list
MessageBox.Show("The name " + searchName + "was among the most popular
boy's names!");
}//tell us about it
else if (girlsNames.Contains(searchName2))
{//if just the girls name is on the list
MessageBox.Show("The name " + searchName2 + " was among the most popular
girl's names!");
}//tell us about it
else
{
MessageBox.Show("The name was not amongst the most popular names.");
}//If not, let us know as well.
}
else
{//if the search names were empty..
MessageBox.Show("Enter a name, please.");
}//Prompt for valid user input.
}
}
}
Form1.Designer.cs
namespace NameSearch
{
partial class Form1
{
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;
///
/// Clean up any resources being used.
6. ///
/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.txtBoysInput = new System.Windows.Forms.TextBox();
this.txtGirlsInput = new System.Windows.Forms.TextBox();
this.lblBoysInput = new System.Windows.Forms.Label();
this.lblInputGirls = new System.Windows.Forms.Label();
this.btnSearch = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// txtBoysInput
//
this.txtBoysInput.Location = new System.Drawing.Point(105, 48);
this.txtBoysInput.Name = "txtBoysInput";
this.txtBoysInput.Size = new System.Drawing.Size(100, 20);
this.txtBoysInput.TabIndex = 0;
//
// txtGirlsInput
//
this.txtGirlsInput.Location = new System.Drawing.Point(105, 74);
this.txtGirlsInput.Name = "txtGirlsInput";
this.txtGirlsInput.Size = new System.Drawing.Size(100, 20);