Exemplo n.º 1
0
 public void JSONStorage_New()
 {
     JSONStorage<BaseClass> jsbs = new JSONStorage<BaseClass>();
     Assert.IsTrue(jsbs.GetType().GetMethod("New").ReturnType == typeof(void));
     BaseClass bs = new BaseClass();
     Item it = new Item();
     User us = new User();
     Inventory inv = new Inventory();
     jsbs.New(bs);
     Assert.IsTrue(jsbs.All().ContainsKey($"{bs.GetType().Name}.{bs.id}"));
     Assert.IsTrue(jsbs.All().ContainsValue(bs));
     jsbs.New(it);
     Assert.IsTrue(jsbs.All().ContainsKey($"{it.GetType().Name}.{it.id}"));
     Assert.IsTrue(jsbs.All().ContainsValue(it));
     jsbs.New(us);
     Assert.IsTrue(jsbs.All().ContainsKey($"{us.GetType().Name}.{us.id}"));
     Assert.IsTrue(jsbs.All().ContainsValue(us));
     jsbs.New(inv);
     Assert.IsTrue(jsbs.All().ContainsKey($"{inv.GetType().Name}.{inv.id}"));
     Assert.IsTrue(jsbs.All().ContainsValue(inv));
     jsbs.All().Remove($"{bs.GetType().Name}.{bs.id}");
     jsbs.All().Remove($"{it.GetType().Name}.{it.id}");
     jsbs.All().Remove($"{us.GetType().Name}.{us.id}");
     jsbs.All().Remove($"{inv.GetType().Name}.{inv.id}");
 }
Exemplo n.º 2
0
    private static int Create(string[] input)
    {
        if (input.Length == 1)
        {
            Console.WriteLine("class name missing");
            return(1);
        }
        string className = input[1].ToLower();

        if (!classes.ContainsKey(className))
        {
            Console.WriteLine($"{input[1]} is not a valid object type");
            return(1);
        }
        Type type = Type.GetType($"{classes[className]}, InventoryLibrary");
        // if (type == null)
        //     return (1);
        BaseClass new_obj = (BaseClass)Activator.CreateInstance(type);

        UpdateConsole(ref new_obj);
        Console.WriteLine(new_obj.ToString());
        jsonStorage.New(new_obj);
        jsonStorage.Save();
        return(0);
    }
Exemplo n.º 3
0
 public void JSONStorage_Save()
 {
     JSONStorage<BaseClass> jsbs = new JSONStorage<BaseClass>();
     Assert.IsTrue(jsbs.GetType().GetMethod("Save").ReturnType == typeof(void));
     BaseClass bs = new BaseClass();
     Item it = new Item();
     User us = new User();
     Inventory inv = new Inventory();
     jsbs.New(bs);
     jsbs.New(it);
     jsbs.New(us);
     jsbs.New(inv);
     jsbs.Save();
     jsbs.All().Remove($"{bs.GetType().Name}.{bs.id}");
     jsbs.All().Remove($"{it.GetType().Name}.{it.id}");
     jsbs.All().Remove($"{us.GetType().Name}.{us.id}");
     jsbs.All().Remove($"{inv.GetType().Name}.{inv.id}");
     jsbs.Save();
     FileAssert.Exists("storage/inventory_manager.json");
 }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            JSONStorage storage = new JSONStorage();

            storage.Load();
            string prompt = @"Inventory Manager
