|
| 1 | +using System; |
| 2 | + |
| 3 | +// ourAnimals array will store the following: |
| 4 | +string animalSpecies = ""; |
| 5 | +string animalID = ""; |
| 6 | +string animalAge = ""; |
| 7 | +string animalPhysicalDescription = ""; |
| 8 | +string animalPersonalityDescription = ""; |
| 9 | +string animalNickname = ""; |
| 10 | +string suggestedDonation = ""; |
| 11 | + |
| 12 | +// variables that support data entry |
| 13 | +int maxPets = 8; |
| 14 | +string? readResult; |
| 15 | +string menuSelection = ""; |
| 16 | +decimal decimalDonation = 0.00m; |
| 17 | + |
| 18 | +// array used to store runtime data |
| 19 | +string[,] ourAnimals = new string[maxPets, 7]; |
| 20 | + |
| 21 | +// sample data ourAnimals array entries |
| 22 | +for (int i = 0; i < maxPets; i++) |
| 23 | +{ |
| 24 | + switch (i) |
| 25 | + { |
| 26 | + case 0: |
| 27 | + animalSpecies = "dog"; |
| 28 | + animalID = "d1"; |
| 29 | + animalAge = "2"; |
| 30 | + animalPhysicalDescription = "medium sized cream colored female golden retriever weighing about 45 pounds. housebroken."; |
| 31 | + animalPersonalityDescription = "loves to have her belly rubbed and likes to chase her tail. gives lots of kisses."; |
| 32 | + animalNickname = "lola"; |
| 33 | + suggestedDonation = "85.00"; |
| 34 | + break; |
| 35 | + |
| 36 | + case 1: |
| 37 | + animalSpecies = "dog"; |
| 38 | + animalID = "d2"; |
| 39 | + animalAge = "9"; |
| 40 | + animalPhysicalDescription = "large reddish-brown male golden retriever weighing about 85 pounds. housebroken."; |
| 41 | + animalPersonalityDescription = "loves to have his ears rubbed when he greets you at the door, or at any time! loves to lean-in and give doggy hugs."; |
| 42 | + animalNickname = "gus"; |
| 43 | + suggestedDonation = "49.99"; |
| 44 | + break; |
| 45 | + |
| 46 | + case 2: |
| 47 | + animalSpecies = "cat"; |
| 48 | + animalID = "c3"; |
| 49 | + animalAge = "1"; |
| 50 | + animalPhysicalDescription = "small white female weighing about 8 pounds. litter box trained."; |
| 51 | + animalPersonalityDescription = "friendly"; |
| 52 | + animalNickname = "snow"; |
| 53 | + suggestedDonation = "40.00"; |
| 54 | + break; |
| 55 | + |
| 56 | + case 3: |
| 57 | + animalSpecies = "cat"; |
| 58 | + animalID = "c4"; |
| 59 | + animalAge = ""; |
| 60 | + animalPhysicalDescription = ""; |
| 61 | + animalPersonalityDescription = ""; |
| 62 | + animalNickname = "lion"; |
| 63 | + suggestedDonation = ""; |
| 64 | + |
| 65 | + break; |
| 66 | + |
| 67 | + default: |
| 68 | + animalSpecies = ""; |
| 69 | + animalID = ""; |
| 70 | + animalAge = ""; |
| 71 | + animalPhysicalDescription = ""; |
| 72 | + animalPersonalityDescription = ""; |
| 73 | + animalNickname = ""; |
| 74 | + suggestedDonation = ""; |
| 75 | + break; |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + ourAnimals[i, 0] = "ID #: " + animalID; |
| 80 | + ourAnimals[i, 1] = "Species: " + animalSpecies; |
| 81 | + ourAnimals[i, 2] = "Age: " + animalAge; |
| 82 | + ourAnimals[i, 3] = "Nickname: " + animalNickname; |
| 83 | + ourAnimals[i, 4] = "Physical description: " + animalPhysicalDescription; |
| 84 | + ourAnimals[i, 5] = "Personality: " + animalPersonalityDescription; |
| 85 | + |
| 86 | + if (!decimal.TryParse(suggestedDonation, out decimalDonation)){ |
| 87 | + decimalDonation = 45.00m; // if suggestedDonation NOT a number, default to 45.00 |
| 88 | + } |
| 89 | + ourAnimals[i, 6] = $"Suggested Donation: {decimalDonation:C2}"; |
| 90 | +} |
| 91 | + |
| 92 | +// top-level menu options |
| 93 | +do |
| 94 | +{ |
| 95 | + // NOTE: the Console.Clear method is throwing an exception in debug sessions |
| 96 | + Console.Clear(); |
| 97 | + |
| 98 | + Console.WriteLine("Welcome to the Contoso PetFriends app. Your main menu options are:"); |
| 99 | + Console.WriteLine(" 1. List all of our current pet information"); |
| 100 | + Console.WriteLine(" 2. Display all dogs with a specified characteristic"); |
| 101 | + Console.WriteLine(); |
| 102 | + Console.WriteLine("Enter your selection number (or type Exit to exit the program)"); |
| 103 | + |
| 104 | + readResult = Console.ReadLine(); |
| 105 | + if (readResult != null) |
| 106 | + { |
| 107 | + menuSelection = readResult.ToLower(); |
| 108 | + } |
| 109 | + |
| 110 | + // switch-case to process the selected menu option |
| 111 | + switch (menuSelection) |
| 112 | + { |
| 113 | + case "1": |
| 114 | + // list all pet info |
| 115 | + for (int i = 0; i < maxPets; i++) |
| 116 | + { |
| 117 | + if (ourAnimals[i, 0] != "ID #: ") |
| 118 | + { |
| 119 | + Console.WriteLine(); |
| 120 | + for (int j = 0; j < 7; j++) |
| 121 | + { |
| 122 | + Console.WriteLine(ourAnimals[i, j].ToString()); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + Console.WriteLine("\r\nPress the Enter key to continue"); |
| 128 | + readResult = Console.ReadLine(); |
| 129 | + |
| 130 | + break; |
| 131 | + |
| 132 | + case "2": |
| 133 | + // #1 Display all dogs with a multiple search characteristics |
| 134 | + |
| 135 | + string dogCharacteristic = ""; |
| 136 | + |
| 137 | + while (dogCharacteristic == "") |
| 138 | + { |
| 139 | + // #2 have user enter multiple comma separated characteristics to search for |
| 140 | + Console.WriteLine($"\r\nEnter one desired dog characteristic to search for"); |
| 141 | + readResult = Console.ReadLine(); |
| 142 | + if (readResult != null) |
| 143 | + { |
| 144 | + dogCharacteristic = readResult.ToLower().Trim(); |
| 145 | + Console.WriteLine(); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + bool noMatchesDog = true; |
| 150 | + string dogDescription = ""; |
| 151 | + |
| 152 | + // #4 update to "rotating" animation with countdown |
| 153 | + string[] searchingIcons = {". ", ".. ", "..."}; |
| 154 | + |
| 155 | + // loop ourAnimals array to search for matching animals |
| 156 | + for (int i = 0; i < maxPets; i++) |
| 157 | + { |
| 158 | + |
| 159 | + if (ourAnimals[i, 1].Contains("dog")) |
| 160 | + { |
| 161 | + |
| 162 | + // Search combined descriptions and report results |
| 163 | + dogDescription = ourAnimals[i, 4] + "\r\n" + ourAnimals[i, 5]; |
| 164 | + |
| 165 | + for (int j = 5; j > -1 ; j--) |
| 166 | + { |
| 167 | + // #5 update "searching" message to show countdown |
| 168 | + foreach (string icon in searchingIcons) |
| 169 | + { |
| 170 | + Console.Write($"\rsearching our dog {ourAnimals[i, 3]} for {dogCharacteristic} {icon}"); |
| 171 | + Thread.Sleep(250); |
| 172 | + } |
| 173 | + |
| 174 | + Console.Write($"\r{new String(' ', Console.BufferWidth)}"); |
| 175 | + } |
| 176 | + |
| 177 | + // #3a iterate submitted characteristic terms and search description for each term |
| 178 | + |
| 179 | + if (dogDescription.Contains(dogCharacteristic)) |
| 180 | + { |
| 181 | + // #3b update message to reflect term |
| 182 | + // #3c set a flag "this dog" is a match |
| 183 | + Console.WriteLine($"\nOur dog {ourAnimals[i, 3]} is a match!"); |
| 184 | + |
| 185 | + noMatchesDog = false; |
| 186 | + } |
| 187 | + |
| 188 | + // #3d if "this dog" is match write match message + dog description |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + if (noMatchesDog) |
| 193 | + { |
| 194 | + Console.WriteLine("None of our dogs are a match found for: " + dogCharacteristic); |
| 195 | + } |
| 196 | + |
| 197 | + Console.WriteLine("\n\rPress the Enter key to continue"); |
| 198 | + readResult = Console.ReadLine(); |
| 199 | + |
| 200 | + break; |
| 201 | + |
| 202 | + default: |
| 203 | + break; |
| 204 | + } |
| 205 | + |
| 206 | +} while (menuSelection != "exit"); |
0 commit comments