Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,569 questions

56,787 answers

573 users

How to declare, initialize, and print a medium complexity, varied, feature‑rich data structure in Scala

1 Answer

0 votes
import java.time.LocalDate

case class Address(
  street: String = "",
  city: String = "",
  zip: Int = 0
)

enum Status:
  case Active, Inactive, Unknown

case class Person(
  homeAddress: Address = Address(),
  favoriteNumbers: List[Int] = List.fill(5)(0),
  nickname: Option[String] = None,
  birthDate: LocalDate = LocalDate.of(1990, 1, 1),
  notes: String = "No notes",
  middleName: Option[String] = None,
  hobbies: List[String] = Nil,
  status: Status = Status.Unknown
)

def printPerson(p: Person): Unit =
  println(s"Address: ${p.homeAddress.street}, ${p.homeAddress.city} ${p.homeAddress.zip}")

  println("Favorite Numbers: " + p.favoriteNumbers.mkString(" "))

  println("Nickname: " + p.nickname.getOrElse("(none)"))

  println("Birth Date: " + p.birthDate)

  println("Notes: " + p.notes)

  println("Middle Name: " + p.middleName.getOrElse("(none)"))

  println("Hobbies: " + (if p.hobbies.nonEmpty then p.hobbies.mkString(", ") + ", " else "(none)"))

  println("Status: " + p.status.toString)

@main def run(): Unit =
  val p = Person(
    homeAddress = Address("Fifth Avenue", "New York", 423900),
    favoriteNumbers = List(7, 13, 21, 42, 99),
    nickname = Some("Orion"),
    birthDate = LocalDate.of(1026, 6, 15),
    notes = "Voss likes C/C++ and structs",
    middleName = Some("Pulse-9"),
    hobbies = List("Coding", "Dreaming", "Walking", "Sci-Fi Movies"),
    status = Status.Active
  )

  printPerson(p)


/*
run:

Address: Fifth Avenue, New York 423900
Favorite Numbers: 7 13 21 42 99
Nickname: Orion
Birth Date: 1026-06-15
Notes: Voss likes C/C++ and structs
Middle Name: Pulse-9
Hobbies: Coding, Dreaming, Walking, Sci-Fi Movies, 
Status: Active

*/

 



answered Sep 3 by avibootz

Related questions

...