Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string.h>
- #include <vector>
- #include <algorithm>
- #include <cmath>
- #include <conio.h>
- using namespace std;
- int holder_re = 1;
- // class order
- // {
- // public:
- // int id, price;
- // char name[10];
- // };
- class order
- {
- public:
- int id, price, amount;
- char name[10];
- order()
- {
- id = price = 0;
- amount = 1;
- strcpy(name, "");
- }
- };
- class admins
- {
- public:
- char name[20], password[20];
- };
- void coutWell(string s, bool no)
- {
- int space = 25;
- int ast = 50;
- int n = s.size();
- if (no)
- s = '~' + s;
- int last = 7;
- for (int j = 0; j < last; j++)
- {
- for (int i = 0; i < space; i++)
- cout << " ";
- if (j == 0 || j == last - 1)
- {
- for (int i = 0; i < ast; i++)
- cout << "*";
- }
- else if (j == last / 2)
- {
- cout << '*';
- int test = ast - 2;
- test /= 2;
- test -= n / 2;
- for (int i = 0; i < test; i++)
- cout << " ";
- cout << s;
- if (no == 0)
- test--;
- for (int i = 0; i < ast - test - n - 3; i++)
- cout << " ";
- cout << "*";
- }
- else
- {
- cout << "*";
- for (int i = 0; i < ast - 2; i++)
- cout << " ";
- cout << "*";
- }
- for (int i = 0; i < space; i++)
- cout << " ";
- cout << "\n";
- }
- }
- void coutProducts(vector<string> &s, bool no)
- {
- int ast = 100;
- int n = s.size();
- for (int j = 0; j < n + 2; j++)
- {
- for (int i = 0; i < 20; i++)
- cout << " ";
- if (j == 0 || j == n + 1)
- {
- for (int i = 0; i < ast; i++)
- cout << "*";
- cout << "\n";
- continue;
- }
- if (!no)
- {
- int cnt = 6 + (int)log10(j) + 1;
- cout << "* " << j << " - ";
- for (int i = 0; i < (int)s[j - 1].size(); i++)
- {
- if (i < s[j - 1].size())
- cout << s[j - 1][i], cnt++;
- }
- for (int i = 0; i < ast - cnt; i++)
- cout << " ";
- cout << "*\n";
- }
- else
- {
- string test;
- int cnt = 3;
- cout << "* ";
- for (int i = 0; i < (int)s[j - 1].size(); i++)
- {
- if (s[j - 1][i] != ' ')
- {
- while (i < (int)s[j - 1].size() && s[j - 1][i] != ' ')
- {
- test += s[j - 1][i];
- i++;
- cnt++;
- }
- i--;
- cout << test;
- for (int k = 0; k < 20 - (int)test.size(); k++)
- {
- cout << " ";
- cnt++;
- }
- test.clear();
- }
- else
- continue;
- }
- for (int i = 0; i < ast - cnt; i++)
- cout << " ";
- cout << "*\n";
- }
- }
- }
- // *******************************************************ADMIN INTERFACE*******************************************************
- // add admin
- void addadmin()
- {
- system("cls");
- coutWell("SIGNUP", 1);
- fstream outfile;
- outfile.open("admin.txt", ios::out | ios::app);
- admins s1;
- cout << "Enter Your USERNAME: ";
- cin >> s1.name;
- char test[20];
- bool again = false;
- do
- {
- if (again)
- cout << "\n\nTwo PASSWORDS are not the same, please try again...\n";
- cout << "Enter Your PASSWORD: ";
- for (int i = 0; i < 20; i++)
- {
- s1.password[i] = _getch();
- if (s1.password[i] == 13)
- {
- s1.password[i] = '\0';
- break;
- }
- putch('*');
- }
- char test2[20];
- strcpy(test, test2);
- cout << "\nEnter Your PASSWORD again: ";
- for (int i = 0; i < 20; i++)
- {
- test[i] = _getch();
- if (test[i] == 13)
- {
- test[i] = '\0';
- break;
- }
- putch('*');
- }
- } while (strcmp(test, s1.password) && (again = true));
- outfile.write((char *)&s1, sizeof(s1));
- outfile.close();
- system("cls");
- cout << "Signed up successfully, please reopen again";
- for (int i = 0; i < 500000000;)
- {
- if (i == 100000000 || i == 200000000 || i == 300000000 || i == 400000000)
- cout << ".";
- i++;
- if (i == 500000000 - 1)
- cout << "\n";
- }
- }
- // check if admin.txt isn't empty txt file (if it's empty, we must add an admin with the previous function)
- bool check_empty()
- {
- fstream infile;
- infile.open("admin.txt", ios::in);
- if (infile.peek() == EOF)
- {
- infile.close();
- return false;
- }
- infile.close();
- return true;
- }
- // check if given id matches given password in admin.txt (if wrong 3 times then the program will be closed)
- bool check_admin()
- {
- char i[20];
- char s[20];
- cout << "Enter Your USERNAME: ";
- cin >> i;
- cout << "Enter PASSWORD: ";
- for (int i = 0; i < 20; i++)
- {
- s[i] = _getch();
- if (s[i] == 13)
- {
- s[i] = '\0';
- break;
- }
- putch('*');
- }
- fstream infile;
- infile.open("admin.txt", ios::in);
- admins s1;
- while (!infile.eof())
- {
- infile.read((char *)&s1, sizeof(s1));
- if (strcmp(s1.name, i) == 0)
- {
- if (strcmp(s1.password, s) == 0)
- {
- infile.close();
- return true;
- }
- else
- {
- infile.close();
- return false;
- }
- }
- }
- infile.close();
- return false;
- }
- void readorder()
- {
- system("cls");
- order s1;
- ifstream read;
- read.open("order.txt", ios::in);
- vector<string> s;
- if (read.is_open())
- {
- s.push_back("ID Name price");
- read.read((char *)&s1, sizeof(s1));
- while (!read.eof())
- {
- s.push_back(to_string(s1.id) + " " + s1.name + " " + to_string(s1.price));
- read.read((char *)&s1, sizeof(s1));
- }
- read.close();
- coutProducts(s, 1);
- }
- else
- {
- coutWell("the file can not open", 1);
- }
- }
- void writeorder()
- {
- system("cls");
- coutWell("New Order", 1);
- fstream outfile;
- outfile.open("order.txt", ios::out | ios::app);
- char c;
- do
- {
- order s1;
- cout << "Enter ID: ";
- cin >> s1.id;
- cout << "Enter NAME: ";
- cin >> s1.name;
- cout << "Enter price: ";
- cin >> s1.price;
- outfile.write((char *)&s1, sizeof(s1));
- cout << "Enter another order (y/n): ";
- cin >> c;
- } while (c == 'y');
- outfile.close();
- }
- void search()
- {
- system("cls");
- char str[10];
- cout << "Enter name to search: " << endl;
- cin >> str;
- bool found = false;
- order s1;
- ifstream read;
- read.open("order.txt", ios::in);
- if (read.is_open())
- {
- read.read((char *)&s1, sizeof(s1));
- while (!read.eof())
- {
- if (strcmp(str, s1.name) == 0)
- {
- cout << "ID\tName\tprice" << endl;
- cout << s1.id << "\t" << s1.name << "\t" << s1.price << endl;
- found = true;
- }
- read.read((char *)&s1, sizeof(s1));
- }
- if (!found)
- {
- cout << "name not found " << endl;
- }
- read.close();
- }
- else
- {
- cout << "the file can not open" << endl;
- }
- char contin;
- cout << "Enter any key to continue.... ";
- cin >> contin;
- }
- void updateorder()
- {
- readorder();
- char str[10];
- cout << "Enter name for search: " << endl;
- cin >> str;
- bool found = false;
- order s1;
- fstream in;
- in.open("order.txt", ios::in | ios::out);
- if (in.is_open())
- {
- in.read((char *)&s1, sizeof(s1));
- while (!in.eof())
- {
- if (strcmp(str, s1.name) == 0)
- {
- cout << "Enter the new price of:" << str << " ";
- cin >> s1.price;
- int curPos = in.tellg();
- int stuSize = sizeof(s1);
- in.seekp(curPos - stuSize, ios::beg);
- in.write((char *)&s1, sizeof(s1));
- in.seekp(curPos - stuSize, ios::beg);
- in.read((char *)&s1, sizeof(s1));
- cout << "ID\tName\tprice" << endl;
- cout << s1.id << "\t" << s1.name << "\t" << s1.price << endl;
- found = true;
- }
- in.read((char *)&s1, sizeof(s1));
- }
- if (!found)
- {
- cout << "name not found " << endl;
- }
- in.close();
- }
- else
- {
- cout << "the file can not open" << endl;
- }
- char contin;
- cout << "Enter any key to continue.... ";
- cin >> contin;
- }
- void copyFile()
- {
- ifstream in("order.txt"); // order = text
- ofstream out("copy text.txt");
- if (in.is_open())
- {
- char ch;
- while (in.get(ch))
- out.put(ch);
- in.close();
- out.close();
- }
- cout << "Copied successfully ";
- for (int i = 0; i < 300000000; i++)
- {
- if (i == 100000000 || i == 200000000)
- cout << ".";
- }
- cout << "\n";
- }
- void deleteorder()
- {
- readorder();
- char str[10];
- cout << "Enter name for search: " << endl;
- cin >> str;
- bool found = false;
- order s1;
- ifstream in("order.txt", ios::in);
- ofstream out("temp.txt", ios::out);
- if (in.is_open())
- {
- in.read((char *)&s1, sizeof(s1));
- while (!in.eof())
- {
- if (strcmp(str, s1.name) != 0)
- {
- out.write((char *)&s1, sizeof(s1));
- found = true;
- }
- in.read((char *)&s1, sizeof(s1));
- }
- if (!found)
- {
- cout << "name not found " << endl;
- }
- in.close();
- out.close();
- remove("order.txt");
- rename("temp.txt", "order.txt");
- }
- else
- {
- cout << "the file can not open" << endl;
- }
- readorder();
- }
- void fix()
- {
- fstream file;
- file.open("order.txt", ios::in | ios::out);
- ofstream testt;
- vector<order> nums;
- order o1;
- if (file.is_open())
- {
- while (!file.eof())
- {
- file.read((char *)&o1, sizeof(o1));
- nums.push_back(o1);
- }
- file.close();
- testt.open("order.txt", ios::out);
- for (int i = 0; i < (int)nums.size() - 1; i++)
- {
- nums[i].id = i + 1;
- testt.write((char *)&nums[i], sizeof(nums[i]));
- }
- testt.close();
- }
- }
- // *****************************************************USER INTERFACE****************************************************
- // shows user's selections
- void readselections()
- {
- order s1;
- ifstream read;
- read.open("selections.txt", ios::in);
- vector<string> s;
- if (read.is_open())
- {
- s.push_back("ID Name price amount");
- read.read((char *)&s1, sizeof(s1));
- while (!read.eof())
- {
- s.push_back(to_string(s1.id) + " " + s1.name + " " + to_string(s1.price) + " " + to_string(s1.amount));
- read.read((char *)&s1, sizeof(s1));
- }
- read.close();
- coutProducts(s, 1);
- }
- else
- {
- coutWell("the file can not open", 1);
- }
- }
- // function that counts all the prince in selections.txt and return the total price
- int total_price()
- {
- int total = 0;
- order s1;
- ifstream read;
- read.open("selections.txt", ios::in);
- if (read.is_open())
- {
- read.read((char *)&s1, sizeof(s1));
- while (!read.eof())
- {
- total += (s1.price * s1.amount);
- read.read((char *)&s1, sizeof(s1));
- }
- read.close();
- }
- return total;
- }
- // copy selections.txt to temp.txt to count the new amount of each item after adjusting the amount
- void increase_amount(char name[], int amount, bool adding = true)
- {
- order s1;
- ifstream read;
- read.open("selections.txt", ios::in);
- if (read.is_open())
- {
- read.read((char *)&s1, sizeof(s1));
- while (!read.eof())
- {
- if (strcmp(s1.name, name) == 0)
- {
- if (amount == s1.amount)
- {
- read.read((char *)&s1, sizeof(s1));
- continue;
- }
- else if (adding == true)
- s1.amount += amount;
- else
- {
- while (amount > s1.amount and !adding)
- {
- cout << "Invalid amount please try again: ";
- cin >> amount;
- }
- if (amount == s1.amount)
- {
- read.read((char *)&s1, sizeof(s1));
- continue;
- }
- s1.amount -= amount;
- }
- }
- fstream outfile;
- outfile.open("temp.txt", ios::out | ios::app);
- outfile.write((char *)&s1, sizeof(s1));
- outfile.close();
- read.read((char *)&s1, sizeof(s1));
- }
- read.close();
- }
- remove("selections.txt");
- rename("temp.txt", "selections.txt");
- }
- // we search for the giving id in order.txt file so we get its name and send it to the previous function (changing the flag to false)
- void delete_selection(int id, int amount)
- {
- int i = 1;
- order s1;
- ifstream read;
- read.open("order.txt", ios::in);
- if (read.is_open())
- {
- read.read((char *)&s1, sizeof(s1));
- while (!read.eof())
- {
- if (s1.id == id)
- {
- // if we didn't change to false he'll add the amount not subtract it
- increase_amount(s1.name, amount, false);
- return;
- }
- read.read((char *)&s1, sizeof(s1));
- }
- read.close();
- }
- }
- // read all the selection file and determine if the item is already in or not and if it is we add the amount to it
- bool search_name_selection(char name[])
- {
- order s1;
- ifstream read;
- read.open("selections.txt", ios::in);
- if (read.is_open())
- {
- read.read((char *)&s1, sizeof(s1));
- while (!read.eof())
- {
- if (strcmp(s1.name, name) == 0)
- {
- read.close();
- return true;
- }
- read.read((char *)&s1, sizeof(s1));
- }
- read.close();
- }
- return false;
- }
- // we get the id and amount of the item that user wants and check with the functions we have it's already exists or not
- // and if it's not we add it to the selections.txt file and if it's already exists we add the amount to it
- void increase_amount_id()
- {
- int id, amount, id_holder;
- cout << "what is the order you want: ";
- cin >> id;
- cout << "how many do you want: ";
- cin >> amount;
- order s1;
- ifstream read;
- read.open("order.txt", ios::in);
- if (read.is_open())
- {
- read.read((char *)&s1, sizeof(s1));
- while (!read.eof())
- {
- if (s1.id == id)
- {
- if (search_name_selection(s1.name))
- increase_amount(s1.name, amount);
- else
- {
- fstream outfile;
- s1.amount = amount;
- outfile.open("selections.txt", ios::out | ios::app);
- outfile.write((char *)&s1, sizeof(s1));
- outfile.close();
- s1.amount = 1;
- break;
- }
- }
- read.read((char *)&s1, sizeof(s1));
- }
- read.close();
- }
- }
- // delete all the selections because the user finished the order
- void empty_selections()
- {
- fstream outfile;
- outfile.open("selections.txt", ios::out | ios::trunc);
- outfile.close();
- }
- // otro? m4 3arf asmha aeh lol (exiting interface)
- void complete_order()
- {
- system("cls");
- cout << "The total price is: " << total_price() << endl;
- cout << "Thank you for visiting our restruant" << endl;
- cout << "Hope for you a good day" << endl;
- for (int i = 0; i < 500000000; i++)
- {
- if (i == 100000000 || i == 200000000 || i == 300000000 || i == 400000000)
- cout << ".";
- }
- cout << "\n";
- empty_selections();
- }
- //******************************************************************************************************
- int main()
- {
- int admin;
- lable:
- system("cls");
- coutWell("1- Admin // 2- customer", 0);
- cout << "Please enter you choice : ";
- cin >> admin;
- char ch;
- if (admin == 1)
- {
- char add_anoter_admin = 'n';
- if (!check_empty())
- {
- addadmin();
- }
- else
- {
- system("cls"),
- coutWell("SIGNIN", 1);
- int cnt = 0;
- retry:
- bool flag = check_admin();
- if (!flag)
- {
- if (cnt > 0)
- system("cls"),
- coutWell("SIGNIN", 1);
- cout << "invalid id or password" << endl;
- cnt++;
- if (cnt == 3)
- {
- cout << "you have entered wrong password 3 times" << endl;
- return 0;
- }
- goto retry;
- }
- system("CLS");
- if (flag)
- {
- do
- {
- system("CLS");
- vector<string> s =
- {
- "add a new order",
- "view the menu",
- "search in menu",
- "update the menu",
- "copy the menu",
- "delete an order",
- "Sign out"};
- coutProducts(s, 0);
- cout << "Select your choice: ";
- cin >> ch;
- switch (ch)
- {
- case '1':
- writeorder();
- fix();
- break;
- case '2':
- readorder();
- char x;
- cout << "Enter any key to continue..... ";
- cin >> x;
- break;
- case '3':
- search();
- break;
- case '4':
- updateorder();
- fix();
- break;
- case '5':
- copyFile();
- break;
- case '6':
- deleteorder();
- fix();
- break;
- case '7':
- goto lable;
- break;
- default:
- cout << "invalid choice" << endl;
- }
- } while (ch != '7');
- }
- }
- }
- else
- {
- // customer view
- do
- {
- menu:
- system("CLS");
- vector<string> s = {
- "Welcome",
- "1 - View menu",
- "2 - Sign out"};
- coutProducts(s, 1);
- cin >> ch;
- switch (ch)
- {
- case '1':
- int tomenu;
- cout << "\n\n";
- readorder();
- cout << "\n\n";
- cout << "1. add an order: " << '\n';
- cout << "2. show selected order: " << '\n';
- cout << "3. go back to main menu: " << '\n';
- cout << "4. complete order: " << '\n';
- cout << "\n\nyour total price is: " << total_price() << '\n';
- tryagain:
- cout << "Enter your choice: ";
- cin >> tomenu;
- if (tomenu == 1)
- increase_amount_id();
- else if (tomenu == 2)
- {
- system("CLS");
- readselections();
- int holder;
- cout << "\n\n";
- cout << "1. go back to main menu: \n";
- cout << "2. delete an order: \n";
- cout << "3. complete order: \n";
- cout << "Enter your choice: ";
- cin >> holder;
- if (holder == 1)
- goto menu;
- else if (holder == 2)
- {
- system("CLS");
- readselections();
- int id, amount;
- cout << "Enter the id of the order you want to delete: ";
- cin >> id;
- cout << "Enter the amount of the order you want to delete: ";
- cin >> amount;
- delete_selection(id, amount);
- }
- else if (holder == 3)
- complete_order();
- else
- cout << "invalid choice";
- }
- else if (tomenu == 3)
- goto menu;
- else if (tomenu == 4)
- complete_order();
- else
- {
- cout << "invalid choice" << endl;
- goto tryagain;
- }
- goto menu;
- break;
- case '2':
- goto lable;
- break;
- default:
- cout << "invalid choice" << endl;
- }
- } while (ch != '2');
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment