#342 Refactoring a Product Processing Service
Implement the TODO and refactor the code if necessary:
@Service
@RequiredArgsConstructor
@Sl4j
class ProductServiceImpl implements ProductService {
private final ProductRepository repository;
private OtherService otherService;
private Map<String, String> dictionary;
@PostConstruct
@Transactional
public void postConstruct(){
dictionary = otherService.getDictionary();
}
@Autowired
public void setOtherService(OtherService otherService) {
this.otherService = otherService;
}
/** Method returns a list of processed Products
*
* @param productIds
* @return
*/
@Transactional
public List<ProductProcessResult> processProducts(List<Long> productIds){
productIds.stream().map(productIds.mapToObject)
//TODO
}
@Transactional
public Product getProduct(Long id){
if(id == null){
throw new UnsupportedOperationException("Not supported");
}
return repository.findById(id);
}
public ProductProcessResult process(Product product) throws IOException {
if(product.getProductStatus() == PROCESSED){
throw new IOException("a product in PROCESSED state cannot be processed");
}
otherService.process(product);
product.setProductState(PROCESSED);
return product;
}
}
@Getter
@HashcodeAndEquals
public class Product {
enum ProductStatus{
NEW, PROCESSED
}
private int id;
private ProductStatus productStatus;
private String name;
private Double price;
private List<String> tags;
Product(int id, ProductStatus productStatus, String name, Double price, List<String> tags){
this.id = id;
this.productStatus = productStatus;
this.name = name;
this.price = price;
this.tags = tags;
}
}
Question review
Sign in to start the dialogue
The mentor keeps the context of the current question and your previous messages.
Sign in
Available with Premium
This is just one of the solutions available with Premium. And more:
- 250+ advanced lectures
- 1300+ practice exercises
- 500+ solutions to problems from real interviews
- Personalized Java Hub certificates
- Expanded access to the AI mentor