Skip to content

Commit

Permalink
Merge pull request #10 from VladShyrokyi/update
Browse files Browse the repository at this point in the history
[REFACTORING] Code clean up
  • Loading branch information
VladShyrokyi authored Aug 14, 2021
2 parents 1135949 + 16506c1 commit 7f7a44c
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 98 deletions.
164 changes: 82 additions & 82 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ var deliveryConfiguration=TextBlockFactory.createTemplate(
"Delivery address: %[ADDRESS]%\n"+
"Delivery date: %[DATE]%",
Map.of(
"METHOD",TextBlockFactory.createText("courier"),
"ADDRESS",TextBlockFactory.createText("Khreschatyk St, 32, Kyiv, 02000"),
"DATE",TextBlockFactory.createText("08.04.2021")
"METHOD",TextBlockFactory.createText("courier"),
"ADDRESS",TextBlockFactory.createText("Khreschatyk St, 32, Kyiv, 02000"),
"DATE",TextBlockFactory.createText("08.04.2021")
)
);
var paymentConfiguration=TextBlockFactory.createTemplate(
);
var paymentConfiguration=TextBlockFactory.createTemplate(
"Payment method: %[METHOD]%",
Map.of("METHOD",TextBlockFactory.createText("cash"))
);
var orderConfiguration=TextBlockFactory.createTemplate(
);
var orderConfiguration=TextBlockFactory.createTemplate(
"Customer name: %[CUSTOMER_NAME]%\n"+
"Phone: %[PHONE]%\n"+
"Email address: %[EMAIL]%\n"+
Expand All @@ -59,15 +59,15 @@ var deliveryConfiguration=TextBlockFactory.createTemplate(
"Comment to order: %[COMMENT]%\n"+
"Order date: %[ORDER_DATE]%",
Map.of(
"CUSTOMER_NAME",TextBlockFactory.createText("Vladislav Shirokiy"),
"PHONE",TextBlockFactory.createText("+8888888888"),
"EMAIL",TextBlockFactory.createText("[email protected]"),
"DELIVERY_CONFIGURATION",deliveryConfiguration,
"PAYMENT_CONFIGURATION",paymentConfiguration,
"COMMENT",TextBlockFactory.createText("Deliver quickly!"),
"ORDER_DATE",TextBlockFactory.createText("04.08.2021")
"CUSTOMER_NAME",TextBlockFactory.createText("Vladislav Shirokiy"),
"PHONE",TextBlockFactory.createText("+8888888888"),
"EMAIL",TextBlockFactory.createText("[email protected]"),
"DELIVERY_CONFIGURATION",deliveryConfiguration,
"PAYMENT_CONFIGURATION",paymentConfiguration,
"COMMENT",TextBlockFactory.createText("Deliver quickly!"),
"ORDER_DATE",TextBlockFactory.createText("04.08.2021")
)
);
);

class Order {
final Integer number;
Expand All @@ -82,73 +82,73 @@ class Order {
this.price = price;
}
}
Function<Order, TextBlockContract> orderFactory = order ->
TextBlockFactory.createTemplate("%[NUMBER]%. %[ITEM]% %[COUNT]%x%[PRICE]% $",
Map.of(
"NUMBER", TextBlockFactory.createText(order.number.toString()),
"ITEM", TextBlockFactory.createText(order.item),
"COUNT", TextBlockFactory.createText(order.count.toString()),
"PRICE", TextBlockFactory.createText(order.price.toString())
)
);
var orders = List.of(
new Order(1, "Chair", 1, 25),
new Order(2, "Table", 1, 50),
new Order(3, "Wardrobe", 1, 45)
);
final int discount = 15;
final Integer shippingCost = 25;
var block = TextBlockFactory.createTemplate(
"Order №%[ORDER_NUMBER]%\n" +
"%[ORDER_CONFIGURATION]%\n" +
"-------\n" +
"%[ORDERS]%\n" +
"\n" +
"%[CHECK]%",
Map.of(
"ORDER_NUMBER", TextBlockFactory.createText("13"),
"ORDER_CONFIGURATION", orderConfiguration,
"ORDERS", TextBlockFactory.mergeTemplates("\n", orders.stream()
.map(orderFactory)
.collect(Collectors.toList())
),
"CHECK", TextBlockFactory.createTemplate(
"Discount: %[DISCOUNT]% $\n" +
"Shipping cost: %[SHIPPING_COST]% $\n" +
"Total amount: %[TOTAL_AMOUNT]% $",
Map.of(
"DISCOUNT", TextBlockFactory.createText(Integer.toString(discount)),
"SHIPPING_COST", TextBlockFactory.createText(shippingCost.toString()),
"TOTAL_AMOUNT", TextBlockFactory.createText(Integer.toString(
orders.stream().reduce(0,
(integer, order) -> integer
+ order.price * order.count,
Integer::sum
)
+ shippingCost - discount
))
)
)
)
);
String result = "Order №13\n" +
"Customer name: Vladislav Shirokiy\n" +
"Phone: +8888888888\n" +
"Email address: [email protected]\n" +
"Delivery method: courier\n" +
"Delivery address: Khreschatyk St, 32, Kyiv, 02000\n" +
"Delivery date: 08.04.2021\n" +
"Payment method: cash\n" +
"Comment to order: Deliver quickly!\n" +
"Order date: 04.08.2021\n" +
"-------\n" +
"1. Chair 1x25 $\n" +
"2. Table 1x50 $\n" +
"3. Wardrobe 1x45 $\n" +
"\n" +
"Discount: 15 $\n" +
"Shipping cost: 25 $\n" +
"Total amount: 130 $";
Function<Order, TextBlockContract> orderFactory = order -> TextBlockFactory.createTemplate(
"%[NUMBER]%. %[ITEM]% %[COUNT]%x%[PRICE]% $",
Map.of(
"NUMBER", TextBlockFactory.createText(order.number.toString()),
"ITEM", TextBlockFactory.createText(order.item),
"COUNT", TextBlockFactory.createText(order.count.toString()),
"PRICE", TextBlockFactory.createText(order.price.toString())
)
);
var orders = List.of(
new Order(1, "Chair", 1, 25),
new Order(2, "Table", 1, 50),
new Order(3, "Wardrobe", 1, 45)
);
final int discount = 15;
final Integer shippingCost = 25;
var block = TextBlockFactory.createTemplate(
"Order №%[ORDER_NUMBER]%\n" +
"%[ORDER_CONFIGURATION]%\n" +
"-------\n" +
"%[ORDERS]%\n" +
"\n" +
"%[CHECK]%",
Map.of(
"ORDER_NUMBER", TextBlockFactory.createText("13"),
"ORDER_CONFIGURATION", orderConfiguration,
"ORDERS", TextBlockFactory.mergeTemplates(
"\n",
orders.stream().map(orderFactory).collect(Collectors.toList())
),
"CHECK", TextBlockFactory.createTemplate(
"Discount: %[DISCOUNT]% $\n" +
"Shipping cost: %[SHIPPING_COST]% $\n" +
"Total amount: %[TOTAL_AMOUNT]% $",
Map.of(
"DISCOUNT", TextBlockFactory.createText(Integer.toString(discount)),
"SHIPPING_COST", TextBlockFactory.createText(shippingCost.toString()),
"TOTAL_AMOUNT", TextBlockFactory.createText(Integer.toString(
orders.stream().reduce(
0,
(integer, order) -> integer + order.price * order.count,
Integer::sum
)
+ shippingCost - discount
))
)
)
)
);
String result = "Order №13\n" +
"Customer name: Vladislav Shirokiy\n" +
"Phone: +8888888888\n" +
"Email address: [email protected]\n" +
"Delivery method: courier\n" +
"Delivery address: Khreschatyk St, 32, Kyiv, 02000\n" +
"Delivery date: 08.04.2021\n" +
"Payment method: cash\n" +
"Comment to order: Deliver quickly!\n" +
"Order date: 04.08.2021\n" +
"-------\n" +
"1. Chair 1x25 $\n" +
"2. Table 1x50 $\n" +
"3. Wardrobe 1x45 $\n" +
"\n" +
"Discount: 15 $\n" +
"Shipping cost: 25 $\n" +
"Total amount: 130 $";
Assertions.assertEquals(block.write(),result);
```

Expand Down
32 changes: 16 additions & 16 deletions src/test/java/example/TemplateExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ public Order(Integer number, String item, Integer count, Integer price) {
this.price = price;
}
}
Function<Order, TextBlockContract> orderFactory = order ->
TextBlockFactory.createTemplate("%[NUMBER]%. %[ITEM]% %[COUNT]%x%[PRICE]% $",
Map.of(
"NUMBER", TextBlockFactory.createText(order.number.toString()),
"ITEM", TextBlockFactory.createText(order.item),
"COUNT", TextBlockFactory.createText(order.count.toString()),
"PRICE", TextBlockFactory.createText(order.price.toString())
)
);
Function<Order, TextBlockContract> orderFactory = order -> TextBlockFactory.createTemplate(
"%[NUMBER]%. %[ITEM]% %[COUNT]%x%[PRICE]% $",
Map.of(
"NUMBER", TextBlockFactory.createText(order.number.toString()),
"ITEM", TextBlockFactory.createText(order.item),
"COUNT", TextBlockFactory.createText(order.count.toString()),
"PRICE", TextBlockFactory.createText(order.price.toString())
)
);
var orders = List.of(
new Order(1, "Chair", 1, 25),
new Order(2, "Table", 1, 50),
Expand All @@ -84,9 +84,9 @@ public Order(Integer number, String item, Integer count, Integer price) {
Map.of(
"ORDER_NUMBER", TextBlockFactory.createText("13"),
"ORDER_CONFIGURATION", orderConfiguration,
"ORDERS", TextBlockFactory.mergeTemplates("\n", orders.stream()
.map(orderFactory)
.collect(Collectors.toList())
"ORDERS", TextBlockFactory.mergeTemplates(
"\n",
orders.stream().map(orderFactory).collect(Collectors.toList())
),
"CHECK", TextBlockFactory.createTemplate(
"Discount: %[DISCOUNT]% $\n" +
Expand All @@ -96,10 +96,10 @@ public Order(Integer number, String item, Integer count, Integer price) {
"DISCOUNT", TextBlockFactory.createText(Integer.toString(discount)),
"SHIPPING_COST", TextBlockFactory.createText(shippingCost.toString()),
"TOTAL_AMOUNT", TextBlockFactory.createText(Integer.toString(
orders.stream().reduce(0,
(integer, order) -> integer
+ order.price * order.count,
Integer::sum
orders.stream().reduce(
0,
(integer, order) -> integer + order.price * order.count,
Integer::sum
)
+ shippingCost - discount
))
Expand Down

0 comments on commit 7f7a44c

Please sign in to comment.