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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
void InputError();
struct drinks
{
long itemno;
char itemname[100];
int quantity;
double unitprice;
};
void InsertNewRecord()
{
ofstream outDrinks;
int choice;
drinks x;
if(outDrinks)
{
do
{
cout << "Please enter the new record " << endl;
cout << "New Item No: ";
cin >> x.itemno;
cout << "New Item Name: ";
cin >> x.itemname ;
cout << "New Quantity: ";
cin >> x.quantity ;
cout << "New Price: ";
cin >> x.unitprice ;
cout << "1. Save || 2. Cancel" << endl;
cin >> choice;
}
while (!(choice==1 || choice==2));
if (choice==1)
{
ofstream outsRecord("Drinks.txt",ios::app,ios::binary);
outsRecord.write((char*)&x,sizeof(drinks));
outsRecord.close();
cout << "Record Saved" << endl ;
}
else
{
cout << "Record Cancel" << endl;
}
}
else
{
cout << "Invalid File Opening" << endl;
outDrinks.close();
}
}
void DeleteRecord()
{
drinks x;
int num;
cout << "DELETE RECORD" << endl;
cout << "Please enter the record that you want to delete: ";
cin >> num;
ifstream Drinksa("Drinks.txt",ios::in,ios::binary);
int stop=0,c=0;
if(Drinksa)
{
while(Drinksa.read((char*)&x,sizeof(drinks)))
{
c++;
if((num==x.itemno))
{
stop=c;
}
}
}
else
cout << "Invalid File Opening" << endl;
Drinksa.close();
rename("Drinks.txt","Temp.txt");
int counter=0;
ofstream Drinks("Drinks.txt",ios::app,ios::binary);
ifstream temp("Temp.txt",ios::in,ios::binary);
if(temp)
{
while(temp.read((char*)&x,sizeof(drinks))){
counter++;
if((counter==stop))
{
cout << "Record Deleted" << endl;
}
else
Drinks.write((char*)&x,sizeof(x));
}
}
else
cout << "Invalid File Opening"<< endl;
Drinks.close();
temp.close();
system("Del Temp.txt");
}
void UpdateRecord()
{
fstream in, temp;
drinks x;
int num;
cout<<"UPDATE RECORD"<<endl;
cout << "Enter Item No : ";
cin >> num;
rename("drinks.txt","temp.txt");
in.open("drinks.txt");
temp.open("temp.txt");
while(temp.read((char*)&x,sizeof(drinks))) {
if (num == x.itemno)
{
cout << "Current Record is:" << endl ;
cout << "Item NO: " << x.itemno << endl ;
cout << "Item Name: " << x.itemname << endl ;
cout << "Quantity: " << x.quantity << endl ;
cout << "Unit Price: " << x.unitprice << endl ;
cout << "Please enter the new value" << endl ;
cout << "Update Item No: " << endl ;
cin >> x.itemno ;
cout << "Update Item Name: " << endl ;
cin >> x.itemname ;
cout << "Update Quantity: "<< endl ;
cin >> x.quantity ;
cout << "Update Unit Price: "<< endl ;
cin >> x.unitprice ;
}
in.write((char*)&x,sizeof(drinks));
}
in.close();
temp.close();
system("Del temp.txt");
}
void DisplayAllRecord()
{
drinks x;
ifstream iRecord("drinks.txt",ios::app,ios::binary);
if(iRecord)
{
cout << "Item No"<<setw(12)<<"Item Name"<<setw(14)<<"Quantity" <<setw(17)<<"Unit Price"<< endl;
while (iRecord.read((char*)&x,sizeof(drinks)))
{
cout <<x.itemno<<setw(12)<<x.itemname<<setw(14)<<x.quantity<<setw(17)<<x.unitprice<< endl;
}
}
else
cout <<"Error Not Found" << endl;
iRecord.close();
}
void Name()
{
drinks x;
int recordFound;
char InputName[100];
cout << "Enter Item Name: ";
cin >> InputName;
ifstream iRecord("drinks.txt");
if(iRecord)
{
while(!iRecord.eof())
{
iRecord.read((char*)&x,sizeof(drinks));
if(strcmp(InputName,x.itemname) == 0){
cout << "Item No"<<setw(12)<<"Item Name"<<setw(14)<<"Quantity" <<setw(17)<<"Unit Cost"<< endl;
cout <<x.itemno<<setw(18)<<x.itemname<<setw(12)<<x.quantity<<setw(16)<<x.unitprice<< endl;
recordFound = 1;
break;
}
else
recordFound = 0;
}
if (recordFound==0)
{
cout << "Record not found" << endl;
}
}
else
cout << "Invalid file Opening" << endl ;
iRecord.close();
}
void Number()
{
drinks x;
int recordFound;
int number;
cout << "Enter Item Number: " << endl;
cin >> number;
ifstream iRecord("drinks.txt");
if(iRecord)
{
while(!iRecord.eof())
{
iRecord.read((char*)&x,sizeof(drinks));
if(number == x.itemno){
cout << "Item No"<<setw(12)<<"Item Name"<<setw(14)<<"Quantity" <<setw(17)<<"Unit Cost"<< endl;
cout <<x.itemno<<setw(18)<<x.itemname<<setw(12)<<x.quantity<<setw(16)<<x.unitprice<< endl;
recordFound = 1;
break;
}
else
recordFound = 0;
}
if (recordFound==0)
{
cout << "Record not found" << endl;
}
}
else
cout << "Invalid file Opening" << endl ;
iRecord.close();
}
void DisplayRecord()
{
int num;
int choose;
cout << "1. Search with item name || 2. Search with item no. :";
cin >> choose;
switch(choose)
{
case 1:
Name();
break;
case 2:
Number();
break;
}
}
void Menu()
{
char choice;
cout << "INTI COLLEGE DRINK STALL" << endl;
cout << "Sarawak, Malaysia" << endl;
cout << "***********************" << endl << endl;
cout << "Choose one of the following menu: " << endl << endl;
cout << "(a) Display a record" << endl;
cout << "(b) Insert a new record" << endl;
cout << "(c) Delete a record" << endl;
cout << "(d) Update a record" << endl;
cout << "(e) Display all records" << endl;
cout << "(f) Exit" << endl << endl;
cout << "Please enter your choice: " ;
cin >> choice;
switch (tolower(choice))
{
case 'a':
DisplayRecord() ;
break;
case 'b':
InsertNewRecord();
break;
case 'c':
DeleteRecord();
break;
case 'd':
UpdateRecord();
break;
case 'e':
DisplayAllRecord() ;
break;
case 'f':
InputError();
break;
default:
cout << "Invalid Input!" ;
}
}
void InputError()
{
char choice;
cout << "Y: Exit || R: Return" << endl;
cout << "Do you want to Exit: ";
cin >> choice;
switch (tolower(choice))
{
case 'y':
cout << "Thank you for using this program. Have a nice day! " << endl;
break;
case 'r':
Menu();
}
}
|