Описание класса TMan, создающего тип «Человек» - C#
Формулировка задачи:
Здравствуйте,помогите с лабораторной ,только начал изучать язык,а требуют уже сейчас выполнить следующее-
Составить описание класса TMan, создающего тип «Человек». Определите элементы-данные класса:
― фамилия;
― имя;
― отчество;
― возраст;
― адрес человека.
Предусмотрите следующие конструкторы класса:
― по умолчанию;
― получающий параметры;
Напишите функции-методы класса:
― позволяющие изменять данные о человеке раздельно и проверяющие допустимость вводимых значений. В случае недопустимых значений полей выбрасываются исключения.
― определения, является ли человек избирателем, если учитывать только его возраст;
― определения соответствия фамилии человека некоторой заданной.
Написать программу, демонстрирующую работу с этим классом. Программа должна содержать меню, позволяющее осуществить проверку всех методов класса.
Буду очень признателен за помощь,спасибо.
Решение задачи: «Описание класса TMan, создающего тип «Человек»»
textual
Листинг программы
using System;
using System.Linq;
namespace Man
{
class Program
{
class Man
{
string sName;
public string SName { get { return sName; } }
string fName;
public string FName { get { return fName; } }
string pName;
public string PName { get { return pName; } }
int age;
public int Age { get { return age; } }
string adres;
public string Adres { get { return adres; } }
public void PrintInfo()
{
Console.WriteLine("FName: {0}\nSName: {1}\nPName: {2}\nAge: {3}\nAdres: {4}\nPress any key", fName, sName, pName, age.ToString(), adres);
Console.ReadKey();
}
public Man()
{
}
public Man(string sname, string fname, string pname, int a, string adr)
{
sName = sname;
fName = fname;
pName = pname;
age = a;
adres = adr;
}
public bool SetFName(string value)
{
bool result = false;
if (value.Length >= 3)
{
result = true;
fName = value;
}
return result;
}
public bool SetSName(string value)
{
bool result = false;
if (value.Length >= 3)
{
result = true;
sName = value;
}
return result;
}
public bool SetPName(string value)
{
bool result = false;
if (value.Length >= 3)
{
result = true;
pName = value;
}
return result;
}
public bool SetAge(int value)
{
bool result = false;
if (value >= 0)
{
result = true;
age = value;
}
return result;
}
public bool SetAdres(string value)
{
bool result = false;
if (value.Length >= 3)
{
result = true;
adres = value;
}
return result;
}
public bool IsVoter()
{
return (age >= 21) ? true : false;
}
public bool IsSNameEquals(string value)
{
return (sName.Equals(value)) ? true : false;
}
}
static Man man;
static void InputMan()
{
Console.Write("Input first name: ");
string fname = Console.ReadLine();
Console.Write("Input surname: ");
string sname = Console.ReadLine();
Console.Write("Input patronymic: ");
string pname = Console.ReadLine();
Console.Write("Input age: ");
int age = int.Parse(Console.ReadLine());
Console.Write("Input adres: ");
string adres = Console.ReadLine();
Console.Clear();
man = new Man(sname, fname, pname, age, adres);
man.PrintInfo();
}
static void menu()
{
while (true)
{
int userChoose;
Console.Clear();
Console.WriteLine("1. Create man (default)");
Console.WriteLine("2. Create man (Custom)");
Console.WriteLine("3. Set first name");
Console.WriteLine("4. Set surname");
Console.WriteLine("5. Set patronymic");
Console.WriteLine("6. Set age");
Console.WriteLine("7. Set adres");
Console.WriteLine("8. Is voter");
Console.WriteLine("9. Check surname");
Console.WriteLine("0. Exit");
userChoose = int.Parse(Console.ReadKey().KeyChar.ToString());
Console.Clear();
switch (userChoose)
{
case 0: return; break;
case 1: { man = new Man(); man.PrintInfo(); } break;
case 2: { InputMan(); } break;
case 3:
{
Console.Write("Intput first name: ");
string fname = Console.ReadLine();
Console.Clear();
if (man.SetFName(fname))
man.PrintInfo();
else
{
Console.Write("Error.");
Console.ReadKey();
}
} break;
case 4:
{
Console.Write("Intput surname: ");
string sname = Console.ReadLine();
Console.Clear();
if (man.SetSName(sname))
man.PrintInfo();
else
{
Console.Write("Error.");
Console.ReadKey();
}
} break;
case 5:
{
Console.Write("Intput patronymic: ");
string pname = Console.ReadLine();
Console.Clear();
if (man.SetPName(pname))
man.PrintInfo();
else
{
Console.Write("Error.");
Console.ReadKey();
}
} break;
case 6:
{
Console.Write("Intput age: ");
string age = Console.ReadLine();
Console.Clear();
if (man.SetAge(int.Parse(age)))
man.PrintInfo();
else
{
Console.Write("Error.");
Console.ReadKey();
}
} break;
case 7:
{
Console.Write("Intput adres: ");
string adres = Console.ReadLine();
Console.Clear();
if (man.SetAdres(adres))
man.PrintInfo();
else
{
Console.Write("Error.");
Console.ReadKey();
}
} break;
case 8:
{
if (man.IsVoter())
Console.Write("Yes");
else
Console.Write("No");
Console.ReadKey();
} break;
case 9:
{
Console.Write("Input surname for check: ");
string sname = Console.ReadLine();
if (man.IsSNameEquals(sname))
Console.Write("Yes, value is equals");
else
Console.Write("No");
Console.ReadKey();
} break;
default: { continue; }
}
}
}
static void Main(string[] args)
{
menu();
}
}
}