-
Notifications
You must be signed in to change notification settings - Fork 0
/
Menu.cpp
147 lines (127 loc) · 2.23 KB
/
Menu.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
#include"Menu.h"
#include<iostream>
using namespace std;
#include"stdafx.h"
Menu::Menu(int size)
{
capacity = size;
noOfItems = 0;
itemList = new MenuItem*[capacity];
for (int i = 0; i < capacity; i++)
{
itemList[i] = 0;
}
}
Menu::Menu(const Menu & ref)
{
if (ref.itemList)
{
capacity = ref.capacity;
noOfItems = ref.noOfItems;
itemList = new MenuItem*[capacity];
for (int i = 0; i < noOfItems; i++)
{
itemList[i] = new MenuItem(*(ref.itemList[i]));
}
}
}
Menu & Menu::operator=(const Menu & ref)
{
if (this == &ref)
return *this;
this->~Menu();
capacity = ref.capacity;
noOfItems = ref.noOfItems;
itemList = new MenuItem*[capacity];
for (int i = 0; i < noOfItems; i++)
{
itemList[i] = new MenuItem(*ref.itemList[i]);
}
return *this;
// TODO: insert return statement here
}
bool Menu::isFull() const
{
return noOfItems == capacity ? 1 : 0;
}
void Menu::reSize(int newSize)
{
if (newSize <= 0)
{
this->~Menu();
return;
}
else
{
int tempNo = newSize<noOfItems ? newSize : noOfItems;
MenuItem ** temp = new MenuItem*[newSize];
//
for (int i = 0; i < tempNo&&i<capacity; i++)
{
temp[i] = new MenuItem(*itemList[i]);
}
this->~Menu();
itemList = temp;
capacity = newSize;
noOfItems = tempNo;
}
}
bool Menu::isEmpty()
{
return noOfItems == 0;
}
bool Menu::addItem(const MenuItem & nm)
{
if (!isFull())
{
itemList[noOfItems] = new MenuItem(nm);
noOfItems++;
return 1;
}
else
return 0;
}
bool Menu::removeItem(const CString & nm)
{
int index = searchItemPosition(nm);
if (index == -1)
return 0;
delete itemList[index];
itemList[index] = new MenuItem(*itemList[noOfItems]);
delete itemList[noOfItems];
itemList[noOfItems] = 0;
}
int Menu::searchItemPosition(CString nm)
{
int i = 0;
while (i < noOfItems&&itemList[i]->getItemName().isEqual(nm) == 0)
{
i++;
}
return i == noOfItems ? -1 : i;
}
int Menu::getNoOfItems() const
{
return noOfItems;
}
void Menu::displayMenu() const
{
for (int i = 0; i < noOfItems; i++)
{
(*itemList[i]).displayMenuItem();
}
}
Menu::~Menu()
{
if (!itemList)
return;
for (int i = 0; i < noOfItems; i++)
{
delete itemList[i];
itemList[i] = 0;//nullptr;
}
delete[] itemList;
itemList = nullptr;
noOfItems = 0;
capacity = 0;
}