From 34afc691725ee0d1976c9382a74da4eee3fce835 Mon Sep 17 00:00:00 2001 From: ipvg Date: Fri, 26 Jul 2024 22:56:50 -0400 Subject: [PATCH] end 019 --- .../ChallengeProject/Own/Program.cs | 41 +++++++++++++------ 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/019_branching_and_looping_structures/ChallengeProject/Own/Program.cs b/019_branching_and_looping_structures/ChallengeProject/Own/Program.cs index 9aa4447..1eea9e8 100644 --- a/019_branching_and_looping_structures/ChallengeProject/Own/Program.cs +++ b/019_branching_and_looping_structures/ChallengeProject/Own/Program.cs @@ -136,20 +136,33 @@ int availability(string[,] animals) { return slots; } -string get_input(string text = "Please enter your text: ", bool integer = false) { +string get_input(string text = "Please enter your text: ", + bool integer = false, + string[]? opts = null) { bool invalid = true; while (invalid) { print(text, false); string? usr_in = Console.ReadLine(); if (!string.IsNullOrEmpty(usr_in) && usr_in.Trim() != "") { + string resp = usr_in.Trim(); if (integer) { - string resp = usr_in.Trim(); int temp_int; if (int.TryParse(resp, out temp_int)) { return resp; } + } else if (opts != null) { + resp = resp.ToLower(); + if (opts.Contains(resp)) { + return resp; + } else { + print($"Please enter a valid option (", false); + foreach (string opt in opts) { + print($" {opt} ", false); + } + print(")"); + } } else { - return usr_in.Trim(); + return resp; } } } @@ -159,7 +172,7 @@ string get_input(string text = "Please enter your text: ", bool integer = false) void add_new_pet(string[,] animals, int slots) { int at_indx = max_pets - slots; string id = $"{rand_str(abcd)}{rand_int(nums)}"; - string specie = get_input("Enter pet specie: "); + string specie = get_input("Enter pet specie: ", false, species); string name = get_input("Enter the pet name (? if unknown): "); int age = Int32.Parse(get_input("Enter pet age (-1 if unknown): ", true)); string desc = get_input("Enter the physical description (? if unknown): "); @@ -182,7 +195,9 @@ void ask_new_pet() { if (slots > 0) { bool another = false; do { - string resp = get_input("Do you want to enter info for another pet?: "); + string resp = get_input( + "Do you want to enter info for another pet?: " + ); bool invalid = true; if (resp != "") { while (invalid) { @@ -218,7 +233,7 @@ void check_age_and_desc(string[,] animals) { int pet_count = max_pets - (availability(animals)); for (int j = 0; j < pet_count; j++) { if (animals[j, 3] == "-1") { - print("\n"+separator); + print("\n" + separator); string[] animal = { animals[j,0], animals[j,1], animals[j,2], animals[j,3], animals[j,4], animals[j,5] @@ -226,21 +241,21 @@ void check_age_and_desc(string[,] animals) { print_pet(animal); int age = Int32.Parse( get_input( - $"Enter an age for ID: {animals[j,1]}" + + $"Enter an age for ID: {animals[j, 1]}" + " (-1 if unknown): ", true ) ); animals[j, 3] = age.ToString(); } if (animals[j, 2] == "?") { - print("\n"+separator); + print("\n" + separator); string[] animal = { animals[j,0], animals[j,1], animals[j,2], animals[j,3], animals[j,4], animals[j,5] }; print_pet(animal); string desc = get_input( - $"Enter a physical description for ID: {animals[j,1]}" + + $"Enter a physical description for ID: {animals[j, 1]}" + " (? if unknown): " ); animals[j, 2] = desc; @@ -256,27 +271,27 @@ void check_nick_and_perso(string[,] animals) { int pet_count = max_pets - (availability(animals)); for (int j = 0; j < pet_count; j++) { if (animals[j, 5] == "?") { - print("\n"+separator); + print("\n" + separator); string[] animal = { animals[j,0], animals[j,1], animals[j,2], animals[j,3], animals[j,4], animals[j,5] }; print_pet(animal); string nick = get_input( - $"Enter a nickname for ID: {animals[j,1]}" + + $"Enter a nickname for ID: {animals[j, 1]}" + "(? if unknown): " ); animals[j, 5] = nick; } if (animals[j, 4] == "?") { - print("\n"+separator); + print("\n" + separator); string[] animal = { animals[j,0], animals[j,1], animals[j,2], animals[j,3], animals[j,4], animals[j,5] }; print_pet(animal); string perso = get_input( - $"Enter a personality description for ID: {animals[j,1]}" + + $"Enter a personality description for ID: {animals[j, 1]}" + "(? if unknown): " ); animals[j, 4] = perso;