Question

In: Computer Science

IN C++ PLEASE: (1) Extend the ItemToPurchase class per the following specifications: Parameterized constructor to assign...

IN C++ PLEASE:

(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).
  • Public member functions
    • SetDescription() mutator & GetDescription() accessor
    • PrintItemDescription() - Outputs the item name and description in the format name: description
  • Private data members
    • string itemDescription - Initialized in default constructor to "none"

(2) Create three new files:

  • ShoppingCart.h - Class declaration
  • ShoppingCart.cpp - Class definition
  • main.cpp - main() function (Note: main()'s functionality differs from checkpoint A)

Build the ShoppingCart class with the following specifications. Note: Some can be function stubs (empty functions) initially, to be completed in later steps.

  • Default constructor
  • Parameterized constructor which takes the customer name and date as parameters
  • Private data members
    • string customerName - Initialized in default constructor to "none"
    • string currentDate - Initialized in default constructor to "January 1, 2016"
    • vector < ItemToPurchase > cartItems
  • Public member functions
    • GetCustomerName() accessor
    • GetDate() accessor
    • AddItem()
      • Has parameter ItemToPurchase. Does not return anything.
      • Adds the item to cartItems vector as long as the quantity is not zero.
    • IsItemInCart()
      • Has a string (an item's name) parameter. Checks if that item is in cart. If so, returns True else False.
      • Has an integer parameter passed by reference. If the item exists, populates it with index in cartItems vector. ((I ESPECIALLY NEED HELP WITH THIS PART PLEASE)))
    • RemoveItem()
      • Removes item from cartItems vector. Has a string (an item's name) parameter. Does not return anything.
      • 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 parameter ItemToPurchase with updated data members. Does not return anything.
      • If item can be found (by name) in the cart, check if the input parameter has default values for data members: description, price, and quantity. If not, modify that data member of the item in cart.
      • If item cannot be found (by name) in cart, output this message: Item not found in cart. Nothing modified.
    • GetNumItemsInCart()
      • Returns quantity of all items in cart. Has no parameters.
    • GetCostOfCart()
      • Determines and returns the total cost of items in cart. Has no parameters.
    • PrintTotal()
      • Outputs total of objects in cart.
      • If cart is empty, output this message: SHOPPING CART IS EMPTY
    • PrintDescriptions()
      • Outputs each item's description.

Solutions

Expert Solution

//class ItemToPurchase definition

class ItemToPurchase {
    string itemDescription;

    public:
        string itemName;
        int itemPrice, itemQuantity;

        //Parameterized Constructor with default values
        ItemToPurchase(string itemName = "none", string itemDescription = "none", int itemPrice = 0, int itemQuantity = 0) {
            this.itemName = itemName;
            this.itemDescription = itemDescription;
            this.itemPrice = itemPrice;
            this.itemQuantity = itemQuantity;
        }

    void SetDescription(string itemDescription) {
        this.itemDescription = itemDescription;
    }

    string GetDescription() {
        return this.itemDescription;
    }

    void PrintItemDescription() {
        cout << this.itemName << ": " << this.itemDescription << endl;
    }
};

ShoppingCart.h

#ifndef SHOPPINGCART_H
#define SHOPPINGCART_H

class ShoppingCart : public ItemToPurchase{
    private :
        string customerName,currentDate;
        vector<ItemToPurchase> cartItems;

    public :
        ShoppingCart();
        ShoppingCart(string customerName,string date);
        
        string GetCustomerName();
        string GetDate();
    
        bool IsItemInCart(string itemName, int &index);
        void ModifyItem(ItemToPurchase item);
        void AddItem(ItemToPurchase item);
        void RemoveItem(string itemName);
        
        int GetNumItemsInCart();
        int GetCostOfCart();
        
        void PrintTotal();
        void PrintDescriptions();
};

#endif



ShoppingCart.cpp

#include<bits/stdc++.h>
#include"ShoppingCart.h"

using namespace std;

 //Default Constructor
ShoppingCart::ShoppingCart(){
    customerName="none";
    currentDate="January 1, 2016"
}

//parametrised Construtor
ShoppingCart::ShoppingCart(string customerName, string date){ 
    this.customerName = customerName;
    this.currentDate = date;
}

string ShoppingCart::GetCustomerName(){
    return customerName;
}

string ShoppingCart::GetDate(){
    return currentDate;
}

bool ShoppingCart::IsItemInCart(string itemName,int &index){
    bool found = false;
    int pos = 0;
    for(ItemToPurchase item : cartItems){
        if(item.itemName == itemName){
            found = true;
            index = pos;

            return found;
        }
        pos++;
    }
    
    return found;
}

void ShoppingCart::ModifyItem(ItemToPurchase item){
    int pos = -1;
    if( IsItemInCart(item.itemName, pos) ){
        // Modifies an item's description, price, and/or quantity. Has parameter ItemToPurchase with updated data members.
        cartItems[pos].SetDescription(item.GetDescription());
        cartItems[pos].itemPrice=item.itemPrice;
        cartItems[pos].itemQuantity=item.itemQuantity;
    }else{
        //Item cannot be found (by name) in cart, output this message:
        cout<<"Item not found in cart. Nothing modified.\n";
    }
}

void ShoppingCart :: AddItem(ItemToPurchase item){
    // Adds the item to cartItems vector as long as the quantity is not zero.
    if(item.itemQuantity!=0){
        cartItems.push_back(item);
    }
}

void ShoppingCart::RemoveItem(string itemName){
    int pos = -1;
    
    if( IsItemInCart(itemName, pos) ){
        cartItems.erase( cartItems.begin() + pos );
    }else{
        cout<<"Item not found in cart. Nothing removed.\n";
    }
}

int ShoppingCart::GetNumItemsInCart(){
    // Returns quantity of all items in cart. Has no parameters.
    int noOfItems = 0;
    for(ItemToPurchase item:cartItems){
        noOfItems += item.itemQuantity;
    }
    return noOfItems;
}

int ShoppingCart::GetCostOfCart(){
    // Determines and returns the total cost of items in cart.
    int totalCost = 0;
    for(ItemToPurchase item:cartItems){
        totalCost += (item.itemQuantity*item.itemPrice);
    }
    return totalCost;
}

void ShoppingCart::PrintTotal(){
    // Outputs total of objects in cart.
    if(cartItems.size() == 0){
        cout << "SHOPPING CART IS EMPTY\n";
    }else{
        cout << cartItems.size()<<"\n";
    }
}

void ShoppingCart::PrintDescriptions(){
    // Outputs each item's description.
    for(ItemToPurchase item:cartItems){
        cout << item.PrintItemDescription();
    }
}


main.cpp

#include<bits/stdc++.h>
#include"ShoppingCart.h"

using namespace std;

int main(){
    // your code  
            
    return 0;
}

Related Solutions

Step 1: Extend the dispenserType class per the following specifications. Add a parameterized constructor to initialize...
Step 1: Extend the dispenserType class per the following specifications. Add a parameterized constructor to initialize product name (name), product cost (cost) and product quantity (noOfItems). This constructor will be invoked from main.cpp. One such call to this constructor will look like: dispenserType sanitizer("hand sanitizer", 50, 100); Add the declaration and definition of this parameterized constructor to .h and .cpp files. Step2: Define a new class, cashRegister. Start by creating cashRegister.h and cashRegister.cpp files. Be sure to update your CMakeLists.txt...
Please make your Task class serializable with a parameterized and copy constructor, then create a TaskListDemo...
Please make your Task class serializable with a parameterized and copy constructor, then create a TaskListDemo with a main( ) method that creates 3 tasks and writes them to a binary file named "TaskList.dat". We will then add Java code that reads a binary file (of Task objects) into our program and displays each object to the console. More details to be provided in class. Here is a sample transaction with the user (first time the code is run): Previously...
1. Can you extend an abstract class? In what situation can you not inherit/extend a class?...
1. Can you extend an abstract class? In what situation can you not inherit/extend a class? 2. Can you still make it an abstract class if a class does not have any abstract methods?
In C++ 14.22 A dynamic class - party list Complete the Party class with a constructor...
In C++ 14.22 A dynamic class - party list Complete the Party class with a constructor with parameters, a copy constructor, a destructor, and an overloaded assignment operator (=). //main.cpp #include <iostream> using namespace std; #include "Party.h" int main() { return 0; } //party.h #ifndef PARTY_H #define PARTY_H class Party { private: string location; string *attendees; int maxAttendees; int numAttendees;    public: Party(); Party(string l, int num); //Constructor Party(/*parameters*/); //Copy constructor Party& operator=(/*parameters*/); //Add destructor void addAttendee(string name); void changeAttendeeAt(string...
C++ Code Required to Show The constructor of parent class executes before child class
C++ Code Required to Show The constructor of parent class executes before child class
In C++ Write a class named TestScores. The class constructor should accept an array of test...
In C++ Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a member function that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an exception. Demonstrate the class in program.
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's constructor...
C# Programming 1. Create an Employee class with two fields: idNum and hourlyWage. The Employee constructor...
C# Programming 1. Create an Employee class with two fields: idNum and hourlyWage. The Employee constructor requires values for both fields. Upon construction, thrown an ArgumentException if the hourlyWage is less than 7.50 or more than 50.00. Write a program that establishes, one at a time, at least three Employees with hourlyWages that are above, below, and within the allowed range. Immediately after each instantiation attempt, handle any thrown Exceptions by displaying an error message. Save the file as EmployeeExceptionDemo.cs....
1. From the constructor, you infer that the name of the class containing the constructor must be_______ .
1. From the constructor, you infer that the name of the class containing the constructor must be_______ .2. Fill in the blanks so that the statement will instantiate a JButton object and assigns it to variable of JButton type:JButton btn= _______ ("OK");3. Fill in the blanks to complete the description of the constructor in the corresponding UML class diagram:+<>______( _____ : ______)
The following code is for a class named Box. The class Box includes a constructor method...
The following code is for a class named Box. The class Box includes a constructor method Box, and a method getVolume(). For your assignment you are to develop a java class named MatchBox. Your class MatchBox must extend the class Box and in addition to the attributes width, height, and depth that are defined in the class Box, MatchBox must add a new attribute weight. The getVolume method must both report the values of width, height, depth, and weight, but...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT