This repository has been archived by the owner. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShoppingList.cpp
More file actions
116 lines (108 loc) · 2.38 KB
/
ShoppingList.cpp
File metadata and controls
116 lines (108 loc) · 2.38 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
#include <iostream>
#include "ShoppingRec.h"
#include "ShoppingList.h"
#include "File.h"
#include "Utils.h"
using namespace std;
namespace sdds {
bool loadList() {
ShoppingRec shpr = {};
bool ok = false;
if (openFileForRead()) {
ok = true;
while (noOfRecs < MAX_NO_OF_RECS &&
freadShoppingRec(&shpr)) {
recs[noOfRecs++] = shpr;
}
closeFile();
}
return ok;
}
bool saveList() {
bool ok = false;
if (openFileForOverwrite()) {
ok = true;
for (int i = 0; i < noOfRecs; i++) {
fwriteShoppintRec(&recs[i]);
}
closeFile();
}
return ok;
}
void removeItem(int index) {
if (index >= 0 && index < noOfRecs) {
noOfRecs--;
for (int i = index; i < noOfRecs; i++) {
recs[i] = recs[i + 1];
}
}
}
void clearList() {
cout << "This will remove all the items from the list;" << endl
<< "Are you sure? (Y)es/(N)o" << endl;
if (yes()) {
noOfRecs = 0;
}
else {
cout << "Cancelled!" << endl;
}
}
void toggleBought() {
cout << "Item number: ";
toggleBoughtFlag(&recs[readInt(1, noOfRecs) - 1]);
}
void addItemToList() {
if (noOfRecs < MAX_NO_OF_RECS) {
recs[noOfRecs] = getShoppingRec();
noOfRecs += !isShoppingRecEmpty(&recs[noOfRecs]);
}
else {
cout << "Shopping List is full!" << endl;
}
}
void removeItemfromList() {
if (!listIsEmpty()) {
int value = 0;
cout << "Item number to delete: ";
value = readInt(1, noOfRecs);
cout << "Deleting following item, are you sure?" << endl;
displayShoppingRec(&recs[value - 1]);
cout << "(Y)es/(N)o: ";
if (yes()) {
removeItem(value - 1);
}
else {
cout << "Cancelled!" << endl;
}
}
else {
cout << "List is empty!" << endl;
}
}
bool listIsEmpty() {
return noOfRecs == 0;
}
void displayList() {
for (int i = 0; i < noOfRecs; i++) {
cout << (i + 1) << "-";
displayShoppingRec(&recs[i]);
}
}
void removeBoughtItems() {
if (!listIsEmpty()) {
cout << "Removing bought items, are you sure?" << endl;
cout << "(Y)es/(N)o: ";
if (yes()) {
for (int i = 0; i < noOfRecs; i++) {
if (recs[i].m_bought) removeItem(i--);
}
}
else {
cout << "Cancelled!" << endl;
}
}
else {
cout << "List is empty!" << endl;
}
}
}