-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
133 lines (111 loc) · 3.29 KB
/
main.cpp
File metadata and controls
133 lines (111 loc) · 3.29 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
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <algorithm>
class Database
{
private:
struct Student
{
std::string _name {""};
std::string _surname {""};
int _index {0};
Student(std::string name, std::string surname, int index)
: _name(name), _surname(surname), _index(index) {}
};
private:
std::vector<Student> data;
std::vector<Student>::iterator getIteratorToStudent(const int index);
public:
void addStudent(const std::string name, const std::string surname, const int index);
void printStudents();
void sortStudents();
bool findStudentByIndex(const int index);
bool delStudentByIndex(const int index);
friend std::ostream& operator<<(std::ostream& os, const Student& student);
friend bool compare(const Student& leftSide, const Student& rightSide);
};
void Database::addStudent(std::string name, std::string surname, int index)
{
Student student(name, surname, index);
data.push_back(student);
}
void Database::printStudents()
{
for(const auto& student : data)
std::cout << student;
std::cout << "\n";
}
bool compare(const Database::Student& leftSide, const Database::Student& rightSide)
{
return leftSide._index < rightSide._index;
}
void Database::sortStudents()
{
std::sort(data.begin(), data.end(), compare);
}
bool Database::findStudentByIndex(const int index)
{
auto iterator = getIteratorToStudent(index);
if(iterator != data.end())
{
std::cout << *iterator << "\n";
return true;
}
else
{
std::cout << index << " not found in database\n";
return false;
}
}
std::vector<Database::Student>::iterator Database::getIteratorToStudent(const int index)
{
//lambda expression
auto iterator = std::find_if(data.begin(), data.end(),
[&index](const Student& student)
{ return student._index == index; }
);
return iterator;
}
bool Database::delStudentByIndex(const int index)
{
auto iterator = getIteratorToStudent(index);
if(iterator != data.end())
{
data.erase(iterator);
return true;
}
else
{
std::cout << "Invalid index\n";
return false;
}
}
std::ostream& operator<<(std::ostream& os, const Database::Student& student)
{
os << std::left << std::setw(13) << student._surname << " "
<< std::left << std::setw(9) << student._name << " "
<< std::left << std::setw(7) << student._index << "\n";
return os;
}
int main(){
std::cout << "\nProject1: StudentDatabase\n\n";
Database base;
base.addStudent("Adam", "Malinowski", 12345);
base.addStudent("Tomek", "Kowalski", 55555);
base.addStudent("Kamil", "Dabrowski", 33333);
base.addStudent("Ada", "Kwiatkowska", 22222);
std::cout << "--Print all:--\n";
base.printStudents();
std::cout << "--Sort all:--\n";
base.sortStudents();
base.printStudents();
std::cout << "--Find:--\n";
base.findStudentByIndex(33333);
std::cout << "--Delete:--\n";
base.printStudents();
base.delStudentByIndex(33333);
base.printStudents();
return 0;
}