ホームページ  >  記事  >  Java  >  ChatGPT を使用した注文処理サービスの構築 (取り組みに貢献)、無事に完了

ChatGPT を使用した注文処理サービスの構築 (取り組みに貢献)、無事に完了

DDD
DDDオリジナル
2024-09-25 17:50:02588ブラウズ

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

AI は私の日常業務の変化と効率向上に貢献しました

開発者として、時間枠が限られている場合、注文処理サービスを構築するのは大変だと感じることがあります。ただし、ChatGPT のような AI 主導の開発ツールを利用すると、コードの生成、エンティティの設計、問題の解決を段階的に行うことで、プロセスを大幅にスピードアップできます。この記事では、ChatGPT を使用して、要件の収集から完成まで、完全に機能する注文処理サービスをわずか 2 日 で構築した方法を説明します。

正直に言うと、小さなスレッドやさまざまな小さなタスクのプロンプトがたくさんあり、完全なプロジェクトに要約することはできませんが、全体的には 70 ~ 80% 役に立ちました。さらに、ここに元のコードの一部を示します。私がレビューした後、手動で変更されている可能性があるため、私が共有した github にはこの関数が見つからない可能性があります。

1 日目: 要件の理解と設定

ステップ 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、Francise、Order、Queue、および OrderItem のエンティティが必要でした。 ChatGPT を使用して、必要なフィールドを持つこれらのエンティティを定義しました。

プロンプト:

システムの User エンティティを設計します。ユーザーは、携帯電話番号、アドレス、役割 (CUSTOMER など) を持つことができます。

結果: ChatGPT は JPA を使用して単純な User エンティティを提供しました:

@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 日目: ビジネス ロジックの実装

ステップ 4: 発注ロジック

基本的な 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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。