| | 1 | '''2013年9月6日代码道场活动纪实'''[[BR]] |
| | 2 | [[BR]] |
| | 3 | '''代码道场的参与者''':秦鸿源,陈阳,王安宁,丁健勇,李炳岳,黄志强,李剑文,张艺辉,江毅超, 李峰[[BR]] |
| | 4 | [[BR]] |
| | 5 | '''地点''':4G会议室[[BR]] |
| | 6 | [[BR]] |
| | 7 | 回顾一下,上几期,我们做了购书活动的扩展 |
| | 8 | {{{ |
| | 9 | 单集购买5本,优惠7元。 |
| | 10 | 支持买6送1活动。 |
| | 11 | 支持购买100元送10活动。 |
| | 12 | }}} |
| | 13 | 经过讨论,我们打算将扩展进行到底,我们再添加一种扩展方案, 我们来实现扩展功能:[[BR]] |
| | 14 | 使用所有的方案结帐,哪种最省钱,就用哪种结帐,[[BR]] |
| | 15 | 我们在编写测试代码的时候,用到了Mockito这个测试框架,用来模拟前几次活动中的结帐方案, 测试代码如下: |
| | 16 | {{{ |
| | 17 | package cn.pconline.harrypotter; |
| | 18 | import java.util.ArrayList; |
| | 19 | import java.util.List; |
| | 20 | |
| | 21 | import junit.framework.Assert; |
| | 22 | |
| | 23 | import org.junit.Test; |
| | 24 | import org.mockito.Mockito; |
| | 25 | |
| | 26 | public class BestCalculatorTest { |
| | 27 | @Test |
| | 28 | public void testCalculate() throws Exception { |
| | 29 | |
| | 30 | |
| | 31 | //(0--4)单集不够5本 |
| | 32 | //( 5 )单集买5本(符合方案2) |
| | 33 | //( 6 )单集大于5本不够7本 (符合方案2)-- 单集大于40元(5本书的价钱)不够56元(最多7本) |
| | 34 | //(7--12)单集大于等于56元(7本书的价钱)小于100元(最多12本)(符合方案2,3) |
| | 35 | //(13~~ )单集大于100元(符合方案2,3,4) |
| | 36 | |
| | 37 | //(0--5)多集不够6本(方案1) |
| | 38 | |
| | 39 | //( 6 )多集大于5本少于7本 (方案1,方案2)-- 多集大于等于56元(7本书的价钱)小于100元(最多12本) |
| | 40 | //(有一集大于2本)符合方案1 |
| | 41 | //(单集6本)符合方案2 |
| | 42 | //(单集5本,另有1集1本)符合方案1,方案2 |
| | 43 | //(7 -- 12)多集大于等于7本,少于13本(方案1,方案2,方案3) |
| | 44 | //(有2集)符合方案1,方案3 |
| | 45 | //(有单集5本以上,全是单集)符合方案2,方案3 |
| | 46 | //(有2集以上书,有一集5本以上,7本以上)符合方案1,方案2,方案3 |
| | 47 | // (13~~ )多集大于100元(方案1,方案2,方案3,方案4) |
| | 48 | //(大于12本,有2集以上书)符合方案1,方案3,方案4 |
| | 49 | //(大于12本,有5集以上书)符合方案1,方案2,方案3,方案4 |
| | 50 | |
| | 51 | //模拟defaultCashier对象 |
| | 52 | DefaultCashier defaultCashier = Mockito.mock(DefaultCashier.class); |
| | 53 | List<Book> books = new ArrayList<Book>(); |
| | 54 | books.add(new Book(BookType.one)); |
| | 55 | books.add(new Book(BookType.one)); |
| | 56 | books.add(new Book(BookType.one)); |
| | 57 | Mockito.when(defaultCashier.calculate(books)).thenReturn(3); |
| | 58 | |
| | 59 | //模拟sendBookActivity对象 |
| | 60 | SendBookActivity sendBookActivity = Mockito.mock(SendBookActivity.class); |
| | 61 | Mockito.when(sendBookActivity.calculate(books)).thenReturn(5); |
| | 62 | |
| | 63 | //模拟sendMoneyActivity对象 |
| | 64 | SendMoneyActivity sendMoneyActivity = Mockito.mock(SendMoneyActivity.class); |
| | 65 | Mockito.when(sendMoneyActivity.calculate(books)).thenReturn(7); |
| | 66 | |
| | 67 | //模拟discountCashier对象 |
| | 68 | DiscountCashier discountCashier = |
| | 69 | Mockito.mock(DiscountCashier.class); |
| | 70 | Mockito.when(discountCashier.calculate(books)).thenReturn(9); |
| | 71 | |
| | 72 | //1.没书没方案 |
| | 73 | |
| | 74 | BestCalculator bestCalculator = new BestCalculator(); |
| | 75 | int price = bestCalculator.calculate(null); |
| | 76 | Assert.assertEquals(0, price); |
| | 77 | |
| | 78 | List<Book> emptyBooks = new ArrayList<Book>(); |
| | 79 | price = bestCalculator.calculate(emptyBooks); |
| | 80 | Assert.assertEquals(0, price); |
| | 81 | |
| | 82 | //2.有书没方案 |
| | 83 | bestCalculator.setCalculators(null); |
| | 84 | price = bestCalculator.calculate(books); |
| | 85 | Assert.assertEquals(2400, price); |
| | 86 | |
| | 87 | //3.有书有方案 |
| | 88 | List<PriceCalculator> calculators = |
| | 89 | new ArrayList<PriceCalculator>(); |
| | 90 | calculators.add(defaultCashier); |
| | 91 | calculators.add(sendBookActivity); |
| | 92 | calculators.add(sendMoneyActivity); |
| | 93 | calculators.add(discountCashier); |
| | 94 | bestCalculator.setCalculators(calculators); |
| | 95 | |
| | 96 | price = bestCalculator.calculate(books); |
| | 97 | Assert.assertEquals(3, price); |
| | 98 | |
| | 99 | //4.没书有方案 |
| | 100 | price = bestCalculator.calculate(null); |
| | 101 | Assert.assertEquals(0, price); |
| | 102 | |
| | 103 | price = bestCalculator.calculate(emptyBooks); |
| | 104 | Assert.assertEquals(0, price); |
| | 105 | |
| | 106 | } |
| | 107 | } |
| | 108 | |
| | 109 | }}} |
| | 110 | 实现代码 |
| | 111 | {{{ |
| | 112 | package cn.pconline.harrypotter; |
| | 113 | |
| | 114 | import java.util.ArrayList; |
| | 115 | import java.util.List; |
| | 116 | |
| | 117 | public class BestCalculator implements PriceCalculator { |
| | 118 | |
| | 119 | private List<PriceCalculator> calculators = new ArrayList<PriceCalculator>(); |
| | 120 | |
| | 121 | |
| | 122 | public List<PriceCalculator> getCalculators() { |
| | 123 | return calculators; |
| | 124 | } |
| | 125 | |
| | 126 | |
| | 127 | public void setCalculators(List<PriceCalculator> calculators) { |
| | 128 | this.calculators = calculators; |
| | 129 | } |
| | 130 | |
| | 131 | |
| | 132 | |
| | 133 | @Override |
| | 134 | public int calculate(List<Book> list) { |
| | 135 | // TODO Auto-generated method stub |
| | 136 | |
| | 137 | // 没有书的时候 |
| | 138 | if(list == null || list.isEmpty()){ |
| | 139 | return 0; |
| | 140 | } |
| | 141 | int minPrice = 800 * list.size(); // 最低价格 |
| | 142 | // 没有方案的时候 |
| | 143 | if(calculators == null || calculators.isEmpty()){ |
| | 144 | return minPrice; |
| | 145 | } |
| | 146 | |
| | 147 | |
| | 148 | for(PriceCalculator priceCalculator : calculators){ |
| | 149 | int price = priceCalculator.calculate(list); |
| | 150 | if (price < minPrice) { |
| | 151 | minPrice = price; |
| | 152 | } |
| | 153 | } |
| | 154 | return minPrice; |
| | 155 | } |
| | 156 | |
| | 157 | } |
| | 158 | }}} |