首页  >  文章  >  Java  >  使用 ChatGPT 构建订单处理服务(贡献努力)并已完成

使用 ChatGPT 构建订单处理服务(贡献努力)并已完成

DDD
DDD原创
2024-09-25 17:50:02588浏览

Building an Orders Processing Service with ChatGPT (contribute  efforts) and Finished in ays

人工智能为改变我的日常工作并提高效率做出了贡献

作为一名开发人员,当您的时间有限时,构建订单处理服务有时会让人感到不知所措。然而,借助 ChatGPT 等人工智能驱动的开发工具的强大功能,您可以通过生成代码、设计实体和逐步解决问题来显着加快流程。在本文中,我将向您介绍如何使用 ChatGPT 在短短 2 天内构建功能齐全的订单处理服务,从收集需求到完成。

老实说,对于不同的小任务有很多小线程和提示,我无法将它们总结成一个完整的项目,但总的来说......它帮助了我 70 - 80%。另外,这里是一些原始代码,经过我审阅,可能是手工修改的,所以你可能在我分享的github上找不到这个功能。

第一天:了解要求和设置

第 1 步:收集并明确需求

我做的第一件事就是列出该服务所需的核心功能。以下是我需要的主要功能:

  1. 用户注册:允许用户使用手机号码和地址进行注册。
  2. 特许经营位置搜索:使客户能够查看和查找附近的咖啡特许经营店。
  3. 下订单:顾客可以从菜单中下单包含多个项目。
  4. 队列管理:跟踪客户在队列中的位置并提供预期等待时间。
  5. 取消订单:顾客可以随时退出队列并取消订单。

第 2 步:使用 ChatGPT 生成 API 端点

我请求 ChatGPT 帮助我设计满足需求的 API 结构。这是我使用的第一个提示的示例:

提示:

使用 Spring Boot 为用户注册系统创建 API 端点,用户可以在其中使用自己的姓名、手机号码和地址进行注册。

结果: ChatGPT 生成了多个端点:

  • POST /users/register:注册新用户。
  • GET /franchises/nearby:根据纬度和经度查找附近的咖啡特许经营店。
  • POST /orders: 下单购买多件商品。
  • GET /orders/{orderId}/queue-position:检查用户在队列中的位置。
  • DELETE /orders/{orderId}: 取消订单并退出队列。

第 3 步:实体设计

对于订单处理服务,我们需要 User、Franchise、Order、Queue 和 OrderItem 实体。我使用 ChatGPT 来定义这些具有必要字段的实体。

提示:

为系统设计用户实体。用户可以拥有手机号码、地址和角色(例如 CUSTOMER)。

结果: ChatGPT 使用 JPA 提供了一个简单的用户实体:

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private UUID id;

    @Column(nullable = false, unique = true)
    private String username;
    @Column(nullable = false)
    private String password;
    private String mobileNumber;
    private String address;
    private UserRole role; // CUSTOMER, ADMIN
}

我对特许经营、订单和队列实体重复了此过程。

第 2 天:实施业务逻辑

第四步:下单逻辑

设置基本 API 和实体后,我开始实施下单业务逻辑。这是该服务的关键部分,因为它需要处理菜单中的多个项目并管理队列位置。

提示:

实现多个商品下订单的逻辑,其中每个商品都链接到特许经营中的特定菜单。

结果: ChatGPT 指导我设计了一个 OrderService 来处理这个问题。这是实现的一部分:

public Order createOrder(UUID customerId, UUID franchiseId, List<OrderItemDTO> items) {
    Order order = new Order();
    order.setCustomer(userRepository.findById(customerId).orElseThrow());
    order.setFranchise(franchiseRepository.findById(franchiseId).orElseThrow());

    List<OrderItem> orderItems = items.stream()
        .map(itemDto -> new OrderItem(menuItemRepository.findById(itemDto.getMenuItemId()), itemDto.getQuantity()))
        .collect(Collectors.toList());
    order.setItems(orderItems);
    order.setQueuePosition(findQueuePositionForFranchise(franchiseId));
    return orderRepository.save(order);
}

第5步:队列管理

接下来,我请 ChatGPT 帮助我设计将客户放入队列并跟踪其位置的逻辑。

提示:

如何计算咖啡加盟系统中订单的排队位置和等待时间?

结果: ChatGPT 建议创建一个 QueueService 来跟踪订单并根据时间戳为其分配位置。我的实现如下:

public int findQueuePositionForFranchise(UUID franchiseId) {
    List<CustomerQueue> queue = customerQueueRepository.findAllByFranchiseId(franchiseId);
    return queue.size() + 1;
}

它还提供了根据平均订单处理时间估计等待时间的指导。

第 6 步:取消订单

最后,我实现了允许客户取消订单并退出队列的逻辑:

public void cancelOrder(UUID orderId) {
    Order order = orderRepository.findById(orderId).orElseThrow();
    queueService.removeFromQueue(order.getQueue().getId(), order.getId());
    orderRepository.delete(order);
}

Finalizing the Project

By the end of Day 2, I had a fully functional service that allowed customers to:

  • Register using their mobile number and address.
  • View nearby franchises.
  • Place orders with multiple items from the menu.
  • Check their queue position and waiting time.
  • Cancel their order at any time.

Key Takeaways

  • Leverage AI for Routine Tasks: ChatGPT sped up repetitive tasks like designing APIs, generating boilerplate code, and implementing common business logic patterns.
  • Divide and Conquer: By breaking the project into small, manageable tasks (such as user registration, queue management, and order placement), I was able to implement each feature sequentially.
  • AI-Assisted Learning: While ChatGPT provided a lot of code, I still had to understand the underlying logic and tweak it to fit my project’s needs, which was a great learning experience.
  • Real-Time Debugging: ChatGPT helped me solve real-time issues by guiding me through errors and exceptions I encountered during implementation, which kept the project on track.

I have a few more steps to create the documentation, use liquidbase and have chatGPT generate sample data for easier testing.

Conclusion

Building an order processing system for a coffee shop in 2 days may sound daunting, but with AI assistance, it’s achievable. ChatGPT acted like a coding assistant, helping me transform abstract requirements into a working system quickly. While AI can provide a foundation, refining and customizing code is still an essential skill. This project taught me how to maximize the value of AI tools without losing control of the development process.

By following the steps I took, you can speed up your own projects and focus on higher-level problem-solving, leaving the routine code generation and guidance to AI.

Full source Github

以上是使用 ChatGPT 构建订单处理服务(贡献努力)并已完成的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn