Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Program Program creating book-store database. Each command requires separate

ID: 3887941 • Letter: C

Question

C++ Program



Program creating book-store database. Each command requires separate function. struct Book string ISBN; string title; int quantity, int edition; double pricc; Program needs to read and process a transaction file “storeTrans.txt". This file contains several commands such as: display, add, scarchBook, delcte, updatePrice, updateQuantity, sort and save. Each command requires new linc Example of display: ISBN Title Edition Quantity Price 123456789 C++ForDummies 3 5 150.00 Add bookISBN bookTitle bookQuantity bookPricc . .Adds new book record to the databasc Should read information from the transaction file If book docsn't exist: add new entry Should display message that the book was successfully added . Should check for duplicate book entries (same ISBN) use push back function to add to the end of vector “123456789 successfully added to the database " If book alrcady exists, do not add. Display message . “123456789 already exists. The number ofcopies is now 5…” · .ScarchBook bookTitle . Scarches and returms all books with matching title. Exact title OR kcyword found Display message ."Scarching for book 123456789 C++ForDummics 5" .If no cntry found "Scarching for book Sorry, no matching book exists" . -

Explanation / Answer

#include<iostream>
#include<fstream>
#include<string>
#include<vector>

using namespace std;

struct Book {
   string ISBN;
   string title;
   int quantity;
   int edition;
   double price;
};

void swap(struct Book &x, struct Book &y){
     struct Book temp;
     temp.ISBN = x.ISBN;
     temp.title = x.title;
     temp.quantity = x.quantity;
     temp.edition = x.edition;
     temp.price = x.price;
    
     x.ISBN = y.ISBN;
     x.title = y.title;
     x.quantity = y.quantity;
     x.edition = y.edition;
     x.price = y.price;

     y.ISBN = temp.ISBN;
     y.title = temp.title;
     y.quantity = temp.quantity;
     y.edition = temp.edition;
     y.price = temp.price;

}

int main(){

   vector<Book> data;
   ifstream fin;
   string input;
   string isbn;
   int quantity;
   int edition;
   int found;
  
   double price;
   string title;
   struct Book item;
  
   fin.open("storeTrans.txt");
   if (fin){
      while(fin >> input ){
          if (input == "AddBook"){
             fin >> item.ISBN >> item.title >> item.quantity >> item.price >> item.edition;
             found = 0;
             for(int i = 0; i<data.size(); i++){
                 if (data[i].ISBN == isbn){
                    found = 1;
                    quantity = data[i].quantity;
                 }
             }
             if (found == 0){
                data.push_back(item);
                cout << isbn << " successfully added to the database" << endl;
             }  
             else {
                cout << isbn << " already exists. The number of copies is " << quantity << endl;
             }       

          }
          if (input == "SearchBook"){
             fin >> title;
             int found = 0;
             for(int i = 0; i<data.size(); i++){
                 if (data[i].ISBN == isbn){
                    found = 1;
                    cout << data[i].ISBN << " "<< title << endl;
                 }
             }
             if (found == 0)
                cout << title << " book does not exist " << endl;

          }
          if (input == "DeleteBook"){
             fin >> isbn;
             int found = 0;
             for(int i = 0; i<data.size(); i++){
                 if (data[i].ISBN == isbn){
                    found = 1;
                    data.erase(data.begin()+i);
                 }
             }
             if (found == 1)
                cout << isbn << " book successfully deleted "<< endl;
             else
                cout << isbn << " book does not exist " << endl;
          }
          if (input == "UpdatePrice"){
             fin >> isbn >> price;
             int found = 0;
             for(int i = 0; i<data.size(); i++){
                 if (data[i].ISBN == isbn){
                    found = 1;
                    data[i].price = price;
                 }
             }
             if (found == 1)
                cout << isbn << " book price updated to " << quantity << endl;
             else
                cout << isbn << " book does not exist " << endl;
          }
          if (input == "UpdateQuantity"){
             fin >> isbn >> quantity;
             int found = 0;
             for(int i = 0; i<data.size(); i++){
                 if (data[i].ISBN == isbn){
                    found = 1;
                    data[i].quantity = quantity;
                 }
             }
             if (found == 1)
                cout << isbn << " book quantity updated to " << quantity << endl;
             else
                cout << isbn << " book does not exist " << endl;
          }
          if (input == "Sort"){
             for(int i = 0; i<data.size(); i++){
                for(int j = i; j<data.size(); j++){
                   if (data[i].title > data[j].title){
                        swap(data[i],data[j]);
                   }
                  
                   if (data[i].title == data[j].title){
                        if (data[i].edition > data[j].edition)
                           swap(data[i],data[j]);
                   }
                }
             }
             cout << "Database Sorted.." << endl;
          }
          if (input == "Save"){
             ofstream fout("storeOut.txt");
             for(int i = 0; i<data.size(); i++){
                 fout << data[i].ISBN << " " << data[i].title << " " << data[i].edition;
                 fout << " " << data[i].quantity << " " << data[i].price << endl;
             }
             fout.close();
          }

      }
   }
   else {
       cout << "Error opening file ";
   }     

   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote