Suppose your company is having an inventory sale. You are selling four different
ID: 3817197 • Letter: S
Question
Suppose your company is having an inventory sale. You are selling four different items by case. Example, Coke (ID number 1), (ID number 2), Canada Dry (ID number 3), and Dr. Pepper (ID number 4) by the case. Write a program to do the following: a. Read in the case inventory for each brand for the start of the week; b. Process all weekly sales and purchase records for each brand; and Display the final inventory. Each transaction will consist of two items. The first item will be the brand identification number (integer). The second will be the amount purchased (a positive integer number value) or the amount sold (a negative integer value). The weekly inventory for each brand (for the start of the week) will also consist of two items: the identification and initial inventory for the brand. For now, you may assume that you always have sufficient foresight to prevent depletion of your inventory for any brand.Explanation / Answer
c++ code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string line;
ifstream myfile ("input.txt");
std::vector<int> available(4);
available[0] = 0;available[1] = 0;
available[2] = 0;available[3] = 0;
std::vector<int> sold(4);
sold[0] = 0;sold[1] = 0;
sold[2] = 0;sold[3] = 0;
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
string buf; // Have a buffer string
stringstream ss(line); // Insert the string into a stream
vector<string> tokens; // Create vector to hold our words
while (ss >> buf)
tokens.push_back(buf);
int id = atoi(tokens[0].c_str()) - 1;
int q = atoi(tokens[1].c_str());
if( q > 0) //purchase
{
available[id] = available[id] + q;
}
else //sold
{
if(available[id] - q >= 0)
{
available[id] = available[id] - abs(q);
sold[id] = sold[id] + abs(q);
}
else
{
cout << "Can not sell " << abs(q) << "Items right now! We only have " << available[id] << " items with id " << id+1 <<" available!" << endl;
}
}
}
myfile.close();
}
else
{
cout << "Unable to open file" << endl;
exit(1);
}
for (int i = 0; i < 4; ++i)
{
cout << "ID = " << i+1 << " Items sold = :" << sold[i] <<" Items available = :" << available[i] << endl;
}
return 0;
}
Sample input.txt
1 145
2 153
3 160
4 159
1 -100
2 -100
3 -100
4 -100
1 -45
1 45
Sample Output:
ID = 1 Items sold = :145 Items available = :45
ID = 2 Items sold = :100 Items available = :53
ID = 3 Items sold = :100 Items available = :60
ID = 4 Items sold = :100 Items available = :59
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.