-
Notifications
You must be signed in to change notification settings - Fork 12
/
AlternatingDinerMenuIterator.h
53 lines (46 loc) · 1.28 KB
/
AlternatingDinerMenuIterator.h
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
#ifndef ALTERNATING_DINER_MENU_ITERATOR_H
#define ALTERNATING_DINER_MENU_ITERATOR_H
#include "Iterator.h"
#include "MenuItem.h"
#include <ctime>
#include <iostream>
#include <string>
#include <vector>
/*
* Note: this code is untested yet. To test it your self, have the
* createIterator member function's return a ref to an AlternatingDinerMenuIterator.
*/
class AlternatingDinerMenuIterator : public Iterator<MenuItem> {
public:
AlternatingDinerMenuIterator(std::vector<MenuItem> &i);
MenuItem* next() override;
bool hasNext() const override { return iter != items.end(); } // && *iter != nullptr
void remove() override;
private:
std::vector<MenuItem> &items;
std::vector<MenuItem>::iterator iter;
};
inline
AlternatingDinerMenuIterator::AlternatingDinerMenuIterator(std::vector<MenuItem> &i) :
items(i), iter(items.begin())
{
auto epoch_time = std::time(nullptr);
auto calendar_time = std::gmtime(&epoch_time);
auto step = calendar_time->tm_wday % 2; // result is 0 or 1
iter += step;
}
inline
MenuItem*
AlternatingDinerMenuIterator::next()
{
auto menuItem = &*iter;
iter += 2;
return menuItem;
}
inline
void
AlternatingDinerMenuIterator::remove()
{
std::cerr << "Alternating Diner Menu Iterator does not support remove()";
}
#endif /* ALTERNATING_DINER_MENU_ITERATOR_H */