In: Computer Science
c# language
Create a class “Person” which included first name, last name, age,
gender, salary, and havekids (Boolean) variables. You have to
create constructors and prosperities for the class.
Create a “MatchingDemo” class. In the main function, the program
reads the number of people in the database from the
“PersonInfo.txt” file and creates a dynamic array of the object. It
also reads the people's information from “PersonInfo.txt” file and
save them into the array.
Give the user the ability to search gender, age, salary or havekids
and then display the result in descending order. If there is no
matching result, the program displays “No Matching Found”.
Create a search function with the parameters for search that one
person the user wanted.
Create a sorting function to display the result in descending
order.
8
Mario
Lopez
25
M
200000
Y
Emanuel
Goldstein
45
M
100000
N
Emily
Autumn
26
F
200000
Y
Betty
White
57
F
130000
N
Nancy
White
21
F
80000
Y
Irene
Godinez
25
F
5200
N
Brad
Pitt
60
M
0
Y
Cee
Garza
26
F
40000
N
visual studio
If you cant help stop responding with comments, I provided as much information as I could.
//--------------- Person.cs --------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Persons
{
public class Person
{
private string firstName;
private string lastName;
private int age;
private string gender;
private double salary;
private bool haveKids;
public string FirstName
{
get
{
return firstName;
}
set
{
firstName = value;
}
}
public string LastName
{
get
{
return lastName;
}
set
{
lastName = value;
}
}
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
public string Gender
{
get
{
return gender;
}
set
{
gender = value;
}
}
public double Salary
{
get
{
return salary;
}
set
{
salary = value;
}
}
public bool HaveKids
{
get
{
return haveKids;
}
set
{
haveKids = value;
}
}
Person()
{
}
public Person(string firstName, string lastName, int age, string
gender, double salary, bool haveKids)
{
this.FirstName = firstName;
this.LastName = lastName;
this.Age = age;
this.Gender = gender;
this.Salary = salary;
this.HaveKids = haveKids;
}
private string haveKidsFormat()
{
return haveKids ? "Y" : "N";
}
override
public String ToString()
{
return String.Format(" {0,-15:} {1,-10:} {2,-10:} {3,-10:}
{4,-10:0.00} {5,-15:}",
firstName,lastName,age,gender,salary,haveKidsFormat());
}
}
}
//------------- MatchingDemo.cs ------------
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Persons
{
class MatchingDemo
{
List<Person> persons;
MatchingDemo()
{
persons = new List<Person>();
}
private Person getData(string[] data)
{
string fName = data[0];
string lName = data[1];
int age;
string gender;
double salary;
bool haveKids;
try
{
age = Convert.ToInt32(data[2]);
}
catch(Exception)
{
Console.WriteLine("Invalid type data existed for the fields
age");
return null;
}
data[3] = data[3].ToUpper();
if (data[3].Equals("M") || data[3].Equals("F"))
{
gender = data[3];
}
else
{
Console.WriteLine("Invalid Gender");
return null;
}
try
{
salary = Convert.ToDouble(data[4]);
}
catch(Exception)
{
Console.WriteLine("Invalid Salary");
return null;
}
data[5] = data[5].ToUpper();
if (data[5].Equals("Y"))
{
haveKids = true;
}
else if (data[5].Equals("N"))
{
haveKids = false;
}
else
{
Console.WriteLine("Invalid Value for haveKids");
return null;
}
Person person = new Person(fName, lName, age, gender, salary,
haveKids);
return person;
}
private void loadPersons()
{
try
{
StreamReader sr = new StreamReader("PersonInfo.txt");
string line = sr.ReadLine();
while(line != null)
{
string[] data = line.Trim().Split(',');
if(data.Length != 6)
{
Console.WriteLine("\nError: Invalid number of fields in line: " +
line.Trim());
}
else
{
Person person = getData(data);
if(person == null)
{
Console.WriteLine("Can't insert Line: " + line + " Due to
errors");
}
else
{
persons.Add(person);
}
}
line = sr.ReadLine();
}
sr.Close();
}
catch(Exception)
{
Console.WriteLine("Exception Occured While Reading
PersonInfo.txt");
}
}
private void printPersons(List<Person> list)
{
Console.WriteLine("\n"+String.Format(" {0,-15:} {1,-10:} {2,-10:}
{3,-10:} {4,-10:0.00} {5,-15:}", "First Name", "Last Name", "Age",
"Gender", "Salary", "Have Kids"));
Console.WriteLine();
for (int i = 0;i<list.Count;i++)
{
Console.WriteLine(list[i].ToString());
}
}
private List<Person> searchByGender(String gender)
{
List<Person> result = new List<Person>();
Person person;
int c = 0;
for(int i = 0;i<persons.Count;i++)
{
person = (Person)persons[i];
if(person.Gender.Equals(gender))
{
result.Add(person);
c++;
}
}
result.Sort((a, b) => string.Compare(b.FirstName,
a.FirstName));
return result;
}
private List<Person> searchByAge(int age)
{
List<Person> result = new List<Person>();
Person person;
int c = 0;
for (int i = 0; i < persons.Count; i++)
{
person = (Person)persons[i];
if (person.Age == age)
{
result.Add(person);
c++;
}
}
result.Sort((a, b) => string.Compare(b.FirstName,
a.FirstName));
return result;
}
private List<Person> searchBySalary(double salary)
{
List<Person> result = new List<Person>();
Person person;
int c = 0;
for (int i = 0; i < persons.Count; i++)
{
person = (Person)persons[i];
if (person.Salary == salary)
{
result.Add(person);
c++;
}
}
result.Sort((a, b) => string.Compare(b.FirstName,
a.FirstName));
return result;
}
private List<Person> searchByHaveKids(bool haveKids)
{
List<Person> result = new List<Person>();
Person person;
int c = 0;
for (int i = 0; i < persons.Count; i++)
{
person = (Person)persons[i];
if (person.HaveKids == haveKids)
{
result.Add(person);
c++;
}
}
result.Sort((a, b) => string.Compare(b.FirstName,
a.FirstName));
return result;
}
private void menu()
{
Console.WriteLine("\n------- Menu -----------");
Console.WriteLine("1. Search by Age");
Console.WriteLine("2. Search by Gender");
Console.WriteLine("3. Search by Salary");
Console.WriteLine("4. Search by Have Kids");
Console.WriteLine("0. Exit");
Console.Write("Enter Your Option: ");
}
private void start()
{
loadPersons();
Console.WriteLine("--------- Loaded Persons From Database
----------\n");
printPersons(persons);
int choice =0;
do
{
menu();
try
{
choice = Convert.ToInt32(Console.ReadLine().Trim());
switch(choice)
{
case 1:
{
Console.Write("\nEnter Age: ");
int age;
age = Convert.ToInt32(Console.ReadLine().Trim());
if(age <0 || age > 100)
{
Console.WriteLine("\nInvalid Age Entered");
}
else
{
List<Person> ages = searchByAge(age);
if(ages.Count == 0)
{
Console.WriteLine("\nSorry No Persons Find by Age: " + age);
}
else
{
printPersons(ages);
}
}
break;
}
case 2:
{
Console.Write("\nEnter Gender: ");
string gender;
gender = Console.ReadLine().Trim().ToUpper();
if(gender.Equals("M") || gender.Equals("F"))
{
List<Person> genders = searchByGender(gender);
if (genders.Count == 0)
{
Console.WriteLine("\nSorry No Persons Find by Age: " +
gender);
}
else
{
printPersons(genders);
}
}
else
{
Console.WriteLine("\nInvalid Gender Entered");
}
break;
}
case 3:
{
Console.Write("\nEnter Salary: ");
double salary;
salary = Convert.ToDouble(Console.ReadLine().Trim());
if (salary < (double)0 )
{
Console.WriteLine("\nInvalid salary Entered");
}
else
{
List<Person> salaries = searchBySalary(salary); ;
if (salaries.Count == 0)
{
Console.WriteLine("\nSorry No Persons Find by Age: " +
salary);
}
else
{
printPersons(salaries);
}
}
break;
}
case 4:
{
Console.Write("\nEnter Have Kids(Y or N): ");
string haveKids;
haveKids = Console.ReadLine().Trim().ToUpper();
if (haveKids.Equals("Y") || haveKids.Equals("N"))
{
if (haveKids.Equals("Y"))
{
List<Person> kids = searchByHaveKids(true);
if (kids.Count == 0)
{
Console.WriteLine("\nSorry No Persons Find by Age: " +
haveKids);
}
else
{
printPersons(kids);
}
}
else
{
List<Person> kids = searchByHaveKids(false);
if (kids.Count == 0)
{
Console.WriteLine("\nSorry No Persons Find by Age: " +
haveKids);
}
else
{
printPersons(kids);
}
}
}
else
{
Console.WriteLine("\nInvalid Input Entered");
}
break;
}
default:
Console.WriteLine("Qutting....");
break;
}
}
catch(Exception)
{
Console.WriteLine("\nInvalid Input");
choice = -1;
}
} while (choice != 0);
}
static void Main(string[] args)
{
MatchingDemo test = new MatchingDemo();
test.start();
Console.WriteLine("Enter any key to Close");
Console.ReadKey();
}
}
}
//------------PersonInfo.txt -----------
Mario,Lopez,25,M,200000,Y
Emanuel,Goldstein,45,M,100000,N
Emily,Autumn,26,F,200000,Y
Betty,White,57,F,130000,N
Nancy,White,21,F,80000,Y
Irene,Godinez,25,F,5200,N
Brad,Pitt,60,M,0,Y
Cee,Garza,26,F,40000,N
//--------------- OUTPUT -----------