-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path50-HackerRankTest-NotesStore.cs
107 lines (103 loc) · 3.15 KB
/
50-HackerRankTest-NotesStore.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HackerRank
{
public class NotesStore
{
public NotesStore() { }
Dictionary<string, string> noteMap = new Dictionary<string, string>();
public void AddNote(string state, string name)
{
string stateName = getStateName(state);
if (!string.IsNullOrEmpty(name))
{
if (stateName != "invalid")
{
noteMap.Add(name, state);
}
else
{
throw new Exception($"Invalid state {state}");
}
}
else
{
throw new Exception("Name can not be empty");
}
}
public List<string> GetNotes(string state)
{
string stateName = getStateName(state);
if (stateName != "invalid")
{
List<string> lstState = new List<string>();
foreach (KeyValuePair<string, string> kvp in noteMap)
{
if (state == kvp.Value)
lstState.Add(kvp.Key);
}
return lstState;
}
else
{
throw new Exception($"Invalid state {state}");
}
}
public string getStateName(string state)
{
string name = "";
switch (state)
{
case "active":
name = "active";
break;
case "completed":
name = "completed";
break;
case "others":
name = "others";
break;
default:
name = "invalid";
break;
}
return name;
}
}
public class HackerRank_Test2
{
public static void Input()
{
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);
}
}
}
}
}