Repository navigation
Expand file tree
/
Copy pathq6.cpp
More file actions
173 lines (148 loc) · 5 KB
/
Copy pathq6.cpp
File metadata and controls
173 lines (148 loc) · 5 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
// WAP to store book database in a file. Design a book class with members ISBN number, name of author, title, price. The Program should able to store book object to a file, find a book in the database and display according to ISBN number. Also display all records.
// getInput()
// display()
// add_record
// show_record
// search_record_by_isbn_number & modify also.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
class Book {
std::string isbn;
std::string author;
std::string title;
double price;
public:
// Constructors
Book() {}
Book(const std::string &isbn, const std::string &author, const std::string &title, double price)
: isbn(isbn), author(author), title(title), price(price) {}
// Member functions
void getInput() {
std::cout << "Enter ISBN: ";
std::cin >> isbn;
std::cin.ignore(); // Clear the newline character from the buffer
std::cout << "Enter Author: ";
std::getline(std::cin, author);
std::cout << "Enter Title: ";
std::getline(std::cin, title);
std::cout << "Enter Price: ";
std::cin >> price;
}
void display() const {
std::cout << "ISBN: " << isbn << ", Author: " << author << ", Title: " << title << ", Price: $" << price << std::endl;
}
std::string getISBN() const { return isbn; }
// Overloading extraction and insertion operators for file operations
friend std::ofstream &operator<<(std::ofstream &ofs, const Book &book);
friend std::ifstream &operator>>(std::ifstream &ifs, Book &book);
};
std::ofstream &operator<<(std::ofstream &ofs, const Book &book) {
ofs << book.isbn << "\n" << book.author << "\n" << book.title << "\n" << book.price << "\n";
return ofs;
}
std::ifstream &operator>>(std::ifstream &ifs, Book &book) {
getline(ifs, book.isbn);
getline(ifs, book.author);
getline(ifs, book.title);
ifs >> book.price;
ifs.ignore(); // Clear the newline character from the buffer
return ifs;
}
class BookDatabase {
std::string filename;
public:
BookDatabase(const std::string &filename) : filename(filename) {}
void add_record() {
Book book;
book.getInput();
std::ofstream ofs(filename, std::ios::app); // Append mode
ofs << book;
ofs.close();
}
void show_records() {
std::ifstream ifs(filename);
Book book;
while (ifs >> book) {
book.display();
}
ifs.close();
}
void search_record_by_isbn_number() {
std::string search_isbn;
std::cout << "Enter ISBN to search: ";
std::cin >> search_isbn;
std::cin.ignore(); // Clear the newline character from the buffer
std::ifstream ifs(filename);
Book book;
bool found = false;
while (ifs >> book) {
if (book.getISBN() == search_isbn) {
std::cout << "Book found:\n";
book.display();
found = true;
break;
}
}
ifs.close();
if (!found) {
std::cout << "Book with ISBN " << search_isbn << " not found.\n";
}
}
void modify_record() {
std::vector<Book> books;
std::string search_isbn;
std::cout << "Enter ISBN to modify: ";
std::cin >> search_isbn;
std::cin.ignore(); // Clear the newline character from the buffer
bool found = false;
std::ifstream ifs(filename);
Book book;
while (ifs >> book) {
if (book.getISBN() == search_isbn) {
std::cout << "Book found:\n";
book.display();
std::cout << "\nEnter new details for the book:\n";
book.getInput();
found = true;
}
books.push_back(book);
}
ifs.close();
std::ofstream ofs(filename); // Overwrite mode
for (const auto &b : books) {
ofs << b;
}
ofs.close();
if (!found) {
std::cout << "Book with ISBN " << search_isbn << " not found.\n";
} else {
std::cout << "Book details updated successfully.\n";
}
}
};
int main() {
BookDatabase db("books.txt");
int choice;
do {
std::cout << "\nMenu:\n";
std::cout << "1. Add Record\n";
std::cout << "2. Show Records\n";
std::cout << "3. Search Record by ISBN\n";
std::cout << "4. Modify Record\n";
std::cout << "5. Exit\n";
std::cout << "Enter choice: ";
std::cin >> choice;
std::cin.ignore(); // Clear the newline character from the buffer
switch (choice) {
case 1: db.add_record(); break;
case 2: db.show_records(); break;
case 3: db.search_record_by_isbn_number(); break;
case 4: db.modify_record(); break;
case 5: std::cout << "Goodbye!\n"; break;
default: std::cout << "Invalid choice!\n";
}
} while (choice != 5);
return 0;
}