Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication1
- {
- class Persons
- {
- private string name = null;
- private int age = 0;
- private string email = null;
- public string Name
- {
- get { return this.name; }
- set
- {
- if (String.IsNullOrEmpty(value))
- {
- throw new Exception("Name cannot be empty!");
- }
- this.name = value;
- }
- }
- public int Age
- {
- get { return this.age; }
- set
- {
- if (this.age < 1 || this.age > 100)
- {
- throw new ArgumentOutOfRangeException ("Age shoud be in range 1...100!");
- }
- this.age = value;
- }
- }
- public string Email
- {
- get { return this.email; }
- set
- {
- if (ValidateEmail(this.email) == false)
- {
- throw new Exception("Invalid email. Email should contain \"@\"");
- }
- this.email = value;
- }
- }
- private bool ValidateEmail(string email)
- {
- bool isValidEmail = false;
- for (int i = 0; i < email.Length; i++)
- {
- if (email[i] == '@')
- {
- isValidEmail = true;
- break;
- }
- }
- return isValidEmail;
- }
- public Persons (string name, int age)
- {
- this.Name = name;
- this.Age = age;
- }
- public Persons(string name, int age, string email)
- {
- this.Name = name;
- this.Age = age;
- this.Email = email;
- }
- public override string ToString()
- {
- return "First person's name is " + this.name + "he/she is " + this.age + "years old and his/her email is " + this.email;
- }
- static void Main(string[] args)
- {
- Persons Ani = new Persons("Ani", 35);
- Persons Lili = new Persons("Lili", 3, "Lili@baby.tv");
- // Persons Rumi = new Persons ("Rumi", 102);
- // Persons Mitko = new Persons ("", 40, "noemail");
- Console.WriteLine(Ani);
- Console.WriteLine(Lili);
- // Console.WriteLine(Rumi);
- // Console.WriteLine(Mitko);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement