I have been engaged in development work for more than two years, and the work record folder is several GB without knowing it. Today, I will share an example of a shopping cart used in the previous project. Although the technology used is relatively old (drag-and-drop control lateral view), I think it contains a lot of object-oriented ideas that can be chewed carefully, in particular, the subordination between products and shopping cart objects. Purchase
I have been engaged in development work for more than two years, and the work record folder is several GB without knowing it. Today, I will share an example of a shopping cart used in the previous project. Although the technology used is relatively old (drag-and-drop control lateral view), I think it contains a lot of object-oriented ideas that can be chewed carefully, in particular, the subordination between products and shopping cart objects. Purchase
I have been engaged in development work for more than two years, and the work record folder is several GB without knowing it. Today, I will share an example of a shopping cart used in the previous project. Although the technology used is relatively old (drag-and-drop control lateral view), I think it contains a lot of object-oriented ideas that can be chewed carefully, in particular, the subordination between products and shopping cart objects. The shopping cart is an old and common topic, hoping to make a good result. The following is a brief introduction! (Via: Girl gift Network)
This short and concise shopping cart has three main functions: 1. Adjust the discount scheme 2. Commodity List 3. Shopping Cart
- Discount Scheme Adjustment
- Item List
- Shopping Cart
- The core idea code of shopping cart is as follows:
Product. cs
1 using System; 2 using System.Collections.Generic; 3 4 [Serializable] 5 public class Product { 6 7 int id; 8 9 public int Id {10 get { return id; }11 set { id = value; }12 }13 14 string name;15 16 public string Name {17 get { return name; }18 set { name = value; }19 }20 21 decimal price;22 23 public decimal Price {24 get { return price; }25 set { price = value; }26 }27 28 string unit;29 30 public string Unit {31 get { return unit; }32 set { unit = value; }33 }34 35 public Product(int id, string name, decimal price, string unit) {36 this.id = id;37 this.name = name;38 this.price = price;39 this.unit = unit;40 }41 }ShopCartItem. cs
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. web; 5 6 [Serializable] 7 public class ShopCartItem {8 9 private Product product; 10 private int count; 11 12 public Product {13 get {return product ;} 14 set {product = value;} 15} 16 public int Count {17 get {return count;} 18 set {count = value;} 19} 20 21 ///22 // The total discount price for a single item. 23 ///24 public decimal Price {25 get {26 decimal price = (decimal) 0; 27 List
DiscountsUsing = (List
) HttpContext. current. application ["DiscountsUsing"]; 28 price = this. totalPrice; 29 foreach (IDiscountable discount in discountsUsing) {30 price = price * (decimal) discount. getDiscount (this. product. price, this. count); 31} 32 return price; 33} 34} 35 36 ///
37 // total original price of a single item 38 ///39 public decimal TotalPrice {40 get {41 return this. product. price * this. count; 42} 43} 44 45 public ShopCartItem (Product product, int count) {46 this. product = product; 47 this. count = count; 48} 49}
ShopCartSet. cs
1 using System; 2 using System. Collections. Generic; 3 using System. Linq; 4 using System. Web; 5 6 [Serializable] 7 public class ShopCartSet: IEnumerable
{8 9 private Dictionary
Items; 10 11 public ShopCartSet () {12 this. items = new Dictionary
(); 13} 14 15 ///
16 // total original prices of each item 17 ///18 public decimal TotalPrice {19 get {20 decimal price = (decimal) 0; 21 foreach (ShopCartItem item in this) {22 price = price + item. totalPrice; 23} 24 return price; 25} 26} 27 28 ///
29 // total discount price of each item 30 ///31 public decimal Price {32 get {33 decimal price = (decimal) 0; 34 foreach (ShopCartItem item in this) {35 price = price + item. price; 36} 37 return price; 38} 39} 40 41 public ShopCartItem this [int id] {42 get {43 return this. items [id]; 44} 45 set {46 this. items [id] = value; 47} 48} 49 50 public void Add (Product product, int count) {51 this. add (new ShopCartItem (product, count); 52} 53 54 public void Ad D (ShopCartItem item) {55 if (! This. items. containsKey (item. product. id) {56 this. items. add (item. product. id, item); 57} 58 else {59 this. items [item. product. id]. count ++; 60} 61} 62 63 public void Remove (int key) {64 this. items. remove (key); 65} 66 67 public void Remove (Product product) {68 this. items. remove (product. id); 69} 70 71 public void Remove (ShopCartItem shopCartItem) {72 this. items. remove (shopCartItem. product. id); 73} 74 75 # region interface 76 public IEnumerator
GetEnumerator () {77 return this. items. values. getEnumerator (); 78} 79 80 System. collections. IEnumerator System. collections. IEnumerable. getEnumerator () {81 return this. items. values. getEnumerator (); 82} 83 # endregion84}
Source code download