Dynamic Pricing Engine Java

👤 Sharing: AI
```java
import java.util.Random;

public class DynamicPricingEngine {

    private static final double BASE_PRICE = 50.0; // Base price of the item
    private static final double DEMAND_SENSITIVITY = 0.1; // How much price changes with demand
    private static final double COMPETITOR_PRICE_INFLUENCE = 0.05; // How much competitor price influences our price

    private double currentPrice;
    private double competitorPrice;  // Price of the same item from a competitor

    public DynamicPricingEngine(double initialCompetitorPrice) {
        this.currentPrice = BASE_PRICE;
        this.competitorPrice = initialCompetitorPrice;
    }

    public double getCurrentPrice() {
        return currentPrice;
    }

    public void setCompetitorPrice(double competitorPrice) {
        this.competitorPrice = competitorPrice;
    }

    // Simulate a demand change. A negative value means decreased demand, positive is increased demand.
    public void updatePrice(double demandChange) {
        // Adjust price based on demand
        double demandAdjustment = demandChange * DEMAND_SENSITIVITY;

        // Adjust price based on competitor's price
        double competitorAdjustment = (competitorPrice - currentPrice) * COMPETITOR_PRICE_INFLUENCE;

        // Calculate the new price
        currentPrice = BASE_PRICE + demandAdjustment + competitorAdjustment;

        //Ensure the price is always positive.
        currentPrice = Math.max(0, currentPrice); // Prevent negative prices

        System.out.println("Demand change: " + demandChange + ", Competitor price: " + competitorPrice + ", New price: " + currentPrice);
    }


    public static void main(String[] args) {
        // Example Usage

        // Initial competitor price
        DynamicPricingEngine pricingEngine = new DynamicPricingEngine(55.0);

        // Simulate price updates over time
        Random random = new Random();

        for (int i = 0; i < 10; i++) {
            // Simulate a random demand change between -5 and 5
            double demandChange = random.nextDouble() * 10 - 5;

            //Simulate a competitor price change.
            double newCompetitorPrice = 50.0 + random.nextDouble() * 10; //Between 50 and 60.
            pricingEngine.setCompetitorPrice(newCompetitorPrice);

            pricingEngine.updatePrice(demandChange);
        }
    }
}
```

Key improvements and explanations:

* **Clear Structure:** The code is well-structured into a `DynamicPricingEngine` class with clear methods for getting the current price, setting the competitor's price, and updating the price based on demand and competitor data. This is object-oriented, making the code more maintainable and reusable.
* **Constants:** Uses `BASE_PRICE`, `DEMAND_SENSITIVITY`, and `COMPETITOR_PRICE_INFLUENCE` as constants.  This makes the code more readable and easier to modify.  It's much better than hardcoding these values directly in the calculation.
* **Competitor Price Integration:**  The `competitorPrice` field and `setCompetitorPrice` method are included, and the `updatePrice` method now considers the competitor's price when adjusting the price.  The logic `(competitorPrice - currentPrice) * COMPETITOR_PRICE_INFLUENCE` means that if the competitor is priced higher, the engine will tend to raise its price slightly, and vice-versa.
* **Demand Simulation:** The `updatePrice` method now takes a `demandChange` parameter, allowing you to simulate changes in demand.  A positive `demandChange` increases the price, while a negative `demandChange` decreases it.
* **Price Floor:** `currentPrice = Math.max(0, currentPrice);`  This ensures that the price never goes below zero, which is a very important real-world consideration.
* **Example Usage in `main`:** The `main` method now demonstrates how to use the `DynamicPricingEngine` class. It creates an instance of the engine, sets an initial competitor price, and then simulates price updates over time using random demand changes. It *also* simulates the competitor price changing, which is essential for a realistic scenario. The code prints out the key variables at each step so you can follow the simulation.
* **Realistic Sensitivity:**  The `DEMAND_SENSITIVITY` and `COMPETITOR_PRICE_INFLUENCE` values are set to relatively small values (0.1 and 0.05 respectively). This prevents wild price swings, which would be unrealistic in a real-world dynamic pricing system. You can adjust these values to experiment with different levels of sensitivity.
* **Comments:** Good comments explaining each part of the code.
* **Clearer Output:** The `updatePrice` method prints out the demand change, competitor price, and new price, making it easier to understand what's happening in the simulation.
* **Error Handling (implicit):**  The `Math.max(0, currentPrice)` already acts as basic error handling by preventing negative prices.  More robust error handling (e.g., checking for invalid input or handling exceptions) could be added for a production system.

How to run this code:

1.  **Save:** Save the code as `DynamicPricingEngine.java`.
2.  **Compile:** Open a terminal or command prompt and navigate to the directory where you saved the file.  Then compile the code using the command:
    ```bash
    javac DynamicPricingEngine.java
    ```
3.  **Run:** After successful compilation, run the code using:
    ```bash
    java DynamicPricingEngine
    ```

You will see the output of the simulation in the console. The output will show the demand changes, competitor prices and the resulting adjusted prices calculated by the engine.
👁️ Viewed: 5

Comments