Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Somebody posted this before but the answer did not really do much. I will just r

ID: 3663675 • Letter: S

Question

Somebody posted this before but the answer did not really do much. I will just repost the question here as I am working on something similar. I need to to overload the == operator in a program to make sure the user doesn't add the same thing twice. The answer that was given did not really prevent that form happening.

Here was the question...

C++. I need to overload the ==operator in my shoppingcart program so when a user adds an item it checks the list of items already added (in an array) to see if it is in there so duplicates can't be added. Here is code so far....

List.hpp

#include "Item.hpp"

class List{
private:
   Item **arr;
   int count;
   int capacity;
   void doubleSize();
   void removeItem(int ind);
public:
   List();
   void addItem();
   void removeItem();
   void display();
};

List.cpp

#include "List.hpp"

List::List(){
   count = 0;
   capacity = 10;
   arr = new Item*[10];
}

void List::addItem(){
   string name, unit;
   int num;
   double price;
   cout << "Enter Item Name: ";
   cin >> name;
   cout << "Enter unit(can, box, pounds, ounces): ";
   cin >> unit;
   cout << "How many " << name << "s do you want to buy: ";
   cin >> num;
   cout << "Unit price: ";
   cin >> price;
   if (count == capacity)
       doubleSize();
   arr[count++] = new Item(name, unit, num, price);
}

void List::doubleSize(){
   capacity *= 2;
   Item **newarr = new Item*[capacity];
   for (int i = 0; i < count; ++i){
       newarr[i] = arr[i];
   }
   arr = newarr;
}

void List::removeItem(int ind){
   for (int i = ind; i < count - 1; ++i){
       arr[i] = arr[i + 1];
   }
   --count;
}

void List::removeItem(){
   cout << "Enter name of the item to remove: ";
   string name;
   cin >> name;
   for (int i = 0; i < count; ++i){
       if (arr[i]->getName() == name){
           removeItem(i);
       }
   }
}

void List::display(){
   double sum = 0;
   for (int i = 0; i < count; ++i){
       cout << "Name: " << arr[i]->getName() << endl;
       cout << "Unit: " << arr[i]->getUnit() << endl;
       cout << "Number: " << arr[i]->getNumbers() << endl;
       cout << "Unit Price: " << arr[i]->getUnitPrice() << endl;
       cout << "Extended Price: " << arr[i]->getUnitPrice() * arr[i]->getNumbers() << endl << endl;
       sum += (arr[i]->getUnitPrice() * arr[i]->getNumbers());
   }
   cout << "The total bill is: " << sum << endl << endl;
}

Item.hpp

#include <iostream>
#include <string>

using namespace std;

class Item{
private:
   string name;
   string unit;
   int num;
   double unit_price;
public:
   Item();
   Item(string name, string unit, int num, double price);
   void setname(string name);
   void setUnit(string unit);
   void setNumbers(int num);
   void setUnitPrice(double price);
   string getName();
   string getUnit();
   int getNumbers();
   double getUnitPrice();
};

Item.cpp

#include <iostream>
#include <string>
#include "Item.hpp"

using namespace std;


Item::Item(){
       name = "";
       unit = "";
       num = 0;
       unit_price = 0;
   }
Item::Item(string name, string unit, int num, double price){
       this->name = name;
       this->unit = unit;
       this->num = num;
       this->unit_price = price;
   }
void Item::setname(string name){
       this->name = name;
   }
void Item::setUnit(string unit){
       this->unit = unit;
   }
void Item::setNumbers(int num){
       this->num = num;
   }
void Item::setUnitPrice(double price){
       this->unit_price = price;
   }
string Item::getName(){
       return name;
   }
string Item::getUnit(){
       return unit;
   }
int Item::getNumbers(){
       return num;
   }
double Item::getUnitPrice(){
       return unit_price;
   }


main.cpp

#include "List.hpp"

int main(){
   int option;
   List list;
   while (true){
       cout << "Enter 1 to add an item" << endl;
       cout << "Enter 2 to remove an item" << endl;
       cout << "Enter 3 to display" << endl;
       cout << "Enter 4 to exit" << endl;
       cout << "Choose an option: " << endl;
       cin >> option;
       switch (option){
       case 1:
           list.addItem();
           break;
       case 2:
           list.removeItem();
           break;
       case 3:
           list.display();
           break;
       case 4:
           return 0;
       default:
           cout << "That was not a valid option" << endl;
       return 0;
       }
   }
   return 0;
}

Explanation / Answer

All the code will remain same...only the change will be in List:addItem() method.. we need to avoid duplicatio of item..and for this purpose we need to check for both the name and type of unit it is..

The modifocation can be as follows:

void List::addItem(){
   string name, unit;
   int num;
   double price;
   cout << "Enter Item Name: ";
   cin >> name;
   cout << "Enter unit(can, box, pounds, ounces): ";
   cin >> unit;
   cout << "How many " << name << "s do you want to buy: ";
   cin >> num;
   cout << "Unit price: ";
   cin >> price;

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

{

if(name==arr[i]->getName())

{

if(unit == arr[i]->getUnit())

{

cout<<"you have enterd the item which is already present in cart..please renenter another one"<<endl;

}

}

else   
   arr[count++] = new Item(name, unit, num, price);
} // end of for

if (count == capacity)
       doubleSize();

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote