The code below is for a coding challenge for a job. I have everything working as they requested, I've reviewed it myself and believe everything is good. I'm just looking to see if anyone see's anything that I may have done in a way that may be considered bad practice, or if there is any obvious improvements that could be made to clean up the code or increase performance. Any feedback is welcome.
Here are the instructions given to me by the company:
- Present a menu to the user with 3 options [View Persons, Add Person, Exit]
- If the user selects 'View Persons', the application should show a list of persons in the application.
- If the user selects 'Add Person', they should be instructed to enter a name. The program should assign a unique id to the new person.
- If a person selects 'Exit', the application should close. Otherwise, the application should stay running and/or indicate that the choice was invalid.
- Optional: Create a 4th feature where you can search by name and the program indicates whether or not the name is in the system.
- Use an in memory list to store person objects.
- Use multiple classes and object oriented design to write your program (don't put all of your logic in the Program class).
- What we look for:
- Does the program meet the requirements/instructions and is it stable?
- Is the code clean & appropriately commented?
- Is there an understandable object oriented design?
using System;
using System.Collections.Generic;
using System.Linq;
namespace CSECodeSampleConsole
{
public class Program
{
public static void Main(string[] args)
{
Menu menu = new Menu();
menu.MainMenu();
}
}
internal class Person
{
public int Id { get; set; }
public string Name { get; set; }
public static int globalId = 1;
public Person(string name)
{
this.Name = name;
this.Id = globalId;
globalId++;
}
}
internal class Menu
{
List<Person> people = new List<Person>();
public void MainMenu() //Display and Navigate Menu.
{
Console.WriteLine("Please make a selection by entering the corresponding number:");
Console.WriteLine("1.View Persons");
Console.WriteLine("2.Add Person");
Console.WriteLine("3. Search For Person");
Console.WriteLine("4.Exit");
var input = Console.ReadLine();
switch (Convert.ToInt32(input))
{
case 1:
DisplayNames();
break;
case 2:
AddPerson();
break;
case 3:
SearchList();
break;
case 4:
Environment.Exit(0);
break;
default:
Console.WriteLine("Invalid Input\n\n");
MainMenu();
break;
}
return;
}
public void DisplayNames() //Display all names and their Id's in list.
{
if(people.Count == 0)
{
Console.WriteLine("No People To Display Yet.\n\n");
MainMenu();
}
else
{
Console.WriteLine("List of Current People:\n");
foreach (Person person in people)
{
Console.WriteLine(person.Id + " - " + person.Name);
}
Console.WriteLine("\n\n");
MainMenu();
}
return;
}
public void AddPerson() //Add a new person to the list.
{
Console.WriteLine("Please Enter The Person's Name: ");
var result = Console.ReadLine();
Person newPerson = new Person(result);
people.Add(newPerson);
Console.WriteLine(newPerson.Name + " added successfully.\n\n");
MainMenu();
return;
}
public void SearchList() //Find and display search results.
{
Console.WriteLine("Please Enter A Name To Search For: ");
var result = Console.ReadLine();
var searchResults = people.Where(n => n.Name.Contains(result, StringComparison.OrdinalIgnoreCase)).ToList();
if (searchResults.Count == 0)
{
Console.WriteLine("No Results Were Found");
}
else
{
Console.WriteLine("The Following Results Were Found: \n");
foreach (Person person in searchResults)
{
Console.WriteLine(person.Id + " - " + person.Name);
}
}
Console.WriteLine("\n\n");
MainMenu();
}
}
public static class MyExtensionMethods
{
public static bool Contains(this string source, string toCheck, StringComparison comp)
{ //Created this to take away the case sensitivity of the Contains method.
return source != null && toCheck != null && source.IndexOf(toCheck, comp) >= 0;
}
}
}