Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

use the SimpleVector class template(below) to include: (language c++) ? A sort function. You can use the selection sort algorithm (see code below that works

use the SimpleVector class template(below) to include: (language c++)

?

A sort function. You can use the selection sort algorithm (see code below that works for an array of int. You will need to modify slightly (figure out where to use T).Also, no parameter of

size needed since SimpleVector knows number of elements in array.

?

A search function.It receives a value to search for (datatype T). If found, return its position in the list. If not found, return -1.

You should test this functionality on a simple list of int to make sure it all works BEFORE moving on.

?

You will also add code to make the list expand when full, but you should make that the LAST thing you work on (see end of assignment)

Create a class called Item to represent items in inventory. It will need attributes of:

?

SKU

?

Description

?

Quantity in stock

Functions needed (alongwith the standard set/get functions for all attributes)

?

operator<<(to specify the format to print an Item, see sample output)

?

operator<(compare items based on SKU)

?

operator==(compare items based on SKU)

?

You will need 2 parameterized constructors: 1. one that takes SKU, desc, and quantity.

2. Another that takes SKU only (because search needs an Item object)

?

Note that the search method expects an ITEM object. So you should create a dummy item with this SKU to pass to the search function (make a constructor in Item to create an object with a particular SKU but desc as and quantity as 0 ASK IF CONFUSED)

?

You will need to overload == to make two Items equal if their SKU is the same.

Your app has the following menu options (note upper or lower case should work):

A: add an item

Prompt for SKU. Validate that it does not exist in the inventory.

Enter description, quantity in stock and add object to vector.

S: search for an item

Enter an SKU to search for. If found, display the info about it.

Ask if updates need to be made to description or quantity.

L: list all items

Print a list of all items sorted by SKU

Q: quit

Sample Run (use any type of product description you like, these are just examples)

Inventory Menu

A-add an item

S-search for an item

L-list all items

Q-quit

Your choice: L

Choice is L

No items

Inventory Menu

A-add an item

S-search for an item

L-list all items

Q-quit

Your choice: S

Enter SKU to find: 10

10 is not a valid SKU

Inventory Menu

A-add an item

S-search for an item

L-list all items

Q-quit

Your choice: A

Enter SKU: 100

Enter description: keyboard

Enter quantity: 10

Inventory Menu

A-add an item

S-search for an item

L-list all items

Q-quit

Your choice: L

SKU: 100, keyboard [stock = 10 ]

Inventory Menu

A-add an item

S-search for an item

L-list all items

Q-quit

Your choice: A

Enter SKU: 200

Enter description: mouse

Enter quantity: 20

Inventory Menu

A-add an item

S-search for an item

L-list all items

Q-quit

Your choice: A

Enter SKU: 110

Enter description: USBdrive

Enter quantity: 2

Inventory Menu

A-add an item

S-search for an item

L-list all item

Q-quit

Your choice: L

SKU: 100, keyboard [stock = 10 ]

SKU: 110, USBdrive [stock = 2 ]

SKU: 200, mouse [stock = 20 ]

Inventory Menu

A-add an item

S-search for an item

L-list all items

Q-quit

Your choice: S

Enter SKU to find: 110

SKU: 110, USBdrive [stock = 2 ]

Update

description (y/n)?n

Update quantity (y/n) ? y

Enter new quantity: 7

Quantity updated

SKU: 110, USBdrive [stock = 7 ]

Inventory Menu

A-add an item

S-search for an item

L-list all items

Q-quit

Your choice:L

SKU: 100, keyboard [stock = 10 ]

SKU: 110, USBdrive [stock = 7 ]

SKU: 200, mouse [stock = 20 ]

Inventory Menu

A-add an item

S-search for an item

L-list all items

Q-quit

Your choice: q

Goodbye!

Test your program carefully, make sure to test all features (this output does not)! Check the output carefully! Try to break it , think of good testing scenarios.

Code for selection sort

void selectionSort(

int array[], int size

)

{

int startScan, minIndex;

int

minValue;

int size = array.size();

for (startScan = 0; startScan < (size

-

1); startScan++)

{

minIndex = startScan;

minValue = array[startScan];

for (int index = startScan + 1; index < size; index++)

{

if (array[index] < minValue)

{

minValue = array[index];

minIndex = index;

}

}

//swap

array[minIndex] = array[startScan];

array[startScan] = minValue;

}

}

Make the list self-expanding (required)

Enhance SimpleVector so that the list never becomes full. When inserting a value, if full, a new array should be allocated with a size of 10 more than the current size. Copy all the values over to the newly allocated array, and then delete the previous one. THINK about the order of doing things ...(draw pictures) Test by making the maxSize initially very small, like 2 elements. Put a cout statement in your insert to show when a new list is being created. Add 2 more values and make sure all elements print correctly.

//start it as a vector of int

template

class SimpleVector

{

private:

//need a pointer to represent the array

T* list;

int numElements;

int maxSize; //current number of elements allocated

public:

//default constructor

SimpleVector();

//copy constructor

SimpleVector(const SimpleVector &);

//destructor

~SimpleVector();

//insert an item on the list

void insert(T);

void print();

bool isFull();

bool isEmpty();

int getSize() { return numElements; }

T& getItemAt(int);

//overload []

T& operator[](int);

bool isValidSubscript(int s){

return (s >= 0 && s < numElements);

}

//exception handling class

class BadSubscript

{

private:

int sub;

public:

BadSubscript() { sub = 0; }

BadSubscript(int s) { sub = s; }

int getSub() { return sub; }

};

};

//for a template all class definitions go in .h file

//copy constructor

template

SimpleVector ::SimpleVector(const SimpleVector &obj)

{

//make a DEEP copy of obj

//copy the array size

numElements = obj.numElements;

maxSize = obj.maxSize;

//allocate memory for this copy

list = new T[maxSize];

//copy the data over element by element

for (int i = 0; i < numElements; i++)

list[i] = obj.list[i];

}

template

T& SimpleVector::operator[](int position)

{

return getItemAt(position);

}

template

T& SimpleVector::getItemAt(int position)

{

//return value at this position of the list

//make sure position is valid

if (isValidSubscript(position))

{

return list[position];

}

else

{

throw (BadSubscript(position));

}

}

template

bool SimpleVector::isFull()

{

//return true if array is full

return (numElements == maxSize);

}

template

bool SimpleVector::isEmpty()

{

return (numElements == 0);

}

template

SimpleVector::SimpleVector()

{

//sest a default size

maxSize = 10;

//allocate the array

list = new T[maxSize];

numElements = 0;

}

template

void SimpleVector::insert(T val)

{

//really need to make sure there is space

//need to increase size of list if not

if (!isFull())

{

//make val the next element of the array

list[numElements] = val;

//update numElements

numElements++;

}

}

//destructor

template

SimpleVector::~SimpleVector()

{

//release the memory that has been allocated

delete[]list;

cout << "destructor called, memory deleted" << endl;

}

template

void SimpleVector::print()

{

//print all the data in the list

for (int i = 0; i < numElements; i++)

{

cout << list[i] << endl;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Deductive And Object Oriented Databases Second International Conference Dood 91 Munich Germany December 18 1991 Proceedings Lncs 566

Authors: Claude Delobel ,Michael Kifer ,Yoshifumi Masunaga

1st Edition

3540550151, 978-3540550150

More Books

Students also viewed these Databases questions