如何修复“找不到局部变量”错误?

huangapple 未分类评论43阅读模式
英文:

How to fix "Can not find local variable" error?

问题

@RequestMapping(value="/mini/all_products/allpurchase_view", method = RequestMethod.POST)
public ResponseEntity<Map<String, Object>> addNewPurchase(Authentication authentication, @RequestBody List<Long> products){
    Map<String, Object> dto = new HashMap<>();
    User user = userDetailslogged(authentication);

    if(authentication == null)
        return new ResponseEntity<>(makeMapResponse("Error", "User Not Authenticated"), HttpStatus.UNAUTHORIZED);
    if(user == null)
        return new ResponseEntity<>(makeMapResponse("Error", "Register This User Please"), HttpStatus.UNAUTHORIZED);

    Double pricePurchase = 0.0;
    List<Product> purchaseProducts = new ArrayList<>();

    for (Long id : products){
        Product product = productRepository.getOne(id);
        product.setProductStock(product.getProductStock() - 1);
        productRepository.save(product);
        pricePurchase += product.getProductPrice();
        purchaseProducts.add(product);
    }

    Double discountMore5 = 0.0;
    if(purchaseProducts.size() > 5)
        discountMore5 = pricePurchase * 0.10;
    else
        discountMore5 = 0.0;

    Map<Long, Integer> counter = new HashMap<Long, Integer>();
    Double discountEqual4 = 0.0;

    for(Long productId : products)
         if(counter.containsKey(productId))
             counter.put(productId, counter.get(productId) + 1);
         else
             counter.put(productId, 1);

    for(Map.Entry<Long, Integer> repeated : counter.entrySet()) {
       // This is where the problem is:
       **Product prods** = productRepository.getOne(repeated.getKey());
        if(repeated.getKey() == prods.getId() && repeated.getValue() >= 4)
            discountEqual4 = prods.getProductPrice() * Math.floor(repeated.getValue() / 4);
    }

    Double totalDiscount = discountEqual4 + discountMore5;

    Purchase purchase1 = new Purchase(new Date(), user, products.size(), pricePurchase, totalDiscount, purchaseProducts);
    purchaseRepository.save(purchase1);

    return new ResponseEntity<>(makeMapResponse("Success", "Product Added"), HttpStatus.CREATED);
}
英文:

Good Day developers , im just building this app using spring boot , and in one of its methods i got this error:"Cannot find local variable 'prods'".Basically i just obtain a list of ids assigned to products and from there start to develope functions that considering this ids and other app requirements might throw a result.But in my last forloop i initialize for second time a variable of type Product with a diferent name(prods) despite of having initialized and save it before in former function with different name(product), but then consistently gives me that error , even though if i debug not letting me to find a possible source of the issue.
Any idea about why is this happening .Thanks in advance!!!!.Im sharing the part of my code where the problem is.Thanks!!!

@RequestMapping(value=&quot;/mini/all_products/allpurchase_view&quot;,method = RequestMethod.POST)
public ResponseEntity &lt;Map&lt;String,Object&gt;&gt;addNewPurchase( Authentication authentication,RequestBody List&lt;Long&gt; products){
    Map&lt;String,Object&gt; dto=new HashMap&lt;&gt;();
    User user=userDetailslogged(authentication);

    if(authentication==null)
        return new ResponseEntity&lt;&gt;(makeMapResponse(&quot;Error&quot;,&quot;User No Authenticated&quot;), HttpStatus.UNAUTHORIZED);
    if(user==null)
        return new ResponseEntity&lt;&gt;(makeMapResponse(&quot;Error&quot;,&quot;Register This User Please &quot;), HttpStatus.UNAUTHORIZED);

    Double pricePurchase = 0.0;
    List&lt;Product&gt;purchaseProducts=new ArrayList&lt;&gt;();

    for (Long id : products){
        Product product=productRepository.getOne(id);
        product.setProductStock(product.getProductStock()-1);
        productRepository.save(product);
        pricePurchase=pricePurchase+product.getProductPrice();
        purchaseProducts.add(product);
    }

    Double DiscountMore5 = 0.0;
    if(purchaseProducts.size() &gt; 5)
        DiscountMore5 = pricePurchase * 0.10;
    else
        DiscountMore5 = 0.0;

    Map&lt;Long,Integer&gt; counter = new HashMap&lt;Long,Integer&gt;();
    Double discountEqual4 = 0.0;

    for(Long productId:products)
         if(counter.containsKey(productId))
             counter.put(productId,counter.get(productId)+1);
         else
             counter.put(productId,1);

    for(Map.Entry&lt;Long,Integer&gt; repeated :counter.entrySet()) {
       // This is where the problem is:
       **Product prods**=productRepository.getOne(repeated.getKey());
        if(repeated.getKey() == prods.getId() &amp;&amp; repeated.getValue() &gt;= 4)
            discountEqual4 = prods.getProductPrice()*Math.floor(repeated.getValue()/4);
    }

    Double totalDiscount=discountEqual4+DiscountMore5;

    Purchase purchase1=new Purchase(new Date(),user,products.size(),pricePurchase,totalDiscount,purchaseProducts);
    purchaseRepository.save(purchase1);

    return new ResponseEntity&lt;&gt;(makeMapResponse(&quot;Success&quot;,&quot;Product Added&quot;), HttpStatus.CREATED);
}

huangapple
  • 本文由 发表于 2020年4月4日 23:49:55
  • 转载请务必保留本文链接:https://java.coder-hub.com/61030692.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定