Each command requires separate function Program creating book-store database. Ea
ID: 3888654 • Letter: E
Question
Each command requires separate function
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;
}
void AddBook(vector<Book> &data, Book item){
int found;
int quantity;
found = 0;
for(int i = 0; i<data.size(); i++){
if (data[i].ISBN == item.ISBN){
found = 1;
quantity = data[i].quantity;
}
}
if (found == 0){
data.push_back(item);
cout << item.ISBN << " successfully added to the database" << endl;
}
else {
cout << item.ISBN << " already exists. The number of copies is " << quantity << endl;
}
}
void SearchBook(vector<Book> &data,string title){
int found = 0;
for(int i = 0; i<data.size(); i++){
if (data[i].title == title){
found = 1;
cout << data[i].ISBN << " "<< title << endl;
}
}
if (found == 0)
cout << title << " book does not exist " << endl;
}
void DeleteBook(vector<Book> &data,string 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;
}
void UpdatePrice(vector<Book> &data, string isbn, double 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 " << price << endl;
else
cout << isbn << " book does not exist " << endl;
}
void UpdateQuantity(vector<Book> &data, string isbn, int 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;
}
void Sort(vector<Book> &data){
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;
}
void Save(vector<Book> &data){
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();
}
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;
AddBook(data,item);
}
if (input == "SearchBook"){
fin >> title;
SearchBook(data,title);
}
if (input == "DeleteBook"){
fin >> isbn;
DeleteBook(data,isbn);
}
if (input == "UpdatePrice"){
fin >> isbn >> price;
UpdatePrice(data,isbn,price);
}
if (input == "UpdateQuantity"){
fin >> isbn >> quantity;
UpdateQuantity(data,isbn,quantity);
}
if (input == "Sort"){
Sort(data);
}
if (input == "Save"){
Save(data);
}
}
}
else {
cout << "Error opening file ";
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.