Changes between Version 1 and Version 2 of codekata/harrypoter


Ignore:
Timestamp:
08/20/2013 12:01:52 PM (13 years ago)
Author:
chenyang
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • codekata/harrypoter

    v1 v2  
    1 '''2013年8月9日代码道场活动纪实''' 
     1'''2013年8月9日代码道场活动纪实'''[[BR]] 
     2[[BR]] 
     3'''代码道场的参与者''':秦鸿源,陈阳,王安宁,丁健勇,邝巨恒,李炳岳,黄志强,李剑文,张艺辉, 李峰[[BR]] 
     4[[BR]] 
     5'''地点''':4G会议室[[BR]] 
     6        本次代码道场的题目,从大家推荐的题目中选出,考虑到上次活动的题目难度太大,我们选择了秦鸿源提供的题目.[[BR]] 
     7        题目如下: 
     8{{{ 
     9        购买哈利波特书本,每本8块钱 
     10        1.买2集,9.5折 
     11        2.买3集,9折 
     12        3.买4集,8.5折 
     13        4.买5集,8折 
     14        5.买6集,7.5折 
     15        6.买7集,7折 
     16         
     17        求出读者所买的书的总价 
     18 
     19        扩展: 
     20        单集购买5本,优惠7元。 
     21        支持买6送1活动。 
     22        支持购买100元送10活动。 
     23}}}      
     24 
     25活动刚开始,我们依然是先讨论需求,包括本人(陈阳)在内少部分同事,一开始没理解"几本","几集",的概念,将它们混淆,经过小组成员的[[BR]] 
     26解释,大家都明白后,由我起头,创建了名为harrypotter的项目,按照讨论的结果,我们先创建了Book对象,设计了两个属性价格和第几集,[[BR]] 
     27编写测试代码的时候,发现用int类型表示第几集,不合理,然后由秦鸿源提出,用枚举,于是将第几集,设计成枚举,代码如下: 
     28{{{ 
     29package cn.pconline.harrypotter; 
     30 
     31/** 
     32 * 
     33 * @author pc 
     34 */ 
     35public enum BookType { 
     36    one, 
     37    two, 
     38    three, 
     39    four, 
     40    five, 
     41    six, 
     42    seven 
     43     
     44} 
     45         
     46}}} 
     47Book.java 如下: 
     48{{{ 
     49package cn.pconline.harrypotter; 
     50 
     51/** 
     52 * 
     53 * @author pc 
     54 */ 
     55public class Book { 
     56    private int price; 
     57    private BookType bookType; 
     58 
     59    private Book(){ 
     60         
     61    } 
     62     
     63    public Book(BookType bookType){ 
     64        this.price = 800; 
     65        this.bookType = bookType; 
     66    } 
     67     
     68    public Book(int price,BookType bookType){ 
     69        this.price = price; 
     70        this.bookType = bookType; 
     71    } 
     72     
     73    public int getPrice() { 
     74        return price; 
     75    } 
     76 
     77    public void setPrice(int price) { 
     78        this.price = price; 
     79    } 
     80 
     81    public BookType getBookType() { 
     82        return bookType; 
     83    } 
     84 
     85    public void setBookType(BookType bookType) { 
     86        this.bookType = bookType; 
     87    } 
     88 
     89} 
     90         
     91}}} 
     92我们不想一开始的时候,就实现得很复杂,我们循序渐进,由易到难,先完成,每一本书都买一本的情况。[[BR]] 
     93经大家计论,我们设计BookStore对象来实现,计算总价的方法,public int calculate(List<Book> list)[[BR]] 
     94这里有一个小插曲,这个方法,本来被设计成返回类型是double的,但考虑到用浮点数计算,会损失精度,我们用分表示,总价的单位,所以最后被改成int类型[[BR]] 
     95我们先编写测试代码 
     96{{{ 
     97/* 
     98 * To change this template, choose Tools | Templates 
     99 * and open the template in the editor. 
     100 */ 
     101package cn.pconline.harrypotter; 
     102 
     103import java.util.ArrayList; 
     104import java.util.List; 
     105import org.junit.After; 
     106import org.junit.AfterClass; 
     107import org.junit.Assert; 
     108import org.junit.Before; 
     109import org.junit.BeforeClass; 
     110import org.junit.Test; 
     111import static org.junit.Assert.*; 
     112 
     113/** 
     114 * 
     115 * @author pc 
     116 */ 
     117public class BookStoreTest { 
     118    private List<Book> list = new ArrayList<Book>(); 
     119    private BookStore bs = null; 
     120     
     121    public BookStoreTest() { 
     122    } 
     123     
     124    @BeforeClass 
     125    public static void setUpClass() { 
     126    } 
     127     
     128    @AfterClass 
     129    public static void tearDownClass() { 
     130    } 
     131     
     132    @Before 
     133    public void setUp() { 
     134         bs = new BookStore(); 
     135    } 
     136     
     137    @After 
     138    public void tearDown() { 
     139    } 
     140 
     141    /** 
     142     * Test of calculate method, of class BookStore. 
     143     */ 
     144    @Test 
     145    public void testCalculate() { 
     146         
     147        //没买任何书 
     148        int price0 = bs.calculate(list); 
     149        Assert.assertEquals(price0, 0); 
     150        //买一集1本,计算价格 
     151        Book book1 = new Book(BookType.one); 
     152        list.add(book1); 
     153        int price = bs.calculate(list); 
     154        Assert.assertEquals(price, 800); 
     155         
     156        list.add(new Book(BookType.two));        
     157        int price2 =  bs.calculate(list); 
     158        assertEquals(price2, ((800 * 2 * 95)/ 100)); 
     159         
     160        list.add(new Book(BookType.three)); 
     161        int price3 = bs.calculate(list); 
     162        Assert.assertEquals(price3, (800 * 3 * 90) / 100); 
     163         
     164        list.add(new Book(BookType.four)); 
     165        int price4 = bs.calculate(list); 
     166        Assert.assertEquals(price4, (800 * 4 * 85) / 100); 
     167         
     168        list.add(new Book(BookType.five)); 
     169        int price5 = bs.calculate(list); 
     170        Assert.assertEquals(price5, (800 * 5 * 80) / 100); 
     171         
     172        list.add(new Book(BookType.six)); 
     173        int price6 = bs.calculate(list); 
     174        Assert.assertEquals(price6, (800 * 6 * 75) / 100); 
     175         
     176        list.add(new Book(BookType.seven)); 
     177        int price7 = bs.calculate(list); 
     178        Assert.assertEquals(price7,(800 * 7 * 70)/100); 
     179         
     180    } 
     181   
     182     
     183     
     184} 
     185                 
     186}}} 
     187接下来,准备写这个方法的实现了,轮到了王安宁操刀,王安宁给大家带了惊喜,王安宁以非常简洁优雅的代码代替 
     188了穷举式的if...else...else if... else 
     189代码如下: 
     190{{{ 
     191    public int calculate(List<Book> list) { 
     192        if (list == null || list.isEmpty()) { 
     193            return 0; 
     194        } 
     195        int discount = 100 - (5 * (list.size() - 1)); 
     196        return 800 * list.size() * discount / 100; 
     197    } 
     198         
     199}}} 
     200这次的活动,比较完美,达到了预期的目的,每个小组成员都得到了编写代码的机会,也享受到了,团队协作完成任务的乐趣。[[BR]] 
     201还从有经验的同事身上学习到了编程技巧,还得到了李峰赞扬。[[BR]] 
     202下次活动,我们将实现扩展需求,待续......[[BR]]