public static void Main() { int commandCount = int.Parse(Console.ReadLine()); List <Pet> pets = new List <Pet>(); List <PetClinic> clinics = new List <PetClinic>(); for (int i = 0; i < commandCount; i++) { string[] commandInput = Console.ReadLine().Split(); string command = commandInput[0]; switch (command) { case "Create": try { string typeOfCreation = commandInput[1]; if (typeOfCreation == "Pet") { string name = commandInput[2]; int age = int.Parse(commandInput[3]); string kind = commandInput[4]; Pet pet = new Pet(name, age, kind); pets.Add(pet); } else { string name = commandInput[2]; int roomCount = int.Parse(commandInput[3]); PetClinic clinic = new PetClinic(name, roomCount); clinics.Add(clinic); } } catch (InvalidOperationException invalidOperationException) { Console.WriteLine(invalidOperationException.Message); } break; case "Add": Pet petToAdd = pets.FirstOrDefault(p => p.Name == commandInput[1]); PetClinic clinicToAdd = clinics.FirstOrDefault(c => c.Name == commandInput[2]); Console.WriteLine(clinicToAdd.Add(petToAdd)); break; case "Release": PetClinic clinicToRelease = clinics.FirstOrDefault(c => c.Name == commandInput[1]); Console.WriteLine(clinicToRelease.Release()); break; case "HasEmptyRooms": PetClinic clinicToCheck = clinics.FirstOrDefault(c => c.Name == commandInput[1]); Console.WriteLine(clinicToCheck.HasEmptyRooms); break; case "Print": PetClinic clinicToPrint = clinics.FirstOrDefault(c => c.Name == commandInput[1]); if (commandInput.Length == 3) { int roomNumber = int.Parse(commandInput[2]); Console.WriteLine(clinicToPrint.Print(roomNumber)); } else { Console.WriteLine(clinicToPrint.PrintAll()); } break; } } }
public void ExecuteCommand(string command, List <PetClinic> clinics, List <Pet> pets) { string[] commandData = command.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string commandType = commandData[0]; string petName = string.Empty; string clinicName = string.Empty; PetClinic currentClinic = null; try { switch (commandType) { case "Create": if (commandData[1] == "Clinic") { clinicName = commandData[2]; int numberOfRooms = int.Parse(commandData[3]); clinics.Add(new PetClinic(clinicName, numberOfRooms)); } else if (commandData[1] == "Pet") { petName = commandData[2]; int age = int.Parse(commandData[3]); string kind = commandData[4]; pets.Add(new Pet(petName, age, kind)); } break; case "Add": petName = commandData[1]; clinicName = commandData[2]; currentClinic = clinics.FirstOrDefault(c => c.Name == clinicName); var currentPet = pets.FirstOrDefault(p => p.Name == petName); Console.WriteLine(currentClinic.AddPet(currentPet)); break; case "Release": clinicName = commandData[1]; currentClinic = clinics.FirstOrDefault(c => c.Name == clinicName); Console.WriteLine(currentClinic.Release()); break; case "Print": clinicName = commandData[1]; currentClinic = clinics.FirstOrDefault(c => c.Name == clinicName); if (commandData.Length < 3) { Console.Write(currentClinic.PrintAll()); } else { int roomIndex = int.Parse(commandData[2]); Console.WriteLine(currentClinic.PrintRoom(roomIndex)); } break; case "HasEmptyRooms": clinicName = commandData[1]; currentClinic = clinics.FirstOrDefault(c => c.Name == clinicName); Console.WriteLine(currentClinic.HasEmptyRooms); break; } } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } catch (Exception) { Console.WriteLine("Invalid Operation!"); } }