Spring Boot火车票订票购票票务系统设计与实现
由于原始代码较为复杂且涉及到火车票订票的具体业务逻辑,我们将提供核心的Spring Boot应用配置和服务层的简化示例。
// 火车票服务接口
public interface TicketService {
Order queryOrder(String orderId);
boolean isTicketLeft(String trainNumber, String from, String to);
Order purchaseTicket(String trainNumber, String from, String to);
}
// 火车票服务实现
@Service
public class TicketServiceImpl implements TicketService {
@Autowired
private OrderRepository orderRepository;
@Autowired
private RestTemplate restTemplate;
@Override
public Order queryOrder(String orderId) {
return orderRepository.findById(orderId).orElse(null);
}
@Override
public boolean isTicketLeft(String trainNumber, String from, String to) {
String url = "http://ts.sdo.com/train/left_ticket/?train_date=2023-03-01&station_train_code=" + trainNumber + "&from_station=" + from + "&to_station=" + to;
String response = restTemplate.getForObject(url, String.class);
// 根据响应结果判断是否有票
return !"无".equals(response);
}
@Override
public Order purchaseTicket(String trainNumber, String from, String to) {
// 模拟下单操作
Order order = new Order();
order.setOrderId(UUID.randomUUID().toString());
order.setTrainNumber(trainNumber);
order.setFrom(from);
order.setTo(to);
orderRepository.save(order);
return order;
}
}
// 实体类Order
@Entity
public class Order {
@Id
private String orderId;
private String trainNumber;
private String from;
private String to;
// 省略getter和setter
}
// 仓库接口OrderRepository
public interface OrderRepository extends JpaRepository<Order, String> {
}
// 控制器类TicketController
@RestController
public class TicketController {
@Autowired
private TicketService ticketService;
@GetMapping("/query_order")
public Order queryOrder(@RequestParam String orderId) {
return ticketService.queryOrder(orderId);
}
@GetMapping("/is_ticket_left")
public boolean isTicketLeft(@RequestParam String trainNumber, @RequestParam String from, @RequestParam String to) {
评论已关闭