-3

Fallowing code explain the loose coupling concept. I want to implement the main method to added items (with price and quantity) and calculate the total price with sales tax. How I implement the main method.

class ShopingCartEntry {

    public float price;
    public int quantity;

    public float getTotalPrice() {
        return price * quantity;
    }
}

class ShopingCartContents {

    public ShopingCartEntry[] items;

    public float getTotalPrice() {
        float totalPrice = 0;
        for (ShopingCartEntry item : items) {
            totalPrice += item.getTotalPrice();
        }
        return totalPrice;
    }
}

class Order {

    private ShopingCartContents cart;
    private float salesTax;

    public Order(ShopingCartContents cart, float salesTax) {
        this.cart = cart;
        this.salesTax = salesTax;
    }

    public float orderTotalPrice() {
        return cart.getTotalPrice() * (1.0f + salesTax);
    }
}

public class LooseCoupling {

    public static void main(String[] args) {

    }
}
  • *I want to implement the main method to added items (with price and quantity) and calculate the total price with sales tax* that's not a requirement – Tim May 17 '17 at 08:53
  • You may want to ask how does the loose coupling concept works in this business scenario and the data models. Try to understand the data modeling here. – Shafin Mahmud May 17 '17 at 08:56
  • f**o**llowing, you surely mean following, not fallowing. – Holger May 17 '17 at 09:11

1 Answers1

0

Firstly I don't think your code is complete.

Secondly below is a pseudo code for your implementation

class ShopingCartEntry {

    public float price;
    public int quantity;

    public float getTotalPrice() {
        return price * quantity;
    }


    // either add a constructor or getter / setter to initialize price & quantity
}

class ShopingCartContents {

    public ShopingCartEntry[] items; // instead of an array you should have a parameterized constructor 

    public float getTotalPrice() { // pass a list of ShopingCartEntry
        float totalPrice = 0;
        for (ShopingCartEntry item : items) {
            totalPrice += item.getTotalPrice();
        }
        return totalPrice;
    }



}

class Order {

    private ShopingCartContents cart; 
    private float salesTax;

    // add a List<ShopingCartEntry> object

    public Order(ShopingCartContents cart, float salesTax) {
        this.cart = cart;
        this.salesTax = salesTax;
        // add the object to the orderList - new list
    }

    public float orderTotalPrice() {
        return cart.getTotalPrice() * (1.0f + salesTax); // pass the list as a parameter to cart.getTotalPrice()
    }

    // add a mtheod to add contents / objects to the list - name addContents to the list
}

public class LooseCoupling {

    public static void main(String[] args) {
        /* 1. create an object of order class - passing the item and price
         2. run a loop to add object to the order list method - addContents
         3. finally you should call oreder.orderTotalPrice() */

         // secondly add another method to remove objects from list

    }
}
Saurabh Jhunjhunwala
  • 2,832
  • 3
  • 29
  • 57
  • Did you copy the entire code, just to insert a comment containing your actual answer? – Holger May 17 '17 at 09:23
  • As I wrote this a pseudo code, the reason I wrote comments & pseudo, is because I dont have test data nor do I have the exact requirement. My main motto was to suggest a sequence which the user can use to finish his / her work. – Saurabh Jhunjhunwala May 17 '17 at 09:29
  • There is no problem with writing pseudo code, the question is why you copied the OP’s entire code, just to hide your pseudo code somewhere within it as a comment. Why not writing a list of three items just as a list of three items? – Holger May 17 '17 at 09:34