【问题标题】:How to implement main method fallowing code based on loose Coupling [closed]如何基于松散耦合实现主方法闲置代码[关闭]
【发布时间】:2017-10-16 15:04:20
【问题描述】:

闲散的代码解释了松散耦合的概念。我想实现添加项目的主要方法(价格和数量)并计算总价和销售税。我如何实现主要方法。

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) {

    }
}

【问题讨论】:

  • 我想实现添加项目的主要方法(价格和数量)并计算总价和销售税这不是要求
  • 您可能想问松耦合概念在这个业务场景和数据模型中是如何工作的。尝试了解这里的数据建模。
  • fo放纵,你的意思肯定是跟随,而不是放纵。

标签: java loose-coupling


【解决方案1】:

首先,我认为您的代码不完整。

其次是你的实现的伪代码

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

    }
}

【讨论】:

  • 您是否复制了整个代码,只是为了插入包含您实际答案的评论?
  • 因为我写了这个伪代码,我写 cmets & pseudo 的原因是因为我没有测试数据,也没有确切的要求。我的主要座右铭是建议用户可以用来完成他/她的工作的序列。
  • 写伪代码没有问题,问题是为什么你复制了OP的整个代码,只是为了将你的伪代码隐藏在其中作为注释。为什么不把三项的列表写成三项的列表呢?
猜你喜欢
  • 2021-09-09
  • 1970-01-01
  • 2013-07-29
  • 2010-10-22
  • 2014-03-15
  • 2013-10-11
  • 1970-01-01
  • 2017-06-05
  • 1970-01-01
相关资源
最近更新 更多