-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoops.cpp
More file actions
116 lines (97 loc) · 2.73 KB
/
Copy pathoops.cpp
File metadata and controls
116 lines (97 loc) · 2.73 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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Book {
public:
// Properties
string title;
string author;
bool isAvailable;
// Constructor
Book(string title, string author){
this->title=title;
this->author=author;
}
// Method to check out the book
void checkOut() {
if (isAvailable) {
isAvailable = false;
cout << "Book checked out successfully." << endl;
} else {
cout << "Book is not available." << endl;
}
}
// Method to return the book
void returnBook() {
isAvailable = true;
cout << "Book returned successfully." << endl;
}
};
class Member {
public:
// Properties
string name;
int memberId;
vector<Book*> borrowedBooks;
// Constructor
Member(string name, int memberId) : name(name), memberId(memberId) {}
// Method to borrow a book
void borrowBook(Book* book) {
if (book->isAvailable) {
borrowedBooks.push_back(book);
book->checkOut();
cout << "Book borrowed successfully." << endl;
} else {
cout << "Book is not available for borrowing." << endl;
}
}
// Method to return a book
void returnBook(Book* book) {
auto it = find(borrowedBooks.begin(), borrowedBooks.end(), book);
if (it != borrowedBooks.end()) {
borrowedBooks.erase(it);
book->returnBook();
cout << "Book returned successfully." << endl;
} else {
cout << "You did not borrow this book from the library." << endl;
}
}
};
class Library {
public:
// Properties
vector<Book> books;
vector<Member> members;
// Method to add a book to the library
void addBook(const Book& book) {
books.push_back(book);
cout << "Book added to the library." << endl;
}
// Method to add a member to the library
void addMember(const Member& member) {
members.push_back(member);
cout << "Member added to the library." << endl;
}
};
int main() {
// Create a library
Library library;
// Add books to the library
Book book1("The Catcher in the Rye", "J.D. Salinger");
Book book2("To Kill a Mockingbird", "Harper Lee");
library.addBook(book1);
library.addBook(book2);
// Add members to the library
Member member1("Alice", 101);
Member member2("Bob", 102);
library.addMember(member1);
library.addMember(member2);
// Member borrows a book
member1.borrowBook(&book1);
// Member tries to borrow the same book again (not allowed)
member1.borrowBook(&book1);
// Member returns the book
member1.returnBook(&book1);
return 0;
}