Categories
C#: Notes Store
Date
Author
In this challenge, the task is to implement a class called NotesStore. The class will manage a collection of notes, with each note having a state and a name. Valid states for notes are ‘completed’, ‘active’, and ‘others’. All other states are invalid.
The class must have the following methods:
- AddNote(state, name): adds a note with the given name (string) and state (string) to the collection. In addition to that:
- If the passed name is empty, then it throws an Exception with the message ‘Name cannot be empty’.
- If the passed name is non-empty but the given state is not a valid state for a note, then it throws an Exception with the message ‘Invalid state {state}’.
- GetNotes(state): returns a list of note names with the given state (string) added so far. The names are returned in the order the corresponding notes were added. In addition to that:
- If the given state is not a valid note state, then it throws an Exception with the message ‘Invalid state {state}’.
- If no note is found in this state, it returns an empty list.
Input Format For Custom Testing
In the first line, there is an integer, n, denoting the number of operations to be performed.
Each line i of the n subsequent lines (where 0 ≤ i < n) contains space-separated strings such that the first of them is a function name, and the remaining ones, if any, are parameters for that function.
Sample Case 0
Sample Input For Custom Testing
6 AddNote active DrinkTea AddNote active DrinkCoffee AddNote completed Study GetNotes active GetNotes completed GetNotes foo
Sample Output
DrinkTea,DrinkCoffee Study Error: Invalid state foo
Explanation
For all 3 AddNote operations, the AddNote function is called with a state and a name. Then, the GetNotes function is called for ‘active’ state and ‘completed’ state respectively, and the result is printed. Then, the GetNotes function is called for ‘foo’ state, which throws an error since this state is invalid, and the error is printed.Sample Case 1
Sample Input For Custom Testing
3 AddNote active AddNote foo Study GetNotes completed
Sample Output
Error: Name cannot be empty Error: Invalid state foo No Notes
Explanation
The AddNote function is called with ‘active’ state and an empty name, which throws an error since the name is empty. Then, AddNote is called with ‘foo’ state, which throws an error since the state is invalid. Finally, the GetNotes function is called for ‘completed’ state, an empty list is returned since no note exists in this state, and ‘No Notes’ is printed by the stubbed code.SOLUTION:
using System;
using System.Collections.Generic;
namespace Solution
{
public class NotesStore
{
private List notes = new List();
public void AddNote(string state, string name)
{
if (string.IsNullOrEmpty(name))
{
throw new Exception("Name cannot be empty");
}
if (!IsValidState(state))
{
throw new Exception($"Invalid state {state}");
}
notes.Add($"{state}:{name}");
}
public List GetNotes(string state)
{
if (!IsValidState(state))
{
throw new Exception($"Invalid state {state}");
}
List result = new List();
foreach (string note in notes)
{
string[] parts = note.Split(':');
if (parts[0] == state)
{
result.Add(parts[1]);
}
}
return result;
}
private bool IsValidState(string state)
{
return state == "completed" || state == "active" || state == "others";
}
}
public class Solution
{
public static void Main()
{
var notesStoreObj = new NotesStore();
var n = int.Parse(Console.ReadLine());
for (var i = 0; i < n; i++)
{
var operationInfo = Console.ReadLine().Split(' ');
try
{
if (operationInfo[0] == "AddNote")
{
notesStoreObj.AddNote(operationInfo[1], operationInfo.Length == 2 ? "" : operationInfo[2]);
}
else if (operationInfo[0] == "GetNotes")
{
var result = notesStoreObj.GetNotes(operationInfo[1]);
if (result.Count == 0)
{
Console.WriteLine("No Notes");
}
else
{
Console.WriteLine(string.Join(",", result));
}
}
else
{
Console.WriteLine("Invalid Parameter");
}
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message);
}
}
}
}
}