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

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);
}
</orderitem></orderitemdto>

第5步:隊列管理

接下來,我請 ChatGPT 幫我設計將客戶放入隊列並追蹤其位置的邏輯。

提示:

如何計算咖啡加盟系統中訂單的排隊位置和等待時間?

結果: ChatGPT 建議建立一個 QueueService 來追蹤訂單並根據時間戳為其分配位置。我的實作如下:

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

它還提供了根據平均訂單處理時間估計等待時間的指導。

第 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
在Java應用程序中緩解平台特定問題的策略是什麼?在Java應用程序中緩解平台特定問題的策略是什麼?May 01, 2025 am 12:20 AM

Java如何緩解平台特定的問題? Java通過JVM和標準庫來實現平台無關性。 1)使用字節碼和JVM抽像操作系統差異;2)標準庫提供跨平台API,如Paths類處理文件路徑,Charset類處理字符編碼;3)實際項目中使用配置文件和多平台測試來優化和調試。

Java的平台獨立性與微服務體系結構之間有什麼關係?Java的平台獨立性與微服務體系結構之間有什麼關係?May 01, 2025 am 12:16 AM

java'splatformentenceenhancesenhancesmicroservicesharchitecture byferingDeploymentFlexible,一致性,可伸縮性和便攜性。 1)DeploymentFlexibilityAllowsibilityAllowsOllowsOllowSorlowsOllowsOllowsOllowSeStorunonAnyPlatformwithajvM.2)penterencyCrossServAccAcrossServAcrossServiCessImplifififiesDeevelopmentandeDe

GRAALVM與Java的平台獨立目標有何關係?GRAALVM與Java的平台獨立目標有何關係?May 01, 2025 am 12:14 AM

GraalVM通過三種方式增強了Java的平台獨立性:1.跨語言互操作,允許Java與其他語言無縫互操作;2.獨立的運行時環境,通過GraalVMNativeImage將Java程序編譯成本地可執行文件;3.性能優化,Graal編譯器生成高效的機器碼,提升Java程序的性能和一致性。

您如何測試Java應用程序的平台兼容性?您如何測試Java應用程序的平台兼容性?May 01, 2025 am 12:09 AM

效率testjavaapplicationsforplatformcompatibility oftheSesteps:1)setUpautomatedTestingTestingActingAcrossMultPlatFormSusingCitoolSlikeSlikeJenkinSorgithUbactions.2)contuctualtemualtemalualTesteTESTENRETESTINGINREALHARTWARETOLEALHARDOELHARDOLEATOCATCHISSUSESUSEUSENINCIENVIRENTMENTS.3)schictcross.3)schoscross.3)

Java編譯器(Javac)在實現平台獨立性中的作用是什麼?Java編譯器(Javac)在實現平台獨立性中的作用是什麼?May 01, 2025 am 12:06 AM

Java編譯器通過將源代碼轉換為平台無關的字節碼,實現了Java的平台獨立性,使得Java程序可以在任何安裝了JVM的操作系統上運行。

在平台獨立性的平台獨立性上使用字節碼優於本機代碼的優點是什麼?在平台獨立性的平台獨立性上使用字節碼優於本機代碼的優點是什麼?Apr 30, 2025 am 12:24 AM

ByteCodeachievesPlatFormIndenceByByByByByByExecutedBoviratualMachine(VM),允許CodetorunonanyplatformwithTheApprepreprepvm.Forexample,Javabytecodecodecodecodecanrunonanydevicewithajvm

Java真的100%獨立於平台嗎?為什麼或為什麼不呢?Java真的100%獨立於平台嗎?為什麼或為什麼不呢?Apr 30, 2025 am 12:18 AM

Java不能做到100%的平台獨立性,但其平台獨立性通過JVM和字節碼實現,確保代碼在不同平台上運行。具體實現包括:1.編譯成字節碼;2.JVM的解釋執行;3.標準庫的一致性。然而,JVM實現差異、操作系統和硬件差異以及第三方庫的兼容性可能影響其平台獨立性。

Java的平台獨立性如何支持代碼可維護性?Java的平台獨立性如何支持代碼可維護性?Apr 30, 2025 am 12:15 AM

Java通過“一次編寫,到處運行”實現平台獨立性,提升代碼可維護性:1.代碼重用性高,減少重複開發;2.維護成本低,只需一處修改;3.團隊協作效率高,方便知識共享。

See all articles

熱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

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

熱工具

SublimeText3 Mac版

SublimeText3 Mac版

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

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

記事本++7.3.1

記事本++7.3.1

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

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器