首頁 Java java教程 使用 ChatGPT 建立訂單處理服務(貢獻努力)並已完成

使用 ChatGPT 建立訂單處理服務(貢獻努力)並已完成

Sep 25, 2024 pm 05:50 PM

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

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1654
14
CakePHP 教程
1413
52
Laravel 教程
1306
25
PHP教程
1252
29
C# 教程
1225
24
公司安全軟件導致應用無法運行?如何排查和解決? 公司安全軟件導致應用無法運行?如何排查和解決? Apr 19, 2025 pm 04:51 PM

公司安全軟件導致部分應用無法正常運行的排查與解決方法許多公司為了保障內部網絡安全,會部署安全軟件。 ...

如何將姓名轉換為數字以實現排序並保持群組中的一致性? 如何將姓名轉換為數字以實現排序並保持群組中的一致性? Apr 19, 2025 pm 11:30 PM

將姓名轉換為數字以實現排序的解決方案在許多應用場景中,用戶可能需要在群組中進行排序,尤其是在一個用...

如何優雅地獲取實體類變量名構建數據庫查詢條件? 如何優雅地獲取實體類變量名構建數據庫查詢條件? Apr 19, 2025 pm 11:42 PM

在使用MyBatis-Plus或其他ORM框架進行數據庫操作時,經常需要根據實體類的屬性名構造查詢條件。如果每次都手動...

如何使用MapStruct簡化系統對接中的字段映射問題? 如何使用MapStruct簡化系統對接中的字段映射問題? Apr 19, 2025 pm 06:21 PM

系統對接中的字段映射處理在進行系統對接時,常常會遇到一個棘手的問題:如何將A系統的接口字段有效地映�...

IntelliJ IDEA是如何在不輸出日誌的情況下識別Spring Boot項目的端口號的? IntelliJ IDEA是如何在不輸出日誌的情況下識別Spring Boot項目的端口號的? Apr 19, 2025 pm 11:45 PM

在使用IntelliJIDEAUltimate版本啟動Spring...

Java對像如何安全地轉換為數組? Java對像如何安全地轉換為數組? Apr 19, 2025 pm 11:33 PM

Java對象與數組的轉換:深入探討強制類型轉換的風險與正確方法很多Java初學者會遇到將一個對象轉換成數組的�...

電商平台SKU和SPU數據庫設計:如何兼顧用戶自定義屬性和無屬性商品? 電商平台SKU和SPU數據庫設計:如何兼顧用戶自定義屬性和無屬性商品? Apr 19, 2025 pm 11:27 PM

電商平台SKU和SPU表設計詳解本文將探討電商平台中SKU和SPU的數據庫設計問題,特別是如何處理用戶自定義銷售屬...

如何利用Redis緩存方案高效實現產品排行榜列表的需求? 如何利用Redis緩存方案高效實現產品排行榜列表的需求? Apr 19, 2025 pm 11:36 PM

Redis緩存方案如何實現產品排行榜列表的需求?在開發過程中,我們常常需要處理排行榜的需求,例如展示一個�...

See all articles