Knowee
Questions
Features
Study Tools

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

Question

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

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

Solution

Claro, aquí tienes un programa que cumple con los requisitos especificados. El programa incluye la clase Item, la implementación de un árbol AVL, un menú para interactuar con el usuario y la función para leer los ítems desde un archivo.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

class Item {
public:
    string itemName;
    string category;
    int price;

    Item(string name, string cat, int pr) : itemName(name), category(cat), price(pr) {}

    bool operator<(const Item& other) const {
        return itemName < other.itemName;
    }

    void print() const {
        cout << "Item Name: " << itemName << ", Category: " << category << ", Price: " << price << endl;
    }
};

// Nodo del árbol AVL
struct AVLNode {
    Item data;
    AVLNode* left;
    AVLNode* right;
    int height;

    AVLNode(const Item& item) : data(item), left(nullptr), right(nullptr), height(1) {}
};

// Funciones auxiliares para el árbol AVL
int height(AVLNode* node) {
    return node ? node->height : 0;
}

int getBalance(AVLNode* node) {
    return node ? height(node->left) - height(node->right) : 0;
}

AVLNode* rightRotate(AVLNode* y) {
    AVLNode* x = y->left;
    AVLNode* T2 = x->right;

    x->right = y;
    y->left = T2;

    y->height = max(height(y->left), height(y->right)) + 1;
    x->height = max(height(x->left), height(x->right)) + 1;

    return x;
}

AVLNode* leftRotate(AVLNode* x) {
    AVLNode* y = x->right;
    AVLNode* T2 = y->left;

    y->left = x;
    x->right = T2;

    x->height = max(height(x->left), height(x->right)) + 1;
    y->height = max(height(y->left), height(y->right)) + 1;

    return y;
}

AVLNode* insert(AVLNode* node, const Item& item) {
    if (!node) return new AVLNode(item);

    if (item < node->data)
        node->left = insert(node->left, item);
    else if (node->data < item)
        node->right = insert(node->right, item);
    else
        return node;

    node->height = 1 + max(height(node->left), height(node->right));

    int balance = getBalance(node);

    if (balance > 1 && item < node->left->data)
        return rightRotate(node);

    if (balance < -1 && node->right->data < item)
        return leftRotate(node);

    if (balance > 1 && node->left->data < item) {
        node->left = leftRotate(node->left);
        return rightRotate(node);
    }

    if (balance < -1 && item < node->right->data) {
        node->right = rightRotate(node->right);
        return leftRotate(node);
    }

    return node;
}

void inOrder(AVLNode* root) {
    if (root) {
        inOrder(root->left);
        root->data.print();
        inOrder(root->right);
    }
}

void preOrder(AVLNode* root) {
    if (root) {
        root->data.print();
        preOrder(root->left);
        preOrder(root->right);
    }
}

void postOrder(AVLNode* root) {
    if (root) {
        postOrder(root->left);
        postOrder(root->right);
        root->data.print();
    }
}

void readItems(istream& in, AVLNode*& root) {
    string name, category;
    int price;
    while (in >> price >> ws) {
        getline(in, name, ' ');
        getline(in, category);
        root = insert(root, Item(name, category, price));
    }
}

int main() {
    AVLNode* root = nullptr;
    int choice;
    string filename;

    while (true) {
        cout << "Menu:\n";
        cout << "1. Add item data\n";
        cout << "2. Remove item data\n";
        cout << "3. Display the item data normally\n";
        cout << "4. Display all the items sorted by their name ascending\n";
        cout << "5. Display all the items sorted by their name descending\n";
        cout << "6. Display all the items sorted by their prices ascending\n";
        cout << "7. Display all the items sorted by their prices descending\n";
        cout << "8. Read items from file\n";
        cout << "9. Exit\n";
        cout << "Enter your choice: ";
        cin >> choice;

        if (choice == 9) break;

        switch (choice) {
            case 1: {
                string name, category;
                int price;
                cout << "Enter item name: ";
                cin >> ws;
                getline(cin, name);
                cout << "Enter category: ";
                getline(cin, category);
                cout << "Enter price: ";
                cin >> price;
                root = insert(root, Item(name, category, price));
                break;
            }
            case 3: {
                cout << "Items in the tree (in-order traversal):\n";
                inOrder(root);
                break;
            }
            case 4: {
                cout << "Items sorted by name ascending:\n";
                inOrder(root);
                break;
            }
            case 5: {
                cout << "Items sorted by name descending:\n";
                postOrder(root);
                break;
            }
            case 6: {
                cout << "Items sorted by price ascending:\n";
                // Implementar la lógica para ordenar por precio ascendente
                break;
            }
            case 7: {
                cout << "Items sorted by price descending:\n";
                // Implementar la lógica para ordenar por precio descendente
                break;
            }
            case 8: {
                cout << "Enter filename: ";
                cin >> filename;
                ifstream file(filename);
                if (file.is_open()) {
                    readItems(file, root);
                    file.close();
                } else {
                    cout << "Unable to open file\n";
                }
                break;
            }
            default:
                cout << "Invalid choice\n";
        }
    }

    return 0;
}

Este programa incluye la clase Item, la estructura del nodo AVL, las funciones auxiliares para manejar el árbol AVL, y un menú para que el usuario interactúe con el programa. También incluye la función readItems para leer los ítems desde un archivo y agregarlos al árbol AVL.

This problem has been solved

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

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()

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 •    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

Objective: The objective of this activity is to implement a basic inventory management system using Java programming concepts such as user-defined methods, scanner, arrays, loops, and conditions.Task Overview:1. Create a new Java class named `InventorySystem`.2. Declare the following static variables inside the `InventorySystem` class:   - `MAX_ITEMS`: an integer constant to define the maximum number of items in the inventory (set it to 10).   - `items`: a string array to store the names of the items in the inventory (initialize it with the size of `MAX_ITEMS`).   - `quantities`: an integer array to store the quantities of the items in the inventory (initialize it with the size of `MAX_ITEMS`).3. Implement the `main` method inside the `InventorySystem` class:   - Create a `Scanner` object named `scanner` to read user input from the console.   - Use a `while` loop to keep the program running until the user chooses to exit.   - Inside the loop, display a menu of options to the user:     1. Add item to inventory     2. Sell item from inventory     3. Display inventory     4. Exit   - Use a `switch` statement to execute the corresponding action based on the user's choice (1, 2, 3, or 4).   - If the user selects option 1, call the `addItem` method.   - If the user selects option 2, call the `sellItem` method.   - If the user selects option 3, call the `displayInventory` method.   - If the user selects option 4, exit the program.Sample Output 4. Implement the `displayInventory` method:   - Iterate through the `items` and `quantities` arrays and display the name and quantity of each item in the inventory.Sample OutputNote: For other displayInventory functions, check the sample output in the addItem and in the sellItem methods. 5. Implement the `addItem` method:   - Prompt the user to enter the name of the item to be added.   - Check if the item already exists in the inventory by searching for its name in the `items` array.   - If the item does not exist, add it to the list. Automatically it will have value of 1.   - If the item already exists, prompt the user to enter the quantity to be added.   - Update the quantity of the existing item in the `quantities` array.Sample Output6. Implement the `sellItem` method:   - Prompt the user to enter the name of the item to be sold.   - Check if the item exists in the inventory by searching for its name in the `items` array.   - If the item does not exist or if its quantity is 0, display an error message.   - If the item exists and its quantity is greater than 0, prompt the user to enter the quantity to be sold.   - Update the quantity of the sold item in the `quantities` array.Sample Output7. Compile and run the `InventorySystem` class to test the inventory management system.8. Test the system by adding items to the inventory, selling items, and displaying the current inventory.9. When exit is chosen, the program will terminate

Objective: The objective of this activity is to implement a basic inventory management system using Java programming concepts such as user-defined methods, scanner, arrays, loops, and conditions.Task Overview:1. Create a new Java class named `InventorySystem`.2. Declare the following static variables inside the `InventorySystem` class:   - `MAX_ITEMS`: an integer constant to define the maximum number of items in the inventory (set it to 10).   - `items`: a string array to store the names of the items in the inventory (initialize it with the size of `MAX_ITEMS`).   - `quantities`: an integer array to store the quantities of the items in the inventory (initialize it with the size of `MAX_ITEMS`).3. Implement the `main` method inside the `InventorySystem` class:   - Create a `Scanner` object named `scanner` to read user input from the console.   - Use a `while` loop to keep the program running until the user chooses to exit.   - Inside the loop, display a menu of options to the user:     1. Add item to inventory     2. Sell item from inventory     3. Display inventory     4. Exit   - Use a `switch` statement to execute the corresponding action based on the user's choice (1, 2, 3, or 4).   - If the user selects option 1, call the `addItem` method.   - If the user selects option 2, call the `sellItem` method.   - If the user selects option 3, call the `displayInventory` method.   - If the user selects option 4, exit the program.Sample Output 4. Implement the `displayInventory` method:   - Iterate through the `items` and `quantities` arrays and display the name and quantity of each item in the inventory.Sample OutputNote: For other displayInventory functions, check the sample output in the addItem and in the sellItem methods. 5. Implement the `addItem` method:   - Prompt the user to enter the name of the item to be added.   - Check if the item already exists in the inventory by searching for its name in the `items` array.   - If the item does not exist, add it to the list. Automatically it will have value of 1.   - If the item already exists, prompt the user to enter the quantity to be added.   - Update the quantity of the existing item in the `quantities` array.Sample Output6. Implement the `sellItem` method:   - Prompt the user to enter the name of the item to be sold.   - Check if the item exists in the inventory by searching for its name in the `items` array.   - If the item does not exist or if its quantity is 0, display an error message.   - If the item exists and its quantity is greater than 0, prompt the user to enter the quantity to be sold.   - Update the quantity of the sold item in the `quantities` array.Sample Output7. Compile and run the `InventorySystem` class to test the inventory management system.8. Test the system by adding items to the inventory, selling items, and displaying the current inventory.9. When exit is chosen, the program will terminate.

1/1

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.