Merged Launch/RestartSession into SessionManager

- Renamed isRestartSession to isFinishedSession
This commit is contained in:
Tony Bark 2025-02-07 07:31:37 -05:00
parent 5b85ee7a1c
commit 70e948569d

View file

@ -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();
SessionManager();