Scan to open on your phone
Point a camera at the QR code - it opens this exact paste, no app needed.
https://codepastes.com/zzgrr566mxg6p2qrassignmentJava
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct Coffee
{
int id;
string name;
double price;
int quantity;
int temp;
};
void addCoffee(Coffee coffee[], int &count)
{
cout << "Enter Coffee ID: ";
cin >> coffee[count].id;
cin.ignore();
cout << "Enter Coffee Name: ";
getline(cin, coffee[count].name);
cout << "Enter Coffee Price: ";
cin >> coffee[count].price;
cout << "Enter Coffee Quantity: ";
cin >> coffee[count].quantity;
count++;
}
void displayCoffee(const Coffee coffee[], int count)
{
cout << left << setw(10) << "ID" << setw(20) << "Name" << setw(10) << "Price" << setw(10) << "Quantity" << endl;
cout << "--------------------------------------------------------" << endl;
for (int i = 0; i < count; i++)
{
cout << left << setw(10) << coffee[i].id
<< setw(20) << coffee[i].name
<< setw(10) << coffee[i].price
<< setw(10) << coffee[i].quantity
<< endl;
}
cout << "--------------------------------------------------------" << endl;
}
int getbyid(const Coffee coffee[], int count, int id)
{
for (int i = 0; i < count; i++)
{
if (coffee[i].id == id)
{
return i;
}
}
return -1;
}
void sortbyID(Coffee coffee[], int count)
{
for (int i = 0; i < count - 1; i++)
{
for (int j = 0; j < count - i - 1; j++)
{
if (coffee[j].id > coffee[j + 1].id)
{
Coffee temp = coffee[j];
coffee[j] = coffee[j + 1];
coffee[j + 1] = temp;
}
}
}
}
void calculateTotalPrice(const Coffee coffee[], int count)
{
double totalPrice = 0.0;
for (int i = 0; i < count; i++)
{
totalPrice += coffee[i].price * coffee[i].quantity;
}
cout << "Total Price of all Coffee: $" << totalPrice << endl;
}
void updateCoffeeByID(Coffee coffee[], int count, int id)
{
int index = getbyid(coffee, count, id);
if (index != -1)
{
cout << "Enter new Coffee Name: ";
cin.ignore();
getline(cin, coffee[index].name);
cout << "Enter new Coffee Price: ";
cin >> coffee[index].price;
cout << "Enter new Coffee Quantity: ";
cin >> coffee[index].quantity;
cout << "Coffee with ID " << id << " has been updated." << endl;
}
else
{
cout << "Coffee with ID " << id << " not found." << endl;
}
}
int main()
{
Coffee coffee[100];
int count = 0;
int choice;
do
{
cout << "1. Add Coffee" << endl;
cout << "2. Display Coffee" << endl;
cout << "3. Search Coffee by ID" << endl;
cout << "4. Sort Coffee by ID" << endl;
cout << "5. Calculate Total Price" << endl;
cout << "6. Update coffee by ID" << endl;
cout << "0. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
addCoffee(coffee, count);
break;
case 2:
displayCoffee(coffee, count);
break;
case 3:
{
int id;
cout << "Enter Coffee ID to search: ";
cin >> id;
int index = getbyid(coffee, count, id);
if (index != -1)
{
cout << "Coffee Found: " << endl;
cout << left << setw(10) << "ID" << setw(20) << "Name" << setw(10) << "Price" << setw(10) << "Quantity" << endl;
cout << left << setw(10) << coffee[index].id
<< setw(20) << coffee[index].name
<< setw(10) << coffee[index].price
<< setw(10) << coffee[index].quantity
<< endl;
}
else
{
cout << "Coffee with ID " << id << " not found." << endl;
}
break;
}
case 4:
sortbyID(coffee, count);
cout << "Coffee sorted by ID." << endl;
displayCoffee(coffee, count);
break;
case 5:
displayCoffee(coffee, count);
calculateTotalPrice(coffee, count);
break;
case 6:
int id;
cout << "Enter Coffee ID to update: ";
cin >> id;
updateCoffeeByID(coffee, count, id);
break;
case 0:
cout << "Exiting program." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 0);
return 0;
}
4,675 chars · codepastes.com