我如何为订单分析编写函数,该函数将采用不同对象数据类型的数组和 return kotlin 中的对象
How can I write a function for an order Analysis that will take array of different object data types and return an object in kotlin
请问,您如何从 kotlin 中的以下数据中获取同一日期的平均数量。
接受此输入的函数和 return 带有 {"Monday" : 3.40, "Tuesday": 4.5}
的对象以及如何将数据传递给函数的简要说明(因为它没有特定任务)将不胜感激。
mapOf(
"orderId" to 554,
"creationDate" to "2017-03-25T10:35:20",
"orderLines" to arrayOf(
mapOf("productId" to 9872, "name" to "pencil", "quantity" to 3, "unitPrice" to 3.00)
)
),
mapOf(
"orderId" to 555,
"creationDate" to "2017-03-25T11:24:20",
"orderLines" to arrayOf(
mapOf("productId" to 9872, "name" to "Pencil", "quantity" to 1, "unitPrice" to 3.00),
mapOf("productId" to 1746, "name" to "Eraser", "quantity" to 1, "unitPrice" to 1.00)
)
),
mapOf(
"orderId" to 453,
"creationDate" to "2017-03-27T14:53:12",
"orderLines" to arrayOf(
mapOf("productId" to 5723, "name" to "Pen", "quantity" to 4, "unitPrice" to 4.22),
mapOf("productId" to 9872, "name" to "Pencil", "quantity" to 3, "unitPrice" to 3.12),
mapOf("productId" to 3433, "name" to "Erasers Set", "quantity" to 1, "unitPrice" to 6.15)
)
),
mapOf(
"orderId" to 690,
"creationDate" to "2017-03-26T11:14:00",
"orderLines" to arrayOf(
mapOf("productId" to 9872, "name" to "pencil", "quantity" to 4, "unitPrice" to 3.12),
mapOf("productId" to 4098, "name" to "Marker", "quantity" to 5, "unitPrice" to 4.50)
)
)```
使用包含一堆不相关类型的 Map
s 是一个坏主意,你不应该在像 Kotlin 这样的静态类型语言中这样做(并且你失去了静态类型的很多优势如果你这样做)。拥有一堆非结构化的 Any
s,你必须将其转换为你 认为 应该在你需要使用它的时候,那是自找麻烦。
您最好改用 classes,定义您的数据:
data class OrderLine(
val productId: Int,
val name: String,
val quantity: Int,
val unitPrice: Float // *I don't recommend using floats for this*, see below
}
data class Order(
val orderId: Int,
val creationDate: String, // or you could parse it to a Date so you can work with it
val orderLines: List<OrderLine>
)
现在您可以使用数据结构创建数据,而不是随机收集可能包含或不包含有效数据的东西(Map<Any>
没有保证)
Order(
orderId = 554, // you don't need the parameter names, just using them for comparison
creationDate = "2017-03-25T11:24:20",
orderLines = listOf(
OrderLine(productId = 9872, name = "pencil", quantity = 3, unitPrice = 3.00f)
)
)
然后您就可以轻松地处理您的数据。您可以在 Order
class 上添加一个函数,为您提供每个订单的商品总数量:
Order(...) {
fun getTotalQuantity() = orderLines.sumOf(OrderLine::quantity)
// or even better, as a property
val totalQuantity: Int get() = orderLines.sumOf { it.quantity } // same as above
}
// use that to sum all the order totals
val total = orders.sumOf { it.totalQuantity }
或者你可以 运行 对整个数据集的函数
val total = orders.sumOf { order ->
order.orderLines.sumOf { orderLine -> orderLine.quantity }
}
至于价格,您不应该将浮点数和双精度值用于货币数据 - 这是不准确的,并且当您处理大量的数据时,错误可能会累积起来。在您的情况下,我不会使用 3.00
的单价,而是使用 Int
并将其称为 300(美分或其他)。这样它总是准确的数字,您可以除以 100 以用于显示目的
请问,您如何从 kotlin 中的以下数据中获取同一日期的平均数量。
接受此输入的函数和 return 带有 {"Monday" : 3.40, "Tuesday": 4.5}
的对象以及如何将数据传递给函数的简要说明(因为它没有特定任务)将不胜感激。
mapOf(
"orderId" to 554,
"creationDate" to "2017-03-25T10:35:20",
"orderLines" to arrayOf(
mapOf("productId" to 9872, "name" to "pencil", "quantity" to 3, "unitPrice" to 3.00)
)
),
mapOf(
"orderId" to 555,
"creationDate" to "2017-03-25T11:24:20",
"orderLines" to arrayOf(
mapOf("productId" to 9872, "name" to "Pencil", "quantity" to 1, "unitPrice" to 3.00),
mapOf("productId" to 1746, "name" to "Eraser", "quantity" to 1, "unitPrice" to 1.00)
)
),
mapOf(
"orderId" to 453,
"creationDate" to "2017-03-27T14:53:12",
"orderLines" to arrayOf(
mapOf("productId" to 5723, "name" to "Pen", "quantity" to 4, "unitPrice" to 4.22),
mapOf("productId" to 9872, "name" to "Pencil", "quantity" to 3, "unitPrice" to 3.12),
mapOf("productId" to 3433, "name" to "Erasers Set", "quantity" to 1, "unitPrice" to 6.15)
)
),
mapOf(
"orderId" to 690,
"creationDate" to "2017-03-26T11:14:00",
"orderLines" to arrayOf(
mapOf("productId" to 9872, "name" to "pencil", "quantity" to 4, "unitPrice" to 3.12),
mapOf("productId" to 4098, "name" to "Marker", "quantity" to 5, "unitPrice" to 4.50)
)
)```
使用包含一堆不相关类型的 Map
s 是一个坏主意,你不应该在像 Kotlin 这样的静态类型语言中这样做(并且你失去了静态类型的很多优势如果你这样做)。拥有一堆非结构化的 Any
s,你必须将其转换为你 认为 应该在你需要使用它的时候,那是自找麻烦。
您最好改用 classes,定义您的数据:
data class OrderLine(
val productId: Int,
val name: String,
val quantity: Int,
val unitPrice: Float // *I don't recommend using floats for this*, see below
}
data class Order(
val orderId: Int,
val creationDate: String, // or you could parse it to a Date so you can work with it
val orderLines: List<OrderLine>
)
现在您可以使用数据结构创建数据,而不是随机收集可能包含或不包含有效数据的东西(Map<Any>
没有保证)
Order(
orderId = 554, // you don't need the parameter names, just using them for comparison
creationDate = "2017-03-25T11:24:20",
orderLines = listOf(
OrderLine(productId = 9872, name = "pencil", quantity = 3, unitPrice = 3.00f)
)
)
然后您就可以轻松地处理您的数据。您可以在 Order
class 上添加一个函数,为您提供每个订单的商品总数量:
Order(...) {
fun getTotalQuantity() = orderLines.sumOf(OrderLine::quantity)
// or even better, as a property
val totalQuantity: Int get() = orderLines.sumOf { it.quantity } // same as above
}
// use that to sum all the order totals
val total = orders.sumOf { it.totalQuantity }
或者你可以 运行 对整个数据集的函数
val total = orders.sumOf { order ->
order.orderLines.sumOf { orderLine -> orderLine.quantity }
}
至于价格,您不应该将浮点数和双精度值用于货币数据 - 这是不准确的,并且当您处理大量的数据时,错误可能会累积起来。在您的情况下,我不会使用 3.00
的单价,而是使用 Int
并将其称为 300(美分或其他)。这样它总是准确的数字,您可以除以 100 以用于显示目的