In: Computer Science
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++
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.