// Cyber Cafe Management System C++ Project
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include <cstdlib>
#include <windows.h> //delay
#include <cstring>
#include <ctime>
using namespace std;

class account_query //binary file
{
	private:
		int id;
		char first_name[10];
		char last_name[10];
		char address[35];
		char email[35];
		char phone[14];
		char renewal_date[10];
		char code [20];
		char processor [40];
		char motherboard [40];
		char memory [40];
		
	public:
		string date() {return renewal_date;}
		int get_id() {return id;}
		void new_member();
		void account();
		void account2();
		void show_data();
		void read_rec();
		void update_rec();
		void delete_rec();
		void search_rec();	
		void computer();
		void new_computer();
		void show_computer();
		void read_computer();
		void update_computer();
		void delete_computer();
		void search_computer();
		void renewal();
		void membership();
};

struct comp //declaration variable
{
	char code [20];
	char processor [40];
	char motherboard [40];
	char memory [40];
}c;

void account_query::account() //caling functions
{
	cout << "ID Number    => ";
	cin >> id;
	cout << "First Name   => ";
	cin >> first_name;
	cout << "Last Name    => ";
	cin >> last_name;
	cout << "Street Name  => " ;
	cin.ignore(1);  //ignore first byte
	cin.getline(address,35); //streamsize is 35
	cout << "Phone Number => ";
	cin >> phone;
	cout << "Email        => ";
	cin >> email;
	cout << "Renewal Date => ";
	cin.ignore(1);
	cin.getline(renewal_date,10);
	cout << endl;
}

void account_query::account2() //caling functions
{
	cout << "ID Number    => ";
	cin >> id;
	cout << "First Name   => ";
	cin >> first_name;
	cout << "Last Name    => ";
	cin >> last_name;
	cout << "Street Name  => " ;
	cin.ignore(1);  //ignore first byte
	cin.getline(address,35); //streamsize is 35
	cout << "Phone Number => ";
	cin >> phone;
	cout << "Email        => ";
	cin >> email;
	
}

void account_query::membership()
{
	cout << "Renewal Date    => ";
	cin.ignore(1);
	cin.getline(renewal_date,10);
	cout << endl;
}

void account_query::show_data()
{		
		cout << id;
		cout << "      Name         => "       << first_name << " " << last_name << endl;
		cout << "            Address      => " << address << endl;
		cout << "            Phone Number => " << phone << endl;
		cout << "            Email        => " << email << endl;
		cout << "            Renewal Date => " << renewal_date << endl;
		cout << "\n";		
}

void account_query::new_member()
{
	ofstream outfile;
	outfile.open("AceRecord", ios::binary|ios::app); //open file in binary mode and append mode(cause all output to that file to be appended to the end (end of file)
	account();
	outfile.write((char*)this,sizeof(*this)); 	//outfile.write(buffer,size)   to create buffer: (char*)&obj,sizeof(obj)
										  		// this pointer is point to member function in class mode
	outfile.close();
}

void account_query::read_rec()
{
	ifstream infile;
	infile.open("AceRecord",ios::binary); 
	if(!infile)
	{
		cout << "Error in Opening!! File Not Found" << endl;
		return;
	}
	cout << "\n";
	cout << "------------------DATA FROM FILE------------------" << endl;
	cout << "\n";
	cout << "ID Number" << "              Ace's Member              "<<endl;
	while(!infile.eof()) // using bool: eof will give true value (bool=1) is there are no data in that file 
	{
		if(infile.read((char*)this,sizeof(*this))) //infile.read(buffer,size)
		{
			cout << "\n";
			show_data();
		}
	}
	infile.close();
}

void account_query::update_rec()
{
	account_query a;
	int number_id;
	fstream iofile;
	iofile.open("AceRecord", ios::binary | ios::in | ios::out);
	if(!iofile)
	{
		cout << "Error in Opening!! File Not Found" << endl;
		return;
	}
	iofile.seekg(0, ios::end); //move read position to the end of file
	int total = iofile.tellg()/sizeof(account_query);
	iofile.tellg(); //get posiiton in output mode and return "put"
	cout << "There are " << total << " Record[s] in this file";
	cout << "\n\n";
	read_rec();
	cout << "\n";
	cout << "Enter Record to Edit => ";
	cin >> number_id ;
	cout << "\n";
	iofile.seekg(0); //move read position must enter a value cause u playing with byte
    while(iofile.read((char *)&a,sizeof(a)))
    {
   	 if(a.get_id() == number_id)
   	 {
   		cout<<"Record "<< number_id <<" has following data"<<endl;
   		a.show_data();
   		iofile.seekp(0);
   		cout<<"\nEnter data to Modify "<<endl;
   		a.account2();
   		iofile.write((char *)&a,sizeof(a));
		break;
	 } 
	}
	
	iofile.close();	
}
void account_query::delete_rec()
{
	account_query a;
	int number_id;
	int flag; // determine the next process when previous line is executed 
	string ref;
	ifstream infile;
	infile.open("AceRecord",ios::binary);
	if(!infile)
	{
		cout << "Error in Opening!! File Not Found" << endl;
		return;
	}
	infile.seekg(0,ios::end);
	int total;
	total = infile.tellg()/sizeof(account_query);
	cout << "There are " << total << " Record[s] in this file";
	cout << "\n";
	read_rec();
	cout << "\n";
	cout << "Enter ID Number to Delete => ";
	cin >> number_id;
	infile.seekg(0);
	while (infile.read((char *)&a,sizeof(a)))
	{
		if(a.get_id() == number_id)
		{
			a.show_data();
			break;
		}
	}
	ofstream tmpfile;
	tmpfile.open("tmpfile", ios::binary|ios::out);
	infile.seekg(0);
	while (infile.read((char *)&a,sizeof(a)))
	{
		if(a.get_id() != number_id)
		{
			
			cout << "Sure To Delete This Record " << endl;
			cout << "\n";
			cout << "[Y] for yes and [N] for no " << endl;
			cout << "=> ";
			cin >> ref;
			if(ref=="Y" || ref=="y")
			{
				tmpfile.write((char *)&a, sizeof (a));
			}
			if(ref=="N" || ref=="n")
			{
				break;
			}
			
		}
	}
	infile.close();
	tmpfile.close();
	remove ("AceRecord");
	rename ("tmpfile","AceRecord");
}

void account_query::search_rec()
{
	account_query a;
	int number_id;
	int flag;
	fstream infile;
	infile.open("AceRecord", ios::binary | ios::in);
	if(!infile)
	{
		cout<<"\nError in opening! File Not Found!!"<<endl;
        return;
	}
	infile.seekg(0,ios::end);
    int count = infile.tellg()/sizeof(*this);
    cout<<"There Are "<<count<<" Record[s] In This File"<<endl;
    read_rec();
    cout << "------------------------------------------------" << endl;
    cout << "Enter ID Number to Search => ";
	cin>>number_id;
	cout << "\n";
 	infile.seekg(0);
 	while(infile.read((char*)&a,sizeof(a)))
 	{
 		if(a.get_id()==number_id)
 		{
 			a.show_data();
 			break;
 			
		}
		
	}
    infile.close();
}

void account_query::computer()
{
	cout << "Computer Code       => ";
	cin.ignore(1);
	cin.getline(c.code,20);
	cout << "Processor Model     => ";
	cin.ignore(0);
	cin.getline(c.processor,40);
	cout << "Motherboard Company => ";
	cin.ignore(0);
	cin.getline(c.motherboard,40);
	cout << "Memory Capacity     => ";
	cin.ignore(0);
	cin.getline(c.memory,40);
	cout << endl;
}

void account_query::new_computer()
{
	ofstream infile;
	infile.open("ComputerRecord", ios::binary | ios::app);
	computer();
	infile.write((char*)&c,sizeof (c));
	infile.close();
}

void account_query::show_computer()
{
	cout << "Computer Code       => " << c.code        << endl;
	cout << "Processor Model     => " << c.processor   << endl;
	cout << "Motherboard Company => " << c.motherboard << endl;
	cout << "Memory Company      => " << c.memory      << endl;
}

