HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Simple Product Inventory software using C# and OOP

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
simpleinventoryproductusingsoftwareandoop

Problem

I did this small program, as part of a list of coding exercises. The list proposes the coding of a small software that's supposed to manage an inventory of products. Here's what is requested:


Product Inventory Project - Create an application which manages an
inventory of products. Create a product class which has a price, id,
and quantity on hand. Then create an inventory class which keeps track
of various products and can sum up the inventory value.

This is the features I implemented in this application:

  • Possibilty of adding products.



  • Local storage of the data as JSON objects.



  • List of all products.



  • Possibilty of remove an specific product.



  • You can clear the whoe inventory.



  • Get the value of the inventory (sum of price of all items).



  • Get the count of products in the inventory.



  • Get the unit count (sum of quantity of all items).



I've created a Visual Studio solution with two projects:

  • IvManager.ConsoleApp - The presentation part.



  • IvManager.Business - Library containing the business logic.



Inside of IvManager.Business I have three classes:

Product - The product object that I have to manage.

Inventory - This is a static class that holds a list of products and has several methods:

  • Load() - Private method that loads the data from the local file to the Inventory.Products list of products.



  • Save() - Private method that saves the products list to the disk (in JSON format).



  • RemoveProduct() - Remove an specific product accoding to its id.



  • Add() - Add a new product to the inventory.



  • GetNewId() - Gets an available id accoding to the items in the list.



  • GetProductCount() - Get the count of products in the inventory.



  • GetUnitCount() - Gets the sum of the quantity of all items.



  • GetInventoryValue() - Sum of the price of all items.



  • ClearInventory() - Removes all the items from the inventory.



DataManager - Private static class that handles save and recover data from the disk. It has two methods:

  • LoadProducts() -

Solution

-
:

private static void Save()
{
    DataManager.SaveProducts(Products);
}

public static void RemoveProduct(int productId)
{
    Inventory.Products.RemoveAll(x => x.Id == productId);
    Save();
}

public static void Add(Product product)
{
    Products.Add(product);
    Save();
}


So every time you add a product or remove a product, ALL products are re-saved? What if you have millions of products? Isn't that overkill and inefficient? (Answer: Yes)

-
What's with everything being static? What's the good reason behind it? I can see a good reason for at least the Inventory class to not be static - it has state! Get rid of all that static stuff, then create an instance of each class you need and use it normally.

-
:

public static int GetNewId()
{
    int id;
    if (Inventory.Products.Count == 0)
        id = 1;
    else
    {
        id = Inventory.Products.Last().Id + 1;
    }

    return id;
}


This is a really bad idea because what's the guarantee that the products are always stored and loaded in the exact same order, AND that their order in the list is never changed? (None, especially when the list is publicly settable and modifiable.) You can easily end up with multiple products with the same ID.

-
DataManager should be a repository.

Code Snippets

private static void Save()
{
    DataManager.SaveProducts(Products);
}

public static void RemoveProduct(int productId)
{
    Inventory.Products.RemoveAll(x => x.Id == productId);
    Save();
}

public static void Add(Product product)
{
    Products.Add(product);
    Save();
}
public static int GetNewId()
{
    int id;
    if (Inventory.Products.Count == 0)
        id = 1;
    else
    {
        id = Inventory.Products.Last().Id + 1;
    }

    return id;
}

Context

StackExchange Code Review Q#151265, answer score: 7

Revisions (0)

No revisions yet.