Changes between Version 4 and Version 5 of codekata


Ignore:
Timestamp:
05/30/2013 09:52:31 AM (13 years ago)
Author:
lifeng
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • codekata

    v4 v5  
    2121* 顾问只能问问题,不能提方案,驾驶员和导航员必须回答所有问题 
    2222 
    23 = 2013-05-10 && 2013-05-17 活动 = 
    24 为了减少对工作的影响,每周活动安排在周五下午,每次活动时间控制为2小时。 
    25 题目由参与同事各自出一个,之后汇总由大家以无记名投票的方式选举出一个。本次丁健勇贡献的题目贴近生活,有趣味,难度适中。最后以较高票数中标。题目如下:[[BR]] 
    26  
    27 {{{ 
    28   
    29 公司饭堂排队吃饭,有11元,13元,15元3个窗口。 
    30   
    31 固定有500人就餐,其中280人第一选择是11元窗口,150人第一选择是13元窗口,70人第一选择是15元窗口。 
    32   
    33 每分钟11元窗口可打饭3人,13元窗口可打饭2人,15元窗口可打饭1人,每分钟有10名员工依次进场。 
    34   
    35 由于人的排队耐心有限,所以假如11元窗口的队伍人数大于50人时,员工会选择13元窗口,当13元窗口的队伍人数大于30人时,员工会选择15元窗口, 
    36 当15元窗口人数大于15人时,员工则不会再等待,直接出外就餐。 
    37   
    38 求出饭堂一天的营业额。 
    39 }}} 
    40  
    4123[[Image(kata1.png, 500px)]] 
    42  
    43  
    44 依靠小组成员的集体智慧与优秀的团队合作精神,经过2周的时间,成功地完成了这一题目。 
    45  
    46 [[BR]] 
    47 测试代码如下: 
    48 {{{ 
    49 package lunch; 
    50  
    51 import java.util.HashMap; 
    52 import java.util.LinkedList; 
    53 import java.util.List; 
    54 import java.util.Map; 
    55 import org.junit.After; 
    56 import org.junit.AfterClass; 
    57 import org.junit.Assert; 
    58 import org.junit.Before; 
    59 import org.junit.BeforeClass; 
    60 import org.junit.Test; 
    61 import static org.junit.Assert.*; 
    62  
    63 /** 
    64  * 
    65  * @author pc 
    66  */ 
    67 public class LunchTest { 
    68  
    69     private Lunch lunch = null; 
    70     private LinkedList<String> list; 
    71  
    72     public LunchTest() { 
    73     } 
    74  
    75     @BeforeClass 
    76     public static void setUpClass() { 
    77     } 
    78  
    79     @AfterClass 
    80     public static void tearDownClass() { 
    81     } 
    82  
    83     @Before 
    84     public void setUp() { 
    85         lunch = new Lunch(); 
    86         list = lunch.getPeople(); 
    87         list.offer("11"); 
    88         list.offer("13"); 
    89         list.offer("11"); 
    90         list.offer("11"); 
    91         list.offer("15"); 
    92         list.offer("15"); 
    93         list.offer("11"); 
    94         list.offer("13"); 
    95         list.offer("13"); 
    96         list.offer("13"); 
    97  
    98         list.offer("13"); 
    99         list.offer("13"); 
    100         list.offer("13"); 
    101         list.offer("13"); 
    102         list.offer("15"); 
    103         list.offer("15"); 
    104         list.offer("11"); 
    105         list.offer("13"); 
    106         list.offer("13"); 
    107         list.offer("13"); 
    108     } 
    109  
    110     @After 
    111     public void tearDown() { 
    112     } 
    113  
    114  
    115     @Test 
    116     public void testInit() { 
    117         lunch = new Lunch(); 
    118         lunch.init(); 
    119         List<String> people = lunch.getPeople(); 
    120         Assert.assertEquals(Lunch.TOTAL_PEOPLE, lunch.getPeople().size()); 
    121         int count11 = 0; 
    122         int count13 = 0; 
    123         int count15 = 0; 
    124         for (String s : people) { 
    125             if ("11".equals(s)) { 
    126                 count11++; 
    127             } 
    128             if ("13".equals(s)) { 
    129                 count13++; 
    130             } 
    131             if ("15".equals(s)) { 
    132                 count15++; 
    133             } 
    134         } 
    135         //TODO 验证首选11元的人数 
    136         Assert.assertEquals(Lunch.TOTAL_PEOPLE_11, count11); 
    137  
    138         //TODO 验证首选13元的人数 
    139         Assert.assertEquals(Lunch.TOTAL_PEOPLE_13, count13); 
    140  
    141         //TODO 验证首选15元的人数 
    142         Assert.assertEquals(Lunch.TOTAL_PEOPLE_15, count15); 
    143  
    144         //TODO 验证随机 
    145  
    146     } 
    147  
    148     @Test 
    149     public void testCheckpoint() { 
    150         //lunch.init(); 
    151  
    152         Map<String, Integer> queue = lunch.getQueue(); 
    153         queue.put("11", 0); 
    154         queue.put("13", 0); 
    155         queue.put("15", 0); 
    156         lunch.setQueue(queue); 
    157          
    158         lunch.checkpoint(); 
    159         Assert.assertEquals(10, list.size()); 
    160         Assert.assertEquals(1, (int) queue.get("11")); 
    161         Assert.assertEquals(2, (int) queue.get("13")); 
    162         Assert.assertEquals(1, (int) queue.get("15")); 
    163         Assert.assertEquals(4, lunch.getPersonCount11()); 
    164         Assert.assertEquals(4, lunch.getPersonCount13()); 
    165         Assert.assertEquals(2, lunch.getPersonCount15()); 
    166          
    167         lunch.checkpoint(); 
    168         Assert.assertEquals(0, list.size()); 
    169         Assert.assertEquals(0, (int) queue.get("11")); 
    170         Assert.assertEquals(7, (int) queue.get("13")); 
    171         Assert.assertEquals(2, (int) queue.get("15")); 
    172         Assert.assertEquals(5, lunch.getPersonCount11()); 
    173         Assert.assertEquals(11, lunch.getPersonCount13()); 
    174         Assert.assertEquals(4, lunch.getPersonCount15()); 
    175     } 
    176      
    177     @Test 
    178     public void testCheckPointHavePeople() { 
    179         Map<String, Integer> queue = lunch.getQueue(); 
    180         queue.put("11", 50); 
    181         queue.put("13", 30); 
    182         queue.put("15", 10); 
    183         lunch.setQueue(queue);         
    184          
    185         lunch.checkpoint(); 
    186         Assert.assertEquals(10, list.size()); 
    187         Assert.assertEquals(47, (int) queue.get("11")); 
    188         Assert.assertEquals(28, (int) queue.get("13")); 
    189         Assert.assertEquals(14, (int) queue.get("15")); 
    190         Assert.assertEquals(0, lunch.getPersonCount11()); 
    191         Assert.assertEquals(0, lunch.getPersonCount13()); 
    192         Assert.assertEquals(5, lunch.getPersonCount15()); 
    193     } 
    194  
    195     @Test 
    196     public void testGetBalance() { 
    197         int balance = lunch.getPersonCount11() * Lunch.COST_ELEVEN 
    198                 + lunch.getPersonCount13() * Lunch.COST_THIRTEEN 
    199                 + lunch.getPersonCount15() * Lunch.COST_FIFTEEN; 
    200         Assert.assertEquals(balance, lunch.getBalance()); 
    201     } 
    202  
    203     @Test 
    204     public void testChoose() { 
    205  
    206         Map<String, Integer> queue = new HashMap<String, Integer>(); 
    207         queue.put("11", 33); 
    208         queue.put("13", 0); 
    209         queue.put("15", 0); 
    210  
    211  
    212         String people = "11"; 
    213         lunch.setQueue(queue); 
    214  
    215         Assert.assertTrue(lunch.choose11(people)); 
    216  
    217         queue = new HashMap<String, Integer>(); 
    218         queue.put("11", 49); 
    219         queue.put("13", 0); 
    220         queue.put("15", 0); 
    221         lunch.setQueue(queue); 
    222         Assert.assertTrue(lunch.choose11(people)); 
    223  
    224         queue = new HashMap<String, Integer>(); 
    225         queue.put("11", 50); 
    226         queue.put("13", 0); 
    227         queue.put("15", 0); 
    228         lunch.setQueue(queue); 
    229         Assert.assertFalse(lunch.choose11(people)); 
    230  
    231         people = "13"; 
    232         Assert.assertFalse(lunch.choose11(people)); 
    233     } 
    234  
    235     @Test 
    236     public void testChoose13() { 
    237         Map<String, Integer> queue = new HashMap<String, Integer>(); 
    238  
    239         String people = "13"; 
    240         queue = new HashMap<String, Integer>(); 
    241         queue.put("11", 0); 
    242         queue.put("13", 30); 
    243         queue.put("15", 0); 
    244         lunch.setQueue(queue); 
    245         Assert.assertFalse(lunch.choose13(people)); 
    246  
    247         queue = new HashMap<String, Integer>(); 
    248         queue.put("11", 0); 
    249         queue.put("13", 29); 
    250         queue.put("15", 0); 
    251         lunch.setQueue(queue); 
    252         Assert.assertTrue(lunch.choose13(people)); 
    253  
    254         people = "15"; 
    255         Assert.assertFalse(lunch.choose13(people)); 
    256  
    257     } 
    258  
    259     @Test 
    260     public void testChoose15() { 
    261  
    262         Map<String, Integer> queue = new HashMap<String, Integer>(); 
    263  
    264         String people = "15"; 
    265         queue = new HashMap<String, Integer>(); 
    266         queue.put("11", 0); 
    267         queue.put("13", 0); 
    268         queue.put("15", 15); 
    269         lunch.setQueue(queue); 
    270         Assert.assertFalse(lunch.choose15(people)); 
    271  
    272         queue = new HashMap<String, Integer>(); 
    273         queue.put("11", 0); 
    274         queue.put("13", 0); 
    275         queue.put("15", 14); 
    276         lunch.setQueue(queue); 
    277         Assert.assertTrue(lunch.choose15(people)); 
    278     } 
    279      
    280     @Test 
    281     public void testHandlePieceOfLunch(){ 
    282         Map<String, Integer> queue = lunch.getQueue(); 
    283         queue.put("11", 30); 
    284         queue.put("13", 20); 
    285         queue.put("15", 10); 
    286         lunch.setQueue(queue); 
    287          
    288         lunch.handlePieceOfLunch(); 
    289         Assert.assertEquals(27, (int) queue.get("11")); 
    290         Assert.assertEquals(18, (int) queue.get("13")); 
    291         Assert.assertEquals(9, (int) queue.get("15")); 
    292          
    293         queue = lunch.getQueue(); 
    294         queue.put("11", 0); 
    295         queue.put("13", 0); 
    296         queue.put("15", 0); 
    297         lunch.setQueue(queue); 
    298         lunch.handlePieceOfLunch(); 
    299         Assert.assertEquals(0, (int) queue.get("11")); 
    300         Assert.assertEquals(0, (int) queue.get("13")); 
    301         Assert.assertEquals(0, (int) queue.get("15")); 
    302          
    303         queue = lunch.getQueue(); 
    304         queue.put("11", 3); 
    305         queue.put("13", 2); 
    306         queue.put("15", 1); 
    307         lunch.setQueue(queue); 
    308         lunch.handlePieceOfLunch(); 
    309         Assert.assertEquals(0, (int) queue.get("11")); 
    310         Assert.assertEquals(0, (int) queue.get("13")); 
    311         Assert.assertEquals(0, (int) queue.get("15")); 
    312          
    313     } 
    314 } 
    315 }}} 
    316  
    317 源代码如下: 
    318 {{{ 
    319 package lunch; 
    320  
    321 import java.util.ArrayList; 
    322 import java.util.Collections; 
    323 import java.util.HashMap; 
    324 import java.util.LinkedList; 
    325 import java.util.List; 
    326 import java.util.Map; 
    327 import java.util.Queue; 
    328  
    329 /** 
    330  * 
    331  * @author pc 
    332  */ 
    333 public class Lunch { 
    334  
    335     public static final int WAIT_NUM_OF_11 = 50; 
    336     public static final int WAIT_NUM_OF_13 = 30; 
    337     public static final int WAIT_NUM_OF_15 = 15; 
    338      
    339     public static final int TOTAL_PEOPLE = 500; 
    340     public static final int TOTAL_PEOPLE_11 = 280; 
    341     public static final int TOTAL_PEOPLE_13 = 150; 
    342     public static final int TOTAL_PEOPLE_15 = 70; 
    343     public static final int COST_ELEVEN = 11; 
    344     public static final int COST_THIRTEEN = 13; 
    345     public static final int COST_FIFTEEN = 15; 
    346  
    347     public static final int NUM_OF_HANDLE_11 = 3; 
    348     public static final int NUM_OF_HANDLE_13 = 2; 
    349     public static final int NUM_OF_HANDLE_15 = 1; 
    350      
    351      
    352     private Map<String, Integer> queue = new HashMap<String, Integer>(); 
    353     private LinkedList<String> people = new LinkedList<String>(); 
    354      
    355     private int personCount11 = 0; 
    356     private int personCount13 = 0; 
    357     private int personCount15 = 0; 
    358  
    359     public Map<String, Integer> getQueue() { 
    360         return queue; 
    361     } 
    362  
    363     public void setQueue(Map<String, Integer> queue) { 
    364         this.queue = queue; 
    365     } 
    366  
    367     public LinkedList<String> getPeople() { 
    368         return people; 
    369     } 
    370  
    371     public int getPersonCount11() { 
    372         return personCount11; 
    373     } 
    374  
    375     public int getPersonCount13() { 
    376         return personCount13; 
    377     } 
    378  
    379     public int getPersonCount15() { 
    380         return personCount15; 
    381     } 
    382  
    383     /** 
    384      * 初始化进场顺序 
    385      */ 
    386     public void init() { 
    387         for (int i = 0; i < TOTAL_PEOPLE_11; i++) { 
    388             people.add("11"); 
    389         } 
    390         for (int i = 0; i < TOTAL_PEOPLE_13; i++) { 
    391             people.add("13"); 
    392         } 
    393         for (int i = 0; i < TOTAL_PEOPLE_15; i++) { 
    394             people.add("15"); 
    395         } 
    396  
    397         queue.put("11", 0); 
    398         queue.put("13", 0); 
    399         queue.put("15", 0); 
    400  
    401         Collections.shuffle(people); 
    402     } 
    403  
    404     public int getBalance() { 
    405         return COST_ELEVEN * personCount11 + COST_THIRTEEN * personCount13 
    406                 + COST_FIFTEEN * personCount15; 
    407     } 
    408  
    409     /** 
    410      * 每分钟时的处理方法 
    411      */ 
    412     public void checkpoint() { 
    413         for (int i = 0; i < 10; i++) { 
    414             String p = people.poll(); 
    415  
    416             // 判断队列人数 
    417             boolean result = choose11(p); 
    418             if (result) { 
    419                 queue.put("11", queue.get("11") + 1); 
    420                 personCount11++; 
    421             } else { 
    422                 result = choose13(p); 
    423                 if (result) { 
    424                     queue.put("13", queue.get("13") + 1); 
    425                     personCount13++; 
    426                 } else { 
    427                     if (choose15(p)) { 
    428                         queue.put("15", queue.get("15") + 1); 
    429                         personCount15++; 
    430                     } 
    431                 } 
    432             } 
    433         } 
    434  
    435         handlePieceOfLunch(); 
    436     } 
    437  
    438     public void handlePieceOfLunch() { 
    439         queue.put("11", queue.get("11") - 3 < 0 ? 0 : queue.get("11") - 3); 
    440         queue.put("13", queue.get("13") - 2 < 0 ? 0 : queue.get("13") - 2); 
    441         queue.put("15", queue.get("15") - 1 < 0 ? 0 : queue.get("15") - 1); 
    442     } 
    443  
    444     public boolean choose11(String p) { 
    445         return queue.get("11") < WAIT_NUM_OF_11 && "11".equals(p); 
    446     } 
    447  
    448     public boolean choose13(String p) { 
    449         return queue.get("13") < WAIT_NUM_OF_13 && ("11".equals(p) || "13".equals(p)); 
    450     } 
    451  
    452     public boolean choose15(String p) { 
    453         return queue.get("15") < WAIT_NUM_OF_15; 
    454     } 
    455  
    456     /** 
    457      * @param args the command line arguments 
    458      */ 
    459     public static void main(String[] args) { 
    460         // TODO code application logic here 
    461         int times = TOTAL_PEOPLE % 10 == 0 ? TOTAL_PEOPLE / 10 : TOTAL_PEOPLE / 10 + 1; 
    462         Lunch lunch = new Lunch(); 
    463         lunch.init(); 
    464         for (int i = 0; i < times; i++) { 
    465             lunch.checkpoint(); 
    466         } 
    467         int total = lunch.getBalance(); 
    468         System.out.println("total:" + total); 
    469         System.out.println("getPersonCount11:" + lunch.getPersonCount11()); 
    470         System.out.println("getPersonCount13:" + lunch.getPersonCount13()); 
    471         System.out.println("getPersonCount15:" + lunch.getPersonCount15()); 
    472     } 
    473 } 
    474  
    475 }}} 
    476  
    477 这次Code Kata活动,总体来说,还是比较顺利,中途也出现了一些,代码设计上的分歧意见,和代码设计中发现的问题,但小组成员都虚心倾听别人意见,认真思考 
    478 最后讨论统一编程思路,大家都受益良多。[[BR]] 
    479  
    480 比如说,开始活动之前,王安宁就发现题目本身存在一些设计不合理的地方,后来经过讨论修改,敲订题目。[[BR]] 
    481  
    482 秦鸿源在大家在陷入过度设计陷井的时候,站出来,提出分解问题,各个击破的方案,让大家思路更清晰,更有条理。[[BR]] 
    483  
    484 陈阳提出使用队列比使用数组更适合解决打饭排队问题,丁健勇与邝巨桓,在小组成编码过程中给过很多非常不错的经验与建议。[[BR]] 
    485  
    486 新入职的黄志强和李炳岳,也作为顾问全程参与了活动,虽然没有直接参与编码,但也从中学习到了不少知识。[[BR]] 
    487  
    488 活动之中还有很多精彩的细节... 
    489  
    490 欢迎其他组有兴趣的同事加入:)