// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.sps.data;

import com.google.appengine.api.datastore.Entity;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.stream.Collectors;

/** Class that computes and stores user's spending analytics. */
public class SpendingAnalytics {
  private final HashMap<String, Double> storeAnalytics;
  private final HashMap<String, Double> categoryAnalytics;

  public SpendingAnalytics(ImmutableSet<Entity> allReceipts) {
    storeAnalytics = new HashMap<>();
    categoryAnalytics = new HashMap<>();

    for (Entity receipt : allReceipts) {
      updateStoreAnalytics(receipt, storeAnalytics);

      updateCategoryAnalytics(receipt, categoryAnalytics);
    }
  }

  /** Updates storeAnalytics hashmap with store info from the passed in receipt. */
  private void updateStoreAnalytics(Entity receipt, HashMap<String, Double> storeAnalytics) {
    String store = (String) receipt.getProperty("store");
    Double price = (Double) receipt.getProperty("price");

    // Don't add to hashmap if either store or price are invalid.
    if (!Strings.isNullOrEmpty(store) && price != null) {
      // Either update the existing entry or add a new one if it doesn't exist.
      if (storeAnalytics.containsKey(store)) {
        storeAnalytics.put(store, storeAnalytics.get(store) + price);
      } else {
        storeAnalytics.put(store, price);
      }
    }
  }

  /** Updates categoryAnalytics hashmap with category info from the passed in receipt. */
  private void updateCategoryAnalytics(Entity receipt, HashMap<String, Double> categoryAnalytics) {
    Double price = (Double) receipt.getProperty("price");
    ImmutableSet<String> categories;

    // If categories is null for a particular receipt, just skip it and continue.
    try {
      categories = ImmutableSet.copyOf((ArrayList) receipt.getProperty("categories"));
    } catch (NullPointerException exception) {
      return;
    }

    for (String category : categories) {
      // Don't add to hashmap if price is invalid.
      if (price != null) {
        // Either update the existing entry or add a new one if it doesn't exist.
        if (categoryAnalytics.containsKey(category)) {
          categoryAnalytics.put(category, categoryAnalytics.get(category) + price);
        } else {
          categoryAnalytics.put(category, price);
        }
      }
    }
  }

  public HashMap<String, Double> getStoreAnalytics() {
    return storeAnalytics;
  }

  public HashMap<String, Double> getCategoryAnalytics() {
    return categoryAnalytics;
  }
}
@@ -1755,17 +1755,16 @@
     if (
-!
 Strings.
@@ -1788,18 +1788,18 @@
 re) 
-&&
+%7C%7C
  price 
-!
+=
 = nu
@@ -1802,24 +1802,43 @@
 = null) %7B%0A  
+    return;%0A    %7D%0A%0A
     // Eithe
@@ -1903,26 +1903,24 @@
  exist.%0A    
- 
- 
 if (storeAna
@@ -1944,26 +1944,24 @@
 y(store)) %7B%0A
-  
       storeA
@@ -2012,26 +2012,24 @@
 ) + price);%0A
-  
     %7D else %7B
@@ -2031,26 +2031,24 @@
 lse %7B%0A      
-  
 storeAnalyti
@@ -2069,24 +2069,16 @@
 price);%0A
-      %7D%0A
     %7D%0A  
@@ -2324,24 +2324,135 @@
 y(%22price%22);%0A
+%0A    // Don't add any categories to hashmap if price is invalid.%0A    if (price == null) %7B%0A      return;%0A    %7D%0A%0A
     Immutabl
@@ -2777,88 +2777,8 @@
   //
- Don't add to hashmap if price is invalid.%0A      if (price != null) %7B%0A        //
  Eit
@@ -2851,18 +2851,16 @@
 .%0A      
-  
 if (cate
@@ -2894,26 +2894,24 @@
 ategory)) %7B%0A
-  
         cate
@@ -2976,26 +2976,24 @@
 ) + price);%0A
-  
       %7D else
@@ -2999,26 +2999,24 @@
 e %7B%0A        
-  
 categoryAnal
@@ -3047,18 +3047,8 @@
 e);%0A
-        %7D%0A
     

Refactor some statements to be guard clauses