-
Notifications
You must be signed in to change notification settings - Fork 0
/
Item.cpp
207 lines (205 loc) · 5.5 KB
/
Item.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
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
#include "Item.h"
#include<iostream>
#include<windows.h>
using namespace std;
void InLine2(int n){
for (int i = 0; i < n; i++)
cout<<"=";
}
void TextColor2(int x)
{
HANDLE mau;
mau = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(mau,x);
}
std::ostream& operator <<(std::ostream &out ,const Item &p){
out<<p.maItem<<endl;
out<<p.tenItem<<endl;
out<<p.price<<endl;
return out;
}
bool check_exist(const Item &x){
Item* p=new Item[1000];
int n=getInfo(p);
for (int i=0;i<n-1;i++){
if (x.maItem==p[i].maItem) return 1;
}
return 0;
delete [] p;
}
std::istream& operator >>(std::istream &in,Item &p){
cout<<"Nhap ma mat hang: ";getline(cin,p.maItem);
while(check_exist(p)){
cout<<"Ma mat hang da duoc su dung, nhap lai: ";
getline(cin,p.maItem);
}
cout<<"Nhap ten mat hang: ";getline(cin,p.tenItem);
cout<<"Nhap gia: ";in>>p.price;
return in;
}
void Item::setmaItem(string maIteam){
this->maItem=maIteam;
}
string Item::getmaItem(){
return maItem;
}
void Item::setTenItem(string tenIteam){
this->tenItem=tenIteam;
}
string Item::getTenItem(){
return tenItem;
}
void Item::setPrice(int price){
this->price=price;
}
int Item::getprice(){
return price;
}
int getInfo(Item* p){
ifstream myFile;
myFile.open("Item/Item.txt");
if (!myFile.is_open()){
cout<<"Khong mo duoc file";
system("exit");
}
int total=0;
while (!myFile.eof()){
string line;
getline(myFile,p[total].maItem);
getline(myFile,p[total].tenItem);
getline(myFile,line);
stringstream geek(line);
geek>>p[total].price;
total++;
continue;
}
myFile.close();
return total;
}
void displayx(Item*p,string x){
int total=getInfo(p);cout<<endl;
for (int i=0;i<total-1;i++){
if(x==p[i].maItem){
cout<<setw(10)<<left<<"MaMH";
cout<<setw(30)<<left<<"Ten mon";
cout<<setw(20)<<left<<"Gia"<<endl;
cout<<setw(10)<<left<<p[i].maItem;
cout<<setw(30)<<left<<p[i].tenItem;
cout<<setw(20)<<left<<p[i].price<<endl;
}
}
}
void display(Item* p){
int n=getInfo(p);cout<<endl;
InLine2(21);TextColor2(11);
cout<<"Thuc Don";TextColor2(7);
InLine2(23); cout<<endl;
cout<<"+--------------------------------------------------+\n";
cout<<setw(10)<<left<<"| MaMH";
cout<<setw(26)<<left<<"| Ten mon";
cout<<setw(20)<<left<<"| Gia |"<<endl;
cout<<"+--------------------------------------------------+\n";
for (int i=0;i<n-1;i++){
cout<<"| "<<setw(7)<<left<<p[i].maItem;
cout<<"| "<<setw(22)<<left<<p[i].tenItem;
cout<<"| "<<setw(10)<<left<<p[i].price<<"|"<<endl;
}
cout<<"+--------------------------------------------------+\n";
}
void Add(Item *p){
add:
cout<<endl;InLine2(19);TextColor2(11);
cout<<"Them Item";TextColor2(7);
InLine2(19); cout<<endl;
Item x;
ofstream file2;
file2.open("Item/Item.txt",ios::app);
if(!file2.is_open()) return;
cin>>x;
file2<<x;
getInfo(p);
file2.close();
TextColor2(10);cout<<"\n\t Da them thanh cong\n";TextColor2(7);
cout<<"\nBan co muon tiep tuc them Item? (y/n) : ";
char t;cin>>t;
if(t=='y'){
system("cls");
goto add;
}
}
int check_maItem(Item *p,string ma){
int total=getInfo(p);cout<<endl;
int check=0;
for (int i=0;i<total-1;i++){
if(ma==p[i].maItem){
check=1;
}
}
return check;
}
void Delete(Item *p){
Del:
cout<<endl;InLine2(19);TextColor2(11);
cout<<"Xoa Item";TextColor2(7);
InLine2(19); cout<<endl;
string x;
cout<<"\nNhap ma cua mat hang ban muon xoa: ";cin>>x;
if(check_maItem(p,x)==0){
TextColor2(12); cout<<"Ma Item nay khong ton tai";TextColor2(7);
cout<<"\n\nBan co muon nhap lai ma Item ? (y/n) : ";
char t;cin>>t;
if(t=='y'){
system("cls");
goto Del;
}
}
else{
cout<<"\nThong tin Item muon xoa :";
displayx(p,x);
ifstream is("Item/Item.txt");
cout<<"Ban co chac muon xoa :";
TextColor2(14);cout<<"\n\n\t -Notification-";TextColor2(7);
cout<<"\n +--------------------------------+";
cout<<"\n | 1.CO 2.KHONG |";
cout<<"\n +--------------------------------+";
char a;
cout<<"\n\nNhap lua chon : ";cin>>a;
switch (a){
case '1' :
{
ofstream file2;
file2.open("temp.txt", ofstream::out);
int n=getInfo(p);
int i;
for (i=0;i<n-1;i++){
if(x==p[i].maItem) break;
}
for(int j=0;j<n-1;j++){
if(j!=i){
file2<<p[j];
}
}
TextColor2(10);cout<<"\n\n\t Da xoa thanh cong\n";TextColor2(7);
file2.close();
is.close();
remove("Item/Item.txt");
rename("temp.txt", "Item/Item.txt");
cout<<"\nBan co muon tiep tuc xoa Item (y/n) :" ;char d;cin>>d;
if(d=='y'){
system("cls");
goto Del;
}
break;
}
case '2':
{
cout<<"\nBan co muon tiep tuc xoa Item (Y/N) :" ;char d;cin>>d;
if(d=='Y'){
system("cls");
goto Del;
}
break;
}
}
}
}