Design Pattern Series-Decorative mode

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;//Decorative Mode Demonamespacediscountapplication2{classProgram {Static voidMain (string[] args) {            //Preferential Scheme OneOrder Order1 =NewOrder (); Order1. Goodstotal= -; OrderDiscount OrderDiscount=NewOrderDiscount (); Applyfullreductiondiscount D1=NewApplyfullreductiondiscount ( -,Ten); ApplyDiscount D2=NewApplyDiscount (0.95, 200m); ApplyDiscount D3=NewApplyDiscount (0.8); Console.WriteLine ("\ n Preferential scheme one"); D1.            Setdiscountobject (OrderDiscount); D2.            Setdiscountobject (D1); D3.            Setdiscountobject (D2); D3.            Execute (Order1); //Preferential Scheme twoOrder Order2 =NewOrder (); Order2. Goodstotal= Max; OrderDiscount OrderDiscount2=NewOrderDiscount (); Applyfullreductiondiscount K1=NewApplyfullreductiondiscount ( -,9.9m); ApplyDiscount K2=NewApplyDiscount (0.98, 199m); ApplyDiscount K3=NewApplyDiscount (0.88); Console.WriteLine ("\ n Preferential scheme two"); K1.            Setdiscountobject (OrderDiscount2); K3.            Setdiscountobject (K1); K2.            Setdiscountobject (K3); K2.            Execute (ORDER2);        Console.readkey (); }    }     Public classOrder {/// <summary>        ///total value of goods/// </summary>         Public decimalgoodstotal{Get;Set;} /// <summary>        ///deduction of discount, final customer to pay amount/// </summary>         Public decimalgrandtotal{Get;Set;} }     Public InterfaceIorderdiscount {voidExecute (order order); }     Public classOrderdiscount:iorderdiscount { Public voidExecute (Order order) {Console.WriteLine ("The original price is {0}", order.        Goodstotal.toroundstring ()); }    }     Public Abstract classDiscountbase:iorderdiscount {PrivateIorderdiscount Discount =NULL;  Public  voidsetdiscountobject (iorderdiscount discount) { This. Discount =discount; }         Public Virtual voidExecute (Order order) {if(Discount! =NULL) {discount.            Execute (order); }        }    }      //Full reduction offer     Public classApplyfullreductiondiscount:discountbase {Private decimalFullmoney =decimal.        Zero; Private decimalReductionmoney =decimal.        Zero;  PublicApplyfullreductiondiscount (decimalFullmoney,decimalReductionmoney) {             This. Fullmoney =Fullmoney;  This. Reductionmoney =Reductionmoney; }         Public Override voidExecute (Order order) {Base.            Execute (order); if(Order. Goodstotal >=Fullmoney) {Order. Goodstotal= order. Goodstotal-Reductionmoney; Console.WriteLine ("{ 2} amount after {0} minus {1}", This. Fullmoney, This. Reductionmoney, order.                Goodstotal.toroundstring ()); return; } Console.WriteLine ("condition not satisfied, cannot use offer with {0} minus {1}", This. Fullmoney, This. Reductionmoney); }    }    //Discounted Rates     Public classApplydiscount:discountbase {Private DoubleDiscountRate =1d; Private decimal? Moneycondition =NULL; PrivateOrder order =NULL;  PublicApplyDiscount (DoubleDiscountRate,decimal?moneycondition) {             This. DiscountRate =discountrate;  This. moneycondition =moneycondition; }         PublicApplyDiscount (Doublediscountrate): This(DiscountRate,NULL)        {        }         Public Override voidExecute (Order order) { This. Order =order; Base. Execute ( This. order); if( This. moneycondition! =NULL)            {                if( This. Order. Goodstotal >= This. Moneycondition.value) {                     This.                Executediscount (); }                Else{Console.WriteLine ("The condition is not satisfied, the amount is not full {0}, cannot meet the discount of {1} discount", This. moneyCondition.Value.ToRoundString (), This. discountrate); return; }            }            Else            {                 This.            Executediscount (); } Console.WriteLine ("{1} after the {0} discount is hit", This. DiscountRate, order.        Goodstotal.toroundstring ()); }        Private voidExecutediscount () { This. Order. Goodstotal = This. Order. Goodstotal * (decimal) This. DiscountRate; }    }    //Extending the decimal type     Public Static classDecimalextend { Public Static stringToroundstring ( This decimald) {returnMath.Round (D,2, Midpointrounding.toeven). ToString ("F2"); }    }}

Design Pattern Series-Decorative mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.