-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from VladShyrokyi/update
[REFACTORING] Code clean up
- Loading branch information
Showing
2 changed files
with
98 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"+ | ||
|
@@ -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; | ||
|
@@ -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); | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters