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.

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.

21 changes: 19 additions & 2 deletions src/main/kotlin/orders/Order.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,30 @@ class Order(
*/
fun addProduct(product: Product?) {
// TODO: add product to _products, ignore null
if (product != null) {
_products.add(product)
}
}

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


}

/**
* Returns the total price of all products in the order.
*/
override fun calculateTotal(): Int {
// TODO: sum the prices of all products
return 0
var sum = 0
for (product in _products) {
sum += product.price
}
return sum
}

/**
Expand All @@ -41,6 +50,10 @@ class Order(
*/
fun pay() {
// TODO: throw if _products is empty, otherwise set status to Paid
if (_products.size==0){
throw IllegalStateException("empty")
}
else{status = OrderStatus.Paid}
}

/**
Expand All @@ -49,5 +62,9 @@ class Order(
*/
fun cancel(reason: String?) {
// TODO: set status to Cancelled with reason (default "Unknown reason" if null)
if (reason!=null){
status= OrderStatus.Cancelled(reason)
}
status= OrderStatus.Cancelled("Unknown reason")
}
}
12 changes: 10 additions & 2 deletions src/main/kotlin/orders/OrderExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,13 @@ fun Order.applyDiscount(
discountPercent: Int,
logger: ((String) -> Unit)? = null
) {
// TODO: apply discount to each product using extension + scoped functions
}
products.toList().forEach { product ->
removeProductById(product.id)

val discountedPrice = (product.price * (100 - discountPercent) / 100)
val discountedProduct = product.copy(price = discountedPrice)

addProduct(discountedProduct)
logger?.invoke("${product.name} ${product.price} is now $discountedPrice")
}
}
6 changes: 5 additions & 1 deletion src/main/kotlin/orders/OrderProcessor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@ package orders
*/
fun processOrder(order: Order): String {
// TODO: use when to return the appropriate string
return ""
return when (val status = order.status) {
OrderStatus.Created -> "Order ${order.id} is new"
OrderStatus.Paid -> "Order ${order.id} is paid"
is OrderStatus.Cancelled -> "Order ${order.id} is cancelled: ${status.reason}"
}
}