Knowee
Questions
Features
Study Tools

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

Question

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

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, 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 << "Rewards: " << 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.setIsOldCustomer(false);
    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, sets whether the customer is old or not, and displays the cart. The output will be the total price and the rewards for each cart.

This problem has been solved

Similar Questions

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

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.

Dinesh is working in a supermarket and he is developing a program to calculate the cost of different types of items. Help him write the program that does the following:a) Create a base class, ItemType, with one virtual function double calculateAmount()b) Create a class called wooden that extends ItemType class with a number of items and cost as its private attributes. Obtain the data members and override the virtual function and calculate the total amount.c) Create a class called electronics that extends ItemType class with cost as its private attribute. Obtain the data member and override the virtual function and calculate the amount with 20% discount.Note: This question helps in clearing Infosys tests.Input format :The first line consists of an integer choice (1 or 2) representing the choice of item type.If the choice is 1 (wooden items), the next line consists of two space-separated integers: noOfItems and cost, representing the number of wooden items and their individual cost, respectively.If the choice is 2 (electronics), the next line consists of a single floating-point number cost, representing the cost of the electronic item.Output format :The output prints a floating-point number representing the calculated total cost of the chosen item type rounded off to two decimal places.Code constraints :10 < cost < 1060 < noOfItems < 20Sample test cases :Input 1 :15 840.5Output 1 :4202.50Input 2 :21800.56Output 2 :1440.45Note :

In the last lesson, you created a class called DiscountedItem as part of a Shopping Cart application. Please copy your solutions from the last lesson into the Active Code window below (or in repl or another IDE) before completing this challenge.The ShoppingCart contains a polymorphic ArrayList called order that you can use to add Items or DiscountedItems to the shopping cart. The Item class keeps track of the name and the price of each Item. The DiscountedItem class you wrote in the last lesson adds on a discount amount.In this challenge, you will write a method called int countDiscountedItems() in the ShoppingCart class.This method will use a loop to traverse the ArrayList of Items called order.In the loop, you will test if each Item is a DiscountedItem by using the instanceof keyword (object instanceof Class returns true or false) similar to its use in the add(Item) method.If it is a DiscountedItem, then you will count it.At the end of the loop, the method will return the count.Make sure you print out the number of discounted items in the main method or in printOrder(), so that you can test your method. Add more items to the order to test it.Copy in your code for DiscountedItem below and then write a method called countDiscountedItems which traverses the polymorphic ArrayList<Item>. Use instanceof to test each item to see if it is a DiscountedItem.Save & Run2024/3/18 13:34:41 - 2 of 2Show CodeLensReformat1import java.util.*;2​3/**4 * The ShoppingCart class has an ArrayList of Items. You will write a new class5 * DiscountedItem that extends Item. This code is adapted6 * https://practiceit.cs.washington.edu/problem/view/bjp4/chapter9/e10-DiscountBill7 */8public class Tester9{10 public static void main(String[] args)11 {12 ShoppingCart cart = new ShoppingCart();13 cart.add(new Item("bread", 3.25));14 cart.add(new Item("milk", 2.50));15 // cart.add(new DiscountedItem("ice cream", 4.50, 1.50));16 // cart.add(new DiscountedItem("apples", 1.35, 0.25));17​18 cart.printOrder();19 }20}21​22class DiscountedItem extends Item23{24 // Copy your code from the last lesson's challenge here!25 // add an instance variable for the discount26 private double discount;27​28 // Add constructors that call the super constructor29 public DiscountedItem(String name, double price1, double discount1)30 {31 super(name,price1);32 discount = discount1;33 }34​35 // Add get/set methods for discount36 public double getDiscount()37 {38 return 9.5; // return discount here instead of 039 }40 public void setDiscount(double discountNew)41 {42 discount = discountNew;43 }44​45 // Add a toString() method that returns a call to the super toString46 public String toString()47 {48 return super.toString() + super.valueToString(discount); 49 }50 // and then the discount in parentheses using the super.valueToString() method51​52}53​54// Add a method called countDiscountedItems()55class ShoppingCart56{57 private ArrayList<Item> order;58 private double total;59 private double internalDiscount;60​61 public ShoppingCart()62 {63 order = new ArrayList<Item>();64 total = 0.0;65 internalDiscount = 0.0;66 }67​68 public void add(Item i)

1/2

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.