Shopping Management System – C++ Project

In this code snippet, I’m going to share Shopping Management System C++ Code.

I’ve got a project in my college to make a Shopping Management System, so I thought, why not I should share it with you guys.

 

#include <algorithm> //Use in Converting Sting to lowercase
#include<iostream>
#include<conio.h>
#include<string>
#include<fstream>

using namespace std;

#define rows 100 //No of Rows
#define columns 3 //No. of columns
string columns_title[] = {"S.No","Name","Price"}; //You can add any number of columns

/*
Username and Passwords
*/
#define username "shubham"
#define pwd "12"
#define admin_username "admin"
#define admin_pwd "15"

string payment_method[] = {"Cash","Paytm","Debit/Credit Card","Net Banking"}; //Available Payment Methods
string stars(int); //To print * n times
string stars(int,string); //To Print a string n times
void print_action_menu(); //Printing Menu for User and Admin
void shopping(); //Printing Shopping list
void shop(); //Shopping Action and Printing Invoice
string tolowerstr(string ); //Convert string to lowercase
bool ask_is_admin(); //Ask to user if he is admin
string read_pwd(); //Read password inputs from console
void delete_product(); //Delete a Product from file
void payment_mode(); //Choosing Payment Mode
void invoice(); //Checking Invoice Conditions

class check_login{
	public:
	static bool is_admin; //To Check if Logged in User is Admin or not
	static bool logged_in; //To chcek if user is logged in or not
	check_login(){
	}
	bool login(){
		return login(false,ask_is_admin());	
	}
	bool login(bool recursion=false,bool admin=false);
}login;
/*Initializing the Static Values*/
bool check_login::is_admin=false;
bool check_login::logged_in=false;

bool check_login::login(bool recursion,bool admin){
	cout<<endl<<endl;
	cout<<stars(10)<<(admin?"Admin":"User")<<" Login"<<stars(10)<<endl<<endl;
	string fake_username="";
	string fake_pwd="";
	cout<<"Username:\t";
	cin>>fake_username;
	cout<<"Password:\t";
	fake_pwd=read_pwd();
	if((!admin && fake_username==username && fake_pwd==pwd) || (admin && fake_username==admin_username && fake_pwd==admin_pwd)){
		if(admin)
			is_admin=true;
		logged_in=true;
		cout<<endl<<endl<<stars(5)<<" Access Granted "<<stars(5)<<endl<<endl;
		return true;
	}else{
		cout<<endl<<endl<<"Invalid Username and Password"<<endl;
		if(recursion)
			check_login::login(recursion,admin);
		else
			return false;
	}
}

class products:private check_login{
	string data[rows][columns];
	fstream ifile;
	public:
	int quantity;
	int id;
	string payment_method;
	string transaction_hash;
		//Constructor
		products(){
			ifile.open("p.txt",ios::out|ios::in);
			ifile.seekg(0);
			int data_pos = 0;
			if(ifile.is_open()){
				string line;
				getline(ifile,line);
				while(!ifile.eof()){
				const char* str_char = line.c_str();
					int i=0,
					pos=0,
					length=line.length();
//					cout<<line<<endl;
//					cout<<length<<endl;
					for(pos=0;pos<=length;pos++){
						char ch = *(str_char + pos);
						if(ch==';'){
//							cout<<";"<<endl;
							i++;
						}else{						
//							cout<<ch;
							data[data_pos][i] += ch;	
						}
					}
					getline(ifile,line);
					++data_pos;
				}
			}
		}
		//Destructor
		~products(){
			save(); //Saving File When User closes the Window
			ifile.clear();
			ifile.close();
		}
		string* get(int i){
			id=i;
			return data[i];
		}
		void save(){
			ofstream ofile;
			ofile.open("p.txt",ios::out|ios::trunc);
			for(int i=0;i<total();i++){
				if(!data[i][0].empty()){
					string line="";
					for(int j=0;j<columns;j++){
						line += data[i][j];
						if(j!=(columns-1))
							line += ";";
					}
					ofile<<line<<"\n";
				}
			}
		}
		int total(){
			int i=0;
			for(i=0;i<100;i++){
				if(data[i][0].empty())
					return i;
			}
		}
		void print(){
			print(0,total());
		}
		void print(int index,int total){
			if((total-index)==1){	
				for(int t=0;t<columns;t++){
					if(t==0){
						cout<<columns_title[t]<<": \t\t"<<t+1<<endl;
					}else
					cout<<columns_title[t]<<": \t\t"<<data[index][t-1]<<endl;
				}
			}else{
				for(int t=0;t<columns;t++){
					cout<<columns_title[t]<<"\t\t";
				}
				cout<<endl<<stars(60,"-")<<endl;
				for(int i=index;i<total;i++){
					cout<<i+1<<".\t\t";
					for(int j=0;j<columns;j++){
						cout<<data[i][j]<<"\t\t";
					}
					cout<<"\n";
				}
			}
		}
		void print(int i){		
			print(i,i+1);
		}
		void edit(int i){
			system("cls");
			if(!is_admin){
				login(true,true);
				system("cls");
			}
			cin.ignore();
			for(int t=1;t<columns;t++){
				cout<<"Product "<<columns_title[t]<<":\t";	
			    string str;
			    getline(cin,str);
				data[i][t-1]=str;
			}
			cout<<endl<<endl<<endl<<"Data Saved Successfully"<<endl<<endl;
		}
		void add(){
			edit(total());
			save();
		}
		void del(int i){
			if(!is_admin){
				system("cls");
				login(true,true);
				system("cls");
			}
			for(i;i<=total();i++){
				for(int j=0;j<columns;j++){
					data[i-1][j]=data[i][j];
				}
			}
		}
}pro;
//To convert an string to lower case
string tolowerstr(string data){
	transform(data.begin(), data.end(), data.begin(),::tolower);
	return data;
}

bool ask_is_admin(){
	bool is_admin;
	string input;
	cout<<endl<<endl<<"Who are you?"<<endl<<endl;
	cout<<"1.User\n2.Admin\n\n\nEnter Your Choice: \t";
	cin>>input;
	input = tolowerstr(input);
	if(input=="2" || input=="admin")
		return true;
	else
		return false;
}
//Read input from user and print * 
string read_pwd(){
	string fake_pwd;
	char c=_getch();
	while(c!=13){
		fake_pwd.push_back(c);
		cout<<"*";
		c = _getch();
	}
	return fake_pwd;
}

string stars(int n){
	return stars(n,"*");
}
string stars(int n,string c){
	string s="";
	for(int i=0;i<n;i++)
		s.append(c);
	
	return s;
}

void print_action_menu(){
	//Menu for User
	string user_menus[] = {
		"Shopping",
		"Print Invoice",
		"Exit"
	};
	
	//Menu for Admin
	string admin_menus[] = {
		"View Products",
		"Add Product",
		"Edit Product",
		"Delete Product",
		"Exit"
	};
	int total_user_menus = sizeof(user_menus)/sizeof(user_menus[0]);
	int total_admin_menus = sizeof(admin_menus)/sizeof(admin_menus[0]);
	int choice;
	while((login.is_admin && choice!=total_admin_menus) || (!login.is_admin && choice!=total_user_menus)){ //Infinite loop		
		cout<<endl<<endl<<stars(60)<<endl<<stars(60)<<endl<<endl;
		if(!login.is_admin){
			cout<<endl<<endl<<"What do you want to do?"<<endl<<endl;
			
			for(int i=0;i<(sizeof(user_menus)/sizeof(*user_menus));i++){
				cout<<i+1<<". "<<user_menus[i]<<endl;
			}
		}else{
			cout<<endl<<endl<<"What do you want to manage?"<<endl<<endl;
			
			for(int i=0;i<(sizeof(admin_menus)/sizeof(*admin_menus));i++){
				cout<<i+1<<". "<<admin_menus[i]<<endl;
			}
		}
		cout<<"\n\nEnter your Choice: ";
		cin>>choice;
		if(!login.is_admin){
			switch(choice){
				case 1:
					shopping();
					break;
				case 2:
					invoice();
					break;
			}
		}else{
			switch(choice){
				case 2:
					system("cls");
					cout<<endl<<endl<<stars(78)<<endl<<stars(31)<<" ADD A PRODUCT "<<stars(32)<<endl<<stars(78)<<endl<<endl;
					pro.add();
					getch();
					system("cls");
					break;
				case 1:
					system("cls");
					cout<<endl<<endl<<stars(78)<<endl<<stars(31)<<" PRODUCTS LIST "<<stars(32)<<endl<<stars(78)<<endl<<endl;
					pro.print();
					getch();
					system("cls");
					break;
				case 3:
					system("cls");
					cout<<endl<<endl<<stars(78)<<endl<<stars(31)<<" EDIT Product "<<stars(32)<<endl<<stars(78)<<endl<<endl;
					pro.print();
					int product_id;
					cout<<endl<<endl<<"Enter Product Id:\t";
					cin>>product_id;
					product_id -= 1;
					if(!pro.get(product_id)[0].empty()){
						pro.edit(product_id);
					}else{
						cout<<"Product Not Found!!"<<endl<<endl;
					}
					getch();
					system("cls");
					break;
				case 4:
					delete_product();
					break;
			}
		}
	}
	
}
int str_int(string str){
	int count=0;
//	cout<<endl;
	for(int i=0;i<str.length();i++){
		int n = (int) str.c_str()[i]-48;
		if(n>=0){
			count = count*10;
			count += (int) str.c_str()[i]-48;
//			cout<<(int)str.c_str()[i]-48<<" ";
		}
	}
//	cout<<endl;
	return count;
}
void shopping(){
	system("cls");
	cout<<endl<<endl<<stars(78)<<endl<<stars(30)<<" CHOOSE A PRODUCT "<<stars(30)<<endl<<stars(78)<<endl<<endl;
	pro.print();
	cout<<endl<<endl<<"Enter your Choice:\t";
	int choice;
	cin>>choice;
	if(!pro.get(choice-1)[0].empty()){
		pro.id=choice-1;
		pro.transaction_hash.clear();
		shop();
	}
}
void invoice(){
	system("cls");
	if(pro.transaction_hash.empty()){
		cout<<"You don't have anything in your cart.\n\n Do some shopping First.\n\n";
		getch();
		shopping();
	}else{
		shop();
	}
}
void shop(){
	int i=pro.id;
	system("cls");
	if(!login.logged_in)
	{
		cout<<endl<<endl<<stars(20)<<" LOGIN REQUIRED "<<stars(20)<<endl<<endl;
		login.login(true,false);
		system("cls");
	}
	if(!pro.transaction_hash.empty()){
		cout<<endl<<endl<<stars(78)<<endl<<stars(34)<<" INVOICE "<<stars(35)<<endl<<stars(78)<<endl<<endl;
	}
	pro.print(i);
	string* product = pro.get(i);
	cout<<endl<<"Quantity: \t";
	if(pro.quantity)
	cout<<pro.quantity<<endl;
	else{
		int quantity;
	cin>>quantity;
	pro.quantity=quantity;	
	}
	if(!pro.transaction_hash.empty()){
		cout<<endl<<"Payment Method:\t "<<pro.payment_method;
		cout<<endl<<endl<<"Transaction Hash:\t "<<pro.transaction_hash;
	}
	cout<<endl<<endl<<endl<<stars(40,"-")<<endl;
//	cout<<product[1]<<endl<<str_int(product[1])<<endl;
	cout<<"Total Amount: \t Rs."<<(pro.quantity*str_int(product[1]))<<"/-";
	cout<<endl<<stars(40,"-")<<endl;
	if(pro.transaction_hash.empty()){
		payment_mode();
	}
	getch();
}
void payment_mode(){
	system("cls");
	cout<<"\n\n"<<"How would you like to Pay?\n\n";
	for(int i=0;i<4;i++){
		cout<<i+1<<". "<<payment_method[i]<<endl;
	}
	cout<<endl<<endl<<"Enter Your Choice:\t";
	int choice;
	cin>>choice;
	choice -= 1;
	if(choice>=0 && choice<4){	
		if(!payment_method[choice].empty())
		{
			pro.payment_method=payment_method[choice];
			if(choice!=0){
				string t_hash;
				cout<<endl<<endl<<"Transaction Code:\t";
				cin>>t_hash;
				pro.transaction_hash = t_hash;
			}else{
				pro.transaction_hash = "Cash Payment";
			}
			shop();
		}	
	}else{
		payment_mode();
	}
}
void delete_product(){
	system("cls");
	cout<<endl<<endl<<stars(78)<<endl<<stars(30)<<" CHOOSE A PRODUCT "<<stars(30)<<endl<<stars(78)<<endl<<endl;
	pro.print();
	cout<<endl<<endl<<"Enter your Choice:\t";
	int choice;
	cin>>choice;
	if(!pro.get(choice-1)[0].empty()){
		pro.del(choice);
	}
}
int main(){
	bool admin_login=ask_is_admin();
	bool recursion=false;
	if(admin_login){
		recursion=true;
		login.login(recursion,admin_login);
	}
	system("cls");
	print_action_menu();
}

Shubham Kumar

Hey, I am Shubham and i love Blogging, Coding and exploring new things and obviously sharing my experience with you.

Leave a Reply

Your email address will not be published. Required fields are marked *

shares