将ArrayList从一个方法传递到同一类中的另一个方法?

姆萨拉蒂

我正在为一家服装店编写一个小项目。在我的课程之一中,每次用户选择某种产品时,都会传递2个变量(itemprice)。

它们将被传递为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?
   }  
}
v
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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将变量从一个方法传递到同一个类中的另一个方法?

将last_inserted id从一个函数传递到codeigniter中同一类中的另一个函数

将方法的返回值作为参数传递给同一类中的另一个方法

将一个方法中的变量访问到同一类中的另一个方法

将图像传递给同一类中的另一个函数

在python的同一类中的另一个方法中调用一个类的方法

从同一类中的另一个方法调用方法时出错

测试使用同一类中另一个方法的方法

使用 getattr 从同一类中的另一个方法调用方法

无法从 JAVA 中同一类的另一个方法调用方法

从同一类的静态方法访问另一个方法中的变量

无法在同一类JS的另一个方法中调用方法

如何通过Java中的方法将对象从一个类传递到另一个类

在Java中将ArrayList从一个类传递到另一个类

将变量从一个Swift类传递到另一个的最佳方法?

如何将参数从一个函数传递到另一个函数,但在php中的同一个类中

无法从同一类中的另一个方法调用一个方法

在Python的同一类中从另一个方法调用一个方法

Mockito-验证一个方法是否在同一类中调用另一个方法

单元测试一个方法调用同一类中的另一个方法

如何从同一类 C# 中的另一个方法访问一个方法的变量?

如何模拟正在测试的同一类中的另一个方法?

从同一类中的另一个方法打印值

从同一类中的另一个方法访问变量

在 Laravel 中以相同的方法将变量从一个查询传递到另一个查询

将ArrayList <Object>从一个Intent传递到另一个Intent

将属性从一个bean递归复制到另一个(不属于同一类)(包括嵌套bean)

使用另一个类中的 jbutton 操作将 jcombobox 变量从一个类传递到另一个类

如何将变量从一个类传递到另一个?