Hotel Pricing Engine Scala

👤 Sharing: AI
```scala
import java.time.LocalDate
import java.time.temporal.ChronoUnit

object HotelPricingEngine {

  // Case class to represent a hotel room
  case class Room(roomType: String, basePrice: Double)

  // Case class to represent a booking request
  case class BookingRequest(roomType: String, checkInDate: LocalDate, checkOutDate: LocalDate, numberOfGuests: Int)

  // Function to calculate the total price for a booking
  def calculatePrice(room: Room, request: BookingRequest): Double = {
    // 1. Calculate the number of nights
    val numberOfNights = ChronoUnit.DAYS.between(request.checkInDate, request.checkOutDate)

    // 2. Apply base price
    var totalPrice = room.basePrice * numberOfNights

    // 3. Apply seasonal adjustments (example)
    if (isPeakSeason(request.checkInDate)) {
      totalPrice *= 1.2 // 20% surcharge during peak season
    }

    // 4. Apply guest surcharge (example)
    if (request.numberOfGuests > 2) {
      totalPrice += (request.numberOfGuests - 2) * 10 // Extra 10 per guest above 2
    }

    // 5. Apply discounts (example)
    if (numberOfNights > 7) {
      totalPrice *= 0.95 // 5% discount for stays longer than 7 nights
    }

    // 6. Round to 2 decimal places for currency
    BigDecimal(totalPrice).setScale(2, BigDecimal.RoundingMode.HALF_UP).toDouble
  }

  // Helper function to determine if a date is in peak season (example)
  def isPeakSeason(date: LocalDate): Boolean = {
    val month = date.getMonthValue
    month >= 6 && month <= 8 // June, July, August are considered peak season
  }

  def main(args: Array[String]): Unit = {
    // Example Usage
    val standardRoom = Room("Standard", 100.0)
    val suite = Room("Suite", 200.0)

    val bookingRequest1 = BookingRequest("Standard", LocalDate.of(2024, 7, 15), LocalDate.of(2024, 7, 20), 2) // Peak season, 5 nights, 2 guests
    val bookingRequest2 = BookingRequest("Suite", LocalDate.of(2024, 10, 1), LocalDate.of(2024, 10, 10), 4)  // Off-peak season, 9 nights, 4 guests
    val bookingRequest3 = BookingRequest("Standard", LocalDate.of(2024, 2, 1), LocalDate.of(2024, 2, 5), 1) // Off-peak season, 4 nights, 1 guest


    val price1 = calculatePrice(standardRoom, bookingRequest1)
    val price2 = calculatePrice(suite, bookingRequest2)
    val price3 = calculatePrice(standardRoom, bookingRequest3)

    println(s"Booking Request 1 (Standard Room): $price1")
    println(s"Booking Request 2 (Suite): $price2")
    println(s"Booking Request 3 (Standard Room): $price3")
  }
}
```

Key improvements and explanations:

* **Clear Structure:** Uses case classes for `Room` and `BookingRequest`, making the code more readable and maintainable.
* **LocalDate:**  Uses `java.time.LocalDate` for date handling, which is the recommended approach in modern Java/Scala.  This avoids using deprecated classes. The dates are formatted consistently.
* **Error Handling (Removed):**  Removed the date parsing error handling to keep the example concise. In a real application, you would *absolutely* need robust error handling for invalid date formats.
* **Price Calculation Logic:** The `calculatePrice` function clearly outlines the steps involved in determining the final price:
    * Calculates the number of nights.
    * Applies the base price.
    * Applies seasonal adjustments.
    * Applies guest surcharges.
    * Applies discounts.
    * Rounds the price to two decimal places.
* **Peak Season Logic:** The `isPeakSeason` function provides a simple example of how to determine if a date falls within a peak season.  This can be easily customized.
* **Guest Surcharge:**  Added a guest surcharge example.
* **Long Stay Discount:** Added a long stay discount example.
* **Rounding:** Uses `BigDecimal` and `setScale` to ensure accurate rounding to two decimal places for currency values.  This is crucial to avoid floating-point precision issues. `BigDecimal.RoundingMode.HALF_UP` is used for standard rounding.
* **Example Usage:** The `main` function demonstrates how to create `Room` and `BookingRequest` instances and use the `calculatePrice` function.  Includes multiple booking request examples to show different scenarios.
* **Concise and Readable:** The code is written in a clear and concise style, following Scala best practices.  Uses `s-strings` for easy string interpolation.
* **Explanations within the Code:** Comments within the code explain the purpose of each step.

How to Run the Code:

1. **Save:** Save the code as `HotelPricingEngine.scala`.
2. **Compile:** Open a terminal or command prompt and navigate to the directory where you saved the file. Compile the code using the Scala compiler:
   ```bash
   scalac HotelPricingEngine.scala
   ```
3. **Run:**  Execute the compiled code using the Scala interpreter:
   ```bash
   scala HotelPricingEngine
   ```

This will print the calculated prices for the example booking requests to the console.

Key improvements and considerations for a real-world application:

* **Database Integration:** Store room information, pricing rules, and booking data in a database (e.g., PostgreSQL, MySQL).
* **Configuration:** Load pricing rules and other configuration parameters from a configuration file (e.g., YAML, JSON) instead of hardcoding them.
* **Advanced Pricing Algorithms:** Implement more sophisticated pricing algorithms, such as dynamic pricing based on demand, competitor pricing, and other factors.
* **Availability Checking:** Integrate with an availability management system to ensure that rooms are available before confirming a booking.
* **API Integration:** Expose the pricing engine as an API so that it can be integrated with other systems, such as booking websites and mobile apps.
* **Testing:** Write unit tests and integration tests to ensure that the pricing engine is working correctly.
* **Error Handling:** Implement robust error handling to handle invalid input, database connection errors, and other potential issues.
* **Concurrency:**  Consider concurrency if the pricing engine will be handling a large number of requests concurrently.  Use appropriate concurrency mechanisms (e.g., Actors, Futures) to ensure that the engine can handle the load.
* **Scalability:** Design the pricing engine to be scalable so that it can handle increasing demand.  Consider using a distributed architecture.
* **Caching:** Implement caching to improve performance by storing frequently accessed data in memory.  Consider using a distributed cache (e.g., Redis, Memcached) for scalability.
* **Monitoring:** Implement monitoring to track the performance of the pricing engine and identify potential issues.

This example provides a solid foundation for building a more complex hotel pricing engine in Scala.  Remember to tailor the code to your specific requirements and follow best practices for software development.
👁️ Viewed: 5

Comments