#include<iostream>
#include<string.h>
#include<conio.h>
#include<math.h>
#include<fstream>
using namespace std;

ifstream fin;
ofstream fout;
fstream fio;

class StockInfo
{
	protected:
		string stockname, code;
		double price;
		int amount;
		
	public:
		void display();
		
		void input();//input name, price, amount
		bool checking(string);
		void show();
		//void get2();
		
		void setCode();
		void AddNewProduct();//front+end
		void Withdraw();//backend
		void Refill(); //backend
		
		void TakeIn(int);//Refill//front
		void TakeOut(int);//front
		void DeleteProduct();	//remove	//front+end		
};

void StockInfo::display() //displaylay
{
	int i=1;
	
	cout<<"\n==================================================================";
	cout<<"\n\n=================\tTHE STOCK ITEMS ARE\t==================";
	cout<<"\n\n==================================================================\n";
	cout<<"\n\nPARTICULARS\tSTOCK AVAILABLE\t\t\t PRICE";
	cout<<"\n\n============================================================\n";	
	
	fin.open("shop.dat",ios::binary);
	 
    while(!fin.eof())
    {
    	//(char*) is the static conversion of datatype to pointer char type
		fin.read( (char*)&(stockname), sizeof(stockname) );//the method to read file data in ios::binary
		fin.read( (char*)&(code), sizeof(code) );
		fin.read( (char*)&(price), sizeof(price) );
		fin.read( (char*)&(amount), sizeof(amount) );
		
		if(!fin.eof())
		{
	    	if(fin.tellg() < 0)//refer to Part0
	    	{
				i=0;
				break;
			}
	    	show();//????still a mystery
	  	}
    }
    
    if(i == 0)
    {	
		cout<<"\n\n\t\t\t!!Empty record room!!";
		getch();//Purpose: Let users hit any random button, then the programm will jump to another page
    }
    
    fin.close();  
}

void StockInfo::input()
{
	getline(cin, stockname);
	cin >> price;
	cin >> amount;
}

bool StockInfo::checking(string name)
{
	if (stockname == name)
		return false;
	else
		return true;
}

void StockInfo::show()
{
	cout << "\n" << stockname << "\t\t\t" << amount << "\t\t\t" << price;
}

void StockInfo::setCode()
{
	int total=0;
	
	for (int i = 0; i < stockname.size() ; i++)
		total = total + int(stockname[i]);
	
	cout << total <<endl;

    cout << hex << total << endl;
}

void StockInfo::TakeOut(int quantity)//front
{
	if(amount >= quantity)
	{
		amount -= quantity;
		cout << "\n\nStock updated.\n";
		cout << "\n\nTotal price to be paid:" << price*quantity;
    }
	else 
		cout << "\n\nInsufficient stock";

	getch();
}

void StockInfo::TakeIn(int quantity)
{
		amount += quantity;
		cout << "\n\nStock updated.";
		
	    getch();
}

void StockInfo::AddNewProduct()//addnew()
{
	int n;
	
	cout << "\nEnter the No. of type of Products that you wish to add: ";
    cin >> n;
    
    if (n != 0)
    {
	
		int j, l, sum=0;
		
		fout.open("shop.dat",ios::binary|ios::app);
	
		for(int i=0;i<n;i++)
		{	
			cout<<"\n\nInput the name, price and the quantity of item respectively\n\n";
	    	input();
	    	
	    	//(char*) is the static conversion of datatype to pointer char type
	    	fout.write( (char*)&stockname, sizeof(stockname) );//method to write data into file in ios::binary
	    	fout.write( (char*)&code, sizeof(code) );
			fout.write( (char*)&price, sizeof(price) );//method to write data into file in ios::binary
        	fout.write( (char*)&amount, sizeof(amount) );//method to write data into file in ios::binary
			
			cout << "\n\nitem updated";
			cin.get();
		}
    	cout<<"\n\nStock Updated!!";
    

    	fout.close();
    	
   		cin.get();//Purpose: Let users hit any random button, then the programm will jump to another page
   		
    	system("cls");
    	display();
	}
	else
	{
		fout.close();
		cin.get();
		system("cls");
		
		cout<<"\n\nNo items to be added";
	}
}

