/*************************************************/ // R. A. Hillyard // partList.cpp // November 2001 // // Test program to demonstrate the use // of a class as a member of another class /*************************************************/ #include<iostream> #include "inventory.h" using namespace std; const int MaxListSize = 10; class PartList { public: PartList(); void addPart(Inventory item); void deletePart(int num); void upDateCost(int num); void display(); int lookupByPartNum(int num); public: Inventory partsList[MaxListSize]; int size; }; /*************************************************/ //default constructor /*************************************************/ PartList::PartList() { size = 0; } /*************************************************/ //add a part to the list /*************************************************/ void PartList::addPart(Inventory part) { if(size < MaxListSize) { partsList[size] = part; size++; } else cout << "The List is full. Entry not added\n"; } /*************************************************/ //delete a part from the list //shift each element below delete index up by one //this overwrites the deletion and shortens the array by one /*************************************************/ void PartList::deletePart(int num) { for(int i = 0; i < size; i++) { if(partsList[i].getPartNum() == num) //first find index to delete { for(int j = i; j < size; j++) //then shift all entries up one {partsList[j] = partsList[j+1];} size--; //and reduce size of array } }//end for i } /*************************************************/ //display each entry in list /*************************************************/ void PartList::display() { cout << "The List contains the following " << size << " parts\n"; for(int i = 0; i < size; i++) {cout << partsList[i];} cout << endl; } /*************************************************/ void PartList::upDateCost(int num) { double newPrice; int index = lookupByPartNum(num);
if(index >= 0) { cout << "Current price: "<< partsList[index].getCost() << endl; cout << "Enter new price: "; cin >> newPrice; partsList[index].setCost(newPrice); } else {cout << num << " not in the list - unable to update\n";} } /*************************************************/ int PartList::lookupByPartNum(int num) { for(int i = 0; i < size; i++) { if(partsList[i].getPartNum() == num) { return i;} }//end for i return -1; } /*************************************************/ /*************************************************/ int main() { //declare a variable of type PartList PartList inventoryList; //declare variables of type Inventory Inventory part1(1234, 542, 1022.99); Inventory part2(2345, 234, 77.69); Inventory part3(3567, 734, 22.34); Inventory part4(5678, 124, 337.69); Inventory part5(2333, 433, 57.69); //add parts to list inventoryList.addPart(part1); inventoryList.addPart(part2); inventoryList.addPart(part3); inventoryList.addPart(part4); inventoryList.addPart(part5); int num; //variable for user input //test of delete member function inventoryList.display(); //display list before delete cout << "Enter part number to delete: "; cin >> num; inventoryList.deletePart(num); inventoryList.display(); //dispaly list after delete //test of upDateCost cout << "enter part number to update cost: "; cin >> num; inventoryList.upDateCost(num); inventoryList.display(); //dispaly list after update } /*********************Program Output****************** The List contains the following 5 parts 1234 542 1022.99 2345 234 77.69 3567 734 22.34 5678 124 337.69 2333 433 57.69 Enter part number to delete: 3567 The List contains the following 4 parts 1234 542 1022.99 2345 234 77.69 5678 124 337.69 2333 433 57.69 enter part number to update cost: 2345 Current price: 77.69 Enter new price: 88.98 The List contains the following 4 parts 1234 542 1022.99 2345 234 88.98 5678 124 337.69 2333 433 57.69 */