Sunday, June 20, 2021

Notes Store | HackerRank Certification | C#

using System;
using System.Collections.Generic;
using System.IO;

namespace Solution
{

    public class NotesStore
    {
        public IDictionary<string,string> noteCollection = new Dictionary<string, string>();
        
        public NotesStore() {}
        public void AddNote(String state, String name) {
            if(name == ""){
                throw new Exception("Name cannot be empty");
            }else if(state != "completed" && state != "active" && state != "others")
            {
                throw new Exception($"Invalid state {state}");
            }else
            {
                noteCollection.Add(name, state);
            }
            
        }
        public List<String> GetNotes(String state) {
            List<string> li = new List<string>();
            if(state != "completed" && state != "active" && state != "others")
            {
                throw new Exception($"Invalid state {state}");
            }else
            {
                foreach(KeyValuePair<string, string> kvp in noteCollection)
                {
                    if(state == kvp.Value){
                        li.Add(kvp.Key);
                    }
                }
            }
            return li;
            
        }
    } 

    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);
                }
            }
        }
    }
}

8 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. It's not working getting some errors

    ReplyDelete
  3. using System;
    using System.Collections.Generic;
    using System.IO;

    namespace Solution
    {
    public class NotesStore
    {
    //collections,Dictionary.
    Private Dictionary> notes;
    public NotesStore() {
    this.notes = new Dictionary>();
    }
    public void AddNote(String state, String name) {
    if(String.IsNullOrEmpty(name)){
    throw new Exception("Name cannot be empty");
    }
    if(state == "completed" || state == "active" || state == "others"){
    if(this.notes.ContainsKey(state)){
    this.notes[state].Add(name);
    }
    else{
    this.notes[state] = new List();
    this.notes[state].Add(name);
    else{
    throw new Exception("Invalid state” +state );
    }
    public List GetNotes(String state) {
    if(state == "completed" || state == "active" || state == "others")
    {
    If(this.notes.ContainsKey(state)== false){
    return new List();
    }
    var notes = this.notes[state];
    if(notes == null || notes.Count <= 0) {
    return new List();
    }
    if {
    return this.notes[state];
    else{
    throw new Exception("Invalid state” +state );
    }
    }
    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);
    }
    }
    }
    }
    }

    ReplyDelete
  4. employees management ka bhi plz

    ReplyDelete

horizontal ads