Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Persons
- {
- class ShowPerson
- {
- public static void Main()
- {
- Person FirstPerson = new Person("Traqn", 10);
- Console.WriteLine(FirstPerson);
- }
- }
- public class Person
- {
- private string name;
- private int age;
- private string email;
- public Person(string name, int age, string email)
- {
- this.Name = name;
- this.Age = age;
- this.Email = email;
- }
- public Person(string name, int age)
- : this(name, age, null)
- {
- }
- public string Name
- {
- get { return this.name; }
- set
- {
- if (String.IsNullOrEmpty(value))
- {
- throw new ArgumentException("Name cannot be empty!");
- }
- this.name = value;
- }
- }
- public int Age
- {
- get { return this.age; }
- set
- {
- if (value < 1 || value > 100)
- {
- throw new ArgumentException("Invalid age! It should be in the range [1..100]");
- }
- this.age = value;
- }
- }
- public string Email
- {
- get { return this.email; }
- set
- {
- if (IsValidEmail(value) == false && value != null)
- {
- throw new ArgumentException("Invalid email address");
- }
- this.email = value;
- }
- }
- public override string ToString()
- {
- return string.Format("Name: {0}, age: {1}, email: {2}", this.name, this.age, this.email ?? "[no email]");
- }
- private bool IsValidEmail(string email)
- {
- bool isValid = false;
- foreach (char ch in email)
- {
- if (ch == '@')
- {
- isValid = true;
- break;
- }
- }
- return isValid;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement