#include "PE.h"

ParqueEstacionamento::
 ParqueEstacionamento(unsigned int lot, unsigned int n_max_clientes) : num_max_clientes(n_max_clientes), lotacao(lot) {
  *clientes = new InfoCartao[n_max_clientes]; 
  num_clientes = 0;
  vagas = lot;
}

ParqueEstacionamento::~ParqueEstacionamento() {
  delete [] clientes;
}

bool ParqueEstacionamento::novo_cliente(const string &nome) {
  if (num_clientes < num_max_clientes) {
    clientes[num_clientes].nome = nome;
    clientes[num_clientes].presente = false;
    num_clientes++;
    return true;
  } else {
    return false;
  }
}

unsigned int ParqueEstacionamento::num_lugares() const {
  return lotacao;
}

unsigned int ParqueEstacionamento::num_lugares_ocupados() const {
  return lotacao - vagas;
}

int ParqueEstacionamento::posicao_cliente(const string &nome) const {
  for (int i = 0; i < n_max_clientes; i++) {
    if (clientes[i].nome == nome) return i;
  }
  return -1;
}

bool ParqueEstacionamento::entrar(const string &nome) {
  int pos = posicao_clientes(nome);
  if (vagas > 0 && pos >= 0 && !clientes[pos].presente) {
      clientes[pos].presente = true;
      vagas--;
      return true;
  } else return false;
} 

bool ParqueEstacionamento::sair(const string &nome) {
  int pos = posicao_clientes(nome);
  if (pos >= 0 && clientes[pos].presente) {
    clientes[pos].presente = false;
    vagas++;
    return true;
  } else return false;
}

void ParqueEstacionamento::listar(ostream &os) const {
  for (int i = 0; i < n_max_clientes; i++) {
    if (clientes[i].presente) os << i << ". " << clientes[i].nome << endl;
  }
}

bool ParqueEstacionamento::operator==(const ParqueEstacionamento &pe) const {
  if (pe.num_clientes != this->num_clientes) return false;
  if (pe.lotacao != this->lotacao) return false;
  if (pe.n_max_clientes != this->n_max_clientes) return false;
  for (int i = 0; i < n_max_clientes; i++) {
    if ((pe.clientes[i].nome != this->clientes[i].nome) || (pe.clientes[i].presente != this->clientes[i].presente)) 
      return false;
  }
  return true;
} 

