end 019 - pend: validate specie

This commit is contained in:
ipvg 2024-07-26 21:17:57 -04:00
parent 0a1b0dc467
commit 3c751c6a2f
3 changed files with 108 additions and 37 deletions

View File

@ -9,9 +9,9 @@ iteration statements to achieve app specifications.
In this module, you'll demonstrate your ability to: In this module, you'll demonstrate your ability to:
- Use Visual Studio Code to develop a C# console application that uses a - Develop a C# console application that uses a combination of selection and
combination of selection and iteration statements to implement logical iteration statements to implement logical workflows in accordance with supplied
workflows in accordance with supplied application data and user interactions. application data and user interactions.
- Evaluate the underlying conditions and make an informed decision when - Evaluate the underlying conditions and make an informed decision when
choosing between if-elseif-else and switch statements, and between foreach, choosing between if-elseif-else and switch statements, and between foreach,
for, while, and do iteration statements. for, while, and do iteration statements.

View File

@ -1,9 +1,11 @@
int max_pets = 8; int max_pets = 8;
bool exit = false; bool exit = false;
string? selection; string? selection;
string separator = "+-------------------------------------" + const string WIP = "Under Construction - please check " +
"back next month to see progress.";
const string separator = "+-------------------------------------" +
"--------------------------------------+"; "--------------------------------------+";
string main_menu = @" const string main_menu = @"
1. List all of our current pet information. 1. List all of our current pet information.
2. Assign values to the ourAnimals array fields. 2. Assign values to the ourAnimals array fields.
3. Ensure animal ages and physical descriptions are complete. 3. Ensure animal ages and physical descriptions are complete.
@ -16,9 +18,9 @@ string main_menu = @"
Enter menu item selection or type 'Exit' to exit the program Enter menu item selection or type 'Exit' to exit the program
"; ";
string[] description = { string[] description = {
"big sized female golden colored weighing about 20 pounds. housebroken.", "big sized female golden colored weighing 20 pounds. housebroken.",
"large dark-brown male siver back weighing about 15 pounds. housebroken.", "large dark-brown male siver back weighing 15 pounds. housebroken.",
"small white female weighing about 8 pounds. translucid at sun.", "small white female weighing about 8 pounds. translucid.",
"medium size male. fluorescent at night.", "medium size male. fluorescent at night.",
}; };
string[] personality = { string[] personality = {
@ -29,7 +31,7 @@ string[] personality = {
}; };
string[] options = { "1", "2", "3", "4", "5", "6", "7", "8", "exit" }; string[] options = { "1", "2", "3", "4", "5", "6", "7", "8", "exit" };
string[] nickname = { "lola", "loki", "fuzz", "boby", "gogo", "besti" }; string[] nickname = { "lola", "loki", "fuzz", "boby", "gogo", "besti" };
string[] species = { "cat", "dog", "bee", "pig" }; string[] species = { "cat", "dog", "bee", "pig", "fly", "rat", "bat" };
string[] abcd = { "a", "b", "c", "d" }; string[] abcd = { "a", "b", "c", "d" };
int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8 }; int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8 };
string[,] our_animals = new string[max_pets, 6]; string[,] our_animals = new string[max_pets, 6];
@ -55,25 +57,32 @@ int rand_int(int[] choices) {
return choices[indx]; return choices[indx];
} }
void populate_animals_array() { string get_uniq_id(string[,] animals) {
for (int i = 0; i < max_pets - 4; i++) { for (int i = 0; i < max_pets - 4; i++) {
bool uniq_id = false; bool uniq_id = false;
our_animals[i, 0] = rand_str(species);
while (!uniq_id) { while (!uniq_id) {
bool check = true; bool check = true;
string id = $"{rand_str(abcd)}{rand_int(nums)}"; string id = $"{rand_str(abcd)}{rand_int(nums)}";
for (int j = 0; j < max_pets - 4; j++) { for (int j = 0; j < max_pets - 4; j++) {
if (!string.IsNullOrEmpty(our_animals[j, 1]) && our_animals[j, 1] == id) { if (!string.IsNullOrEmpty(animals[j, 1]) && animals[j, 1] == id) {
uniq_id = false; uniq_id = false;
check = false; check = false;
break; break;
} }
} }
if (check) { if (check) {
our_animals[i, 1] = id;
uniq_id = true; uniq_id = true;
return id;
} }
} }
}
return "?";
}
void populate_animals_array() {
for (int i = 0; i < max_pets - 4; i++) {
our_animals[i, 0] = rand_str(species);
our_animals[i, 1] = get_uniq_id(our_animals);
our_animals[i, 2] = rand_str(description); our_animals[i, 2] = rand_str(description);
our_animals[i, 3] = $"{rand_int(nums)}"; our_animals[i, 3] = $"{rand_int(nums)}";
our_animals[i, 4] = rand_str(personality); our_animals[i, 4] = rand_str(personality);
@ -81,8 +90,8 @@ void populate_animals_array() {
} }
} }
void press_enter() { void press_enter(string msg = "\n\tPress 'Enter' to continue") {
print("\n\tPress 'Enter' to continue", false); print(msg, false);
Console.ReadLine(); Console.ReadLine();
} }
@ -96,6 +105,7 @@ void print_pets(string[,] animals) {
} }
print_pet(animal); print_pet(animal);
} }
press_enter();
} }
void print_pet(string[] pet) { void print_pet(string[] pet) {
@ -136,7 +146,6 @@ string get_input(string text = "Please enter your text: ", bool integer = false)
string resp = usr_in.Trim(); string resp = usr_in.Trim();
int temp_int; int temp_int;
if (int.TryParse(resp, out temp_int)) { if (int.TryParse(resp, out temp_int)) {
print($"Opción 1: {temp_int}");
return resp; return resp;
} }
} else { } else {
@ -144,7 +153,7 @@ string get_input(string text = "Please enter your text: ", bool integer = false)
} }
} }
} }
return ""; return "?";
} }
void add_new_pet(string[,] animals, int slots) { void add_new_pet(string[,] animals, int slots) {
@ -155,8 +164,6 @@ void add_new_pet(string[,] animals, int slots) {
int age = Int32.Parse(get_input("Enter pet age (-1 if unknown): ", true)); int age = Int32.Parse(get_input("Enter pet age (-1 if unknown): ", true));
string desc = get_input("Enter the physical description (? if unknown): "); string desc = get_input("Enter the physical description (? if unknown): ");
string perso = get_input("Enter pet personality (? if unknown): "); string perso = get_input("Enter pet personality (? if unknown): ");
//print($"Will be added at index: {at_indx}");
//print($"{id}\n{specie}\n{name}\n{age}\n{desc}\n{perso}");
animals[at_indx, 0] = specie; animals[at_indx, 0] = specie;
animals[at_indx, 1] = id; animals[at_indx, 1] = id;
animals[at_indx, 2] = desc; animals[at_indx, 2] = desc;
@ -200,19 +207,87 @@ void ask_new_pet() {
} while (another && slots > 0); } while (another && slots > 0);
if (slots == 0) { if (slots == 0) {
print("We have reached our limit on the number of pets that we can manage."); print("We have reached our limit on the number of pets that we can manage.");
press_enter();
} }
press_enter();
} else {
press_enter();
} }
press_enter();
} }
void check_age_and_desc() { void check_age_and_desc(string[,] animals) {
int pet_count = max_pets - (availability(our_animals)); clear();
print("\nEnsure animal ages and physical descriptions are complete");
int pet_count = max_pets - (availability(animals));
for (int j = 0; j < pet_count; j++) {
if (animals[j, 3] == "-1") {
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);
int age = Int32.Parse(
get_input(
$"Enter an age for ID: {animals[j,1]}" +
" (-1 if unknown): ", true
)
);
animals[j, 3] = age.ToString();
}
if (animals[j, 2] == "?") {
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]}" +
" (? if unknown): "
);
animals[j, 2] = desc;
}
}
press_enter();
} }
void check_nick_and_perso(string[,] animals) {
clear();
print("Ensure animal nicknames and personality descriptions are complete");
print(separator);
int pet_count = max_pets - (availability(animals));
for (int j = 0; j < pet_count; j++) {
if (animals[j, 5] == "?") {
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]}" +
"(? if unknown): "
);
animals[j, 5] = nick;
}
if (animals[j, 4] == "?") {
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]}" +
"(? if unknown): "
);
animals[j, 4] = perso;
}
}
press_enter();
}
populate_animals_array(); populate_animals_array();
while (!exit) { while (!exit) {
clear(); clear();
print(main_menu); print(main_menu);
@ -222,40 +297,34 @@ while (!exit) {
switch (selection) { switch (selection) {
case "1": case "1":
print_pets(our_animals); print_pets(our_animals);
press_enter();
break; break;
case "2": case "2":
ask_new_pet(); ask_new_pet();
break; break;
case "3": case "3":
clear(); check_age_and_desc(our_animals);
print("Ensure animal ages and physical descriptions are complete");
check_age_and_desc();
press_enter();
break; break;
case "4": case "4":
clear(); check_nick_and_perso(our_animals);
print("Ensure animal nicknames and personality descriptions are complete");
press_enter();
break; break;
case "5": case "5":
clear(); clear();
print("Edit an animal's age"); print(WIP);
press_enter(); press_enter();
break; break;
case "6": case "6":
clear(); clear();
print("Edit an animal's personality description"); print(WIP);
press_enter(); press_enter();
break; break;
case "7": case "7":
clear(); clear();
print("Display all cats with a specified characteristic"); print(WIP);
press_enter(); press_enter();
break; break;
case "8": case "8":
clear(); clear();
print("Display all dogs with a specified characteristic"); print(WIP);
press_enter(); press_enter();
break; break;
case "exit": case "exit":

View File

@ -22,3 +22,5 @@ Following
- [switch case construct](./015_switch_case/015_csharp.md) - [switch case construct](./015_switch_case/015_csharp.md)
- [FizzBuzz Challenge](./016_Iterate_using_for_statement/016_csharp.md) - [FizzBuzz Challenge](./016_Iterate_using_for_statement/016_csharp.md)
- [Looping logic using the do-while and while](./017_Looping_logic_using_do-while_and_while/017_csharp.md) - [Looping logic using the do-while and while](./017_Looping_logic_using_do-while_and_while/017_csharp.md)
- [Conditional branching and looping](./018_Conditional_branching_and_looping/018_csharp.md)
- [Branching and looping structures](./019_branching_and_looping_structures/019_csharp.md)