-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
131 lines (121 loc) · 4.32 KB
/
Copy pathmain.cpp
File metadata and controls
131 lines (121 loc) · 4.32 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
#include "Book.h"
#include "MemberRecord.h"
#include "Library.h"
#include "Admin.h"
#include "MemberAccount.h"
#include <iostream>
#include <limits>
using namespace std;
void adminMenu(Library& lib) {
int choice;
do {
cout << "\n--- Admin Menu ---" << endl;
cout << "1. Add Book" << endl;
cout << "2. Remove Book" << endl;
cout << "3. Display All Books" << endl;
cout << "4. Display All Members" << endl;
cout << "5. Save to File" << endl;
cout << "6. Load from File" << endl;
cout << "0. Logout" << endl;
cout << "Enter choice: ";
cin >> choice;
if (choice == 1) {
string title, author, isbn;
cout << "Title: "; cin.ignore(); getline(cin, title);
cout << "Author: "; getline(cin, author);
cout << "ISBN: "; getline(cin, isbn);
lib.addBook(Book(title, author, isbn));
cout << "Book added." << endl;
} else if (choice == 2) {
string isbn;
cout << "Enter ISBN to remove: "; cin >> isbn;
lib.removeBook(isbn);
cout << "Book removed (if it existed)." << endl;
} else if (choice == 3) {
lib.displayAllBooks();
} else if (choice == 4) {
lib.displayAllMembers();
} else if (choice == 5) {
lib.saveToFile("data/books.txt");
cout << "Saved." << endl;
} else if (choice == 6) {
lib.loadFromFile("data/books.txt");
cout << "Loaded." << endl;
}
} while (choice != 0);
}
void memberMenu(Library& lib, int memberId) {
int choice;
do {
cout << "\n--- Member Menu ---" << endl;
cout << "1. Search Book by ISBN" << endl;
cout << "2. Borrow Book" << endl;
cout << "3. Return Book" << endl;
cout << "0. Logout" << endl;
cout << "Enter choice: ";
cin >> choice;
if (choice == 1) {
string isbn;
cout << "Enter ISBN: "; cin >> isbn;
Book* b = lib.searchBook(isbn);
if (b) b->display();
else cout << "Book not found." << endl;
} else if (choice == 2) {
string isbn;
cout << "Enter ISBN to borrow: "; cin >> isbn;
if (lib.issueBook(isbn, memberId))
cout << "Book issued successfully." << endl;
else
cout << "Could not issue book (not available or not found)." << endl;
} else if (choice == 3) {
string isbn;
cout << "Enter ISBN to return: "; cin >> isbn;
if (lib.returnBookByMember(isbn, memberId))
cout << "Book returned successfully." << endl;
else
cout << "Could not return book." << endl;
}
} while (choice != 0);
}
int main() {
Library lib;
lib.addBook(Book("The Hobbit", "J.R.R. Tolkien", "12345"));
lib.addBook(Book("1984", "George Orwell", "999"));
lib.addMember(MemberRecord(1, "Hira"));
lib.addMember(MemberRecord(2, "Ahmed"));
int choice;
do {
cout << "\n=== Library Management System ===" << endl;
cout << "1. Login as Admin" << endl;
cout << "2. Login as Member" << endl;
cout << "0. Exit" << endl;
cout << "Enter choice: ";
cin >> choice;
if (choice == 1) {
string user, pass;
cout << "Username: "; cin >> user;
cout << "Password: "; cin >> pass;
if (user == "admin" && pass == "1234") {
Admin a(user, pass);
cout << "Welcome, " << a.getUsername() << "! Role: " << a.getRole() << endl;
adminMenu(lib);
} else {
cout << "Invalid credentials." << endl;
}
} else if (choice == 2) {
int id; string pass;
cout << "Member ID: "; cin >> id;
cout << "Password: "; cin >> pass;
MemberRecord* m = lib.searchMember(id);
if (m && pass == "1234") {
MemberAccount ma(m->getName(), pass);
cout << "Welcome, " << ma.getUsername() << "! Role: " << ma.getRole() << endl;
memberMenu(lib, id);
} else {
cout << "Invalid credentials." << endl;
}
}
} while (choice != 0);
cout << "Goodbye!" << endl;
return 0;
}