Question: CODES: main.cpp #include #include #include ShoppingCart.h using namespace std; char PrintMenu() { char answer; cout < < MENU < < endl; cout < < a

CODES:

main.cpp

#include

#include

#include "ShoppingCart.h"

using namespace std;

char PrintMenu()

{

char answer;

cout << "MENU" << endl;

cout << "a - Add item to cart" << endl;

cout << "d - Remove item from cart" << endl;

cout << "c - Change item quantity" << endl;

cout << "i - Output items' descriptions" << endl;

cout << "o - Output shopping cart" << endl;

cout << "q - Quit" << endl

<< endl;

while (true)

{

cout << "Choose an option:" << endl;

cin >> answer;

if (answer == 'a' || answer == 'A')

break;

if (answer == 'd' || answer == 'D')

break;

if (answer == 'c' || answer == 'C')

break;

if (answer == 'i' || answer == 'I')

break;

if (answer == 'o' || answer == 'O')

break;

if (answer == 'q' || answer == 'Q')

break;

}

return answer;

}

ItemToPurchase AddItem()

{

string itemName = "";

string itemDescription;

int itemQuantity;

int itemPrice;

cout << "ADD ITEM TO CART" << endl;

cout << "Enter the item name:" << endl;

cin.ignore();

getline(cin, itemName);

cout << "Enter the item description:" << endl;

cin.ignore();

getline(cin, itemDescription);

cout << "Enter the item price:" << endl;

cin >> itemPrice;

cout << "Enter the item quantity:" << endl

<< endl;

cin >> itemQuantity;

ItemToPurchase itemToAdd(itemName, itemDescription, itemPrice, itemQuantity);

return itemToAdd;

}

int main()

{

string itemToRemove;

string customerName;

string currentDate;

cout << "Enter customer's name:" << endl;

getline(cin, customerName);

cout << "Enter today's date:" << endl

<< endl;

getline(cin, currentDate);

cout << "Customer name: " << customerName << endl;

cout << "Today's date: " << currentDate << endl

<< endl;

ShoppingCart itemCart(customerName, currentDate);

char answer = PrintMenu();

while (answer != 'q')

{

if (answer == 'o' || answer == 'O')

{

cout << "OUTPUT SHOPPING CART" << endl;

itemCart.PrintTotal();

}

if (answer == 'a' || answer == 'A')

{

itemCart.AddItem(AddItem());

}

if (answer == 'i' || answer == 'I')

{

cout << "OUTPUT ITEMS' DESCRIPTIONS" << endl;

itemCart.PrintDescriptions();

}

if (answer == 'd' || answer == 'D')

{

cout << "REMOVE ITEM FROM CART" << endl

<< "Enter name of item to remove:" << endl;

cin.ignore();

getline(cin, itemToRemove);

}

if (answer == 'c' || answer == 'C')

break;

if (answer == 'q' || answer == 'Q')

break;

answer = PrintMenu();

}

}

ShoppingCart.cpp

#include

#include

#include "ShoppingCart.h"

using namespace std;

unsigned int i;

// parametrized constructor

ShoppingCart::ShoppingCart(string cName, string cDate){

this->currentDate = "January 1, 2016";

this->currentDate = cDate;

this->customerName = "none";

this->customerName = cName;

}

// function to get name

string ShoppingCart::GetCustomerName(){

return customerName;

}

// function to get date

string ShoppingCart::GetDate( ){

return currentDate;

}

// function to add item

void ShoppingCart::AddItem(ItemToPurchase item){

cartItems.push_back(item);

}

// function to remove item

void ShoppingCart::RemoveItem(string item){

unsigned int oldsize = cartItems.size();

for (i = 0; i < cartItems.size(); i++){

if (cartItems.at(i).GetName() == item)

cartItems.erase(cartItems.begin()+i);

}

if(oldsize == cartItems.size())

cout << "item not found in cart. Nothing removed." << endl;

}

// function to modify item

void ShoppingCart::ModifyItem(ItemToPurchase item){

bool bItemsModified = false;

for (i = 0; i < cartItems.size(); i++){

if (cartItems.at(i).GetName() == item.GetName()){

cartItems.at(i).SetPrice(item.GetPrice());

cartItems.at(i).SetQuantity(item.GetQuantity());

cartItems.at(i).SetDescription(item.GetDescription());

bItemsModified = true;

}

}

if (bItemsModified == false)

cout << " Item not found in cart. Nothing modified.";

}

