public class OrderManager
{
public void PlaceOrder(OrderDTO order)
{
// Validate order based on business rules
// Check for stock availablity on items ordered
// Add the order to the database
// Set the order id on the OrderDTO object
}
public bool CancelOrder(Guid orderId)
{
// Retrieve order from database
// Determine if the order can be canceled
// if order can be canceled, set as canceled
// return true/false if order was canceled
}
public bool AddItemToOrder(Guid orderId, OrderItemDTO ItemToAdd)
{
// Retrieve order from database
// Determine if the item can be added to the order
// Add a new item row in the database
// return true/false if item was added to the order
}
public bool ProcessOrder(Guid orderId)
{
// Check to ensure this order can be processed.
// Validate order based on business rules
// Update the stock levels of products ordered
// return true/false if order was processed
}
}
public class Order
{
private Guid _id;
private DateTime _creationDate;
private int _shippingMethod;
private int _status;
private List<OrderItems> _orderItems;
public Guid Id
{
get { return _id; }
set { _id = value; }
}
public List<OrderItems> OrderItems
{
get { return _orderItems; }
set { _orderItems = value; }
}
// Business Logic
public void Place()
{
// Validate order based on business rules to ensure it is in
// a good state to add to the database
// Check for stock availablity on items ordered
this.Add();
}
public void Cancel()
{
// Check to ensure this order can be canceled.
this.Status = Status.Cancelled();
this.Save();
}
public void ProcessOrder()
{
// Check to ensure this order can be processed.
// Validate order based on business rules
// Udpate the stock levels of products ordered
}
// Data Access Methods
public void Save()
{
// Code to persist changes to the database
}
public void Add()
{
// Code to Add this object to the database
}
public void Delete()
{
// Code to remove this object from the database
}
public static List<Order> FindAll()
{
// Code to retrive all Orders from the database
}
public static Order FindBy(Guid id)
{
// Code to retrive a specific Order from the database
}
}
public Guid Id
{
get { return _id; }
set { _id = value; }
}
public float ShippingCost()
{
return ShippingMethod.ShippingCostTo(this.DispatchAddress, this.ItemsTotalWeight());
}
public float Total()
{
return DiscountOffer.TotalPriceWithDiscountOfferAppliedTo(
this.Items, ShippingCost());
}
public void Process()
{
if (this.CanProcess())
{
// Charge the card
Customer.Card.Charge(this.Total());
// Set the status of the order
this.Status = Status.Shipped;
// Adjust the stock levels
foreach (OrderItem item in Items)
{
item.Product.DecreaseStockBy(item.QtyOrdered);
}
else
{
throw new InvalidOrderStateForOperationException(
String.Format(
"Order {0} cannot be processed in its current state {1}",
this.Id, this.Status.ToString());
}
}
public bool CanProcess()
{
if (!this.Status == Status.Shipped && !this.Status = Status.Cancelled)
{
return (this.HasEnoughStockFor(me.Items) &&