代码的目的:购物系统,该功能显示仓库中匹配的产品名称
- 什么是没有如果不是,则为Random.nextInt()的范围。被分配在()内部?
- 在好玩的fillWarehouse中,如果我没有设置no。在“ StockUnit(Random.nextInt(),Random.nextInt())”内部,当我在主目录中调用println(“项数:$ {p.availableItems}”)时,生成了-890373473 / 1775292982等。
- 如果我在里面设置100,例如“ StockUnit(Random.nextInt(100),Random.nextInt(100))”,则会生成编号263/199等。为什么不在0-100之间?我可以知道如何更改代码,以使“项数”在100以内吗?
- 我应该为编写更好的代码而工作的任何链接或主题?
i cannot find the answers from https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.random/
Sincere thanks!
fun main(args: Array<String>) {
val warehouse = Warehouse()
...
println("Show info")
showInfo(warehouse)
}
fun showInfo(warehouse: Warehouse) {
println("Get Info")
val input = readLine() ?: "-"
val p = warehouse.getProductByName(input)
if (p != null) {
println("Product: $p")
println("Number of items: ${p.availableItems}")
println("Profit: ${p.profitPerItem}")
}
}
class Warehouse {
private val products = mutableListOf<Product>()
...
fun getProductByName (productName: String): Product? {
for (prod in products)
if (prod.productName == productName) return prod
return null
}
fun fillWarehouse (productName: String,
basePrice: Double,
productDescription: String,
chargeOnTop: Double = 50.0,
intialStockUnits: Int = 3) {
val newProduct = Product(productName, basePrice, basePrice * (1 + chargeOnTop / 100), productDescription)
//add quantity, daysBeforeExpiration
for (i in 1 .. intialStockUnits){
val unit = StockUnit(Random.nextInt(),Random.nextInt() )
newProduct.addStock(unit)
}
open class Product(
val productName: String,
var basePrice: Double,
open val salesPrice: Double,
val description: String) {
...
var stockUnits = mutableListOf<StockUnit>()
...
// availableItems = Total of stockUnits
var availableItems: Int = 0
get() = stockUnits.sumBy { it.quantity }
}
class StockUnit(var quantity:Int, var daysBeforeExpiration:Int){
...
}