*IMPORTANT: each command requires a function. And list of function prototypes. *
ID: 3889006 • Letter: #
Question
*IMPORTANT: each command requires a function. And list of function prototypes.
*IMPORTANT: each command requires a function. And list of function prototypes.
Program creating book-store database. Each command requires separate function. struct Book string ISBN; string title; int quantity; int edition; double price; Program needs to read and process a transaction file "storeTrans.txt". This file contains several commands such as: display, add, searchBook, delete, updatePrice, updateQuantity, sort and save. Each command requires new line Example of display: ISBN Title Edition Quantity Price 123456789 C++ForDummies 3 5 150.00 Add bookISBN bookTitle bookQuantity bookPrice Adds new book record to the database . Should read information from the transaction file Should check for duplicate book entries (same ISBN) If book doesn't exist: add new entry Should display message that the book was successfully added If book already exists, do not add. Display message ° use push back function to add to the end of vector “123456789 successfully added to the database “123456789 already exists. The number of copies is now 5…” " · . . SearchBook bookTitle Searches and returns all books with matching title. Exact title OR keyword found Display message . . . "Searching for book... 123456789 C++ForDummies 5” If no entry found ."Searching for book. Sorry, no matching book existsExplanation / 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
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.