Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

c++ problem need c++ format ------------------------------------------------------------------- #ifndef ITEM_H #define ITEM_H #include class Item { public: //constructor Item(std::string name, double price, int quantity); //accessors std::string get_name()

c++ problem

need c++ format

-------------------------------------------------------------------

#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; /ame of the item

double price; //price for one item

int quantity; // hoe many are you buyying

};

#endif

-----------------------------------------------------------------------

#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;

}

image text in transcribedimage text in transcribed

A Shopping Cart Create a class ShoppingCart that will represent the user's 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. Don't 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. eadd()the add() method must take an Item as a parameter and add it at the end of the cart if the item isn't 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. e write The write() method must write the name price, and quarti o all items contain edin the cart. as well as tie o aval e ota p ce of 1 tems n te at 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

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

Step: 3

blur-text-image

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

Filing And Computer Database Projects

Authors: Jeffrey Stewart

2nd Edition

007822781X, 9780078227813

More Books

Students also viewed these Databases questions

Question

Understand the importance of strategic HR planning.

Answered: 1 week ago

Question

What is Change Control and how does it operate?

Answered: 1 week ago

Question

How do Data Requirements relate to Functional Requirements?

Answered: 1 week ago