/*
LAB EXERCISE
Exercise 3, page 137 
NAME   : NG JING ER
MATRIC : A19EC0115
*/

#include <iostream>
#include <exception>
using namespace std;

const int MAX=3;

class Array{
	private:
		int data[MAX];
		int count;
		
	public:
		//TASK 1
		class Full{};
		class NegativeIndex{};
		class Empty{};
		
		Array(){count=0;}
		int getCount() const{return count;}
		
		void add(int element){
		
			//TASK 2
			if(count>=MAX)
			throw Full();
			
			data[count]=element;
			count++;
		}
		void remove(){
			
			//TASK 3
			if(count==0)
			throw Empty();
			
			count--;
		}
		void displayElement(){
			int index;
			cout<<"Enter the index of the element you want to display =>";
			cin>>index;
			
			//TASK 4
			if(index<0)
			throw NegativeIndex();
			
			//TASK 5
			if(index>=count)
			throw index;
						
			cout<<"Index: "<<index<<" Element: "<<data[index]<<endl;
		}
};

int main(){
	Array a;
	
	a.add(11);
	cout<<"Number 11 has been added. Current number of element ="<<a.getCount()<<endl;
	
	a.add(22);
	cout<<"Number 22 has been added. Current number of element ="<<a.getCount()<<endl;
	
	cout<<endl;
	
	try{
		a.displayElement();
	}
	
	//TASK 6
	catch(Array::NegativeIndex){
		cout<<"Index entered is NEGATIVE !"<<endl;
	}
	
	//TASK 7
	catch(int number){
		cout<<"Index entered: "<<number<<" is invalid"<<endl;
	}
	
	
	catch(...){}
	cout<<endl;
	
	try{
		a.add(33);
		cout<<"Number 33 has been added. Current number of element ="<<a.getCount()<<endl;
		
		a.add(44);
		cout<<"Number 44 has been added. Current number of element ="<<a.getCount()<<endl;
		
	}
	
	//TASK 8
	catch(Array::Full){
		cout<<"FULL! "<<endl;
	}
	
	catch(...){}
	cout<<endl;
	
	try{
		a.remove();
		cout<<"An element has been removed. Current number of element ="<<a.getCount()<<endl;
		
		a.remove();
		cout<<"An element has been removed. Current number of element ="<<a.getCount()<<endl;
		
		a.remove();
		cout<<"An element has been removed. Current number of element ="<<a.getCount()<<endl;
		
		a.remove();
		cout<<"An element has been removed. Current number of element ="<<a.getCount()<<endl;
	}
	
	//TASK 9
	catch(Array::Empty){
		cout<<"EMPTY !"<<endl;
	}
		
	catch(...){}
	
	return 0;
	
}
