Question

In: Computer Science

2. Create a class called Invoice that a store might use to represent an invoice for...

2. Create a class called Invoice that a store might use to represent an invoice for an item sold at the store. An Invoice should include four data members—the ID for the item sold (type string), name of item (type string), item description (type string) and the price of the item (type int). Your class should have a constructor that initializes the four data members. A constructor that receives multiple arguments. Example: ClassName( TypeName1 parameterName1, TypeName2 parameterName2, ... ) Provide a set and a get function for each data member. SearchbyNameAndAdd(): Ask the user which item he/she wants to purchase, if the item is found in the list then store the price of the item in a temporary array or variable. This function will keep adding the amount of items being selected by the user. Once the user has selected all the item he/she needs exit from this function and display the total amount by using the function below: o A function named getTotalAmount () that displays the total amount/bill as an int value.

write it in C++

Solutions

Expert Solution

I have implemented the "Invoice" class which contains itemId, itemName, itemDesc, itemPrice and totalAmount which is static data member for calculating totalAmount of all the items that are purchased by user.

Program:-

#include<iostream>

using namespace std;


class Invoice{

private:
    // store item id
    string itemId;

    // store item name
    string itemName;

    // store item desc
    string itemDesc;

    // store item price
    double itemPrice;

    // create static variable for total amount
    static int totalAmount;

public:

    // default constructor
    Invoice(){
    }

    // constructor intialize data members (create Item)
    Invoice(string id, string name, string desc, double price){

        itemId = id;
        itemName = name;
        itemDesc = desc;
        itemPrice = price;
    }

    // get function for itemId
    string getItemId(){

        return itemId;
    }

    // set function for itemId
    void setItemId(string id){

        itemId = id;
    }


    // get function for itemName
    string getItemName(){

        return itemName;
    }

    // set function for itemName
    void setItemName(string name){

        itemName = name;
    }


    // get function for itemDesc
    string getItemDesc(){

        return itemDesc;
    }

    // set function for itemDesc
    void setItemDesc(string desc){

        itemDesc = desc;
    }

    // get function for itemId
    double getItemPrice(){

        return itemPrice;
    }

    // set function for itemId
    void setItemPrice(double price){

        itemPrice = price;
    }

    // This function continuously ask the user to purchase an Item
    static void searchByNameAndAdd(Invoice items[]){

        string ch = "";

        do{
            // calculate total Items
            //int totalItems = sizeof(items)/sizeof(items[0]);

            // print item name
            int i=0;
            cout<<"------- Item Store -------\n"<<endl;
            for(i=0; i<3; i++)
                cout<<(i+1)<<". "<<items[i].getItemName()<<endl;

            cout<<(i+1)<<". EXIT"<<endl;
            // get the item name from the user
            cout<<"\nWhich item you want to purchase(Enter item Name) : ";
            cin>>ch;

            bool flag = false;
            // search item name into the array
            for(int i=0; i<3; i++){
                if(ch == items[i].getItemName()){
                    Invoice::totalAmount += items[i].getItemPrice();
                    flag = true;
                }
            }

            if(!flag && ch!="EXIT")
                cout<<"\nNo Item in the store\nPlease select right Item\n";
            else if(ch!="EXIT"){
                cout<<"you purchased "<<ch<<endl<<endl;
            }

        }while(ch!="EXIT");

        // call getTotalAmount() function for displaying totalAmount
        getTotalAmount();
    }


    //This function print the totalAmount for the user
    static void getTotalAmount(){

        // display total amount
        cout<<"\nTotal Amount is : "<<Invoice::totalAmount<<endl;
    }

};

// initialize static data member
int Invoice::totalAmount=0;

