vakho

C# Lab - [21/11/12]

Nov 21st, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace MyConApp
  7. {
  8.     class Program
  9.     {
  10.         static List<Person> PersonList = null;
  11.  
  12.         static void Main(string[] args)
  13.         {
  14.             PersonList = new List<Person>();
  15.  
  16.             bool Exit = false;
  17.             while (!Exit)
  18.             {
  19.                 string menuIndex = ShowMenu();
  20.                 Exit = ActionsMenu(menuIndex);
  21.             }
  22.         }
  23.  
  24.         #region Person Functions
  25.  
  26.         public static void NewPerson()
  27.         {
  28.             Person p = new Person();
  29.             Console.Write("Input Name: ");
  30.             p.FirstName = Console.ReadLine();
  31.             Console.Write("Input Surname: ");
  32.             p.LastName = Console.ReadLine();
  33.             Console.Write("Input Age: ");
  34.             p.Age = int.Parse(Console.ReadLine());
  35.             PersonList.Add(p);
  36.         }
  37.  
  38.         public static void SearchPerson()
  39.         {
  40.             Console.Write("Input Filter: ");
  41.             string filter = Console.ReadLine();
  42.            
  43.             List<Person> FoundPersons = PersonList.FindAll(delegate(Person p)
  44.             {
  45.                 return p.FirstName.Contains(filter) ||
  46.                        p.LastName.Contains(filter) ||
  47.                        p.Age.ToString().Contains(filter);
  48.             });
  49.  
  50.             foreach (Person i in FoundPersons)
  51.             {
  52.                 Console.WriteLine(i.ToString());
  53.             }
  54.            
  55.         }
  56.  
  57.         public static void EditPerson()
  58.         {
  59.             Console.Write("Input person index: ");
  60.             int i = int.Parse(Console.ReadLine());
  61.  
  62.             string FirstName = "";
  63.             Console.WriteLine("FirstName: {0}", FirstName = Console.ReadLine());
  64.  
  65.             string LastName = "";
  66.             Console.WriteLine("LastName: {0}", LastName = Console.ReadLine());
  67.  
  68.             string Age = "";
  69.             Console.WriteLine("FirstName: {0}", Age = Console.ReadLine());
  70.  
  71.             Person p = PersonList[i];
  72.  
  73.             if (!string.IsNullOrWhiteSpace(FirstName)) p.FirstName = FirstName;
  74.             if (!string.IsNullOrWhiteSpace(LastName)) p.LastName = LastName;
  75.             if (!string.IsNullOrWhiteSpace(Age)) p.Age = int.Parse(Age);
  76.  
  77.             Console.Write("Person has been updated successfully!");
  78.  
  79.             Console.WriteLine(PersonList[i].ToString());
  80.         }
  81.  
  82.         public static void DeletePerson()
  83.         {
  84.             Console.Write("Input index of the person you are willing to delete: ");
  85.  
  86.             int i = int.Parse(Console.ReadLine());
  87.  
  88.             PersonList.RemoveAt(i);
  89.  
  90.             Console.Write("Person at index of {0} has been successfully delted!", i);
  91.         }
  92.  
  93.         public static void ClearListAndEverything()
  94.         {
  95.             PersonList.Clear();
  96.             Console.Clear();
  97.         }
  98.  
  99.         #endregion
  100.  
  101.         private static bool ActionsMenu(string menuIndex)
  102.         {
  103.             bool Exit = false;
  104.             switch (menuIndex)
  105.             {
  106.                 case "1": NewPerson(); break; //New Person
  107.                 case "2": SearchPerson(); break; //Search Person    
  108.                 case "3": EditPerson(); break; //Edit Person
  109.                 case "4": DeletePerson(); break; //Delete Person
  110.                 case "5": ClearListAndEverything(); break; //Clear List
  111.                 case "6": Exit = true; break; //Exit
  112.                 default: Console.WriteLine("You have inputted wrong number! Please try again..."); break;
  113.             }
  114.             return Exit;
  115.         }
  116.  
  117.         private static string ShowMenu()
  118.         {
  119.             Console.WriteLine("\"Person Creator v. 0.1\"");
  120.             Console.WriteLine("1. New Person");
  121.             Console.WriteLine("2. Search Person");
  122.             Console.WriteLine("3. Edit Person");
  123.             Console.WriteLine("4. Delete Person");
  124.             Console.WriteLine("5. Clear List");
  125.             Console.WriteLine("6. Exit");
  126.  
  127.             string menuIndex = Console.ReadLine();
  128.  
  129.             return menuIndex;
  130.         }
  131.     }
  132.  
  133.     public class Person
  134.     {
  135.         public string FirstName { get; set; }
  136.         public string LastName { get; set; }
  137.         public int Age { get; set; }
  138.  
  139.         public Person(string F = "Noname", string L = "Noname", int A = 0)
  140.         {
  141.             this.FirstName = F;
  142.             this.LastName = L;
  143.             this.Age = A;
  144.         }
  145.  
  146.         public override string ToString()
  147.         {
  148.             return FirstName + "\t" + LastName + "\t" + Age.ToString();
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment