-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventory.cpp
More file actions
162 lines (151 loc) · 4.22 KB
/
Copy pathInventory.cpp
File metadata and controls
162 lines (151 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <bits/stdc++.h>
#include "Inventory.h"
#include <fstream>
#include <string.h>
#include <iostream>
#include "Book.h"
#include <vector>
#include <exception>
#include <stdexcept>
#include <limits>
using namespace std;
void Inventory::addBookToInventory()
{
fstream inventoryFile;
Book book;
inventoryFile.open("inventory.txt",ios::out|ios::app);
if(!inventoryFile)
{
cerr << "\nError: file could not be opened" << endl;
}
int numOfBooks;
cout<<"\nplease enter number of books you want to add : ";
cin >>numOfBooks;
while(cin.fail())
{
cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
cout << "\nBad entry.Number Of Books Must be Number, PLEASE Enter a NUMBER : ";
cin >> numOfBooks;
}
for(int i=0; i<numOfBooks; i++)
{
cout<<"******Enter Details of Book "<<i+1<<"*********"<<endl;
book.ctreateBook();
inventoryFile.write((char*)&book, sizeof(book));
if(!inventoryFile)
{
cerr << "\nCould not write to file\n";
}
cout <<"\nThis book has been added to inventory successfully \n"<<endl;
}
inventoryFile.close();
}
void Inventory::deleteBookFromInventory()
{
Book book;
fstream inventoryFile;
fstream tempFile;
char title[40];
char author[40];
cout<<"Please enter title of book you want to delete :"<<endl;
cin.ignore();
cin.getline(title,40);
cout<<"Please enter author of book you want to delete :";
cin.getline(author,40);
int flag=0;
if(isExist(title,author))
{
tempFile.open("temp.txt",ios::out);
if(!tempFile)
{
cerr << "Error: file could not be opened" << endl;
}
loadBookfromInventory();
for(int i=0; i<readingInventoryBooks.size(); i++)
{
book=readingInventoryBooks[i];
if((strcmp(book.getTitle(),title)==0) && (strcmp(book.getAuthor(),author)==0))
{
continue;
}
tempFile.write((char*)&book, sizeof(book));
}
cout<<"this book has been deleted from inventory successfully "<<endl;
tempFile.close();
remove("inventory.txt");
rename("temp.txt","inventory.txt");
}
else
cout<<"This book not found in our inventory ";
}
void Inventory::loadBookfromInventory()
{
readingInventoryBooks.clear();
fstream inventoryFile;
Book book;
inventoryFile.open("inventory.txt",ios::in);
if(!inventoryFile)
{
cerr << "Error: file could not be opened" << endl;
}
while(inventoryFile.read((char*)&book, sizeof(book)))
{
readingInventoryBooks.push_back(book);
}
inventoryFile.close();
}
void Inventory::displayEntireInventory()
{
vector<Book> allBooks;
allBooks=getInventoryBooks();
int i;
cout<<"All Books In Inventory :\n ";
for( i=0; i<allBooks.size(); i++)
{
cout<<"========================\n";
cout<<"\nBook "<<i+1<<endl;
allBooks[i].displayBookDetails();
}
if(i== 0)
cout<<"Inventory is empty"<<endl;
}
bool Inventory::isExist(char* title,char* author)
{
loadBookfromInventory();
int flag=0;
for(int i=0; i<readingInventoryBooks.size(); i++)
{
if((strcmp(readingInventoryBooks[i].getTitle(),title)==0) && (strcmp(readingInventoryBooks[i].getAuthor(),author)==0))
{
readingInventoryBooks[i].displayBookDetails();
flag=1;
return true;
break;
}
}
if(flag==0)
return false;
}
void Inventory::sortBooksByPrice()
{
vector<Book> allBooks;
allBooks=getInventoryBooks();
int i;
sort(allBooks.begin(), allBooks.end());
cout<<"\nAll Inventory Books after sorting By price : "<<endl;
cout<<"Sorted Books With Price : \n";
for(i=0; i<allBooks.size(); i++)
{
cout<<"Book : "<<i+1<<endl;
allBooks[i].displayBookDetails();
cout<<"=================================="<<endl;
}
if(i==0)
cout<<"There are not books in inventory to be sorted!!"<<endl;
}
vector<Book> Inventory::getInventoryBooks()
{
loadBookfromInventory();
return readingInventoryBooks;
}