void account_query::read_computer()
{
	ifstream infile;
	infile.open("ComputerRecord", ios::binary);
	{
		if(!infile)
		{
			cout << "Error in Opening!! File Not Found" << endl;
			return;
		}
		cout << "\n";
		infile.seekg(0,ios::end);
		int tell = infile.tellg()/sizeof(c);
		infile.tellg();
		cout << "There are " << tell << " Record[s] in this file";
		cout << "\n\n";
		cout << "------------------DATA FROM FILE------------------" << endl;
		cout << "\n";
		infile.seekg(0);
		while(!infile.eof())
		{
			if(infile.read((char*)&c,sizeof(c)))
			{
				show_computer();
				cout << "\n";
			}
		}
	}
	infile.close();
}

void account_query::update_computer()
{
	account_query a;
	char kod [20];
	int flag;
	fstream myfile;
	myfile.open("ComputerRecord",ios::binary | ios::out | ios::in);
	if(!myfile)
	{
		cout << "Error in Opening!! File Not Found" << endl;
		return;
	}
	
	read_computer();
	cout << "\n";
	cout << "Enter Computer Code to Edit => ";
	cin >> kod;
	cout << "\n";
	while(myfile.read((char*)&c,sizeof(c)))
	{
		if(strcmp(c.code,kod)==0) //strcmp is to check whether is char is same or not. If same the system will return 0 value if not it will give you a random value
		{
			system("CLS");
			cout << "--------------------------------------------------" << endl;
			cout << "\n";
			cout << "Record " << kod << " has following data" << endl;
			cout << "\n";
			a.show_computer();
			myfile.seekp(0);
			cout << "\n";
			cout << "\nEnter Data to Modify " << endl;
			a.computer();
			myfile.write((char *)&c,sizeof(c));
			flag=1;
		}
	}
	if(flag==1)
	{
		cout << "Successfully Updated" << endl;
	}
	if(flag!=1)
	{
		cout << "Record Not Found" << endl;
	}
	myfile.close();
}

void account_query::delete_computer()
{
	char kod[20];
	string ref;
	account_query a;
	ifstream infile;
	infile.open("ComputerRecord" , ios::binary);
	if(!infile)
	{
		cout << "Error in Opening!! File Not Found" << endl;
		return;
	}
	a.read_computer();
	cout << "\n";
	cout << "Enter Unique Code to Delete => ";
	cin >> kod;
	
	while(infile.read((char*)&c,sizeof(c)))
	{
		if(strcmp(c.code,kod)==0)
		{
			a.show_computer();
			break;
		}
	}
	ofstream tmpfile;
	tmpfile.open("tmpfile", ios::binary);
	infile.seekg(0);
	while(infile.read((char*)&c,sizeof(c)))
	{
		cout << "--------------------------------------------------" << endl;
		cout << "\n";
		if(strcmp(c.code,kod)!=0) // will return positive or negative value
		{
			cout << "Sure To Delete This Record " << endl;
			cout << "\n";
			cout << "[Y] for yes and [N] for no " << endl;
			cout << "=> ";
			cin >> ref;
			if(ref=="Y" || ref=="y")
			{
				tmpfile.write((char *)&c, sizeof (c));
				cout << " Record Deleted " << endl;
				break;
				
			}
			if(ref=="N" || ref=="n")
			{
				return;
			}
		}
	}
	infile.close();
	tmpfile.close();
	remove("ComputerRecord");
	rename("tmpfile","ComputerRecord");
	
}

void account_query::search_computer()
{
	account_query a;
	char kod[20];
	int flag;
	cout << "Enter Record to Search => ";
	cin >> kod;
	ifstream infile;
	infile.open("ComputerRecord", ios::binary);
	if(!infile)
	{
		cout << "Error in Opening!! File Not Found" << endl;
		return;
	}
	a.read_computer();
	infile.seekg(0);
	while(infile.read((char*)&c,sizeof(c)))
	{
		if(strcmp(c.code,kod)==0)
		{
			a.show_computer();
			flag=1;
			break;
		}
	}
	infile.close();
	if(flag==1)
	{
		cout << " Record Found " << endl;
	}
	if(flag!=1)
	{
		cout << " Record Not Found " << endl;
	}
}

