-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvendingmachine.cc
executable file
·68 lines (61 loc) · 1.89 KB
/
vendingmachine.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <uPRNG.h>
#include <iostream>
#include <fstream>
#include "bottlingplant.h"
#include "vendingmachine.h"
#include "nameserver.h"
void VendingMachine::main() {
printer.print(Printer::Kind::Vending, id, 'S', sodaCost);
for (;;) {
try {
_Accept(~VendingMachine) {
break;
} or _Accept(inventory) {
printer.print(Printer::Kind::Vending, id, 'r');
_Accept(restocked);
printer.print(Printer::Kind::Vending, id, 'R');
} or _Accept(buy);
} catch( uMutexFailure::RendezvousFailure & ) {
// XXX
}
}
printer.print(Printer::Kind::Vending, id, 'F');
}
VendingMachine::VendingMachine( Printer & prt, NameServer & nameServer, unsigned int id, unsigned int sodaCost ) :
printer{prt}, id{id}, sodaCost{sodaCost}
{
nameServer.VMregister(this);
currentInventory = new unsigned int[NUM_OF_FLAVOURS];
for (int i = 0; i < NUM_OF_FLAVOURS; i++) {
currentInventory[i] = 0;
}
}
VendingMachine::~VendingMachine() {
delete [] currentInventory;
}
void VendingMachine::buy( BottlingPlant::Flavours flavour, WATCard & card ) {
std::ofstream test_out{"t.out", std::ios::app};
if (card.getBalance() < sodaCost) {
_Throw Funds();
}
if (currentInventory[flavour] == 0) {
_Throw Stock();
}
currentInventory[flavour]--;
if (prng(1,5) == 1) { // 1 in 5 chance the soda is free
printer.print(Printer::Kind::Vending, id, 'A');
_Throw Free();
}
card.withdraw(sodaCost);
printer.print(Printer::Kind::Vending, id, 'B', flavour, currentInventory[flavour]);
}
unsigned int * VendingMachine::inventory() {
return currentInventory;
}
void VendingMachine::restocked() {}
_Nomutex unsigned int VendingMachine::cost() const {
return sodaCost;
}
unsigned int VendingMachine::getId() const {
return id;
}