//This was code from a previous exercise - let me know if you need the question
ID: 3913779 • Letter: #
Question
//This was code from a previous exercise - let me know if you need the question for this code.
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<cstdlib>
#include<cstring>
using namespace std;
class InventoryItem{
private:
string description;
int quantityOnHand;
double price;
public:
InventoryItem()
{
description="NULL";
quantityOnHand=0;
price=0;
}
InventoryItem(string desc, int q, double p)
{
description=desc;
quantityOnHand=q;
price=p;
}
string getDescription()
{
return description;
}
int getQuantityOnHand()
{
return quantityOnHand;
}
double getPrice()
{
return price;
}
void setDescription(string d)
{
this->description=d;
}
void setPrice(double p)
{
this->price=p;
}
void setQuantityOnHand(int q)
{
this->quantityOnHand=q;
}
void display()
{
cout<<"Description: "<<description<<endl<<"Quantity: "<<quantityOnHand<<endl<<"Price: "<<price<<endl;
}
void read(ifstream &inFile)
{
string line;
getline(inFile, line, ',');
setDescription(line);
int q; double p; char comma;
inFile >> q;
setQuantityOnHand(q);
inFile >> comma;
inFile >> p;
setPrice(p);
inFile.ignore(100, ' ');
}
};
int main()
{
ifstream input;
input.open("inventory.txt");
vector<InventoryItem> inventory;
while(!input.eof())
{
InventoryItem item;
item.read(input);
inventory.push_back(item);
}
//print the data
for(int i=0; i<inventory.size(); i++)
{
inventory.at(i).display();
cout<<endl<<endl;
}
cin.get();
return 0;
}
//This was the task before the exercise 1 that I need.
Exerise 1.Declare a class Book as a derived class of InventoryItem. You only need to do the class specification in this task. Write a specification in a header file called Book.h. Make sure to include InventoryItem.h file from Task 1.
Exerise 2.class Book should have the following data members:
Title
Name of author (assume only one author for now)
Publisher
Exerise 3.Write the prototypes of the following member functions in class Book
A default constructor that initializes the above data members to default values of your choice and uses the default constructor of the base class
Accessor and mutator functions for book title, author, and publisher
A parametrized constructor that initializes all data members including description, price, and quantity on hand. Remember to use as much of the code from the base class and to use the set functions to set data members.
A member function display to display all attributes of a book
A member function read to read all attributes of a book from an input stream.
Task 3: Incremental implementation and testing
Exercise 1.Write a stub for each member function of class Book. You can this either in Book.h or in a separate implementation file (Book.cpp). A function stub consists of the function header and an empty body if the function is a void function. For value-returning functions, have the function return a dummy value.
Exercise 2.Write a driver program to test your new class. In function main() of the driver program, declare one instance of class Book. Build your project and fix all syntax errors.
Exercise 3.Incrementally implement the member functions of class Book. Each time you implement a function test it. Start with the set functions, then implement the get functions. Use the get function to test the set functions. Then implement and test the constructors. Finally, implement and test the display and read member functions.
When implementing your class, you need to adhere to the following practices:
Use code from the base class whenever possible
Any changes to the data members should be done using the class mutator functions.
//This is the part needed
Exercise 1.Use the driver program below to test your Book class. Make sure to include the header file with the Book class you defined in the previous tasks. Create a sample input file with two or three book records for testing purpose.
//*******************************************************
// Driver program to test class Book.
// Note that it uses the vector class from the Standard Template Library
// to create a dynamic list of Book instances
#include include
#include
#include
#include
#include “Book.h”
using namespace std;
int main()
{
ifstream file("data.csv"); //Assume the name of input file is “data.csv”
if (!file)
{
cerr <<"Can't open file "<< "data.csv"<
return(EXIT_FAILURE);
}
vector bookList;
Book temp;
while (file)
{
temp.read(file);
bookList.push_back(temp);
}
for (int i = 0; i < bookList.size(); ++i)
bookList[i].display();
system("pause");
return 0;
}
1. Submit a copy of your input file (text format)
2. Submit a copy of your class definitions and test program (text format)
3. Submit a screenshot of your program execution
*Please let me know if more info is needed this is form a exercise with multiple tasks
Explanation / Answer
/* Bookshop.cppp*/
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
class book
{
char author[20];
char title[20];
char publisher[20];
double price;
int stock;
public:
book();
void insertdata();
void display();
int search(char[],char[]);
void nocopies(int);
};
book::book()
{
char *author=new char[50];
char * title=new char[50];
char *publisher=new char[50];
price=0;
stock=0;
}
void book::insertdata()
{
cout<<" Enter the name of Book:";
cin>>title;
cout<<" Enter The Name Of Author:";
cin>>author;
cout<<" Enter The name of Publisher:";
cin>>publisher;
cout<<" Enter the Price of book:";
cin>>price;
cout<<" Enter Stock of book:";
cin>>stock;
}
void book::display()
{
cout<<" "<<title<<" "<<author<<" "<<publisher<<" "<<price<<" "<<stock;
}
int book::search(char t[],char a[])
{
if(strcmp(title,t)&&(strcmp(author,a)))
{
return 0;
}
else
{
return 1;
}
}
void book::nocopies(int num)
{
if(stock>=num)
{
cout<<" Title is avilable";
cout<<" Cost of"<<num<<"Books is Rs."<<(price*num);
}
else
{
cout<<" Required copies not in stock";
}
}
int main()
{
int ch,n,i,flag=0,copies,key=0;
book b[100];
char bname[50];
char key_title[50],key_author[50];
do
{
cout<<" ************Book Store*******************";
cout<<" 1.Insert Details of book 2.Display 3.search 4.exit";
cout<<" Enter Your Choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<" How many books data u want to enter";
cin>>n;
for(i=0;i<n;i++)
{
b[i].insertdata();
}
break;
case 2:
cout<<" "<<"TITLE"<<" "<<"AUTHOR"<<" "<<"PUBLISHER"<<" "<<"PRICE"<<" "<<"STOCK";
for(i=0;i<n;i++)
{
cout<<" ";
b[i].display();
}
break;
case 3:
cout<<" Enter title of required book";
cin>>key_title;
cout<<" Enter author of required book";
cin>>key_author;
for(i=0;i<n;i++)
{
if(b[i].search(key_title,key_author))
{
flag=1;
cout<<" "<<"TITLE"<<" "<<"AUTHOR"<<" "<<"PUBLISHER"<<" "<<"PRICE"<<" "<<"STOCK";
b[i].display();
//break;
key=i;
}
}
if(flag==1)
cout<<" Book is available";
else
{
cout<<" book is Not available";
break;
}
if(flag==1)
{
cout<<" Please enter the required number of copies of the book";
cin>>copies;
b[key].nocopies(copies);
}
break;
case 4: exit(EXIT_SUCCESS);
break;
default :
cout<<" Wrong Choice";
break;
}
}while(ch!=5);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.