In: Computer Science
Let’s say you have a light shop where you sale and purchase having different types of light and different electric appliances. Now using principles of OOP I want you want make an OOP program which can cater the aforementioned problem.(show different types of purchase and sales )
C#
public List<PurchaseOrder> Save(PurchaseOrder po)
{
List<PurchaseOrder> poList = InitialData();
po.PoNumber = "PO010003";
poList.Add(po);
return poList;
}
public ActionResult Update([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<ProductViewModel> products)
{
if (products != null && this.ModelState.IsValid)
{
var purchaseOrder = new PurchaseOrder()
{
DateOfOrder = DateTime.Now,
DateOfDelivery = DateTime.Now.AddDays(2),
ClientId = this.UserProfile.OrganizationId ?? 0,
SupplierId = products.FirstOrDefault().SupplierId,
CreatorId = this.UserProfile.Id
};
this.PurchaseOrders.Add(purchaseOrder);
var order = products.Select(product => new OrderQuantity()
{
ProductId = product.Id, PurchaseOrderId = purchaseOrder.Id, Quantity = product.Quantities
}).ToList();
this.OrderQuantities.Add(order);
this.AddToastMessage("Succefull", "You made order !", ToastType.Success);
return this.Redirect("/Private/Orders/All");
}
this.AddToastMessage("Error!", string.Empty, ToastType.Error);
return this.Json(products.ToDataSourceResult(request, this.ModelState));
}
public static Batch CreateOrders2()
{
var b = new Batch();
var o = new PurchaseOrder();
o.CustId = "001";
var i = new light();
i.ProdId = "111";
i.Price = 500;
i.Quantity = 1;
o.Item.Add(i);
i = new fan();
i.ProdId = "222";
i.Price = 150;
i.Quantity = 3;
o.light.Add(i);
o.fan.Add(i);
b.PurchaseOrder.Add(o);
o = new PurchaseOrder();
o.CustId = "002";
i = new cooker();
i.ProdId = "333";
i.Price = 250;
i.Quantity = 1;
b.PurchaseOrder.Add(o);
return b;
}
protected void btnSave_Click(object sender, EventArgs e)
{
PurchaseOrderModule module = new PurchaseOrderModule();
PurchaseOrder purchaseOrder = new PurchaseOrder();
try
{
purchaseOrder.CustomerId = Convert.ToInt32(ddlCustomers.SelectedValue);
purchaseOrder.PONo = txtPONo.Text;
purchaseOrder.Price = Convert.ToDecimal(txtPrice.Text);
purchaseOrder.CreatedBy = txtCreatedBy.Text;
purchaseOrder.DateCreated = Convert.ToDateTime(txtDateCreated.Text);
purchaseOrder.DeliveryDateTime = Convert.ToDateTime(txtDeliveryDate.Text);
purchaseOrder.Quantity = Convert.ToInt32(txtQuantity.Text);
purchaseOrder.Remarks = txtRemarks.Text;
purchaseOrder.Status = Convert.ToInt32(ddlStatus.SelectedValue);
purchaseOrder.TotalPrice = Convert.ToDecimal(txtTotalPrice.Text);
if (ViewState["id"] != null)
purchaseOrder.Id = Convert.ToInt32(ViewState["id"]);
module.Save(purchaseOrder);
Response.Redirect("~/PurchasedOrder.aspx");
//lblCustomer.Text = ddlCustomers.SelectedValue;
//Helper.EnableControls(false, Helper.GetControlsInPlaceHolder("InfoPlaceHolder", Master));
}
catch (Exception ex)
{
lblError.Text = "Please contact administrator. Error message : " + ex.Message;
}
}
public PurchaseOrder Add(PurchaseOrder order)
{
this.orders.Add(order);
this.orders.Save();
return order;
public CheckoutDetailsVM(PurchaseOrder p, string FullName, string Email, string BillingAddress, string ShippingAddress)
{
this.LastOrder = p;
this.FullName = FullName;
this.Email = Email;
this.BillingAddress = BillingAddress;
this.ShippingAddress = ShippingAddress;
}
public void CreateUpdateCustomer(PurchaseOrder order, IIdentity identity)
{
// try catch so this does not interrupt the order process.
try
{
var billingAddress = order.OrderAddresses.FirstOrDefault(x => x.Name == Constants.Order.BillingAddressName);
var shippingAddress = order.OrderAddresses.FirstOrDefault(x => x.Name == Constants.Order.ShippingAddressName);
MembershipUser user = null;
if (!identity.IsAuthenticated)
{
string email = null;
if (billingAddress != null)
{
email = billingAddress.Email.Trim();
user = Membership.GetUser(email);
}
if (user == null)
{
var customer = CreateCustomer(email, Guid.NewGuid().ToString(), billingAddress.DaytimePhoneNumber, billingAddress, shippingAddress, false,
createStatus => Log.Error("Failed to create user during order completion. " + createStatus.ToString()));
if (customer != null)
{
order.CustomerId = Guid.Parse(customer.PrimaryKeyId.Value.ToString());
order.CustomerName = customer.FirstName + " " + customer.LastName;
order.AcceptChanges(); SetExtraCustomerProperties(order, customer); _emailService.SendWelcomeEmail(billingAddress.Email);
}
}
else
{
var customer = CustomerContext.Current.GetContactForUser(user);
order.CustomerName = customer.FirstName + " " + customer.LastName;
order.CustomerId = Guid.Parse(customer.PrimaryKeyId.Value.ToString());
order.AcceptChanges();
SetExtraCustomerProperties(order, customer);
}
}
else
{
user = Membership.GetUser(identity.Name);
var customer = CustomerContext.Current.GetContactForUser(user);
SetExtraCustomerProperties(order, customer);
}
}
catch (Exception ex)
{
// Log here
Log.Error("Error during creating / update user", ex);
}
}
private void Create(string filename)
{
XmlSerializer ser=new XmlSerializer(typeof(PurchaseOrder));
PurchaseOrder po=new PurchaseOrder();
Address addr=new Address();
addr.FirstName="xxxx";
po.MyAddress=addr;
TextWriter writer=new StreamWriter(filename);
ser.Serialize(writer, po);
writer.Close();
}
private void Create(string filename)
{
XmlSerializer ser=new XmlSerializer(typeof(PurchaseOrder));
PurchaseOrder po=new PurchaseOrder();
Light item1=new Light("Philips", (decimal)500.2);
Fan item2=new Fan("Usha", (decimal)150.4);
Cooker item3=new Cooker("Prestige",(decimal)250.33)
po.ItemsOrdered=new Light[1];
po.ItemsOrdered=new Fan[3];
po.ItemsOrdered=new Cooker [1];
po.ItemsOrdered[0]=item1;
po.ItemsOrdered[1]=item2;
TextWriter writer=new StreamWriter(filename);
ser.Serialize(writer, po);
writer.Close();
}