Skip to content
Open

hw1 #35

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.

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.

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

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

/**
* 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() {
if(products.isNotEmpty()){
status = OrderStatus.Paid
}
else{
throw IllegalStateException("Order has no products")
}
// TODO: throw if _products is empty, otherwise set status to Paid
}

Expand All @@ -48,6 +57,7 @@ class Order(
* If [reason] is null, use "Unknown reason".
*/
fun cancel(reason: String?) {
status = OrderStatus.Cancelled(reason = reason ?: "Unknown reason")
// TODO: set status to Cancelled with reason (default "Unknown reason" if null)
}
}
6 changes: 6 additions & 0 deletions src/main/kotlin/orders/OrderExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,11 @@ fun Order.applyDiscount(
discountPercent: Int,
logger: ((String) -> Unit)? = null
) {
products.forEach { product ->
removeProductById(product.id)
val discountedProduct = product.copy(price = product.price * (100 - discountPercent) / 100)
addProduct(discountedProduct)
logger?.invoke("${product.price}")
}
// TODO: apply discount to each product using extension + scoped functions
}
7 changes: 5 additions & 2 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(order.status){
is OrderStatus.Cancelled -> "Order ${order.id} is cancelled ${(order.status as OrderStatus.Cancelled).reason}"
OrderStatus.Created -> "Order ${order.id} is created"
OrderStatus.Paid -> "Order ${order.id} is paid"
}
}