NEW Programming Assignment C++

Programming Assignment #3

Inventory Class Hierarchy

Deliverables: Submit a zip file of your project folder including the input files used to demo you program. Demo is mandatory for any credit.

Demo: Demo final version of your project using the test driver function and input data shown in the last exercise of Task 5. No credit for the lab or assignment without a demo by the due date.

===========================================================================

This assignment builds on the class hierarchy of Unit 5 Lab (see below)

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




Book

  • title: string

  • author: string

  • publisher: string

+ Book()

+ Book(string, int, double, string, string, string)

+ getTitle() const: string

+ getAuthor() const: string

+ getPublisher() const : string

+ setTitle(string): void

+ setAuthor(string): void

+ setPublisher(string): void

+ display() const: void

+ read(ifstream &): void

Task 1: List of authors

Our design so far assumes that a book has only one author. You need to modify class Book to be able to accommodate multiple authors, each represented with a name. A good way to do this is to use the C++ Standard Template Library and declare member authors to be of type vector<string>. However, since one of the objectives of this assignment is to practice with dynamic data and pointers as members of a class, we want to define a new class List to store the list of author names.


List should have two private data members, a pointer to string which will be use to dynamically allocate an array of string of a certain size, integer representing the size of the allocate array (call it listCapacity), and integer representing the actual number of elements in the list (call it listSize). List should also have the following public member functions (at a minimum):

  1. a parametrized class constructor to initialize the list to at least one item. Note that the constructor does not populate the list, it just allocates a string array of a given capacity. You should also provide a default constructor.

  2. a function size() that returns the number of items in the list.

  3. a function that returns the value in the ith element (be sure to validate i).

  4. a function that adds a new item to the back of the list (call it push_back() ). If the list is full, allocate a new array of the appropriate size, copy all existing elements into the new array, and add the new item.

  5. a function that removes the element at the back of the list (call it pop_back()).

  6. a function clear() to delete all the items from the list.


  1. Define a class List with the above specification.


Task 2: Testing Class List

Consider the following test driver program:

#include "List.h"

void testList();

int main()

{

tesList();

cin.get();

system("pause");

return 0;

}

//*******************************************************

// This function is used to test class List

int testList()

{

List a(3); // testing constructor, push_back(), size(), and getItem()

a.push_back("James Bond");

a.push_back("Iman Tillman");

a.push_back("Mary Joe Hernandez");

a.push_back("Roger Moore"); // one more that original capacity

cout <<"Number of items in list a: " << a.size()<<endl;

for (int i =0; i < a.size(); ++i)

cout <<a.getItem(i) <<endl;

cout <<endl<<"Testing List coping: "<<endl;

List b = a; // testing copy constructor

cout << endl << "list b: "<<endl;

for (int i =0; i < b.size(); ++i)

cout <<b.getItem(i) <<endl;

a.pop_back();

a.pop_back();

a.pop_back();

a.push_back("John Doe");

cout <<endl<<"Displaying b after changing a: "<<endl;

for (int i = 0; i < b.size(); ++i)

cout <<b.getItem(i) <<endl;

return 0;

}

  1. Predict the above program output with class List as specified in Task 1.




  1. Run the test program and compare its output to your prediction in Exercise 1. Did it behave as you expected. Explain why. You may also run a test case with the assignment operation.

Task 3: Class Dynamic Data Members

  1. Add a class copy constructor to class List to do deep copying. Use the test program from Task 2 to test it.


  1. Add a class destructor to delete dynamically allocated data.


  1. Add a public member function that overloads the assignment operator to perform deep copying (remember to deallocate dynamic data of the destination object before copying). Use the test program from Task 2 to test it.


You may add any other member functions that you deem necessary or helpful for other tasks. However, you should be careful not to provide any public member function that modifies the size or capacity of the list since these should only be changed by the constructors or when an item is added or removed from the list.


  1. Demo your final class List definition


Task 4: Update class Book with the authors list member

  1. Modify data member authors in class Book to be of type List. Add the following accessors and mutators (at a minimum):

    1. getAuthors: returns a List object holding the authors name list and number of authors

    2. addAuthor: adds an author to the list of authors

    3. removeAuthor: removes the last author added to the list.

    4. clearAuthors: removes all authors.


  1. Update member functions display of class Book and class InventoryItem to

    1. Output to any output stream not just cout, and


    1. Output book information in the following format (all on one line):


description<tab>quantity on hand<tab>price<tab>number of authors<tab>list authors separated by tabs<tab>publisher

  1. Overload the stream insertion operator << for both class Book and class InventoryItem. Important Note: keep the display functions, and have operator << call function display. This is particularly important for last part of this assignment.


  1. Update the read member functions of class Book and class InventoryItem to read book information from an input stream in the following format:


