我正在为一家服装店编写一个小项目。在我的课程之一中,每次用户选择某种产品时,都会传递2个变量(item和price)。
它们将被传递为2种不同的方法。
一个加上物品,一个加上价格。
我正在尝试采用第一个阵列,其中包含所有已添加到购物车中的衣服,而另一个则包含每个项目的所有价格
每个项目及其价格都有相似的指标,因为它们是同时添加的...
所以现在...我的问题是,我确实希望将这两个ArrayList都传递到我的方法“ PrintRecipt() ”中,这样我就可以打印价格在其前面的商品了。
码:-
package clothesshop;
import java.util.ArrayList;
public class Cart {
void cartAddi(String item){//adding a certain item will be passed here
ArrayList<String> clothes = new ArrayList<String>(5);
clothes.add(item);
}
int cartAddp(int price){//along with its price to be added to the cart
ArrayList<Integer> cost = new ArrayList<Integer>(5);
cost.add(price); int TotalP=0;
for (int total : cost) {
TotalP = TotalP + total; //total price
}
return TotalP;
}
void PrintRecipt(){
//taking both ArrayLists and printing them here?
}
}
package clothesshop;
import java.util.ArrayList;
public class Cart {
private ArrayList<String> clothes = new ArrayList<String>(5);
private ArrayList<Integer> costs = new ArrayList<Integer>(5);
int cartAdd(String item, int price){//adding a certain item will be passed here
clothes.add(item);
costs.add(price);
int TotalP=0;
for (int total : costs) {
TotalP = TotalP + total; //total price
}
return TotalP;
}
void PrintRecipt() {
for (int i=0; i<clothes.length; i++) {
System.out.println(clothes.get(i) + " at " + costs.get(i));
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句