// function to get number of items

int ShoppingCart::GetNumItemsInCart(){

return this->cartItems.size();

}

// function to get cost

int ShoppingCart::GetCostOfCart() {

int total = 0;

for (i = 0; i < cartItems.size(); i++){

total = total + (cartItems.at(i).GetPrice()*cartItems.at(i).GetQuantity());

}

return total;

}

// function to print total

void ShoppingCart::PrintTotal(){

int total = 0;

cout << customerName << "'s Shopping Cart - " << currentDate << endl;

if(cartItems.size() == 0){

cout << "Number of Items: 0" << endl << endl;

cout << "SHOPPING CART IS EMPTY" << endl << endl;

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

}

else{

cout << "Number of Items: " << this->cartItems.size() + 1 << endl << endl;

for(i = 0; i < this->cartItems.size(); i++){

this->cartItems.at(i).PrintItemCost();

total = total + (this->cartItems.at(i).GetPrice()*this->cartItems.at(i).GetQuantity());

}

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

}

}

void ShoppingCart::PrintDescriptions(){

cout << customerName << "'s Shopping Cart - " << currentDate << endl;

cout << "Item Descriptions" << endl;

for(i = 0; i < cartItems.size(); i++){

cartItems.at(i). PrintItemDescription();

}

}

ShoppingCart.h

#ifndef SHOPPINGCART

#define SHOPPINGCART

#include

#include

#include "ItemToPurchase.h"

using namespace std;

class ShoppingCart {

public:

ShoppingCart(string cName = "none", string cDate="none");

string GetCustomerName();

string GetDate();

void AddItem(ItemToPurchase);

void RemoveItem(string itemNames);

void ModifyItem(ItemToPurchase);

int GetNumItemsInCart();

int GetCostOfCart();

void PrintTotal();

void PrintDescriptions();

private:

string customerName;

string currentDate;

vector < ItemToPurchase > cartItems;

};

#endif

ItemToPurchase.cpp

#include

#include

#include "ItemToPurchase.h"

using namespace std;

void ItemToPurchase::SetName(string itemNam) {

itemName = itemNam;

}

void ItemToPurchase::SetPrice(int itemPric) {

itemPrice = itemPric;

}

void ItemToPurchase::SetQuantity(int itemQuantit) {

itemQuantity = itemQuantit;

}

void ItemToPurchase::SetDescription(string itemDescriptio) {

itemDescription = itemDescriptio;

}

string ItemToPurchase::GetName() const {

return itemName;

}

int ItemToPurchase::GetPrice() const {

return itemPrice;}

int ItemToPurchase::GetQuantity() const {

return itemQuantity;}

string ItemToPurchase::GetDescription() const {

return itemDescription;}

ItemToPurchase::ItemToPurchase(string name, string description, int price, int quantity) {

itemName=name;

itemPrice=price;

itemQuantity=quantity;

itemDescription=description;}

ItemToPurchase::ItemToPurchase() {

itemName="";

itemPrice=0;

itemQuantity=0;

itemDescription="";}

void ItemToPurchase::PrintItemCost() {

cout << itemName << " " << itemQuantity << " @ $" << itemPrice << " = $" << itemQuantity * itemPrice << endl;

return;}

void ItemToPurchase::PrintItemDescription() {

cout << itemName << ": " << itemDescription << endl;

return;}

ItemToPurchase.h

#ifndef ITEMTOPURCHASE

#define ITEMTOPURCHASE

#include

#include

using namespace std;

class ItemToPurchase {

public:

ItemToPurchase();

ItemToPurchase(string, string, int, int); // constructor

void SetName(string);

void SetPrice(int);

void SetQuantity(int);

void SetDescription(string);

void PrintItemCost();

void PrintItemDescription();

string GetName() const;

int GetPrice() const;

int GetQuantity() const;

string GetDescription() const;

private:

string itemName;

int itemPrice;

int itemQuantity;

string itemDescription;

};

#endif

MY PROBLEM:

Tests default constructor and accessors (ShoppingCart)

Test feedback

Shopping Cart improperly initialized with default constructor. GetCustomerName() returns none GetDate() returns none

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!