---
# System prepended metadata

title: Question

---

# Question
Implement the function `retrieveAddress` using the methods `getStreet1`, `getStreet2`, and `getStreet3` to create an `Address`.
```scala
case class Address(
  street1: String,
  street2: Option[String],
  street3: Option[String]
)

sealed trait AddressError
case class MainStreetError(desc: String) extends AddressError
case class AdditionalStreetLine1Error(desc: String) extends AddressError

def retrieveAddress : Either[AddressError, (List[AddressError],Address)] = {
    val mainStreet: Either[AddressError, String] = 
        Try(getStreet1)
            .toEither
            .mapLeft(AddressError(_.getMessage()))
    
    val additionalStreetLine1 = Try(getStreet2).toOption
    val additionalStreetLine2 = Try(getStreet3).toOption
    
    mainStreet.map(m => Address(m, additionalStreetLine1, additionalStreetLine2)) 

    for {
        street1 <- Try(getStreet1).toEither                      
            .mapLeft(AddressError(_.getMessage()))
        additionalStreetLine1 = Try(getStreet2).toOption
        additionalStreetLine2 = Try(getStreet3).toOption
    } yield Address(street1, additionalStreetLine1, additionalStreetLine2)


    // MINE
    val additionalStreetLine1 = Try(getStreet2).toOption
    val additionalStreetLine2 = Try(getStreet3).toOption
    Try(getStreet1) match {
        case Success(m) => Right(Address(m, additionalStreetLine1, additionalStreetLine2))
        case Failure(e) => Left(AddressError(e.getMessage()))
    }
}
// =======================================================================  
// LIBRARY METHODS 
// =======================================================================
/**
  * Retrieves the main street address.
  * @throws RuntimeException if empty
  */
def getStreet1: String = {
  // implementation
}
/**
  * Retrieves additional street address line 2.
  * @throws RuntimeException if empty
  */
def getStreet2: String = {
  // implementation
}
/**
  * Retrieves additional street address line 3.
  * @throws RuntimeException if empty
  */
def getStreet3: String = {
  // implementation
}