int main(){

    // initialize totalItems
    int totalItem = 3;

    Invoice items[totalItem];

    // enter first item detail

    // set ItemId
    items[0].setItemId("ID1");

    // set ItemName
    items[0].setItemName("Item1");

    // set ItemDesc
    items[0].setItemDesc("ItemDescription1");

    // set ItemPrice
    items[0].setItemPrice(50);


    // enter second item detail

    // set ItemId
    items[1].setItemId("ID2");

    // set ItemName
    items[1].setItemName("Item2");

    // set ItemDesc
    items[1].setItemDesc("ItemDescription2");

    // set ItemPrice
    items[1].setItemPrice(70);



    // enter third item detail

    // set ItemId
    items[2].setItemId("ID3");

    // set ItemName
    items[2].setItemName("Item3");

    // set ItemDesc
    items[2].setItemDesc("ItemDescription3");

    // set ItemPrice
    items[2].setItemPrice(90);


    // call the searchByNameAndAdd() function
    Invoice::searchByNameAndAdd(items);

    return 0;

}

Output:-

I hope you will understand the above program.

Do you feel needful and useful then please upvote me.

Thank you.


Related Solutions

Invoice Class - Create a class called Invoice that a hardware store might use to represent...
Invoice Class - Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables—a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. If the quantity passed to the constructor is...
Invoice Class - Create a class called Invoice that a hardware store might use to represent...
Invoice Class - Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables—a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. If the quantity passed to the constructor is...
3.12 (Invoice Class) Create a class called Invoice that a hardware store might use to represent...
3.12 (Invoice Class) Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables-a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method for...
with PHP Create a class called Invoice that a hardware store might use to represent an...
with PHP Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables — a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method...
----------------------------------------------------------------------------------------------- Create a class called MathOperations that a teacher might use to represent the basic type...
----------------------------------------------------------------------------------------------- Create a class called MathOperations that a teacher might use to represent the basic type of mathematical operations that may be performed. The class should include the following: Three double private variables as instance variables, number1, number2, and result. Your class should have a default constructor that initializes the three instance variables to zero. Your class should also have a constructor that initializes the two instance variables (number1 and number2) to the value entered by the user from keyboard....
With PHP Create a class called Account that a bank might use to represent customers' bank...
With PHP Create a class called Account that a bank might use to represent customers' bank accounts. Your class should include one data member of type int to represent the account balance. Your class should provide a constructor that receives an initial balance and uses it to initialize the data member. The constructor should validate the initial balance to ensure that it is greater than or equal to 0. If not, the balance should be set to 0 and the...
Please solve using jupyter notebook . 10.10- (Invoice Class) Create a class called Invoice that a...
Please solve using jupyter notebook . 10.10- (Invoice Class) Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as data attributes—a part number (a string), a part description (a string), a quantity of the item being purchased (an int) and a price per item (a Decimal). Your class should have an __init__ method that initializes the four data attributes....
12. Create a class called bMoney. It should store money amounts as long doubles. Use the...
12. Create a class called bMoney. It should store money amounts as long doubles. Use the function mstold() to convert a money string entered as input into a long double, and the function ldtoms() to convert the long double to a money string for display. (See Exercises 6 and 10.) You can call the input and output member functions getmoney() and putmoney(). Write another member function that adds two bMoney amounts; you can call it madd(). Adding bMoney objects is...
IN C++ Create a class called TextInt. The purpose of a TextInt is to store an...
IN C++ Create a class called TextInt. The purpose of a TextInt is to store an integer and convert it to the English text form of the integer when needed, such as ‘zero’ for 0, ‘one’ for 1, and so on, up to ‘nine thousand nine hundred ninety nine’ for 9999. You do NOT need punctuation (commas, hyphens, ‘and’, etc.) The TextInt class should have its own .h and .cpp files. At least the translate function should be implemented in...
Create a class named UsedFurnitureItem to represent a used furniture item that the store sells. Private...
Create a class named UsedFurnitureItem to represent a used furniture item that the store sells. Private data members of a UsedFurnitureItem are: age (double) // age in years – default value for brandNewPrice (double) // the original price of the item when it was brand new description (string) // a string description of the item condition (char) // condition of the item could be A, B, or C. size (double) // the size of the item in cubic inches. weight...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT