C++ question, plese do not use any other language. main.cpp #include <iostream>
ID: 3881426 • Letter: C
Question
C++ question, plese do not use any other language.
main.cpp
#include <iostream>
#include <iomanip>
using namespace std;
#include "ShoppingCart.h"
using namespace std;
char PrintMenu(ShoppingCart& theCart){
char menuOp = ' ';
string name;
string descr;
int price = 0;
int quantity = 0;
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 (menuOp != 'a' && menuOp != 'd' && menuOp != 'c' &&
menuOp != 'i' && menuOp != 'o' && menuOp != 'q') {
cout << "Choose an option:" << endl;
cin >> menuOp;
}
switch(menuOp) {
case 'a':
{
cin.ignore();
cout << "ADD ITEM TO CART" << endl;
cout << "Enter the item name:" << endl;
getline(cin, name);
cout << "Enter the item description:" << endl;
getline(cin, descr);
cout << "Enter the item price:" << endl;
cin >> price;
cout << "Enter the item quantity:" << endl;
cin >> quantity;
ItemToPurchase newItem(name, descr, price, quantity);
theCart.AddItem(newItem);
menuOp = ' ';
cout << endl;
}
break;
case 'd':
cin.ignore();
cout << "REMOVE ITEM FROM CART" << endl;
cout << "Enter name of item to remove:" << endl;
getline(cin, name);
theCart.RemoveItem(name);
menuOp = ' ';
cout << endl;
break;
case 'c':
{
cin.ignore();
cout << "CHANGE ITEM QUANTITY" << endl;
cout << "Enter the item name:" << endl;
getline(cin, name);
cout << "Enter the new quantity:" << endl;
cin >> quantity;
ItemToPurchase item;
item.SetName(name);
item.SetQuantity(quantity);
theCart.ModifyItem(item);
menuOp = ' ';
cout << endl;
}
break;
case 'i':
cout << "OUTPUT ITEMS' DESCRIPTIONS" << endl;
theCart.PrintDescriptions();
menuOp = ' ';
cout << endl;
break;
case 'o':
cout << "OUTPUT SHOPPING CART" << endl;
theCart.PrintTotal();
menuOp = ' ';
cout << endl;
break;
}
return menuOp;
}
int main() {
string custName;
string today;
char menuChoice = ' ';
cout << "Enter customer's name:" << endl;
getline(cin, custName);
cout << "Enter today's date:" << endl;
getline(cin, today);
cout << endl;
cout << "Customer name: " << custName << endl;
cout << "Today's date: " << today << endl << endl;
ShoppingCart myCart(custName, today);
while(menuChoice != 'q') {
menuChoice = PrintMenu(myCart);
}
return 0;
}
ItemToPurchase.h
#ifndef ITEM_TO_PURCHASE_H
#define ITEM_TO_PURCHASE_H
#include <string>
using namespace std;
class ItemToPurchase {
public:
ItemToPurchase();
void SetName(string name);
void SetPrice(int price);
void SetQuantity(int quantity);
string GetName() const;
int GetPrice() const;
int GetQuantity() const;
void Print();
private:
string itemName;
int itemPrice;
int itemQuantity;
};
#endif
ItemToPurchase.cpp
#include <iostream>
using namespace std;
#include "ItemToPurchase.h"
ItemToPurchase::ItemToPurchase() {
itemName = "none";
itemPrice = 0;
itemQuantity = 0;
return;
}
void ItemToPurchase::SetName(string name) {
itemName = name;
return;
}
void ItemToPurchase::SetPrice(int price) {
itemPrice = price;
return;
}
void ItemToPurchase::SetQuantity(int quantity) {
itemQuantity = quantity;
return;
}
string ItemToPurchase::GetName() const {
return itemName;
}
int ItemToPurchase::GetPrice() const {
return itemPrice;
}
int ItemToPurchase::GetQuantity() const {
return itemQuantity;
}
void ItemToPurchase::Print() {
cout << itemName << " " << itemQuantity << " @ $"
<< itemPrice << " = $" << itemPrice * itemQuantity << endl;
}
need to code ShoppingCart.h and ShoppingCart.cpp
Explanation / Answer
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();
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.