Advertisement
AvengersAssemble

Phonebook Manager C#

Sep 30th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication25
  8. {
  9.     public delegate void isDateValid(int falseInfo, string falseType);
  10.     enum Months : byte { January, February, March, April, May, June, July, August, September, October, November, December, False };
  11.     public class Date
  12.     {
  13.         private int year;
  14.         private Months month;
  15.         private int day;
  16.         private bool isFalse = false;
  17.         public isDateValid dateCheck;
  18.         public Date(int year, int month, int day)
  19.         {
  20.             this.year = year;
  21.             if (month > 0 && month < 13)
  22.             {
  23.                 this.month = (Months)month-1;
  24.             }
  25.             else
  26.             {
  27.                 this.month = (Months)13;
  28.                 isFalse = true;
  29.             }
  30.                 this.day = day;
  31.         }
  32.         public void DayMonthCheck()
  33.         {
  34.             if (month != (Months) 13)
  35.             {
  36.                 if ((int)month > 2 && (int)month < 8)
  37.                 {
  38.                     if ((int)month % 2 == 0)
  39.                     {
  40.                         if (day > 30)
  41.                         {
  42.                             dateCheck(day, "day");
  43.                             isFalse = true;
  44.                         }
  45.                     }
  46.                     else
  47.                     {
  48.                         if (day > 31)
  49.                         {
  50.                             dateCheck(day, "day");
  51.                             isFalse = true;
  52.                         }
  53.                     }
  54.                 }
  55.                 else if ((int)month >= 8)
  56.                 {
  57.                     if( (int) month % 2 == 0)
  58.                     {
  59.                         if (day > 31)
  60.                         {
  61.                             dateCheck(day, "day");
  62.                             isFalse = true;
  63.                         }
  64.                     }
  65.                     else
  66.                     {
  67.                         if (day > 30)
  68.                         {
  69.                             dateCheck(day, "day");
  70.                             isFalse = true;
  71.                         }
  72.                     }
  73.                 }
  74.                 else if ((int)month == 1)
  75.                 {
  76.                     if (day > 31)
  77.                     {
  78.                         dateCheck(day, "day");
  79.                         isFalse = true;
  80.                     }
  81.                 }
  82.                 else if ((int)month == 2)
  83.                 {
  84.                     if (day > 28)
  85.                     {
  86.                         dateCheck(day, "day");
  87.                         isFalse = true;
  88.                     }
  89.                 }
  90.             }
  91.             else
  92.                 dateCheck((int)month, "month");
  93.             if (day < 1)
  94.             {
  95.                 dateCheck(day, "day");
  96.                 isFalse = true;
  97.             }
  98.         }
  99.         public bool MonthSearch(string month)
  100.         {
  101.             if(this.month.ToString() == month || Convert.ToString(Convert.ToInt32(this.month + 1)) == month)
  102.                 return true;
  103.             return false;
  104.         }
  105.         public void Print()
  106.         {
  107.             if (!isFalse)
  108.             {
  109.                     Console.Write("\n{0}/{1}/{2}\n\n", day, (int)month+1, year);
  110.             }
  111.         }
  112.     }
  113.     public class Phonebook
  114.     {
  115.         private string firstName;
  116.         private string lastName;
  117.         private string phoneNum;
  118.         private string city;
  119.         public Phonebook(string fName, string lName, string phoneNum, string city)
  120.         {
  121.             firstName = fName;
  122.             lastName = lName;
  123.             this.phoneNum = phoneNum;
  124.             this.city = city;
  125.         }
  126.         public bool CitySearch(string city)
  127.         {
  128.             if(this.city == city)
  129.                 return true;
  130.             return false;
  131.         }
  132.         public bool NameSearch(string nameFL)
  133.         {
  134.             if (firstName == nameFL || lastName == nameFL)
  135.                 return true;
  136.             return false;
  137.         }
  138.         public void Print()
  139.         {
  140.             Console.Write("\nName: {0} {1}\nPhone number: {2}\nCity: {3}\nDate of birth: ", firstName, lastName, phoneNum, city);
  141.         }
  142.     }
  143.     class Program
  144.     {
  145.         static void Main(string[] args)
  146.         {
  147.             bool endChanges;
  148.             int i = 0;
  149.             Phonebook[] book = new Phonebook[100];
  150.             Date[] dateOfBirth = new Date[100];
  151.             for (int a = 0; a < book.Length; a++)
  152.             {
  153.                 Console.Write("Last name: ");
  154.                 string lName = Console.ReadLine();
  155.                 Console.Write("First name: ");
  156.                 string fName = Console.ReadLine();
  157.                 Console.Write("Phone number: ");
  158.                 string phoneNum = Console.ReadLine();
  159.                 Console.Write("City: ");
  160.                 string city = Console.ReadLine();
  161.                 Console.Write("Date of birth -\nYear: ");
  162.                 int year = int.Parse(Console.ReadLine());
  163.                 Console.Write("Month: ");
  164.                 int month = int.Parse(Console.ReadLine());
  165.                 Console.Write("Day: ");
  166.                 int day = int.Parse(Console.ReadLine());
  167.                 dateOfBirth[i] = new Date(year, month, day);
  168.                 dateOfBirth[i].dateCheck = new isDateValid(DateValidation);
  169.                 dateOfBirth[i].DayMonthCheck();
  170.                 book[i] = new Phonebook(lName, fName, phoneNum, city);
  171.                 book[i].Print();
  172.                 dateOfBirth[i].Print();
  173.                 i++;
  174.                 endChanges = false;
  175.                 while (!endChanges)
  176.                 {
  177.                     Console.WriteLine("Press '1' to add new member\nPress '2' to delete member\nPress '3' to  print all members\nPress '4' to search by city\nPress '5' to search by month of birth\nPress '6' to search by name");
  178.                     int selection = int.Parse(Console.ReadLine());
  179.                     switch (selection)
  180.                     {
  181.                         case 1:
  182.                             endChanges = true;
  183.                             continue;
  184.                         case 2:
  185.                             Console.WriteLine("Which member would you like to delete (enter member number)");
  186.                             int member = int.Parse(Console.ReadLine());
  187.                             book[member - 1] = null;
  188.                             dateOfBirth[member - 1] = null;
  189.                             for (int m = member - 1; m < i; m++)
  190.                             {
  191.                                 book[m] = book[m + 1];
  192.                                 dateOfBirth[m] = dateOfBirth[m + 1];
  193.                             }
  194.                             book[i] = null;
  195.                             dateOfBirth[i] = null;
  196.                             i--;
  197.                             Console.WriteLine();
  198.                             break;
  199.                         case 3:
  200.                             for (int k = 0; k < i; k++)
  201.                             {
  202.                                 book[k].Print();
  203.                                 dateOfBirth[k].Print();
  204.                             }
  205.                             break;
  206.                         case 4:
  207.                             Console.WriteLine("Specify city: ");
  208.                             string cityC = Console.ReadLine();
  209.                             for (int l = 0; l < i; l++)
  210.                             {
  211.                                 if (book[l].CitySearch(cityC))
  212.                                 {
  213.                                     book[l].Print();
  214.                                     dateOfBirth[l].Print();
  215.                                 }
  216.                             }
  217.                             break;
  218.                         case 5:
  219.                             Console.WriteLine("Specify month of birth: ");
  220.                             string monthC = Console.ReadLine();
  221.                             for (int l = 0; l < i; l++)
  222.                             {
  223.                                 if (dateOfBirth[l].MonthSearch(monthC))
  224.                                 {
  225.                                     book[l].Print();
  226.                                     dateOfBirth[l].Print();
  227.                                 }
  228.                             }
  229.                             break;
  230.                         case 6:
  231.                             Console.WriteLine("Specify name(first/last): ");
  232.                             string nameFL = Console.ReadLine();
  233.                             for (int l = 0; l < i; l++)
  234.                             {
  235.                                 if (book[l].NameSearch(nameFL))
  236.                                 {
  237.                                     book[l].Print();
  238.                                     dateOfBirth[l].Print();
  239.                                 }
  240.                             }
  241.                             break;
  242.                         default:
  243.                             Console.WriteLine("Invalid request!\n");
  244.                             break;
  245.                     }
  246.                 }
  247.             }
  248.         }
  249.         static void DateValidation(int date, string type)
  250.         {
  251.             Console.WriteLine("Invalid date! {0} {1} doesn't exist.", type, date);
  252.         }
  253.     }
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement