Changes between Initial Version and Version 1 of codekata1


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

--

Legend:

Unmodified
Added
Removed
Modified
  • codekata1

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