static void Main(string[] args) { List <Product> products = new List <Product>() { new Product() { Name = "DVD", Value = 250.98 }, new Product() { Name = "Blue Ray", Value = 450 } }; RegularOrder regular = new RegularOrder(products); Console.WriteLine($"regular order total: {regular.CalculateTotalOrderPrice().ToString("C")}"); PreOrder pre = new PreOrder(products); Console.WriteLine($"pre order total: {pre.CalculateTotalOrderPrice().ToString("C")}"); // NOW WE USE A DECORATOR PremiumPreorder premiumPre = new PremiumPreorder(pre); Console.WriteLine($"pre order total for premium: {premiumPre.CalculateTotalOrderPrice().ToString("C")}"); // ANOTHER ONE EverythingMustGoOrder everythingOrder = new EverythingMustGoOrder(regular); Console.WriteLine($"everything must go (regular) order total: {everythingOrder.CalculateTotalOrderPrice().ToString("C")}"); EverythingMustGoOrder everythingPreOrder = new EverythingMustGoOrder(pre); Console.WriteLine($"everything must go (pre) order total: {everythingPreOrder.CalculateTotalOrderPrice().ToString("C")}"); }
public override bool HandleSaveTask() { bool success = false; try { RegularOrder order = SelectedOrder; if (order != null && ValidateInput()) { // Collects order information Collection <OrderItem> orderItems = new Collection <OrderItem>(); order.sender_id = Sender.id; order.recipient_id = Recipient.id; order.tour_id = cboDestination.SelectedValue.ToString(); order.order_status = CurrentDeliveryStatus.ToString(); int totalQuantity = 0; decimal totalValue = 0; decimal totalCost = 0; foreach (var item in ucItemsList.OrderItems) { OrderItem newOrderItem = new OrderItem() { id = IDGenerator.NewOrderItemId(order.id, orderItems.Count + 1), order_id = order.id, name = item.name, weight = item.weight, size = item.size, quantity = item.quantity, value = item.value, description = item.description, cost = item.cost, order_item_number = item.order_item_number }; orderItems.Add(newOrderItem); totalQuantity += item.quantity; totalValue += item.value * item.quantity; totalCost += item.cost; } order.total_quantity = totalQuantity; order.total_value = totalValue; order.total_cost = decimal.Parse(tbTotalCost.Text); if (rbtnDeliveryStatus_Closed.Checked) { order.closed_date = DateTime.Now; } string result = _business.Update(order, orderItems); if (string.IsNullOrEmpty(result)) { MessageBox.Show("Đơn hàng đã cập nhật thành công.", string.Empty, MessageBoxButtons.OK, MessageBoxIcon.None); ChangeOrderDetailsEditingStatus(false); _isEditing = false; success = true; DataBind(); PopulateOrderDetails(order); } else { MessageBox.Show(result, Constants.Messages.ERROR_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error); } } } catch (Exception ex) { AppLogger.logError(this.ToString(), "Error occurs when saving order changes.", ex); MessageBox.Show("Có lỗi xảy ra trong khi cập nhập đơn hàng.", Constants.Messages.ERROR_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error); } return(success); }
static void Main(string[] args) { var random = new Random(); // ********* Builder(Fluent) Pattern Usage: START ********* // creating dummy data for products simulation int capacity = 3; var products = new List <Product>(capacity); for (int i = 1; i <= capacity; i++) { products.Add( new Product { Id = Guid.NewGuid(), Name = $"Product {i}", Price = Math.Round(random.NextDouble() * 100, 2), NumberInStock = random.Next(100) }); } // creation of builder is mandatory. because we use it when we create manager for report builder var builder = new ProductStockReportBuilder(products); var manager = new ProductStockReportManager(builder); // report is generated by manager manager.BuildReport(); var productsReport = builder.GetReport(); Console.WriteLine(productsReport); // ********* Builder(Fluent) Pattern Usage: END ********* // ------------------------------------------------------------------------- // ********* Fluent Builder Interface with Recursive Generics Pattern Usage: START ********* var emp = EmployeeBuilderManager.NewEmployee .WithFirstName("Nuran").WithLastName("Tarlan").WithAge(18) .WithEMail("*****@*****.**") .WithMainPhone("xxx-xxx-xx-xx").WithBackupPhone("xxx-xxx-xx-xx") .AtPosition(Positions.FullStackDeveloper).WithSalary(500).Build(); Console.WriteLine(emp); // ********* Fluent Builder Interface with Recursive Generics Pattern Usage: END ********* // ------------------------------------------------------------------------- // ********* Faceted Builder Pattern Usage: START var car = new CarBuilderFacade() .Info.WithBrand(CarBrands.Subaru).WithModel("Impreza").CreatedAt(2003).WithDistance(186522.5) .Engine.WithEngine(1800).WithHorsePower(422).WithTorque(600) .Trade.WithPrice(18000.0m).IsSecondHand(true) .Address.InCity("Baku").InDealer("Subaru XXXXX-XX") .Build(); Console.WriteLine(car); // ********* Faceted Builder Pattern Usage: END // ------------------------------------------------------------------------- // ********* Factory Method Pattern Usage: START AirConditioner.InitializeFactories() .ExecuteCreation(Actions.Cooling, 20.2) .Operate(); // ********* Factory Method Pattern Usage: END // ------------------------------------------------------------------------- Console.WriteLine("\n-------------------------------\n"); // ********* Singleton Pattern Usage: START var db1 = SingletonDataContainer.GetInstance; Console.WriteLine($"Population of Sumqayit: {db1.GetPopulation("Sumqayit")}"); var db2 = SingletonDataContainer.GetInstance; Console.WriteLine($"Baku: {db2.GetPopulation("Baku")} people"); // ********* Singleton Pattern Usage: END // ------------------------------------------------------------------------- Console.WriteLine("\n-------------------------------\n"); // ********* Adapter Pattern Usage: START var xmlConverter = new CustomXmlConverter(); var adapter = new XmlToJsonAdapter(xmlConverter); adapter.ConvertXmlToJson(); // ********* Adapter Pattern Usage: END // ------------------------------------------------------------------------- Console.WriteLine("\n-------------------------------\n"); // ********* Composite Pattern Usage: START // single gift var phone = new SingleGift("Phone", 500); phone.CalculateTotalPrice(); Console.WriteLine(); // composite many gifts together var rootBox = new CompositeGift("Surprise Box for Teenagers", true); var truckToy = new SingleGift("Truck Toy", 220); var plainToy = new SingleGift("Plain Toy", 89); rootBox.Add(truckToy); rootBox.Add(plainToy); var childBox = new CompositeGift("Surprise Box for Children", false); var soldierToy = new SingleGift("Soldier Toy", 15); childBox.Add(soldierToy); rootBox.Add(childBox); rootBox.CalculateTotalPrice(); // ********* Composite Pattern Usage: END // ------------------------------------------------------------------------- Console.WriteLine("\n-------------------------------\n"); // ********* Decorator Pattern Usage: START var regularOrder = new RegularOrder(); Console.WriteLine(regularOrder.CalculateTotalOrderPrice() + "\n"); var preOrder = new PreOrder(); Console.WriteLine(preOrder.CalculateTotalOrderPrice() + "\n"); var premiumPreOrder = new PremiumPreOrder(preOrder); Console.WriteLine(premiumPreOrder.CalculateTotalOrderPrice()); // ********* Decorator Pattern Usage: END // ------------------------------------------------------------------------- Console.WriteLine("\n-------------------------------\n"); // ********* Command Pattern Usage: START var modifyPrice = new ModifyPrice(); var product = new Product2("Phone", 500); modifyPrice.Execute(new ProductCommand(product, PriceAction.Increase, 100)); modifyPrice.Execute(new ProductCommand(product, PriceAction.Decrease, 700)); modifyPrice.Execute(new ProductCommand(product, PriceAction.Decrease, 20)); Console.WriteLine(product); modifyPrice.UndoActions(); Console.WriteLine(product); // ********* Command Pattern Usage: END // ------------------------------------------------------------------------- Console.WriteLine("\n-------------------------------\n"); // ********* Strategy Pattern Usage: START var reports = new List <DeveloperReport> { new DeveloperReport { Id = 1, Name = "Dev1", Level = DeveloperLevel.Senior, HourlyRate = 30.5, WorkingHours = 160 }, new DeveloperReport { Id = 2, Name = "Dev2", Level = DeveloperLevel.Junior, HourlyRate = 20, WorkingHours = 120 }, new DeveloperReport { Id = 3, Name = "Dev3", Level = DeveloperLevel.Senior, HourlyRate = 32.5, WorkingHours = 130 }, new DeveloperReport { Id = 4, Name = "Dev4", Level = DeveloperLevel.Junior, HourlyRate = 24.5, WorkingHours = 140 } }; var calculatorContext = new SalaryCalculator(new JuniorDeveloperSalaryCalculator()); var juniorTotal = calculatorContext.Calculate(reports); Console.WriteLine($"Total amount for junior salaries is: {juniorTotal}"); calculatorContext.SetCalculator(new SeniorDeveloperSalaryCalculator()); var seniorTotal = calculatorContext.Calculate(reports); Console.WriteLine($"Total amount for senior salaries is: {seniorTotal}"); Console.WriteLine($"Total cost for all the salaries is: {juniorTotal + seniorTotal}"); // ********* Strategy Pattern Usage: END // ------------------------------------------------------------------------- Console.WriteLine("\n-------------------------------\n"); // ********* Facade Pattern Usage: START var facade = new Facade.Facade(); var chickenOrder = new Order() { DishName = "Chicken with rice", DishPrice = 20.0, User = "******", ShippingAddress = "Random street 123" }; var sushiOrder = new Order() { DishName = "Sushi", DishPrice = 52.0, User = "******", ShippingAddress = "More random street 321" }; facade.OrderFood(new List <Order> { chickenOrder, sushiOrder }); // ********* Facade Pattern Usage: END // ------------------------------------------------------------------------- Console.WriteLine("\n-------------------------------\n"); // ********* Prototype Pattern Usage: START var grid = new List <IBlock>(); grid.AddRange(new List <IBlock> { BlockFactory.Create("Hello, world!"), BlockFactory.Create("3"), BlockFactory.Create("16/05/2002"), // datetime BlockFactory.Create("08/22/2021") // string, because 12 month exist in calendar }); foreach (var block in grid) { Console.WriteLine(block); } var block5 = (DateTimeBlock)grid[2].Clone(); block5.Format = "MM/dd/yyyy"; grid.Add(block5); Console.WriteLine("\nblock5 is added..."); Console.WriteLine(block5); var block6 = (DateTimeBlock)grid[4].Clone(); block6.DateTime = DateTime.UtcNow; grid.Add(block6); Console.WriteLine("\nblock6 is added..."); Console.WriteLine(block6); // ********* Prototype Pattern Usage: END // ------------------------------------------------------------------------- Console.WriteLine("\n-------------------------------\n"); // ********* Protection-Proxy Pattern Usage: START var settings = new Settings("settings config"); Console.WriteLine(settings.GetConfig()); var authService = new AuthService(); var protectedSettings = new ProtectedSettings(authService); Console.WriteLine(protectedSettings.GetConfig()); // ********* Protection-Proxy Pattern Usage: END // ********* Iterator Pattern Usage: START var numbers = new CustomLinkedList <int>(1); for (int i = 2; i < 8; i++) { numbers.Add(i); } var iterator = numbers.Iterator; while (!iterator.Complete) { Console.WriteLine(iterator.Next()); } // Exception will be thrown // Console.WriteLine(iterator.Next()); // ********* Iterator Pattern Usage: END }
static void Main(string[] args) { // Factory Pattern var factory = new AirConditioner().ExecuteCreation(Actions.Cooling, 20); factory.Operate(); //--------------------------------------------------------------------- //Facted Builder Pattern var car = new CarBuilderFacade() .Info .WithColor("Black") .WithNumberOfDoors(4) .WithType("BMW") .Built .AtAddress("Address") .InCity("Alexandria") .Build(); Console.WriteLine(car.ToString()); //------------------------------------------------------------------------ // Composite Pattern var phone = new SingleGift("Phone", 256); phone.CalculateTotalPrice(); //composite gift var rootBox = new CompositeGift("RootBox", 0); var truckToy = new SingleGift("TruckToy", 289); var plainToy = new SingleGift("PlainToy", 587); rootBox.Add(truckToy); rootBox.Add(plainToy); var childBox = new CompositeGift("ChildBox", 0); var soldierToy = new SingleGift("SoldierToy", 200); childBox.Add(soldierToy); rootBox.Add(childBox); Console.WriteLine($"Total price of this composite present is: {rootBox.CalculateTotalPrice()}"); //----------------------------------------------------------------------- // Decorator Pattern var regularOrder = new RegularOrder(); Console.WriteLine(regularOrder.CalculateTotalOrderPrice()); Console.WriteLine(); var preOrder = new Preorder(); Console.WriteLine(preOrder.CalculateTotalOrderPrice()); Console.WriteLine(); var premiumPreorder = new PremiumPreorder(preOrder); Console.WriteLine(premiumPreorder.CalculateTotalOrderPrice()); //--------------------------------------------------------------------- // Strategy Pattern var reports = new List <DeveloperReport> { new DeveloperReport { Id = 1, Name = "Dev1", Level = DeveloperLevel.Senior, HourlyRate = 30.5, WorkingHours = 160 }, new DeveloperReport { Id = 2, Name = "Dev2", Level = DeveloperLevel.Junior, HourlyRate = 20, WorkingHours = 120 }, new DeveloperReport { Id = 3, Name = "Dev3", Level = DeveloperLevel.Senior, HourlyRate = 32.5, WorkingHours = 130 }, new DeveloperReport { Id = 4, Name = "Dev4", Level = DeveloperLevel.Junior, HourlyRate = 24.5, WorkingHours = 140 } }; var calculatorContext = new SalaryCalculator(new JuniorDevSalaryCalculator()); var juniorTotal = calculatorContext.Calculate(reports); Console.WriteLine($"Total amount for junior salaries is: {juniorTotal}"); calculatorContext.SetCalculator(new SeniorDevSalaryCalculator()); var seniorTotal = calculatorContext.Calculate(reports); Console.WriteLine($"Total amount for senior salaries is: {seniorTotal}"); Console.WriteLine($"Total cost for all the salaries is: {juniorTotal + seniorTotal}"); Console.Read(); }
private void btnCreateOrder_Click(object sender, EventArgs e) { if (ValidateInput() && MessageBox.Show(Constants.Messages.CREATE_ORDER_CONFIRMATION_MESSAGE, Constants.Messages.CREATE_ORDER_CONFIRMATION_CAPTION, MessageBoxButtons.OKCancel) == DialogResult.OK) { bool success = true; string saveResult = string.Empty; try { // Creates OrderBase instance and gets general info OrderBase order = new OrderBase(); order.id = rbtnOrderType_Regular.Checked ? IDGenerator.NewId <RegularOrder>() : IDGenerator.NewId <IrregularOrder>(); AppLogger.logDebug(this.ToString(), string.Format("Id: {0}", order.id)); order.payment_status = this.rbtnPaymentStatus_Paid.Checked ? Constants.PaymentStatus.Paid.ToString() : Constants.PaymentStatus.Unpaid.ToString(); order.order_status = Constants.DeliveryStatus.Waiting.ToString(); order.tour_id = cboDestination.SelectedValue.ToString(); order.created_date = DateTime.Now; order.created_by = SystemParam.CurrentUser.user_name; int totalQuantity = 0; decimal totalValue = 0; decimal totalCost = 0; foreach (var item in ucItemsList.OrderItems) { item.id = IDGenerator.NewOrderItemId(order.id, ucItemsList.OrderItems.IndexOf(item) + 1); item.order_id = order.id; totalQuantity += item.quantity; totalValue += item.value * item.quantity; totalCost += item.cost; } order.total_quantity = totalQuantity; order.total_value = totalValue; order.total_cost = totalCost; // Convert order info per order type if (rbtnOrderType_Regular.Checked) { RegularOrder regularOrder = order.ToRegular(); regularOrder.sender_id = Sender.id; regularOrder.recipient_id = Recipient.id; saveResult = _regularBusiness.Insert(regularOrder, ucItemsList.OrderItems); } else { IrregularOrder irregularOrder = order.ToIrregular(); irregularOrder.sender_name = tbSenderName.Text; irregularOrder.sender_phone = tbSenderPhoneNumber.Text; irregularOrder.sender_id_card_no = tbSenderIDCardNo.Text; irregularOrder.sender_address = tbSenderAddress.Text; irregularOrder.recipient_name = tbRecipientName.Text; irregularOrder.recipient_phone = tbRecipientPhoneNumber.Text; irregularOrder.recipient_id_card_no = tbRecipientIDCardNo.Text; irregularOrder.recipient_address = tbRecipientAddress.Text; if (rbtnPaymentStatus_Paid.Checked && (new IrregularOrderPaymentView(irregularOrder, ucItemsList.OrderItems)).ShowDialog() != System.Windows.Forms.DialogResult.OK) { AppLogger.logDebug(this.ToString(), "Payment process cancelled."); return; } saveResult = _irregularBusiness.Insert(irregularOrder, ucItemsList.OrderItems); } } catch (Exception ex) { AppLogger.logError(this.ToString(), ex.Message, ex); success = false; } if (success && string.IsNullOrEmpty(saveResult)) { AppLogger.logInfo(this.ToString(), "Finish inserting new order into database."); MessageBox.Show("Đã tạo thành công đơn hàng mới.", string.Empty, MessageBoxButtons.OK, MessageBoxIcon.None); this.DialogResult = System.Windows.Forms.DialogResult.OK; this.Close(); } else { if (string.IsNullOrEmpty(saveResult)) { saveResult = Constants.Messages.CREATE_ORDER_ERROR_MESSAGE; } MessageBox.Show(saveResult, Constants.Messages.ERROR_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error); } } }