Question: Instructions: For this lab, you will use the code in the D List .cpp file and the D List .h file to make a list

Instructions: For this lab, you will use the code in the DList.cpp file and the DList.h file to make a list of items for a camera store, and implement functions to insert, find, erase, display items. You will have to modify the code, since it was written for a list of integers. The items for the list are in a file called InventoryFile.txt

Each line of the file InventoryFile has the following format:

Item Number an integer

Number in stock an integer (between 0 and 999)

Unit Price a floating-point value

Minimum inventory level an integer

Item name a character string

You will probably have to define a struct, to hold the info for each item, and then your list will be an array of the structs

Your driver file should create the list, read in all the items, and then ask the user what to do (insert, delete, etc)

When you are done, the list should be output to a file called NewInventory.

DList.cpp:

/*-- DList.cpp----------------------------------------------------------- This file implements List member functions. -------------------------------------------------------------------------*/ #include  #include  using namespace std; #include "DList.h" //--- Definition of class constructor List::List(int maxSize) : mySize(0), myCapacity(maxSize) { myArray = new(nothrow) ElementType[maxSize]; assert(myArray != 0); } //--- Definition of class destructor List::~List() { delete [] myArray; } //--- Definition of copy constructor List::List(const List & origList) : mySize(origList.mySize), myCapacity(origList.myCapacity) { //--- Get new array for copy myArray = new(nothrow) ElementType[myCapacity]; if (myArray != 0) // check if memory available //--- Copy origList's elements into this new array for(int i = 0; i < mySize; i++) myArray[i] = origList.myArray[i]; else { cerr << "*** Inadequate memory to allocate storage for list *** "; exit(1); } } //--- Definition of assignment operator const List & List::operator=(const List & rightHandSide) { if (this != &rightHandSide) // check that not self-assignment { //-- Allocate a new array if necessary if (myCapacity != rightHandSide.myCapacity) { delete[] myArray; myCapacity = rightHandSide.myCapacity; myArray = new(nothrow) ElementType[myCapacity]; if (myArray == 0) // check if memory available { cerr << "*Inadequate memory to allocate stack *** "; exit(1); } } //--- Copy rightHandSide's list elements into this new array mySize = rightHandSide.mySize; for(int i = 0; i < mySize; i++) myArray[i] = rightHandSide.myArray[i]; } return *this; } //--- Definition of empty() bool List::empty() const { return mySize == 0; } //--- Definition of display() void List::display(ostream & out) const { for (int i = 0; i < mySize; i++) out << myArray[i] << " "; } //--- Definition of output operator ostream & operator<< (ostream & out, const List & aList) { aList.display(out); return out; } //--- Definition of insert() void List::insert(ElementType item, int pos) { if (mySize == myCapacity) { cerr << "*** No space for list element -- terminating " "execution *** "; exit(1); } if (pos < 0 || pos > mySize) { cerr << "*** Illegal location to insert -- " << pos << ". List unchanged. *** "; return; } // First shift array elements right to make room for item for(int i = mySize; i > pos; i--) myArray[i] = myArray[i - 1]; // Now insert item at position pos and increase list size myArray[pos] = item; mySize++; } //--- Definition of erase() void List::erase(int pos) { if (mySize == 0) { cerr << "*** List is empty *** "; return; } if (pos < 0 || pos >= mySize) { cerr << "Illegal location to delete -- " << pos << ". List unchanged. *** "; return; } // Shift array elements left to close the gap for(int i = pos; i < mySize; i++) myArray[i] = myArray[i + 1]; // Decrease list size mySize--; }

DList.h:

