Store name + number and sort in size order based on the numbers

后端 未结 5 1598
梦如初夏
梦如初夏 2021-01-24 07:20

Doing a college project and I\'m a bit stuck..

Basically, I need to take the string input for an employee name and an integer input for the amount of properties they sol

5条回答
  •  情话喂你
    2021-01-24 07:53

    Probably the easiest way is to store the values together in a single list, by creating a simple class to hold them:

    public class Realtor
    {
        public string Name { get; set; }
        public int PropertiesSold { get; set; }
    }
    

    Then you can populate instances of this class from the user input, add them to a list, and then use the linq extenstion method OrderByDescending to order the items by the realtor.PropertiesSold property:

    public static void Main(string[] args)
    {
        var realtors = new List();
    
        for (var i = 0; i < 3; i++)
        {
            var realtor = new Realtor();
    
            Console.Write("Please enter the employee name: ");
            realtor.Name = Console.ReadLine();
    
            realtor.PropertiesSold = GetIntFromUser(
                "Please enter the number of Properties Sold: ", x => x >= 0);
    
            realtors.Add(realtor);
            Console.WriteLine();
        }
    
        realtors = realtors.OrderByDescending(realtor => realtor.PropertiesSold).ToList();
    
        Console.WriteLine("Results in order of most properties sold:\n");
        Console.WriteLine($"{"Name".PadRight(10)} Properties Sold");
        Console.WriteLine($"{new string('-', 10)} {new string('-', 15)}");
    
        foreach (var realtor in realtors)
        {
            Console.WriteLine($"{realtor.Name.PadRight(10)} {realtor.PropertiesSold}");
        }
    
        GetKeyFromUser("\nDone! Press any key to exit...");
    }
    

    Sample Output


    Note

    The sample code above uses a method called GetIntFromUser to get the number of properties sold. The reason for this is that we can't expect that the users will always enter a valid number, so we want to do some validation on it first.

    The method (below) takes in a string that is used to prompt the user for input, and it takes an optional validation function that can be used to ensure that the value they enter is allowed (in the sample above, I specified that the value must be greater than or equal to zero).

    Here is the method used in the sample above:

    public static int GetIntFromUser(string prompt, Func validator = null)
    {
        int result;
        var cursorTop = Console.CursorTop;
    
        do
        {
            ClearSpecificLineAndWrite(cursorTop, prompt);
        } while (!int.TryParse(Console.ReadLine(), out result) ||
                 !(validator?.Invoke(result) ?? true));
    
        return result;
    }
    
    private static void ClearSpecificLineAndWrite(int cursorTop, string message)
    {
        Console.SetCursorPosition(0, cursorTop);
        Console.Write(new string(' ', Console.WindowWidth));
        Console.SetCursorPosition(0, cursorTop);
        Console.Write(message);
    }
    

提交回复
热议问题