-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCart.cpp
More file actions
74 lines (68 loc) · 1.48 KB
/
Cart.cpp
File metadata and controls
74 lines (68 loc) · 1.48 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
#include "Cart.hpp"
#include <fstream>
#include <string>
#include <cstdlib>
// Reads and parses the text file
void Cart::readList(std::string filename)
{
// create input stream object
std::ifstream in(filename);
// check if the file was opened
if(!in.is_open())
{
std::cerr << "Could not open file to read grocery list." << std::endl;
// exit the program
exit(-1);
}
// create string to read in each line
std::string line;
while (!in.eof())
{
std::getline(in, line);
// add line to list
_list.push_back(line);
}
in.close();
}
// returns element at index i of list
std::string& Cart::operator[](int i)
{
return _list[i];
}
// prints list; used for debugging
void Cart::to_string() const
{
std::cout << "printing cart:" << std::endl;
for(auto item : _list)
std::cout << item << std::endl;
}
// prints list; used for debugging
void Cart::to_stringHTML() const
{
std::cout << "----------------start cart.to_stringHTML-----------------<br>" << std::endl;
for(auto item : _list)
std::cout << item << "<br>" << std::endl;
std::cout << "---------------- end cart.to_stringHTML-----------------<br>" << std::endl;
}
// adds item to list
void Cart::add_to_cart(std::string item)
{
_list.push_back(item);
}
// removes item from list
void Cart::remove_item(std::string item)
{
for(size_t i = 0; i < _list.size(); i++)
{
if(_list[i] == item)
{
_list.erase(_list.begin()+i);
return;
}
}
}
// returns list
std::vector<std::string> Cart::list() const
{
return _list;
}