Exemplo n.º 1
0
    private static int ClassNames(string[] input)
    {
        HashSet <string> classNames = new HashSet <string>();

        foreach (string key in jsonStorage.All().Keys)
        {
            string name = key.Split(".")[0];
            classNames.Add(name);
        }
        Console.WriteLine($"[\n  {String.Join(",\n  ", classNames)}\n]");
        return(0);
    }
Exemplo n.º 2
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.º 3
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.º 4
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;
                }
            }
        }
Exemplo n.º 5
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}");
 }