Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A Shopping Cart Create a class ShoppingCart that will represent the users virtual shopping cart. The shopping cart will use the STL std::vector for storing

A Shopping Cart

Create a class ShoppingCart that will represent the users virtual shopping cart. The shopping cart will use the STL std::vector for storing items that are added to the cart, and the public interface will expose methods that allow specific kinds of operations to be performed on the virtual cart.

Create the files ShoppingCart.h and ShoppingCart.cpp to contain the class definition and method implementations for your ShoppingCart class. Dont forget to add include guards in the header file! It might be a good idea to start a new project directory for this assignment, and copy the Item library from your in-lab exercise into it.

Class description for ShoppingCart:

The shopping cart only needs a single internal (private) attribute: a std::vector of Item objects.

The public interface of the cart must provide the following methods. You must name the methods exactly as specified; assume that your code will be tested against a different main program during grading.

Constructor

The default constructor is all that is needed for the cart; the implicit default constructor is OK, so there is no need to define any constructor.

add()

the add() method must take an Item as a parameter and add it at the end of the cart if the item isnt already in the cart. If the cart already contains the item, increase the quantity of the existing item. Two items are considered the same if and only if both the name and price are the same. This method does not return any value. If the item to be added to the cart has a quantity less than 1, it must not actually be added to the cart.

update_quantity()

The update_quantity() method must take the name of an item (a string) and the new quantity (an integer) as parameters. If the cart contains the item, the quantity must be updated to the new value and the method must return true. If no item in the cart matches the name provided, the method must return false. If the new quantity is zero (or any negative value), the item must be removed from the cart if it is found and the method must return true. Note that it is possible that two items with the same name (but different prices) could exist in the cart due to the behavior of the add() method if this is the case, the first item in the cart (the one nearest the beginning) must be updated.

remove()

The remove() method must take the name of an item (a string) and remove the matching item from the cart if the cart contains an item with that name. If the cart did contain the item, the method must return true; if the cart did not contain the item, the method must return false. Note that it is possible that two items with the same name (but different prices) could exist in the cart due to the behavior of the add() method if this is the case, only the first item in the cart (the one nearest the beginning) must be removed.

write()

The write() method must write the name, price, and quantity of all items contained in the cart, as well as the total value (total price) of all items in the cart to the console in a clean, easily readable format. Note that the total value must take into account the quantity of each item.

total_value()

The total_value() method takes no parameters and must return the total value (total price) of all items in the cart. Note that the total value must take into account the quantity of each item. If the cart is empty, the total value is zero.

Testing

Create a main program cart_test.cpp to test your shopping cart library. There are no specific input/output or interface requirements for this testing code, but you should be sure that your tests exercise your library fully. Dont just test the obvious or common use-cases, be sure to look for edge cases as well things that are not expected to happen (or things that would be unlikely to happen), but that are not prohibited by the behavior guidelines specified above.

use c++ format

**************************

item.h

#ifndef ITEM_H

#define ITEM_H

#include

class Item {

public:

//constructor

Item(std::string name, double price, int quantity);

//accessors

std::string get_name() const;

double get_price() const;

int get_quantity() const;

//mutators

void set_price(double new_price);

void set_quantity(int new_quantity);

private:

std::string name; //name of the item

double price; //price for one item

int quantity; // how many are you buyying

};

#endif

******************************

item.cpp

#include "Item.h"

#include

Item::Item(std::string name, double price, int quantity){

this -> name = name; // "this ->" means "self"

this -> price = price;

this -> quantity = quantity;

}

std::string Item::get_name() const{

return name;

}

double Item::get_price() const{

return price;

}

int Item::get_quantity() const{

return quantity;

}

void Item::set_price(double new_price){

price = new_price;

}

void Item::set_quantity(int new_quantity){

quantity = new_quantity;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The World Wide Web And Databases International Workshop Webdb 98 Valencia Spain March 27 28 1998 Selected Papers Lncs 1590

Authors: Paolo Atzeni ,Alberto Mendelzon ,Giansalvatore Mecca

1st Edition

3540658904, 978-3540658900

More Books

Students also viewed these Databases questions

Question

List behaviors to improve effective leadership in meetings

Answered: 1 week ago