//file setOfLoans.h

class Loan;

class SetOfLoans {


public:
	SetOfLoans(int initial_size = 4) {
		elements = new Loan*[initial_size];
		capacity = initial_size;
		numberOfElements = 0;
		index = 0;
	}
	~SetOfLoans(void){
		delete [] elements;
	}
	void add(Loan & element){ //store element in the set
		//numberOfElements++;
		if(numberOfElements == capacity) {  //grow the container if container is full
			capacity *= 2;
            cout << "\n\tExpanding set capacity to " << capacity;
            Loan** temp = elements;
            elements = new Loan*[capacity];
            for(int i=0; i<capacity; i++)
				elements[i] = temp[i];
            delete [] temp;
        }
		elements[numberOfElements++] = &element;
		//printOn(cout);
	}
	Loan & someElement() const {	// return an element from the set but don't remove it
		if(index >= numberOfElements)
			index = 0;
		Loan* anElement = elements[index];
		index++;
		return *anElement;
	}
	Loan & removeSomeElement(){	// return an element from the set and remove it from the set
		//Loan* tempElement = elements[numberOfElements--];
		if(index>=numberOfElements)
			index = 0;
		Loan* tempElement = elements[index];
		numberOfElements--;
		for(int i = index; i < numberOfElements; i++){
			elements[i] = elements[i+1];
		}
		if(numberOfElements < capacity/4) {  //shrink the container if set is less than a quarter full
			capacity/=2;
			cout << "\n\tDecreasing set capacity to " << capacity;
			/*Loan** temp = new Loan*[capacity];
			for(int i=0; i<numberOfElements; i++)
				temp[i] = elements[i];*/
			Loan** temp = elements;
			elements = new Loan*[capacity];
			for(int i=0; i<numberOfElements ; i++)
				elements[i] = temp[i];
			delete [] temp;
		}
		//delete elements[numberOfElements];
		return *tempElement;
	}
	int size() const {		//answer the number of elements in the set, not just the size of the array
		return numberOfElements;
	}
	void printOn(ostream & ostr) const {	//print the elements of the set
		for(int i=0; i<numberOfElements; i++){
			ostr << "\n" << *(elements[i]);
		}
	}

	int getIndex(){ return index;}

private:
	Loan** elements;
	int numberOfElements;	//number of elements in the set
	int capacity;			//size of the available array memory
	mutable int index;				//used to help with the iteration

};

ostream & operator<<(ostream & ostr, const SetOfLoans & s) {
     s.printOn(ostr);
     return ostr;
}