#include <iostream>
#include <string>
#include <fstream>
#include<chrono>
#include<vector>
#include<ctime>
#include<thread>
#include<mutex>
using namespace std;
mutex g_lock;
class prod
{
public:
string prodname;
int prodid;
int amount;
int seqno;
prod* next;
prod(string, int, int, int, prod*);
};
prod::prod(string tempname,int tempid, int tempamount, int seq_no, prod* tempNext)
{
prodname=tempname;
prodid=tempid;
amount=tempamount;
seqno=seq_no;
next=tempNext;
}
typedef prod* prodPtr;
void getline(istream &stream, string &str, char delimiter)
{ char temp[500];
stream.get(temp, 500, delimiter);
stream.ignore(500, delimiter);
str = temp;
}
void getline(istream &stream, int &num, char delimiter)
{ int temp;
stream >> temp;
stream.ignore(500, delimiter);
num= temp;
}
void readFile(prodPtr &root);
void insert (prodPtr &root);
void delprod(prodPtr &root);
prodPtr locateNode(prodPtr temp, string prodn);
void searchname(prodPtr temp);
void printList(prodPtr temp);
void saveFile(prodPtr temp);
int countNodes(prodPtr temp);
void thr(){
g_lock.lock();
cout<<"Thread launched\t"<<endl;
this_thread::sleep_for(chrono::seconds(rand()%2));
auto timenow =chrono::system_clock::to_time_t(chrono::system_clock::now());
cout << ctime(&timenow) << endl;
g_lock.unlock();
}
void readFile(prodPtr &root)
{
int numprod,prodi,sqn;
string prodn;
int prodamt;
ifstream infile ("PRODUCTS.txt", ios::in);
infile >> numprod;
infile.ignore(500,'\n');
for (int count = 0; count < numprod; count++)
{
getline(infile,prodn, '\n');
getline(infile, prodi, '\n');
getline(infile, prodamt, '\n');
getline(infile,sqn, '\n');
root = new prod (prodn,prodi, prodamt, sqn, root);
}
}
void insert (prodPtr &root)
{
int numprod,prodi,sqn;
string prodn;
int prodamt;
cout << "PRODUCT NAME:\t";
cin.ignore(500,'\n');
getline(cin, prodn, '\n');
cout << "PRODUCT ID:\t";
getline(cin, prodi, '\n');
cout << "PRODUCT AMOUNT:\t";
getline(cin,prodamt, '\n');
cout << "PRODUCT SEQ NO:\t";
getline(cin,sqn, '\n');
root = new prod (prodn,prodi, prodamt, sqn, root);
std::vector<std::thread> threads;
for(int i = 0; i < 1; ++i){
threads.push_back(std::thread(thr));
}
for(auto& thread : threads){
thread.join();
}
}
void delprod(prodPtr &root)
{
string prodname;
int prodid;
int amount;
int seqno;
cout << "Product NAME:\t";
cin.ignore(500,'\n');
getline(cin, prodname, '\n');
prodPtr p = locateNode(root, prodname);
if (p == NULL)
cout << "\nDeletion cannot be done.\n\n";
else if (root == p)
root = p->next;
else
{
prodPtr q = root;
while (q->next != p)
q = q->next;
q->next = p->next;
}
delete p;
cout<<"\n\n************Product deleted*******************\n\n"<<endl;
std::vector<std::thread> threads;
for(int i = 0; i < 1; ++i){
threads.push_back(std::thread(thr));
}
for(auto& thread : threads){
thread.join();
}
}
prodPtr locateNode(prodPtr temp, string prodn)
{
while (temp != NULL)
{
if (temp->prodname == prodn)
{
return temp;
}
temp = temp->next;
}
return NULL;
std::vector<std::thread> threads;
for(int i = 0; i < 1; ++i){
threads.push_back(std::thread(thr));
}
for(auto& thread : threads){
thread.join();
}
}
void searchname(prodPtr temp)
{
string prodn;
cout<<"PRODUCT NAME PLEASE:\t";
cin.ignore(500,'\n');
getline(cin, prodn, '\n');
while (temp != NULL)
{
if (prodn == temp->prodname)
{
cout<<"Product Name : "<<temp->prodname << "\n";
cout<<"Product Id : "<<temp->prodid << "\n";
cout<<"Product amount : "<<temp->amount<< "\n";
cout <<"Product Sequence Number : "<<temp->seqno << "\n";
}
temp = temp->next;
}
cout << "\n";
std::vector<std::thread> threads;
for(int i = 0; i < 1; ++i){
threads.push_back(std::thread(thr));
}
for(auto& thread : threads){
thread.join();
}
}
void printList(prodPtr temp)
{
while (temp != NULL)
{
cout<<"Product Name : "<<temp->prodname << "\n";
cout <<"Product Id :" <<temp->prodid << "\n";
cout <<"Product amount : "<<temp->amount << "\n";
cout <<"Product Sequence Number :"<<temp->seqno << "\n";
temp = temp->next;
}
cout << "\n";
std::vector<std::thread> threads;
for(int i = 0; i < 1; ++i){
threads.push_back(std::thread(thr));
}
for(auto& thread : threads){
thread.join();
}
}
void saveFile(prodPtr temp)
{
int count = countNodes(temp);
ofstream outFile("Input_records.txt",ios::out);
outFile << count << "\n";
while (temp != NULL)
{
outFile <<"Product Name : "<<temp->prodname<< "\n";
outFile <<"Product Id :"<< temp->prodid << "\n";
outFile <<"Product Amount : " << temp->amount << "\n";
outFile <<"Product Sequence Number : "<< temp->seqno << "\n";
temp = temp->next;
}
cout << "\n";
std::vector<std::thread> threads;
for(int i = 0; i < 1; ++i){
threads.push_back(std::thread(thr));
}
for(auto& thread : threads){
thread.join();
}
}
int countNodes(prodPtr temp)
{
int countB = 0;
while (temp != NULL)
{
countB++;
temp = temp->next;
}
return countB;
std::vector<std::thread> threads;
for(int i = 0; i < 1; ++i){
threads.push_back(std::thread(thr));
}
for(auto& thread : threads){
thread.join();
}
}
int main()
{
int choice;
prodPtr root = NULL;
readFile(root);
do
{
cout << "\n\n\t\t<<<<<<<<<=======================================================================================================>>>>>>>>>>";
cout << "\n\n\t\t>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>PRODUCT INFORMATION FILES<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<";
cout << "\n\n\t\t<<<<<<<=======================================================================================================>>>>>>>>>>>";
cout << "\nMenu: Select your option\n";
cout << "\n(1) Add a product to the list\n";
cout << "\n(2) Delete by product name\n";
cout << "\n(3) Search a product by name\n";
cout << "\n(4) List all products\n";
cout << "\n(5) Quit.\n";
cout << "\n<=================================================Enter your choice==================================================>\n ";
cin >> choice;
if (1 <= choice && choice <= 4)
{
switch (choice)
{
case 1:
insert(root);
break;
case 2:
delprod(root);
break;
case 3:
searchname(root);
break;
case 4:
printList(root);
break;
default:
cout << "Invalid choice. Enter again.\n\n";
break;
}
}
}
while (choice != 5);
saveFile(root);
return 0;
}