You are required to implement a simple C++ program for an online shopping cart using a single class. The program should include a class ShoppingCart to represent the shopping cart. Each item in the cart has a name, price, and quantity.The ShoppingCart class should have the following:Private attributes for an array of item names, an array of item prices, an array of item quantities, the total number of items in the cart, and a flag indicating whether the customer is an old customer.A member function addItem that takes the item name, price, and quantity as arguments and adds them to the cart.A member function calculateTotal that calculates the total price of all items in the cart, taking into account a 10% discount for old customers,using the this pointer.A member function displayCart that displays the information of all items in the cart, including the total price and any applicable discounts or rewards. Award 1 reward point for every Rs.10 spent. This function should also demonstrate the usage of functions with objects as arguments.Testcase1:Sample input:2 // no of itemsKeypad // item1000 // price2 // quantityEarphone501Y // Old customerOutput:Rs.1845.00184.5 // RewardTestcase 2:Sample Input:3Keyboard5001Mouse3001Monitor10001NOutput:Rs.1800.00180
Question
You are required to implement a simple C++ program for an online shopping cart using a single class. The program should include a class ShoppingCart to represent the shopping cart. Each item in the cart has a name, price, and quantity.The ShoppingCart class should have the following:Private attributes for an array of item names, an array of item prices, an array of item quantities, the total number of items in the cart, and a flag indicating whether the customer is an old customer.A member function addItem that takes the item name, price, and quantity as arguments and adds them to the cart.A member function calculateTotal that calculates the total price of all items in the cart, taking into account a 10% discount for old customers,using the this pointer.A member function displayCart that displays the information of all items in the cart, including the total price and any applicable discounts or rewards. Award 1 reward point for every Rs.10 spent. This function should also demonstrate the usage of functions with objects as arguments.Testcase1:Sample input:2 // no of itemsKeypad // item1000 // price2 // quantityEarphone501Y // Old customerOutput:Rs.1845.00184.5 // RewardTestcase 2:Sample Input:3Keyboard5001Mouse3001Monitor10001NOutput:Rs.1800.00180
Solution
Here is a simple implementation of the ShoppingCart class in C++:
#include<iostream>
#include<string>
using namespace std;
class ShoppingCart {
private:
string itemNames[100];
double itemPrices[100];
int itemQuantities[100];
int totalItems;
bool isOldCustomer;
public:
ShoppingCart() {
totalItems = 0;
isOldCustomer = false;
}
void addItem(string name, double price, int quantity) {
itemNames[totalItems] = name;
itemPrices[totalItems] = price;
itemQuantities[totalItems] = quantity;
totalItems++;
}
double calculateTotal() {
double total = 0;
for(int i = 0; i < totalItems; i++) {
total += itemPrices[i] * itemQuantities[i];
}
if(isOldCustomer) {
total *= 0.9;
}
return total;
}
void displayCart() {
double total = calculateTotal();
cout << "Total: Rs." << total << endl;
cout << "Reward: " << total / 10 << endl;
}
void setIsOldCustomer(bool isOld) {
isOldCustomer = isOld;
}
};
int main() {
ShoppingCart cart;
cart.addItem("Keypad", 1000, 2);
cart.addItem("Earphone", 500, 1);
cart.setIsOldCustomer(true);
cart.displayCart();
ShoppingCart cart2;
cart2.addItem("Keyboard", 500, 1);
cart2.addItem("Mouse", 300, 1);
cart2.addItem("Monitor", 1000, 1);
cart2.displayCart();
return 0;
}
This program creates a ShoppingCart class with the required attributes and methods. It then creates two ShoppingCart objects, adds items to them, and displays their total cost and reward points. The setIsOldCustomer method is used to indicate whether the customer is an old customer.
Similar Questions
online shopping cartYou are required to implement a simple C++ program for an online shopping cart using a single class. The program should include a class ShoppingCart to represent the shopping cart. Each item in the cart has a name, price, and quantity.The ShoppingCart class should have the following:Private attributes for an array of item names, an array of item prices, an array of item quantities, the total number of items in the cart, and a flag indicating whether the customer is an old customer.A member function addItem that takes the item name, price, and quantity as arguments and adds them to the cart.A member function calculateTotal that calculates the total price of all items in the cart, taking into account a 10% discount for old customers,using the this pointer.A member function displayCart that displays the information of all items in the cart, including the total price and any applicable discounts or rewards. Award 1 reward point for every Rs.10 spent. This function should also demonstrate the usage of functions with objects as arguments.Testcase1:Sample input:2 // no of itemsKeypad // item1000 // price2 // quantityEarphone501Y // Old customerOutput:Rs.1845.00184.5 // RewardTestcase 2:Sample Input:3Keyboard5001Mouse3001Monitor10001NOutput:Rs.1800.00180
in C++ code array-based or vector-based:Write a program that will allow users the ability to order items from a supermarket online. The Item will consist of three attributes.● Item Name ● It’s a category ● Item priceFor the Item creation, there must be an item class where objects are part of the node which will form the non-linear trees. In other terms, the program is made up of three tree data structures which are 1 binary search tree, 2 heaps and 3 avl and they are all node-based. Below is an example of what the item class must include, however, more functionality can be added. class item { String itemname; string category; Int price; public: item(string, string, int); bool operator<(const item&); void print(); };The program will implement the three tree data structures as following:A Menu that allow user to choose which non linear structure: i. AVL Treesthe tree should have its mini menu: The mini-menu has all the operations that are applied to the tree structures:I. Add item data, II. Remove item data, III. Display the item data normally IV. Display all the items sorted by their name ascending V. Display all the items sorted by their name descending, VI. Display all the items sorted by their prices ascending VII. Display all the items sorted by their prices descending.Note: For Heap, the sorting is done using, Min Heap, Max Heap and Heap sort 3) Read the items from a file or add items manually To do so you should: Implement a global readItems(istream&,tree&) function, which takes the file name and the tree and reads the items in the file into the tree. Consider having the following file is a user reading list to use it for filling your trees. here's a sample of input: 12 chocolate milk drink 10 bananas fruit 75 pepsi drink 20 cheddar cheese dairy 49 Tuna meat 90 fanta orange drink 20 yought dairy 13 mint gum candy 2 water drink 9 apples fruit 66 beef meat 284 cheese cake desset 68
Online Shopping System using Inheritance and Static Members Consider an online shopping system where members, including buyers and sellers, participate in buying and selling items. The system is designed using inheritance with a base class Member, and subclasses Buyer and Seller. The Member class maintains a unique member number, name, and amount in hand for each member. Both Buyer and Seller classes inherit from the Member class. The Seller class additionally maintains an item ID, item name, and item price. The system prompts the user to input details for a specified number of sellers and buyers. After inputting the details, the system allows a buyer to purchase an item from a seller by entering the item ID. The system then transfers the item price from the buyer's amount in hand to the seller's amount in hand. Furthermore, a static member totalMembers is implemented in the Member class to count the total number of members in the system. A static method displayTotalMembers() is provided to display the total number of members at the end of the program execution. Your task is to understand and analyze the provided Java code, identify the inheritance hierarchy and static members used, and explain how the code functions to maintain and operate the online shopping system. Highlight the interactions between buyers and sellers, the data stored for each member, and the utilization of static members to track the total number of members in the system.
Develop an online book purchase system using structure concept in C and refer the following constraints.• Define structures to represent a book and a user. Include attributes such as book ID, title, author, price.• Use appropriate data types for each attribute and ensure encapsulation by making attributes private or protected.• Select suitable functions to add, delete, and display books in the structure.Input:2CSE101 C++ ProgrammingDennis Ritchie2000CSE102Python ProgrammingDavid Mark 2500Output:Do you want to delete Y or N? YCSE101Successfully deleted!Remaining RecordsCSE102Python ProgrammingDavid Mark 2500
User You are required to implement a simple Shopping List Manager using ArrayList data structure. The program must meet the following requirements. 1. Write a C++ program that allows users to manage a shopping list. 2. Use ArrayList data structure to store items in the shopping list. 3. Your program should contain the following functionalities. a. Add item to the shopping list. b. Remove items from the shopping list. c. View current items in the shopping list. d. Clear the shopping list (i.e. remove all items). e. Exit the program. 4. The program should display a menu to the user and allow them to choose options using numbers. 5. Ensure error handling for invalid inputs. 6. Use Object oriented programing principles where applicable. An output screenshot is given at the end of this file for your complete understanding and reference. Note: Don’t use ArrayList Built in library . You must follow the following program structure for naming and to build the program. Program Structure: Class: ShoppingList Attributes: 1. Items[100] : string 2. ItemCount: int Explicit Default Constructor: 1. ShoppingList() Functions: 1. addItem(…) //Where … represents function takes parameters. 2. removeItem(…) //Where … represents function takes parameters. 3. viewList() 4. clearList() Default Function: 1. main()
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.