void account_query::renewal()
{
	account_query a;
	int number_id;
	fstream iofile;
	iofile.open("AceRecord", ios::binary | ios::in | ios::out);
	if(!iofile)
	{
		cout << "Error in Opening!! File Not Found" << endl;
		return;
	}
	cout << "\n";
	cout << "---------------------RENEWAL---------------------" << endl;
	cout << "\n";
	cout << "Enter MemberShip ID    => ";
	cin >> number_id ;
	cout << "\n";
	iofile.seekg(0); //move read position must enter a value cause u playing with byte
    while(iofile.read((char *)&a,sizeof(a)))
    {
   	 if(a.get_id() == number_id)
   	 {
   		cout<<"Record "<< number_id <<" has following data"<<endl;
   		cout << "\n";
   		a.show_data();
   		iofile.seekp(0);
   		cout<<"\nEnter data to Modify "<<endl;
   		a.membership();
   		iofile.write((char *)&a,sizeof(a));
		break;
	 } 	
	}
	iofile.close();
}
void login(string& admin,string& password)
{
	cout << "ADMINISTRATION LOGIN" << endl;
	cout << "Admin => ";
	cin >> admin;
	cout << "Password => ";
	cin >> password;
	cout << endl;
}
void menu(int)
{
	cout << "WELCOME TO ACE COMPUTER ENTRY MENU" << "\n" << endl;
	cout << "1. Master Entry" << endl;
	cout << "2. Cafe Management" << endl;
	cout << "3. Exit" << endl;
	cout << "\n";
}

void member_entry(int)
{
	cout << "MEMBER ENTRY OPTION" << endl;
	cout << "1. Add New Member" << endl;
	cout << "2. Show Member" << endl;
	cout << "3. Update Record" << endl;
	cout << "4. Delete Record" << endl;
	cout << "5. Search Record" << endl;
	cout << "6. Return" << endl;
	
}

void cafe_management(int)
{
	cout << "CAFE MANAGEMENT" << endl;
	cout << "Ace Cafe Management" << endl;
	cout << "1. Booking" << endl;
	cout << "2. Charges" << endl;
	cout << "3. Renewal" << endl;
	cout << "4. Return" << endl;
}

void computer_entry(int)
{
	cout << "COMPUTER ENTRY OPTION" << endl;
	cout << "1. Add New Computer" << endl;
	cout << "2. Show Computer" << endl;
	cout << "3. Update Computer" << endl;
	cout << "4. Delete Computer" << endl;
	cout << "5. Search Record" << endl;
	cout << "6. Return" << endl;
}

void booking(int)
{
	cout << "-------BOOKING-------" << endl;
	cout << "1. Member Login" << endl;
	cout << "2. Member Log Out" << endl;
	cout << "3. Non-Member User Log In" << endl;
	cout << "4. Non-Member User Log Out" << endl;
	cout << "5. Return" << endl;
}

void charges(int)
{
	cout << "-------CHARGES-------" << endl;
	cout << "1. Take Charges" << endl;
	cout << "2. Show Charges" << endl;
	cout << "3. Return" << endl;
}

