-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
179 lines (153 loc) · 3.35 KB
/
Copy pathmain.cpp
File metadata and controls
179 lines (153 loc) · 3.35 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <iostream>
#include <string>
#include <vector>
#include <limits>
class Book
{
private:
std::string title;
std::string author;
bool isIssued{false};
public:
Book(std::string title, std::string author)
{
this->title = title;
this->author = author;
}
void display() const
{
std::cout << "\nTitle: " << title << "\nAuthor: " << author << "\nIssued: " << (isIssued ? "Yes" : "No") << "\n";
}
void issue()
{
if (isIssued)
{
std::cout << "\nBook already issued!";
}
else
{
isIssued = true;
std::cout << "\nBook issued successfully!";
}
}
void returnBook()
{
if (!isIssued)
{
std::cout << "\nCan not return the book that was never issued.";
}
else
{
isIssued = false;
std::cout << "\nHope you liked reading this book.";
}
}
std::string getTitle() const
{
return title;
}
};
class Library
{
private:
std::vector<Book> books;
public:
void addBooks()
{
std::string title, author;
std::cout << "Enter title: ";
std::getline(std::cin, title);
std::cout << "Enter author: ";
std::getline(std::cin, author);
books.push_back(Book(title, author));
}
void showBooks()
{
if (books.empty())
{
std::cout << "No books availabe\n";
return;
}
for (int i = 0; i < books.size(); i++)
{
std::cout << "\nIndex: " << i;
books[i].display();
}
}
void issueBook()
{
if (books.empty())
{
std::cout << "No books availabe";
return;
}
int index;
std::cout << "Enter the index: ";
std::cin >> index;
if (index < 0 || index >= books.size())
{
std::cout << "Invalid index\n";
return;
}
books[index].issue();
}
void returnBook()
{
if (books.empty())
{
std::cout << "No books available\n";
return;
}
int index;
std::cout << "Enter the index: ";
std::cin >> index;
if (index < 0 || index >= books.size())
{
std::cout << "Invalid index\n";
return;
}
books[index].returnBook();
}
};
int main()
{
Library lib;
int input = 0;
while (input != 5)
{
std::cout << "\nMenu\n"
<< "1. Add Book\n"
<< "2. Show Books\n"
<< "3. Issue Book\n"
<< "4. Return Book\n"
<< "5. Exit\n";
std::cout << "Enter choice: ";
std::cin >> input;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (input == 1)
{
lib.addBooks();
}
else if (input == 2)
{
lib.showBooks();
}
else if (input == 3)
{
lib.issueBook();
}
else if (input == 4)
{
lib.returnBook();
}
else if (input == 5)
{
std::cout << "Goodbye!\n";
}
else
{
std::cout << "Invalid choice\n";
}
}
return 0;
}