-
Notifications
You must be signed in to change notification settings - Fork 1
/
vendingmachine.cc
95 lines (87 loc) · 2.68 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "vendingmachine.h"
#include "nameserver.h"
#include "printer.h"
#include "watcard.h"
//#include "config.h"
#include <iostream>
using namespace std;
//Set all values to default
VendingMachine::VendingMachine( Printer &prt, NameServer &nameServer, unsigned int id, unsigned int sodaCost,
unsigned int maxStockPerFlavour ){
this->print = &prt;
this->nameServer = &nameServer;
this->id = id;
this->sodaCost = sodaCost;
this->maxStockPerFlavour = maxStockPerFlavour;
exe = -1;
noBuy = false;
//Set stock to 0
for( int i = 0; i < 2; i++ ) {
stocks[i] = 0;
}
}
VendingMachine::~VendingMachine(){
}
void VendingMachine::main(){
print->print(Printer::Vending, id, 'S', sodaCost);
//Register with nameserver
nameServer->VMregister( this );
for(;;){
_Accept( VendingMachine::~VendingMachine ){
//if shutting down allow an inventory to finish
print->print(Printer::Vending, id, 'F');
if(noBuy) {
_Accept(VendingMachine::restocked);
}
return;
//if someone is refilling stock wait for finish
} or _When(noBuy) _Accept( VendingMachine::restocked ){
noBuy = false;
print->print(Printer::Vending, id, 'R');
//Otherwise allow both buy and inventory
} or _When(!noBuy) _Accept( VendingMachine::inventory){
noBuy = true;
print->print(Printer::Vending, id, 'r');
} or _When(!noBuy) _Accept( VendingMachine::buy ){
exe = -1;
unsigned int balance = card->getBalance();
unsigned int stock = stocks[flav];
//uRendezvousAcceptor();
//Check for funds and stock erros
if( balance < sodaCost ) {
exe = 1;
} else if( stock == 0 ) {
exe = 0;
} else {
//seccuessful buy
print->print(Printer::Vending, id, 'B', (int)flav, stocks[flav] - (unsigned int)1);
stocks[flav]-=1;
card->withdraw( sodaCost );
}
buyWait.signalBlock();
}
}
}
void VendingMachine::buy( Flavours flavour, WATCard &card ){
flav = flavour;
this->card = &card;
//Wait for buy to be accepted
buyWait.wait();
if( exe == 1 ) {
throw VendingMachine::Funds();
} else if( exe == 0) {
throw VendingMachine::Stock();
}
}
unsigned int* VendingMachine::inventory(){
return stocks;
}
void VendingMachine::restocked(){
}
//get cost or ID
unsigned int VendingMachine::cost() {
return sodaCost;
}
unsigned int VendingMachine::getId(){
return id;
}