Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You can adapt this from http://pastebin.com/1qq6YkdW , the checkout simulator. Y

ID: 3533825 • Letter: Y

Question

You can adapt this from http://pastebin.com/1qq6YkdW, the checkout simulator. You already have code that reads the data from the file into an array of structs. Now do the following:


1. 1. Write a function that takes a pointer to the array of structs to a function, along with the number of items in the array. Return a pointer to the item that costs the most. Thus your function declaration would look like this:

Product *findExpensive(Product *, int count);

2. 2. Within your main function, call the function and display all of the information on that item. That is, show the PLU, name, etc

Explanation / Answer

#include <iostream>

#include <iostream>

#include <fstream>

#include <string>

#include <cstdlib>

#include <vector>

using namespace std;


class Product

{

private:

int plu;

string productName;

int type;

double price;

double inventory;

public:

Product();

Product(int, string, int, double, double);

int getPLU();

string getProductName();

int getType();

double getPrice();

double getInventory();

double updateInventory(double);

  

};


Product *findExpensive(Product pr*, int count)

{

int i=0,x=0;

int max=0;

while(pr[i]!=null)

{

if(max<pr[i].getPrice())

{

max=pr[i].getPrice();

x=i;

}

}

return *pr[x];

}

Product::Product()

{

plu = 0;

productName = "none yet";

type = 0;

price = 0;

inventory = 0;

}

Product::Product(int pl, string pn, int t, double pr, double i)

{

plu = pl;

productName = pn;

type = t;

price = pr;

inventory = i;

}

int Product::getPLU()

{ return plu;}


string Product::getProductName()

{ return productName;}


int Product::getType()

{ return type;}


double Product::getPrice()

{ return price;}


double Product::getInventory()

{ return inventory;}


double Product::updateInventory(double quantity)

{

if (quantity > inventory)

cout << "This item is not in stock." << endl;

else inventory -= quantity;

return inventory;

}



Product inv[100]; //array of 100 objects

int c = 100; //max product is 100


int menu(void) //user options

{

int option;

cout << "Enter 1 to begin checkout or 0 to exit" << endl;



cin >> option;

return option;

};

void updateFile() //output to a new file

{

ofstream newInvFile("newinventory.txt");


for (int p = 0; p < c; p++)

{

newInvFile << inv[p].getPLU() << " " << inv[p].getProductName() << " " << inv[p].getType() << " " << inv[p].getPrice() << " " << inv[p].getInventory() << endl;

}


}

void checkout(void)

{

double total = 0;

double quantity;

int input;

int PLU = 0;


do

{

PLU = 0;

cout << "Enter PLU code or (0) to exit" << endl;

cin >> input;

if (input == 0)

break;

for (int p = 0; p < c; p++)

{

if (inv[p].getPLU() == input)

{

PLU = p;

break;

}

}


if (inv[PLU].getType() == 1)// Determine whether product is sold by weight

{

cout << "Weight: ";

}

else

{

cout << "Quantity: ";

}


cin >> quantity;

total += quantity * inv[PLU].getPrice();

inv[PLU].updateInventory(quantity);

}

while (input != 0);


cout << "Total: $" << total << endl;


if (total > 50) //apply discount if total is over $50

{

total = total * 0.95;

cout << "YAY! Your purchase of over $50 qualifies for a 5% discount. Total: $" << total << endl;

}


}

int main()

{

int plu;

string productName;

int type;

double price;

double inventory;

c = 0;


ifstream original ("product.txt"); //read from original product file

if (original.is_open())

{

while (!original.eof())

{

original >> plu >> productName >> type >> price >> inventory;


Product temp = Product(plu, productName, type, price, inventory);

inv[c] = temp;

c++;

}

original.close();

}

int option;


do

{

cout << "WELCOME!" << endl;


option = menu();

if (option == 1)

checkout();

else

updateFile();

}

while(option != 0);

temp=*findExpensive(inv, c);

cout<<"Most expensive item:- PLU:"<<temp.getPLU()<<" Name:"<<getProductName()<<" Type:"<<getType()<<" Price:"<<getPrice()<<" Inventory:"<<getInventory();

system ("pause");

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote