Question

In: Computer Science

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

7.7 Ch 7 Program: Online shopping cart (continued) (C)

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

(1) Extend the ItemToPurchase struct to contain a new data member. (2 pt)

  • char itemDescription[ ] - set to "none" in MakeItemBlank()

Implement the following related functions for the ItemToPurchase struct.

  • PrintItemDescription()
    • Has an ItemToPurchase parameter.


Ex. of PrintItemDescription() output:

Bottled Water: Deer Park, 12 oz.


(2) Create three new files:

  • ShoppingCart.h - struct definition and related function declarations
  • ShoppingCart.c - related function definitions
  • main.c - main() function (Note: main()'s functionality differs from the warm up)

Build the ShoppingCart struct with the following data members and related functions. Note: Some can be function stubs (empty functions) initially, to be completed in later steps.

  • Data members (3 pts)
    • char customerName [ ]
    • char currentDate [ ]
    • ItemToPurchase cartItems [ ] - has a maximum of 10 slots (can hold up to 10 items of any quantity)
    • int cartSize - the number of filled slots in array (number of items in cart of any quantity)
  • Related functions
    • AddItem()
      • Adds an item to cartItems array. Has parameters ItemToPurchase and ShoppingCart. Returns ShoppingCart object.
    • RemoveItem()
      • Removes item from cartItems array (does not just set quantity to 0; removed item will not take up a slot in array). Has a char[ ](an item's name) and a ShoppingCart parameter. Returns ShoppingCart object.
      • If item name cannot be found, output this message: Item not found in cart. Nothing removed.
    • ModifyItem()
      • Modifies an item's description, price, and/or quantity. Has parameters ItemToPurchase and ShoppingCart. Returns ShoppingCart object.
    • GetNumItemsInCart() (2 pts)
      • Returns quantity of all items in cart. Has a ShoppingCart parameter.
    • GetCostOfCart() (2 pts)
      • Determines and returns the total cost of items in cart. Has a ShoppingCart parameter.
    • PrintTotal()
      • Outputs total of objects in cart. Has a ShoppingCart parameter.
      • If cart is empty, output this message: SHOPPING CART IS EMPTY
    • PrintDescriptions()
      • Outputs each item's description. Has a ShoppingCart parameter.


Ex. of PrintTotal() output:

John Doe's Shopping Cart - February 1, 2016
Number of Items: 8

Nike Romaleos 2 @ $189 = $378
Chocolate Chips 5 @ $3 = $15
Powerbeats 2 Headphones 1 @ $128 = $128

Total: $521


Ex. of PrintDescriptions() output:

John Doe's Shopping Cart - February 1, 2016

Item Descriptions
Nike Romaleos: Volt color, Weightlifting shoes
Chocolate Chips: Semi-sweet
Powerbeats Headphones: Bluetooth headphones


(3) In main(), prompt the user for a customer's name and today's date. Output the name and date. Create an object of type ShoppingCart. (1 pt)

Ex.

Enter Customer's Name:
John Doe
Enter Today's Date:
February 1, 2016

Customer Name: John Doe
Today's Date: February 1, 2016


(4) Implement the PrintMenu() function. PrintMenu() has a ShoppingCart parameter, and outputs a menu of options to manipulate the shopping cart. Each option is represented by a single character. Build and output the menu within the function.

If the an invalid character is entered, continue to prompt for a valid choice. Hint: Implement Quit before implementing other options. Call PrintMenu() in the main() function. Continue to execute the menu until the user enters q to Quit. (3 pts)

Ex:

MENU
a - Add item to cart
r - Remove item from cart
c - Change item quantity
i - Output items' descriptions
o - Output shopping cart
q - Quit

Choose an option:


(5) Implement the "Output shopping cart" menu option. (3 pts)

Ex:

OUTPUT SHOPPING CART
John Doe's Shopping Cart - February 1, 2016
Number of Items: 8

Nike Romaleos 2 @ $189 = $378
Chocolate Chips 5 @ $3 = $15
Powerbeats Headphones 1 @ $128 = $128

Total: $521


(6) Implement the "Output item's description" menu option. (2 pts)

Ex.

OUTPUT ITEMS' DESCRIPTIONS
John Doe's Shopping Cart - February 1, 2016

Item Descriptions
Nike Romaleos: Volt color, Weightlifting shoes
Chocolate Chips: Semi-sweet
Powerbeats Headphones: Bluetooth headphones


(7) Implement "Add item to cart" menu option. (3 pts)

Ex:

ADD ITEM TO CART
Enter the item name:
Nike Romaleos
Enter the item description:
Volt color, Weightlifting shoes
Enter the item price:
189
Enter the item quantity:
2


(8) Implement the "Remove item from cart" menu option. (4 pts)

Ex:

REMOVE ITEM FROM CART
Enter name of item to remove:
Chocolate Chips


(9) Implement "Change item quantity" menu option. Hint: Make new ItemToPurchase object before using ModifyItem() function. (5 pts)

Ex:

CHANGE ITEM QUANTITY
Enter the item name:
Nike Romaleos
Enter the new quantity:
3

Solutions

Expert Solution

Working code implemented in C and appropriate comments provided for better understanding:

Here I am attaching code for these files:

  • main.c
  • ItemToPurchase.c
  • ItemToPurchase.h
  • ShoppingCart.c
  • ShoppingCart.h

Source code for main.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ShoppingCart.h"
#include "ItemToPurchase.h"

#define CUST_NAME_LEN 50
#define CURR_DATE_LEN 20
#define ITEM_NAME_LEN 50
#define ITEM_DESC_LEN 100

void PrintMenu(ShoppingCart cart);
ShoppingCart MenuAddItem(ShoppingCart cart);
ShoppingCart MenuRemoveItem(ShoppingCart cart);
ShoppingCart MenuChangeItemQty(ShoppingCart cart);
void MenuPrintDescriptions(ShoppingCart cart);
void MenuOutputCart(ShoppingCart cart);

int main(int argc, char const *argv[]) {
// Declare variables
char name[CUST_NAME_LEN];
char date[CURR_DATE_LEN];
char choice = ' ';
char tmp;
ShoppingCart cart;

// Take user input: customer name
printf("Enter Customer's Name:\n");
fgets(name, CUST_NAME_LEN, stdin);
name[strlen(name) - 1] = '\0';

// Take user input: current date
printf("Enter Today's Date:\n");
fgets(date, CURR_DATE_LEN, stdin);
date[strlen(date) - 1] = '\0';

// Initialize ShoppingCart
strcpy(cart.customerName, name);
strcpy(cart.currentDate, date);
cart.cartSize = 0;

// Print initial information
printf("\nCustomer Name: %s\n", cart.customerName);
printf("Today's Date: %s\n\n", cart.currentDate);

PrintMenu(cart);
do {
printf("Choose an option:\n");
scanf(" %c%c", &choice, &tmp);

switch (choice) {
case 'q':
break;
case 'a':
cart = MenuAddItem(cart);
PrintMenu(cart);
break;
case 'r':
cart = MenuRemoveItem(cart);
PrintMenu(cart);
break;
case 'c':
break;
case 'i':
MenuPrintDescriptions(cart);
PrintMenu(cart);
break;
case 'o':
MenuOutputCart(cart);
PrintMenu(cart);
break;
default:
break;
}
} while (choice != 'q');

return 0;
}

void PrintMenu(ShoppingCart cart) {
printf("MENU\n");
printf("a - Add item to cart\n");
printf("r - Remove item from cart\n");
printf("c - Change item quantity\n");
printf("i - Output items' descriptions\n");
printf("o - Output shopping cart\n");
printf("q - Quit\n\n");
}

ShoppingCart MenuAddItem(ShoppingCart cart) {
char item_name[ITEM_NAME_LEN];
char item_desc[ITEM_DESC_LEN];
int item_price = 0;
int item_qty = 0;
ItemToPurchase item;

printf("ADD ITEM TO CART\n");

printf("Enter the item name:\n");
fgets(item_name, ITEM_NAME_LEN, stdin);
item_name[strlen(item_name) - 1] = '\0';
strcpy(item.itemName, item_name);

printf("Enter the item description:\n");
fgets(item_desc, ITEM_DESC_LEN, stdin);
item_desc[strlen(item_desc) - 1] = '\0';
strcpy(item.itemDescription, item_desc);

printf("Enter the item price:\n");
scanf("%d", &item_price);
item.itemPrice = item_price;

printf("Enter the item quantity:\n\n");
scanf("%d", &item_qty);
item.itemQuantity = item_qty;

cart = AddItem(item, cart);
return cart;
}

ShoppingCart MenuRemoveItem(ShoppingCart cart) {
char item_name[ITEM_NAME_LEN];

printf("REMOVE ITEM FROM CART\n");
printf("Enter name of item to remove:\n");
scanf(" %s", item_name);
for (int i = 0; i < cart.cartSize; i++) {
if (strcmp(item_name, cart.cartItems[i].itemName) == 0) {
cart = RemoveItem(item_name, cart);
} else {
printf("Item not found in cart. Nothing removed.\n\n");
}
}
return cart;
}

ShoppingCart MenuChangeItemQty(ShoppingCart cart) {
return cart;
}

void MenuPrintDescriptions(ShoppingCart cart) {
printf("OUTPUT ITEMS' DESCRIPTIONS\n");
PrintDescriptions(cart);
}

void MenuOutputCart(ShoppingCart cart) {
printf("OUTPUT SHOPPING CART\n");
PrintTotal(cart);
}


Source code for ItemToPurchase.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ItemToPurchase.h"

void MakeItemBlank(ItemToPurchase* item) {
strcpy(item->itemName, "none");
strcpy(item->itemDescription, "none");
item->itemPrice = 0;
item->itemQuantity = 0;
}

void PrintItemCost(ItemToPurchase item) {
printf("%s %d @ $%d = $%d\n"
, item.itemName
, item.itemQuantity
, item.itemPrice
, item.itemPrice * item.itemQuantity
);
}

void PrintItemDescription(ItemToPurchase item) {
printf("%s: %s\n", item.itemName, item.itemDescription);
}

Source code for ItemToPurchase.h:

#ifndef ITEMTOPURCHASE__INC__
#define ITEMTOPURCHASE__INC__

#include <stdio.h>
#include <stdlib.h>

#define ITEM_NAME_LEN 50
#define ITEM_DESC_LEN 100

typedef struct ItemToPurchase {
char itemName[ITEM_NAME_LEN];
char itemDescription[ITEM_DESC_LEN];
int itemPrice;
int itemQuantity;
} ItemToPurchase;

void MakeItemBlank(ItemToPurchase* item);
void PrintItemCost(ItemToPurchase item);
void PrintItemDescription(ItemToPurchase item);

#endif

Source code for ShoppingCart.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ShoppingCart.h"

ShoppingCart AddItem(ItemToPurchase item, ShoppingCart cart) {
// Adds an item to cart
cart.cartItems[cart.cartSize] = item;
cart.cartSize++;
return cart;
}

ShoppingCart RemoveItem(char item_name[], ShoppingCart cart) {
// Removes an item from cart
for (int i = 0; i < cart.cartSize; i++) {
if (strcmp(item_name, cart.cartItems[i].itemName) == 0) {
cart.cartItems[i].itemQuantity = 0;
}
}
return cart;
}

ShoppingCart ModifyItem(ItemToPurchase item, ShoppingCart cart) {
// Modifies contents (name, qty, etc.) of an item in cart
return cart;
}

int GetNumItemsInCart(ShoppingCart cart) {
// Returns the total quantity of items in a cart
int count = 0;
for (int i = 0; i < cart.cartSize; i++) {
count += cart.cartItems[i].itemQuantity;
}
return count;
return 0;
}

int GetCostOfCart(ShoppingCart cart) {
// Returns the total cost of items in a cart
int total = 0;
for (int i = 0; i < cart.cartSize; i++) {
total += cart.cartItems[i].itemPrice * cart.cartItems[i].itemQuantity;
}
return total;
}

void PrintTotal(ShoppingCart cart) {
// Outputs total number of items in cart
printf("%s's Shopping Cart - %s\n", cart.customerName, cart.currentDate);
printf("Number of Items: %d\n\n", GetNumItemsInCart(cart));
if (GetNumItemsInCart(cart) == 0) {
printf("SHOPPING CART IS EMPTY\n");
} else {
for (int i = 0; i < cart.cartSize; i++) {
PrintItemCost(cart.cartItems[i]);
}
}
printf("\nTotal: $%d\n\n", GetCostOfCart(cart));
}

void PrintDescriptions(ShoppingCart cart) {
// Outputs each item's description
printf("%s's Shopping Cart - %s\n\n", cart.customerName, cart.currentDate);
printf("Item Descriptions\n");
for (int i = 0; i < cart.cartSize; i++) {
PrintItemDescription(cart.cartItems[i]);
}
printf("\n");
}

Source code for ShoppingCart.h:

#ifndef SHOPPINGCART__INC__
#define SHOPPINGCART__INC__

#include <stdio.h>
#include <stdlib.h>
#include "ItemToPurchase.h"

#define CUST_NAME_LEN 50
#define CURR_DATE_LEN 20
#define MAX_CART_SIZE 10

typedef struct ShoppingCart {
char customerName[CUST_NAME_LEN];
char currentDate[CURR_DATE_LEN];
int cartSize;
ItemToPurchase cartItems[MAX_CART_SIZE];
} ShoppingCart;

ShoppingCart AddItem(ItemToPurchase item, ShoppingCart cart); // Adds an item to cart
ShoppingCart RemoveItem(char item_name[], ShoppingCart cart); // Removes an item from cart
ShoppingCart ModifyItem(ItemToPurchase item, ShoppingCart cart); // Modifies contents (name, qty, etc.) of an item in cart
int GetNumItemsInCart(ShoppingCart cart); // Returns the total quantity of items in a cart
int GetCostOfCart(ShoppingCart cart); // Returns the total cost of items in a cart
void PrintTotal(ShoppingCart cart); // Outputs total number of items in cart
void PrintDescriptions(ShoppingCart cart); // Outputs each item's description

#endif

Output Screenshots:

Hope it helps, if you like the answer give it a thumbs up. Thank you.


Related Solutions

IN C++ 7.22 LAB*: Program: Online shopping cart (Part 2) This program extends the earlier "Online...
IN C++ 7.22 LAB*: Program: Online shopping cart (Part 2) 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 the quantity, price, and subtotal PrintItemDescription() - Outputs the...
C++.This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1)...
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 the quantity, price, and subtotal PrintItemDescription() - Outputs the item name and description Private data members string itemDescription -...
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1)...
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class to contain a new attribute. (2 pts) item_description (string) - Set to "none" in default constructor Implement the following method for the ItemToPurchase class. print_item_description() - Prints item_description attribute for an ItemToPurchase object. Has an ItemToPurchase parameter. Ex. of print_item_description() output: Bottled Water: Deer Park, 12 oz. (2) Build the ShoppingCart class with the following data attributes and related methods....
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1)...
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class to contain a new attribute. (2 pts) item_description (string) - Set to "none" in default constructor Implement the following method for the ItemToPurchase class. print_item_description() - Prints item_description attribute for an ItemToPurchase object. Has an ItemToPurchase parameter. Ex. of print_item_description() output: Bottled Water: Deer Park, 12 oz. (2) Build the ShoppingCart class with the following data attributes and related methods....
In python 3 please: This program extends the earlier "Online shopping cart" program. (Start working from...
In python 3 please: This program extends the earlier "Online shopping cart" program. (Start working from your Program 7). (1) Build the ShoppingCart class with the following data attributes and related methods. Note: Some can be method stubs (empty methods) initially, to be completed in later steps.(3 pt) ● Parameterized constructor which takes the customer name and date as parameters ● Attributes ○ customer_name (string) ○ current_date (string) ○ cart_items (list) ● Methods ○ add_item() ■ Adds an item to...
This week you will write a program that mimics an online shopping cart . You will...
This week you will write a program that mimics an online shopping cart . You will use all the programming techniques we have learned thus far this semester, including if statements, loops, lists and functions. It should include all of the following: The basics - Add items to a cart until the user says "done" and then print out a list of items in the cart and a total price. - Ensure the user types in numbers for quantities -...
Python This week you will write a program in Python that mimics an online shopping cart...
Python This week you will write a program in Python that mimics an online shopping cart . You will use all the programming techniques we have learned thus far this semester, including if statements, loops, lists and functions. It should include all of the following: The basics - Add items to a cart until the user says "done" and then print out a list of items in the cart and a total price. - Ensure the user types in numbers...
Python This week you will write a program in Pyhton that mimics an online shopping cart...
Python This week you will write a program in Pyhton that mimics an online shopping cart . You will use all the programming techniques we have learned thus far this semester, including if statements, loops, lists and functions. It should include all of the following: The basics - Add items to a cart until the user says "done" and then print out a list of items in the cart and a total price. - Ensure the user types in numbers...
9.25 LAB*: Program: Online shopping cart (Part 2) Note: Creating multiple Scanner objects for the same...
9.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 first saving your earlier program). (1) Extend the ItemToPurchase class per the following specifications: Private fields string itemDescription -...
Create a Python 3 program that acts as a grocery store shopping cart. 1. Create a...
Create a Python 3 program that acts as a grocery store shopping cart. 1. Create a class named GroceryItem. This class should contain: - an __init__ method that initializes the item’s name and price - a get_name method that returns the item’s name - a get_price method that returns the item’s price - a __str__ method that returns a string representation of that GroceryItem - an unimplemented method is_perishable Save this class in GroceryItem.py. 2. Create a subclass Perishable that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT