Last active
June 30, 2022 13:27
-
-
Save atesibrahim/42715325bd8437bf7e572cbed835dbaf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Service | |
public class ProductServiceImpl implements ProductService { | |
@Autowired | |
ProductRepository productRepository; | |
@Autowired | |
CoinAmount coinAmount; | |
@Override | |
public ProductDispenseResponse dispense(ProductRequest request) { | |
Optional<Product> product = productRepository.findById(request.getId()); | |
if (product.get().getStock() < 1) { | |
return ProductDispenseResponse.builder() | |
.responseMessage("SOLD OUT") | |
.currentBalance(coinAmount.getBalance()) | |
.build(); | |
} | |
if (coinAmount.getBalance() < product.get().getPrice()) { | |
return ProductDispenseResponse.builder() | |
.responseMessage("Unfortunately your balance is not sufficient.") | |
.currentBalance(coinAmount.getBalance()) | |
.build(); | |
} | |
updateStock(product.get()); | |
coinAmount.setBalance(coinAmount.getBalance() - product.get().getPrice()); | |
return ProductDispenseResponse.builder() | |
.productName(product.get().getName()) | |
.responseMessage("THANK YOU") | |
.currentBalance(coinAmount.getBalance()) | |
.build(); | |
} | |
private void updateStock(Product product) { | |
product.setStock(product.getStock()-1); | |
productRepository.save(product); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment