Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In project 2 , you defined the following two classes: / / ShoppingCart.h template class ShoppingCart:public Bag { private: double totalPrice; public: ShoppingCart ( )

In project2, you defined the following two classes:
// ShoppingCart.h
template
class ShoppingCart:public Bag {
private:
double totalPrice;
public:
ShoppingCart();
double getTotalPrice()const;
bool remove(const ItemType& anEntry);
bool add(const ItemType& newEntry);
};
// Item.h
class Item
{
private:
std::string name; // name of the item
int quantity; // quantity of the item
float price; // price of the item
public:
Item();
Item(std::string name, float price, int qtty);
void setName(std::string name);
void setQuatity(int qtty);
void setPrice(float price);
std::string getName() const;
int getQuantity() const;
float getPrice() const;
friend std::istream &operator>>(std::istream &ins, Item &target);
};
std::ostream &operator<<(std::ostream &out, const Item &target);
bool operator ==(const Item& ob1, const Item& ob2);
In Project2, the add member function allows duplicated items. For example, suppose the shopping cart currently contains the following items
-------------------------------------------------
Name Unit_Price Quantity
T-shirt $19.992
Sweater $39.991
The total charge is $79.97
--------------------------------------------------
If we add a new item as follows:
T-shirt $19.993
the shopping cart becomes like this:
-------------------------------------------------
Name Unit_Price Quantity
T-shirt $19.992
Sweater $39.991
T-shirt $19.993
The total charge is $139.94
--------------------------------------------------
Two T-shirt items are considered duplicated items although their quantities are different (In Project 2 we ignore the quantity when defining operator ==).
Let's redefine the add member function to avoid duplicated items by only changing the quantity if the new item already exists. In above example, after adding the new item "T-shirt $19.993", the shopping cart should look like this:
-------------------------------------------------
Name Unit_Price Quantity
T-shirt $19.995
Sweater $39.991
The total charge is $139.94
--------------------------------------------------
To implement this new add function, first check if this newEntry is in the cart, and then perform the following two logic steps according to the checked results:
if this newEntry is not a duplicated item, simply add it to the cart. Then update the totalPrice;
otherwise, find the duplicated item from the cart and add its quantity to the new newEntry's quantity. Then remove the duplicated item from the cart; add the modified newEntry to the cart; and update the totalPrice.
Answer the following two questions about redefining the add function.
Question 6(5 points)
For step 1), which of the following implementations is correct?
Question 6 options:
a)
if(!contains(newEntry)
{
bool flag = add(newEntry);
if(flag)
totalPrice+=newEntry.getQuantity()*newEntry.getPrice();
}
b)
If(!contains(newEntry))
{
bool flag = add(newEntry);
if(flag)
totalPrice+= newEntry.getPrice();
}
c)
if(!contains(newEntry))
{
bool flag = Bag::add(newEntry);
if(flag)
totalPrice =2* totalPrice;
}
d)
If(!contains(newEntry))
{
bool flag = Bag::add(newEntry);
if(flag)
totalPrice+=newEntry.getQuantity()*newEntry.getPrice();
}
Question 7(5 points)
For step 2), which of the following implementations is correct?
Question 7 options:
a)
vector items = toVector();
int numItems = items.size();
int q_current;
int q_new = newEntry.getQuantity();
for(int i=0; i< numItems; ++i)
{
if(items[i]==newEntry){
q_current=items[i].getQuantity();
items[i].setQuantity *(q_current+q_new);
remove (items[i]);
break;
}
bool flag = add(newEntry);
if(flag)
totalPrice+=q_new * newEntry.getPrice();
b)
int numItems = getCurrentSize();
int q_current;
int q_new = newEntry.getQuantity();
for(int i=0; i< numItems; ++i)
{
if(items[i]==newEntry){
q_current=items[i].getQuantity();
newEntry.setQuantity(q_new+q_current);
remove(items[i]);
break;
}
}
bool flag = Bag::add(newEntry);
if (flag)
totalPrice+=q_new * newEntry.getPrice();
c)
vector items = toVector();
int numItems = items.size();
int q_current;
int q_new = newEntry.quantity;
for(int i=0; i< numItems; ++i)
{

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

More Books

Students also viewed these Databases questions

Question

What is kernel trick? How is it used in SVM?

Answered: 1 week ago