-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.cpp
120 lines (97 loc) · 2.73 KB
/
admin.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
#include "admin.h"
#include"menu.h"
#include"fstream"
Admin::Admin(string id, string name, string password)
: id(id), name(name), password(password), adminData{"admin123","admin","adminPassword" }
{
}
bool Admin::login()
{
return (adminData[0] == id &&adminData[2] == password);
}
void Admin::updatePrice()
{
Menu M;
vector<string> items = M.getItems();
vector<int> prices = M.getPrice();
if (items.empty())
{
cout << "Menu is empty. Nothing to update." << endl;
return;
}
int itemNum;
cout << "Enter the item number to update price: ";
cin >> itemNum;
cin.ignore();
if (itemNum >= 1 && static_cast<size_t>(itemNum) <= items.size())
{
int newPrice;
itemNum -= 1;
cout << "Enter the new price for " << items[itemNum] << ": ";
cin >> newPrice;
cin.ignore();
prices[itemNum] = newPrice;
ofstream writeFile("menuData.txt");
if (!writeFile.is_open())
{
cout << "Error opening file for writing." << endl;
return;
}
for (size_t i = 0; i < items.size(); ++i)
{
writeFile << items[i] << " " << prices[i] << endl;
}
writeFile.close();
cout << "Price updated successfully." << endl;
}
else
{
cout << "Invalid item number." << endl;
}
}
void Admin::addMoneyToUser()
{
string userRollNo, userName;
cout << "Enter the Roll No and name of the user to add money: ";
cin >> userRollNo;
cin.ignore();
cin >> userName;
ifstream readFile("usersData.txt");
if (!readFile.is_open())
{
cout << "Error opening users file for reading." << endl;
return;
}
string name, rollNo, password;
int balance;
bool found = false;
string updatedData;
while (readFile >> name >> rollNo >> password >> balance)
{
if (rollNo == userRollNo && password == userName)
{
int amount;
cout << "Enter the amount to add to user's balance: ";
cin >> amount;
cin.ignore();
balance += amount;
found = true;
}
updatedData += name + " " + rollNo + " " + password + " " + to_string(balance) + "\n";
}
readFile.close();
if (!found)
{
cout << "User with Roll No " << userRollNo << " not found." << endl;
return;
}
// Rewrite the entire file with the updated data
ofstream writeFile("usersData.txt");
if (!writeFile.is_open())
{
cout << "Error opening users file for writing." << endl;
return;
}
writeFile << updatedData;
cout << "Money added to the user's balance. Updated balance: " << balance << " INR" << endl;
}