Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

123 changes: 123 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions .idea/deploymentTargetSelector.xml

This file was deleted.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/markdown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ plugins {
kotlin("jvm") version "2.1.0"
}

kotlin {
jvmToolchain(21)
}

group = "orders"
version = "1.0"

Expand Down
20 changes: 14 additions & 6 deletions src/main/kotlin/orders/Order.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,45 @@ class Order(
* If the product is null, it should be ignored.
*/
fun addProduct(product: Product?) {
// TODO: add product to _products, ignore null
product?.let {
_products.add(it)
}
}

/**
* Removes the first product matching [productId].
*/
fun removeProductById(productId: Int) {
// TODO: remove product from _products by id
val productToRemove = _products.find { it.id == productId }
productToRemove?.let {
_products.remove(it)
}
}

/**
* Returns the total price of all products in the order.
*/
override fun calculateTotal(): Int {
// TODO: sum the prices of all products
return 0
return _products.sumOf { it.price }
}

/**
* Marks the order as paid.
* Throws [IllegalStateException] if the order has no products.
*/
fun pay() {
// TODO: throw if _products is empty, otherwise set status to Paid
if (_products.isEmpty()) {
throw IllegalStateException("Cannot pay for an empty order")
}
status = OrderStatus.Paid
}

/**
* Cancels the order with the given reason.
* If [reason] is null, use "Unknown reason".
*/
fun cancel(reason: String?) {
// TODO: set status to Cancelled with reason (default "Unknown reason" if null)
val finalReason = reason ?: "Unknown reason"
status = OrderStatus.Cancelled(finalReason)
}
}
14 changes: 12 additions & 2 deletions src/main/kotlin/orders/OrderExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,15 @@ fun Order.applyDiscount(
discountPercent: Int,
logger: ((String) -> Unit)? = null
) {
// TODO: apply discount to each product using extension + scoped functions
}
val currentProducts = products.toList()
for (product in currentProducts) {
val discountAmount = (product.price * discountPercent) / 100
val newPrice = product.price - discountAmount
val discountedProduct = product.copy(price = newPrice)
discountedProduct.also {
logger?.invoke("Discount applied for ${it.name}: ${product.price} -> ${it.price}")
}
removeProductById(product.id)
addProduct(discountedProduct)
}
}
9 changes: 6 additions & 3 deletions src/main/kotlin/orders/OrderProcessor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ package orders
* - Cancelled -> "Order {id} is cancelled: {reason}"
*/
fun processOrder(order: Order): String {
// TODO: use when to return the appropriate string
return ""
}
return when (val status = order.status) {
is OrderStatus.Created -> "Order ${order.id} is new"
is OrderStatus.Paid -> "Order ${order.id} is paid"
is OrderStatus.Cancelled -> "Order ${order.id} is cancelled: ${status.reason}"
}
}