| 19 | | |
| | 19 | 我们定义了结帐接口 |
| | 20 | {{{ |
| | 21 | package cn.pconline.harrypotter; |
| | 22 | |
| | 23 | import java.util.List; |
| | 24 | |
| | 25 | public interface PriceCalculator { |
| | 26 | |
| | 27 | public int calculate(List<Book> list); |
| | 28 | } |
| | 29 | |
| | 30 | }}} |
| | 31 | [[BR]] |
| | 32 | 单集购买5本,优惠7元。方案2 |
| | 33 | [[BR]] |
| | 34 | 测试代码 |
| | 35 | {{{ |
| | 36 | package cn.pconline.harrypotter; |
| | 37 | |
| | 38 | import java.util.ArrayList; |
| | 39 | import java.util.List; |
| | 40 | |
| | 41 | import junit.framework.Assert; |
| | 42 | |
| | 43 | import org.junit.Test; |
| | 44 | |
| | 45 | /** |
| | 46 | * 测试"单集购买5本,优惠7元" 计价优惠方式 |
| | 47 | * @author pc |
| | 48 | * |
| | 49 | */ |
| | 50 | public class DiscountCashierTest { |
| | 51 | |
| | 52 | |
| | 53 | DiscountCashier dc = new DiscountCashier(); |
| | 54 | |
| | 55 | @Test |
| | 56 | public void testCalculate() { |
| | 57 | |
| | 58 | //单集不够5本,总数够5本 |
| | 59 | List<Book> list = new ArrayList<Book>(); |
| | 60 | Book book1 = new Book(BookType.one); |
| | 61 | Book book2 = new Book(BookType.two); |
| | 62 | Book book3 = new Book(BookType.three); |
| | 63 | Book book4 = new Book(BookType.four); |
| | 64 | Book book5 = new Book(BookType.five); |
| | 65 | |
| | 66 | list.add(book1); |
| | 67 | list.add(book2); |
| | 68 | list.add(book3); |
| | 69 | list.add(book4); |
| | 70 | list.add(book5); |
| | 71 | |
| | 72 | int price = dc.calculate(list); |
| | 73 | |
| | 74 | Assert.assertEquals(5 * 800, price); |
| | 75 | |
| | 76 | list = new ArrayList<Book>(); |
| | 77 | |
| | 78 | //单集够5本,第一集够5本 |
| | 79 | list.add(book1); |
| | 80 | list.add(book1); |
| | 81 | list.add(book1); |
| | 82 | list.add(book1); |
| | 83 | |
| | 84 | list.add(book1); |
| | 85 | list.add(book2); |
| | 86 | price = dc.calculate(list); |
| | 87 | |
| | 88 | Assert.assertEquals(6 * 800 - 700, price); |
| | 89 | |
| | 90 | //多集够5本,第二集够5本 |
| | 91 | list.add(book2); |
| | 92 | list.add(book2); |
| | 93 | list.add(book2); |
| | 94 | list.add(book2); |
| | 95 | |
| | 96 | price = dc.calculate(list); |
| | 97 | |
| | 98 | Assert.assertEquals((10 * 800) - (2 * 700), |
| | 99 | price); |
| | 100 | |
| | 101 | // 第一集有10本,第二集5本 |
| | 102 | list.add(book1); |
| | 103 | list.add(book1); |
| | 104 | list.add(book1); |
| | 105 | list.add(book1); |
| | 106 | list.add(book1); |
| | 107 | |
| | 108 | price = dc.calculate(list); |
| | 109 | |
| | 110 | Assert.assertEquals((15 * 800) - (3 * 700), |
| | 111 | price); |
| | 112 | } |
| | 113 | } |
| | 114 | |
| | 115 | }}} |
| | 116 | 实现代码 |
| | 117 | {{{ |
| | 118 | package cn.pconline.harrypotter; |
| | 119 | |
| | 120 | import java.util.List; |
| | 121 | |
| | 122 | public class DiscountCashier implements PriceCalculator { |
| | 123 | |
| | 124 | @Override |
| | 125 | public int calculate(List<Book> list) { |
| | 126 | //满足优惠的次数 |
| | 127 | int count = 0; |
| | 128 | if(list == null){ |
| | 129 | return 0; |
| | 130 | }else{ |
| | 131 | if(list.size() >= 5){ |
| | 132 | for(BookType type : BookType.values()){ |
| | 133 | int num = |
| | 134 | BookStore.getBookNumByType(type, list); |
| | 135 | if( num >= 5){ |
| | 136 | count += num / 5; |
| | 137 | }; |
| | 138 | } |
| | 139 | return (800 * list.size()) - (count * 700); |
| | 140 | }else{ |
| | 141 | return 800 * list.size(); |
| | 142 | } |
| | 143 | } |
| | 144 | } |
| | 145 | } |
| | 146 | |
| | 147 | }}} |
| | 148 | 送书活动,买7本送一本 |
| | 149 | [[BR]] |
| | 150 | 测试代码 |
| | 151 | {{{ |
| | 152 | package cn.pconline.harrypotter; |
| | 153 | |
| | 154 | import java.util.ArrayList; |
| | 155 | import java.util.List; |
| | 156 | |
| | 157 | import junit.framework.Assert; |
| | 158 | |
| | 159 | import org.junit.Test; |
| | 160 | |
| | 161 | public class SendBookActivityTest { |
| | 162 | SendBookActivity dc = new SendBookActivity(); |
| | 163 | @Test |
| | 164 | public void testCalulate() throws Exception { |
| | 165 | //少于7本的情况 |
| | 166 | List<Book> list = new ArrayList<Book>(); |
| | 167 | Book book1 = new Book(BookType.one); |
| | 168 | Book book2 = new Book(BookType.two); |
| | 169 | list.add(book1); |
| | 170 | list.add(book1); |
| | 171 | list.add(book1); |
| | 172 | list.add(book1); |
| | 173 | list.add(book1); |
| | 174 | list.add(book2); |
| | 175 | |
| | 176 | int price = dc.calculate(list); |
| | 177 | Assert.assertEquals(6 * 800, price); |
| | 178 | |
| | 179 | //等于7本的情况 |
| | 180 | list.add(book2); |
| | 181 | price = dc.calculate(list); |
| | 182 | Assert.assertEquals((7-1) * 800, price); |
| | 183 | |
| | 184 | //等于14本 |
| | 185 | for(int i = 0 ; i < 7; i ++){ |
| | 186 | list.add(book1); |
| | 187 | } |
| | 188 | |
| | 189 | price = dc.calculate(list); |
| | 190 | |
| | 191 | Assert.assertEquals((14 - 2) * 800,price); |
| | 192 | |
| | 193 | } |
| | 194 | } |
| | 195 | |
| | 196 | }}} |
| | 197 | 实现代码 |
| | 198 | {{{ |
| | 199 | package cn.pconline.harrypotter; |
| | 200 | |
| | 201 | import java.util.List; |
| | 202 | |
| | 203 | /** |
| | 204 | * 送书活动,买7本送一本 |
| | 205 | * @author pc |
| | 206 | * |
| | 207 | */ |
| | 208 | public class SendBookActivity implements PriceCalculator{ |
| | 209 | public int calculate(List<Book> list){ |
| | 210 | |
| | 211 | int saveTimes = 0; //优惠的次数 |
| | 212 | |
| | 213 | if(list == null ){ |
| | 214 | return 0; |
| | 215 | } |
| | 216 | saveTimes += list.size()/7; |
| | 217 | return (list.size()-saveTimes) * 800; |
| | 218 | } |
| | 219 | } |
| | 220 | |
| | 221 | }}} |
| | 222 | 买100减10(即90) |
| | 223 | [[BR]] |
| | 224 | 测试代码 |
| | 225 | {{{ |
| | 226 | package cn.pconline.harrypotter; |
| | 227 | |
| | 228 | import java.util.ArrayList; |
| | 229 | import java.util.List; |
| | 230 | |
| | 231 | import org.junit.Assert; |
| | 232 | import org.junit.Test; |
| | 233 | |
| | 234 | public class SendMoneyActivityTest { |
| | 235 | |
| | 236 | @Test |
| | 237 | public void testCalculate() { |
| | 238 | Book book = new Book(BookType.one); |
| | 239 | List<Book> books = new ArrayList<Book>(); |
| | 240 | PriceCalculator pc = new SendMoneyActivity(); |
| | 241 | // 不足100块 |
| | 242 | books.add(book); |
| | 243 | books.add(book); |
| | 244 | int price = pc.calculate(books); |
| | 245 | Assert.assertEquals( 800 * 2 , price); |
| | 246 | |
| | 247 | // 大于100块 |
| | 248 | for(int i = 0 ;i < 11 ;i++){ |
| | 249 | books.add(book); |
| | 250 | } |
| | 251 | price = pc.calculate(books); |
| | 252 | Assert.assertEquals( 800 * 13 - 1000 , price); |
| | 253 | |
| | 254 | // 200块以上 |
| | 255 | for(int j = 0; j < 13; j++){ |
| | 256 | books.add(book); |
| | 257 | } |
| | 258 | price = pc.calculate(books); |
| | 259 | Assert.assertEquals((800 * 26) - (20 * 100), price); |
| | 260 | } |
| | 261 | } |
| | 262 | }}} |
| | 263 | 实现代码 |
| | 264 | {{{ |
| | 265 | package cn.pconline.harrypotter; |
| | 266 | |
| | 267 | import java.util.List; |
| | 268 | |
| | 269 | /** |
| | 270 | * @author pc |
| | 271 | * 买100减10(即90) |
| | 272 | */ |
| | 273 | public class SendMoneyActivity implements PriceCalculator { |
| | 274 | |
| | 275 | @Override |
| | 276 | public int calculate(List<Book> list) { |
| | 277 | int price = 0; |
| | 278 | //List为空(没有书) |
| | 279 | if(list == null || list.isEmpty()){ |
| | 280 | return price; |
| | 281 | } |
| | 282 | |
| | 283 | //List不为空(有书) |
| | 284 | int sumOfPrice = list.size() * 800; |
| | 285 | price = sumOfPrice - (sumOfPrice / 10000) * 1000; |
| | 286 | return price; |
| | 287 | } |
| | 288 | } |
| | 289 | }}} |