如何創建一個庫存類,該庫有兩個方法(加減法),它將一個金額加上或減去一個Item類,並返回一個新的Item,其計數調整正確?如何用包含2個方法的類修改現有的類?
語言:斯卡拉
什麼代碼應該做的:
val Shirts = Item("Hanes", 12)
val Inv = new Inventory
scala> Inv.subtract(5, Shirts)
output: Item(Hanes,7)
scala> Inv.add(5, Shirts)
output: Item(Hanes,17)
的代碼,我有:
case class Item(val brand: String, val count: Int)
class Inventory {
def add(amount:Int):Int={
count+=amount
}
def subtract(amount:Int):Int={
count-= amount
}
}
注:我無法弄清楚如何修改項目類庫存類包含2個方法。任何幫助表示讚賞。
Scott,您必須將所有代碼放在代碼語法高亮顯示中。請修復。 –
Ashwin,原諒我,我還在學習。它看起來像有人修復它,我會在下一次這樣做。 –
不,這都是好人!只是讓你知道,所以回答你的問題更容易。 –