From 16506c1e6020fbf5d91fb9bc828a0ba1eb0a166e Mon Sep 17 00:00:00 2001 From: Shyrokyi Vladislav Date: Sat, 14 Aug 2021 22:57:11 +0300 Subject: [PATCH] [REFACTORING] Code clean up --- readme.md | 164 ++++++++++----------- src/test/java/example/TemplateExample.java | 32 ++-- 2 files changed, 98 insertions(+), 98 deletions(-) diff --git a/readme.md b/readme.md index cceb189..cd99c82 100644 --- a/readme.md +++ b/readme.md @@ -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("vlad16062001@gmail.com"), - "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("vlad16062001@gmail.com"), + "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 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: vlad16062001@gmail.com\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 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: vlad16062001@gmail.com\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); ``` diff --git a/src/test/java/example/TemplateExample.java b/src/test/java/example/TemplateExample.java index 247a1ab..41802a4 100644 --- a/src/test/java/example/TemplateExample.java +++ b/src/test/java/example/TemplateExample.java @@ -58,15 +58,15 @@ public Order(Integer number, String item, Integer count, Integer price) { this.price = price; } } - Function 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 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), @@ -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" + @@ -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 ))