static void AddToDatabase <T>(QuizElement quizElement, string path) { string jsonString = File.ReadAllText(path); List <T> quizElementList = JsonConvert.DeserializeObject <List <T> >(jsonString); quizElementList.Add((T)(object)quizElement); jsonString = JsonConvert.SerializeObject(quizElementList, Formatting.Indented); File.WriteAllText(path, jsonString); }
public static QuizElementMultAnswers CreateQuizElement() { try { string question = QuizElement.promptEnterQuestion(); var answerList = new List <Answer> { }; var cont = true; while (cont) { Console.WriteLine("Please enter a possible answer, alternatively simply press enter to go to the next step."); Console.Write("> "); var input = Console.ReadLine(); if (input.Equals("")) { cont = false; } else { Console.WriteLine("Is this answer true? (Type y/n for true/false)"); Console.Write("> "); var input2 = Console.ReadLine().ToLower(); if (input2.Equals("y")) { answerList.Add(new Answer(input, true)); } else if (input2.Equals("n")) { answerList.Add(new Answer(input, false)); } else { throw new System.FormatException(); } } } return(new QuizElementMultAnswers(question, answerList)); } catch (System.FormatException) { Console.WriteLine("Invalid input! Please try again."); return(QuizElementMultAnswers.CreateQuizElement()); } }
public static QuizElementSingleAnswer CreateQuizElement() { try { string question = QuizElement.promptEnterQuestion(); var answerList = new List <Answer> { }; var cont = true; while (cont) { Console.WriteLine("Please enter a possible answer, alternatively simply press enter to go to the next step."); Console.Write("> "); var input = Console.ReadLine(); if (input.Equals("")) { cont = false; } else { answerList.Add(new Answer(input, false)); } } Console.WriteLine("Your answers are: "); for (int i = 0; i < answerList.Count; i++) { Console.WriteLine(i + 1 + ". " + answerList[i].text); } Console.WriteLine("Which answer is correct?"); Console.Write("> "); var correctAnwer = Int32.Parse(Console.ReadLine()); answerList[correctAnwer].isTrue = true; return(new QuizElementSingleAnswer(question, answerList)); } catch (System.FormatException) { Console.WriteLine("Invalid input! Please try again."); return(QuizElementSingleAnswer.CreateQuizElement()); } }
static void CreateNewQuizElement() { while (true) { DisplayQuestionTypes(); try { var input = Int32.Parse(Console.ReadLine()); QuizElement question = null; string basePath = "QuizElements\\"; switch (input) { case 1: question = QuizElementTrueFalse.CreateQuizElement(); AddToDatabase <QuizElementTrueFalse>(question, basePath + "TrueFalse.json"); break; case 2: question = QuizElementText.CreateQuizElement(); AddToDatabase <QuizElementText>(question, basePath + "Text.json"); break; case 3: question = QuizElementGuess.CreateQuizElement(); AddToDatabase <QuizElementGuess>(question, basePath + "Guess.json"); break; case 4: question = QuizElementMultAnswers.CreateQuizElement(); AddToDatabase <QuizElementMultAnswers>(question, basePath + "MultAnswers.json"); break; case 5: question = QuizElementSingleAnswer.CreateQuizElement(); AddToDatabase <QuizElementSingleAnswer>(question, basePath + "SingleAnswer.json"); break; default: break; } Console.Write("Saving question....."); if (question != null) { _quizElements.Add(question); Console.WriteLine("Done!"); } else { Console.WriteLine("Error!"); } break; } catch (System.FormatException) { Console.WriteLine("Input had the wring format, try again!"); } } }