Homework Help Question & Answers

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
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 Coin

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
  • Ch 7 Program: Online shopping cart (continued) (Java) This is for zybook and its not working...

    Ch 7 Program: Online shopping cart (continued) (Java) This is for zybook and its not working I need Help Please. Can you follow the instructions please 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 fields string 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)...

  • ---------------main.cpp #include <queue> #include <iostream> using namespace std; int main (int argc, char* argv[]) { string...

    ---------------main.cpp #include <queue> #include <iostream> using namespace std; int main (int argc, char* argv[]) { string personName = ""; int counter = 0; int youPosition; queue<string> peopleInQueue; getline(cin, personName); while (personName != "-1") { // TODO: Add personName to peopleInQueue // determine position of "You" (youPosition) getline(cin, personName); } cout << "Welcome to the ticketing service... " << endl; cout << "You are number " << youPosition << " in the queue." << endl; // TODO: In a loop, remove...

  • 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...

  • 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,...

  • ----------------main.cpp #include<iostream> #include <fstream> #include <vector> #include <string> #include "StatePair.h" using namespace std; int main() {...

    ----------------main.cpp #include<iostream> #include <fstream> #include <vector> #include <string> #include "StatePair.h" using namespace std; int main() {    ifstream inFS; // File input stream    int zip;    int population;    string abbrev;    string state;    unsigned int i;    // ZIP code - state abbrev. pairs    vector<StatePair <int, string>> zipCodeState;    // state abbrev. - state name pairs    vector<StatePair<string, string>> abbrevState;    // state name - population pairs    vector<StatePair<string, int>> statePopulation;    // Fill the three...

  • #include <iostream> #include <iomanip> #include <vector> #include <string> using namespace std; struct menuItemType { string menuItem;...

    #include <iostream> #include <iomanip> #include <vector> #include <string> using namespace std; struct menuItemType { string menuItem; double menuPrice; }; void getData(menuItemType menuList[]); void showMenu(menuItemType menuList[], int x); void printCheck(menuItemType menuList[], int menuOrder[], int x); int main() { const int menuItems = 8; menuItemType menuList[menuItems]; int menuOrder[menuItems] = {0}; int orderChoice = 0; bool ordering = true; int count = 0; getData(menuList); showMenu(menuList, menuItems); while(ordering) { cout << "Enter the number for the item you would\n" << "like to order, or...

  • #include<iostream> #include<string> #include<fstream> using namespace std; string mergeOneLine(int id,string last_name, string first_name, string phone_number, float quizzes[14],...

    #include<iostream> #include<string> #include<fstream> using namespace std; string mergeOneLine(int id,string last_name, string first_name, string phone_number, float quizzes[14], float homeworks[10], float labs[7], float tests[3], float project, float discussion, float teamWork, float extra, float sumScore = 0.0, float percentage = 0.0, char letterGrade = 'X') { //Merges all assignments and information into one string and returns it string oneLine; oneLine += to_string(id) + "," + last_name + "," + first_name + "," + phone_number + "-"; for (int i = 0; i <...

  • -------------mian.cpp #include <queue> #include <iostream> using namespace std; int main (int argc, char* argv[]) { string...

    -------------mian.cpp #include <queue> #include <iostream> using namespace std; int main (int argc, char* argv[]) { string personName = ""; int counter = 0; int youPosition; queue<string> peopleInQueue; getline(cin, personName); while (personName != "-1") { // TODO: Add personName to peopleInQueue // determine position of "You" (youPosition) getline(cin, personName); } cout << "Welcome to the ticketing service... " << endl; cout << "You are number " << youPosition << " in the queue." << endl; // TODO: In a loop, remove...

  • #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure...

    #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure char start_state, to_state; char symbol_read; }; void read_DFA(struct transition *t, char *f, int &final_states, int &transitions){ int i, j, count = 0; ifstream dfa_file; string line; stringstream ss; dfa_file.open("dfa.txt"); getline(dfa_file, line); // reading final states for(i = 0; i < line.length(); i++){ if(line[i] >= '0' && line[i] <= '9') f[count++] = line[i]; } final_states = count; // total number of final states // reading...

Free Homework 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.