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

C++ Online shopping cart (continued), using the my previous code below, could us

ID: 3724295 • Letter: C

Question

C++ Online shopping cart (continued), using the my previous code below,
could use help for this next continued part of the program...

Instructions: https://ibb.co/h3nx8S

--My Code--

//ItemToPurchase.h

#ifndef ITEMTOPURCHASE_H
#define ITEMTOPURCHASE_H
#include<string>

using namespace std;

class ItemToPurchase
{
public:
ItemToPurchase();
//Corrected the name of getters and setters
//setItemName was before , now changed to SetName
//as given direction
//using same convention for all setters and getters
void SetName(string name);
void SetPrice(int itemPrice);
void SetQuantity(int itemQuantity);

string GetName();
int GetPrice();
int GetQuantity();

virtual ~ItemToPurchase();

protected:
private:
string itemName;
int itemPrice;
int itemQuantity;
};

#endif // ITEMTOPURCHASE_H

//ItempToPurchase.cpp

#include "ItemToPurchase.h"

ItemToPurchase::ItemToPurchase()
{
//ctor
this->itemName = "none";
this->itemPrice = 0;
this->itemQuantity = 0;
}
//same as in the .h file
//corrected the name of getters and setters
void ItemToPurchase::SetName(string name)
{
this->itemName = name;
}

void ItemToPurchase::SetPrice(int itemPrice)
{
this->itemPrice = itemPrice;
}

void ItemToPurchase::SetQuantity(int itemQuantity)
{
this->itemQuantity = itemQuantity;
}

string ItemToPurchase::GetName()
{
return itemName;
}

int ItemToPurchase::GetPrice()
{
return itemPrice;
}

int ItemToPurchase::GetQuantity()
{
return itemQuantity;
}

ItemToPurchase::~ItemToPurchase()
{
//dtor
}

//main.cpp

#include<iostream>
#include "ItemToPurchase.h"
using namespace std;

int main()
{
ItemToPurchase item1, item2;

string itemName;
int itemPrice;
int itemQuantity;
int totalCost = 0;

cout << "Item 1" << endl;
cout << "Enter the item name:" << endl;
getline(cin, itemName);
//cin.ignore();

cout << "Enter the item price:" << endl;
cin >> itemPrice;

cout << "Enter the item quantity:" << endl;
cin >> itemQuantity;

item1.SetName(itemName);
item1.SetPrice(itemPrice);
item1.SetQuantity(itemQuantity);
cin.ignore();

cout << " Item 2" << endl;
cout << "Enter the item name:" << endl;
getline(cin, itemName);
//cin.ignore();

cout << "Enter the item price:" << endl;
cin >> itemPrice;

cout << "Enter the item quantity:" << endl;
cin >> itemQuantity;
//Corrrected the name of getters and setters
item2.SetName(itemName);
item2.SetPrice(itemPrice);
item2.SetQuantity(itemQuantity);

cout << " TOTAL COST" << endl;
cout << item1.GetName() << " " << item1.GetQuantity() << " @ $"
<< item1.GetPrice() << " = $" << item1.GetQuantity() * item1.GetPrice() << endl;
cout << item2.GetName() << " " << item2.GetQuantity() << " @ $"
<< item2.GetPrice() << " = $" << item2.GetQuantity() * item2.GetPrice() << endl;

totalCost = (item1.GetQuantity() * item1.GetPrice()) +
(item2.GetQuantity() * item2.GetPrice());

cout << " Total: $" << totalCost<<endl;

return 0;
}

Explanation / Answer

Item.hpp

#ifndef Item_hpp
#define Item_hpp

#include <iostream>
using namespace std;

class Item {

private:
string name;
double price;
int quantity;

public:
void setName(string);
void setPrice(double);
void setQuantity(int);
string getName();
double getPrice();
int getQuantity();

// Constructors
Item();

Item(string, double, int);

};

#endif

Item.cpp

#include "Item.hpp"
#include <iostream>
using namespace std;

Item::Item() {
name = "";
price = 0.0;
quantity = 0;
}

Item::Item(string itemName, double itemPrice, int itemQuantity) {
setName(itemName);
setPrice(itemPrice);
setQuantity(itemQuantity);
}

void Item::setName(string itemName) {
name = itemName;
}

void Item::setPrice(double itemPrice) {
price = itemPrice;
}

void Item::setQuantity(int itemQuantity) {
quantity = itemQuantity;
}

string Item::getName() {
return name;
}

double Item::getPrice() {
return price;
}

int Item::getQuantity() {
return quantity;
}

ShoppingCart.hpp

#ifndef ShoppingCart_hpp
#define ShoppingCart_hpp

#include "Item.hpp"
#include <iostream>
using namespace std;

class ShoppingCart {

private:
Item* itemArray[100];
int arrayEnd;

public:
void addItem(Item *);
double totalPrice();

// Constructors
ShoppingCart();

};
#endif

ShoppingCart.cpp

#include "ShoppingCart.hpp"
#include "Item.hpp"
#include <iostream>
using namespace std;

ShoppingCart::ShoppingCart() {
for (int i=0; i<100; i++) {
itemArray[i] = {NULL};
}
arrayEnd = 0;
}

void ShoppingCart::addItem(Item *currentItem) {
itemArray[arrayEnd] = currentItem;
arrayEnd++;
}

double ShoppingCart::totalPrice() {
double totalPrice;

for (int i=0; i<100; i++) {
if (itemArray[i] != NULL) {
totalPrice += itemArray[i]->getPrice() * itemArray[i]->getQuantity();
} else {
break;
}
}

return totalPrice;
}

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