-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.cpp
185 lines (141 loc) · 4.47 KB
/
database.cpp
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
void createDatabase ();
void setInFile();
string generateBarcode(int increment,int secondIncrement);
char getCheckDigit(string barcode );
char iToC(int perDigit);
void searchInDatabase(string barcode);
bool askForProduct();
int cToiD(char x);
bool searchInDatabaseX(string barcode);
bool searchInDatabaseX();
struct productInformation{
string productType;
string manufacturerName;
string productName;
};
productInformation product;
bool askForProduct(){
cout << "Enter product type: ";
getline(cin,product.productType);
cout << "Enter manufacturer name: ";
getline(cin,product.manufacturerName);
cout << "Enter product name: ";
getline(cin,product.productName);
bool x = searchInDatabaseX();
return x;
}
void searchInDatabase(string barcode){
ifstream database("database.txt");
string type;
string manufacturerName;
string productName;
string barcodeinFile;
bool foundInfo = false;
while(database >> type >> manufacturerName >> productName >> barcodeinFile){
if(barcode==barcodeinFile){
cout << "Product Type: " << type << endl;
cout << "Manufacturer Name: " << manufacturerName << endl;
cout << "Product Name: " << productName << endl;
foundInfo=true;
}
}
if(foundInfo==false){
cout << "\nProduct not in database\n\n ";
}
// return foundInfo;
}
bool searchInDatabaseX(){
ifstream database("database.txt");
string type;
string manufacturerName;
string productName;
string barcodeinFile;
bool foundInfo = false;
while(database >> type >> manufacturerName >> productName >> barcodeinFile){
if(type==product.productType && manufacturerName==product.manufacturerName && productName==product.productName){
cout << "Product already exists in Database. Corresponding barcode is " << barcodeinFile << "\n";
foundInfo=true;
}
}
return foundInfo;
}
bool searchInDatabaseX(string barcode){
ifstream database("database.txt");
string type;
string manufacturerName;
string productName;
string barcodeinFile;
bool foundInfo = false;
while(database >> type >> manufacturerName >> productName >> barcodeinFile){
if(barcode==barcodeinFile){
foundInfo=true;
}
}
return foundInfo;
}
char iToC(int perDigit){
return (perDigit+48);
}
int cToiD(char x){
return (int)(x-48);
}
char getCheckDigit(string barcode ){
int total=0;
for(int i=1;i<=11;i=i+2){
total = total + (int)(cToiD(barcode[i]));
}
total = 3 * total;
for(int j =0;j<=10;j=j+2){
total = total + (int)(cToiD(barcode[j]));
}
total = total%10;
if(total==0){
total=10;
}
total = 10-total;
//cout << total << "\n\n\n";
char checkDigit;
checkDigit=(char)(iToC(total));
return checkDigit;
}
string generateBarcode(int increment,int secondIncrement){
string barcode="";
int perDigit;
int extra=secondIncrement;
for(int i=1;i<=12;i++){
perDigit = i+increment;
if(perDigit>=10){
perDigit=perDigit%extra;
secondIncrement--;
if(secondIncrement==0){
secondIncrement=10;
}
}
barcode= barcode + (char)(iToC(perDigit));
}
barcode =barcode + (char)(getCheckDigit(barcode));
bool foundInfo = searchInDatabaseX(barcode);
//cout << endl<<foundInfo << endl;
if(foundInfo){
increment+=1;
barcode = generateBarcode(increment,secondIncrement);
}
//encode(barcode);
return barcode;
}
void setInFile(){
ofstream database("database.txt",ios::app);
bool check = askForProduct();
if(check==false){
string barcodeMain = generateBarcode(0,10);
database << product.productType << ' ' << product.manufacturerName << ' ' << product.productName << ' ' << barcodeMain << endl;
//cout << barcodeMain << endl;
encode(barcodeMain);
}// cout << barcodeMain << endl;
}
void createDatabase (){
setInFile();
}