Ask Experts Questions for FREE Help !
Ask
    mnhmasum's Avatar
    mnhmasum Posts: 2, Reputation: 1
    New Member
     
    #1

    Apr 25, 2016, 04:17 AM
    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();
    mnhmasum's Avatar
    mnhmasum Posts: 2, Reputation: 1
    New Member
     
    #2

    Apr 25, 2016, 04:19 AM
    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 ();


    }

    }
    CravenMorhead's Avatar
    CravenMorhead Posts: 4,532, Reputation: 1065
    Adult Sexuality Expert
     
    #3

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

    What is the scope of your assignment?

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Am I going right to calculate grand total using java polymorphism? [ 0 Answers ]

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...

How to demonstarte the idea of Polymorphism and inheritance in Java? [ 0 Answers ]

Listen, I really need help with my assignment I've been working on it for hours and I'm just stuck! I don't think I understood the question fully and what's the ms is actually expecting me to output for her. I created the super class along with it's 2 sub classes and the main class for testing...

How to calculate total liabilities with only average total assets and average stockh? [ 1 Answers ]

How to calculate total liabilities with only average total assets ($200,00) and average stockholders' equity ($160,000)?


View more questions Search