Example #1
0
        public void Execute()
        {
            _userOutput.WriteMessage("Choose one of note types:");

            foreach (var pair in _creators)
            {
                ThrowIf.Variable.IsNull(pair.Value, nameof(pair.Value));
                _userOutput.WriteMessage($"{pair.Value.NoteType} -- {pair.Value.Info}");
            }

            if (!_userInput.TryGetString("\nEnter note type", out var userInput))
            {
                _userOutput.WriteMessage("Incorrect note type!");
            }

            if (!_creators.TryGetValue(userInput.ToLower(), out var creator))
            {
                _userOutput.WriteMessage("Incorrect note type!");
                return;
            }
            if (creator.TryCreateNote(out var note))
            {
                _notebook.Add(note);
            }
        }
Example #2
0
        public bool TryLoadFile(string fileName, INotebook notebook)
        {
            try
            {
                using (var fileStream = new FileStream(fileName, FileMode.Open))
                {
                    notebook.RemoveAllNotes();
                    var notes = FileUtils.Deserialize <List <INote> >(fileStream);
                    ThrowIf.Variable.IsNull(notes, nameof(notes));
                    //var notes = (List<INote>) formatter.Deserialize(fileStream);
                    foreach (var note in notes)
                    {
                        notebook.Add(note);
                    }
                }

                _userOutput.WriteMessage("Successfully loaded!");
                return(true);
            }
            catch (SerializationException ex)
            {
                _userOutput.WriteMessage("Failed to deserialize. " + ex.Message);
                return(false);
            }
            catch (FileNotFoundException)
            {
                _userOutput.WriteMessage($"File {fileName} doesn't exist");
                return(false);
            }
        }