Question
Im working on a lab for an introductory computer prorgramming class. I have most of the code written, but I'm having trouble getting functions in
I"m working on a lab for an introductory computer prorgramming class. I have most of the code written, but I'm having trouble getting functions in the main lab to connect to the ShoppingCart class.
Here's the instructions and my work will follow.
Background
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:
Bottled Water 10 @ $1.50 = $15.00
Example output of the function which prints the item description:
Bottled Water: Deer Park, 12 oz.
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:
John Doe's Shopping Cart - February 1, 2016 Number of Items: 8 Nike Romaleos 2 @ $189.00 = $378.00 Chocolate Chips 5 @ $3.50 = $17.50 Powerbeats 2 Headphones 1 @ $128.00 = $128.00 Total: $523.50
Example output of the function which prints the item descriptions:
John Doe's Shopping Cart - February 1, 2016 Item Descriptions Nike Romaleos: Volt color, Weightlifting shoes Chocolate Chips: Semi-sweet Powerbeats 2 Headphones: Bluetooth headphones
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.
add - Add item to cart remove - Remove item from cart change - Change item quantity descriptions - Output items' descriptions cart - Output shopping cart options - Print the options menu quit - Quit
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)
- Remember to review the section on Objects and Classes.
- You now have multiple files, and each file should have it's own header.
- 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
Mary Jane Lewis September 1, 2017 options descriptions cart add BYU game shirt Perfect shirt to wear with the ROC 5.00 2 add Sunscreen 50 SPF Perfect to avoid the fourth quarter burn 4.50 3 add Sunscreen 50 SPF Perfect to avoid the fourth quarter burn 4.50 1 descriptions cart change buffalo wings 4 change BYU game shirt 4 cart remove Sunscreen 50 SPF descriptions cart quit
Sample Output - Visual Studio View
Enter Customer's Name: Mary Jane Lewis Enter Today's Date: September 1, 2017 Enter option: options MENU add - Add item to cart remove - Remove item from cart change - Change item quantity descriptions - Output items' descriptions cart - Output shopping cart options - Print the options menu quit - Quit Enter option: descriptions Mary Jane Lewis's Shopping Cart - September 1, 2017 Shopping cart is empty. Enter option: cart Mary Jane Lewis's Shopping Cart - September 1, 2017 Shopping cart is empty. Enter option: add Enter the item name: BYU game shirt Enter the item description: Perfect shirt to wear with the ROC Enter the item price: 5.00 Enter the item quantity: 2 Enter option: add Enter the item name: Sunscreen 50 SPF Enter the item description: Perfect to avoid the fourth quarter burn Enter the item price: 4.50 Enter the item quantity: 3 Enter option: add Enter the item name: Sunscreen 50 SPF Enter the item description: Perfect to avoid the fourth quarter burn Enter the item price: 4.50 Enter the item quantity: 1 Item is already in cart. Nothing added. Enter option: descriptions Mary Jane Lewis's Shopping Cart - September 1, 2017 Item Descriptions BYU game shirt: Perfect shirt to wear with the ROC Sunscreen 50 SPF: Perfect to avoid the fourth quarter burn Enter option: cart Mary Jane Lewis's Shopping Cart - September 1, 2017 Number of Items: 5 BYU game shirt 2 @ $5.00 = $10.00 Sunscreen 50 SPF 3 @ $4.50 = $13.50 Total: $23.50 Enter option: change Enter the item name: buffalo wings Enter the new quantity: 4 Item not found in cart. Nothing modified. Enter option: change Enter the item name: BYU game shirt Enter the new quantity: 4 Enter option: cart Mary Jane Lewis's Shopping Cart - September 1, 2017 Number of Items: 7 BYU game shirt 4 @ $5.00 = $20.00 Sunscreen 50 SPF 3 @ $4.50 = $13.50 Total: $33.50 Enter option: remove Enter name of the item to remove: Sunscreen 50 SPF Enter option: descriptions Mary Jane Lewis's Shopping Cart - September 1, 2017 Item Descriptions BYU game shirt: Perfect shirt to wear with the ROC Enter option: cart Mary Jane Lewis's Shopping Cart - September 1, 2017 Number of Items: 4 BYU game shirt 4 @ $5.00 = $20.00 Total: $20.00 Enter option: quit Goodbye.
My Lab
ItemToPurchase.h
#ifndef ITEMTOPURCHASE_H
#define ITEMTOPURCHASE_H
#include
using namespace std;
class ItemToPurchase {
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;
private:
string itemName = "none";
string itemDescription = "none";
double itemPrice = 0.0;
int itemQuantity = 0;
};
#endif
ItemToPurchase.cpp
#include
#include
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
#include
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 ReturnItemQuantity() const;
double ReturnTotalCost() const;
void PrintTotalCost() const;
void PrintItemDescriptions() const;
private:
string customerName = "none";
string currentDate = "January 1, 2016";
vector
};
#endif
ShoppingCart.cpp
#include
#include
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 = 0;
int i = 0;
bool itemUpdated = false;
cout << "Enter the new quantity: ";
cin >> newQuantity;
for (i = 0; i < cartItems.size(); ++i) {
if (cartItems.at(i).GetName() == item.GetName()) {
cartItems.at(i).SetDescription(item.GetDescription());
cartItems.at(i).SetPrice(item.GetPrice());
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
#include
#include
#include
using namespace std;
#include "ItemToPurchase.h"
#include "ShoppingCart.h"
void AddItem() {
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);
AddObjectItem(itemToAdd);
}
void removeItem() {
string itemToRemove;
cout << "Enter name of item to remove: ";
getline(cin, itemToRemove);
ShoppingCart cartItems.RemoveItemObject(itemToRemove);
}
void changeItem() {
string itemToChange;
cout << "Enter the item name: ";
getline(cin, itemToChange);
ShoppingCart cartItems.UpdateItemObject(itemToChange);
}
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);
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") {
AddItem();
cout << endl;
}
else if (userOption == "remove") {
}
else if (userOption == "change") {
}
}
system("pause");
return 0;
}
What can I do to fix this lab so that it runs correctly?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started