简体   繁体   中英

Passing an ArrayList from a method to another in the same class?

I am coding a small project for a Clothes Shop. and in one of my classes 2 variables( item & price ) will be passed each time a user will select a certain product...

they will be passed into 2 different methods..

one with adding the item and one with adding a price .

and I am trying to take the first array which has all the clothes that have been added to the cart and the other one with all the prices of each and every item

every item and its price has similar indexes because they are being added at the same time...

so now... my problem is that I do want to pass both of these ArrayLists into my method " PrintRecipt() " so I'll be able to print the items with their prices in-front of them...

code:-

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?
   }  
}

Why are you declaring both of the ArrayLists at thier function? Put them as field members, so you can access them from the other functions, and of course in this code you wrote, you can't add more than 1 item, because every add creates a new ArrayList and have no memory of the past cart adds. Make them field, and in the add functions just add to the field of the specific array you want. At the function you asked about, just do a for loop for the ArrayList size, and in every iteration print the i-th element of the first array and the i-th element of the second array.

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));
        }
    }  
}

You can use a single HashMap to store the price against the item. You need to declare this as an instance variable. Then inside printRecipt(), simply iterate over the hashmap entries and print the values according to the format you want.

package clothesshop;
import java.util.ArrayList;
public class Cart {
        private HashMap<String, int> items = new HashMap<String, int>(5);
    int cartAdd(String item, int price){//adding a certain item will be passed here
        items.add(item, price); 
        int TotalP=0;
        for (int total : costs) {
            TotalP = TotalP  + total; //total price
        }
        return TotalP;
    }

    void PrintRecipt() {

    Iterator it = items.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pair = (Map.Entry)it.next();
        System.out.println(pair.getKey() + " at " + pair.getValue());

       }

    }  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM