Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Java (https://www.askmehelpdesk.com/forumdisplay.php?f=440)
-   -   How can I calculate Grand total using java polymorphism? (https://www.askmehelpdesk.com/showthread.php?t=824110)

  • Apr 25, 2016, 04:17 AM
    mnhmasum
    How can I calculate Grand total using java polymorphism?
    package javacleancode;


    class GrandTotal{
    int totalAmount;
    int discount;
    int deliveryCharge;


    void calculateAmount() {
    System.out.println("Total " + totalAmount);


    }
    }


    class TotalWithDiscount extends GrandTotal{
    void calculateAmount() {
    System.out.println("Sub Total " + ((totalAmount - discount)));
    }
    }


    class TotalWithDiscountAndDeliveryCharge extends GrandTotal{
    void calculateAmount() {
    System.out.println("Grand Total " + ((totalAmount - discount) + deliveryCharge));
    }
    }






    public class JavaCleanCode {

    public static void main(String[] args) {

    /When no discount and delivery charge available
    GrandTotal grandTotal = new GrandTotal();
    grandTotal.totalAmount = 450;
    grandTotal.calculateAmount();

    //When only discount available
    GrandTotal totalWithDiscount = new TotalWithDiscount();
    totalWithDiscount.discount = 10;
    totalWithDiscount.totalAmount = 25;
    totalWithDiscount.calculateAmount();
  • Apr 25, 2016, 04:19 AM
    mnhmasum
    Am I going right to calculate grand total using java polymorphism?
    I am developing an e-commerce application where need to calculate grand total by total price, discount and delivery charge. I want to calculate grand total using polymorphism. Am I going right?


    package javacleancode;


    class GrandTotal{
    int totalAmount;
    int discount;
    int deliveryCharge;


    void calculateAmount() {
    System.out.println("Total " + totalAmount);


    }
    }


    class TotalWithDiscount extends GrandTotal{
    void calculateAmount() {
    System.out.println("Sub Total " + ((totalAmount - discount)));
    }
    }


    class TotalWithDiscountAndDeliveryCharge extends GrandTotal{
    void calculateAmount() {
    System.out.println("Grand Total " + ((totalAmount - discount) + deliveryCharge));
    }
    }






    public class JavaCleanCode {

    public static void main(String[] args) {

    /When no discount and delivery charge available
    GrandTotal grandTotal = new GrandTotal();
    grandTotal.totalAmount = 450;
    grandTotal.calculateAmount();

    //When only discount available
    GrandTotal totalWithDiscount = new TotalWithDiscount();
    totalWithDiscount.discount = 10;
    totalWithDiscount.totalAmount = 25;
    totalWithDiscount.calculateAmount();

    //When discount and delivery charge available
    GrandTotal totalWithDiscountAndDeliveryCharge = new TotalWithDiscountAndDeliveryCharge();
    totalWithDiscountAndDeliveryCharge.deliveryCharge = 100;
    totalWithDiscountAndDeliveryCharge.discount = 10;
    totalWithDiscountAndDeliveryCharge.totalAmount = 500;
    totalWithDiscountAndDeliveryCharge.calculateAmount ();


    }

    }
  • Apr 25, 2016, 07:22 AM
    CravenMorhead
    Yes. That looks right to me.

    What is the scope of your assignment?

  • All times are GMT -7. The time now is 09:06 AM.