From 70e948569dff6d4afbc16833097624b396ff58a4 Mon Sep 17 00:00:00 2001 From: Tony Bark Date: Fri, 7 Feb 2025 07:31:37 -0500 Subject: [PATCH] Merged Launch/RestartSession into SessionManager - Renamed isRestartSession to isFinishedSession --- Program.cs | 102 ++++++++++++++++------------------------------------- 1 file changed, 31 insertions(+), 71 deletions(-) diff --git a/Program.cs b/Program.cs index f0efe4f..00e8c84 100644 --- a/Program.cs +++ b/Program.cs @@ -1,10 +1,9 @@ using SimsPersonalityGenerator; -var isRestartSession = false; +var isFinishedSession = false; void GenerateTraits() { - Console.WriteLine("Welcome to the Sim Personality Generator!"); Console.WriteLine("You can prioritize one or more traits to be above 8 by entering them."); Console.Write("Enter traits separated by commas (e.g., Outgoing, Nice), or press Enter to generate a random personality: "); var prioritizedTraitsInput = Console.ReadLine()?.Trim(); @@ -32,87 +31,48 @@ void GenerateTraits() } Sim.DisplayPersonality(traits); - RestartSession(); + isFinishedSession = true; + SessionManager(); } -// TODO: Refactor to fix code duplication for both Session methods -// Method to restart the session based on user input -void RestartSession() +string? GetUserInput(string message) { - // Prompt the user to decide if they want to try again - Console.WriteLine("Would you like to try again?"); - + // Prompt the user to generate a random personality + Console.WriteLine(message); // Read the user input var input = Console.ReadLine(); - - // Check if the input is null or whitespace - if (string.IsNullOrWhiteSpace(input)) - { - // If the input is invalid or empty, exit the application - Environment.Exit(Environment.ExitCode); - } - else - { - // Convert the input to lowercase to handle case insensitivity - switch (input.ToLower()) - { - case "y": // If the user enters 'y' - // Set the flag to restart the session - isRestartSession = true; - // Clear the console for a fresh start - Console.Clear(); - // Call the method to launch a new session - LaunchSession(); - break; - default: // If the user enters anything other than 'y' - // Exit the application with the current exit code - Environment.Exit(Environment.ExitCode); - break; - } - } + + return input; } // Method to launch a new session -void LaunchSession() +void SessionManager() { - // Check if the session should be restarted - if (isRestartSession) - { - // Reset the restart flag after the session is launched - isRestartSession = false; - // Generate traits (presumably some form of random data) - GenerateTraits(); - } + if (!isFinishedSession) + Console.WriteLine("Welcome to the Sim Personality Generator!"); - // Prompt the user to generate a random personality - Console.WriteLine("Would you like to generate a random personality? [y/n]"); + var message = "Would you like to generate a random personality? [y/n]"; + + if (isFinishedSession) + message = "Would you like to generate a new personality? [y/n]"; - // Read the user input - var input = Console.ReadLine(); - - // Check if the input is null or whitespace - if (string.IsNullOrWhiteSpace(input)) + var input = GetUserInput(message); + + // Convert the input to lowercase to handle case insensitivity + switch (input?.ToLower()) { - // If the input is invalid or empty, exit the application - Environment.Exit(Environment.ExitCode); - } - else - { - // Convert the input to lowercase to handle case insensitivity - switch (input.ToLower()) - { - case "y": // If the user enters 'y' - // Clear the console for a fresh start - Console.Clear(); - // Generate traits (presumably some form of random data) - GenerateTraits(); - break; - default: // If the user enters anything other than 'y' - // Exit the application with the current exit code - Environment.Exit(Environment.ExitCode); - break; - } + case "y": // If the user enters 'y' + if (isFinishedSession) isFinishedSession = false; + // Clear the console for a fresh start + Console.Clear(); + // Generate traits (presumably some form of random data) + GenerateTraits(); + break; + default: // If the user enters anything other than 'y' + // Exit the application with the current exit code + Environment.Exit(Environment.ExitCode); + break; } } -LaunchSession(); \ No newline at end of file +SessionManager(); \ No newline at end of file