p-Parking-Garage / CSC330_MidtermProj_Parking / Garage.cpp
Garage.cpp
Raw
// Garage Implementation File
//

#include <iostream>
#include "Garage.h"

using namespace std;

Garage::Garage()
{
	floors = 0;
	carsperfloor = 0;
	hourlyrate = 0;
	monthlyfee = 0;
}

int Garage::maxCapacity() // our garage holds a max capacity of 80 vehicles (declared in global of Garage)
{
	return MAX_CAPACITY;
}

Garage::Garage(int inFloors, int inCarsPerFloor, int inHourlyRate, int inMonthlyFee)
{
	floors = inFloors;
	carsperfloor = inCarsPerFloor;
	hourlyrate = inHourlyRate;
	monthlyfee = inMonthlyFee;
}

void Garage::readGarage(ifstream & garFile)
{
	string line;
	int nfloor = floors;
	while (nfloor--) 
	{
		int ncars = carsperfloor;
		while (--ncars)
		{
			getline(garFile, line, ',');
			if (line[0] != ' ') 
			{
				line = " " + line;
			}
			if (*(line.end() - 1) == '*') 
			{
				line.erase(line.end() - 1);
				parkspot.push_back(make_pair(1, line));
			}
			else 
			{
				parkspot.push_back(make_pair(0, line));
			}
		}
		getline(garFile, line, '\n');
		parkspot.push_back(make_pair(0, line));
	}
}

void Garage::writeGarage(ofstream& garFile)
{
	for (int i = 0; i < floors; i++) 
	{
		for (int j = 0; j < carsperfloor; j++) 
		{
			garFile << parkspot[i * carsperfloor + j].second;
			if (parkspot[i*carsperfloor + j].first == 1) 
			{
				garFile << "*";
			}
			if (j != carsperfloor - 1)
				garFile << ",";
		}
		garFile << endl;
	}
}

string Garage::findSpot(string inType) // will send the type of user to our function which will decide where he parks 
{
	string spot;
	if (inType == "emp")
	{
		auto it = parkspot.begin();
		for (; it != parkspot.begin() + 8; it++)
		{
			if (!it->first)
			{
				it->first = 1;
				spot = it->second;
				break;
			}
		}

	}
	else if (inType == "mem")
	{
		auto it = parkspot.begin() + 8;
		for (; it != parkspot.begin() + 3 * carsperfloor; it++)
		{
			if (!it->first)
			{
				it->first = 1;
				spot = it->second;
				break;
			}
		}
	}
	else
	{
		auto it = parkspot.begin() + 3 * carsperfloor;
		for (; it != parkspot.end(); it++)
		{
			if (!it->first)
			{
				it->first = 1;
				spot = it->second;
				break;
			}
		}
	}

	return spot;
}

void Garage::clearSpot(string spot) // function works, just not implemented due to lack of time
{
	for (auto sp : parkspot)
	{
		if (sp.second == spot)
		{
			sp.first = 0;
			break;
		}
	}
}

void Garage::print() // interactive, displays up to date information of which spots are available or reserved
{
	for (int i = 0; i < floors; i++)
	{
		cout << endl << "Floor: " << i + 1 << endl;
		for (int j = 0; j < carsperfloor; j++)
		{
			cout << parkspot[i * carsperfloor + j].second;
			if (parkspot[i * carsperfloor + j].first == 0)
			{
				cout << ": Free " << "\t";
			}
			else
			{
				cout << ": Occupied " << "\t";
			}

			if ((j + 1) % 4 == 0)
			{
				cout << endl;
			}
		}
	}
}