category, description, quantity on hand, price, number of authors, list of author names separated by commas, publisher


where category is a string that indicates the inventory item category. Assume the allowable categories are: book for books and general for anything else.


Note the following:

  1. Book::read must call InventoryItem::read

  2. The comma after price must be consumed by Book:read

  3. You may use the following function to trim leading and trailing white spaces from strings after you call getline():

void trim(string& s)

{

size_t p = s.find_first_not_of(" \t\n");

s.erase(0, p);

p = s.find_last_not_of(" \t\n");

if (string::npos != p)

s.erase(p+1);

}

  1. Overload the stream extraction operator >> for both classes. Again, Do NOT replace the read function, simply have the operator >> call function read.


  1. Use the driver program below to demo your updated Book class. You may also download a text version of it from file demoNewLab in the Lab6 folder (on EagleOnline). Make sure to include the header files with the Book class definition.

#include "book.h"

#include <vector>

//void testList();

int demoNewBook();

int main() {

demoNewBook();

system("pause");

return 0;

}

int demoNewBook()

{

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 (while (!file.eof())

{

file >> temp;

bookList.push_back(temp);

}

for (size_t i = 0; i < bookList.size(); ++i){

cout << "Book "<<i+1<< ": "<<bookList[i];

cout << endl;

}

system("pause");

return 0;

}

Task 5: Polymorphism

Assume inventory items are of two categories: books or general items (category list can be expanded). As discussed above, a general inventory item has the data fields description, quantity on hand, and price whereas a book has the additional fields title, author list (number of authors and list of their names), and publisher name. Assume that inventory data files contain information from the two categories, each tagged with their category. Example;

general, IPhone 6, 35, 599.0

book, CS book, 15, 199.50, Intro to Programming with C++, 2, D.S. Malik, T. J. Holly, Pearson

general, Samsung Galaxy 7S, 376.99

book, Novel Sci Fi, 2, 6.99, The Martians, 1, Andy Weir, Media Type


  1. Consider the following test program. What do you expect it to output if run with the sample input in the box above?

#include "book.h"

#include <vector>

//void testList();

//int demoNewBook();

int testPolymorphism();

int main()

{

testPolymorphism();

system("pause");

return 0;

}

//*******************************************************

// This program is used to test polymorphism of input and output operations of class Book

int testPolymorphism()

{

ifstream file("data2.csv"); //Assume the name of input file is “data2.csv”

if (!file)

{

cerr << "Can't open file " << "data.csv" << endl;

return(EXIT_FAILURE);

}

InventoryItem *temp;

string category;

getline(file, category, ','); // get the category for first item

trim(category); // remove leading and trailing spaces

if (category == "general")

temp = new InventoryItem;

else if (category == "book")

temp = new Book;

else

{ cout <<endl<<"Invalid category"<<endl;

return(EXIT_FAILURE);

}

file >> *temp;

cout << *temp;

getline(file, category, ','); // get the category for 2nd item

trim(category);

if (category == "general")

temp = new InventoryItem;

else if (category == "book")

temp = new Book;

else

{ cout <<endl<<"Invalid category"<<endl;

return(EXIT_FAILURE);

}

file >> *temp;

cout << *temp;

return 0;

}

  1. Run the test program and verify your prediction. Why isn’t the book information displayed correctly?

  1. Make the required changes to base class InventoryItem so that the appropriate input and output functions are called depending on the type of object calling them i.e. make the functions polymorphic.


  1. Demo you program with the test data and program of exercise 1 above.


Final Project Demo and Submission:

Demo your project with the test driver function below and the sample input shown in the textbox above. Submit a zip file containing the final version of your project on EagleOnline by the assignment due date.

//*******************************************************

// This test function is used to demo programming assignment #3

int demoProject()

{

ifstream file("data2.csv"); //Assume the name of input file is “data.csv”

if (!file)

{

cerr << "Can't open file " << "data.csv" << endl;

return(EXIT_FAILURE);

}

vector<InventoryItem *> productList;

InventoryItem *temp;

string category;

while (!file.eof())

{

getline(file, category, ','); // get the category for next item

trim(category);

if (category == "general")

temp = new InventoryItem;

else if (category == "book")

temp = new Book;

else

{

cerr <<endl<<"Invalid category"<<endl;

return(EXIT_FAILURE);

}

file >> *temp;

productList.push_back(temp);

}

for (int i = productList.size() -1; i >= 0 ; i--){

cout << "Book "<<i+1<< ": "<<*productList[i];

cout << endl<<endl;

}

return 0;

}


10