Careeroza
Real World Example
Checkout Price Calculation
Every e-commerce checkout (Myntra, Meesho, Flipkart) combines arithmetic, comparison and logical operators: subtotal, discount, delivery charge and GST.
Example
public class Main {
public static void main(String[] args) {
double shirtPrice = 799;
int shirtQty = 2;
double jeansPrice = 1499;
int jeansQty = 1;
boolean isFirstOrder = true;
double subtotal = shirtPrice * shirtQty + jeansPrice * jeansQty;
double discount = (isFirstOrder && subtotal >= 2000) ? subtotal * 0.10 : 0;
double afterDiscount = subtotal - discount;
double delivery = afterDiscount >= 499 ? 0 : 40;
double gst = afterDiscount * 0.05;
double total = afterDiscount + delivery + gst;
System.out.println("Subtotal : " + subtotal);
System.out.println("Discount : -" + discount);
System.out.println("Delivery : " + delivery);
System.out.printf("GST (5%%) : %.2f%n", gst);
System.out.printf("To pay : %.2f%n", total);
}
}Output
Subtotal : 3097.0 Discount : -309.70000000000005 Delivery : 0.0 GST (5%) : 139.37 To pay : 2926.67