#308 Code Review of Order Status Update
The following code updates an order status. Find the problems and modify the code:
@Service
public class OrderService {
@Autowired
private OrderRepository orderRepository;
private final NotificationService notificationService;
@Autowired
public OrderService(NotificationService notificationService) {
this.notificationService = notificationService;
}
public void updateOrderStatus(Long orderId, String newStatus) {
Order order = orderRepository.findById(orderId).get();
if (newStatus.equals("COMPLETED")) {
order.setStatus("COMPLETED");
orderRepository.save(order);
notificationService.notifyUser(order.getUserId(), "Your order is completed");
} else if (newStatus.equals("CANCELLED")) {
order.setStatus("CANCELLED");
orderRepository.save(order);
notificationService.notifyUser(order.getUserId(), "Your order is cancelled");
} else if (newStatus.equals("PENDING")) {
order.setStatus("PENDING");
orderRepository.save(order);
} else if (newStatus.equals("IN_PROGRESS")) {
order.setStatus("IN_PROGRESS");
orderRepository.save(order);
} else {
throw new IllegalArgumentException("Unsupported status: " + newStatus);
}
}
}
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