int main ()
{
	int choice;
	int entry;
	int member;
	int cafe;
	int comp;
	int book;
	string admin;
	string password;
	account_query A;
	cout << "\n";
	login(admin,password);
	if(admin=="0" && password=="0")
	{
		system("CLS");
		loop:
		{
			menu(choice);
			cout << "Please Enter Your Operation => ";
			cin >> choice;
			cout << "\n";
			master_entry:
				{		
					if(choice==1)
					{
						system("CLS");
						cout << "MASTER ENTRY" << endl;
						cout << "1. Member Entry" << endl;
						cout << "2. Computer Entry" << endl;
						cout << "3. Return" << endl;
						cout << "Enter Your Entry => ";
						cin >> entry;
						cout << "\n" ;	
						if (entry==1)
						{
							system("CLS");
							member_entry(member);
							cout << "Enter Your Choice => ";
							cin >> member;
							while(member)
							{
								switch(member)
								{
									case 1:
										cout << "\n";
										A.new_member();
										cout << "\n";
										cout << "Successfully Added" << endl;
										Sleep(2000);
										system("CLS");
										break;
									case 2:
										cout << "\n";
										A.read_rec();
										cout << "\n";
										system("Pause");
										system("CLS");
										break;
									case 3:
										cout << "\n";
										A.update_rec();
										cout << "\n" ;
										cout << "Successfully Updated" << endl;
										Sleep(2000);
										system("Pause");
										system("CLS");
										break;
									case 4:
										cout << "\n";
										A.delete_rec();
										cout << "\n" ;
										cout << "Successfully Deleted" << endl;
										Sleep(2000);
										system("CLS");
										break;
									case 5:
										cout << "\n";
										A.search_rec();
										cout << "\n" ;
										system("Pause");
										system("CLS");
										break;
									case 6:
										system("CLS");
										goto master_entry;
								
									default:
										cout << "\n";
										cout << "Invalid Input" << endl;
										cout << "Please Try Again !!!" << endl;
										Sleep (1000);
										cout << "Redirect To Main Menu" << endl;	
										Sleep(3000);
										system("CLS");
										goto master_entry;
								} //switch loop
						//system("CLS");
						member_entry(member);
						cout << "Enter Your Choice => ";
						cin >> member;				
							} //while loop
						} 
						if (entry==2)
						{
							system("CLS");
							computer_entry(comp);
							cout << "Enter Your Choice => ";
							cin >> comp;
							while(comp)
							{
								switch(comp)
								{
									case 1:
										cout << "\n";
										A.new_computer();
										cout << "\n";
										cout << "Successfully Added" << endl;
										Sleep(2000);
										system("CLS");
										break;
									case 2:
										cout << "\n";
										A.read_computer();
										cout << "\n";
										system("Pause");
										system("CLS");
										break;
									case 3:
										cout << "\n";
										A.update_computer();
										system("Pause");
										system("CLS");
										break;
									case 4:
										cout << "\n";
										A.delete_computer();
										cout << "\n" ;
										cout << "Successfully Deleted" << endl;
										Sleep(2000);
										system("CLS");
										break;
									case 5:
										cout << "\n";
										break;
									case 6:
										system("CLS");
										goto master_entry;
									default:
										cout << "\n";
										cout << "Invalid Input" << endl;
										cout << "Please Try Again !!!" << endl;
										Sleep (1000);
										cout << "Redirect To Main Menu" << endl;	
										Sleep(3000);
										system("CLS");
										goto master_entry;
								}
							system("CLS");
							computer_entry(comp);
							cout << "Enter Your Choice => ";
							cin >> comp;
							}
						}
						if (entry==3)
						{
							system("CLS");
							goto loop;
						}
						else
						{
							cout << "Invalid Input" << endl;
							cout << "Please Try Again !!!" << endl;
							Sleep (1000);
							cout << "Redirect To Main Menu" << endl;	
							Sleep(3000);
							system("CLS");
							goto master_entry;
						}
					}
				}
		cafe_management:
			{
				if(choice==2)
				{
					system("CLS");
						cout << "CAFE MANAGEMENT" << endl;
						cout << "1. Member Entry" << endl;
						cout << "2. Computer Entry" << endl;
						cout << "3. Renewal" << endl;
						cout << "4. Return" << endl;
						cout << "Enter Your Entry => ";
						cin >> entry;
						cout << "\n" ;
					if(entry==3)
					{
						A.renewal();
						cout << "Successfully Updated" << endl;
						Sleep (1000);
						goto cafe_management;
					}	
					
					if(entry==4)
					{
						system("CLS");
						goto loop;
					}
					
					else
						{
							cout << "Invalid Input" << endl;
							cout << "Please Try Again !!!" << endl;
							Sleep (1000);
							cout << "Redirect To Main Menu" << endl;	
							Sleep(1000);
							system("CLS");
							goto cafe_management;
						}
				}	
			}

		}	

	}				
	else
	{
		cout << "Incorrect Password!!!" << endl;
		Sleep (1000);
		cout << "Please Use The Correct Administration" << endl;
		Sleep (1000);
		cout << "Program Terminated " << endl;
		Sleep (2000);
		system("CLS");
		return main();
	}
return 0;
}


