#include <iostream>
#include <string>
#include <iomanip> // for nice output
using namespace std;
// Fruit class
class Fruit {
private:
stringname;
int quantity;
double pricePerUnit;
public:
Fruit() {}
Fruit(string name, int quantity, double pricePerUnit) {
this->name = name;
this->quantity = quantity;
this->pricePerUnit = pricePerUnit;
}
void setName(string name) {
this->name = name;
}
void setQuantity(int quantity) {
this->quantity = quantity;
}
void setPricePerUnit(double pricePerUnit) {
this->pricePerUnit = pricePerUnit;
}
void print() {
cout << "| " << setw(10) << left << name << "| " << setw(10) << right << quantity << " | " << fixed << setprecision(2) << setw(10) << pricePerUnit << " |" << endl;
}
};
// amount of Fruit objects
int n = 0;
// array of Fruit objects
Fruit basket[15];
// addItems() function
void addItems() {
int m;
stringname;
int quantity;
double pricePerUnit;
cout << "Enter the number ofitems: ";
cin >> m;
n += m;
for(int i = 0; i < m; i++) {
cout << "Enter item name: ";
cin >> name;
basket[i].setName(name);
cout << "Enter item quantity: ";
cin >> quantity;
basket[i].setQuantity(quantity);
cout << "Enter item price per unit: ";
cin >> pricePerUnit;
basket[i].setPricePerUnit(pricePerUnit);
}
}
// displayItems() function
void displayItems() {
cout << "---------------------------------------------" << endl;
cout << "| No. | " << setw(10) << left << "Name" << "| " << setw(10) << "Quantity" << " | " << setw(10) << "Price" << " |" << endl;
cout << "---------------------------------------------" << endl;
for(int i = 0; i < n; i++) {
cout << "| " << left << setw(4) << i;
basket[i].print();
cout << "---------------------------------------------" << endl;
}
}
// displayParticularItem() function
void displayParticularItem()
{
int no;
cout << "Enter item no:";
cin >> no;
cout << "---------------------------------------------" << endl;
cout << "| No. | " << setw(10) << left << "Name" << "| " << setw(10) << "Quantity" << " | " << setw(10) << "Price" << " |" << endl;
cout << "---------------------------------------------" << endl;
cout << "| " << left << setw(4) << no;
basket[no].print();
cout << "---------------------------------------------" << endl;
}
// modifyItem() function
void modifyItem()
{
int no;
int choice;
stringname;
int quantity;
double pricePerUnit;
cout << "Enter item no:";
cin >> no;
while (choice != 4) {
cout << "Please select one of the choicesbelow:" << endl;
cout << "1. Modify name" << endl;
cout << "2. Modify quantity" << endl;
cout << "3. Modify price per unit" << endl;
cout << "4. Quit" << endl;
cin >> choice;
switch(choice) {
case 1: cout << "Enter item name:";
cin >> name;
basket[no].setName(name); break;
case 2: cout << "Enter itemquantity: ";
cin >> quantity;
basket[no].setQuantity(quantity); break;
case 3: cout << "Enter item priceper unit: ";
cin >> pricePerUnit;
basket[no].setQuantity(pricePerUnit); break;
}
}
}
int main() {
int choice;
while (choice != 5) {
// Menu
cout << "Please select oneof the choices below:" << endl;
cout << "1. Add items" << endl;
cout << "2. Display items" << endl;
cout << "3. Display particular item" << endl;
cout << "4. Modify item" << endl;
cout<< "5. Quit" << endl;
cin >> choice;
switch(choice) {
case 1: addItems(); break;
case 2: displayItems(); break;
case 3: displayParticularItem(); break;
case 4: modifyItem(); break;
}
}
return 0;
}
Comments
Leave a comment