弹指一挥间,从事开发工作两年多了,工作记录文件夹不知不觉好几G了。今天分享下之前项目中用到的一个购物车示例,虽然用的技术比较老(拖放控件DataGview),我觉得里面包含了很多可以细细咀嚼的 面向对象思想 ,尤其是商品和购物车各个对象的从属关系。购
弹指一挥间,从事开发工作两年多了,工作记录文件夹不知不觉好几G了。今天分享下之前项目中用到的一个购物车示例,虽然用的技术比较老(拖放控件DataGview),我觉得里面包含了很多可以细细咀嚼的面向对象思想,尤其是商品和购物车各个对象的从属关系。购物车老生常谈的东西,希望能起到抛砖引玉的效果。下面就简单介绍下吧!(via:女孩礼物网)
此款短小精悍的购物车主要有三大功能:1.折扣方案调整 2.商品列表 3.购物车
Product.cs
<span> 1</span> <span>using</span><span> System; </span><span> 2</span> <span>using</span><span> System.Collections.Generic; </span><span> 3</span> <span> 4</span> <span>[Serializable] </span><span> 5</span> <span>public</span> <span>class</span><span> Product { </span><span> 6</span> <span> 7</span> <span>int</span><span> id; </span><span> 8</span> <span> 9</span> <span>public</span> <span>int</span><span> Id { </span><span>10</span> <span>get</span> { <span>return</span><span> id; } </span><span>11</span> <span>set</span> { id =<span> value; } </span><span>12</span> <span> } </span><span>13</span> <span>14</span> <span>string</span><span> name; </span><span>15</span> <span>16</span> <span>public</span> <span>string</span><span> Name { </span><span>17</span> <span>get</span> { <span>return</span><span> name; } </span><span>18</span> <span>set</span> { name =<span> value; } </span><span>19</span> <span> } </span><span>20</span> <span>21</span> <span>decimal</span><span> price; </span><span>22</span> <span>23</span> <span>public</span> <span>decimal</span><span> Price { </span><span>24</span> <span>get</span> { <span>return</span><span> price; } </span><span>25</span> <span>set</span> { price =<span> value; } </span><span>26</span> <span> } </span><span>27</span> <span>28</span> <span>string</span><span> unit; </span><span>29</span> <span>30</span> <span>public</span> <span>string</span><span> Unit { </span><span>31</span> <span>get</span> { <span>return</span><span> unit; } </span><span>32</span> <span>set</span> { unit =<span> value; } </span><span>33</span> <span> } </span><span>34</span> <span>35</span> <span>public</span> Product(<span>int</span> id, <span>string</span> name, <span>decimal</span> price, <span>string</span><span> unit) { </span><span>36</span> <span>this</span>.id =<span> id; </span><span>37</span> <span>this</span>.name =<span> name; </span><span>38</span> <span>this</span>.price =<span> price; </span><span>39</span> <span>this</span>.unit =<span> unit; </span><span>40</span> <span> } </span><span>41</span> }
ShopCartItem.cs
<span> 1</span> <span>using</span><span> System; </span><span> 2</span> <span>using</span><span> System.Collections.Generic; </span><span> 3</span> <span>using</span><span> System.Linq; </span><span> 4</span> <span>using</span><span> System.Web; </span><span> 5</span> <span> 6</span> <span>[Serializable] </span><span> 7</span> <span>public</span> <span>class</span><span> ShopCartItem { </span><span> 8</span> <span> 9</span> <span>private</span><span> Product product; </span><span>10</span> <span>private</span> <span>int</span><span> count; </span><span>11</span> <span>12</span> <span>public</span><span> Product Product { </span><span>13</span> <span>get</span> { <span>return</span><span> product; } </span><span>14</span> <span>set</span> { product =<span> value; } </span><span>15</span> <span> } </span><span>16</span> <span>public</span> <span>int</span><span> Count { </span><span>17</span> <span>get</span> { <span>return</span><span> count; } </span><span>18</span> <span>set</span> { count =<span> value; } </span><span>19</span> <span> } </span><span>20</span> <span>21</span> <span>///</span> <span><summary></summary></span> <span>22</span> <span>///</span><span> 单项总折后价。 </span><span>23</span> <span>///</span> <span></span> <span>24</span> <span>public</span> <span>decimal</span><span> Price { </span><span>25</span> <span>get</span><span> { </span><span>26</span> <span>decimal</span> price = (<span>decimal</span>)<span>0</span><span>; </span><span>27</span> List<idiscountable> discountsUsing = (List<idiscountable>)HttpContext.Current.Application[<span>"</span><span>DiscountsUsing</span><span>"</span><span>]; </span><span>28</span> price = <span>this</span><span>.TotalPrice; </span><span>29</span> <span>foreach</span> (IDiscountable discount <span>in</span><span> discountsUsing) { </span><span>30</span> price = price * (<span>decimal</span>)discount.GetDiscount(<span>this</span>.product.Price, <span>this</span><span>.count); </span><span>31</span> <span> } </span><span>32</span> <span>return</span><span> price; </span><span>33</span> <span> } </span><span>34</span> <span> } </span><span>35</span> <span>36</span> <span>///</span> <span><summary></summary></span> <span>37</span> <span>///</span><span> 单项总原价 </span><span>38</span> <span>///</span> <span></span> <span>39</span> <span>public</span> <span>decimal</span><span> TotalPrice { </span><span>40</span> <span>get</span><span>{ </span><span>41</span> <span>return</span> <span>this</span>.product.Price * <span>this</span><span>.count; </span><span>42</span> <span> } </span><span>43</span> <span> } </span><span>44</span> <span>45</span> <span>public</span> ShopCartItem(Product product, <span>int</span><span> count) { </span><span>46</span> <span>this</span>.product =<span> product; </span><span>47</span> <span>this</span>.count =<span> count; </span><span>48</span> <span> } </span><span>49</span> }</idiscountable></idiscountable>
ShopCartSet.cs
<span> 1</span> <span>using</span><span> System; </span><span> 2</span> <span>using</span><span> System.Collections.Generic; </span><span> 3</span> <span>using</span><span> System.Linq; </span><span> 4</span> <span>using</span><span> System.Web; </span><span> 5</span> <span> 6</span> <span>[Serializable] </span><span> 7</span> <span>public</span> <span>class</span> ShopCartSet : IEnumerable<shopcartitem><span> { </span><span> 8</span> <span> 9</span> <span>private</span> Dictionaryint, ShopCartItem><span> items; </span><span>10</span> <span>11</span> <span>public</span><span> ShopCartSet() { </span><span>12</span> <span>this</span>.items = <span>new</span> Dictionaryint, ShopCartItem><span>(); </span><span>13</span> <span> } </span><span>14</span> <span>15</span> <span>///</span> <span><summary></summary></span> <span>16</span> <span>///</span><span> 各项总原价 </span><span>17</span> <span>///</span> <span></span> <span>18</span> <span>public</span> <span>decimal</span><span> TotalPrice { </span><span>19</span> <span>get</span><span> { </span><span>20</span> <span>decimal</span> price = (<span>decimal</span>)<span>0</span><span>; </span><span>21</span> <span>foreach</span> (ShopCartItem item <span>in</span> <span>this</span><span>) { </span><span>22</span> price = price +<span> item.TotalPrice; </span><span>23</span> <span> } </span><span>24</span> <span>return</span><span> price; </span><span>25</span> <span> } </span><span>26</span> <span> } </span><span>27</span> <span>28</span> <span>///</span> <span><summary></summary></span> <span>29</span> <span>///</span><span> 各项总折后价 </span><span>30</span> <span>///</span> <span></span> <span>31</span> <span>public</span> <span>decimal</span><span> Price { </span><span>32</span> <span>get</span><span> { </span><span>33</span> <span>decimal</span> price = (<span>decimal</span>)<span>0</span><span>; </span><span>34</span> <span>foreach</span> (ShopCartItem item <span>in</span> <span>this</span><span>) { </span><span>35</span> price = price +<span> item.Price; </span><span>36</span> <span> } </span><span>37</span> <span>return</span><span> price; </span><span>38</span> <span> } </span><span>39</span> <span> } </span><span>40</span> <span>41</span> <span>public</span> ShopCartItem <span>this</span>[<span>int</span><span> id] { </span><span>42</span> <span>get</span><span> { </span><span>43</span> <span>return</span> <span>this</span><span>.items[id]; </span><span>44</span> <span> } </span><span>45</span> <span>set</span><span> { </span><span>46</span> <span>this</span>.items[id] =<span> value; </span><span>47</span> <span> } </span><span>48</span> <span> } </span><span>49</span> <span>50</span> <span>public</span> <span>void</span> Add(Product product, <span>int</span><span> count) { </span><span>51</span> <span>this</span>.Add(<span>new</span><span> ShopCartItem(product, count)); </span><span>52</span> <span> } </span><span>53</span> <span>54</span> <span>public</span> <span>void</span><span> Add(ShopCartItem item) { </span><span>55</span> <span>if</span> (!<span>this</span><span>.items.ContainsKey(item.Product.Id)) { </span><span>56</span> <span>this</span><span>.items.Add(item.Product.Id, item); </span><span>57</span> <span> } </span><span>58</span> <span>else</span><span> { </span><span>59</span> <span>this</span>.items[item.Product.Id].Count++<span>; </span><span>60</span> <span> } </span><span>61</span> <span> } </span><span>62</span> <span>63</span> <span>public</span> <span>void</span> Remove(<span>int</span><span> key) { </span><span>64</span> <span>this</span><span>.items.Remove(key); </span><span>65</span> <span> } </span><span>66</span> <span>67</span> <span>public</span> <span>void</span><span> Remove(Product product) { </span><span>68</span> <span>this</span><span>.items.Remove(product.Id); </span><span>69</span> <span> } </span><span>70</span> <span>71</span> <span>public</span> <span>void</span><span> Remove(ShopCartItem shopCartItem) { </span><span>72</span> <span>this</span><span>.items.Remove(shopCartItem.Product.Id); </span><span>73</span> <span> } </span><span>74</span> <span>75</span> <span>#region</span> 接口实现 <span>76</span> <span>public</span> IEnumerator<shopcartitem><span> GetEnumerator() { </span><span>77</span> <span>return</span> <span>this</span><span>.items.Values.GetEnumerator(); </span><span>78</span> <span> } </span><span>79</span> <span>80</span> <span> System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { </span><span>81</span> <span>return</span> <span>this</span><span>.items.Values.GetEnumerator(); </span><span>82</span> <span> } </span><span>83</span> <span>#endregion</span> <span>84</span> }</shopcartitem></shopcartitem>
源码下载