英文:
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="/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 No 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=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);
}
专注分享java语言的经验与见解,让所有开发者获益!
评论