Upload files to "/"

main
Rinats Andrejevs 2024-04-26 11:02:53 +00:00
parent 18a6da4a6e
commit 93611c81c1
1 changed files with 299 additions and 0 deletions

299
FileName.cpp 100644
View File

@ -0,0 +1,299 @@
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <windows.h>
#include <limits>
using namespace std;
class User { // Product class declaration with public variables
public: //Product order number in the menu
string name, password, date;
double number{};//Product name and manufacturing date //Product price
};
vector<string> split(string str, char delimeter) { //Function to split rows in txt file
vector<string> result;
stringstream ss(str);
string item;
while (getline(ss, item, delimeter))
{
result.push_back(item);
}
return result;
}
void show_menu(vector<User>& menu) { //Function to write out menu file content in console
cout << "\nVending machine menu: ";
for (User i : menu)
{
cout << "\n\nUser number: " << i.number
<< "\nUser name: " << i.name;
}
}
/*
void fill_menu(vector<Product>& menu) { //Function to fill menu vector with products objects
ifstream myfile;
myfile.open("menu.txt");
string data;
vector<string> row;
if (myfile.is_open())
{
while (myfile)
{
getline(myfile, data);
row = split(data, ','); //Splitting text in menu.txt file
if (row.size() > 1)
{
Product product;
product.number = stoi(row[0]);
product.name = row[1];
product.date = row[2];
product.price = stod(row[3]);
menu.push_back(product);
}
}
}
else
{
cout << "Error: couldnt open menu file\n";
}
myfile.close();
}
*/
void add_user(vector<User>& menu) { //Function to add user
cout << "Adding new User\n";
string name, password;
int last_product_number{};
cout << "\nEnter new user name: ";
cin >> name;
cout << "\nEnter new user password: ";
cin >> password;
User user; //Declaring an object
last_product_number = menu[menu.size() - 1].number; //Getting last user number by its size
user.number = last_product_number + 1;
user.name = name;
user.password = password;
menu.push_back(user);
cout << "\nNew User have been succesfully added to the list";
}
void edit_user(vector<User>& menu) { //Function to edit user details
show_menu(menu);
cout << "Editing user\n";
cout << "\nEnter number of user you want to edit: ";
int number;
cin >> number;
for (int i = 0; i < menu.size(); i++)
{
if (menu[i].number == number)
{
cout << "\n\nUser number: " << number //Program write current details about the user he want to edit to console
<< "\nCurrent user name: " << menu[i].name
<< "\nCurrent product password: " << menu[i].password;
cout << "\n\nEnter new name for user: ";
cin >> menu[i].name; //entering new user details
cout << "\nEnter new password for user: ";
cin >> menu[i].password;
}
else if (number > menu.size() || number < 0)
{
cout << "\n\nInvalid user number!\n";
break;
}
}
}
void remove_user(vector<User>& menu) { //Function to remove User from the menu
int choice = MessageBox(0, (LPCWSTR)L"Are you sure that you want to remove a User from the menu?", (LPCWSTR)L"Confirmation", MB_YESNO | MB_ICONQUESTION);
if (choice == IDYES)
{
show_menu(menu);
cout << "Enter number of User to remove: ";
int number;
cin >> number;
for (int i = 0; i < menu.size(); i++)
{
if (menu[i].number == number)
{
menu.erase(menu.begin() + i); //Delete the row with the User from the menu
cout << "\nUser is deleted from the menu!\n";
}
else if (number > menu.size() || number < 0)
{
cout << "\n\nInvalid User number!\n";
break;
}
}
}
else {
return;
}
}
void addRecord(vector<User>& menu) { //Function to add financial record
int counter = 0, answer = 1;
double revenue = 0, loss = 0;
vector<string> records_name;
vector<double> records_price;
fstream myfile("records.txt", ios::app);
/*
cout << "Enter date (YYYY-MM-DD): ";
cin >> menu.date;
cout << "Enter revenue: ";
cin >> record.revenue;
cout << "Enter profit: ";
cin >> record.profit;
*/
myfile.close();
}
void records_report() { //Writing records info from the records.txt file
string line;
ifstream myfile("records.txt"); //Reading from the file
while (getline(myfile, line)) {
cout << "\n" << line;
}
myfile.close();
}
void add_edit_remove_menu(vector<User>& menu) { //Function to display add/menu/remove menu from workers menu
int answer;
cout << "\n\nInventory configuration menu\n\n1. Add User\n2. Edit User\n3. Remove User\n4. Return to worker menu\n\nEnter your option and press return: ";
cin >> answer;
switch (answer)
{
case 1:
add_user(menu);
break;
case 2:
edit_user(menu);
break;
case 3:
remove_user(menu);
break;
case 4:
return;
default: //Handling not valid and non numeric input
cin.clear();
cin.ignore(9999, '\n');
cout << "\nPlease enter valid option!\n";
break;
}
}
void client_menu(vector<User>& menu) { //Function to display users menu
int answer;
cout << "\nClient menu\n\n1. Add Record\n2. Back to main menu\n\nEnter your option and press return: ";
cin >> answer;
switch (answer) {
case 1:
addRecord(menu);
break;
case 2:
return;
default: //Handling not valid and non numeric input
cin.clear();
cin.ignore(9999, '\n');
cout << "\nPlease enter valid option!\n";
break;
}
}
void user_menu(vector<User>& menu) { //Function to display workers menu
while (true) {
int answer;
cout << "\nWorker menu\n\n1. Check records history\n2. Add/Edit/Remove record\n3. Show menu\n4. Back to main menu\nEnter your option and press return: ";
cin >> answer;
switch (answer)
{
case 1:
records_report();
break;
case 2:
add_edit_remove_menu(menu);
break;
case 3:
show_menu(menu);
break;
case 4:
return;
default: //Handling not valid and non numeric input
cin.clear();
cin.ignore(9999, '\n');
cout << "\nPlease enter valid option!\n";
break;
}
}
}
void worker_menu_access(vector<User>& menu) { //Function asks password (1234) from an user to access the worker menu or 0 to return back
int access_code = 0;
do {
cout << "\n\nEnter code to gain access to the worker menu or 0 to return back (format: 0000): ";
cin >> access_code;
if (access_code == 1234 && to_string(access_code).length() == 4) {
user_menu(menu);
break;
}
else if (access_code != 1234 && access_code != 0 || to_string(access_code).length() != 4 && access_code != 0) {
cout << "\nIncorrect password or wrong format ";
}
else if (access_code == 0) {
return;
}
else if (cin.eof()) {
cin.clear();
cin.ignore(9999, '\n');
cout << "\nPlease enter valid option!\n";
break;
}
} while (access_code != 1234 || access_code != 0);
}
int main() {
vector<User> menu;
int answer = 0;
//fill_menu(menu);
ofstream myfile("records.txt", ios::app); //Creating bills text file
myfile.close();
do {
cout << "\n\Accounting software\n\n1. Create new user\n2. Log in user\n3. Exit\n\nEnter your option and press return: ";
cin >> answer;
switch (answer)
{
case 1:
client_menu(menu);
break;
case 2:
worker_menu_access(menu);
break;
case 3:
exit(0);
default: //Handling not valid and non numeric input
cin.clear();
cin.ignore(9999, '\n');
cout << "\nPlease enter valid option!\n";
break;
}
} while (answer != 3);
myfile.close();
return 0;
}