C++ fix
InventoryManager/CSC2110/CD.h
#if !defined CD_H #define CD_H #include "Song.h" #include "Text.h" using CSC2110::String; #include "ListArray.h" using CSC2110::ListArray; namespace CSC2110 { class CD { private: String* artist; String* title; int year; int rating; int num_tracks; ListArray<Song>* songs; public: CD(String* artist, String* title, int year, int rating, int num_tracks); virtual ~CD(); String* getKey(); void addSong(String* title, String* length); void displayCD(); static ListArray<CD>* readCDs(const char* file_name); static int compare_items(CD* one, CD* two); static int compare_keys(String* sk, CD* cd); static char getRadixChar(CD* cd, int index); //1-based }; } #endif
InventoryManager/CSC2110/Keyboard.h
#if !defined KEYBOARD_H #define KEYBOARD_H #include "Text.h" using CSC2110::String; #include <string> using namespace std; namespace CSC2110 { class Keyboard { private: Keyboard(); public: virtual ~Keyboard(); static Keyboard* getKeyboard(); //pre: the string (character literal) that will prompt the user for input //post: the input read from the keyboard interpreted as an int is returned int readInt(string prompt); int getValidatedInt(string prompt, int min, int max); //pre: the string that will prompt the user for input //post: the input read from the keyboard interpreted as a double is returned double readDouble(string prompt); double getValidatedDouble(string prompt, double min, double max); //pre: the string that will prompt the user for input // the string to store the user input and the length of the input storage string //post: the text read from the keyboard is copied into the storage string String* readString(string prompt); }; } #endif
InventoryManager/CSC2110/libCSC2110.a
Keyboard.o
Matrix.o
Random.o
ReadFile.o
String.o
WriteFile.o
Tokens.o
Poly.o
CD.o
Song.o
InventoryManager/CSC2110/ListArray.h
#if !defined (LISTARRAY_H) #define LISTARRAY_H #include "ListArrayIterator.h" namespace CSC2110 { template < class T > class ListArray { private: int max_size; T** items; int sz; void arrayResize(int new_max_size); public: ListArray(); ~ListArray(); bool isEmpty(); int size(); void removeAll(); T* get(int index); void add(int index, T* item); void add(T* item); void remove(int index); void set(int index, T* item); ListArrayIterator<T>* iterator(); T** toArray(); }; template < class T > ListArray<T>::ListArray() { max_size = 10; items = new T*[max_size]; sz = 0; } template < class T > ListArray<T>::~ListArray() { delete[] items; //the items themselves are not deleted } template < class T > bool ListArray<T>::isEmpty() { return (sz == 0); } template < class T > int ListArray<T>::size() { return sz; } template < class T > //1-based T* ListArray<T>::get(int index) { T* item = NULL; if (index >= 1 && index <= sz) { item = items[index - 1]; } return item; } template < class T > void ListArray<T>::add(T* item) { add(sz + 1, item); //add the item to the end of the array list } template < class T > void ListArray<T>::add(int index, T* item) { if (index < 1 || index > sz + 1) { return; } //need more room in the array list if (sz == max_size) { arrayResize(2*max_size); } for (int i = sz; i >= index; i--) { items[i] = items[i - 1]; } items[index - 1] = item; sz++; } template < class T > void ListArray<T>::remove(int index) { if (index < 1 || index > sz) { return; } for (int i = index; i < sz; i++) { items[i - 1] = items[i]; } items[sz - 1] = NULL; sz--; /* if (sz < max_size/2 - 1) //halve the size of the array, smallest size of max_size should be 2 { arrayResize(max_size/2); } */ } template < class T > ListArrayIterator<T>* ListArray<T>::iterator() { ListArrayIterator<T>* iter = new ListArrayIterator<T>(items, sz); return iter; } template < class T > void ListArray<T>::set(int index, T* item) { //could use other methods already written, but this is more efficient if (index >= 1 && index <= sz) { items[index - 1] = item; //overwrite contents at that location } } template < class T > void ListArray<T>::arrayResize(int new_max_size) { max_size = new_max_size; T** temp = new T*[max_size]; for (int i = 0; i < sz; i++) { temp[i] = items[i]; } delete[] items; items = temp; } template < class T > void ListArray<T>::removeAll() { delete[] items; max_size = 10; items = new T*[max_size]; sz = 0; } template < class T > T** ListArray<T>::toArray() { int num_items = size(); T** to_array = new T*[num_items]; for (int i = 0; i < num_items; i++) { to_array[i] = items[i]; } return to_array; } } #endif
InventoryManager/CSC2110/ListArrayIterator.h
#if !defined (NULL) #define NULL 0 #endif #if !defined (LISTARRAYITERATOR_H) #define LISTARRAYITERATOR_H namespace CSC2110 { template < class T > class ListArrayIterator { private: int index; int sz; T** items; public: ListArrayIterator(T** items, int size); ~ListArrayIterator(); bool hasNext(); T* next(); }; template < class T > ListArrayIterator<T>::ListArrayIterator(T** itms, int size) { items = new T*[size]; for (int i = 0; i < size; i++) { items[i] = itms[i]; //snapshot of the data } index = 1; sz = size; } template < class T > ListArrayIterator<T>::~ListArrayIterator() { delete[] items; } template < class T > bool ListArrayIterator<T>::hasNext() { return (index <= sz); } template < class T > T* ListArrayIterator<T>::next() { T* item = NULL; if (hasNext()) { item = items[index - 1]; index++; } return item; } } #endif
InventoryManager/CSC2110/Matrix.h
#if !defined MATRIX_H #define MATRIX_H //the indices are not 0-based!! namespace CSC2110 { class Matrix { private: int rows; int cols; double* mat; public: Matrix(int rows, int cols); //constructor ~Matrix(); //destructor void displayMatrix(); double getElement(int row, int col); void setElement(int row, int col, double val); Matrix* add(Matrix* other); Matrix* multiply(Matrix* other); static Matrix* readMatrix(const char* file_name); //discuss static void writeMatrix(const char* file_name); }; } #endif
InventoryManager/CSC2110/Poly.h
#if !defined (POLY) #define POLY namespace CSC2110 { class Poly { private: int max_power; int degree; double* coeffs; public: Poly(int max_power); ~Poly(); int getDegree(); double getCoeff(int power); void setCoeff(int power, double coeff); double evaluate(double x); Poly* multiply(Poly* other); static Poly* multiply(Poly* one, Poly* two); void displayPoly(); static Poly* readPoly(const char* file_name); void writePoly(const char* file_name); }; } #endif
InventoryManager/CSC2110/Random.h
#if !defined RANDOM_H #define RANDOM_H namespace CSC2110 { class Random { private: Random(); void randomInit(); public: virtual ~Random(); static Random* getRandom(); int getRandomInt(int lower, int upper); float getRandomFloat(float lower, float upper); }; } #endif
InventoryManager/CSC2110/ReadFile.h
#if !defined READ_FILE_H #define READ_FILE_H #include "Text.h" #include <fstream> using namespace std; namespace CSC2110 { class ReadFile { private: ifstream* input_file; bool _eof; bool closed; public: ReadFile(const char* file_name); ~ReadFile(); CSC2110::String* readLine(); bool eof(); void close(); }; } #endif
InventoryManager/CSC2110/Song.h
#if !defined SONG_H #define SONG_H #include "Text.h" using CSC2110::String; namespace CSC2110 { class Song { private: String* title; String* length; public: Song(String* title, String* length); virtual ~Song(); void displaySong(); }; } #endif
InventoryManager/CSC2110/Text.h
#if !defined TEXT_H #define TEXT_H namespace CSC2110 { class String { private: const char* text; int sz; //length of string not including null terminator public: String(const char* char_array); virtual ~String(); void displayString(); int length(); const char* getText(); //add this member function char charAt(int index); int a_to_i(); float a_to_f(); static String* i_to_a(int number); static String* f_to_a(float number); //find the location of a particular character in a String and return the index if found //preconditions: // str is the String being examined for the character delimiter (str must point to a valid String) // delimiter is the character being searched for // start is the index to start the search at (the first index of the String is 0, start cannot exceed the length of the String) //postconditions: // if the preconditions are met, the index of the first delimiter encountered at or after the start index is returned // if the delimiter is not present in the String at index start or later, -1 is returned // if the preconditions are not met, no guarantees on output are made int find(char delimiter, int start); //creates a new String that is extracted from an existing String with characters specified by the start and end indices //preconditions: // str is the String from which the substring will be extracted (str must point to a valid String) // start and end are the indices used to create the substring // start must be less than or equal to end, start must be >= 0, end must be >= 0, end < the length of the String //postconditions: // if the preconditions are met, the String extracted from the parameter String // that starts at index start and ends at index end is created and returned // the original string is unaffected String* substr(int start, int end); //need to document that this compare only has three possible return values (-1, 0, 1) int compare(String* other); }; } #endif
InventoryManager/CSC2110/Tokens.h
#if !defined TOKENS_H #define TOKENS_H #include "Text.h" using CSC2110::String; namespace CSC2110 { class Tokens { private: String** tokens; int max_tokens; int sz; void addToken(String* str); //requires a resizing check void resize(); public: Tokens(String* str, char delimiter); ~Tokens(); //Tokens is not responsible for deleting each token void displayTokens(); String* getToken(int index); //returns a specifically requested token int getNumTokens(); }; } #endif
InventoryManager/CSC2110/WriteFile.h
#if !defined WRITE_FILE #define WRITE_FILE #include "Text.h" using CSC2110::String; #include <fstream> using namespace std; namespace CSC2110 { class WriteFile { private: ofstream* output_file; bool closed; public: WriteFile(const char* file_name); ~WriteFile(); void writeLine(String* line); void close(); }; } #endif
InventoryManager/DequeArray.h
// // DequeArray.h // InventoryManager // // #if !defined NULL #define NULL 0 #endif #if !defined (DequeArray_H) #define DequeArray_H template < class T > class DequeArray { private: int max_queue; T** items; int front; int back; int sz; void arrayResize(int new_size); public: DequeArray(); ~DequeArray(); bool isEmpty(); int size(); void dequeueAll(); T* peek(); T* peekDeque(); void enqueue(T* item); void enqueueReverse(T* item); T* dequeue(); T* dequeueReverse(); }; template < class T > DequeArray<T>::DequeArray() { max_queue = 2; items = new T*[max_queue]; front = 0; back = max_queue - 1; sz = 0; } template < class T > DequeArray<T>::~DequeArray() { delete[] items; } template < class T > bool DequeArray<T>::isEmpty() { return sz == 0; } template < class T > int DequeArray<T>::size() { return sz; } template < class T > T* DequeArray<T>::peek() { T* item = NULL; if (!isEmpty()) { item = items[front]; } return item; } template < class T > T* DequeArray<T>::peekDeque() { T* item = NULL; if (!isEmpty()) { item = items[front]; T* tmp = dequeue(); } return item; } template < class T > void DequeArray<T>::enqueue(T* item) { if (sz == max_queue) { arrayResize(2*max_queue); } //back = (back + 1) % (max_queue); back = back + 1; if (back == max_queue) back = 0; items[back] = item; sz++; } template < class T > void DequeArray<T>::enqueueReverse(T* item) { if (sz == max_queue) { arrayResize(2*max_queue); } //back = (back + 1) % (max_queue); front = front - 1; if (front == -1) front = max_queue-1; items[front] = item; sz++; } template < class T > T* DequeArray<T>::dequeue() { T* item = NULL; if (!isEmpty()) { item = items[front]; items[front] = NULL; //front = (front + 1) % (max_queue); front = front + 1; if (front == max_queue) front = 0; sz--; } return item; } template < class T > T* DequeArray<T>::dequeueReverse() { T* item = NULL; if (!isEmpty()) { item = items[back]; items[back] = NULL; //front = (front + 1) % (max_queue); back = back - 1; if (back == -1) front = max_queue-1; sz--; } return item; } template < class T > void DequeArray<T>::arrayResize(int new_size) { T** temp = new T*[new_size]; int j = front; for (int i = 0; i < sz; i++) { temp[i] = items[j]; j++; if (j == max_queue) j = 0; } front = 0; back = sz - 1; max_queue = new_size; delete[] items; items = temp; } template < class T > void DequeArray<T>::dequeueAll() { delete[] items; max_queue = 2; items = new T*[max_queue]; front = 0; back = max_queue - 1; sz = 0; } #endif
InventoryManager/InventoryDriver.cpp
InventoryManager/InventoryDriver.cpp
//
// main.cpp
// InventoryManager
//
#include
"InventoryManager.h"
#include
"Text.h"
using
CSC2110
::
String
;
#include
"Keyboard.h"
using
CSC2110
::
Keyboard
;
#include
<
iostream
>
#include
<
iomanip
>
using
namespace
std
;
int
inventoryChoice
()
{
Keyboard
*
kb
=
Keyboard
::
getKeyboard
();
int
inv_choice
=
kb
->
getValidatedInt
(
"Are you using (1) LIFO or (2) FIFO inventory management? "
,
1
,
2
);
return
inv_choice
;
}
//DO THIS
//buy Widgets (check invalid input and reprompt if necessary)
void
buyWidgets
(
InventoryManager
*
im
)
{
int
num_to_buy
=
kb
->
getValidatedInt
(
"How many widgets would you like to buy?"
,
1
,
10000
);
double
cost
=
kb
->
readDouble
(
"What is the cot for each widget?"
);
im
->
buyWidgets
(
cost
,
num_to_buy
);
}
//DO THIS
//sell Widgets and return the profit (check invalid input and reprompt if necessary)
double
sellWidgets
(
InventoryManager
*
im
)
{
int
num_to_sell
=
kb
->
getValidatedInt
(
"How many widgets would you like to sell?"
,
1
,
10000
);
double
price
=
kb
->
readDouble
(
"What is the price for each widget?"
);
double
profit
=
im
->
sellWidgets
(
price
,
num_to_sell
);
cout
<<
"Your total profit for this transaction is $"
<<
profit
<<
endl
;
return
profit
;
}
bool
mainMenu
(
InventoryManager
*
im
)
{
Keyboard
*
kb
=
Keyboard
::
getKeyboard
();
int
menu_choice
=
kb
->
getValidatedInt
(
"1. Buy Widgets \r\n2. Sell Widgets\r\n3. Quit\r\nWhat would you like to do? "
,
1
,
3
);
double
profit
=
0
;
if
(
menu_choice
==
1
)
{
buyWidgets
(
im
);
return
1
;
}
else
if
(
menu_choice
==
2
)
{
sellWidgets
(
im
);
return
1
;
}
else
{
return
0
;
}
}
int
main
()
{
cout
<<
setprecision
(
2
)
<<
fixed
;
int
inv_choice
=
inventoryChoice
();
InventoryManager
*
im
=
new
InventoryManager
(
inv_choice
);
bool
ask
=
1
;
while
(
ask
)
{
ask
=
mainMenu
(
im
);
}
double
running_total
=
im
->
getTotalProfit
();
cout
<<
"Your total profit for all transactions is $"
<<
running_total
<<
endl
;
delete
im
;
}
InventoryManager/InventoryManager.cpp
InventoryManager/InventoryManager.cpp
//
// InventoryManager.cpp
// InventoryManager
//
//
#include
"InventoryManager.h"
InventoryManager
::
InventoryManager
(
int
inventory_choice
)
//LIFO or FIFO
{
if
(
inventory_choice
==
1
)
stack
=
new
StackDeque
<
Widget
>
();
else
queue
=
new
QueueDeque
<
Widget
>
();
totalProfit
=
0
;
this
->
inventory_choice
=
inventory_choice
;
}
InventoryManager
::~
InventoryManager
()
{
}
void
InventoryManager
::
buyWidgets
(
double
cost
,
int
num_to_buy
)
{
for
(
int
i
=
0
;
i
<
num_to_buy
;
i
++
)
{
Widget
*
w
=
new
Widget
(
cost
);
if
(
inventory_choice
==
1
)
stack
->
enqueue
(
w
);
else
queue
->
enqueue
(
w
);
}
}
double
InventoryManager
::
getTotalProfit
()
{
return
totalProfit
;
}
double
InventoryManager
::
sellWidgets
(
double
price
,
int
num_to_sell
)
{
double
profit
=
0
;
if
(
stack
->
size
()
>=
num_to_sell
)
{
for
(
int
i
=
0
;
i
<
num_to_sell
;
i
++
)
{
if
(
inventory_choice
==
1
)
{
Widget
*
w
=
stack
->
dequeue
();
profit
=
profit
+
price
-
w
->
getCost
();
}
else
{
Widget
*
w
=
queue
->
dequeue
();
profit
=
profit
+
price
-
w
->
getCost
();
}
};
}
totalProfit
=
totalProfit
+
profit
;
return
profit
;
}
InventoryManager/InventoryManager.h
// // InventoryManager.h // InventoryManager // #ifndef __InventoryManager__InventoryManager__ #define __InventoryManager__InventoryManager__ #include "StackDeque.h" #include "QueueDeque.h" #include "Widget.h" class InventoryManager { StackDeque<Widget>* stack; QueueDeque<Widget>* queue; double totalProfit; int inventory_choice; public: InventoryManager(int inventory_choice); //LIFO or FIFO ~InventoryManager(); void buyWidgets(double cost, int num_to_buy); double getTotalProfit(); double sellWidgets(double price, int num_to_sell); }; #endif /* defined(__InventoryManager__InventoryManager__) */
InventoryManager/QueueDeque.h
// // QueueDeque.h // InventoryManager // // #ifndef InventoryManager_QueueDeque_h #define InventoryManager_QueueDeque_h #include "DequeArray.h" template <class T> class QueueDeque { private: DequeArray<T> *queue; public: QueueDeque(); ~QueueDeque(); bool isEmpty(); int size(); void dequeueAll(); T* peek(); T* peekDeque(); void enqueue(T* item); T* dequeue(); }; template <class T> QueueDeque<T>::QueueDeque() { queue = new DequeArray<T>(); } template <class T> QueueDeque<T>::~QueueDeque() { queue->dequeueAll(); } template <class T> bool QueueDeque<T>::isEmpty() { return queue->isEmpty(); } template <class T> int QueueDeque<T>::size() { return queue->size(); } template <class T> void QueueDeque<T>::dequeueAll() { queue->dequeueAll(); } template <class T> T* QueueDeque<T>::peek() { queue->peek(); } template <class T> T* QueueDeque<T>::peekDeque() { queue->peekDeque(); } template <class T> void QueueDeque<T>::enqueue(T* item) { queue->enqueue(item); } template <class T> T* QueueDeque<T>::dequeue() { return queue->dequeue(); } #endif
InventoryManager/StackDeque.h
// // StackDeque.h // InventoryManager // // #ifndef InventoryManager_StackDeque_h #define InventoryManager_StackDeque_h #include "DequeArray.h" template <class T> class StackDeque { private: DequeArray<T> *queue; public: StackDeque(); ~StackDeque(); bool isEmpty(); int size(); void dequeueAll(); T* peek(); T* peekDeque(); void enqueue(T* item); T* dequeue(); }; template <class T> StackDeque<T>::StackDeque() { queue = new DequeArray<T>(); } template <class T> StackDeque<T>::~StackDeque() { queue->dequeueAll(); } template <class T> bool StackDeque<T>::isEmpty() { return queue->isEmpty(); } template <class T> int StackDeque<T>::size() { return queue->size(); } template <class T> void StackDeque<T>::dequeueAll() { queue->dequeueAll(); } template <class T> T* StackDeque<T>::peek() { queue->peek(); } template <class T> T* StackDeque<T>::peekDeque() { queue->peekDeque(); } template <class T> void StackDeque<T>::enqueue(T* item) { queue->enqueue(item); } template <class T> T* StackDeque<T>::dequeue() { return queue->dequeueReverse(); } #endif
InventoryManager/Widget.cpp
InventoryManager/Widget.cpp
//
// Widget.cpp
// InventoryManager
//
#include
"Widget.h"
Widget
::
Widget
(
double
price
)
{
cost
=
price
;
}
double
Widget
::
getCost
()
{
return
cost
;
}
InventoryManager/Widget.h
// // Widget.h // InventoryManager // // #ifndef __InventoryManager__Widget__ #define __InventoryManager__Widget__ class Widget { private: double cost; public: Widget(double price); double getCost(); }; #endif /* defined(__InventoryManager__Widget__) */