数量选择算法
Algorithm for quantity selection
我遇到了一个我想要解决的特定问题 solve.The android [=11] 上的 电子购物车应用程序的情况是这样的=]
在应用程序中,用户可以选择不同的数量。但问题是我的每个项目都有不同的数量度量并且不同的步进函数增加数量。
那么我该如何简化呢。
Can any one suggest me a good algorithm to solve such problem or may
be an independent solution
提前致谢。
你应该有一个 Product
class。每个产品对象都有不同的数量度量和增量方法。像这样:
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.
}
现在您可以像这样创建不同的产品子class:
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";
}
我遇到了一个我想要解决的特定问题 solve.The android [=11] 上的 电子购物车应用程序的情况是这样的=]
在应用程序中,用户可以选择不同的数量。但问题是我的每个项目都有不同的数量度量并且不同的步进函数增加数量。 那么我该如何简化呢。 Can any one suggest me a good algorithm to solve such problem or may
be an independent solution 提前致谢。
你应该有一个 Product
class。每个产品对象都有不同的数量度量和增量方法。像这样:
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.
}
现在您可以像这样创建不同的产品子class:
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";
}