-------------------------
<ClassNames> show all ClassNames of objects
<All> show all objects
<All [ClassName]> show all objects of a ClassName
<Create [ClassName]> a new object
<Show [ClassName object_id]> an object
<Update [ClassName object_id]> an object
<Delete [ClassName object_id]> an object
<Exit>
";

            string[] types = { "BaseClass", "User", "Item", "Inventory" };

            Console.Write(prompt);
            for (string r = Console.ReadLine(); r != null; r = Console.ReadLine())
            {
                string[] commands = r.Split(' ');
                if (string.Compare(commands[0], "Exit", true) == 0)
                {
                    break;
                }
                if (commands[0].Length == 0)
                {
                    continue;
                }
                var  a     = storage.All();
                bool cFlag = false;
                bool iFlag = false;
                if (commands.Length >= 2)
                {
                    foreach (var type in types)
                    {
                        if (string.Compare(commands[1], type, true) == 0)
                        {
                            cFlag = true;
                        }
                    }
                    if (cFlag == false)
                    {
                        Console.WriteLine("{0} is not a valid object type", commands[1]);
                        continue;
                    }
                }
                if (commands.Length >= 3)
                {
                    foreach (var o in a.Values)
                    {
                        if (string.Compare(o.id, commands[2], true) == 0)
                        {
                            iFlag = true;
                        }
                    }
                    if (iFlag == false)
                    {
                        Console.WriteLine("Object {0} could not be found", commands[2]);
                        continue;
                    }
                }
                if (string.Compare(commands[0], "All", true) == 0)
                {
                    if (commands.Length == 1)
                    {
                        foreach (var key in a.Keys)
                        {
                            Console.WriteLine("{0} {1}", key, a[key].ToString());
                        }
                    }
                    else
                    {
                        foreach (var key in a.Keys)
                        {
                            if (string.Compare(a[key].type, commands[1], true) == 0)
                            {
                                Console.WriteLine("{0} {1}", key, a[key].ToString());
                            }
                        }
                    }
                }
                else if (string.Compare(commands[0], "Show", true) == 0)
                {
                    string key = string.Format("{0}.{1}", commands[1], commands[2]);
                    if (a.ContainsKey(key))
                    {
                        Console.WriteLine(a[key]);
                    }
                    else
                    {
                        Console.WriteLine("Object {0} could not be found", commands[2]);
                    }
                }
                else if (string.Compare(commands[0], "Update", true) == 0)
                {
                    string key = string.Format("{0}.{1}", commands[1], commands[2]);
                    if (a.ContainsKey(key))
                    {
                        Console.WriteLine("Please enter the desired parameter to update followed by the new value, separated by a space");
                        string   u    = Console.ReadLine();
                        string[] up   = u.Split(' ', 2);
                        string   type = a[key].type;
                        bool     pass = false;
                        if (string.Compare(type, "User") == 0)
                        {
                            pass = storage.updateObj <User>(key, up[0], up[1]);
                        }
                        else if (string.Compare(type, "Item") == 0)
                        {
                            pass = storage.updateObj <Item>(key, up[0], up[1]);
                        }
                        else if (string.Compare(type, "Inventory") == 0)
                        {
                            pass = storage.updateObj <Inventory>(key, up[0], up[1]);
                        }
                        else if (string.Compare(type, "BaseClass") == 0)
                        {
                            pass = storage.updateObj <BaseClass>(key, up[0], up[1]);
                        }
                        if (!pass)
                        {
                            Console.WriteLine("Object has no parameter named {0}", up[0]);
                        }
                        storage.objects[key].date_updated = DateTime.Now;
                        storage.Save();
                    }
                    else
                    {
                        Console.WriteLine("Object {0} could not be found", commands[2]);
                    }
                }
                else if (string.Compare(commands[0], "Create", true) == 0)
                {
                    if (string.Compare(commands[1], "User", true) == 0)
                    {
                        Console.WriteLine("Required parameter: name");
                        string inp = Console.ReadLine();
                        User   u   = new User(inp);
                        storage.New(u);
                        storage.Save();
                    }
                    else if (string.Compare(commands[1], "Item", true) == 0)
                    {
                        Console.WriteLine("Please enter item name");
                        string iName = Console.ReadLine();
                        Console.WriteLine("Please enter item description");
                        string iDesc = Console.ReadLine();
                        Console.WriteLine("Please enter item price as a float");
                        string iPrice = Console.ReadLine();
                        Console.WriteLine("Please enter item tags, separated by a space");
                        string        iTags = Console.ReadLine();
                        float         price = -1f;
                        List <string> tags;
                        if (string.IsNullOrEmpty(iName))
                        {
                            Console.WriteLine("name is a required parameter");
                            continue;
                        }
                        if (string.IsNullOrEmpty(iDesc))
                        {
                            iDesc = "";
                        }
                        if (!(string.IsNullOrEmpty(iPrice)))
                        {
                            price = float.Parse(iPrice);
                        }
                        else
                        {
                            price = -1f;
                        }
                        if (!(string.IsNullOrEmpty(iTags)))
                        {
                            string[] tag = iTags.Split(' ');
                            tags = new List <string>(tag);
                        }
                        else
                        {
                            tags = null;
                        }
                        Item i = new Item(iName, iDesc, price, tags);
                        storage.New(i);
                        storage.Save();
                    }
                    else if (string.Compare(commands[1], "Inventory", true) == 0)
                    {
                        Console.WriteLine("Please input desired parameters in order separated by a space");
                        Console.WriteLine("Parameter order: user_id, item_id, quantity");
                        string   inp  = Console.ReadLine();
                        string[] inp2 = inp.Split(' ', 3);
                        if (inp2.Length < 3)
                        {
                            Console.WriteLine("All parameters are required");
                        }
                        Inventory inv = new Inventory(inp2[0], inp2[1], int.Parse(inp2[2]));
                        storage.New(inv);
                        storage.Save();
                    }
                    else
                    {
                        BaseClass n = new BaseClass();
                        storage.New(n);
                        storage.Save();
                    }
                }
                else if (string.Compare(commands[0], "Delete", true) == 0)
                {
                    string key = string.Format("{0}.{1}", commands[1], commands[2]);
                    if (a.ContainsKey(key))
                    {
                        storage.objects.Remove(key);
                        storage.Save();
                    }
                    else
                    {
                        Console.WriteLine("Object {0} could not be found", commands[2]);
                    }
                }
                else if (string.Compare(commands[0], "ClassNames", true) == 0)
                {
                    List <string> names = new List <string>();
                    foreach (var v in a.Values)
                    {
                        names.Add(v.GetType().ToString());
                    }
                    names = names.Distinct().ToList();
                    string res = string.Join(", ", names);
                    Console.WriteLine(res);
                }
                else
                {
                    Console.WriteLine("{0} is not a valid command", commands[0]);
                    continue;
                }
                Console.Write(prompt);
            }
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            JSONStorage myStorage = new JSONStorage();

            myStorage.Load();
            string[] testColection = { "part", "computer part", "fragile" };
            Item     testItem      = new Item("testItem1", "Just a Test", 6.0f, new List <String>(testColection));

            //Console.WriteLine(testItem.name);
            myStorage.New(testItem);
            myStorage.Save();
            Dictionary <String, String> myDict;

            String myIO(string propt)
            {
                Console.Write(propt);
                return(Console.ReadLine().ToLower().Trim());
            }

            bool getBegining(string wholeString)
            {
                String first = wholeString.Split('.')[0];

                if (first == "Item")
                {
                    return(true);
                }
                return(false);
            }

            string getEnd(string wholeString)
            {
                String first = wholeString.Split('.')[1];

                return(first.ToLower());
            }

            string getType(string wholeString)
            {
                String first = wholeString.Split('.')[0];

                return(first.ToLower());
            }

            void ShowAll(int x)
            {
                myDict = myStorage.All();
                switch (x)
                {
                case 0:
                    foreach (var entry in myDict)
                    {
                        Console.WriteLine($"{entry.Value} ID:{getEnd(entry.Key)}");
                    }
                    break;

                case 1:
                    foreach (var entry in myDict)
                    {
                        if (!getBegining(entry.Key))
                        {
                            Console.WriteLine($"{entry.Value} ID:{getEnd(entry.Key)}");
                        }
                    }
                    break;

                case 2:
                    foreach (var entry in myDict)
                    {
                        if (getBegining(entry.Key))
                        {
                            Console.WriteLine($"{entry.Value} ID:{getEnd(entry.Key)}");
                        }
                    }
                    break;
                }
            }

            void Create()
            {
                string classType = myIO("Would you like to create a <User> or <Item>?\n:");

                if ((classType != "user") && (classType != "item"))
                {
                    myIO($"<{classType}> is not a valid object type... please enter <User> or <Item>\n Press enter to try again...");
                    Create();
                }
                string newName = myIO("Please enter the name:");

                if (newName == "")
                {
                    myIO("You must enter a name...\n Press enter to try again...");
                    Create();
                }
                if (classType == "user")
                {
                    User newUser = new User(newName);
                    myStorage.New(newUser);
                    myStorage.Save();
                    myIO($"User {newName} Created!\nPress Enter to continue...\n");
                }
                else
                {
                    string        description = myIO("Please enter a discription of the item if you would like one stored\n:");
                    float         price       = float.Parse(myIO("Please enter a price of the item if you would like one stored\n:"));
                    float         priceReal   = (float)Math.Round(price * 100f) / 100f;
                    string        tagsEnterd  = myIO("Please enter any tags (in one line separated by spaces) for the item if you would like any stored\n:");
                    List <string> tags        = new List <string>(tagsEnterd.Split(' '));
                    Item          newItem     = new Item(newName, description, priceReal, tags);
                    myStorage.New(newItem);
                    myStorage.Save();
                    myIO($"Item {newName} Created!\nPress Enter to continue...\n");
                }
            }

            void Show()
            {
                string classType = myIO("Would you like to create a <User> or <Item>?\n:");

                if ((classType != "user") && (classType != "item"))
                {
                    myIO($"<{classType}> is not a valid object type... please enter <User> or <Item>\n Press enter to try again...");
                    Show();
                }
                string enterdID = myIO("Please enter the id of the item you would like to show\n:");

                myDict = myStorage.All();
                foreach (var entry in myDict)
                {
                    if (getEnd(entry.Key.ToLower()) == enterdID)
                    {
                        Console.WriteLine(entry.Value);
                        return;
                    }
                }
                myIO($"Object <id> could not be found...\n Press enter to try again...");
                Show();
            }

            void Delete()
            {
                string classType = myIO("Would you like to delete a <User> or <Item>?\n:");

                if ((classType != "user") && (classType != "item"))
                {
                    myIO($"<{classType}> is not a valid object type... please enter <User> or <Item>\n Press enter to try again...");
                    Delete();
                }
                string enterdID = myIO("Please enter the id of the item you would like to show\n:");

                myDict = myStorage.All();
                foreach (var entry in myDict)
                {
                    if (getEnd(entry.Key.ToLower()) == enterdID)
                    {
                        myStorage.objects.Remove(entry.Key);
                        myStorage.Save();
                        return;
                    }
                }
                myIO($"Object <id> could not be found...\n Press enter to try again...");
                Delete();
            }

            while (true)
            {
                string responce = myIO($"Inventory Manager\n{"-------------------------"}\n<ClassNames> show all ClassNames of objects\n<All> show all objects\n<All Users> show all Users\n<All Items> show all Items\n<Create> a new object\n<Show> an object\n<Update> an object\n<Delete> an object\n<Exit>\nPlease enter a command from the list above: ");
                switch (responce)
                {
                case "classnames":
                    myIO("Avalible class names are: Item, User\nPress Enter to continue...\n");
                    break;

                case "all":
                    Console.WriteLine("Here is a list of all entries:");
                    ShowAll(0);
                    myIO("Press Enter to continue...\n");
                    break;

                case "all users":
                    Console.WriteLine("Here is a list of all Users:");
                    ShowAll(1);
                    myIO("Press Enter to continue...\n");
                    break;

                case "all items":
                    Console.WriteLine("Here is a list of all Items:");
                    ShowAll(2);
                    myIO("Press Enter to continue...\n");
                    break;

                case "create":
                    Create();
                    break;

                case "show":
                    Create();
                    break;

                case "delete":
                    Delete();
                    break;

                case "exit":
                    return;

                    break;

                default:
                    myIO("Command not recognized...\nPress Enter to try again...\n");
                    break;
                }
            }
        }
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.White;
            //JSON Storage Engine Start
            JSONStorage storageDevice = new JSONStorage();

            storageDevice.Load();

            //List of all classes
            List <string> classList = new List <string>
            {
                "item",
                "user",
                "inventory"
            };

            //List of all Commands
            List <string> commandList = new List <string>
            {
                "classnames",
                "all",
                "create",
                "show",
                "update",
                "delete",
                "exit"
            };

            // //Response to unkown commands
            // string[] commandUnknown = new string[]
            // {
            //     "I'm sorry Dave, I'm afraid I can't do that...",
            //     "This command can serve no purpose anymore.",
            //     "I sincerely hope you weren't expecting a response.",
            //     "Stop it, you're going to brake it",
            //     "01001110 01101111",
            //     "Nope.avi",
            //     "Input a valid command, you have 20 seconds to comply",
            //     "... what now?",
            // };

            // Exit responses
            // string[] exitApp = new string[]
            // {
            //     "I'll be back",
            //     "Hasta la vista",
            //     "Thank you for helping us help you help us all",
            //     "Sleep mode activated",
            //     "It as a pleasure meeting you, now I'll go end my life",
            //     "System shotdown... BOOM"
            // };

            //Prompt Configuration
            string prompt = "$ ";

            Random randomNumber = new Random();

            string[] promptList = new string[] {
                "(° ~°)> ", "(* ~*)> ",
                "(° -°)> ", "(° .°)> ",
                "(` _´)> ", "(O _o)> ",
                "(n _n)> ", "(u _u)> ",
                "(ò _ó)> ", "(¬ _¬)> ",
                "(* -*)> ", "(⌐■_■)> "
            };
            Dictionary <string, ConsoleColor> colorlist = new Dictionary <string, ConsoleColor>();

            colorlist.Add("(° ~°)> ", ConsoleColor.Blue);
            colorlist.Add("(* ~*)> ", ConsoleColor.DarkBlue);
            colorlist.Add("(° -°)> ", ConsoleColor.Yellow);
            colorlist.Add("(° .°)> ", ConsoleColor.DarkYellow);
            colorlist.Add("(` _´)> ", ConsoleColor.Magenta);
            colorlist.Add("(O _o)> ", ConsoleColor.DarkMagenta);
            colorlist.Add("(n _n)> ", ConsoleColor.Green);
            colorlist.Add("(u _u)> ", ConsoleColor.DarkGreen);
            colorlist.Add("(ò _ó)> ", ConsoleColor.Red);
            colorlist.Add("(¬ _¬)> ", ConsoleColor.DarkRed);
            colorlist.Add("(* -*)> ", ConsoleColor.Cyan);
            colorlist.Add("(⌐■_■)> ", ConsoleColor.DarkCyan);

            //User input parser
            string[] userSplit;

            //Console App
            while (true)
            {
                //Display Command list (with Colors!)
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Inventory Manager");
                Console.WriteLine("-------------------------");
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine("<ClassNames> show all ClassNames of objects");
                Console.WriteLine("<All> show all objects");
                Console.WriteLine("<All [ClassName]> show all objects of a ClassName");
                Console.WriteLine("<Create [ClassName]> a new object");
                Console.WriteLine("<Show [ClassName object_id]> an object");
                Console.WriteLine("<Update [ClassName object_id]> an object");
                Console.WriteLine("<Delete [ClassName object_id]> an object");
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("<Exit>");
                //End of Command List Display

                //Choose random string and color for prompt
                int randomConsole = randomNumber.Next(0, promptList.Length);
                prompt = promptList[randomConsole];
                Console.ForegroundColor = colorlist[prompt];
                Console.Write(prompt);

                //Split and turn to lowercase user input
                userSplit = Console.ReadLine().Split(' ');

                //~ "METHODS" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                switch (userSplit[0].ToLower())
                {
                /// <summary>
                /// Displays all available classes.
                /// </summary>
                case "classnames":
                {
                    foreach (string classes in classList)
                    {
                        Console.WriteLine($"{classes}");
                    }
                    continue;
                }

                /// <summary>
                /// Displays all elements.
                /// </summary>
                /// <param name="ClassName">Optional: show only objects with this Class.</param>
                case "all":
                {
                    //Display all elements of a given ClassName.
                    try
                    {
                        if (userSplit[1].ToLower() != null)
                        {
                            if (classList.Contains(userSplit[1].ToLower()))
                            {
                                foreach (KeyValuePair <string, object> kvp in storageDevice.objects)
                                {
                                    if (kvp.Key.Split('.')[0] == userSplit[1].ToLower())
                                    {
                                        Console.WriteLine(kvp.Value);
                                        Console.WriteLine("-------");
                                    }
                                }
                            }
                            else
                            {
                                Console.WriteLine($"{userSplit[1]} is not a valid object type");
                            }
                        }
                    }
                    catch (IndexOutOfRangeException)
                    {
                        //Display all elements in the Objects dictionary.
                        foreach (KeyValuePair <string, object> kvp in storageDevice.objects)
                        {
                            Console.WriteLine(kvp.Value);
                            Console.WriteLine("-------");
                        }
                    }
                    continue;
                }

                /// <summary>
                /// Creates a new object.
                /// </summary>
                /// <param name="ClassName">Object Class.</param>
                case "create":
                {
                    if (userSplit.Length < 2)
                    {
                        Console.WriteLine("Object ClassName is missing");
                        continue;
                    }
                    else
                    {
                        try
                        {
                            if (classList.Contains(userSplit[1].ToLower()))
                            {
                                storageDevice.New(userSplit[1].ToLower());
                                Console.WriteLine($"Object of the class {userSplit[1]} has been created");
                                storageDevice.Save();
                            }
                            else
                            {
                                Console.WriteLine($"{userSplit[1]} is not a valid object type");
                                continue;
                            }
                        }
                        catch (IndexOutOfRangeException)
                        {
                            Console.WriteLine("Usage: Create <ClassName>");
                            continue;
                        }
                    }
                    continue;
                }

                /// <summary>
                /// Shows an existing object.
                /// </summary>
                /// <param name="ClassName">Object Class.</param>
                /// <param name="Id">Object Id.</param>
                case "show":
                {
                    if (userSplit.Length == 2)
                    {
                        Console.WriteLine("Id is missing");
                        continue;
                    }
                    else if (userSplit.Length == 1)
                    {
                        Console.WriteLine("ClassName is missing");
                        continue;
                    }
                    else
                    {
                        if (classList.Contains(userSplit[1].ToLower()))
                        {
                            bool found = false;
                            foreach (KeyValuePair <string, object> kvp in storageDevice.objects)
                            {
                                if (kvp.Key.Split('.')[0] == userSplit[1].ToLower() && kvp.Key.Split('.')[1] == userSplit[2])
                                {
                                    Console.WriteLine(storageDevice.objects[kvp.Key]);
                                    found = true;
                                    break;
                                }
                            }
                            if (found == false)
                            {
                                Console.WriteLine($"Object {userSplit[2]} could not be found");
                            }
                        }
                        else
                        {
                            Console.WriteLine($"{userSplit[1]} is not a valid object type");
                        }
                    }
                    continue;
                }

                /// <summary>
                /// Updates an existing object.
                /// </summary>
                /// <param name="ClassName">Object Class.</param>
                /// <param name="Id">Object Id.</param>
                case "update":
                {
                    if (userSplit.Length == 2)
                    {
                        Console.WriteLine("Id is missing");
                        continue;
                    }
                    else if (userSplit.Length == 1)
                    {
                        Console.WriteLine("ClassName is missing");
                        continue;
                    }
                    continue;
                }

                /// <summary>
                /// Deletes an existing object. Usage: Delete [ClassName] [Id]
                /// </summary>
                /// <param name="ClassName">Object Class.</param>
                /// <param name="Id">Object Id.</param>
                case "delete":
                {
                    if (userSplit.Length == 2)
                    {
                        Console.WriteLine("Id is missing");
                        continue;
                    }
                    else if (userSplit.Length == 1)
                    {
                        Console.WriteLine("ClassName is missing");
                        continue;
                    }
                    else
                    {
                        if (classList.Contains(userSplit[1].ToLower()))
                        {
                            bool found = false;
                            foreach (KeyValuePair <string, object> kvp in storageDevice.objects)
                            {
                                if (kvp.Key.Split('.')[0] == userSplit[1].ToLower() && kvp.Key.Split('.')[1] == userSplit[2])
                                {
                                    storageDevice.objects.Remove(kvp.Key);
                                    storageDevice.Save();
                                    found = true;
                                    break;
                                }
                            }
                            if (found == false)
                            {
                                Console.WriteLine($"Object {userSplit[2]} could not be found");
                            }
                        }
                        else
                        {
                            Console.WriteLine($"{userSplit[1]} is not a valid object type");
                        }
                    }
                    continue;
                }

                /// <summary>
                /// Terminatess the console.
                /// </summary>
                case "exit":
                {
                    // int response = randomNumber.Next(0, exitApp.Length);
                    // Console.WriteLine(exitApp[response]);
                    Console.ForegroundColor = ConsoleColor.White;
                    return;
                }

                /// <summary>
                /// For unknown commands
                /// </summary>
                default:
                {
                    // int response = randomNumber.Next(0, commandUnknown.Length);
                    // Console.WriteLine(commandUnknown[response]);
                    Console.WriteLine("Command Unkown");
                    continue;
                }
                }
            }
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.White;
            //JSON Storage Engine Start
            JSONStorage storageDevice = new JSONStorage();

            storageDevice.Load();

            //List of all classes
            List <string> classList = new List <string>
            {
                "item",
                "user",
                "inventory"
            };

            //List of all Commands
            List <string> commandList = new List <string>
            {
                "classnames",
                "all",
                "create",
                "show",
                "update",
                "delete",
                "exit"
            };

            //Prompt Configuration
            string prompt = "(n _n)> ";

            //User input parser
            string[] userSplit;

            //Console App
            while (true)
            {
                //Display Command list (with Colors!)
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Inventory Manager");
                Console.WriteLine("-------------------------");
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine("<ClassNames> show all ClassNames of objects");
                Console.WriteLine("<All> show all objects");
                Console.WriteLine("<All [ClassName]> show all objects of a ClassName");
                Console.WriteLine("<Create [ClassName]> a new object");
                Console.WriteLine("<Show [ClassName object_id]> an object");
                Console.WriteLine("<Update [ClassName object_id]> an object");
                Console.WriteLine("<Delete [ClassName object_id]> an object");
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("<Exit>");
                //End of Command List Display

                //Print The prompt
                Console.Write(prompt);

                //Split and turn to lowercase user input
                userSplit = Console.ReadLine().Split(' ');

                switch (userSplit[0].ToLower())
                {
                /// <summary>
                /// Displays all available classes.
                /// </summary>
                case "classnames":
                {
                    foreach (string classes in classList)
                    {
                        Console.WriteLine($"{classes}");
                    }
                    continue;
                }

                /// <summary>
                /// Displays all elements.
                /// </summary>
                /// <param name="ClassName">Optional: show only objects with this Class.</param>
                case "all":
                {
                    //Display all elements of a given ClassName.
                    try
                    {
                        if (userSplit[1].ToLower() != null)
                        {
                            if (classList.Contains(userSplit[1].ToLower()))
                            {
                                foreach (KeyValuePair <string, object> kvp in storageDevice.objects)
                                {
                                    if (kvp.Key.Split('.')[0] == userSplit[1].ToLower())
                                    {
                                        Console.WriteLine(kvp.Value);
                                        Console.WriteLine("-------");
                                    }
                                }
                            }
                            else
                            {
                                Console.WriteLine($"{userSplit[1]} is not a valid object type");
                            }
                        }
                    }
                    catch (IndexOutOfRangeException)
                    {
                        //Display all elements in the Objects dictionary.
                        foreach (KeyValuePair <string, object> kvp in storageDevice.objects)
                        {
                            Console.WriteLine(kvp.Value);
                            Console.WriteLine("-------");
                        }
                    }
                    continue;
                }

                /// <summary>
                /// Creates a new object.
                /// </summary>
                /// <param name="ClassName">Object Class.</param>
                case "create":
                {
                    if (userSplit.Length < 2)
                    {
                        Console.WriteLine("Object ClassName is missing");
                        continue;
                    }
                    else
                    {
                        try
                        {
                            if (classList.Contains(userSplit[1].ToLower()))
                            {
                                storageDevice.New(userSplit[1].ToLower());
                                Console.WriteLine($"Object of the class {userSplit[1]} has been created");
                                storageDevice.Save();
                            }
                            else
                            {
                                Console.WriteLine($"{userSplit[1]} is not a valid object type");
                                continue;
                            }
                        }
                        catch (IndexOutOfRangeException)
                        {
                            Console.WriteLine("Usage: Create <ClassName>");
                            continue;
                        }
                    }
                    continue;
                }

                /// <summary>
                /// Shows an existing object.
                /// </summary>
                /// <param name="ClassName">Object Class.</param>
                /// <param name="Id">Object Id.</param>
                case "show":
                {
                    if (userSplit.Length == 2)
                    {
                        Console.WriteLine("Id is missing");
                        continue;
                    }
                    else if (userSplit.Length == 1)
                    {
                        Console.WriteLine("ClassName is missing");
                        continue;
                    }
                    else
                    {
                        if (classList.Contains(userSplit[1].ToLower()))
                        {
                            bool found = false;
                            foreach (KeyValuePair <string, object> kvp in storageDevice.objects)
                            {
                                if (kvp.Key.Split('.')[0] == userSplit[1].ToLower() && kvp.Key.Split('.')[1] == userSplit[2])
                                {
                                    Console.WriteLine(storageDevice.objects[kvp.Key]);
                                    found = true;
                                    break;
                                }
                            }
                            if (found == false)
                            {
                                Console.WriteLine($"Object {userSplit[2]} could not be found");
                            }
                        }
                        else
                        {
                            Console.WriteLine($"{userSplit[1]} is not a valid object type");
                        }
                    }
                    continue;
                }

                /// <summary>
                /// Updates an existing object.
                /// </summary>
                /// <param name="ClassName">Object Class.</param>
                /// <param name="Id">Object Id.</param>
                case "update":
                {
                    if (userSplit.Length == 2)
                    {
                        Console.WriteLine("Id is missing");
                        continue;
                    }
                    else if (userSplit.Length == 1)
                    {
                        Console.WriteLine("ClassName is missing");
                        continue;
                    }
                    continue;
                }

                /// <summary>
                /// Deletes an existing object. Usage: Delete [ClassName] [Id]
                /// </summary>
                /// <param name="ClassName">Object Class.</param>
                /// <param name="Id">Object Id.</param>
                case "delete":
                {
                    if (userSplit.Length == 2)
                    {
                        Console.WriteLine("Id is missing");
                        continue;
                    }
                    else if (userSplit.Length == 1)
                    {
                        Console.WriteLine("ClassName is missing");
                        continue;
                    }
                    else
                    {
                        if (classList.Contains(userSplit[1].ToLower()))
                        {
                            bool found = false;
                            foreach (KeyValuePair <string, object> kvp in storageDevice.objects)
                            {
                                if (kvp.Key.Split('.')[0] == userSplit[1].ToLower() && kvp.Key.Split('.')[1] == userSplit[2])
                                {
                                    storageDevice.objects.Remove(kvp.Key);
                                    storageDevice.Save();
                                    found = true;
                                    break;
                                }
                            }
                            if (found == false)
                            {
                                Console.WriteLine($"Object {userSplit[2]} could not be found");
                            }
                        }
                        else
                        {
                            Console.WriteLine($"{userSplit[1]} is not a valid object type");
                        }
                    }
                    continue;
                }

                /// <summary>
                /// Terminatess the console.
                /// </summary>
                case "exit":
                {
                    Console.ForegroundColor = ConsoleColor.White;
                    return;
                }

                /// <summary>
                /// For unknown commands
                /// </summary>
                default:
                {
                    Console.WriteLine("Command Unkown");
                    continue;
                }
                }
            }
        }
