I have following business model and need to add product available stock quantities to make the quantities ordered do not exceed available items.
public class Product
{
public int productID { get; }
public string name { get; set; }
public int stockQuantity { get; set; }
//other properties here like address and such
}
public class Order
{
public int orderID { get; }
public Customer customer { get; set; }
public Dictionary<int, int> OrderItems { get; set; }
//other properties omitted
}
So, when the order is placed I can call it like so:
Order order =newOrder(); order.customer =HttpContext.Current.User; order.OrderItems.Add(newProduct("abc").ProductID,2)};
What would be the best way to make sure order quantities do not exceed stock quantities of any item in stock(in which case simple exception would be fine)?
I would write Add method on the Order class, something like this:
public class Order
{
public int orderID { get; }
public Customer customer { get; set; }
public Dictionary<int, int> OrderItems { get; set; }
//other properties omitted
public Add(int productId, int quantity)
{
// check quantity here - if exceeds stock, throw exception
// otherwise - add to the dictionary
}
}
One minor problem here is that you can still call Order.OrderItems.Add. The easiest way to solve this is to make OrderItems private and expose a read only property, something like this:
public class Order
{
...
private Dictionary<int, int> _orderItems { get; set; }
public Dictionary<int, int> OrderItems { get { return _orderItems; } }
...
}
You could have both! Why not have an overloaded method which allows you to call it both ways? And one of them, probably the int one, can call the other one after having selected the product based on the id.
Wouldn't it be easier if you put the products in a container? The container then knows the stock quantity and other meta data about the product type? Maybe even a factory?
Wouldn't it be easier if you put the products in a container? The container then knows the stock quantity and other meta data about the product type? Maybe even a factory?
Thanks for the idea. Could you elaborate a little bit on that? Something like a shopping cart you mean?
I would say, you have a Order. This order mainly contains it ID, the customer and the OrderLines.
public class Order
{
public int OrderID;
public Customer Customer;
public Dictionary<int, OrderLine> OrderLines;
}
The OrderLine has the details about the product; like the quantity, totalprice etc.
public class OrderLine
{
public Product Product;
public int Quantity;
public decimal TotalPrice;
}
Now I assume you have some kind of manager to manage the products?
public class ProductManager
{
public List<string> Errors;
public Product NewProduct(string name, int amount)
{
Product pro = new Product();
pro.Name = name;
if((stockAmount - amount) >= 0)
{
return pro;
}
else
{
return null;
//add error
}
}
}
You can use this setup in many ways. Create helpers, create different managers etc.
Ps. an shopping cart is nothing more than a fancy name for a object container, and in case of a shop its called shopping cart :) But its still just a container which contains it objects and meta info.
VictorNow
Member
21 Points
32 Posts
C# OOP question
Feb 06, 2012 02:47 PM|LINK
I have following business model and need to add product available stock quantities to make the quantities ordered do not exceed available items.
So, when the order is placed I can call it like so:
What would be the best way to make sure order quantities do not exceed stock quantities of any item in stock(in which case simple exception would be fine)?
oop
vytautas.ziu...
Contributor
3854 Points
691 Posts
Re: C# OOP question
Feb 06, 2012 02:54 PM|LINK
I would write Add method on the Order class, something like this:
One minor problem here is that you can still call Order.OrderItems.Add. The easiest way to solve this is to make OrderItems private and expose a read only property, something like this:
public class Order { ... private Dictionary<int, int> _orderItems { get; set; } public Dictionary<int, int> OrderItems { get { return _orderItems; } } ... }VictorNow
Member
21 Points
32 Posts
Re: C# OOP question
Feb 06, 2012 05:38 PM|LINK
Thanks for reply to my post. I was wondering what would be better :
public AddItem(int productId, int quantity)
{
}
or
public AddItem(Product product, int quantity)
{
}
In latter case I can write something like this:
order.AddItem(new Product("abc"), 10);
MetalAsp.Net
All-Star
112061 Points
18240 Posts
Moderator
Re: C# OOP question
Feb 06, 2012 05:50 PM|LINK
bmhc
Member
152 Points
60 Posts
Re: C# OOP question
Feb 09, 2012 12:45 PM|LINK
Wouldn't it be easier if you put the products in a container? The container then knows the stock quantity and other meta data about the product type? Maybe even a factory?
oop
VictorNow
Member
21 Points
32 Posts
Re: C# OOP question
Feb 09, 2012 01:51 PM|LINK
Thanks for the idea. Could you elaborate a little bit on that? Something like a shopping cart you mean?
bmhc
Member
152 Points
60 Posts
Re: C# OOP question
Feb 11, 2012 08:54 PM|LINK
I guess you're kind of making a webshop.
I would say, you have a Order. This order mainly contains it ID, the customer and the OrderLines.
public class Order { public int OrderID; public Customer Customer; public Dictionary<int, OrderLine> OrderLines; }The OrderLine has the details about the product; like the quantity, totalprice etc.
public class OrderLine { public Product Product; public int Quantity; public decimal TotalPrice; }Now I assume you have some kind of manager to manage the products?
public class ProductManager { public List<string> Errors; public Product NewProduct(string name, int amount) { Product pro = new Product(); pro.Name = name; if((stockAmount - amount) >= 0) { return pro; } else { return null; //add error } } }You can use this setup in many ways. Create helpers, create different managers etc.
Ps. an shopping cart is nothing more than a fancy name for a object container, and in case of a shop its called shopping cart :) But its still just a container which contains it objects and meta info.