-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingleResponsibilityPrinciple.cpp
More file actions
110 lines (91 loc) · 2.85 KB
/
singleResponsibilityPrinciple.cpp
File metadata and controls
110 lines (91 loc) · 2.85 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
// Single Responsibility Principle
// A class should have only one reason to change, meaning that a class should have only one job.
// If a class has more than one responsibility, it becomes coupled. A change to one responsibility results to modification of the other responsibility.
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
// Reference Only (Without SRP)
// Kept for interview explanation: this class mixes business logic, input, and output.
// Not used in main().
class TodoList {
private:
std::vector<std::string> todos;
public:
void addTask(const std::string& task) {
todos.push_back(task);
}
void deleteTask(const std::string& task) {
todos.erase(std::remove(todos.begin(), todos.end(), task), todos.end());
}
void displayTasks() const {
for (const auto& task : todos) {
std::cout << task << std::endl;
}
}
void inputTask() {
std::string task;
std::cout << "Enter a task: ";
std::getline(std::cin, task);
addTask(task);
}
void removeTask() {
std::string task;
std::cout << "Enter a task to remove: ";
std::getline(std::cin, task);
deleteTask(task);
}
};
// SRP: Handles only task state and business operations.
class TaskManager {
private:
std::vector<std::string> todos;
public:
void addTask(const std::string& task) {
todos.push_back(task);
}
void deleteTask(const std::string& task) {
todos.erase(std::remove(todos.begin(), todos.end(), task), todos.end());
}
const std::vector<std::string>& getTasks() const {
return todos;
}
};
// SRP: Handles only output/presentation.
class TaskUI {
public:
static void displayTasks(const std::vector<std::string>& tasks) {
if (tasks.empty()) {
std::cout << "No tasks available." << std::endl;
return;
}
std::cout << "Current Tasks:" << std::endl;
for (const auto& task : tasks) {
std::cout << "- " << task << std::endl;
}
}
};
// SRP: Handles only input collection.
class TaskInput {
public:
static std::string inputTask() {
std::string task;
std::cout << "Enter a task: ";
std::getline(std::cin, task);
return task;
}
static std::string inputTaskToRemove() {
std::string task;
std::cout << "Enter a task to remove: ";
std::getline(std::cin, task);
return task;
}
};
int main() {
TaskManager taskManager;
// Add one task, remove one task, then display.
taskManager.addTask(TaskInput::inputTask());
taskManager.deleteTask(TaskInput::inputTaskToRemove());
TaskUI::displayTasks(taskManager.getTasks());
return 0;
}