0

I have 2 array...1 with the name of the person who pay something and another one with the price of each payment. If a person didn't buy anything, the arrayPrice with the same index of the nameArray at the "name" position, is empty. How can I set a default value to "0" --> big decimal , if the entry is null? That's my code that fill the array with only [0], even if I have 1 person with 1 payment:

db.collection("users").document(email).collection("Group").document(groupName)
            .get()
            .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                    if (task.isSuccessful()) {
                        DocumentSnapshot document = task.getResult();

                      // Getting all the partecipants of a group
                        String[] values = (String.valueOf(document.get("partecipant")).replace("[", "").replace("]", "").replace(" ", "").split(","));
                        for(String value : values){
                            nameArray.add(value);
                        }
                        Log.v("nameArraydownloaded", nameArray.toString());
                    }
                }
            });

    DocumentReference docRef = db.collection("users").document(email).collection("Group").document(groupName);

    docRef.collection("Payments")
            .get()
            .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                    for (QueryDocumentSnapshot document : queryDocumentSnapshots) {

                        //Extracting payment description from each document


                        cont += 1;
                        //Extracting cost and who payed from each document

                        price = document.getString("cost");
                        priceFloat += Float.parseFloat(price);
                        priceArray.add(new BigDecimal(price));
                        name = document.getString("paid by");
                        nameArray.add(name);

                    }

                    numberOfPaymentTV.setText(String.valueOf(cont));
                    totalCost.setText(decimalFormat.format(priceFloat) + "€");
                    cpp = decimalFormat.format(priceFloat / Float.parseFloat(num_partecipants));
                    costPerPerson.setText(cpp + "€");


                    while(priceArray.size() < nameArray.size()){
                        priceArray.add(BigDecimal.valueOf(0));
                    }
                    if (nameArray.size() > 0) {
                        // Manipulate the arrays
                        Map<String, BigDecimal> totals = new HashMap<>();

                        for (int i = 0; i < nameArray.size(); ++i) {
                            String name = nameArray.get(i);

                            BigDecimal price = priceArray.get(i);
                            BigDecimal total = totals.get(name);

                            if (total != null) {
                                totals.put(name, total.add(price));
                            } else {
                                totals.put(name, price);
                            }
                        }
                        nameArray.clear();
                        priceArray.clear();
                        // Adding single name and price value for each partecipant
                        for (Map.Entry<String, BigDecimal> entry : totals.entrySet()) {
                            nameArray.add(entry.getKey());
                            priceArray.add(entry.getValue());

                        }
                        // Order the array by descending price
                        for (int i = 0; i < priceArray.size(); i++) {
                            for (int j = 0; j < priceArray.size() - 1; j++) {
                                if (priceArray.get(j).compareTo(priceArray.get(j + 1)) < 0) {
                                    BigDecimal tempPrice = priceArray.get(j);
                                    String tempName = nameArray.get(j);
                                    priceArray.set(j, priceArray.get(j + 1));
                                    nameArray.set(j, nameArray.get(j + 1));
                                    priceArray.set(j + 1, tempPrice);
                                    nameArray.set(j + 1, tempName);
                                }
                            }
                        }
                        Log.v("priceArray", priceArray.toString());
                        for (int i = 0; i < nameArray.size() - 1; i++) {
                            if (BigDecimal.valueOf(Float.parseFloat(cpp.replace(",", "."))).subtract(priceArray.get(i)).compareTo(BigDecimal.ZERO) < 0) {
                                receiverArray.add(nameArray.get(i));

                            }

                        }


                        for (int i = 1; i < nameArray.size(); i++) {

                            if (BigDecimal.valueOf(Float.parseFloat(cpp.replace(",", "."))).subtract(priceArray.get(i)).compareTo(BigDecimal.ZERO) > 0) {

                                debtorArray.add(nameArray.get(i));
                            }
                        }
                        if (receiverArray.size() > debtorArray.size()) {
                            for (int i = 0; i < nameArray.size() - 1; i++) {
                                differenceArray.add(priceArray.get(i).subtract(BigDecimal.valueOf(Float.parseFloat(cpp.replace(",", ".")))).abs());

                            }
                        } else {
                            for (int i = 1; i < nameArray.size(); i++) {
                                differenceArray.add(priceArray.get(i).subtract(BigDecimal.valueOf(Float.parseFloat(cpp.replace(",", ".")))).abs());
                            }
                        }

                        if (!receiverArray.isEmpty() && !debtorArray.isEmpty()) {
                            while (receiverArray.size() < debtorArray.size()) {
                                receiverArray.add(receiverArray.get(receiverArray.size() - 1));
                            }
                            while (debtorArray.size() < receiverArray.size()) {
                                debtorArray.add(debtorArray.get(debtorArray.size() - 1));
                            }
                        }
                        Log.v("recArray", receiverArray.toString());
                        Log.v("debArray", debtorArray.toString());
                        Log.v("difArray", differenceArray.toString());

                        customAdapter = new ReportAdapter(getActivity(), debtorArray, receiverArray, differenceArray);
                        listView.setAdapter(customAdapter);
                    }
                }

The array with this code is [14.00], but there are 3 person in the name array, and I need the priceArray to be: [14.00, 0, 0] How can I do that?

10
  • Perhaps what you're looking for is Map#compute. Commented Dec 11, 2018 at 18:13
  • This looks like a follow-up question to stackoverflow.com/q/53706649/8298909 . Are you saying that the original priceArray can have null elements in it? Commented Dec 11, 2018 at 18:38
  • @BenP. No, the price Array contains the total amount spent by everyone. If a partecipants didn't buy anything, I Need to set the default amount to 0, but now my priceArray increase the size only if a partecipants buy something Commented Dec 11, 2018 at 18:45
  • In your original question, you say nameArray = Nicola, Raul, Lorenzo, Raul, Raul, Lorenzo, Nicola and priceArray = 24, 12, 22, 18, 5, 8, 1. What would it look like if Nicola didn't spend anything? Commented Dec 11, 2018 at 18:47
  • @BenP. Yes but know I changed my code..the nameArray contains each partecipants of a group, even if the didn't buy anything. Then, at the begin the priceArray Is the same you wrote in your comment. But then I sum the payment of each partecipants so the price Array became "25,35,30" and the nameArray "nicola,Raul,Lorenzo". Let's suppose that now I have the nameArray with "Nicola,Raul, Lorenzo' and Lorenzo didn't buy anything, this i the new situation : nameArray "Nicola, Raul, Lorenzo", priceArray "25,35", but I want to Be 25,35,0 Commented Dec 11, 2018 at 18:57

1 Answer 1

0

You need map.getOrDefault("key", <defaultValue>) .

Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.