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;
- namespace MyConApp
- {
- class Program
- {
- static List<Person> PersonList = null;
- static void Main(string[] args)
- {
- PersonList = new List<Person>();
- bool Exit = false;
- while (!Exit)
- {
- string menuIndex = ShowMenu();
- Exit = ActionsMenu(menuIndex);
- }
- }
- #region Person Functions
- public static void NewPerson()
- {
- Person p = new Person();
- Console.Write("Input Name: ");
- p.FirstName = Console.ReadLine();
- Console.Write("Input Surname: ");
- p.LastName = Console.ReadLine();
- Console.Write("Input Age: ");
- p.Age = int.Parse(Console.ReadLine());
- PersonList.Add(p);
- }
- public static void SearchPerson()
- {
- Console.Write("Input Filter: ");
- string filter = Console.ReadLine();
- List<Person> FoundPersons = PersonList.FindAll(delegate(Person p)
- {
- return p.FirstName.Contains(filter) ||
- p.LastName.Contains(filter) ||
- p.Age.ToString().Contains(filter);
- });
- foreach (Person i in FoundPersons)
- {
- Console.WriteLine(i.ToString());
- }
- }
- public static void EditPerson()
- {
- Console.Write("Input person index: ");
- int i = int.Parse(Console.ReadLine());
- string FirstName = "";
- Console.WriteLine("FirstName: {0}", FirstName = Console.ReadLine());
- string LastName = "";
- Console.WriteLine("LastName: {0}", LastName = Console.ReadLine());
- string Age = "";
- Console.WriteLine("FirstName: {0}", Age = Console.ReadLine());
- Person p = PersonList[i];
- if (!string.IsNullOrWhiteSpace(FirstName)) p.FirstName = FirstName;
- if (!string.IsNullOrWhiteSpace(LastName)) p.LastName = LastName;
- if (!string.IsNullOrWhiteSpace(Age)) p.Age = int.Parse(Age);
- Console.Write("Person has been updated successfully!");
- Console.WriteLine(PersonList[i].ToString());
- }
- public static void DeletePerson()
- {
- Console.Write("Input index of the person you are willing to delete: ");
- int i = int.Parse(Console.ReadLine());
- PersonList.RemoveAt(i);
- Console.Write("Person at index of {0} has been successfully delted!", i);
- }
- public static void ClearListAndEverything()
- {
- PersonList.Clear();
- Console.Clear();
- }
- #endregion
- private static bool ActionsMenu(string menuIndex)
- {
- bool Exit = false;
- switch (menuIndex)
- {
- case "1": NewPerson(); break; //New Person
- case "2": SearchPerson(); break; //Search Person
- case "3": EditPerson(); break; //Edit Person
- case "4": DeletePerson(); break; //Delete Person
- case "5": ClearListAndEverything(); break; //Clear List
- case "6": Exit = true; break; //Exit
- default: Console.WriteLine("You have inputted wrong number! Please try again..."); break;
- }
- return Exit;
- }
- private static string ShowMenu()
- {
- Console.WriteLine("\"Person Creator v. 0.1\"");
- Console.WriteLine("1. New Person");
- Console.WriteLine("2. Search Person");
- Console.WriteLine("3. Edit Person");
- Console.WriteLine("4. Delete Person");
- Console.WriteLine("5. Clear List");
- Console.WriteLine("6. Exit");
- string menuIndex = Console.ReadLine();
- return menuIndex;
- }
- }
- public class Person
- {
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public int Age { get; set; }
- public Person(string F = "Noname", string L = "Noname", int A = 0)
- {
- this.FirstName = F;
- this.LastName = L;
- this.Age = A;
- }
- public override string ToString()
- {
- return FirstName + "\t" + LastName + "\t" + Age.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment