Reputation: 716
I have this data class that has a function inside and I want to use the function to add element to the data class' set but I'm not sure how to call it.
This is the code:
data class House(val article: MutableSet<Article>){
//only add articles if there are less than 10
fun addArticle(article: Article): Boolean{
if(articles.size <= 10){
articles.add(article)
return true
} else {
return false
}
}
}
data class Article(val type: String,
var color: String)
val article1 = Article("bed", "black")
val article2 = Article("sheets", "white")
val articlesToAdd = House(mutableSetOf(article1, article2))
//add articles to house: THIS DOESN'T WORK
articlesToAdd.forEach{
it.house.addVehicle()
}
So, I want to be able to add articles to my house by using the addArticle function inside of the data class House but the way I've thought of doing it doesn't work. Any help would be very appreciated.
Upvotes: 1
Views: 1339
Reputation: 1869
articlesToAdd
is of type House
therefore it doesn't have a method forEach
. Here's a sample which adds one more Article
.
val article1 = Article("bed", "black")
val article2 = Article("sheets", "white")
val articles = mutableSetOf(article1, article2)
val house = House(articles)
house.addArticle( Article("bed", "red"))
Upvotes: 1