Skip to content
Open

Hw-1 #23

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.

3 changes: 2 additions & 1 deletion .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.

1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
kotlin.code.style=official
org.gradle.java.home=/home/karassad/android-studio/android-studio/jbr
11 changes: 10 additions & 1 deletion src/main/kotlin/orders/Order.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,24 @@ class Order(
*/
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 product = _products.find {it.id == productId}
product?.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 }
}

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

/**
Expand All @@ -49,5 +56,7 @@ class Order(
*/
fun cancel(reason: String?) {
// TODO: set status to Cancelled with reason (default "Unknown reason" if null)
val cancel_reason = reason ?: "Unknown reason"
status = OrderStatus.Cancelled(cancel_reason)
}
}
14 changes: 13 additions & 1 deletion src/main/kotlin/orders/OrderExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,19 @@ package orders
*/
fun Order.applyDiscount(
discountPercent: Int,
logger: ((String) -> Unit)? = null
logger: ((String) -> Unit)? = null //необязательный параметр
) {
// TODO: apply discount to each product using extension + scoped functions
val allProducts = products.toList()

allProducts.forEach { product ->
val apdatedPrice = product.price * (100-discountPercent)/100

val apdatedProduct = product.copy(price = apdatedPrice)

removeProductById(product.id)
addProduct(apdatedProduct)

logger?.invoke("Product ${product.name} discounted from ${product.price} to ${apdatedPrice}")
}
}
7 changes: 6 additions & 1 deletion src/main/kotlin/orders/OrderProcessor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@ package orders
*/
fun processOrder(order: Order): String {
// TODO: use when to return the appropriate string
return ""
return when(val order_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: ${order_status.reason}"
}
}