Background C++, pay attention to samples This main lab extends the earlier prep
ID: 3726537 • Letter: B
Question
Background C++, pay attention to samples
This main lab extends the earlier prep lab "Online shopping cart Part 1". (You should save this as a separate project from the earlier prep lab). You will create an on-line shopping cart like you might use for your on-line purchases. The goal is to become comfortable with setting up classes and using objects. This is your first main lab with objects so review the Objects and Classes section of the style guide.
Requirements
This lab can be done individually or as pair programming.
Expanded ItemToPurchase Class (15 points)
Extend the ItemToPurchase class as follows. We will not do unit testing in this lab so we will not be giving you the names of the member functions. Create good ones on your own.
Create a parameterized constructor to assign item name, item description, item price, and item quantity (default values of "none" for name and description, and 0 for price and quantity).
Additional public member functions
Set an item description
Get an item description
Print the cost of an item - Outputs the item name followed by the quantity, price, and subtotal (see example)
Print the description of an item - Outputs the item name and description (see example)
Additional Private data members
a string for the description of the item.
Example output of the function which prints the cost of an item:
Example output of the function which prints the item description:
ShoppingCart Class (25 points)
Create three new files - Name all your files exactly as specified as that is required for zyBook file submission.
ShoppingCart.h - Class declaration
ShoppingCart.cpp - Class definition
main.cpp - main function (main's functionality will differ significantly from that of the prep lab)
Build the ShoppingCart class with the following specifications. Note: Some functions can be function stubs (empty functions) initially, to be completed in later steps. Also, we usually declare all variable at the top of the code block. Note that you could be declaring some class objects after receiving user input, so that is a case where it would be all right to declare a variable later in your code.
Parameterized constructor which takes the customer name and date as parameters
Private data members
Name of the customer - Initialized in constructor to "none" if no parameter included
Date the cart was created - Initialized in constructor to "January 1, 2016" if no parameter included
Vector of items objects
Must have the following public member functions
Get the name of the customer
Get the date the cart was created
Add an item object to the cart vector. Accepts a new item object as a parameter.
If item name is already in the cart, output this message: "Item is already in cart. Nothing added."
Remove an item object from the cart. Accepts an item name as a parameter.
If item name cannot be found, output this message: "Item not found in cart. Nothing removed."
Update the quantity of an item object in the cart
If item name cannot be found, output this message: "Item not found in cart. Nothing modified." If the quantity is set to 0, still leave the item in the cart, but treat it as a 0 in all counts, etc.
Return the quantity of items in the cart
Return the total cost of all items in the cart
Print the total number and cost of items in the cart
If cart is empty, output this message: "Shopping cart is empty"
Print the description of each item in the cart
If cart is empty, output this message: "Shopping cart is empty"
Example output of the function which prints the number and costs of items in the cart:
Example output of the function which prints the item descriptions:
You may also create private helper functions in the ShoppingCart class if you'd like. Hint: A function to find if and where an item name resides in the cart could be helpful in multiple places.
Main Function Options Menu (8 points)
See the full input/output example below to know the expected format. In main, first prompt the user for a customer's name and today's date. Create an object of type ShoppingCart. Note that you have to declare (and thus create) your cart after you have input the name and date, since you have to set those with the parameterized constructor since you do not have setter functions for name and date.
In main, create a loop and allow the user to enter options to manipulate the cart. First implement the options and quit options. If the user enters an incorrect option, just print the options menu.
In this lab you may assume that when you prompt the user for an input, that they will input an appropriate value (e.g. an int would not be negative, or a string, etc.). Thus, you are not required to do error checking (you may if you want but we will not test for it). Note that in any "real-world" program, you would ALWAYS check for all possible input errors. However, we have done that sufficiently in previous labs for you to understand the concept, so in this lab you may assume legal inputs and focus on other programming aspects. The one exception is if the user enters an unknown option you will print the options menu. Note that you will still have a mix of >> and getline, so make sure you deal with that properly (See section 7.2 if necessary).
Implement the remaining user options (52 points). Think about what parameters need to be passed to class member functions, etc.
cart: Output shopping cart. Note that to do this, all you should need to do is call the appropriate member function on the shopping cart object. Note that by implementing printing options first, it is easy to test and verify later options as you implement them.
descriptions: Output description of item's in the cart.
add: Add an item to the cart.
change: Update the quantity of a current item in the cart to a new value.
remove: Remove an item from the cart.
Deductions graded by TA's
Adherence to the style guidelines. (up to 20 points)
Always create your own default constructor for each class, and create a parameterized constructor(s) as required. You may combine these into one constructor function by creating a default constructor with default parameters. Hint: One way to test this is to declare a default and a fully parameterized object at the top of your code and make sure that works. Then delete them after testing. (5 points each).
Each class must be created and submitted in separate cpp and header files. (15 points)
Note that you can have all your files in a folder and then drag and drop them ALL AT ONCE into the zyBook submit file area, and zyBook will sort each file into its proper place.
You must solve this problem using classes and members as specified. You must not submit a solution that solves the lab, while not using classes. (60 points)
Sample Input
Sample Output - Visual Studio View
Explanation / Answer
ItemToPurchase.h
#ifndef ITEMTOPURCHASE_H
#define ITEMTOPURCHASE_H
#include <string>
using namespace std;
class ItemToPurchase {
private:
string itemName = "none";
string itemDescription = "none";
double itemPrice = 0.0;
int itemQuantity = 0;
public:
ItemToPurchase(string, string, double, int);
void SetName(string itemName);
string GetName() const;
void SetDescription(string itemDescription);
string GetDescription() const;
void SetPrice(double itemPrice);
double GetPrice() const;
void SetQuantity(int itemQuantity);
int GetQuantity() const;
void PrintCost() const;
void PrintDescription() const;
};
#endif
ItemToPurchase.cpp
#include <iostream>
#include <string>
using namespace std;
#include "ItemToPurchase.h"
ItemToPurchase::ItemToPurchase(string name, string description, double price, int quantity) {
SetName(name);
SetDescription(description);
SetPrice(price);
SetQuantity(quantity);
}
void ItemToPurchase::SetName(string itemName) {
this-> itemName = itemName;
return;
}
string ItemToPurchase::GetName() const {
return itemName;
}
void ItemToPurchase::SetDescription(string itemDescription) {
this-> itemDescription = itemDescription;
return;
}
string ItemToPurchase::GetDescription() const {
return itemDescription;
}
void ItemToPurchase::SetPrice(double itemPrice) {
this-> itemPrice = itemPrice;
return;
}
double ItemToPurchase::GetPrice() const {
return itemPrice;
}
void ItemToPurchase::SetQuantity(int itemQuantity) {
this-> itemQuantity = itemQuantity;
return;
}
int ItemToPurchase::GetQuantity() const {
return itemQuantity;
}
void ItemToPurchase::PrintCost() const {
cout << GetName() << " " << GetQuantity() << " @ $" << GetPrice() << " = $" << GetQuantity() * GetPrice() << endl;
}
void ItemToPurchase::PrintDescription() const {
cout << GetName() << ": " << GetDescription() << endl;
}
ShoppingCart.h
#ifndef SHOPPINGCART_H
#define SHOPPINGCART_H
#include <string>
#include <vector>
using namespace std;
#include "ItemToPurchase.h"
class ShoppingCart {
public:
ShoppingCart(string customerName, string customerDate);
ShoppingCart(string);
void SetCustomerName(string customerName);
string GetCustomerName() const;
void SetCustomerDate(string customerDate);
string GetCustomerDate() const;
void AddItemObject(ItemToPurchase) ;
void RemoveItemObject(string item);
void UpdateItemQuantity(string item,int newQuantity);
int ReturnItemQuantity() const;
double ReturnTotalCost() const;
void PrintTotalCost() const;
void PrintItemDescriptions() const;
private:
string customerName = "none";
string currentDate = "January 1, 2016";
vector<ItemToPurchase> cartItems;
};
#endif
ShoppingCart.cpp
#include <iostream>
#include <string>
using namespace std;
#include "ItemToPurchase.h"
#include "ShoppingCart.h"
ShoppingCart::ShoppingCart(string customerName, string customerDate) {
this-> customerName = customerName;
this-> currentDate = customerDate;
}
ShoppingCart::ShoppingCart(string item) {
item;
}
void ShoppingCart::SetCustomerName(string customerName) {
this->customerName = customerName;
}
string ShoppingCart::GetCustomerName() const{
return customerName;
}
void ShoppingCart::SetCustomerDate(string customerDate) {
this->currentDate = customerDate;
}
string ShoppingCart::GetCustomerDate() const {
return currentDate;
}
void ShoppingCart::AddItemObject(ItemToPurchase item) {
int i = 0;
for (i = 0; i < cartItems.size(); ++i) {
if (cartItems.at(i).GetName() == item.GetName()) {
cout << "Item is already in cart. Nothing added." << endl << endl;
}
else {
cartItems.push_back(item);
}
}
}
void ShoppingCart::RemoveItemObject(string item) {
int oldSize = 0;
int i = 0;
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;
}
}
void ShoppingCart::UpdateItemQuantity(string item,int newQuantity) {
int i = 0;
bool itemUpdated = false;
for (i = 0; i < cartItems.size(); ++i) {
if (cartItems.at(i).GetName() == item) {
cartItems.at(i).SetQuantity(newQuantity);
itemUpdated = true;
}
}
if (itemUpdated == false) {
cout << "Item not found in cart. Nothing modified.";
}
}
int ShoppingCart::ReturnItemQuantity() const {
int numberOfItems = 0;
numberOfItems = cartItems.size();
return numberOfItems;
}
double ShoppingCart::ReturnTotalCost() const {
int i = 0;
double totalCost = 0.0;
for (i = 0; i < cartItems.size(); ++i) {
totalCost += cartItems.at(i).GetPrice() * cartItems.at(i).GetQuantity();
}
return totalCost;
}
void ShoppingCart::PrintTotalCost() const {
int i = 0;
double totalItemCost = 0;
double totalCartCost = 0;
cout << customerName << "'s Shopping Cart - " << currentDate << endl;
cout << "Number of Items: " << cartItems.size() << endl << endl;
for (i = 0; i < cartItems.size(); ++i) {
totalItemCost = cartItems.at(i).GetQuantity() * cartItems.at(i).GetPrice();
cout << cartItems.at(i).GetName() << " " << cartItems.at(i).GetQuantity() << " @ $" << cartItems.at(i).GetPrice() << " = $" << totalItemCost << endl;
totalCartCost += totalItemCost;
}
cout << endl << "Total: $" << totalCartCost;
}
void ShoppingCart::PrintItemDescriptions() const {
int i = 0;
cout << customerName << "'s Shopping Cart - " << currentDate << endl;
cout << "Item Descriptions" << endl;
for (i = 0; i < cartItems.size(); ++i) {
cout << cartItems.at(i).GetName() << ": " << cartItems.at(i).GetDescription() << endl;
}
}
main.cpp
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;
#include "ItemToPurchase.h"
#include "ShoppingCart.h"
int main() {
string userName;
string todaysDate;
string userOption;
cout << "Enter Customer's Name: ";
getline(cin, userName);
cout << endl << "Enter Today's Date: ";
getline(cin, todaysDate);
cout << endl << endl;
ShoppingCart itemCart(userName, todaysDate);
//ShoppingCart cartItems;
while (userOption != "quit") {
cout << "Enter option: ";
cin >> userOption;
cout << endl;
if (userOption != "add" && userOption != "remove" && userOption != "change" && userOption != "descriptions" && userOption != "cart") {
cout << "MENU" << endl;
cout << "add - Add item to cart" << endl;
cout << "remove - Remove item from cart" << endl;
cout << "change - Change item quantity" << endl;
cout << "descriptions - Output items' descriptions" << endl;
cout << "cart - Output shopping cart" << endl;
cout << "options - Print the options menu" << endl;
cout << "quit - Quit" << endl << endl;
}
else if (userOption == "add") {
string itemName;
string itemDescription;
double itemPrice;
int itemQuantity;
cout << "Enter the item name: ";
cin.ignore();
getline(cin, itemName);
cout << endl << "Enter the item description: ";
cin.ignore();
getline(cin, itemDescription);
cout << endl << "Enter the item price: ";
cin >> itemPrice;
cout << endl << "Enter the item quantity: ";
cin >> itemQuantity;
ItemToPurchase itemToAdd(itemName, itemDescription, itemPrice, itemQuantity);
itemCart.AddItemObject(itemToAdd);
cout << endl;
}
else if (userOption == "remove") {
string itemToRemove;
cout << "Enter name of item to remove: ";
getline(cin, itemToRemove);
itemCart.RemoveItemObject(itemToRemove);
}
else if (userOption == "change") {
string itemToChange;
int newQuantity;
cout << "Enter the item name: ";
getline(cin, itemToChange);
cout << "Enter the new quantity: ";
cin>> newQuantity;
itemCart.UpdateItemQuantity(itemToChange,newQuantity);
}
else if (userOption == "cart") {
itemCart.PrintTotalCost();
cout<<"Total Cost:"<<itemCart.ReturnTotalCost();
}
else if (userOption == "descriptions") {
itemCart.PrintItemDescriptions();
}
}
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.