Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you please write the main function in this code the expert earlier didn't write it. in C++ please #include #include #include using namespace std;

Can you please write the main function in this code the expert earlier didn't write it. in C++ please

#include

#include

#include

using namespace std;

bool readInventory(string fileName, int &numRecords, string *PLU, double *price, string *productName, int *productSalesType, int *inventory) {

ifstream file(fileName);

if (!file) {

return false;

}

string line;

numRecords = 0;

while (getline(file, line)) {

++numRecords;

}

file.clear();

file.seekg(0, ios::beg);

PLU = new string[numRecords];

price = new double[numRecords];

productName = new string[numRecords];

productSalesType = new int[numRecords];

inventory = new int[numRecords];

int index = 0;

while (file >> PLU[index] >> productName[index] >> productSalesType[index] >> price[index] >> inventory[index]) {

++index;

}

file.close();

return true;

}

double checkout(int numRecords, string *PLU, double *price, int *inventory) {

double totalCost = 0;

string pluCode;

int quantity;

while (true) {

cout << "Enter PLU code (0 to end): ";

cin >> pluCode;

if (pluCode == "0") {

break;

}

int index = -1;

for (int i = 0; i < numRecords; ++i) {

if (PLU[i] == pluCode) {

index = i;

break;

}

}

if (index == -1) {

cout << "Invalid PLU code. Try again." << endl;

continue;

}

cout << "Enter quantity: ";

cin >> quantity;

while (quantity <= 0) {

cout << "Invalid quantity. Try again." << endl;

cout << "Enter quantity: ";

cin >> quantity;

}

if (inventory[index] < quantity) {

cout << "Not enough inventory. Selling only " << inventory[index] << endl;

quantity = inventory[index];

}

totalCost += quantity * price[index];

inventory[index] -= quantity;

}

return totalCost;

}

void printInventory(int numRecords, string *PLU, string *productName, int *productSalesType, double *price, int *inventory) {

cout << "PLU\tProduct Name\tSales Type\tPrice\tInventory" << endl;

for (int i = 0; i < numRecords; ++i) {

cout << PLU[i] << "\t" << productName[i] << "\t\t";

if (productSalesType[i] == 0) {

cout << "Unit\t";

} else {

cout << "Pound\t";

}

cout << price[i] << "\t" << inventory[i] << endl;

}

}

int main() {

int numRecords;

string *PLU = nullptr;

double *price = nullptr;

string *productName = nullptr;

int *productSalesType = nullptr;

int *inventory = nullptr;

string fileName = "inventory.txt";

if (!readInventory(fileName, numRecords, PLU, price, productName, productSalesType, inventory)) {

cout << "Error opening file: " << fileName << endl;

return 1;

}

cout << "Inventory:" << endl;

printInventory(numRecords, PLU, productName, productSalesType, price, inventory);

double totalCost = checkout(numRecords, PLU, price, inventory);

cout << "Total cost: $" << totalCost << endl;

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database And Transact SQL

Authors: Lucy Scott

1st Edition

1974679985, 978-1974679980

More Books

Students also viewed these Databases questions

Question

Brief the importance of span of control and its concepts.

Answered: 1 week ago

Question

What is meant by decentralisation?

Answered: 1 week ago