Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- #include <string>
- using namespace std;
- struct Book
- {
- char name[50];
- int ID;
- char author[50];
- double price;
- int copies;
- };
- ofstream OLib("Library.txt", ios::app);
- ifstream iLib("Library.txt", ios::in);
- void SearchByID(int y);
- void SearchByName(char y[]);
- int main()
- {
- string search, line;
- int choice;
- Book book;
- cout << setw(310) << "Library system by MiDo\n" << endl;
- cout << "Choose one of the following:- " << endl;
- cout << "1. Add a new book.\n2. Update a book.\n3. Delete a book.\n4. Show all books.\n5. Search book by ID.\n6. Search book by Name.\n7. Borrow a book.\n8. Exit." << endl;
- cin >> choice;
- while (choice >= 0 && choice <8) {
- if (choice == 1) {
- cout << "Enter the name of the book you want to add: " << endl;
- getchar();
- cin.get(book.name, 50);
- cout << "Enter the ID of the book you want to add: " << endl;
- cin >> book.ID;
- cout << "Enter the author of the book you want to add: " << endl;
- getchar();
- cin.get(book.author, 50);
- cout << "Enter the price of the book you want to add: " << endl;
- cin >> book.price;
- cout << "Enter the copies of the book you want to add: " << endl;
- cin >> book.copies;
- OLib << left << setw(4) << book.ID << setw(15) << book.name << setw(15) << book.author << setw(8) << book.price << setw(8) << book.copies << endl;
- cout << "Book successfully added!" << endl;
- choice = 0;
- }
- else if (choice == 2) {
- cout << "Enter the ID of the book you want to update." << endl;
- cin >> search;
- while (getline(iLib, line)) {
- if (line.find(search)) {
- line.replace(line.find(search), search.length(), "");
- cout << "Enter the new ID, name, author, price, copies" << endl;
- cin >> book.ID >> book.name >> book.author >> book.price >> book.copies;
- OLib << left << setw(4) << book.ID << setw(15) << book.name << setw(15) << book.author << setw(8) << book.price << setw(8) << book.copies << endl;
- cout << "Book successfully edited!" << endl;
- }
- }
- }
- else if (choice == 3) {
- }
- else if (choice == 4) {
- string id, name, author, price, copies;
- iLib >> id >> name >> author >> price >> copies;
- cout << left << setw(4) << id << setw(15) << name << setw(15) << author << setw(8) << price << setw(8) << copies << endl;
- while (iLib >> book.ID >> book.name >> book.author >> book.price >> book.copies) {
- cout << left << setw(4) << book.ID << setw(15) << book.name << setw(15) << book.author << setw(8) << book.price << setw(8) << book.copies << endl;
- cout << "================================================" << endl;
- }
- choice = 0;
- }
- else if (choice == 5) {
- int x;
- cout << "Enter the ID you want to search for" << endl;
- cin >> x;
- SearchByID(x);
- choice = 0;
- }
- else if (choice == 6) {
- char x[50];
- cout << "Enter the book name you want to search for" << endl;
- cin >> x;
- SearchByName(x);
- choice = 0;
- }
- else if (choice == 7) {
- int x;
- cout << "Enter the ID of the book you want to borrow" << endl;
- cin >> x;
- }
- else if (choice == 8) {
- return 0;
- }
- cout << "1. Add a new book.\n2. Update a book.\n3. Delete a book.\n4. Show all books.\n5. Search book by ID.\n6. Search book by Name.\n7. Borrow a book.\n8. exit." << endl;
- cin >> choice;
- }
- cout << "Program ended!" << endl;
- system("pause");
- return 0;
- }
- void SearchByID(int y)
- {
- bool found = false;
- Book book;
- string id, name, author, price, copies;
- iLib >> id >> name >> author >> price >> copies;
- while (iLib >> book.ID >> book.name >> book.author >> book.price >> book.copies) {
- if (book.ID == y) {
- cout << "Book is found:" << endl;
- cout << left << setw(4) << id << setw(15) << name << setw(15) << author << setw(8) << price << setw(8) << copies << endl;
- cout << left << setw(4) << book.ID << setw(15) << book.name << setw(15) << book.author << setw(8) << book.price << setw(8) << book.copies << endl;
- found = true;
- break;
- }
- }
- if (found == false)
- cout << "Book is not found" << endl;
- }
- void SearchByName(char y[])
- {
- bool found = false;
- Book book;
- string id, name, author, price, copies;
- iLib >> id >> name >> author >> price >> copies;
- while (iLib >> book.ID >> book.name >> book.author >> book.price >> book.copies) {
- if (strcmp(y,book.name) == 0) {
- cout << "Book is found:" << endl;
- cout << left << setw(4) << id << setw(15) << name << setw(15) << author << setw(8) << price << setw(8) << copies << endl;
- cout << left << setw(4) << book.ID << setw(15) << book.name << setw(15) << book.author << setw(8) << book.price << setw(8) << book.copies << endl;
- found = true;
- break;
- }
- }
- if (found == false)
- cout << "Book is not found" << endl;
- }
- void Borrow(int x)
- {
- bool found = false;
- Book book;
- int b;
- string id, name, author, price, copies;
- iLib >> id >> name >> author >> price >> copies;
- while (iLib >> book.ID >> book.name >> book.author >> book.price >> book.copies) {
- if (book.ID == x) {
- b = book.copies;
- cout << "Book is borrowed:" << endl;
- cout << left << setw(4) << id << setw(15) << name << setw(15) << author << setw(8) << price << setw(8) << copies << endl;
- cout << left << setw(4) << book.ID << setw(15) << book.name << setw(15) << book.author << setw(8) << book.price << setw(8) << book.copies << endl;
- found = true;
- break;
- }
- }
- if (found == false)
- cout << "Book is not found" << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment