Put a class in a class

Report a typo

We developed a class to account for clients, but some of the code was lost! Restore the code considering the following conditions:

  • The repository must have a method for getting the number of clients.
  • The repository should not give access to internal fields.
  • We want to see the history of changes, so when adding a new client, the old repository should remain unchanged.

The following code should work:

val alice = Client("Alice")
val bob = Client("Bob")

val repository = Repository(List(alice))

println(repository.size == 1) // true

val updatedRepository = repository.add(bob)

println(repository.size == 1) // true
println(updatedRepository.size == 2) // true

This should not compile:

repository.clients

Sample Input 1:

Alice Bob

Sample Output 1:

true
true
true
Write a program in Scala 3
class ???

class Repository(??? clients: List[???]):
def add(client: Client): Repository = ???
def size: Int = ???
___

Create a free account to access the full topic