Untitled
unknown
plain_text
a year ago
2.9 kB
4
Indexable
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;
class Book {
private:
string isbn;
string title;
string author;
int numCopies;
bool isIssued;
bool isValidISBN(const string& isbn) const {
int hyphenCount = 0;
for (char c : isbn) {
if (c == '-') {
hyphenCount++;
}
}
return hyphenCount == 3 && isbn.length() >= 5;
}
public:
Book(const string& isbn, const string& title, const string& author, int numCopies)
: title(title), author(author), numCopies(numCopies), isIssued(false) {
if (isValidISBN(isbn)) {
this->isbn = isbn;
} else {
throw invalid_argument("Invalid ISBN format");
}
}
void displayBookDetails() const {
cout << "ISBN: " << isbn << "\nTitle: " << title << "\nAuthor: " << author
<< "\nNumber of Copies: " << numCopies << "\n";
cout << "Status: " << (isIssued ? "Issued" : "Available") << "\n";
}
bool issueBook() {
if (numCopies > 0 && !isIssued) {
isIssued = true;
numCopies--;
return true;
}
return false;
}
~Book() {
cout << "Book object destroyed for Title: " << title << endl;
}
};
class Student {
private:
string name;
string studentId;
string libraryCardNumber;
public:
Student(const string& name, const string& studentId, const string& libraryCardNumber)
: name(name), studentId(studentId), libraryCardNumber(libraryCardNumber) {}
void displayStudentDetails() const {
cout << "Name: " << name << "\nStudent ID: " << studentId
<< "\nLibrary Card Number: " << libraryCardNumber << "\n";
}
~Student() {
cout << "Student object destroyed for Student Name: " << name << endl;
}
};
int main() {
try {
string isbn;
while (true) {
cout << "Enter ISBN: ";
getline(cin, isbn); // Changed to getline for safety
try {
Book book(isbn, "C ++", "Prateek", 7);
book.displayBookDetails(); // Display immediately after creation
if (book.issueBook()) {
cout << "Book issued successfully.\n";
} else {
cout << "Book cannot be issued.\n";
}
break; // Exit the loop after a successful book creation
} catch (const invalid_argument& e) {
cerr << "Error: " << e.what() << ". Please enter a valid ISBN.\n";
}
}
Student student("Harthik", "34987", "L 234");
cout << "\nStudent Details:\n";
student.displayStudentDetails();
} catch (const exception& e) {
cerr << "An exception occurred: " << e.what() << endl;
}
return 0;
}
Editor is loading...
Leave a Comment