Changes between Initial Version and Version 1 of codekata/harrypotter4


Ignore:
Timestamp:
10/28/2013 10:27:38 AM (12 years ago)
Author:
chenyang
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • codekata/harrypotter4

    v1 v1  
     1'''2013年9月13,9.20日代码道场活动纪实'''[[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考虑到,实际生活中,商场有时也会进行一些折上折的促俏活动,所以,我们将再一次对购书活动的促俏方式进行扩展 
     14[[BR]] 
     15我们按照顺序,依次对上面几种促销方案,进行折扣,最后计算出总价 
     16[[BR]] 
     17我们设计折上折的促俏方案类为Best2Calculator,它实现了PriceCalculator接口。 
     18[[BR]] 
     19我们对各个方案组合开发了测试代码,如下: 
     20{{{ 
     21package cn.pconline.harrypotter; 
     22 
     23import static org.junit.Assert.*; 
     24 
     25import java.util.ArrayList; 
     26import java.util.List; 
     27 
     28import org.junit.Before; 
     29import org.junit.Test; 
     30 
     31public class Best2CalculatorTest { 
     32        private List<Book> books = null; 
     33        private Best2Calculator best2Calculator = null; 
     34        private Book book1 = new Book(BookType.one); 
     35        private Book book2 = new Book(BookType.two); 
     36        private Book book3 = new Book(BookType.three); 
     37        private Book book4 = new Book(BookType.four); 
     38        private Book book5 = new Book(BookType.five); 
     39        private Book book6 = new Book(BookType.six); 
     40        private Book book7 = new Book(BookType.seven); 
     41         
     42        @Before 
     43        public  void setup() { 
     44                books = new ArrayList<Book>(); 
     45                best2Calculator = new Best2Calculator();                                 
     46        } 
     47         
     48 
     49        /** 
     50         * 测试无满足方案(只买一本) 
     51         * @throws Exception 
     52         */ 
     53        @Test 
     54        public void testCalculate0() throws Exception { 
     55                books.add(book1); 
     56                int price = best2Calculator.calculate(books); 
     57                assertEquals(800, price); 
     58        } 
     59         
     60        /** 
     61         * //满足1种方案 
     62         */ 
     63        @Test 
     64        public void testCalculate1() { 
     65           //只满足第1种方案,多集少于5本,一共少于7本 
     66                books.add(book1); 
     67                books.add(book2); 
     68                books.add(book3); 
     69                int price = best2Calculator.calculate(books); 
     70                assertEquals(3 * 800 * 90/100 ,price); 
     71                 
     72           //只满足第2种方案,单集只买5-6本 
     73                books.clear(); 
     74                for (int i=0; i<5; i++) { 
     75                        books.add(book1); 
     76                } 
     77                price = best2Calculator.calculate(books); 
     78                assertEquals(5 * 800 - 700, price); 
     79                 
     80                books.add(book1); 
     81                price = best2Calculator.calculate(books); 
     82                assertEquals(6 * 800 - 700, price); 
     83 
     84           //只满足第3种方案不存在 
     85                 
     86           //只满足第4种方案不存在 
     87        } 
     88         
     89         
     90         
     91        /** 
     92         * 满足2种方案 
     93         */ 
     94        @Test 
     95        public void testCalculate2(){ 
     96                 //(单集5本,另有1集1本)符合方案1,方案2 
     97                   
     98                for (int i=0; i<5; i++) { 
     99                        books.add(book1); 
     100                } 
     101                books.add(book2); 
     102                int price = best2Calculator.calculate(books); 
     103                assertEquals(6 * 800 * 95/100 - 700, price); 
     104                 
     105                  //(7-12本,单集不能等于或大于5本)符合方案1,方案3 
     106                books.clear(); 
     107                for (int i = 0; i < 4; i++) { 
     108                        books.add(book1); 
     109                } 
     110                 
     111                for(int i = 0; i < 3; i++) { 
     112                        books.add(book3); 
     113                } 
     114                 
     115                price = best2Calculator.calculate(books); 
     116                assertEquals(7 * 800 * 95/100 - 800, price); 
     117                 
     118                //等于12本,单集不能等于或大于5本 
     119                books.clear(); 
     120                for (int i = 0; i < 4; i++) { 
     121                        books.add(book2); 
     122                } 
     123                 
     124                for  (int i = 0; i < 4; i++) { 
     125                        books.add(book5); 
     126                } 
     127                 
     128                for (int i = 0; i < 4; i++) { 
     129                        books.add(book3); 
     130                } 
     131                 
     132                price = best2Calculator.calculate(books); 
     133                assertEquals(12 * 800 * 90 / 100 - 800, price); 
     134                 
     135                //符合方案1,方案4,没有这种方案 
     136                 
     137                //符合方案2,方案3, 
     138                books.clear(); 
     139                for (int i = 0; i < 7; i++) { 
     140                        books.add(book3); 
     141                } 
     142                price = best2Calculator.calculate(books); 
     143                assertEquals(800 * 7 - 700 - 800, price); 
     144                //符合方案2,方案4,没有这种方案 
     145                 
     146                //符合方案3,方案4,没有这种方案 
     147        } 
     148         
     149        /** 
     150         *  满足3种方案 
     151         */ 
     152        @Test 
     153        public void testCalculate3(){ 
     154                //符合方案1,方案2,方案3 
     155                        // 买7 本,一集买2本,二集买5本 
     156                int price = 0; 
     157                books.clear(); 
     158                for (int i = 0; i < 5; i++) { 
     159                        books.add(book2); 
     160                } 
     161                books.add(book1); 
     162                books.add(book1); 
     163                 
     164                price = best2Calculator.calculate(books); 
     165                assertEquals(800 * 7 * 95 / 100 - 700 - 800,  price); 
     166                 
     167              //符合方案1,方案2,方案4,没有这种方案 
     168                 
     169                        //符合方案1,方案3,方案4 
     170                      //买19本,四集各买4本,一集买3本 
     171                 books.clear(); 
     172                 for (int i = 0; i < 4; i++) { 
     173                         books.add(book1); 
     174                         books.add(book2); 
     175                         books.add(book3); 
     176                         books.add(book4); 
     177                 } 
     178                 books.add(book5); 
     179                 books.add(book5); 
     180                 books.add(book5); 
     181                 price = best2Calculator.calculate(books); 
     182                 assertEquals(800 * 19 * 80 / 100 - (800 * 2) - 1000,  price); 
     183                         
     184                        //符合方案2,方案3,方案4 
     185                 /* 
     186                  * 单集18本 
     187                  *  
     188                  * (18 * 800) - (3 * 700) - (2 * 800) = 10700 
     189                  * 所以单集18本满足方案2,3,4 
     190                  */ 
     191                 books.clear(); 
     192                 for (int i = 0; i < 18; i++) { 
     193                         books.add(book1); 
     194                 } 
     195                  
     196                 price = best2Calculator.calculate(books); 
     197                 assertEquals(((18 * 800) - (3 * 700) - (2 * 800) - 10 * 100), price); 
     198        } 
     199         
     200        /** 
     201         * 满足4种方案 
     202         */ 
     203        @Test 
     204        public void testCalculate4(){ 
     205                //符合全部方案 
     206                /*买18本,买两集分别为9本 
     207                 * (18 * 800 * 95 / 100 - (2 * 700) - (2 * 800)) 
     208                 */ 
     209                 
     210                for (int i = 0; i < 9 ; i++) { 
     211                        books.add(book1); 
     212                        books.add(book2); 
     213                } 
     214                int price = best2Calculator.calculate(books); 
     215                assertEquals((18 * 800 * 95 / 100 - (2 * 700) - (2 * 800) - 10 * 100) , price); 
     216                 
     217        } 
     218} 
     219 
     220}}} 
     221Best2Calculator实现代码如下 
     222{{{ 
     223package cn.pconline.harrypotter; 
     224 
     225import java.util.List; 
     226 
     227public class Best2Calculator implements PriceCalculator { 
     228 
     229        @Override 
     230        public int calculate(List<Book> list) { 
     231            if (list == null || list.isEmpty()) { 
     232                return 0; 
     233            } 
     234            //方案1 
     235            PriceCalculator pc1 = new DefaultCashier(); 
     236            //方案2 
     237            PriceCalculator pc2 = new DiscountCashier(); 
     238            //方案3 
     239            PriceCalculator pc3 = new SendBookActivity(); 
     240            //方案4 
     241            PriceCalculator pc4 = new SendMoneyActivity(); 
     242            //没打折的总价 
     243            int tempPrice = list.size() * 800;  
     244 
     245            int currentPrice = 0; // 保存当前价钱 
     246             
     247            currentPrice = tempPrice - (tempPrice - pc1.calculate(list)); 
     248             
     249            currentPrice = currentPrice - (tempPrice - pc2.calculate(list)); 
     250             
     251            currentPrice = currentPrice - (tempPrice - pc3.calculate(list)); 
     252             
     253            if (currentPrice >= 1000) { 
     254                currentPrice = currentPrice - (currentPrice / 10000) * 1000;  
     255            } 
     256             
     257             
     258             
     259                return currentPrice; 
     260        } 
     261     
     262} 
     263         
     264}}} 
     265销售哈里波特书籍这个题目,真的很不错,我们从实现最基本功能到实现各种促俏方案,从易到难,循序渐进,[[BR]] 
     266我们做了好几期,大家都有收获,大家一起讨论,团队协作能力有一定的提高。对单元测试有了更深的理解,对大家养成单元测试的习惯,有了很大的促进作用。[[BR]] 
     267中间,我们使用模拟测试的技术,让新同事耳目一新。[[BR]] 
     268