// Different print formatting for each Auqatic private static void PrintAuqatic(Auqatic auqatic) { Console.WriteLine("Printing Auqatic data: \n" + "Name: " + auqatic.name + "\n" + "Weight: " + auqatic.weight + "\n" + "Height: " + auqatic.height + "\n" + "Color: " + auqatic.color + "\n" + "Domesticated: " + auqatic.domesticated + "\n" + "Strength: " + auqatic.strength + "\n" + "Carnivore: " + auqatic.carnivore + "\n" + "Herbivore: " + auqatic.herbivore + "\n" + "Swimming speed: " + auqatic.swimSpeed + "\n"); }
// Function which Prints from a database file (Json file) private static void CommandPrintDatabase() { while (true) { string compare; if ((compare = Console.ReadLine()).Equals("print")) { Console.WriteLine("Known Command: print"); // Tries to load database file, and if the file exists it will continue if (File.Exists(".\\Animal_Database.json")) { // Write that it loads file Console.WriteLine("Loading File"); // Reads all the text from the file string jsonString = File.ReadAllText(".\\Animal_Database.json"); //Convert text to JsonArray var jArr = JArray.Parse(jsonString); // Loops through Jarray elements for (int i = 0; i < jArr.Count; i++) { // Reads the type of each Json element string strType = (string)jArr[i]["type"]; AnimalType type; // Check if type can be parsed to an enum if (Enum.TryParse(strType, out type)) { // Switch statement for each type, which creates the object and then prints it. switch (type) { case AnimalType.Biped: Biped tempBiped = jArr[i].ToObject <Biped>(); PrintBiped(tempBiped); break; case AnimalType.Quadroped: Quadroped tempQuadroped = jArr[i].ToObject <Quadroped>(); PrintQuadroped(tempQuadroped); break; case AnimalType.Auqatic: Auqatic tempAuqatic = jArr[i].ToObject <Auqatic>(); PrintAuqatic(tempAuqatic); break; case AnimalType.Bird: Bird tempBird = jArr[i].ToObject <Bird>(); PrintBird(tempBird); break; default: break; } } } if (jArr.Count == 0) { Console.WriteLine("Failed to unpack Animals Database"); } } else { Console.WriteLine("No Database exists... Wait for clients to log animals"); } } else { Console.WriteLine("Unknown Command"); } } }
// Handles Json message private static void HandleJsonMessage(string message) { // Checks if the string is a valid Json string. If it is valid, it can be parsed to a Json Object if (IsValidJson(message)) { var jObject = JObject.Parse(message); // Parsing to Json Object string strType = (string)jObject["type"]; AnimalType type; Animal animal = null; // Try to parse the type from string to enum if (Enum.TryParse(strType, out type)) { // Create and print object based on the type switch (type) { case AnimalType.Biped: Biped animalBiped = JsonConvert.DeserializeObject <Biped>(message); PrintBiped(animalBiped); animal = animalBiped; break; case AnimalType.Quadroped: Quadroped animalQuadroped = JsonConvert.DeserializeObject <Quadroped>(message); PrintQuadroped(animalQuadroped); animal = animalQuadroped; break; case AnimalType.Auqatic: Auqatic animalAuqatic = JsonConvert.DeserializeObject <Auqatic>(message); PrintAuqatic(animalAuqatic); animal = animalAuqatic; break; case AnimalType.Bird: Bird animalBird = JsonConvert.DeserializeObject <Bird>(message); PrintBird(animalBird); animal = animalBird; break; default: animal = null; break; } } else { // we done goofd Console.WriteLine("Something went wrong with the new animal :("); } if (animal != null) { if (File.Exists(".\\Animal_Database.json")) { // concatinate json string to existing file string existingDatabase = File.ReadAllText(".\\Animal_Database.json"); // Deserialize json file to list of animals List <Animal> existingAnimals = JsonConvert.DeserializeObject <List <Animal> >(existingDatabase); // Create new animal based on the message json string Animal newAnimal = JsonConvert.DeserializeObject <Animal>(message); // Add animal to existingAnimals list existingAnimals.Add(newAnimal); // Serialize lists to string string animalDatabase = JsonConvert.SerializeObject(existingAnimals); // Write string to file File.WriteAllText(".\\Animal_Database.json", animalDatabase); Console.WriteLine("Saved Animal database"); } else { // Check if its the first animal created. Animal firstAnimal = JsonConvert.DeserializeObject <Animal>(message); List <Animal> firstAnimalList = new List <Animal>(); firstAnimalList.Add(firstAnimal); string firstAnimalDatabase = JsonConvert.SerializeObject(firstAnimalList); File.WriteAllText(".\\Animal_Database.json", firstAnimalDatabase); Console.WriteLine("Saved Animal database"); } } } }