Skip to content

Commit 6ceade6

Browse files
committed
gai
1 parent 2f9a785 commit 6ceade6

4 files changed

Lines changed: 119 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.wbs.java8lambda.chapter11;
2+
3+
/**
4+
* TODO
5+
*
6+
* @author: wangbingshuai
7+
* @create: 2019-11-11 20:25
8+
**/
9+
public class Discount {
10+
public enum Code {
11+
NONE(0), SILVER(5), GOLD(10), PLATINUM(25), DIAMOND(20);
12+
private final int percentage;
13+
14+
Code(int percentage) {
15+
this.percentage = percentage;
16+
}
17+
}
18+
19+
private static double apply(double price, Code code) {
20+
Util.delay();
21+
return Util.format(price * (100 - code.percentage) / 100);
22+
}
23+
24+
public static String applyDiscount(Quote quote) {
25+
return quote.getShopName() + " price is " + Discount.apply(quote.getPrice(), quote.getDiscountCode());
26+
}
27+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.wbs.java8lambda.chapter11;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
6+
/**
7+
* TODO
8+
*
9+
* @author: wangbingshuai
10+
* @create: 2019-11-11 20:29
11+
**/
12+
@Getter
13+
@AllArgsConstructor
14+
public class Quote {
15+
private final String shopName;
16+
private final double price;
17+
private final Discount.Code discountCode;
18+
19+
public static Quote parse(String s) {
20+
String[] split = s.split(":");
21+
String shopName = split[0];
22+
double price = Double.parseDouble(split[1]);
23+
Discount.Code discountCode = Discount.Code.valueOf(split[2]);
24+
return new Quote(shopName, price, discountCode);
25+
}
26+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.wbs.java8lambda.chapter11;
2+
3+
import java.util.Random;
4+
5+
/**
6+
* TODO
7+
*
8+
* @author: wangbingshuai
9+
* @create: 2019-11-11 20:04
10+
**/
11+
public class Shop {
12+
private final String name;
13+
private final Random random;
14+
15+
public Shop(String name) {
16+
this.name = name;
17+
random = new Random(name.charAt(0) * name.charAt(1) * name.charAt(2));
18+
}
19+
20+
public String getPrice(String product) {
21+
double price = calculatePrice(product);
22+
Discount.Code code = Discount.Code.values()[random.nextInt(Discount.Code.values().length)];
23+
return name + ":" + price + ":" + code;
24+
}
25+
26+
public double calculatePrice(String product) {
27+
Util.delay();
28+
return Util.format(random.nextDouble() * product.charAt(0) + product.charAt(1));
29+
}
30+
31+
public String getName() {
32+
return name;
33+
}
34+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.wbs.java8lambda.chapter11;
2+
3+
import java.text.DecimalFormat;
4+
import java.text.DecimalFormatSymbols;
5+
import java.util.Locale;
6+
7+
/**
8+
* TODO
9+
*
10+
* @author: wangbingshuai
11+
* @create: 2019-11-11 20:04
12+
**/
13+
public class Util {
14+
15+
private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#.##", new DecimalFormatSymbols(Locale.US));
16+
17+
public static void delay() {
18+
int delay = 1000;
19+
try {
20+
Thread.sleep(delay);
21+
} catch (InterruptedException e) {
22+
throw new RuntimeException(e);
23+
}
24+
}
25+
26+
public static double format(double number) {
27+
synchronized (DECIMAL_FORMAT) {
28+
return new Double(DECIMAL_FORMAT.format(number));
29+
}
30+
}
31+
32+
}

0 commit comments

Comments
 (0)