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

This is a muliple part question - I have the code for the first part but need to

ID: 3915815 • Letter: T

Question

This is a muliple part question - I have the code for the first part but need to get the code for everything to complete task 4

In this lab, you will (1) modify class InventoryItem to include a price data member and (2) defined a derived class of InventoryItem to represent books.

Task 1:   Add a data member to class InventoryItem

Exercise 1.Add a data member to class InventoryItem to represent the item’s price

Exercise 2.Update the class constructors to initialize the price data member

Exercise 3.Add an accessor (getPrice) that returns the value of price and a mutator to set it (setPrice).

Exercise 4.Use the new accessor function to test the class constructors and setPrice().

Exercise 5.Update the display() member function to display inventory item price, in addition to the other data members. (Note: make sure to change its name to display() if you haven’t done so already).

Exercise 6.Update the read() member function to read price, in addition to description and quantity on hand from an input stream. Note: Make sure to change its name to read(ifstream &). You may assume a simplified CSV (Comma Separated Values) format for the input: every line has the data values of one inventory item separated by commas. Example:

Iphone 6, 33, 599.00

Samsung Galaxy s6, 21, 439.50

   You may use the following function to read data for one inventory item:

void InventoryItem::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, ' ');

}

Below is a UML class diagram of class InventoryItem:

InventoryItem

description: string

qantityOnHand: int

price: double

+ InventoryItem()

+ InventoryItem(string, int, double)

+ getDescription() const: string

+ getQuantityOnhand() const: int

+ getPrice() const : double

+ setDescription(string): void

+ setQuantityOnhand(int): void

+ setPrice(double): void

+ display() const: void

+ read(ifstream &): void

Task 1 Submission (50%):

Write a test program (i.e. function main() ) to read from the an input file any number of inventory items (i.e. read until end of file) and then displays the data on the screen using the read() and display() member functions of class InventoryItem. Use the sample data below to test your class. Submit a screenshot of your program execution.

Iphone 6, 33, 599.00

Samsung Galaxy s6, 21, 439.50

Submit a copy of your class definition and test program in a text file.

//This is the code I have for the task above - Work from this.

#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;

}

//From here is where I need the rest of the code- to complete task 4

Task 2: Derived class Book

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.

Exerise 4.Draw a UML class diagram of class Book

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.

Task 4: Final testing of Book class

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 <iostream> include <fstream>

#include <iomanip>

#include <string>

#include <vector>

#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"<<endl;

              return(EXIT_FAILURE);

       }

      

       vector<Book> 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;

}

Task 4 Submission (50%):

Submit a copy of your input file (text format)

Submit a copy of your class definitions and test program (text format)

Submit a screenshot of your program execution

InventoryItem

description: string

qantityOnHand: int

price: double

+ InventoryItem()

+ InventoryItem(string, int, double)

+ getDescription() const: string

+ getQuantityOnhand() const: int

+ getPrice() const : double

+ setDescription(string): void

+ setQuantityOnhand(int): void

+ setPrice(double): void

+ display() const: void

+ read(ifstream &): void

Explanation / Answer

//Book.h
#pragma once
#ifndef BOOK_H
#define BOOK_H
#include "InventoryItem.h"
#include <string>
#include <iostream>
using namespace std;
class Book : InventoryItem {
private:
   string Title;
   string NameOfAuthor;
   string Publisher;
public:
   Book() {
       Title = "";
       NameOfAuthor = " ";
       Publisher = " ";
   }
   Book(string z, int x, double q, string a, string b, string c) {
       Title = a;
       NameOfAuthor = b;
       Publisher = c;
       setDescription(z);
       setQuantityOnHand(x);
       setPrice(q);
      
   }
   void setTitle(string a) {
       Title = a;
   }
   void setAuthor(string b) {
       NameOfAuthor = b;
   }
   void setPublisher(string c) {
       Publisher = c;
   }
   string getTitle() {
       return Title;
   }
   string getAuthor() {
       return NameOfAuthor;
   }
   string getPublisher() {
       return Publisher;
   }
   void display() {
       cout << "Tile:" << Title << endl;
       cout << "Authotr :" << NameOfAuthor << endl;
       cout << "Publisher:" << Publisher << endl;

   }
   void read(ifstream& inFile) {
       string line;
       int p;
       double q;
       char comma;
       getline(inFile, line, ',');
       inFile >> p;
       inFile >> comma;
       inFile >> q;
       inFile >> comma;
       string line1, line2, line3;
       getline(inFile, line1, ',');
       getline(inFile, line2, ',');
       getline(inFile, line3, ',');
       setTitle(line1);
       setAuthor(line2);
       setPublisher(line3);


   }
  
};


#endif // !BOOK_H


---------------------------------------------------------
//Inventory.h
#pragma once
#ifndef INVENTORYITEM_H
#define INVENTORYITEM_H
#include <string>
#include <iostream>
#include <fstream>
//InventoryItem.cpp
using namespace std; // have to include the using namespace in header file
class InventoryItem
{
private:
   string description; // The item description
   int quantityOnHand;          // Number of units on hand
   double price;
public:
   InventoryItem();
   InventoryItem(int, string);
   InventoryItem(double);
   // Accessor functions
   string getDescription() {
       return description;
   }
   int getQuantityOnHand() {
       return quantityOnHand;
   }
   double getPrice() {
       return price;
   }

   // Mutator functions
   void setDescription(string); // sets description member to argument
   void setQuantityOnHand(int); // sets QuantityOnHand to argument
   void display();
   void readInventoryItem(ifstream& inFile);
   void setPrice(double);

};
#endif

-----------------------------------------------------------------------
//Inventory.cpp
// InventoryItem
#include "InventoryItem.h"
#include <string>

InventoryItem::InventoryItem() {
   description = " ";
   quantityOnHand = 0;
}
InventoryItem::InventoryItem(int a, string d) {
   if (quantityOnHand >= 0)
       quantityOnHand = a;
   description = d;
}
void InventoryItem::setDescription(string d)
// Post: description data member is set to string d;
{
   description = d;
}
void InventoryItem::setPrice(double a) {
   price = a;
}

void InventoryItem::setQuantityOnHand(int q)
// Post: set quantityOnHand to to q if q >= 0; unchanged otherwise;
{
   if (q >= 0)
       quantityOnHand = q;

}
void InventoryItem::display()
{

   cout << "the quantity :" << quantityOnHand << endl;
   cout << " the descpreition : " << description << endl;
   cout << "the item price :" << price << endl;
}
void InventoryItem::readInventoryItem(ifstream& inFile) {
   string des;
   int quant;
   double p;
   if (inFile.is_open()) {

  
       string line;
       getline(inFile, line, ',');
       setDescription(line);
       int q; double p; char comma;
       inFile >> q;
       setQuantityOnHand(q);
       inFile >> comma;
       inFile >> p;
       InventoryItem::setPrice(p);
       inFile.ignore(100, ' ');
   }


}
InventoryItem::InventoryItem(double z) {
   if (z >= 0) {
       price = z;
   }
}
--------------------------------------------------------------
//main.cpp
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>
#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" << endl;
       return(EXIT_FAILURE);
   }

   vector<Book> 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;
}
--------------------------------------------------
data.vcs
Book,10,10.00,The Old Man and the Sea,Ernest Hemingway,Benediction Books
Book,15,12.00,The Great Gatsby,F. Scott Fitzgerald,Scribner
Book,12,8.50,Lord of the Flies,William Golding,Faber and Faber


------------------------------------------------
data.txt
Iphone 6, 33, 599.00
Samsung Galaxy s6, 21, 439.50

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