Question

In: Computer Science

c# language Create a class “Person” which included first name, last name, age, gender, salary, and...

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.

Solutions

Expert Solution

//--------------- 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 -----------


Related Solutions

C# (Thank you in advance) Create an Employee class with five fields: first name, last name,...
C# (Thank you in advance) Create an Employee class with five fields: first name, last name, workID, yearStartedWked, and initSalary. It includes constructor(s) and properties to initialize values for all fields. Create an interface, SalaryCalculate, class that includes two functions: first,CalcYearWorked() function, it takes one parameter (currentyear) and calculates the number of year the worker has been working. The second function, CalcCurSalary() function that calculates the current year salary. Create a Worker classes that is derived from Employee and SalaryCalculate...
Write a class named Person with data attributes for a person’s first name, last name, and...
Write a class named Person with data attributes for a person’s first name, last name, and telephone number. Next, write a class named Customer that is a subclass of the Person class. The Customer class should have a data attribute for a customer number and a Boolean data attribute indicating whether the customer wishes to be on a calling list. Demonstrate an instance of the Customer class in a simple program. Using python
Specifications Create an abstract Employee class that provides private attributes for the first name, last name,...
Specifications Create an abstract Employee class that provides private attributes for the first name, last name, email address, and social security number. This class should provide functions that set and return the employee’s first name, last name, email address, and social security number. This class has a function: get_net_income which returns 0. Create a Manager class that inherits the Employee class. This class should add private attributes for years of experience and the annual salary. This class should also provide...
JAVA Program Create a class called SoccerPlayer Create 4 private attributes: First Name, Last Name, Games,...
JAVA Program Create a class called SoccerPlayer Create 4 private attributes: First Name, Last Name, Games, and Goals Have two constructors Constructor 1 – default constructor; all values to "NONE" or zero Constructor 2 – accepts input of first name, last name, games and goals. Create get and set methods for each of the four attributes Create a method the returns a double that calculates the average goals per game This method checks for zero games played: If there are...
Consider the following class: class Person {         String name;         int age;        ...
Consider the following class: class Person {         String name;         int age;         Person(String name, int age){                this.name = name;                this.age = age;         } } Write a java program with two classes “Teacher” and “Student” that inherit the above class “Person”. Each class has three components: extra variable, constructor, and a method to print the student or the teacher info. The output may look like the following (Hint: you may need to use “super”...
JAVA PROGRAMMING Part 1 Create a class Student, with attributes id, first name, last name. (All...
JAVA PROGRAMMING Part 1 Create a class Student, with attributes id, first name, last name. (All the attributes must be String) Create a constructor that accepts first name and last name to create a student object. Create appropriate getters and setters Create another class StudentOperationClient, that contains a main program. This is the place where the student objects are created and other activities are performed. In the main program, create student objects, with the following first and last names. Chris...
Create a class Employee. Your Employee class should include the following attributes: First name (string) Last...
Create a class Employee. Your Employee class should include the following attributes: First name (string) Last name (string) Employee id (string) Employee home street address (string) Employee home city (string) Employee home state (string) Write a constructor to initialize the above Employee attributes. Create another class HourlyEmployee that inherits from the Employee class.   HourEmployee must use the inherited parent class variables and add in HourlyRate and HoursWorked. Your HourEmployee class should contain a constructor that calls the constructor from the...
Please Code Using Java Create a class called SoccerPlayer Create 4 private attributes: First Name, Last...
Please Code Using Java Create a class called SoccerPlayer Create 4 private attributes: First Name, Last Name, Games, and Goals Have two constructors Constructor 1 – default constructor; all values to "NONE" or zero Constructor 2 – accepts input of first name, last name, games and goals. Create get and set methods for each of the four attributes Create a method the returns a double that calculates the average goals per game This method checks for zero games played: If...
In C, 1) Create variables for: your first and last name total round trip to school...
In C, 1) Create variables for: your first and last name total round trip to school and home (assuming you don't live on campus - make it your hometown). cost of gas 2) Create a program that will: output your first name and your total miles driven for the week. output your last name with the total costs per week
directions: use c++ Create a  ContactInfo class that contains the following member variables: name age phoneNumber The...
directions: use c++ Create a  ContactInfo class that contains the following member variables: name age phoneNumber The ContactInfo class should have a default constructor that sets name = "", phoneNumber = "", and age = 0. The ContactInfo class should have a constructor that accepts the name and phone number as parameters and sets name = <value of parameter name>, aAge = 0, and phoneNumber = <value of parameter phone number>. The ContactInfo class should have accessor and mutator functions for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT