Question

CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

CODES:

main.cpp

#include <iostream>

#include <string>

#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 <iostream>

#include <string>

#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 <string>

#include <vector>

#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 <iostream>

#include <string>

#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 <iostream>

#include <string>

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

code:

ShoppingCart.cpp
------------------------------------------------------------------
#include"ShoppingCart.h"
#include<stdbool.h>
#include<iostream>

string ShoppingCart::GetCustomerName()
{
   return this->customerName;
}

string ShoppingCart::GetDate()
{
   return this->currentDate;
}

void ShoppingCart::AddItem(ItemToPurchase item)
{
   this->cartItems.push_back(item);
}

void ShoppingCart::RemoveItem(string itemName)
{
   bool found = false;
   for (int i = 0; i < this->cartItems.size(); i++)
   {
       if (this->cartItems.at(i).name == itemName)
       {
           found = true;
           this->cartItems.erase(cartItems.begin() + i);
       }
   }
   if (!found)
   {
       cout << endl << "Item not found in cart. Nothing removed." << endl << endl;
   }
}

void ShoppingCart::UpdateQuantity(string itemName, int newQuantity)
{
   bool found = false;
   for (int i = 0; i < this->cartItems.size(); i++)
   {
       if (this->cartItems.at(i).name == itemName)
       {
           found = true;
           this->cartItems.at(i).quantity = newQuantity;
       }
   }
   if (!found)
   {
       cout << endl << "Item not found in cart. Nothing modified." << endl << endl;
   }
}

int ShoppingCart::GetNumItemsInCart()
{
   return this->cartItems.size();
}

int ShoppingCart::GetCostOfCart()
{
   int totalCost = 0;
   if (this->cartItems.size() == 0)
   {
       return 0;
   }
   for (int i = 0; i < this->cartItems.size(); i++)
   {
       totalCost += this->cartItems.at(i).price * this->cartItems.at(i).quantity;
   }
   return totalCost;
}

void ShoppingCart::PrintHeader()
{
   cout << this->customerName << "'s Shopping Cart - " << this->currentDate << endl << endl;
}

void ShoppingCart::PrintTotal()
{
   if (this->cartItems.size() == 0)
   {
       cout << "SHPPING CART IS EMPTY" << endl;
   }
   else
   {
       this->PrintHeader();
       cout << "Number of Items: " << this->cartItems.size() << endl << endl;
       for (int i = 0; i < this->cartItems.size(); i++)
       {
           this->cartItems.at(i).PrintItemCost();
           cout << endl;
       }
       cout << endl << "Total: $" << this->GetCostOfCart() << endl << endl;
   }
}

void ShoppingCart::PrintDescriptions()
{
   this->PrintHeader();
   cout << endl;
   cout << "Item Descriptions" << endl << endl;
   for (int i = 0; i < this->cartItems.size(); i++)
   {
       this->cartItems.at(i).PrintItemDescription();
       cout << endl;
   }
}
-------------------------------------------------------------------------------------
ShoppingCart.h
-------------------------------------------
#pragma once
#include<string>
#include<vector>
#include"ItemToPurchase.h"
using namespace std;
class ShoppingCart {
   string customerName;
   string currentDate;
   vector<ItemToPurchase> cartItems;
   public:
       ShoppingCart()
       {
           this->customerName = "none";
           this->currentDate = "January 1, 2016";
       }
       ShoppingCart(string customerName, string currentDate)
       {
           this->customerName = customerName;
           this->currentDate = currentDate;
       }
       string GetCustomerName();
       string GetDate();
       void AddItem(ItemToPurchase item);
       void RemoveItem(string itemName);
       void UpdateQuantity(string itemName, int newQuantity);
       int GetNumItemsInCart();
       int GetCostOfCart();
       void PrintTotal();
       void PrintDescriptions();
   private:
       void PrintHeader();
};

Add a comment
Know the answer?
Add Answer to:
CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with this assignment, can someone HELP ? This is the assignment: Online shopping...

    I need help with this assignment, can someone HELP ? This is the assignment: Online shopping cart (continued) (C++) This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class per the following specifications: Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt) Public member functions SetDescription() mutator & GetDescription() accessor (2 pts) PrintItemCost() - Outputs the item name followed by...

  • This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).

    Ch 7 Program: Online shopping cart (continued) (Java)This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).(1) Extend the ItemToPurchase class per the following specifications:Private fieldsstring itemDescription - Initialized in default constructor to "none"Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt)Public member methodssetDescription() mutator & getDescription() accessor (2 pts)printItemCost() - Outputs the item name followed by the quantity, price, and subtotalprintItemDescription() - Outputs the...

  • 4.18 Ch 7 Program: Online shopping cart (continued) (C++) This program extends the earlier "Online shopping...

    4.18 Ch 7 Program: Online shopping cart (continued) (C++) This program extends the earlier "Online shopping cart" program. (solution from previous lab assignment is provided in Canvas). (1) Extend the ItemToPurchase class per the following specifications: Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt) Public member functions SetDescription() mutator & GetDescription() accessor (2 pts) PrintItemCost() - Outputs the item name followed by the quantity, price, and subtotal PrintItemDescription() -...

  • #include <iostream> #include <string> #include "hashT.h" #include "stateData.h" using namespace std; void stateData::setStateInfo(string sName, string sCapital,...

    #include <iostream> #include <string> #include "hashT.h" #include "stateData.h" using namespace std; void stateData::setStateInfo(string sName, string sCapital,    double stArea, int yAdm, int oAdm) {    stateName = sName; stateCapital = sCapital; stArea = stateArea; yAdm = yearOfAdmission; oAdm = orderOfAdmission;       } void stateData::getStateInfo(string& sName, string& sCapital,    double& stArea, int& yAdm, int& oAdm) {    sName = stateName; sCapital = stateCapital; stArea = stateArea; yAdm = yearOfAdmission; oAdm = orderOfAdmission;       } string stateData::getStateName() { return stateName;...

  • #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car {...

    #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car { private: string reportingMark; int carNumber; string kind; bool loaded; string choice; string destination; public: Car() { reportingMark = ""; carNumber = 0; kind = "Others"; loaded = 0; destination = "NONE"; } ~Car() { } void setUpCar(string &reportingMark, int &carNumber, string &kind, bool &loaded, string &destination); }; void input(string &reportingMark, int &carNumber, string &kind, bool &loaded,string choice, string &destination); void output(string &reportingMark, int &carNumber,...

  • Need three seperate files. ShoppingCartManager.java ShoppingCart.java ItemsToPurchase.java These are from part 1 what do you mean...

    Need three seperate files. ShoppingCartManager.java ShoppingCart.java ItemsToPurchase.java These are from part 1 what do you mean by deep study 7.25 LAB*: Program: Online shopping cart (Part 2) Note: Creating multiple Scanner objects for the same input stream yields unexpected behavior. Thus, good practice is to use a single Scanner object for reading input from System.in. That Scanner object can be passed as an argument to any methods that read input This program extends the earlier Online shopping cart program (Consider...

  • please help me fix the error in here #include<iostream> #include <string> using namespace std; string getStudentName();...

    please help me fix the error in here #include<iostream> #include <string> using namespace std; string getStudentName(); double getNumberExams(); double getScoresAndCalculateTotal(double E); double calculateAverage(double n, double t); char determineLetterGrade(); void displayAverageGrade(); int main() { string StudentName; double NumberExam, Average, ScoresAndCalculateTotal; char LetterGrade; StudentName = getStudentName(); NumberExam = getNumberExams(); ScoresAndCalculateTotal= getScoresAndCalculateTotal(NumberExam); Average = calculateAverage(NumberExam, ScoresAndCalculateTotal); return 0; } string getStudentName() { string StudentName; cout << "\n\nEnter Student Name:"; getline(cin, StudentName); return StudentName; } double getNumberExams() { double NumberExam; cout << "\n\n Enter...

  • Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In...

    Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and cannot figure it out. Thank you. Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and...

  • In C++ ***//Cat.h//*** #ifndef __Cat_h__ #define __Cat_h__ #include <string> using namespace std; struct Cat { double...

    In C++ ***//Cat.h//*** #ifndef __Cat_h__ #define __Cat_h__ #include <string> using namespace std; struct Cat { double length; double height; double tailLength; string eyeColour; string furClassification; //long, medium, short, none string furColours[5]; }; void initCat (Cat&, double, double, double, string, string, const string[]); void readCat (Cat&); void printCat (const Cat&); bool isCalico (const Cat&); bool isTaller (const Cat&, const Cat&); #endif ***//Cat.cpp//*** #include "Cat.h" #include <iostream> using namespace std; void initCat (Cat& cat, double l, double h, double tL, string eC,...

  • Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void...

    Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void messageAndKey(){ string msg; cout << "Enter message: "; getline(cin, msg); cin.ignore(); //message to uppercase for(int i = 0; i < msg.length(); i++){ msg[i] = toupper(msg[i]); } string key; cout << "Enter key: "; getline(cin, key); cin.ignore(); //key to uppercase for(int i = 0; i < key.length(); i++){ key[i] = toupper(key[i]); } //mapping key to message string keyMap = ""; for (int i = 0,j...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT