-
Notifications
You must be signed in to change notification settings - Fork 0
/
groupoff.cc
47 lines (38 loc) · 1.74 KB
/
groupoff.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "groupoff.h"
#include "printer.h"
void Groupoff::main(){
printer.print(Printer::Kind::Groupoff, Groupoff::States::Start);
for (unsigned int i = 0; i < numStudents; i++){ // wait for all student to acquire a giftcard
_Accept(giftCard);
}
for (unsigned int i = 0; i < numStudents; i++){ // periodically create real WATCards for each student
_Accept(~Groupoff){ // loops until destructor is called or all student have a real card
break;
} _Else {
yield(groupoffDelay); // yield before creating each card
// create gift card to be sent
WATCard * card = new WATCard();
card->deposit(sodaCost);
cards.push_back(card);
// select random student to give card to
unsigned int student = mprng(numStudents - i - 1);
printer.print(Printer::Kind::Groupoff, Groupoff::States::Deposit, sodaCost);
giftcards[student].delivery(card);
// erase future from giftcards vector as the card has been sent out
giftcards.erase(giftcards.begin() + student);
} // Accept
}
printer.print(Printer::Kind::Groupoff, Groupoff::States::Finished);
} // Groupoff::main
Groupoff::Groupoff( Printer & prt, unsigned int numStudents, unsigned int sodaCost, unsigned int groupoffDelay ):
printer(prt), numStudents(numStudents), sodaCost(sodaCost), groupoffDelay(groupoffDelay){} // Groupoff::Groupoff
Groupoff::~Groupoff(){
for (unsigned int i = 0; i < cards.size(); i++){
delete cards.at(i);
}
} // Groupoff::~Groupoff
WATCard::FWATCard Groupoff::giftCard(){
WATCard::FWATCard giftcard;
giftcards.push_back(giftcard);
return giftcard;
} // Groupoff::giftCard