-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtruck.cc
executable file
·68 lines (60 loc) · 2.26 KB
/
truck.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 "truck.h"
#include <iostream>
#include <fstream>
void Truck::main() {
printer.print(Printer::Kind::Truck, 'S');
try {
VendingMachine ** vms = nameServer.getMachineList();
for (;;) {
std::ofstream test_out{"t.out", std::ios::app};
yield(prng(1, 10));
plant.getShipment(cargo);
int inventoryCount = 0;
for (int i = 0; i < NUM_OF_FLAVOURS; ++i) {
inventoryCount += cargo[i];
}
printer.print(Printer::Kind::Truck, 'P', inventoryCount);
for (int i = 0; i < numVendingMachines; i++) {
int totalBottlesMissing = 0;
printer.print(Printer::Kind::Truck, 'd', restockIndex, inventoryCount);
unsigned int * inventory = vms[restockIndex]->inventory();
for (int j = 0; j < NUM_OF_FLAVOURS; j++) {
if (cargo[j] > 0 && inventory[j] < maxStockPerFlavour) {
int newInventory = std::min(inventory[j] + cargo[j], maxStockPerFlavour);
totalBottlesMissing += maxStockPerFlavour - newInventory;
int bottlesUnloaded = newInventory - inventory[j];
inventoryCount -= bottlesUnloaded;
cargo[j] -= bottlesUnloaded;
inventory[j] = newInventory;
}
}
if (totalBottlesMissing > 0) {
printer.print(Printer::Kind::Truck, 'U', restockIndex, totalBottlesMissing);
} else {
printer.print(Printer::Kind::Truck, 'D', restockIndex, inventoryCount);
}
if (prng(100) == 0) {
printer.print(Printer::Kind::Truck, 'X');
yield(10);
}
vms[restockIndex]->restocked();
restockIndex = (restockIndex + 1) % numVendingMachines;
if (inventoryCount == 0) {
break;
}
}
}
} catch(BottlingPlant::Shutdown e) {}
std::ofstream test_out{"t.out", std::ios::app};
printer.print(Printer::Kind::Truck, 'F');
}
Truck::Truck( Printer & prt, NameServer & nameServer, BottlingPlant & plant,
unsigned int numVendingMachines, unsigned int maxStockPerFlavour ) :
printer{prt}, nameServer{nameServer}, plant{plant},
numVendingMachines{numVendingMachines}, maxStockPerFlavour{maxStockPerFlavour} {
cargo = new unsigned int[numVendingMachines];
restockIndex = 0;
}
Truck::~Truck() {
delete [] cargo;
}