mercredi 18 août 2021

c++ object member function to sort a list alphabetically

I have a shopping list in a c++ class that has the item name, price, and expiration date all combined into one object. How can I sort the list alphabetically in a member function?

This is my item class and isLessThan is the sort function

#include "item.h"

InventoryItem::InventoryItem()
{
        itemName = nullptr;
        itemPrice = 0;
        expDate = nullptr;
}

InventoryItem::InventoryItem(const InventoryItem& anItem):InventoryItem()
{
        *this = anItem;
}

InventoryItem::InventoryItem(InventoryItem&& anItem):InventoryItem()
{
        *this = move(anItem);
}

InventoryItem::~InventoryItem()
{
        if(this->itemName)
        {
                delete [] this->itemName;
                this->itemName = nullptr;
        }
        if(this->expDate)
        {
                delete this->expDate;
                this->expDate = nullptr;
        }
}


const InventoryItem& InventoryItem::operator=(const InventoryItem& anItem)
{
        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& InventoryItem::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)
{
        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;
}

bool InventoryItem::isLessThan(const InventoryItem& anItem){
        anItem.getItemName();

        return 0;
}

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'

        //populate anItem with the temporary variables
        anItem.setItemName(itemName);
        anItem.setItemPrice(itemPrice);
        expDate.setDate(year, month, day);
        anItem.setExpDate(expDate);
        return in;
}
#include "itemList.h"
ItemList::ItemList()
{
        size = 0;
        capacity = INIT_CAPACITY;
        list = new InventoryItem[capacity];
}

ItemList::ItemList(const ItemList& aList)
{
        size = aList.size;
        capacity = aList.capacity;
        list = new InventoryItem[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.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 InventoryItem[capacity];
        int index;
        for(index = 0; index < size; index++)
        {
                list[index] = aList.list[index];
        }
        return *this;
}

int ItemList::getSize() const
{
        return size;
}

InventoryItem& 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 InventoryItem[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 >> day;
                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, ':');
        }
}

bool ItemList::addItem(const InventoryItem& anItem){

        append(anItem);
        return true;
}

void ItemList::printList() const
{
        int index;
        for(index = 0; index < size; index++)
        {
                list[index].print();
        }
}

This is for school and I've been looking everywhere for an answer to this and I just can't find it. I can't use any STL headers

Aucun commentaire:

Enregistrer un commentaire