-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassFunctions.cpp
More file actions
582 lines (494 loc) · 12 KB
/
Copy pathclassFunctions.cpp
File metadata and controls
582 lines (494 loc) · 12 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#pragma once
#include"goodsClass.h"
#include"main.h"
#include<iomanip>
//静态成员初始化
int Merchandise::MERSTOCKS[] = { 0,0,0,0 };
Merchandise::Merchandise() {
merName = "";
merId = "";
merPrice = 0;
merMfr = "";
merBrand = "";
merStatus = 0;
merClass = 0;
next = NULL;
MERSTOCKS[merClass - 1]++;
}
void Merchandise::initGood(chartMerchan &ListOfGoods) {
cout << endl;
printLongDash();
printShortDash();
cout << "\t\t\t*商品信息录入*\t\t\t";
printShortDash();
cout << endl;
printLongDash();
bool i = true;
do{
cout << "\n\n类型:[1]食品\t[2]化妆品\t[3]日用品\t[4]饮料\n";
printLongDash();
cout << "\n请选择:";
cin >>merClass;
if(cin.fail()) {
printShortDash();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
cout << "无效字符串,请重试!!!";
printShortDash();
cin.clear();
cin.ignore(1024, '\n');
}
else {
if (merClass == 1 || merClass == 2 || merClass == 3 || merClass == 4) {
i = false;
MERSTOCKS[merClass - 1]++;//对应商品库存+1
}
else {
printShortDash();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
cout << "选项不存在!!!无此类商品!请重试";
printShortDash();
}
}
} while (i);
i = true;
do {
cout << "\n请输入商品名称:";
cin >> merName;
if (merName.size() > 0 && merName.size() < 20) {
i = false;
}
else {
printShortDash();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
cout << "商品名过长不合法!!!请重试";
printShortDash();
}
} while (i);
i = true;
do {
cout << "\n请输入商品编号:";
cin >> merId;
if (ListOfGoods.checkDataId(merId)) {
cin.sync();
printShortDash();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
cout << "数据库中已经存在此编号,请重新输入商品编号!";
printShortDash();
}
else if(merId.size() != 6) {
printShortDash();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
cout << "注意!!!商品编号需6位";
printShortDash();
}else i = false;
} while (i);
i = true;
do {
cout << "\n请输入商品价格:";
cin >> merPrice;
if (cin.fail()) {
printShortDash();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
cout << "您输入的不是数字!!!请重新输入";
printShortDash();
cin.clear();
cin.ignore(1024, '\n');
}
else if (merPrice < 0 || merPrice>100000) {
printShortDash();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
cout << "您输入的价钱不符合常理!!!请重试";
printShortDash();
}
else i = false;
} while (i);
i = true;
do {
cout << "\n请输入生产厂家:";
cin >> merMfr;
if (merMfr.size() > 0 && merMfr.size() < 100) {
i = false;
}
else {
printShortDash();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
cout << "那个生产厂家名字会这么长!!!请重新输入";
printShortDash();
}
} while (i);
i = true;
do {
cout << "\n请输入商品品牌:";
cin >> merBrand;
if (merBrand.size() > 0 && merBrand.size() < 20) {
i = false;
}
else {
printShortDash();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
cout << "有这么长的品牌吗???请重新输入";
printShortDash();
}
} while (i);
merStatus = 1;
}
void Merchandise::display() {
cout << "\n\t\t*商品名:" << merName
<< "\n\t\t*商品类别:" << MERCLASS[merClass - 1]
<< "\n\t\t*商品编号:" << merId
<< "\n\t\t*商品价格:" << merPrice
<< "\n\t\t*生产厂家:" << merMfr
<< "\n\t\t*商品品牌:" << merBrand;
cout << "\n\t\t*状态:";
if(merStatus==1){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
cout<< STATUS[merStatus];
}
else {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
cout << STATUS[merStatus];
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE);
cout<< "\n\t\t*库存:"<<MERSTOCKS[merClass-1];
}
void Merchandise::displayLine() {
cout << setw(8)<< merId
<< setw(14)<<merName
<< setw(13) << merPrice
<< setw(21) << merMfr
<< setw(13) << merBrand;
cout << setw(16) ;
if (merStatus == 1) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
cout << STATUS[merStatus];
}
else {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
cout << STATUS[merStatus];
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE);
cout << endl;
}
string Merchandise::getMerId() {
return merId;
}
string Merchandise::getMerClass() {
return MERCLASS[merClass-1];
}
unsigned short int Merchandise::getClass() {
return merClass;
}
string Merchandise::getMerMfr() {
return merMfr;
}
string Merchandise::getMerName() {
return merName;
}
unsigned short int Merchandise::getMerStatus() {
return merStatus;
}
double Merchandise::getMerPrice() {
return merPrice;
}
string Merchandise::getMerBrand() {
return merBrand;
}
int Merchandise::gerMerRespo() {
return merClass;
}
void Merchandise::modifyIn() {
merStatus = 1;
MERSTOCKS[merClass - 1]++;
}
void Merchandise::modifyOut() {
merStatus = 0;
MERSTOCKS[merClass - 1]--;
}
void Merchandise::modifyData() {
string id;
cout << endl;
printLongDash();
cout << endl;
cout << "想修改信息?那就请输入商品编号吧:";
cin >> id;
while (id.size() != 6) {
cout << endl;
printShortDash();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
cout << "注意!!!商品编号需6位";
printShortDash();
cout << endl;
cout << "请再次输入商品号:";
cin.clear();
cin.ignore(1024, '\n');
cin >> id;
};
if (ListOfGoods.checkDataId(id)) {
Merchandise *ptr = ListOfGoods.modifyStatus(id);
ListOfGoods.front->next = ptr->next;
delete ptr;
cout << endl;
printShortDash();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
cout << "删除成功!!!";
printShortDash();
cout << endl;
}
else {
cout << endl;
printShortDash();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
cout << "数据库无与此商品相关信息";
printShortDash();
cout << "\n\n";
}
}
void Merchandise::modifyClass(unsigned short int backup) {
merClass = backup;
}
void Merchandise::modifyMfr(string backup) {
merMfr = backup;
}
void Merchandise::modifyName(string backup) {
merName = backup;
}
void Merchandise::modifyId(string backup) {
merId = backup;
}
void Merchandise::modifyPrice(double backup) {
merPrice = backup;
}
void Merchandise::modifyBrand(string backup) {
merBrand = backup;
}
void Merchandise::ModifyMERSTOCKS() {
MERSTOCKS[merClass - 1]++;
}
void Merchandise::ModifyMERSTOCKS(bool element,unsigned short int merclass) {
if (element) {
MERSTOCKS[merclass - 1]--;
}
if (!element) {
MERSTOCKS[merclass - 1]++;
}
}
ostream &operator << (ostream &out, Merchandise &merchandise) {
out << " " << merchandise.getMerId()
<< " " << merchandise.getMerName()
<< " " << merchandise.getClass()
<< " " << merchandise.getMerBrand()
<< " " << merchandise.getMerPrice()
<< " " << merchandise.getMerStatus()
<< " " << merchandise.getMerMfr()
//<< " " << merchandise.gerMerRespo()
<< endl;
return out;
}
istream &operator >> (istream &in, Merchandise &merchandise) {
in >> merchandise.merId
>> merchandise.merName
>> merchandise.merClass
>> merchandise.merBrand
>> merchandise.merPrice
>> merchandise.merStatus
>> merchandise.merMfr;
//>> merchandise.MERSTOCKS[merClass - 1];
return in;
}
/**
string merName; //商品名
string merId; //编号
double merPrice; //价格
unsigned short int merStatus; //是否在售
string merMfr; //生产厂家
string merBrand; //品牌
unsigned short int merClass; //商品类型
static int MERSTOCKS[4];//分别代表4种商品各自库存
*/
//表
chartMerchan::chartMerchan() {
header = new Merchandise;
move= header;
front = header;
}
void chartMerchan::insertData(Merchandise *ptr) {
move->next = ptr;
move = ptr;
ptr->next = NULL;
}
int chartMerchan::selectData() {
Merchandise *p=header->next;
int sum=0;
cout << "\n\t";
while (p) {
p = p->next;
sum++;
}
return sum;
}
Merchandise *chartMerchan::getHeader() {
return header;
}
Merchandise* chartMerchan::modifyStatus(string id) {
// Merchandise *p = front->next;
// while (p) {
// if (p->getMerId() == id)
// return p;
// front = front->next;
// p = front->next;
// };
// return NULL;
front = header;
Merchandise *p = front->next;
while (p) {
if (p->getMerId() == id)
return p;
front = front->next;
p = front->next;
};
return NULL;
}
bool chartMerchan::checkDataId(string id) {
Merchandise *p = header->next;
while (p) {
if (p->getMerId() == id)
return true;
p = p->next;
}
return false;
}
int chartMerchan::selectByClass(int merclass, bool isnt) {
bool comby=isnt;
int sum = 0;
Merchandise *p = header->next;
while (p) {
if (p->getMerClass() == MERCLASS[merclass-1]) {
comby = false;
cout << endl;
printBlank();
printLongerDash();
p->display();
cout << "\n\t\t";
printLongerDash();
cout << endl;
sum++;
}
p = p->next;
};
if(comby){
cout << endl;
printShortDash();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
cout << "库存中没有此类商品!!!";
printShortDash();
cout << "\n\n";
}
return sum;
}
void chartMerchan::selectByMfr(string mermfr) {
bool comby = true;
Merchandise *p = header->next;
while (p) {
if (p->getMerMfr() == mermfr) {
comby = false;
cout << endl;
printBlank();
printLongerDash();
p->display();
cout << "\n\t\t";
printLongerDash();
cout << endl;
}
p = p->next;
};
if (comby) {
cout << endl;
printShortDash();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
cout << "库存中没有此生产厂家的商品!!!";
printShortDash();
cout << "\n\n";
}
}
void chartMerchan::selectByName(string mername) {
bool comby = true;
Merchandise *p = header->next;
while (p) {
if (p->getMerName() == mername) {
comby = false;
cout << endl;
printBlank();
printLongerDash();
p->display();
cout << "\n\t\t";
printLongerDash();
cout << endl;
}
p = p->next;
};
if (comby) {
cout << endl;
printShortDash();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
cout << "库存中没有此名称的商品!!!";
printShortDash();
cout << "\n\n";
}
}
Merchandise* chartMerchan::makeListByClass(int merclass) {
Merchandise *head = new Merchandise;
Merchandise *pCompare;
pCompare = head;
Merchandise *insert;
Merchandise *MOVE;
MOVE = header;
if (header->next == NULL) {
return NULL;
}
*head = *header;
head->next = NULL;
MOVE = MOVE->next;
insert = new Merchandise;
*insert = *MOVE;
insert->next = NULL;
pCompare->next = insert;
MOVE = MOVE->next;
while (MOVE) {
pCompare = head;
if (MOVE->getMerId().compare(pCompare->next->getMerId()) < 0) {
insert = new Merchandise;
*insert = *MOVE;
insert->next = pCompare->next;
pCompare->next = insert;
MOVE = MOVE->next;
}
else {
while (MOVE->getMerId().compare(pCompare->next->getMerId()) > 0) {
pCompare = pCompare->next;
if (pCompare->next == NULL) {
break;
}
};
if (pCompare->next == NULL) {
insert = new Merchandise;
*insert = *MOVE;
pCompare->next = insert;
insert->next = NULL;
pCompare = head;
MOVE = MOVE->next;
}
else {
insert = new Merchandise;
*insert = *MOVE;
insert->next = pCompare->next;
pCompare->next = insert;
pCompare = head;
MOVE = MOVE->next;
}
}
};
return head;
}
//ostream &operator << (ostream &out, chartMerchan &chartmerchan) {
//
//}