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
|
#include <iostream>
#include<fstream>
#include <stdlib.h>
using namespace std;
void get_data();
void show_data();
void select_Items();
void calculate();
class product // class for product
{
int pno;
char name[50];
float price;
public:
struct menuitem //definition of a struct to store data about resturant
{
string menu_list;
double price;
};
menuitem menu[16]; //Instance of a struct to store data of 15 resturant items
void get_data(); //prototype of a function to loads data about the items in struct
void show_data(); // prototype of a function show the loaded data
void select_Items(); //prototype of a function to select the items
void calculate(); //prototype of a function to calculate the bill
int c[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
void create_product() // function to create a product
{
cout << "\n Enter The Product No. of The Product ";
cin >> pno;
cout << "\n Enter The Name of The Product ";
cin >> name;
cout << "\n Enter The Price of The Product ";
cin >> price;
}
void show_product() // function to display product
{
cout << "\n The Product No. of the product : " << pno;
cout << "\n The Name of The Product : ";
puts(name);
cout << " Price of the product : " << price;
}
int retpno() // functions for returning values of stored in protected variables
{
return pno;
}
float retprice()
{
return price;
}
char* retname()
{
return name;
}
};
fstream fp; // variable for file stream
product pr; // variable of product type
void write_product() // function for writing product to file
{
fp.open("meal.txt", ios::out | ios::app); // filed open for output to file with appending
pr.create_product();
fp.write((char*)&pr, sizeof(product)); //type casting class product variable pr into character type and storing in file
fp.close();
cout << "\n\n The Product Has Been Created ";
}
void display_all() // fucntion for displaying record from file
{
cout << "\n\n\n\t\t+++Displaying all record+++\n\n";
fp.open("meal.txt", ios::in);
while (fp.read((char*)&pr, sizeof(product)))
{
pr.show_product();
cout << "\n\n====================================\n";
}
fp.close();
}
void display_sp(int n) // function for displaying specific record from file
{
int flag = 0;
fp.open("meal.txt", ios::in);
while (fp.read((char*)&pr, sizeof(product)))
{
if (pr.retpno() == n) // using if condition to find record by matching with input
{
pr.show_product(); // displaying product record if found
flag = 1;
}
}
fp.close();
if (flag == 0)
cout << "\n\nNo record with this number found";
}
void modify_product() // function to modify product by overwriting it
{
int no, found = 0;
cout << "\n\n\tTo Modify ";
cout << "\n\n\tPlease Enter The Product No. of The Product";
cin >> no;
fp.open("meal.txt", ios::in | ios::out);
while (fp.read((char*)&pr, sizeof(product)) && found == 0) // search for record until whole file is read and until found flag is 0
{
if (pr.retpno() == no)
{
pr.show_product();
cout << "\nPlease Enter The New Details of Product" << endl;
pr.create_product();
int pos = -1 * static_cast<int>(sizeof(pr)); // type casting position of product record and overwriting new product at its place
fp.seekp(pos, ios::cur);
fp.write((char*)&pr, sizeof(product));
cout << "\n\n\t Record Updated";
found = 1;
}
}
fp.close();
if (found == 0)
cout << "\n\n Record Not Found ";
}
void delete_product() //function for deleting record from file
{
int no;
cout << "\n\n\n\tDelete Record";
cout << "\n\nPlease Enter The product no. of The Product You Want To Delete";
cin >> no;
fp.open("meal.txt", ios::in | ios::out); // placing all record except the one requested by user in temp file
fstream fp2;
fp2.open("cover.txt", ios::out);
fp.seekg(0, ios::beg);
while (fp.read((char*)&pr, sizeof(product)))
{
if (pr.retpno() != no)
{
fp2.write((char*)&pr, sizeof(product));
}
}
fp2.close();
fp.close();
remove("meal.txt"); // deleting current file
rename("cover.txt", "meal.txt"); // renaming temp file to current file name
cout << "\n\n\tRecord Deleted ..";
}
void admin_menu() // function For Admin to use
{
char ch2;
cout << "\n\n\tADMIN MENU";
cout << "\n\n\t1.CREATE PRODUCT";
cout << "\n\n\t2.DISPLAY ALL PRODUCTS";
cout << "\n\n\t3.QUERY ";
cout << "\n\n\t4.MODIFY PRODUCT";
cout << "\n\n\t5.DELETE PRODUCT";
cout << "\n\n\t6.BACK TO MAIN MENU";
cout << "\n\n\tPlease Enter Your Choice (1-6) ";
cin >> ch2;
switch (ch2) // using switch case statements for selection
{
case '1':
write_product();
break;
case '2':
display_all();
break;
case '3':
int num;
cout << "\n\n\tPlease Enter The Product No. ";
cin >> num;
display_sp(num);
break;
case '4':
modify_product();
break;
case '5':
delete_product();
break;
case '6':
break;
default:
cout << "\a";
admin_menu();
}
}
|