/*-- DList.h -------------------------------------------------------------- This header file defines the data type List for processing lists. Basic operations are: Constructor Destructor Copy constructor Assignment operator empty: Check if list is empty insert: Insert an item erase: Remove an item display: Output the list << : Output operator -------------------------------------------------------------------------*/ #include  #ifndef DLIST #define DLIST typedef int ElementType; class List { public: /******** Function Members ********/ /***** Class constructor *****/ List(int maxSize = 1024); /*---------------------------------------------------------------------- Construct a List object. Precondition: maxSize is a positive integer with default value 1024. Postcondition: An empty List object is constructed; myCapacity == maxSize (default value 1024); myArray points to a dynamic array with myCapacity as its capacity; and mySize is 0. -----------------------------------------------------------------------*/ /***** Class destructor *****/ ~List(); /*---------------------------------------------------------------------- Destroys a List object. Precondition: The life of a List object is over. Postcondition: The memory dynamically allocated by the constructor for the array pointed to by myArray has been returned to the heap. -----------------------------------------------------------------------*/ /***** Copy constructor *****/ List(const List & origList); /*---------------------------------------------------------------------- Construct a copy of a List object. Precondition: A copy of origList is needed; origList is a const reference parameter. Postcondition: A copy of origList has been constructed. -----------------------------------------------------------------------*/ /***** Assignment operator *****/ const List & operator=(const List & rightHandSide); /*---------------------------------------------------------------------- Assign a copy of a List object to the current object. Precondition: none Postcondition: A copy of rightHandSide has been assigned to this object. A const reference to this list is returned. -----------------------------------------------------------------------*/ /***** empty operation *****/ bool empty() const; //--- See Figure 6.1 for documentation /***** insert and erase *****/ void insert(ElementType item, int pos); //--- See Figure 6.1 for documentation (replace CAPACITY with myCapacity) void erase(int pos); //--- See Figure 6.1 for documentation /***** output *****/ void display(ostream & out) const; //--- See Figure 6.1 for documentation private: /******** Data Members ********/ int mySize; // current size of list int myCapacity; // capacity of array ElementType * myArray; // pointer to dynamic array }; //--- end of List class //------ Prototype of output operator ostream & operator<< (ostream & out, const List & aList); #endif

inventoryFile.txt:

1011 20 54.95 15 Telephoto-Pocket-Camera 1012 12 24.95 15 Mini-Pocket-Camera 1021 20 49.95 10 Polaroid-1Step-Camera 1022 13 189.95 12 Sonar-1Step-Camera 1023 15 74.95 5 Pronto-Camera 1031 9 279.99 10 8MM-Zoom-Movie-Camera 1032 15 310.55 10 8MM-Sound/ZoomMovieCamera 1041 10 389.00 12 35MM-Minolta-SLR-XG-7-Camera 1042 11 349.95 12 35MM-Pentax-SLR-AE-1-Camera 1043 20 319.90 12 35MM-Canon-SLR-ME-Camera 1044 13 119.95 12 35MM-Hi-Matic-Camera 1045 20 89.99 12 35MM-Compact-Camera 1511 7 129.95 5 Zoom-Movie-Projector 1512 9 239.99 5 Zoom-Sound-Projector 1521 10 219.99 5 Auto-Carousel-Projector 1522 4 114.95 5 Carousel-Slide-Projector 2011 4 14.95 5 Pocket-Strobe 2012 12 48.55 10 StrobeSX-10 2013 10 28.99 15 Electonic-Flash-SX-10 3011 13 32.99 15 Tele-Converter 3012 14 97.99 15 28MM-Wide-Angle-Lens 3013 13 87.95 15 135MM-Telephoto-Lens 3014 8 267.95 5 35-105MM-Zoom-Lens 3015 7 257.95 5 80-200MM-Zoom-Lens 3111 4 67.50 5 Heavy-Duty-Tripod 3112 10 19.95 5 Lightweight-Tripod 3511 10 159.99 5 35MM-Enlarger-Kit 4011 4 35.98 5 40x40-Deluxe-Screen 4012 10 44.98 5 50x50-Deluxe-Screen 5011 17 4.29 25 120-Slide-Tray 5012 33 2.95 25 100-Slide-Tray 5021 12 6.25 15 Slide-Viewer 5031 12 55.95 10 Movie-Editor 6011 10 59.95 5 Condenser-Microphone 6111 80 0.89 100 AA-Alkaline-Battery 7011 19 19.79 20 Gadget-Bag 8011 45 1.49 50 135-24-Color-Film 8021 60 0.99 50 110-12-Color-Film 8022 42 1.45 50 110-24-Color-Film 8023 37 0.59 25 110-12-B/W-Film 8024 43 0.95 25 110-24-B/W-Film 8031 44 0.89 50 126-12-Color-Film 8032 27 0.59 25 126-12-B/W-Film 8041 39 6.89 50 8MM-Film-Cassette 8042 25 11.89 20 16MM-Film-Cassette 9111 10 959.99 12 Combination-Camera-Kit

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!