#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
#include<Windows.h>
#include "Class.h"

void next(int);
void tabs(int);
void lines(int);
void lines2(int);
void header();
void thankyou();
void receipt();
void set();
char alacarte_menu(char [10]);
char add();
int placeorder();
int getquantity();
void firstmain();
void getOrder();
void method();

using namespace std;

//Register new user
void User::registerUser()
{
	next(1); tabs(3); lines(60);
	next(2); tabs(5); cout << "     REGISTRATION FORM";
	next(1); tabs(3); lines(60);
	next(2); tabs(3); cout << "ENTER FIRST NAME: "; cin >> firstname;
	next(1); tabs(3); cout << "ENTER LAST NAME: "; cin >> lastname;
	next(1); tabs(3); cout << "ENTER USERNAME (UP TO 10 CHARACTERS LONG): "; cin >> username;
	next(1); tabs(3); cout << "ENTER PASSWORD (UP TO 10 CHARACTERS LONG): "; cin >> password;
	next(1); tabs(3); lines(60);
			
	ofstream output("customer.txt");
	output << username << endl << password;
	output.close();
}
//User login page
bool User::loginUser()
{
	next(2); tabs(3); lines(55);
	next(2); tabs(5); cout << "     LOGIN MENU";
	next(1); tabs(3); lines(55);
	next(2); tabs(3); cout << "ENTER USERNAME: "; cin >> username;
	next(1); tabs(3); cout << "ENTER PASSWORD: "; cin >> password;
			
	ifstream read("customer.txt");
	getline(read, un);
	getline(read, pw);
			
	if((un == username) && (pw == password))
	{
		return true;
	}
	else		
	{
		return false;
	}
}		
//Display set items
void Set::getDescriptionSet(Set temp){
		next(1); tabs(3);
		cout << ">>" <<temp.set; next(2); tabs(1);
		cout << temp.s1 << endl; tabs(1);
		cout << temp.s2<< endl; tabs(1);
		cout << temp.s3; next(2); tabs(1);
		cout << "The price is RM " << fixed << setprecision(2) << temp.price << endl;
		tabs(1); lines2(38);
}
//Calculate total price of ala carte
double AlaCarte::calc(){
	amount = getquantity();
	total_price=price*amount;
	return total_price;
}
//Calculate total price of set
double Set::calc(){
	amount = getquantity();
	total_price=price*amount;
	return total_price;
}
//Print ala carte items list
ostream &operator<< (ostream &output, const AlaCarte &list){
	output<<setw(4)<<left<<" "<<setw(2)<<left<<list.code<<setw(4)<<right<<" ";
	output<<"  "<<setw(15)<<left<<list.name;
	output<<setw(8)<<right<<"RM "<<setw(5)<<left<<setprecision(2)<<fixed<<list.price;
			
	return output;
}	

//Global variable
int option = 0;
int order = 0;

int main(){
	User user;
	
	choose:

	firstmain();

	system ("cls");
	
	switch(option)
	{
		case 1: { 	next(1); tabs(3);
					system("cls");
					cout << "" << endl;
					system("cls");
						
					user.registerUser();
					cout << "" << endl;
					system("cls");
						
					next(5); tabs(5);
					cout << "PROCESSING.....";
					Sleep (1000);
					cout << ".......";
					Sleep (1000);
							
					system("cls");
					next(2); tabs(3); lines(55);
					next(2); tabs(4);cout << "CONGRATULATIONS! YOU ARE NOW REGISTERED!" << endl;
					next(1); tabs(3); lines(55);
					next(2);
					goto login;
				}
			
		case 2: {	login:
					bool status = user.loginUser();
										
					if(!status)
					{
						next(2); tabs(5); cout << "False login!" << endl;
						next(2); tabs(3); lines(50);						
						system("PAUSE");
						return 0;
					}
					else
					{
						next(2); tabs(4); cout << "    Succesfully logged in!";
						next(2); tabs(3); lines(50);
						system("cls");
						getOrder();
					}
					break;
				}
				
		default: next(2); tabs(3); cout<<"Choice does not exist! Please Enter Correct Choice"<<endl<<endl;
				 goto choose;
	}
	
	char a, c1[10]="Food", c2[10]="Dessert", c3[10]="Drink";
	int alacarte_choice, set_choice;
	
	AlaCarte *val, totalSum, *t; //t = total
	
	Set set1("Set 1","6 pieces Spicy Nuggets","Medium size French Fries","Medium size Soda pop",15.40);
	Set set2("Set 2","Chicken Burger","Medium size Sweet Corn","Medium size Soda pop",16.00);
	Set set3("Set 3","4 pieces Spicy Nuggets","Medium size Soft Serve","Medium size Milkshake",14.99);
	
	Food f1("French Fries", 1, 8.50), f2("Chicken Burger", 2, 10.99), f3("Spicy Nuggets x6", 3, 6.90);
	Dessert d1("Sweet Corn", 4, 4.20), d2("Soft Serve", 5, 6.30), d3("Cheesecake", 6, 12.05);
	Drink dr1("Mineral Water", 7, 1.20), dr2("Soda pop", 8, 5.50), dr3("Milkshake", 9, 7.60);
	
	system("cls");
	// label
	mainorder:
		
	getOrder();  
	
	//If user choose Ala carte
	if(order == 1){
		
  	 	//Display ala carte menus
	 	system ("cls");
 	 	next(1); tabs(2); alacarte_menu(c1); tabs(4); alacarte_menu(c2);
	 	next(2); tabs(1); lines2(42); tabs(1); lines2(42); next(1);
	 	tabs(1); header(); tabs(1); header(); next(1); tabs(1); lines2(42); tabs(1); lines2(42);
	 	next(1); tabs(1); cout<<f1<<"\t"<<d1;
	 	next(1); tabs(1); cout<<f2<<"\t"<<d2;
		next(1); tabs(1); cout<<f3<<"\t"<<d3; next(1); tabs(1); lines2(42); tabs(1); lines2(42);
	 	next(2); tabs(5); alacarte_menu(c3); next(2); tabs(4); lines2(42); next(1);
	 	tabs(4); header(); next(1); tabs(4); lines2(42);
	 	next(1); tabs(4); cout<<dr1<<"\n\t\t\t\t"<<dr2<<"\n\t\t\t\t"<<dr3;
	 	next(1); tabs(4); lines2(42); next(1); lines(106);
	 
	 	//Label
	 	alacarte:
	 	cout<<"\n\t\t\t\t[Please enter item number only!]";
	 
	 	do{
	  	    alacarte_choice = placeorder();	
		
			ofstream file;
  			file.open ("ac_receipt.txt", ios::app);
		
			switch(alacarte_choice){
			case 1: {
					    t = &f1;
					    t->calc();
						file<<f1.name<<"\t\t\t\t"<<f1.amount<<endl;
						break;
					  }
					  
			case 2: {
					    t = &f2;
					    t->calc();
						file<<f2.name<<"\t\t\t\t"<<f2.amount<<endl;
						break;
					  }	
					  	
			case 3: {
					    t = &f3;
					    t->calc();
						file<<f3.name<<"\t\t\t"<<f3.amount<<endl;
						break;
					  }
			
			case 4: {
						t = &d1;
						t->calc();
						file<<d1.name<<"\t\t\t\t"<<d1.amount<<endl;
						break;
					  }
						
			case 5: {
						t = &d2;
						t->calc();
						file<<d2.name<<"\t\t\t\t"<<d2.amount<<endl;
						break;
					  }
					  
			case 6: {
						t = &d3;
						t->calc();
						file<<d3.name<<"\t\t\t\t"<<d3.amount<<endl;
						break;
					  }
			
			case 7: {
						t = &dr1;
						t->calc();
						file<<dr1.name<<"\t\t\t\t"<<dr1.amount<<endl;
						break;
					  }
					  
			case 8: {
						t = &dr2;
						t->calc();
						file<<dr2.name<<"\t\t\t\t"<<dr2.amount<<endl;
						break;
					  }
					  
			case 9: {
						t = &dr3;
						t->calc();
						file<<dr3.name<<"\t\t\t\t"<<dr3.amount<<endl;
						break;
					  }
					  
			default : {tabs(1); cout<<"Error!" <<endl; next(1); tabs(1); goto alacarte; break;}
		}
		file.close();
	
	  	a = add();
	  
	  	totalSum.total_price += t->total_price;
	  
  	  	}while(a == 'y' || a == 'Y');
	
	 	//Print receipt
	 	receipt();
		
	 	string arr;	
  	 	ifstream in;
  	 	in.open ("ac_receipt.txt");
  	    	while(!in.eof()){					//until End of File is encountered
  		  		getline(in,arr);
          		next(1); tabs(3);
				cout<<setw(30)<<left<<arr;		
        	}
     	in.close();
     	remove("ac_receipt.txt");
       	
     	next(1); tabs(3); cout<<"Total price: 			   RM "<<totalSum.total_price;
     	thankyou();
	
	}
	//If user choose Set
	else if(order == 2){
		
		set();
		set1.getDescriptionSet(set1);
		set2.getDescriptionSet(set2);
		set3.getDescriptionSet(set3);
		//Label
		set:
		cout<<"\n\t[Please enter set number only!]";
		
	 do{
	 	
		set_choice = placeorder();
		
		ofstream file;
  		file.open ("ac_receipt.txt", ios::app);
		
		switch(set_choice){
			case 1:{ 
					   val = &set1;  val->calc();
					   file<<set1.set<<"\t\t\t\t\t"<<set1.amount<<endl;
					   break;
					 }
			
			case 2:{ 
					   val = &set2; val->calc();
					   file<<set2.set<<"\t\t\t\t\t"<<set2.amount<<endl;
					   break;
					 }
			
			case 3:{ 
					   val = &set3; val->calc();
					   file<<set3.set<<"\t\t\t\t\t"<<set3.amount<<endl; 
					   break;
					 }
			
			default : {tabs(1); cout<<"Error!" <<endl; next(1); tabs(1); goto set; break;}
		}
		file.close();
		
	 	a = add();
	    
	    totalSum.total_price += val->total_price;
	
	 
		}while(a == 'y' || a == 'Y');
	 
	 	//Print receipt	
	 	receipt();
		
	 	string arr;	
  	 	ifstream in;
  	 	in.open ("ac_receipt.txt");
  	    	while(!in.eof()){					//until End of File is encountered
  		  		getline(in,arr);
          		next(1); tabs(3);
				cout<<setw(30)<<left<<arr;		
        	}
     	in.close();
     	remove("ac_receipt.txt");
     
	 	next(1); tabs(3); cout << fixed << setprecision(2) << "Total purchase : RM" <<totalSum.total_price << endl;
	 	thankyou();	 
		
	}
	else{
		cout << "\n\t\t\tInvalid data!!! Please enter your choice again\n\n";
		system("pause");
		system("cls");
		goto mainorder;
	}	
	
	return 0;
}

//Function definitions
void next(int n){
	
	for(int i=0; i<n;i++)
		cout<<endl;
}

void tabs(int t){	
	for(int i=0; i<t;i++)
		cout<<"\t";
}

void lines(int l){
	
	for(int i=0; i<l;i++)
		cout<<"_";
}

void lines2(int l2){
	
	for(int i=0; i<l2;i++)
		cout<<"-";
}

void set(){
	system ("cls");
	cout << "\t\t - * - * - * - * - * -\n";
	cout << "\t\t       Set Meals\n";
	cout << "\t\t - * - * - * - * - * -\n\n";
	tabs(1); lines2(38);
}

char alacarte_menu(char n[10]){
	cout<<"    >> "<<n<<" Menu << ";
}
//Menu item header
void header(){
	cout<<setw(1)<<left<<"|"<<setw(8)<<left<<"Item No."<<setw(1)<<right<<"|"
		<<setw(4)<<left<<" "<<setw(9)<<left<<"Item Name"<<setw(6)<<right<<" "
		<<setw(4)<<left<<"|"<<setw(5)<<left<<"Price"<<setw(4)<<right<<"|";
}
//Thank you notes
void thankyou(){
	next(1); tabs(3); lines2(52);
	next(2); tabs(3); cout<<"          Thank You For Ordering With Us!";
	next(2); tabs(3); cout<<"     Enjoy your meal, have a nice day :D";
	next(2); tabs(3); cout<<"           Brought to you by Group 1"; next(5);
	exit(1);
}
//Get order choices from user
int placeorder(){
	int ch;

	do{
		next(2); tabs(1); cout<<"Place your order >> ";
		cin>>ch;
		
		if(ch >=0 && ch <=9){
			return ch;
		}	
		else
			next(1); tabs(1); cout<<"Error! Please Choose From List"<<endl;		
	}while(ch <0 || ch >9);
}
//Get amount of order from user
int getquantity(){
	int q;

	do{
		next(1); tabs(1); cout<<"Enter quantity >> ";
		cin>>q;
		
		if(q > 0 && q < 99)
			return q;
		else
			next(1); tabs(1); cout<<"Error! Please Enter Again"<<endl;		
	}while(q <= 0);
}
//Add more item
char add(){
	char ch;
	do{
		next(2); tabs(1); cout<<"Add another item? [Y]Yes or [N]No";
		next(1); tabs(1); cout<<"> ";
		cin>>ch;
		
		if(ch =='y' || ch =='Y' || ch =='n' || ch =='N'){
			return ch;
		}	
		else
			next(1); tabs(1); cout<<"Error! Try again"<<endl;		
	}while(ch !='y' || ch !='Y'|| ch !='n' || ch !='N');
}
//Display items purchased by customer
void receipt(){
	system("cls");
	next(1); tabs(3); lines2(42);
	//Label
	method:
	
	int paymentMethod;
	method();
	cin >> paymentMethod;
	
	if( paymentMethod >=1 && paymentMethod <=3){
		system ("cls"); next(1); tabs(3);
		
		//Payment by cash
		if(paymentMethod == 1){

			next(1); tabs(3); lines(42);
			next(2); tabs(5); cout<<">> RECEIPT << ";			
			next(2); tabs(3); cout<<"Payment method: Cash ";			
			next(2); tabs(3); cout<<"Payment is successful";
			next(2); tabs(3); cout<<"Your order:                          Amount \n";
		
		}
		//Payment by credit/debit card
		else if(paymentMethod == 2){
			
			next(1); tabs(3); lines(42);
			next(2); tabs(5); cout<<">> RECEIPT << ";
			next(2); tabs(3); cout<<"Payment method: Credit/Debit card ";
			next(2); tabs(3); cout<<"Payment is successful";
			next(2); tabs(3); cout<<"Your order:                          Amount \n";
			
		}
		else if(paymentMethod == 3){
			system("cls");
			next(1); tabs(3); lines(42);
			next(2); tabs(5); cout<<">> PROGRAM EXITED << ";
			exit(1);
		}
		
	}
	else{
	next(1); tabs(3); cout<<"Choice does not exist! Please Enter Correct Choice";
	next(2);
	system("pause");
	goto method;
	}
}
//Get category choice from user
void getOrder()
{
	next(1); tabs(3); lines(55);
	next(2); tabs(5);cout<< ">> Check out Today's Menu <<";
    next(2); tabs(5);cout<< "[1] Ala Carte";
    next(2); tabs(5);cout<< "[2] Set";
	next(2); tabs(5);cout<<"Enter choice:  ";
	cin >> order;
}
//Get user login choice
void firstmain()
{
	next(1); tabs(3); lines(55);
	next(2); tabs(4); cout << "WELCOME TO GROUP 1 FAST FOOD RESTAURANT";
	next(1); tabs(3); lines(55);
	next(1);
	next(1); tabs(3); cout << "   ***BECOME A MEMBER TO ENJOY SPECIAL OFFERS***";
	next(2); tabs(4); cout<<">> [1] REGISTER";
	next(1); tabs(4); cout<<">> [2] LOG-IN";
	next(1); tabs(3); lines(55);
	next(2); tabs(3); cout<<"ENTER YOUR CHOICE >> "; cin >> option;
}
//Display payment method menu
void method(){
	system ("cls");
	next(1); tabs(3); lines(50);
	next(2); tabs(5); cout<<">> Payment Method << ";
	next(2); tabs(3); cout<<">> [1] Cash";
	next(2); tabs(3); cout<<">> [2] Credit/Debit Card";
	next(2); tabs(3); cout<<">> [3] Cancel";
	next(1); tabs(3); lines(50);
	next(2); tabs(3); cout<<">> "; 
}
