【问题标题】:Algorithm for quantity selection [closed]数量选择算法[关闭]
【发布时间】:2015-12-07 13:36:15
【问题描述】:

我遇到了一个我想解决的特定问题。android 上的电子购物车应用程序的场景是这样的

在应用程序中,用户可以选择不同的数量。但问题是我的每个项目都有不同的数量度量不同的步进功能 增加数量。

那么我该如何简化呢。

任何人都可以建议我解决此类问题的好算法吗? 成为一个独立的解决方案

提前致谢。

【问题讨论】:

  • 你能告诉我你所说的“不同数量的衡量标准”是什么意思吗?
  • 公斤、克、束、块等
  • 算法称为加减法。我认为你真正锁定的是一个很好的数据结构。
  • 是的@IngoSchwarz,这就是我要找的......
  • 您对已经给出的答案满意还是我应该提出建议?

标签: android algorithm android-activity shopping-cart


【解决方案1】:

你应该有一个Product 类。每个产品对象都有不同的数量度量和增量方法。像这样的:

public abstract class Product {
    String quantityMeasure; //You can encapsulate this with a getter
    public abstract int step (int value); //here you want to increment "value" with different algorithms.
}

现在您可以像这样创建不同的产品子类:

public class FruitProduct extends Product {
    public abstract int step (int value) {
        //implementation here
    }

    public FruitProduct () {
        quantityMeasure = "grams";
    }
}

现在您可以使用Product 对象获取数量度量。

编辑:

您也可以将Product 键入这样的界面:

public interface Product {
    int step(int value);
    String getQuantityMeasure();
}

记得同时实现getQuantityMeasure 方法!

public String getQuantityMeasure() {
    return "grams";
}

【讨论】:

  • 我很欣赏这个答案。这是一个很好的选择,但在我的情况下,产品类型不固定,这使它成为另一个问题..
  • 那就把产品改成界面吧。
  • @omkomawar 查看我编辑的答案。我解释了如何使用 cYrixmorten 建议的接口。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-26
  • 1970-01-01
  • 2016-04-01
  • 2021-05-16
  • 2015-07-17
  • 2013-10-25
相关资源
最近更新 更多