Skip to content

Instantly share code, notes, and snippets.

@skuro
Last active March 31, 2016 18:53
Show Gist options
  • Save skuro/f6480f7543303c1d4843ede3cf42b5b1 to your computer and use it in GitHub Desktop.
Save skuro/f6480f7543303c1d4843ede3cf42b5b1 to your computer and use it in GitHub Desktop.
package tk.skuro.scala
object Raffle extends App {
case class Attendee (name: String)
type Attendees = Array[Attendee]
case class RaffleState(participants: Attendees = new Array(0))
val initialState = new RaffleState(new Array(0))
mainMenu()
def randomNumber(max: Int) = scala.util.Random.nextInt(max)
def randomAttendee(attendees: Attendees, selector: (Int) => Int) = {
val pick = selector(attendees.length)
attendees(pick)
}
def addParticipant(state: RaffleState): Unit = {
println()
println("Enter the name of the participant:")
val name = scala.io.StdIn.readLine()
val participants = state.participants :+ new Attendee(name)
mainMenu(new RaffleState(participants))
}
def raffle(state: RaffleState): Unit = {
print("And the winner is .")
Thread.sleep(200l)
print("..")
Thread.sleep(200l)
print("..")
Thread.sleep(200l)
print("..")
val luckyOne = randomAttendee(state.participants, randomNumber)
println(s" ${luckyOne.name}!")
}
def mainMenu(state: RaffleState = new RaffleState(new Array(0))): Unit = {
println("Choose one of the following:")
println("1) Enter a participant")
println("2) Raffle!")
println()
print("> ")
val select = scala.io.StdIn.readLine()
select match {
case "1" => addParticipant(state)
case "2" => raffle(state)
case _ => {
println("Try again dude!")
mainMenu(state)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment