SSW SSW - 2 months ago 6
Java Question

How do I create a foreach loop for multiple arrays in a method?

I'm a beginner with code, so I apologize if my data structure and logic is poor practice. I need to print out the total sale for each product. For example, for "mac" it would be labeled as Category 0, and "iphone" would be labeled as Category 1.

I am having trouble matching the index position for the categories with the sum of each respective category. I really just need some kind of for loop. I realize I can make a 2D array as well as use intstream, but I haven't learned it yet. This is only a portion of the code, so using a 2D array would really complicate things.

Right now, I am trying the following:

public static int[] totalSale( int[] mac, int[] iphone, int[] ipad, int[] ipod ){
int[] totalSale = {0,0,0,0};
int sum = 0;
for (int i : mac) {
sum += i;
}
for (int i = 0; i < totalSale.length; i++) {
System.out.println("Total sale for category " + i + ": $" + sum);
}
return totalSale;
}


Earlier, I tried this:

public static int[] totalSale( int[] mac, int[] iphone, int[] ipad, int[] ipod ){
int[] totalSale = {0,0,0,0};

for (int i = 0; i < mac.length ; i++) {
totalSale[0] = totalSale[0] + mac[i];
totalSale[1] = totalSale[1] + iphone[i];
totalSale[2] = totalSale[2] + ipad[i];
totalSale[3] = totalSale[3] + ipod[i];
}
for (int i = 0; i < totalSale.length; i++) {
System.out.println("Total sale for category " + i + ": $" + totalSale[i]);
}
return totalSale;
}


But this code doesn't work, but it assumes that I can only input up to 4 values.

I am hoping to create a code that works for up to 31 values. The first group of code, is there a way to create a for each loop sum for all the arrays?

The print portion of this code should look like:

Total sale for category 0: $XXXXX

Total sale for category 1: $XXXX

Total sale for category 2: $XXX

Total sale for category 3: $XXXX

Total sale for category N: $XXXX

Total sale for category N: $XXXX
and etc.

Here are example inputs for the arrays:

public static void main(String[] arguments) {
int[] days = {1,2,3,4,5};
int[] mac = {11500,9000,13000,900,100};
int[] iphone = {1100,5000,3400,0,0};
int[] ipad = {900,4300000,0,800,0};
int[] ipod = {0,300,120,500,25000};
numberOfDays(days);
int[]totalSales = totalSale(mac,iphone,ipad,ipod);
popularProduct(totalSales);
popularDay(days,mac,iphone,ipad,ipod);
}

Answer

The 2nd version (the earlier one) of 'totalSale'

public static int[] totalSale( int[] mac, int[] iphone, int[] ipad, int[] ipod ){

is not optimal but it is correct. It will print out the right values.
Try it here.

The output is:

Total sale for category 0: $34500
Total sale for category 1: $9500
Total sale for category 2: $4301700
Total sale for category 3: $25920

Consider using a Map. Here is an implmentation using 'Map':

import java.util.HashMap;
import java.util.Map;

public class Test{

    public static void main(String[] arguments) {

        //map category to sales values
        Map<String, int[]> salesMap = new HashMap<>();

        //map category to sales totals
        Map<String, Integer> totalSalesMap = new HashMap<>();

        int[] mac = {11500,9000,13000,900,100};//total 34500
        salesMap.put("Mac",mac);
        int[] iphone = {1100,5000,3400,0,0};//total $9500
        salesMap.put("Iphone",iphone);
        int[] ipad = {900,4300000,0,800,0};
        salesMap.put("Ipad",ipad);
        int[] ipod = {0,300,120,500,25000};
        salesMap.put("Ipod",ipod);

        totalSalesMap = totalSale(salesMap);

        //print totals:
        for( String category : totalSalesMap.keySet()){
            System.out.println("Total sale for category "
                            + category + ": $" + totalSalesMap.get(category));
        }
    }

    public static Map<String, Integer> totalSale(Map<String, int[]> salesMap){

        //returned map holding sales totals
        Map<String, Integer> totalSalesMap = new HashMap<>();

        //iterate over sales map and sum values into totalSalesMap
        for( String category : salesMap.keySet()){

            int[] sales = salesMap.get(category);
            int salesSum = sumArray(sales);

            //add total to returned map
            totalSalesMap.put(category, salesSum);
        }

        return totalSalesMap;
    }

    private static int sumArray(int[] array) {

        int sum = 0;
        for(int i : array) {
            sum += i;
        }

        return sum;
    }
}