end 018
This commit is contained in:
parent
f9c691c11b
commit
22b0b9eb6f
@ -1,4 +1,6 @@
|
|||||||
# Guided project - Develop conditional branching and looping structures in C#
|
# Guided project
|
||||||
|
|
||||||
|
## Develop conditional branching and looping structures in C#
|
||||||
|
|
||||||
Gain experience developing a console app that implements selection and
|
Gain experience developing a console app that implements selection and
|
||||||
iteration statements to achieve app specifications.
|
iteration statements to achieve app specifications.
|
||||||
@ -183,3 +185,9 @@ can unzip the files in a sandbox or hosted environment.
|
|||||||
You're now ready to begin the Guided project exercises. Good luck!
|
You're now ready to begin the Guided project exercises. Good luck!
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
Project Files
|
||||||
|
|
||||||
|
- [Starter](./GuidedProject/Starter/Program.cs)
|
||||||
|
- [Final](./GuidedProject/Final/Program.cs)
|
||||||
|
- [Own](./GuidedProject/Own/Program.cs)
|
||||||
|
@ -1,12 +1,25 @@
|
|||||||
int max_pets = 8;
|
int max_pets = 8;
|
||||||
bool exit = false;
|
bool exit = false;
|
||||||
string? selection;
|
string? selection;
|
||||||
string separator = "+------------------------------------------" +
|
string separator = "+-------------------------------------" +
|
||||||
"--------------------------------------------+";
|
"--------------------------------------+";
|
||||||
|
string main_menu = @"
|
||||||
|
1. List all of our current pet information.
|
||||||
|
2. Assign values to the ourAnimals array fields.
|
||||||
|
3. Ensure animal ages and physical descriptions are complete.
|
||||||
|
4. Ensure animal nicknames and personality descriptions are complete.
|
||||||
|
5. Edit an animal's age.
|
||||||
|
6. Edit an animal's personality description.
|
||||||
|
7. Display all cats with a specified characteristic.
|
||||||
|
8. Display all dogs with a specified characteristic.
|
||||||
|
|
||||||
|
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 about 20 pounds. housebroken.",
|
||||||
"large dark-brown male siver back weighing about 15 pounds. housebroken.",
|
"large dark-brown male siver back weighing about 15 pounds. housebroken.",
|
||||||
"small white female weighing about 8 pounds. translucid at sun.",
|
"small white female weighing about 8 pounds. translucid at sun.",
|
||||||
|
"medium size male. fluorescent at night.",
|
||||||
};
|
};
|
||||||
string[] personality = {
|
string[] personality = {
|
||||||
"friendly",
|
"friendly",
|
||||||
@ -14,18 +27,6 @@ string[] personality = {
|
|||||||
"loves to have her belly rubbed. gives lots of kisses.",
|
"loves to have her belly rubbed. gives lots of kisses.",
|
||||||
"sauvage and lovelly.",
|
"sauvage and lovelly.",
|
||||||
};
|
};
|
||||||
string main_menu = @"
|
|
||||||
1. List all of our current pet information.
|
|
||||||
2. Assign values to the ourAnimals array fields.
|
|
||||||
3. Ensure animal ages and physical descriptions are complete.
|
|
||||||
4. Ensure animal nicknames and personality descriptions are complete.
|
|
||||||
5. Edit an animal’s age.
|
|
||||||
6. Edit an animal’s personality description.
|
|
||||||
7. Display all cats with a specified characteristic.
|
|
||||||
8. Display all dogs with a specified characteristic.
|
|
||||||
|
|
||||||
Enter menu item selection or type 'Exit' to exit the program
|
|
||||||
";
|
|
||||||
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" };
|
||||||
@ -86,6 +87,7 @@ void press_enter() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void print_pets(string[,] animals) {
|
void print_pets(string[,] animals) {
|
||||||
|
clear();
|
||||||
print(separator);
|
print(separator);
|
||||||
for (int j = 0; j < animals.GetLength(0); j++) {
|
for (int j = 0; j < animals.GetLength(0); j++) {
|
||||||
string[] animal = new string[6];
|
string[] animal = new string[6];
|
||||||
@ -113,6 +115,90 @@ void print_pet(string[] pet) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int availability(string[,] animals) {
|
||||||
|
int pet_count = 0;
|
||||||
|
for (int j = 0; j < animals.GetLength(0); j++) {
|
||||||
|
if (!string.IsNullOrEmpty(animals[j, 1])) {
|
||||||
|
pet_count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int slots = max_pets - pet_count;
|
||||||
|
return slots;
|
||||||
|
}
|
||||||
|
|
||||||
|
string get_input(string text="Please enter your text: ") {
|
||||||
|
bool invalid = true;
|
||||||
|
while (invalid){
|
||||||
|
print(text, false);
|
||||||
|
string? usr_in = Console.ReadLine();
|
||||||
|
if (!string.IsNullOrEmpty(usr_in) && usr_in.Trim() != "") {
|
||||||
|
return usr_in.Trim();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
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 name = get_input("Enter the pet name: ");
|
||||||
|
int age = Int32.Parse(get_input("Enter pet age: "));
|
||||||
|
string desc = get_input("Enter the physical description: ");
|
||||||
|
string perso = get_input("Enter pet personality: ");
|
||||||
|
//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, 1] = id;
|
||||||
|
animals[at_indx, 2] = desc;
|
||||||
|
animals[at_indx, 3] = age.ToString();
|
||||||
|
animals[at_indx, 4] = perso;
|
||||||
|
animals[at_indx, 5] = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ask_new_pet() {
|
||||||
|
clear();
|
||||||
|
print("Assign values to the ourAnimals array fields");
|
||||||
|
print(separator);
|
||||||
|
int slots = availability(our_animals);
|
||||||
|
print($"\nWe currently have {max_pets-slots} pets that need homes.");
|
||||||
|
print($"We can manage {slots} more.");
|
||||||
|
if (slots > 0) {
|
||||||
|
bool another = false;
|
||||||
|
do {
|
||||||
|
string resp = get_input("Do you want to enter info for another pet?: ");
|
||||||
|
bool invalid = true;
|
||||||
|
if (resp != "") {
|
||||||
|
while(invalid) {
|
||||||
|
switch (resp) {
|
||||||
|
case "y" or "yes":
|
||||||
|
add_new_pet(our_animals, slots);
|
||||||
|
resp = "";
|
||||||
|
invalid = false;
|
||||||
|
another = true;
|
||||||
|
break;
|
||||||
|
case "n" or "no":
|
||||||
|
invalid = false;
|
||||||
|
another = false;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
resp = get_input("Please enter [Y]es or [N]o: ");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
slots = availability(our_animals);
|
||||||
|
} while (another && slots > 0);
|
||||||
|
if (slots == 0) {
|
||||||
|
print("We have reached our limit on the number of pets that we can manage.");
|
||||||
|
press_enter();
|
||||||
|
}
|
||||||
|
press_enter();
|
||||||
|
} else {
|
||||||
|
press_enter();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
populate_animals_array();
|
populate_animals_array();
|
||||||
while (!exit) {
|
while (!exit) {
|
||||||
clear();
|
clear();
|
||||||
@ -122,14 +208,11 @@ while (!exit) {
|
|||||||
selection = selection.ToLower();
|
selection = selection.ToLower();
|
||||||
switch (selection) {
|
switch (selection) {
|
||||||
case "1":
|
case "1":
|
||||||
clear();
|
|
||||||
print_pets(our_animals);
|
print_pets(our_animals);
|
||||||
press_enter();
|
press_enter();
|
||||||
break;
|
break;
|
||||||
case "2":
|
case "2":
|
||||||
clear();
|
ask_new_pet();
|
||||||
print("Assign values to the ourAnimals array fields");
|
|
||||||
press_enter();
|
|
||||||
break;
|
break;
|
||||||
case "3":
|
case "3":
|
||||||
clear();
|
clear();
|
||||||
@ -143,12 +226,12 @@ while (!exit) {
|
|||||||
break;
|
break;
|
||||||
case "5":
|
case "5":
|
||||||
clear();
|
clear();
|
||||||
print("Edit an animal’s age");
|
print("Edit an animal's age");
|
||||||
press_enter();
|
press_enter();
|
||||||
break;
|
break;
|
||||||
case "6":
|
case "6":
|
||||||
clear();
|
clear();
|
||||||
print("Edit an animal’s personality description");
|
print("Edit an animal's personality description");
|
||||||
press_enter();
|
press_enter();
|
||||||
break;
|
break;
|
||||||
case "7":
|
case "7":
|
||||||
@ -162,7 +245,7 @@ while (!exit) {
|
|||||||
press_enter();
|
press_enter();
|
||||||
break;
|
break;
|
||||||
case "exit":
|
case "exit":
|
||||||
print("\n\tFinalizando aplicación\n");
|
print("\n\tTerminating application\n");
|
||||||
exit = !exit;
|
exit = !exit;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
Loading…
Reference in New Issue
Block a user