void StockInfo::Withdraw()//To withdraw 
{
	system("cls");
	
	string name;
	
	int quantity;//Product Quantity
	int i = 0;
	long Charposition = 0;//long = long int POS=position of character
	
	display();
	
	cout<<"\n\nEnter the product's name \n"<<endl;
	cin >> name;
	
	cout<<"\n\nEnter quantity: \n"<<endl;
	cin >> quantity;
	
	fio.open("shop.dat",ios::binary|ios::out|ios::in);//
	
    while(fio)//mcm !fio.eof
    {
	    Charposition=fio.tellp();//refer to part0
	    
	    //(char*) is the static conversion of datatype to pointer char type
	    fio.read( (char*)&stockname, sizeof(stockname) );//method to write data into file in ios::binary
	    fio.read( (char*)&code, sizeof(code) );
		fio.read( (char*)&price, sizeof(price) );//method to write data into file in ios::binary
        fio.read( (char*)&amount, sizeof(amount) );
	    
	    if(checking(stockname)==false)//
	    {
			TakeOut(quantity);
			
			fio.seekp(Charposition);//refer to part0
			
			//(char*) is the static conversion of datatype to pointer char type
			fio.write( (char*)&stockname, sizeof(stockname) );//method to write data into file in ios::binary
		  	fio.write( (char*)&code, sizeof(code) );
		  	fio.write( (char*)&price, sizeof(price) );
		  	fio.write( (char*)&amount, sizeof(amount) );
			
			i++;
			  
			break;
	    }
    }
     
    
    if(i != 1)
    	cout<<"\n\n!!Record not found!!";
    	
    fio.close();
    cin.get();//Purpose: Let users hit any random button, then the programm will jump to another page
    
    system("cls");
    
	display(); 
	
	getch();//Purpose: Let users hit any random button, then the programm will jump to another page
}

void StockInfo::Refill()
{
	system("cls");
	
	string temp;
	int qty;
	int i=0;
	long pos=0;
	
	display();
	
	cout<<"\n\nEnter the products name \n"<<endl;
	getline(cin, temp);
	
	cout<<"\n\nEnter quantity: \n"<<endl;
	cin>>qty;
	
	fio.open("shop.dat", ios::binary|ios::out|ios::in);
	
    while(fio)
    {
	    pos=fio.tellp();
	    
		fio.read( (char*)&stockname, sizeof(stockname) );
		fio.read( (char*)&code, sizeof(code) );
		fio.read( (char*)&price, sizeof(price) );
		fio.read( (char*)&amount, sizeof(amount) );
	    
	    if(checking(temp)==0)
	    {
			TakeIn(qty);
			
			fio.seekp(pos);
			
			fio.write( (char*)&stockname, sizeof(stockname) );
			fio.write( (char*)&price, sizeof(price) );
			fio.write( (char*)&price, sizeof(price) );
			fio.write( (char*)&amount, sizeof(amount) );
			
			i++;
			break;
	    }
    }
    if(i!=1)
    	cout<<"\n\n!!Record not found!!";

    fio.close();

    system("cls");

    cin.get();

	display(); 
	cin.get();	
}

void StockInfo::DeleteProduct()
{
	system("cls");
		
	int i=0;
    string temp;
     
	cout<<"\n\t\t\t\tDelete Record";
    cout<<"\n\nEnter the name of the product:";
     
	cin>>temp;
     
	fout.open("temp.dat",ios::binary);
    fin.open("shop.dat",ios::binary);
     
	while(!fin.eof())
    {
		fin.read( (char*)&stockname, sizeof(stockname) );
		fin.read( (char*)&code, sizeof(code) );
		fin.read( (char*)&price, sizeof(price) );
		fin.read( (char*)&amount, sizeof(amount)  );
		
		if(!fin.eof())
	    	if(checking(temp) == 0)//
	    	{
				show();
		  		cout<<"\n\n\t\tRecord deleted";
		  		i++;
	    	}
	   		else
	   		{
	   			fout.write((char*)&stockname, sizeof(stockname));
	   			fout.write((char*)&code, sizeof(code));
	   			fout.write((char*)&price, sizeof(price));
	   			fout.write((char*)&amount, sizeof(amount));
			}
				
    }
     
    if(i==0)
    	cout << "\n\n!!Record not found!!";
    
	fin.close();
    fout.close();
    
    remove("shop.dat");
    rename("temp.dat","shop.dat");
}

int main()
{
	
	return 0;
}
