#include <iostream>
#include <iomanip>
#include <string>
#include <limits>

using namespace std;

struct User {
	string name;
	int age;
	string login;
	string mobileNo;
	string password;
};

struct House {
	string lot;
	string propertyName;
	int totalBedrooms;
	int totalBathrooms;
	double price;
	int status; //1 - available, 2 - sold
	string buyerName;
};

void viewHouse(House housing[], int totalHouse) {
	
	system("cls");
  	cout<<"\n\t\t\t     VIEWING HOUSES" << endl;
  	cout<<"\t\t\t----------------------------" << endl;	
  	
	string lot;
	string propertyName;
	int totalBedrooms;
	int totalBathrooms;
	double price;
	int status; //1 - available, 2 - sold
	string buyerName;  	
  	
  	for(int i=0; i<totalHouse; i++) {
  		
  		House house = housing[i];
  		
  		cout << "\t\t\t House " << i+1 << endl;
  		cout << "\t\t\t Lot: " << house.lot << endl;
  		cout << "\t\t\t Property name: " << house.propertyName << endl;
  		cout << "\t\t\t Total bedrooms: " << house.totalBedrooms << endl;
  		cout << "\t\t\t Total bathrooms: " << house.totalBathrooms << endl;
  		
  		cout << setprecision(2);
		cout << fixed;
  		cout << "\t\t\t Price: RM" << house.price << endl;
  		
  		if (house.status == 1) {
  			cout << "\t\t\t Status: available" << endl;
		} else {
			cout << "\t\t\t Status: not available - already sold" << endl;
		}
		
		cout << endl;
	}
  	
  	system("pause");
	system("cls");
	
}

void buyHouse(House housing[], string buyerName, int totalHouse) {
	
	string houseLot;
	
	system("cls");
  	cout<<"\n\t\t\t     BUYING HOUSE" << endl;
  	cout<<"\t\t\t----------------------------" << endl;
	  
  	for(int i=0; i<totalHouse; i++) {
  		
  		House house = housing[i];
  		
  		cout << "\t\t\t House " << i+1 << endl;
  		cout << "\t\t\t Lot: " << house.lot << endl;
  		cout << "\t\t\t Property name: " << house.propertyName << endl;
  		cout << "\t\t\t Total bedrooms: " << house.totalBedrooms << endl;
  		cout << "\t\t\t Total bathrooms: " << house.totalBathrooms << endl;
  		
  		cout << setprecision(2);
		cout << fixed;
  		cout << "\t\t\t Price: RM" << house.price << endl;
  		
  		if (house.status == 1) {
  			cout << "\t\t\t Status: available" << endl;
		} else {
			cout << "\t\t\t Status: not available - already sold" << endl;
		}
		
		cout << endl;
	}
	
	cout << "\t\t\t Enter lot no: ";
  	cin >> houseLot;
  	
  	cin.clear();
  	cin.ignore(numeric_limits<streamsize>::max(), '\n');
	  
	for(int i=0; i<totalHouse; i++) {
  		
  		House house = housing[i];
  		
  		if ((house.lot).compare(houseLot) == 0) {
  			
  			if (house.status == 1) {
  				
	  			cout << endl;
	  			cout << "\t\t\t House with lot no: " << houseLot << " is sold" << endl;
	  			
	  			//update housing array
	  			housing[i].status = 2;
	  			housing[i].buyerName = buyerName;
	  			
	  			break;
	  			 
	  		} else {
	  			cout << endl;
  				cout << "\t\t\t House with lot no: " << houseLot << " already bought by someone else" << endl;  				
  				
  				break;
  				
			}
		}
	}
  	
  	system("pause");
	system("cls");
	
}

void print(House housing[], string buyerName, int totalHouse) {
	
	system("cls");
  	cout<<"\n\t\t\t     PRINT TRANSACTION" << endl;
  	cout<<"\n\t\t\t       HOUSE BOUGHT" << endl;
  	cout<<"\t\t\t----------------------------" << endl;	
  	
  	int index = 0;
  	
	for(int i=0; i<totalHouse; i++) {
  		
  		House house = housing[i];
  		
  		if ((house.buyerName).compare(buyerName) == 0) {
  			
  			index++;
	  		cout << "\t\t\t House " << index << endl;
	  		cout << "\t\t\t Lot: " << house.lot << endl;
	  		cout << "\t\t\t Property name: " << house.propertyName << endl;
	  		cout << "\t\t\t Total bedrooms: " << house.totalBedrooms << endl;
	  		cout << "\t\t\t Total bathrooms: " << house.totalBathrooms << endl;
	  		
	  		cout << setprecision(2);
			cout << fixed;
	  		cout << "\t\t\t Price: RM" << house.price << endl;  
		  	
			cout << endl;		
		}
	}  	
  	
  	system("pause");
	system("cls");
}

const int TOTALHOUSE = 50;

//Home struct already declared in housing.cpp
void initializedHousing(House housings[], int &totalHouse) {
	House house;
	
	house.lot = "0101";
	house.propertyName = "Platino Apartment";
	house.totalBedrooms = 3;
	house.totalBathrooms = 2;
	house.price = 150000;
	house.status = 1;
	house.buyerName = "none";
	
	housings[0] = house;	
	totalHouse++;
	
	house.lot = "0102";
	house.propertyName = "Platino Apartment";
	house.totalBedrooms = 4;
	house.totalBathrooms = 3;
	house.price = 250000;
	house.status = 1;
	house.buyerName = "none";
	
	housings[1] = house;
	totalHouse++;
	
	house.lot = "0103";
	house.propertyName = "Platino Apartment";
	house.totalBedrooms = 5;
	house.totalBathrooms = 4;
	house.price = 350000;
	house.status = 1;
	house.buyerName = "none";
	
	housings[2] = house;
	totalHouse++;
}

void memberMenu(string login)
{
	system("cls");

	cout<<"\n";
	cout<<"\t               ###############################          "<<endl;
	cout<<"\t            #####################################       "<<endl;
	cout<<"\t         ###########################################    "<<endl;
	cout<<"\t      ################################################# "<<endl;
	cout<<"\t                     WELCOME TO HBS (" << login << ")   "<<endl;
	cout<<"\t        ##           Member Main Menu              ##   "<<endl;
	cout<<"\t        ##   ===================================   ##   "<<endl;
	cout<<"\t        ##         1) VIEW HOUSE                   ##   "<<endl;
	cout<<"\t        ##         2) BUY HOUSE                    ##   "<<endl;
	cout<<"\t        ##         3) PRINT                        ##   "<<endl;
	cout<<"\t        ##         4) EXIT                         ##   "<<endl;
	cout<<"\t        ##   ===================================   ##   "<<endl;
	cout<<"\t        #############################################   "<<endl;
	cout<<"\t      ################################################# "<<endl;
	cout<<endl;
}

void exitMemberMenu()
{
	system("cls");

	cout << "\n\n\n\n\n";
	cout << "\t\t\t  THANK YOU FOR USING HBS" << endl;
	cout << "\t\t\t       SEE YOU AGAIN" << endl;
	cout << "\n\n\n\n";

	exit(0);
}

void chooseMemberMenu(User user) {
	
	House housing[TOTALHOUSE];
	
	int totalHouse = 0;
	initializedHousing(housing, totalHouse);
		
	int choice;
	
	do
	{
		memberMenu(user.login);
		
		cout<<"\n\n";
		cout<<"\t\t\t      Enter your choice"<<endl;
		cout<<"\t\t\t             ";
		cin >> choice;
		
		cin.clear();
  		cin.ignore(numeric_limits<streamsize>::max(), '\n');
		
		system("cls");
		
		switch(choice) {
			
			case 1 : {
				viewHouse(housing, totalHouse);
				
				break;
			}
			
			case 2 : {
				buyHouse(housing, user.name, totalHouse);	
				
				break;
			}
			
			case 3 : {
				print(housing, user.name, totalHouse);
				
				break;
			}
			
			case 4 : {
				exitMemberMenu();
				break;
			}
		
			default : {
				cout<<"\n\n\n";
				cout<<"\t\t\t       INVALID INPUT"<<endl;
				cout<<"\t\t\t    Please choose again"<<endl;
				cout<<endl<<endl;					  
				system("pause");
				system("cls");
			}
		}
				
	} while(choice != 4);
}

const int TOTALUSER = 50;

//User struct already declared in membermenu.cpp
void initializedUser(User users[]) {
	User user;
	
	user.name = "James Bond";
	user.age = 12;
	user.mobileNo = "123";
	user.login = "bond";
	user.password = "007";
	users[0] = user;
	
	user.name = "John Wick";
	user.age = 32;
	user.mobileNo = "456";
	user.login = "wick";
	user.password = "111";
	users[1] = user;
	
	user.name = "Tony Stark";
	user.age = 22;
	user.mobileNo = "231";
	user.login = "stark";
	user.password = "222";
	users[2] = user;
}

void loginMenu()
{
	system("cls");

	cout<<"\n";
	cout<<"\t               ###############################          "<<endl;
	cout<<"\t            #####################################       "<<endl;
	cout<<"\t         ###########################################    "<<endl;
	cout<<"\t      ################################################# "<<endl;
	cout<<"\t        ##              WELCOME TO HBS             ##   "<<endl;
	cout<<"\t        ##   ===================================   ##   "<<endl;
	cout<<"\t        ##         1) REGISTER                     ##   "<<endl;
	cout<<"\t        ##         2) LOGIN                        ##   "<<endl;
	cout<<"\t        ##         3) EXIT                         ##   "<<endl;
	cout<<"\t        ##   ===================================   ##   "<<endl;
	cout<<"\t        #############################################   "<<endl;
	cout<<"\t      ################################################# "<<endl;
	cout<<endl;
}

void exitLoginMenu()
{
	system("cls");

	cout << "\n\n\n\n\n";
	cout << "\t\t\t  THANK YOU FOR USING HBS" << endl;
	cout << "\t\t\t       SEE YOU AGAIN" << endl;
	cout << "\n\n\n\n";

	exit(0);
}

User userRegistration()
{
	User user;

  	cout<<"\n\t\t\t     REGISTERATION FORM" << endl;
  	cout<<"\t\t\t----------------------------" << endl;
  
	cout << "\t\t\t Name: ";
  	cin >> user.name;
  	
  	cin.clear();
  	cin.ignore(numeric_limits<streamsize>::max(), '\n');
  	
  	cout << "\t\t\t Age: ";
  	cin >> user.age;
  	
  	cin.clear();
  	cin.ignore(numeric_limits<streamsize>::max(), '\n');
  
	cout << "\t\t\t Mobile No: ";
  	cin >> user.mobileNo;
  	
  	cin.clear();
  	cin.ignore(numeric_limits<streamsize>::max(), '\n');
  
	cout << "\t\t\t Login: ";
  	cin >> user.login;
  
  	cin.clear();
  	cin.ignore(numeric_limits<streamsize>::max(), '\n');
  	
	cout << "\t\t\t Password: ";
  	cin >> user.password;
  	
  	cin.clear();
  	cin.ignore(numeric_limits<streamsize>::max(), '\n');
  	
  	system("pause");
	system("cls");
	
	return user;
}

User userLogin(User users[], int totalUser, bool &loginSuccess) {
	User foundUser, user;
	string login, password;
	
  	cout<<"\n\t\t\t     LOGIN FORM" << endl;
  	cout<<"\t\t\t----------------------------" << endl;	
	
	cout << "\t\t\t Enter your login ID: ";
	cin >> login;
	
	cin.clear();
  	cin.ignore(numeric_limits<streamsize>::max(), '\n');
	
	cout << "\t\t\t Enter your password: ";
	cin >> password;
	
	cin.clear();
  	cin.ignore(numeric_limits<streamsize>::max(), '\n');
	
	for(int i=0; i<totalUser; i++) {
		user = users[i];		
		
		if ((user.login).compare(login) == 0 && (user.password).compare(password) == 0) {
			foundUser = user;
			loginSuccess = true;
			break;			
		}
	}
	
	return foundUser;
}

void chooseLoginMenu()
{
	User users[TOTALUSER];
	User user;
	
	initializedUser(users);	
	
	int choice;
	int totalUser = 3; //from initializedUser
	bool loginSuccess = false;	
	
	do
	{
		loginMenu();
		
		cout<<"\n\n";
		cout<<"\t\t\t      Enter your choice"<<endl;
		cout<<"\t\t\t             ";
		cin >> choice;
		
		cin.clear();
  		cin.ignore(numeric_limits<streamsize>::max(), '\n');
		
		system("cls");
		
		switch(choice)
		{
			case 1 : {
				
				User user = userRegistration();				
				users[totalUser] = user;
				totalUser++;
				
				cout<<"\n\t\t\t     PRINTING REGISTERED USERS\n\n";
				for(int i=0; i<totalUser; i++) {
					
					user = users[i];
					cout << "\t\t\t Login: " << user.login << endl;
					cout << "\t\t\t Password: " << user.password << endl;
					cout << endl;
				}
				
				system("pause");
				system("cls");
				
				break;
			}
			
			case 2 : {
				
				User user = userLogin(users, totalUser, loginSuccess);
				
				if (loginSuccess) {
					cout << endl;
					cout << "\t\t\t Login success!" << endl;
					cout << "\t\t\t Name: " << user.name << endl;
					
					system("pause");
					system("cls");
					
					chooseMemberMenu(user);	
									
				} else {
					cout << endl;
					cout << "\t\t\t Login failed! wrong login/password" << endl;
					system("pause");
					system("cls");
				}
				
				break;
			}
			
			case 3 : {
				exitLoginMenu();
				break;
			}
			
			default : {
				cout<<"\n\n\n";
				cout<<"\t\t\t       INVALID INPUT"<<endl;
				cout<<"\t\t\t    Please choose again"<<endl;
				cout<<endl<<endl;					  
				system("pause");
				system("cls");
			}
		}
		
	} while(choice != 3);
}

int main(void) 
{
	chooseLoginMenu();
	return 0;
} 
