Created
September 15, 2015 12:21
-
-
Save butlermatt/eb3258f89c70e7ad116f to your computer and use it in GitHub Desktop.
Quick and dirty lottery draw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math'; | |
void main() { | |
var tm = new TicketManager(); | |
tm.buyTicket('WeyLand-Yutani', 50000); | |
tm.buyTicket('srolls', 10000); | |
tm.drawTicket(); | |
} | |
class TicketManager { | |
num curTicket; | |
List<Ticket> tickets; | |
TicketManager() { | |
tickets = new List<Ticket>(); | |
curTicket = 1; | |
} | |
void buyTicket(String name, num quanity) { | |
var tick = new Ticket(name, curTicket, curTicket + quanity); | |
curTicket += quanity; | |
tickets.add(tick); | |
} | |
void drawTicket() { | |
var rand = new Random(); | |
var myNum = rand.nextInt(curTicket - 1); | |
myNum += 1; | |
print('Ticket: $myNum'); | |
var winner = tickets.firstWhere((el) => el.hasTicket(myNum)); | |
print('Winner: ${winner.name}'); | |
} | |
} | |
class Ticket { | |
String name; | |
num start; | |
num end; | |
Ticket(this.name, this.start, this.end); | |
bool hasTicket(num winner) => start <= winner && winner <= end; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment