Untitled

 avatar
unknown
c_cpp
3 years ago
6.0 kB
4
Indexable
#include <iostream> // Library used for basic input output
#include <stdlib.h> // Library used for exit function
#include <bits/stdc++.h> // Library used to build assossiative array
#include <fstream> //Library for file processing

using namespace std;

int choise;

void login();
void control();
void add_books();
void display();
void update();
void delete_b();

string user;
string pswd;

string book_id;
string book_name;
string author;
string no_books_str;
int no_books;

map<string, string> users;
map<string, map<string, string> > books;

int main(){
	fstream user_data1("user_data.data"); //Opens Userdata file (Creates if not there)
	user_data1 << "GBStaff" << " 1111"; // Adds Default Accout
	user_data1.close();
	
	ifstream user_data("user_data.data"); //Opens Userdata file in read only mode

	string key, val;
	//cout << "Loading Files...\n\n";
	while (user_data >> key >> val) //Go through the file line by line
	{
	   users[key]=val; //Assigning values to assossiative array
	}
	user_data.close();
	
	ifstream book_data("book_data.data");
	string id, name, auth, copies;
	cout << "Loading Files...\n\n";
	while (book_data >> id >> name >> auth >> copies){
	   books[id]["id"]=id;
	   books[id]["name"]=name;
	   books[id]["author"]=auth;
	   books[id]["no_books"]=copies;
	}
	user_data.close();
		
	//cout << "Done.\n\n\n";
	
	//users["admin"]="122";
	cout<<"--------------------------------------------------------"<<endl; 
	cout<< "----------WELCOME TO GENIUS BOOK SHOP SYSTEM----------"<<endl;
    cout<<" ----------NO.244, Yatinuwara Street, Kandy-----------"<<endl;
	login();
	control();

	return 0;
}

/* Function for Login variable setting */

void login(){
	cout << "\n\n===Login to Your Account===\n\n";
	cout << "Username : ";
	cin >> user;
	
	cout << "You Entered '" + user + "'\n\n";
	
	cout << "Password : ";
	cin >> pswd;
	
	if(users[user]==pswd){
		cout << "Login Success!\n";
		
	}else{
		cout << "Login Fail\n";
		cout << "1. Retry \n0. Exit \n";
		cin >> choise;
		if (choise==1){
			login();
		}else{
			cout << "Exitting...";
			exit(0);
		}
	}	 
}

void control(){
	system("cls");
	cout << "\n\n\t\t\t\tCONTROL PANEL";
	cout << "\n\n1. Add New Book";
	cout << "\n2. Display Available Books";
	cout << "\n3. Update Book";
	cout << "\n4. Delete Book";
	cout << "\n5. Exit";
	cout << "\n6. Logout";
	cout << "\n7. manage sales details\n\n";
	
	cin >> choise;
	
	switch (choise) {

		case 1:
			add_books();
			break;

		case 2:
			display();
			break;

		case 3:
			update();
			break;

		case 4:
			delete_b();
			break;

		case 5:
			exit(0);
			break;

		case 6:
			exit(0);
			break;


		default:
			cout << "\n\nINVALID CHOICE\n";
			control();
		}
}

void add_books(){
	system("cls");
	cout << "\n\n===Add Book===\n\n";
	cout << "Book Id : \n";
	cin >> book_id;
	if(books[book_id]["id"]!=""){
		cout << "\nExisting Book ID\n\n";
		add_books();
	}else{
		cout << "Book Name : \n";
		cin >> book_name;
		cout << "Book Author : \n";
		cin >> author;
		cout << "No of books : \n";
		cin >> no_books_str;
		
		cout << "You added " + book_id + " " + book_name + "\n";
		
		books[book_id]["id"]= book_id;
		books[book_id]["name"]= book_name;
		books[book_id]["author"]= author;
		books[book_id]["no_books"]= no_books_str;
		
		ofstream book_data; //Open the file in write mode
		book_data.open("book_data.data", std::ios_base::app); //Set the file to appened mode
	 	book_data << "\n" + book_id + " " + book_name + " " + author + " " + no_books_str ;
	  	book_data.close();
	}
	
	control();
}

void display(){
	system("cls");
	cout << "ID \t\t Name \t\t Author \t\t No.Of Copies \n\n";
	for(auto const &ent1 : books) {
		cout << books[ent1.first]["id"] + " \t\t ";
		cout << books[ent1.first]["name"] + " \t\t ";
		cout << books[ent1.first]["author"] + " \t\t ";
		cout << books[ent1.first]["no_books"] + " \t\t " << endl << endl;
	}
}

void update(){
	cout << "Enter Book ID to update\n";
	cin >> book_id;
	
	
	if(books[book_id]["id"]!=""){
		cout << "You Chose the book\n";
		cout << books[book_id]["name"] + " by " + books[book_id]["author"] + " (" +books[book_id]["id"] +")\n";
	}else{
		cout << "\nNo such book exists!!!\n\n";
		update();
	}

	cout << "Enter New Name : ";
	cin >> book_name;
	if(book_name!=""){
		books[book_id]["name"]=book_name;
	}
	
	cout << "Enter New Author : ";
	cin >> author;
	
	if(author!=""){
		books[book_id]["author"]=author;
	}
	
	cout << "Enter New No. of books : ";
	cin >> no_books_str;
			
	if(no_books_str!=""){
		books[book_id]["no_books"]=no_books_str;
	}
	
	ofstream book_data;
	book_data.open("book_data.data"); //Open the file in write mode
	for(auto const &ent1 : books) {
		book_data << "\n" + books[ent1.first]["id"]+ " " + books[ent1.first]["name"] + " " + books[ent1.first]["author"] + " " + books[ent1.first]["no_books"] ;
	}
	book_data.close();
	
	control();
}

void delete_b(){
	system("cls");
	cout << "Enter Book ID to delete\n";
	cin >> book_id;
	cout << "You Chose the book\n";
	cout << books[book_id]["name"] + " by " + books[book_id]["author"] + " (" +books[book_id]["id"] +")\n\n";
	cout << "Are you sure (This action cannot be reversed)\n\n1.Yes\n2.No(Re-enter ID)\n9.Menu\n0.Exit\n\n Choise : ";
	cin >> choise;
	
	if(choise==1){
		books.erase(book_id);
		ofstream book_data;
		book_data.open("book_data.data"); //Open the file in write mode
		for(auto const &ent1 : books) 
			book_data << "\n" + books[ent1.first]["id"]+ " " + books[ent1.first]["name"] + " " + books[ent1.first]["author"] + " " + books[ent1.first]["no_books"] ;
			
		book_data.close();
	}else if(choise==2){
		delete_b();
	}else if(choise==9){
		control();
	}else if(choise==0){
		exit(0);
	}else{
		cout << "\n\nINVALID CHOICE\n";	
		delete_b();
	}	
}

Editor is loading...