You can modify the provided source files, header files
and makefile as needed. items.txt contains a set of sample data.
It’s used by the program to populate the inventory. You are not
allowed to use , or any other header files in STL.
You will need to complete the following
tasks.
Create isLessThan for class
InventoryItem. It should compare the invoking item’s
expiration date with a given date.
bool isLessThan (const Date& aDate);
First add the function prototype in
item.h, then put the function implementation in item.cpp
and finally invoke/test the function in main. Please label your
output clearly.
You need to read in the anItem
and aDate. When reading in from user, you
can read in individual attributes and invoking the set methods
of InventoryItem.
Hint: operator < is already defined for Date
ADT.
Create a public member function getItemPrices()
for class ItemList to return the item
prices in the list. The prices are returned through the dynamically
allocated array “itemPrices” and the function
returns the number of prices in the list.
You have to allocate just a big enough array to hold the
prices. For example, if there are 10 items in the list, you should
allocate an array of 10 prices. Each element in the array is a
floating point value for the item price. You could have duplicated
prices.
int getItemPrices(float * & itemPrices) const;
First add the member function prototype inside
class ItemList in itemList.h,
then put the member function implementation in
itemList.cpp and finally invoke/test the function in main.
Please label your output clearly.
Hint: remember to allocate the array before you try to
populate it and also remember to deallocate the array after you are
done using it. If you forget to allocate the array, your code will
crash. If you forget to deallocate the array, you will get memory
leaks. Many people hate C++ for these two frequently encountered
enemies, segmentation fault and memory leaks.
Use the provided makefile to compile your
code:
To compile: make
To run: main or ./main
To check memory leaks: valgrind –leak-check=full
./main
To clean up: make clean
To submit:
Copy the function(s) and the test code (how you invoke
the functions) you have written in solution.txt
Record 3 runs of your test: type “script output.txt” on
the command line and it will start recording your session in a file
called “output.txt.” The file should contain three runs of your
test. Type “exit” to stop recording.
You are required to use valgrind to check memory leaks
when recording the 3 runs.
makefile
main.cpp
itemList.cpp2
itemList.cpp3
item.txt
item.h
item.cpp
item.cpp2
item.cpp3
date.h
date.cpp
date.cpp2
CC = g++ CPPFLAGS -std=C++11 -g -Wall main: main.o date.o item.o itemlist.o main.o: main.h date.h item.h itemlist.h itemlist.o: date.h item.h itemList.h item.o: date.h item.h date.o: date.h clean: rm *.o main
#include "main.h" int main(int argc, char ** argv) { ItemList inventory; char fileName[] "items.txt"; //open file to read data and populate inventory ifstream in; in.open(fileName); if(!in) { << "Fail to open << fileName << " for input!" << endl; return 1; } inventory.readList(in); cer //invoke your functions here to do what is required << endl; cout << endl << "Current Inventory: inventory.printlist(); return 0; }
#pragma once #include <iostream> using namespace std; #include <cstring> #include <fstream> #include <iomanip> #include "date.h" #include "item.h" #include "itemList.h" int main(int args, char ** argv); ;
#pragma once #include "item.h" class ItemList { private: const static int INIT_CAPACITY = 1; //set low for test purpose const static int GROWTH_FACTOR = 2; Inventory Item * list; int size; int capacity; void expandArray(); public: ItemList(); ItemList(const Itemlist& alist); ItemList(Itemlist&& alist); ItemList(); const Itemlist& operator= (const Itemlist& alist); Itemlist& operator= (Itemlist&& alist); int getSize() const; Inventory Item& operator[] (int index); const InventoryItem& operator[] (int index) const; void append(const InventoryItem& anItem); void printList() const; void readList(istream& in); };
#include "itemList.h" ItemList::ItemList() { size = 0; capacity = INIT_CAPACITY; list = new Inventory Item[capacity]; } ItemList::ItemList(const Itemlist& alist) { size = alist.size; capacity = alist.capacity; list = new Inventory Item[capacity]; int index; for(index = 0; index < size; index++) { list[index] = alist.list[index]; } } ItemList:: ItemList(ItemList&& alist) { this->size = alist.size; this->capacity = alist.capacity; this->list = alist.list; alist.list = nullptr; alist.size = 0; alist.capacity = 0; } ItemList:: ItemList() { if(this->list) { delete [] this->list; this->list = nullptr; } } Itemlist& ItemList::operator= (Itemlist&& alist) { if (this == &alist) { return *this; } this->size - alist.size; this->capacity = alist.capacity; if (this->list) { delete [] this->list; } this->list = alist.list; alist.list = nullptr; alist.size = 0;
alist.size 0; alist.capacity = 0; return *this; = } == = const ItemList& ItemList::operator= (const ItemList& alist) { if(this &alist) { return *this; } size alist.size; capacity alist.capacity; if(list != nullptr) { delete [] list; } list = new Inventory Item[capacity]; int index; for(index = 0; index < size; index++) { list[index] = alist.list[index]; } return *this; } int ItemList::getSize() const { return size; } Inventory Item& ItemList::operator[] (int index) { return list[index]; } const InventoryItem& ItemList::operator[] (int index) const { return list[index]; } = void ItemList::expandArray ( ) { capacity = capacity * GROWTH_FACTOR; InventoryItem * tempList = new Inventory Item[capacity]; int index; for(index = 0; index < size; index++) { tempList[index] = list[index]; } delete [] list; list tempList; } =
== void ItemList:: append(const InventoryItem& anItem) { if(size capacity) { expandArray(); } list[size] = anItem; size++; } void ItemList:: readList(istream& in) { char itemName [MAX_CHAR]; float itemPrice; Date expDate; int year; int month; int day; InventoryItem anItem; in.get(itemName, MAX_CHAR, ':'); while (!in.eof()) { in.get(); in >> itemPrice; in.get(); in >> year; in.get(); in >> month; in.get(); ; in.ignore (MAX_CHAR, '\n'); anItem.setItemName(itemName); anItem.setItemPrice(itemPrice); expDate.setDate(year, month, day); anItem.setExpDate (expDate); append(anItem); in.get(itemName, MAX_CHAR, ':'); } } void ItemList::printList() const { int index; for(index = 0; index < size; index++) { list[index].print(); } }
apple:0.99:2022/3/20 banana:0.69:2021/4/28 cookie: 0.50:2023/2/12 donut:1.00:2021/10/28 egg: 3.88:2022/1/30 fish:5.88:2021/2/15 milk:2.99:2021/3/2 yogurt: 6.38:2021/6/13
#pragma once #include <iostream> using namespace std; #include <cstring> #include "date.h" const int MAX_CHAR = 101; class InventoryItem { private: char itemName; float itemPrice; Date expDate; public: Inventory Item(); InventoryItem(const Inventory Item& anItem); InventoryItem(InventoryItem&& anItem); InventoryItem(); const InventoryItem& operator= (const InventoryItem& anItem); InventoryItem& operator= (InventoryItem&& anItem); void setItemName(const char name[]); const char* getItemName() const; void setItemPrice(float price); float getItemPrice() const; void setExpDate(const Date& aDate); const Date * getExpDate() const; void print() const; };
#include "item.h" InventoryItem:: InventoryItem() { itemName = nullptr; itemPrice = 0; expDate = nullptr; } Inventory Item:: InventoryItem(const InventoryItem& anItem): InventoryItem() { *this = an Item; } Inventory Item:: Inventory Item(Inventory Item&& anItem): Inventory Item() { *this - move (anItem); } Inventory Item::~Inventory Item() { if(this->itemName) { delete [] this->itemName; this->itemName = nullptr;/ } if(this->expDate) { delete this->expDate; this->expDate = nullptr; } } == const InventoryItem& InventoryItem::operator=(const Inventory Item& an Item) { if(this &anItem) { return *this; } if(itemName) { delete [] itemName; } itemName = new char[strlen(anItem.itemName ) +1]; strcpy(itemName, anItem.itemName); itemPrice = anItem.itemPrice; if(expDate) { delete expDate; } expDate new Date(*(anItem.expDate)); =
return *this; } InventoryItem& Inventory Item::operator= (InventoryItem&& anItem) { if(this == &anItem) { return "this; } if(itemName) { delete [] itemName; } itemName = anItem. itemName; anItem.itemName = nullptr; itemPrice = anItem. itemPrice; anItem.itemPrice = 0; if(expDate) { delete expDate; } expDate = anItem.expDate; anItem.expDate = nullptr; return *this; } void InventoryItem:: setItemName(const char name[]) { if(this->itemName) delete [] this->itemName; this->itemName = new char[strlen(name) +1]; strcpy(this->itemName, name); } const char* InventoryItem::getItemName() const { return itemName; } void InventoryItem:: setItemPrice(float price) { itemPrice = price; } float InventoryItem::getItemPrice) const { return itemPrice; } void InventoryItem::setExpDate(const Date& aDate) { 16/+hic Saunatal
void InventoryItem::setExpDate(const Date& aDate) { if(this->expDate) delete expDate; this->expDate = new Date(aDate); } const Date * InventoryItem::getExpDate() const { return this->expDate; } void InventoryItem:: print() const { cout << fixed; cout.precision(2); cout << itemName << "\t" «< itemPrice << "\t" << *expDate << endl; } istream& operator>> (istream& in, InventoryItem& anItem) { //assuming data input format "itemName: itemPrice:year/month/day" char itemName [MAX_CHAR]; float itemPrice; Date expDate; int year; int month; int day; //read value in temporary variables in.get(itemName, MAX_CHAR, ':'); in.get(); //throw away in >> itemPrice; in.get(); //throw away in >> year; in.get(); //throw away '/' in >> month; in.get(); //throw away '/' in >> day; in.ignore (MAX_CHAR, '\n'); //throw away '\n' 1/populate anItem with the temporary variables anItem.setItemName (itemName); anItem.setItemPrice(itemPrice); expDate.setDate(year, month, day); anItem.setExpDate(expDate); return in; }
#pragma once #include <ctime> #include <iostream> using namespace std; class Date { private: time_t rawTime; public: Date(); Date(int year, int month, int day); int getYear() const; int getMonth() const; int getDay() const; ostream& print(ostream& out) const; bool operator< (const Date& right) const; bool setDate(int year, int month, int day); }; ostream& operator<< (ostream& out, const Date& aDate);
#include "date.h" Date::Date() { tm * timeInfo; time(&rawTime); time Info = localtime(&rawTime); timeInfo->tm_sec = 0; timeInfo->tm_min = 0; timeInfo->tm_hour = 0; rawTime = mktime(timeInfo); } Date::Date(int year, int month, int day):Date::Date() { setDate(year, month, day); } int Date::getYear() const { tm * timeInfo; timeInfo = localtime(&rawTime); return timeInfo->tm_year + 1900; } int Date::getMonth() const { tm* timeInfo; time Info = localtime(&rawTime); return timeInfo->tm_mon + 1; } tm * int Date::getDay() const { timeInfo; time Info - localtime(&rawTime); return timeInfo->tm_mday; } ostream& Date: :print(ostream& out) const { const int MAX_CHAR = 80; char dateString[MAX_CHAR]; tm * timeInfo; timeInfo = localtime(&rawTime); strftime (dateString, MAX_CHAR, "%a %F", timeInfo); out << dateString; return out; } bool Date::setDate(int year, int month, int day)
bool Date::setDate(int year, int month, int day) { tm * timeInfo; time_t tempTime; = = tempTime = time(&tempTime); timeInfo = localtime(&tempTime); timeInfo->tm_year = year - 1900; timeInfo->tm_mon = month - 1; timeInfo->tm_mday = day; time Info->tm_sec = 0; timeInfo->tm_min = 0; timeInfo->tm_hour = 0; tempTime = mktime(timeInfo); if(tempTime < 0) return false; else { rawTime = tempTime; return true; } } ostream& operator<< (ostream& out, const Date& aDate) { return aDate.print(out); } bool Date:: operators (const Date& right) const { return difftime(right.rawTime, this->rawTime) > 0; }
You can modify the provided source files, header files and makefile as needed. items.txt contains a set of sample data.
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am