Exemplo n.º 8
0
        public static void Main()
        {
            JSONStorage storageDevice = new JSONStorage();

            storageDevice.Load();

            List <string> classList = new List <string>
            {
                "item",
                "user",
                "inventory"
            };

            List <string> commandList = new List <string>
            {
                "classnames",
                "all",
                "create",
                "show",
                "update",
                "delete",
                "exit"
            };

            string prompt = "$ ";

            string[] userSplit;

            while (true)
            {
                Console.WriteLine("Inventory Manager");
                Console.WriteLine("-------------------------");
                Console.WriteLine("<ClassNames> show all ClassNames of objects");
                Console.WriteLine("<All> show all objects");
                Console.WriteLine("<All [ClassName]> show all objects of a ClassName");
                Console.WriteLine("<Create [ClassName]> a new object");
                Console.WriteLine("<Show [ClassName object_id]> an object");
                Console.WriteLine("<Update [ClassName object_id]> an object");
                Console.WriteLine("<Delete [ClassName object_id]> an object");
                Console.WriteLine("<Exit>");

                int randomConsole = randomNumber.Next(0, promptList.Length);
                prompt = promptList[randomConsole];
                Console.Write(prompt);

                userSplit = Console.ReadLine().Split(' ');


                switch (userSplit[0].ToLower())
                {
                /// <summary>Displays all available classes.</summary>
                case "classnames":
                {
                    foreach (string classes in classList)
                    {
                        Console.WriteLine($"{classes}");
                    }
                    continue;
                }

                /// <summary> Displays all elements. </summary>
                case "all":
                {
                    //Display all elements of a given ClassName.
                    try
                    {
                        if (userSplit[1].ToLower() != null)
                        {
                            if (classList.Contains(userSplit[1].ToLower()))
                            {
                                foreach (KeyValuePair <string, object> kvp in storageDevice.objects)
                                {
                                    if (kvp.Key.Split('.')[0] == userSplit[1].ToLower())
                                    {
                                        Console.WriteLine(kvp.Value);
                                        Console.WriteLine("-------");
                                    }
                                }
                            }
                            else
                            {
                                Console.WriteLine($"{userSplit[1]} is not a valid object type");
                            }
                        }
                    }
                    catch (IndexOutOfRangeException)
                    {
                        //Display all elements in the Objects dictionary.
                        foreach (KeyValuePair <string, object> kvp in storageDevice.objects)
                        {
                            Console.WriteLine(kvp.Value);
                            Console.WriteLine("-------");
                        }
                    }
                    continue;
                }

                /// <summary>Creates a new object.</summary>
                case "create":
                {
                    if (userSplit.Length < 2)
                    {
                        Console.WriteLine("Object ClassName is missing");
                        continue;
                    }
                    else
                    {
                        try
                        {
                            if (classList.Contains(userSplit[1].ToLower()))
                            {
                                storageDevice.New(userSplit[1].ToLower());
                                Console.WriteLine($"Object of the class {userSplit[1]} has been created");
                                storageDevice.Save();
                            }
                            else
                            {
                                Console.WriteLine($"{userSplit[1]} is not a valid object type");
                                continue;
                            }
                        }
                        catch (IndexOutOfRangeException)
                        {
                            Console.WriteLine("Usage: Create <ClassName>");
                            continue;
                        }
                    }
                    continue;
                }

                /// <summary> Shows an existing object.</summary>
                case "show":
                {
                    if (userSplit.Length == 2)
                    {
                        Console.WriteLine("Id is missing");
                        continue;
                    }
                    else if (userSplit.Length == 1)
                    {
                        Console.WriteLine("ClassName is missing");
                        continue;
                    }
                    else
                    {
                        if (classList.Contains(userSplit[1].ToLower()))
                        {
                            bool found = false;
                            foreach (KeyValuePair <string, object> kvp in storageDevice.objects)
                            {
                                if (kvp.Key.Split('.')[0] == userSplit[1].ToLower() && kvp.Key.Split('.')[1] == userSplit[2])
                                {
                                    Console.WriteLine(storageDevice.objects[kvp.Key]);
                                    found = true;
                                    break;
                                }
                            }
                            if (found == false)
                            {
                                Console.WriteLine($"Object {userSplit[2]} could not be found");
                            }
                        }
                        else
                        {
                            Console.WriteLine($"{userSplit[1]} is not a valid object type");
                        }
                    }
                    continue;
                }

                /// <summary> Updates an existing object. </summary>
                case "update":
                {
                    if (userSplit.Length == 2)
                    {
                        Console.WriteLine("Id is missing");
                        continue;
                    }
                    else if (userSplit.Length == 1)
                    {
                        Console.WriteLine("ClassName is missing");
                        continue;
                    }
                    continue;
                }

                /// <summary>Deletes an existing object. Usage: Delete [ClassName] [Id]</summary>
                case "delete":
                {
                    if (userSplit.Length == 2)
                    {
                        Console.WriteLine("Id is missing");
                        continue;
                    }
                    else if (userSplit.Length == 1)
                    {
                        Console.WriteLine("ClassName is missing");
                        continue;
                    }
                    else
                    {
                        if (classList.Contains(userSplit[1].ToLower()))
                        {
                            bool found = false;
                            foreach (KeyValuePair <string, object> kvp in storageDevice.objects)
                            {
                                if (kvp.Key.Split('.')[0] == userSplit[1].ToLower() && kvp.Key.Split('.')[1] == userSplit[2])
                                {
                                    storageDevice.objects.Remove(kvp.Key);
                                    storageDevice.Save();
                                    found = true;
                                    break;
                                }
                            }
                            if (found == false)
                            {
                                Console.WriteLine($"Object {userSplit[2]} could not be found");
                            }
                        }
                        else
                        {
                            Console.WriteLine($"{userSplit[1]} is not a valid object type");
                        }
                    }
                    continue;
                }

                /// <summary> Terminatess the console.</summary>
                case "exit":
                {
                    // int response = randomNumber.Next(0, exitApp.Length);
                    // Console.WriteLine(exitApp[response]);
                    Console.ForegroundColor = ConsoleColor.White;
                    return;
                }

                /// <summary>For unknown commands</summary>
                default:
                {
                    // int response = randomNumber.Next(0, commandUnknown.Length);
                    // Console.WriteLine(commandUnknown[response]);
                    Console.WriteLine("Command Unkown");
                    continue;
                }
                }
            }
        }