1. Program to process a shopping list for a departmental store using a class. The list includes details such as code number and price of each item, and operations like adding, deleting items, and printing the total value of an order.
#include <iostream.h>
#include<conio.h>
class Item {
public:
int code;
float price;
void inputDetails() {
cout << "Enter item code: ";
cin >> code;
cout << "Enter item price: ";
cin >> price;
}
void displayDetails() {
cout << "Item Code: " << code << endl;
cout << "Item Price: " << price << endl;
}
};
class ShoppingList {
public:
Item items[100];
int count;
ShoppingList() {
count = 0;
}
void addItem() {
if (count < 100) {
items[count].inputDetails();
count++;
cout << "Item added successfully." << endl;
} else {
cout << "Shopping list is full." << endl;
}
}
void deleteItem(int code) {
int i, found = 0;
for (i = 0; i < count; i++) {
if (items[i].code == code) {
found = 1;
break;
}
}
if (found) {
for (int j = i; j < count - 1; j++) {
items[j] = items[j + 1];
}
count--;
cout << "Item deleted successfully." << endl;
} else {
cout << "Item not found." << endl;
}
}
void printTotal() {
float total = 0;
for (int i = 0; i < count; i++) {
total += items[i].price;
}
cout << "Total value of the order: " << total << endl;
}
};
int main() {
ShoppingList list;
int choice;
do {
cout << "\n1. Add Item\n2. Delete Item\n3. Print Total\n4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
list.addItem();
break;
case 2: {
int code;
cout << "Enter item code to delete: ";
cin >> code;
list.deleteItem(code);
break;
}
case 3:
list.printTotal();
break;
case 4:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice." << endl;
}
} while (choice != 4);
getch();
return 0;
}