Challenge

Project 1

Purchase Class Documentation

Overview

The Purchase class is designed to manage the purchasing process involving different vendors and commodities. It allows users to input commodity and vendor data, compare prices, and generate a summary of the total purchase orders.

Class Members

comoditi structure:

Represents the details of a commodity, including the number of laptops, mouse, headsets, bags, and adapters.

compare method:

Compares the prices of commodities among different vendors and calculates the minimum total purchase order.

menu method:

Provides a user-friendly menu for interacting with the program. Options include entering commodity data, entering vendor data, showing details, writing details to a file, and exiting.

get_data_com method:

Takes user input to gather commodity details.

get_data_ven method:

Takes user input to gather vendor details.

writeData method:

Writes the commodity, vendor, and total purchase order details to a file.

writeHeaders_v and writeHeaders_c methods:

Write headers for the vendor and commodity sections in the output file.

writeVendorData and writeComData methods:

Write vendor and commodity data to the output file.

writeTotalPurchaseOrders method:

Calculates and writes the total purchase orders for each vendor.

User Manual

Getting Started

Compile the Code:

Use a C++ compiler to compile the source code.

Example: g++ -o purchase_program purchase_program.cpp

Run the Program:

Execute the compiled program.

Example: ./purchase_program

Menu Options

Enter Commodity Data:

Enter the quantity of laptops, mice, headsets, bags, and adapters needed.

Enter Vendor Data:

Enter the price details for each commodity from different vendors.

Show Details:

Compare vendor prices and display the vendor with the lowest price for each commodity.

Display the total purchase order amount for each vendor.

Write Details:

Save commodity, vendor, and total purchase order details to an output file ("output.txt" by default).

Exit:

Terminate the program.

Output File

The program generates an output file containing the following sections:

Order Details:

Displays the quantity of each commodity needed.

Vendor Details:

Shows the prices provided by each vendor for laptops, mice, headsets, bags, and adapters.

Total Purchase Orders:

Presents the total purchase orders for each vendor.

Example Usage
Enter commodity and vendor data.
View details to compare vendor prices.
Write details to an output file.
Exit the program.

Notes

Ensure valid numerical inputs are provided.

Source Code:

#include <iostream>

#include <iomanip>

#include <fstream>

#include <vector>

using namespace std;

class Purchase

{

public:

struct comoditi

{

int Laptop;

int Mouse;

int Headset;

int bag;

int adapter;

};

// void compare(comoditi *&vendor, comoditi &p_odr,int n);

void compare(vector<comoditi> &vendor, comoditi &p_odr, int n);

void menu(int num, comoditi &p_odr, vector<comoditi> &vendor, const string &filename)

{

int choice;

while (1)

{

cout << "-----------------------------------------" << endl;

cout << "-------Enter the option for entry--------" << endl;

cout << "-----------------------------------------" << endl;

cout << "-------- 1. Enter Comodity Data --------" << endl;

cout << "--------- 2. Enter Vendor Data ---------" << endl;

cout << "--------- 3. Show Details----------------" << endl;

cout << "--------- 4. Write Details---------------" << endl;

cout << "--------- 5. Exit ----------------------" << endl;

cout << "-----------------------------------------" << endl;

cout << endl;

cin >> choice;

switch (choice)

{

case 1:

p_odr = get_data_com();

break;

case 2:

vendor = get_data_ven(num);

break;

case 3:

compare(vendor, p_odr, num);

break;

case 4:

// exit(0);

writeData(vendor, p_odr, num, filename);

break;

case 5:

exit(0);

break;

default:

cout << "Invalid Input" << endl;

break;

}

}

}

comoditi get_data_com()

{

comoditi p_odr;

cout << "---------Enter Comodities to be Purchased--------------" << endl;

cout << endl;

cout << "The number of laptop need: ";

cin >> p_odr.Laptop;

cout << endl;

cout << "The number of mouse need: ";

cin >> p_odr.Mouse;

cout << endl;

cout << "The number of Headset need: ";

cin >> p_odr.Headset;

cout << endl;

cout << "The number of Laptop bag need: ";

cin >> p_odr.bag;

cout << endl;

cout << "The number of adapter need: ";

cin >> p_odr.adapter;

cout << endl;

return p_odr;

}

vector<comoditi> get_data_ven(int num)

{

// comoditi *vendor = new comoditi[num];

vector<comoditi> vendor(num);

// comoditi vendor[num];

for (int i = 0; i < num; i++)

{

cout << "Vendor No." << i << " quotation." << endl;

cout << "Price of laptop: ";

cin >> vendor[i].Laptop;

cout << endl;

cout << "Price of Mouse: ";

cin >> vendor[i].Mouse;

cout << endl;

cout << "Price of headset: ";

cin >> vendor[i].Headset;

cout << endl;

cout << "Price of laptop bag: ";

cin >> vendor[i].bag;

cout << endl;

cout << "Price of adapter: ";

cin >> vendor[i].adapter;

cout << endl;

}

return vendor;

}

void writeData(const vector<Purchase::comoditi> &vendor, const Purchase::comoditi &p_odr, int n, const string &filename)

{

ofstream outputFile(filename);

writeHeaders_c(outputFile);

writeComData(outputFile, p_odr);

outputFile << "-------------------------------------------------------------------------------------------------------------" << endl;

writeHeaders_v(outputFile);

// Write vendor data

for (int i = 0; i < n; ++i)

{

writeVendorData(outputFile, i, vendor[i]);

}

outputFile << "-------------------------------------------------------------------------------------------------------------" << endl;

// Write total purchase orders

writeTotalPurchaseOrders(outputFile, vendor, p_odr, n);

outputFile << "-------------------------------------------------------------------------------------------------------------" << endl;

outputFile.close();

}

void writeHeaders_v(ofstream &outputFile)

{

outputFile << " VENDOR Details " << endl;

outputFile << "-------------------------------------------------------------------------------------------------------------" << endl;

outputFile << "Vendor No" << setw(20) << "Laptop" << setw(20) << "Mouse" << setw(20) << "Headset" << setw(20) << "Bag" << setw(20) << "Adapter" << endl;

outputFile << "-------------------------------------------------------------------------------------------------------------" << endl;

}

void writeHeaders_c(ofstream &outputFile)

{

outputFile << " ORDER Details " << endl;

outputFile << "-------------------------------------------------------------------------------------------------------------" << endl;

outputFile << "Laptop" << setw(20) << "Mouse" << setw(20) << "Headset" << setw(20) << "Bag" << setw(20) << "Adapter" << endl;

outputFile << "-------------------------------------------------------------------------------------------------------------" << endl;

}

void writeVendorData(ofstream &outputFile, int row, const Purchase::comoditi &vendor)

{

outputFile << row << setw(30) << vendor.Laptop << setw(20) << vendor.Mouse << setw(20) << vendor.Headset << setw(20) << vendor.bag << setw(20) << vendor.adapter << endl;

}

void writeComData(ofstream &outputFile, const Purchase::comoditi &p_odr)

{

outputFile << p_odr.Laptop << setw(20) << p_odr.Mouse << setw(20) << p_odr.Headset << setw(20) << p_odr.bag << setw(20) << p_odr.adapter << endl;

}

void writeTotalPurchaseOrders(ofstream &outputFile, const vector<Purchase::comoditi> &vendor, const Purchase::comoditi &p_odr, int n)

{

outputFile << "-------------------------------------------------------------------------------------------------------------" << endl;

int totalLaptop[n], totalMouse[n], totalHeadset[n], totalBag[n], totalAdapter[n];

outputFile << "Total Purchase Orders(in Rs)" << setw(20) << "Laptop" << setw(20) << "Mouse" << setw(20) << "Headset" << setw(20) << "Laptop bag" << setw(20) << "Adapter" << endl;

for (int i = 0; i < n; ++i)

{

totalLaptop[i] = p_odr.Laptop * vendor[i].Laptop;

totalMouse[i] = p_odr.Laptop * vendor[i].Mouse;

totalHeadset[i] = p_odr.Laptop * vendor[i].Headset;

totalBag[i] = p_odr.Laptop * vendor[i].bag;

totalAdapter[i] += p_odr.Laptop * vendor[i].adapter;

outputFile << "Vendor:" << i << setw(40) << totalLaptop[i] << setw(20) << totalMouse[i] << setw(20) << totalHeadset[i] << setw(20) << totalBag[i] << setw(20) << totalAdapter[i] << endl;

}

}

};

void Purchase::compare(vector<comoditi> &vendor, comoditi &p_odr, int n)

{

cout << "_________ Comparision Data _________________" << endl;

cout << " Laptop Price " << endl;

comoditi least, Total[n];

int least_nL = 0, least_nM = 0, least_nH = 0, least_nB = 0, least_nA = 0, Total_Sum[n], least_tot = 0;

least.Laptop = vendor[0].Laptop;

least.Mouse = vendor[0].Mouse;

least.Headset = vendor[0].Headset;

least.bag = vendor[0].bag;

least.adapter = vendor[0].adapter;

for (int i = 0; i < n; i++)

{

cout << "Vendor No:" << i << setw(15) << vendor[i].Laptop << endl;

if (least.Laptop > vendor[i].Laptop)

{

least.Laptop = vendor[i].Laptop;

least_nL = i;

}

}

cout << " Mouse Price " << endl;

for (int i = 0; i < n; i++)

{

cout << "Vendor No:" << i << setw(15) << vendor[i].Mouse << endl;

if (least.Mouse > vendor[i].Mouse)

{

least.Mouse = vendor[i].Mouse;

least_nM = i;

}

}

cout << " Headset Price " << endl;

for (int i = 0; i < n; i++)

{

cout << "Vendor No:" << i << setw(15) << vendor[i].Headset << endl;

if (least.Headset > vendor[i].Headset)

{

least.Headset = vendor[i].Headset;

least_nH = i;

}

}

cout << " bag Price " << endl;

for (int i = 0; i < n; i++)

{

cout << "Vendor No:" << i << setw(15) << vendor[i].bag << endl;

if (least.bag > vendor[i].bag)

{

least.bag = vendor[i].bag;

least_nB = i;

}

}

cout << " adapter Price " << endl;

for (int i = 0; i < n; i++)

{

cout << "Vendor No:" << i << setw(15) << vendor[i].adapter << endl;

if (least.adapter > vendor[i].adapter)

{

least.adapter = vendor[i].adapter;

least_nA = i;

}

}

cout << "The Lowest Laptop Price is offered by Vendor:" << least_nL << " with price of Rs " << least.Laptop << endl;

cout << "The Lowest Mouse Price is offered by Vendor:" << least_nM << " with price of Rs " << least.Mouse << endl;

cout << "The Lowest Headset Price is offered by Vendor:" << least_nH << " with price of Rs " << least.Headset << endl;

cout << "The Lowest bag Price is offered by Vendor:" << least_nB << " with price of Rs " << least.bag << endl;

cout << "The Lowest adapter Price is offered by Vendor:" << least_nA << " with price of Rs " << least.adapter << endl;

for (int i = 0; i < n; i++)

{

Total[i].Laptop = p_odr.Laptop * vendor[i].Laptop;

Total[i].Mouse = p_odr.Laptop * vendor[i].Mouse;

Total[i].Headset = p_odr.Laptop * vendor[i].Headset;

Total[i].bag = p_odr.Laptop * vendor[i].bag;

Total[i].adapter = p_odr.Laptop * vendor[i].adapter;

Total_Sum[i] = Total[i].Laptop + Total[i].Mouse + Total[i].Headset + Total[i].bag + Total[i].adapter;

cout << " The Purchase order by vendor :" << i << " is Rs. " << Total_Sum[i] << endl;

}

least_tot = Total_Sum[0];

int min_v = 0;

for (int i = 0; i < n; i++)

{

if (least_tot > Total_Sum[i])

{

least_tot = Total_Sum[i];

min_v = i;

}

else

{

continue;

}

}

cout << "The Min Price of Order is given by Vendor " << min_v << " with total purchase order of Rs. " << least_tot << endl;

}

int main()

{

Purchase o_1;

Purchase::comoditi p_odr;

vector<Purchase::comoditi> vendor;

int num;

cout << "---------Enter Vendor data--------------" << endl;

cout << " Enter number of vendors required: ";

cin >> num;

cout << endl;

o_1.menu(num, p_odr, vendor, "output.txt");

return 0;

}

OUTPUT

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.