You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
2.8 KiB
139 lines
2.8 KiB
#include "Train.h"
|
|
|
|
#include "TrainExceptions.h"
|
|
|
|
// Si des passagers peuvent être à deux endroits à la fois, il va potentiellement falloir le faire descendre deux fois du train.
|
|
#define UBIQUITE
|
|
|
|
using namespace std;
|
|
|
|
Train::Train(int nbWagons)
|
|
: lesWagons{} {
|
|
//lesWagons.reserve(nbWagons);
|
|
for (int i = 1; i <= nbWagons; ++i) {
|
|
lesWagons.emplace_back(i);
|
|
}
|
|
}
|
|
|
|
Train::Train(std::initializer_list<int> listeNumerosWagons)
|
|
: lesWagons{} {
|
|
//lesWagons.reserve(listeNumerosWagons.size());
|
|
for (int numWagon : listeNumerosWagons) {
|
|
//lesWagons.push_back(new Wagon{numWagon});
|
|
lesWagons.emplace_back(numWagon);
|
|
}
|
|
}
|
|
|
|
void Train::demarrer() {
|
|
roule = true;
|
|
}
|
|
|
|
void Train::arreter() {
|
|
roule = false;
|
|
}
|
|
|
|
bool Train::isRoule() const {
|
|
return roule;
|
|
}
|
|
|
|
void Train::monterDansLeTrain(int numeroWagon, const Passager& passager) {
|
|
if (roule) {
|
|
throw RouleException{};
|
|
}
|
|
// -> map
|
|
for (Wagon& wagon : lesWagons) {
|
|
if (wagon.getNumero() == numeroWagon) {
|
|
if (!wagon.ajouter(passager)) {
|
|
throw WagonPleinException{};
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
throw WagonInexistantException{};
|
|
}
|
|
|
|
void Train::descendreDuTrain(const Passager& passager) {
|
|
if (roule) {
|
|
throw RouleException{};
|
|
}
|
|
#ifdef UBIQUITE
|
|
bool descendu = false;
|
|
for (Wagon& wagon : lesWagons) {
|
|
descendu |= wagon.enlever(passager);
|
|
}
|
|
if (!descendu) {
|
|
throw PassagerException{};
|
|
}
|
|
#else
|
|
for (Wagon& wagon : lesWagons) {
|
|
if (wagon.enlever(passager)) {
|
|
return;
|
|
}
|
|
}
|
|
throw PassagerException{};
|
|
#endif
|
|
}
|
|
|
|
void Train::deplacerAuWagonSuivant(const Passager& passager) {
|
|
if (roule) {
|
|
throw RouleException{};
|
|
}
|
|
vector<Wagon>::iterator it = lesWagons.begin();
|
|
while (it != lesWagons.end() && !it->enlever(passager)) {
|
|
++it;
|
|
}
|
|
if (it == lesWagons.end()) {
|
|
throw PassagerException{};
|
|
}
|
|
++it;
|
|
if (it == lesWagons.end()) {
|
|
throw BoutDuTrainException{};
|
|
}
|
|
if (it->estPlein()) {
|
|
throw WagonPleinException{};
|
|
}
|
|
it->ajouter(passager);
|
|
}
|
|
|
|
void Train::deplacerAuWagonPrecedent(const Passager& passager) {
|
|
if (roule) {
|
|
throw RouleException{};
|
|
}
|
|
vector<Wagon>::reverse_iterator it = lesWagons.rbegin();
|
|
while (it != lesWagons.rend() && !it->enlever(passager)) {
|
|
++it;
|
|
}
|
|
if (it == lesWagons.rend()) {
|
|
throw PassagerException{};
|
|
}
|
|
++it;
|
|
if (it == lesWagons.rend()) {
|
|
throw BoutDuTrainException{};
|
|
}
|
|
if (it->estPlein()) {
|
|
throw WagonPleinException{};
|
|
}
|
|
it->ajouter(passager);
|
|
}
|
|
|
|
int Train::monterDansLeTrainAPartirDe(int numeroWagon, const Passager& passager) {
|
|
if (roule) {
|
|
throw RouleException{};
|
|
}
|
|
vector<Wagon>::iterator it = lesWagons.begin();
|
|
while (it != lesWagons.end() && it->getNumero() != numeroWagon) {
|
|
++it;
|
|
}
|
|
if (it == lesWagons.end()) {
|
|
throw WagonInexistantException{};
|
|
}
|
|
while (it != lesWagons.end() && it->estPlein()) {
|
|
++it;
|
|
}
|
|
if (it == lesWagons.end()) {
|
|
throw BoutDuTrainException{};
|
|
}
|
|
it->ajouter(passager);
|
|
return it->getNumero();
|
|
}
|