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() - Load data from the disk.
- SaveProducts() - Save data to the disk.
Code of Product.cs:
[Serializable]
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
}
Code of Inventory.cs
:
public static class Inventory
{
public static List<Product> products;
public static List<Product> Products
{
get
{
if (products.Count == 0)
{
Load();
}
return products;
}
set { products = value; }
}
static Inventory()
{
Products = new List<Product>();
}
private static void Load()
{
Products = DataManager.LoadProducts();
}
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;
}
public static int GetProductCount()
{
return Inventory.Products.Count();
}
public static int GetUnitCount()
{
return Inventory.Products.Select(x => x.Quantity).Sum();
}
public static decimal GetInventoryValue()
{
return Inventory.Products.Select(x => (x.Price * x.Quantity)).Sum();
}
public static void ClearInventory()
{
Inventory.Products.Clear();
Save();
}
}
Code of DataManager.cs
:
static class DataManager
{
private static string dataPath = "data.json";
public static List<Product> LoadProducts()
{
List<Product> listOfProducts = new List<Product>();
if (File.Exists(dataPath))
{
string json = File.ReadAllText("data.json");
if (!string.IsNullOrWhiteSpace(json))
{
listOfProducts = JsonConvert.DeserializeObject<List<Product>>(json);
}
};
return listOfProducts;
}
public static void SaveProducts(List<Product> productsToSave)
{
if (!File.Exists(dataPath))
File.Create(dataPath);
string json = JsonConvert.SerializeObject(productsToSave);
File.WriteAllText(dataPath, json);
}
}
Here's the full code, including the console application where the presentation is: https://gist.github.com/andradedearthur/20d6fc4b1325c11ecc7822e0bdb19fe8
I'm a student of C# and Object Orientation, so I would like to know what can be improved in this code. Probably the biggest challenge to me was to define in what class each method should go, and what should be the responsibilities of each class. Any feedback is welcome.