Hi All,
E-Commerce Order Processing Engine | Java Consumer Functional Interface | Real-Time Java 8 Example
Today I have released new video on "E-Commerce Order Processing Engine | Java Consumer Functional Interface | Real-Time Java 8 Example"
This is frequently asked question in Java Interview.
Please watch full video, share, like and Subscribe Youtube channel and press bell icon. So you will get latest video notification.
|
package org.practice.basicprgms; public class OrderProcessingDemo { public static void main(String[] args) { Order order1 = new Order(1001, "John", 15000); Order order2 = new Order(1002, "Sara", 3000); OrderProcessingService.applyDiscount .andThen(OrderProcessingService.calculateTax) .andThen(OrderProcessingService.finalAmount) .andThen(OrderProcessingService.rewardPoints) .andThen(OrderProcessingService.status) .accept(order1);
OrderProcessingService.applyDiscount .andThen(OrderProcessingService.calculateTax) .andThen(OrderProcessingService.finalAmount) .andThen(OrderProcessingService.rewardPoints) .andThen(OrderProcessingService.status) .accept(order2);
System.out.println("----------- ORDER SUMMARY -----------"); System.out.println("Order Id : " + order1.getOrderId()); System.out.println("Customer : " + order1.getCustomerName()); System.out.println("Order Amount : ₹" + order1.getOrderAmount()); System.out.println("Discount : ₹" + order1.getDiscount()); System.out.println("GST (18%) : ₹" + order1.getTax()); System.out.println("Final Amount : ₹" + order1.getFinalAmount()); System.out.println("Reward Points : " + order1.getRewardPoints()); System.out.println("Status : " + order1.getStatus());
System.out.println("\n\nOrder Id : " + order2.getOrderId()); System.out.println("Customer : " + order2.getCustomerName()); System.out.println("Order Amount : ₹" + order2.getOrderAmount()); System.out.println("Discount : ₹" + order2.getDiscount()); System.out.println("GST (18%) : ₹" + order2.getTax()); System.out.println("Final Amount : ₹" + order2.getFinalAmount()); System.out.println("Reward Points : " + order2.getRewardPoints()); System.out.println("Status : " + order2.getStatus()); } } package org.practice.basicprgms; import java.util.function.Consumer; public class OrderProcessingService { // 1. Apply Discount public static Consumer<Order> applyDiscount = order -> { if(order.getOrderAmount() >= 10000) order.setDiscount(order.getOrderAmount() * 0.10); else order.setDiscount(order.getOrderAmount() * 0.5); };
// 2. Calculate Tax/GST public static Consumer<Order> calculateTax = order -> { double taxableAmount = order.getOrderAmount() - order.getDiscount(); order.setTax(taxableAmount * 0.18); };
// 3. Calculate Final Amount public static Consumer<Order> finalAmount = order -> { double finalAmount = order.getOrderAmount() - order.getDiscount() + order.getTax(); order.setFinalAmount(finalAmount); };
// 4. Calculate Rewards Points public static Consumer<Order> rewardPoints = order -> { int rewardsPoints = (int)order.getFinalAmount() / 100; order.setRewardPoints(rewardsPoints); };
// 5. Find OrderStatus public static Consumer<Order> status = order -> { order.setStatus("Order Confirmed"); }; } package org.practice.basicprgms; public class Order { private int orderId; private String customerName; private double orderAmount; private double discount; private double tax; private double finalAmount; private int rewardPoints; private String status;
public Order() { super(); } public Order(int orderId, String customerName, double orderAmount) { super(); this.orderId = orderId; this.customerName = customerName; this.orderAmount = orderAmount; } public int getOrderId() { return orderId; } public void setOrderId(int orderId) { this.orderId = orderId; } public String getCustomerName() { return customerName; } public void setCustomerName(String customerName) { this.customerName = customerName; } public double getOrderAmount() { return orderAmount; } public void setOrderAmount(double orderAmount) { this.orderAmount = orderAmount; } public double getDiscount() { return discount; } public void setDiscount(double discount) { this.discount = discount; } public double getTax() { return tax; } public void setTax(double tax) { this.tax = tax; } public double getFinalAmount() { return finalAmount; } public void setFinalAmount(double finalAmount) { this.finalAmount = finalAmount; } public int getRewardPoints() { return rewardPoints; } public void setRewardPoints(int rewardPoints) { this.rewardPoints = rewardPoints; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; }
} |
0 Comments