Question
Create a c++ code of a delete menu that will delete an item from user input(cin) and check out menu that will show total amount
Create a c++ code of a delete menu that will delete an item from user input(cin) and check out menu that will show total amount to be paid. refer to the code below:
#include
#include
using namespace std;
struct Product {
string name;
double price;
};
struct Node {
Product data;
Node* next;
};
class ShoppingCart {
private:
Node* head;
int size;
public:
ShoppingCart() {
head = NULL;
size = 0;
}
void addProduct(Product product) {
Node* newNode = new Node;
newNode->data = product;
newNode->next = head;
head = newNode;
size++;
}
void displayCart() {
Node* current = head;
while (current != NULL) {
cout << current->data.name <<" -$" << current->data.price << endl;
current = current->next;
}
cout << endl;
}
int getSize(){
return size;
}
};
Product storeProducts[]={
{"T-shirt", 15.99},
{"Jeans", 39.99},
{"Shoes", 59.99},
{"Sunglasses", 29.99},
{"'Hat", 19.99}
};
void displayStoreProducts() {
cout << "Available products: " << endl;
for (int i = 0; i < 5; i++) {
cout << i+1 <<". " <<
storeProducts[i].name<<" - $" <<
storeProducts[i].price << endl;
}
}
int main() {
ShoppingCart cart;
int choice;
do {
cout << "Menu options:" << endl;
cout << "1. Display available products"<
cout << "2. Add a product to the cart" << endl;
cout << "3. Display the cart" << endl;
cout << "4. Delete an Item" << endl;
cout << "5. Check out" << endl;
cout <<"6. Exit" 1<< endl;
cout << "Enter your choice: "; cin >> choice;
switch (choice) {
case 1:
displayStoreProducts();
break;
case 2:
int productChoice;
cout << "Enter the number of the product you want to add to the cart:";
cin >> productChoice;
if (productChoice < 1 || productChoice > 5) {
cout << "Invalid choice." << endl;
} else {
cart.addProduct(storeProducts[productChoice-1]);
cout << "Product added to cart."
<< endl;
}
break;
case 3:
if (cart.getSize() == 0) {
cout << "Cart is empty." << endl;
}else {
cout << "Cart contents:" << endl;
cart.displayCart();
} break;
case 4:
break;
case 5:
cout << "Goodbye!" << endl;
break;
case 6:
cout << "Goodbye!" << endl;
break;
default:
cout << "Invalid choice." << endl; break;
}
} while (choice != 4);
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started