Home > OS >  Using multiple arrays as information in a method
Using multiple arrays as information in a method

Time:11-16

I'm struggeling a bit with arrays and user what's inside with loops. I have this question for example (ignore what's inside of the previewOrder method, i was trying stuff out):


public class Ex1_19 {
    final static String NAMES[]= {"Spa reine 25 ","Bru plate 50","Bru pét 50",
            "Pepsi","Spa orange", "Schweppes Tonic","Schweppes Agr","Ice Tea","Ice Tea Pêche",
            "Jus d'orange Looza", "Cécémel", "Red Bull","Petit Expresso","Grand Expresso","Café décaféiné ",
            "Lait Russe ","Thé et infusions","Irish Coffee ","French Coffee ","Cappuccino","Cécémel chaud",
            "Passione Italiano","Amour Intense", "Rhumba Caliente ","Irish Kisses ","Cuvée Trolls 25",
            "Cuvee Trolls 50","Ambrasse-Temps 25","Ambrasse-Temps 50 ","Brasse-Temps Cerises 25",
            "Brasse-Temps Cerises 50","La Blanche Ste Waudru 25","Blanche Ste Waudru 50",
            "Brasse-Temps citr 25","Brasse-Temps citr 50","Spaghetti Bolo ","Tagl Carbonara",
            "Penne poulet baslc ","Tagl American","Tagl saum"};
            
    final static double NETPRICES[]= {2.2, 2.3,3.9,2.2,2.2,2.6,2.6,2.6,2.6,2.6,2.6,4.5,2.2,2.2,2.2,2.5,2.5,7.0,7.0,2.8,2.8,6.2,6.2,6.2,6.2,
                                        2.9,5.5,2.7,5.1,3.1,5.8,2.6,4.9,2.6,4.9,10.8,11.2,12.2,14.5,16.9};
    
    public static void main(String[] args) {
        int Order[][]={{3,2},{1,3},{12,4},{37,1},{36,3},{0,0},{0,0},{0,0}, {0,0}};
        
        previewOrder(Order);
    }
    public static void previewOrder(int order[][]) {
        int i = 0;
        int j = 0;
        
        while(i < order.length && j < order.length) {
            System.out.println(NAMES[i]);
            i  ;
            j  ;
        }
    }
}

My result has to be something like this but with what's inside the "order" array:

Bru pét 50 3.9 2 7,80 Spa reine 25 2.2 3 6,60 Red Bull 4.5 4 18,00 Tagl Carbonara 11.2 1 11,20 Spaghetti Bolo 10.8 3 32,40

In my exercice I have to use a while loop and I have to put the order array in my method parameters. I can't figure out how to make them all communicate.

Sorry if this question has been answered somewhere else but I don't know how to search for it.

EDIT: I know that Orders does not use zero based index, but starts at 1. This is probably because "Order" is supposed to be a user entry. The first number of the array is like a drink number.

I wasn't very clear on the expected output. Bru pét 50 (NAME[3]) 3.9 (NETPRICE[3]) 2 (Order[][2]) 7.80 NETPRICE[3] * Order[][2] and this for every occurence in Order

CodePudding user response:

The Order array (btw: should be named order or orders) obviously contains references to ordered items and their amount. It's a two dimensional array, like a table with two columns.

Your expected output of "Bru pét 50 3.9 2 7,80", coming from Order[0] {3,2} indicates that the first element (Order[0][0]) is a reference to the items name (from NAMES) and the price (from NETPRICES). The second value of each "row" is the item amount (2) and finally there's the computed total.

For reasons unknown, Orders does not not use zero-based indexed, but starts at 1. So ORDER[0] having value {3,2} actually referes to NAMES[2] and NETPRICES[2]. This needs to be taken into account when picking the right item form NAMES and NETPRICES.

Anyhow: This is what your method could look like. You still need to tweak the output according to your needs.

    public static void previewOrder(int order[][]) {
        for (int i = 0; i < order.length; i  ) {

            int index = order[i][0] - 1;

            if (index < 0 || index > NAMES.length || index > NETPRICES.length) {
                continue;
            }

            String name = NAMES[order[i][0] - 1];
            double price = NETPRICES[order[i][0] - 1];
            int amount = order[i][1];
            double total = amount * price;

            System.out.println(
                    name   " "   price   " "   amount   " "   total
            );

        }
    }

CodePudding user response:

Try this.

public static void previewOrder(int order[][]) {
    Stream.of(order)
        .filter(x -> x[0] != 0)
        .forEach(x -> {
            String name = NAMES[x[0] - 1];
            int unit = x[1];
            double price = NETPRICES[x[0] - 1];
            System.out.printf("%s %.1f %d %.2f%n",
            name, price, unit, price * unit);
        });
}

output:

Bru p?t 50 3.9 2 7.80
Spa reine 25  2.2 3 6.60
Red Bull 4.5 4 18.00
Tagl Carbonara 11.2 1 11.20
Spaghetti Bolo  10.8 3 32.40
  • Related