Question: iN C++ given code needs viewby Price So when outputted it will view by PartID, chronological order, and Price #include using namespace std; class DB

iN C++

given code needs viewby Price

So when outputted it will view by PartID, chronological order, and Price

#include using namespace std; class DB {

public: void store(); void viewChronologically(); void sortbyPartID(); void viewbyPartID();

private: class Part { public: int sl; // declaring a variable of integer type for defining store() int ID; float Price; int Quantity; Part(void){}; Part(int Partnum, float rate, int quant) { ID = Partnum; Price = rate; Quantity = quant; } // appropriate initialization of class Part's data members ID, Price and Quantity

void store(int s) // passing an integer variable as an argument { // taking ID, Price , Quantity as input as per the given output format sl = s; cout << "Enter ID: "; cin >> ID; cout << "Enter Unit Price: "; cin >> Price; cout << "Enter Inventory Quantity: "; cin >> Quantity; };

void print() { cout << "Part ID: " << ID << " Unit Price = " << Price << " Inventory = " << Quantity << endl; }; };

Part n1, n2, n3;

Part *IDp1, *IDp2, *IDp3;

void swap(Part *a, Part *b); void sortbyChronologicalOrder(); }; void DB::store() {

cout << "Input ID, price, and quantity for three parts, as prompted" << endl;

cout << "Part 1:" << endl; n1.store(1);

cout << "Part 2:" << endl; n2.store(2);

cout << "Part 3:" << endl; n3.store(3);

IDp1 = &n1; IDp2 = &n2; IDp3 = &n3;

// Prints a thank you message after storing as indicated in the output format cout << "Thank you. The three parts have been securely stored in the database." << endl;

};

void DB::viewChronologically() { sortbyChronologicalOrder(); cout << "The three items in the database (in chronological order) are: " << endl;

n1.print(); n2.print(); n3.print(); };

// Definition for the swap method in the DB class: void DB::swap(DB::Part *a, Part *b) // passing a pointer variable of class type as arguments { Part temp; // declaration of a variable temp as class Part type temp = *a; *a = *b; *b = temp; };

void DB::sortbyPartID() {

if (IDp1->ID > IDp2->ID) swap(IDp1, IDp2);

if (IDp2->ID > IDp3->ID) { swap(IDp2, IDp3);

if (IDp1->ID > IDp2->ID) swap(IDp1, IDp2); } };

void DB::sortbyChronologicalOrder() { if (IDp1->sl > IDp2->sl) swap(IDp1, IDp2);

if (IDp2->sl > IDp3->sl) { swap(IDp2, IDp3);

if (IDp1->sl > IDp2->sl) swap(IDp1, IDp2); } }

void DB::viewbyPartID() { //print the data of the three items present in the database by calling print() using each object of class Part n1,n2,n3 cout << "The three items in the database (sorted by part ID) are:" << endl;

n1.print(); n2.print(); n3.print(); };

int main() {

DB DB1; DB1.store(); DB1.sortbyPartID(); DB1.viewbyPartID(); DB1.viewChronologically();

cout << "Have a good day! Bye!" << endl;

exit(0); };

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!