include include include shirtTypeh using namespace std void
#include <iostream>
#include <fstream>
#include \"shirtType.h\"
using namespace std;
void getInput(shirtType s[],ifstream& infile, int& numShirts);
int main()
{
int numShirts;
shirtType shirts[500];
ifstream infile;
infile.open(\"h:\\\\shirtlab.txt\");
getInput(shirts,infile,numShirts);
for(int i =0; i<numShirts;i++)
shirts[i].print();
infile.close();
return 0;
}
void getInput(shirtType s[], ifstream& infile, int& numShirts)
{
int id, q,index=0;
string c,size;
double p;
infile>>id;
infile.ignore(100,\'\ \');
getline(infile,c);
infile>>size>>p>>q;
while(infile)
{
s[index].setAll(id, c, size, p, q);
index++;
infile>>id;
infile.ignore(100,\'\ \');
getline(infile,c);
infile>>size>>p>>q;
}
numShirts=index;
}
See also the description of the class shirtType below:
#include <iostream>
#include <string>
using namespace std;
class shirtType
{
private:
int shirtID;
string color;
string size;
double price;
int quantity;
public:
void setAll(int i,string c,string s, double p, int q);
void print() const;
void newQuan();
shirtType();
shirtType(int,string,string,double,int);
};
shirtType::shirtType(int i,string c,string s, double p, int q)
{
color = c;
size = s;
if(i>0)
shirtID=i;
else
shirtID=0;
if(p>0.0)
price = p;
else
price=0.0;
if (q>=0)
quantity=q;
else
quantity = 0;
}
shirtType::shirtType()
{
shirtID=0;
color=\"\";
size=\"\";
price=0.0;
quantity=0;
}
void shirtType::setAll(int i,string c,string s, double p, int q)
{
color = c;
size = s;
if(i>0)
shirtID=i;
else
shirtID=0;
if(p>0.0)
price = p;
else
price=0.0;
if (q>=0)
quantity=q;
else
quantity = 0;
}
void shirtType::newQuan()
{
cout<<\"Enter the new quantity of shirts: \"<<endl;
cin>>quantity;
}
void shirtType::print() const
{
cout<<\"Shirt ID: \"<<shirtID<<endl;
cout<<\"Color: \"<<color<<endl;
cout<<\"Size: \"<<size<<endl;
cout<<\"Price: $\"<<price<<endl;
cout<<\"Quantity: \"<<quantity<<endl;
}
WHAT NEEDS TO BE DONE:
The main program will declare an array of shirtType objects (no more than 500), get file input for some number of array objects, and determine how many array objects were input. *DO NOT USE ANY STRUCTS*
Next, the program presents the user with a menu of choices:
a – Add a shirt
s – Sell a shirt
r – Restock a shirt
p – Print a list of all shirts
q – Quit
The program reads the user’s choice, calls a function to do the user’s choice, then displays the menu again. The program loops and does the user’s choices until the user selects q (Quit).
Required functions in main( ):
getInput function to read file input into the array of shirtType objects
Menu function: displays the menu and gets input of the user’s choice
Add a shirt function: gets keyboard input from the user for shirtID, color, size, price, and number of shirts on hand; the next available shirtType object in the array accesses its member function setAll with these values as parameters, then the number of shirts in the array is incremented.
Sell a shirt function: Asks the user to input a shirt ID of the shirt to be sold, then finds the corresponding shirt object in the array (or reports “not found” to the user and exits the function). Checks that the quantity on hand is greater than 0 and if so, the shirt object accesses the class member function that modifies the quantity on hand (newQuan) to reduce the quantity on hand by the number sold.
Restock a shirt function: Asks the user to input a shirt ID of the shirt being restocked, then finds the corresponding shirt object in the array (or reports “not found” to the user and exits the function). Gets user input of the number of shits to add. The shirt object accesses the class member function that modifies the quantity on hand (newQuan) by the number of shirts coming in. It outputs the new value of quantity on hand.
Print a list of all shirts function: Output the data values of all shirts in the array.
shirtType class functions: Note that you may modify the class definition of shirtType by inserting useful class functions as needed. For example, to search for a shirt ID in the array objects, you need a public function getShirtID which returns the shirtID data value. In the header file, put the function prototype in the class definition, and add the function definition below it
Solution
//shirtType.h
#include <iostream>
#include <string>
using namespace std;
class shirtType
{
private:
int shirtID;
string color;
string size;
double price;
int quantity;
public:
void setAll(int i,string c,string s, double p, int q);
void print() const;
void newQuan();
int changeQuan();
int getShirtId(int);
shirtType();
int search(int);
shirtType(int,string,string,double,int);
};
int shirtType::search(int sellId)
{
if(shirtID == sellId)
return 1;
else
return 0;
}
int shirtType::getShirtId(int i)
{
return shirtID;
}
shirtType::shirtType(int i,string c,string s, double p, int q)
{
color = c;
size = s;
if(i>0)
shirtID=i;
else
shirtID=0;
if(p>0.0)
price = p;
else
price=0.0;
if (q>=0)
quantity=q;
else
quantity = 0;
}
shirtType::shirtType()
{
shirtID=0;
color=\"\";
size=\"\";
price=0.0;
quantity=0;
}
void shirtType::setAll(int i,string c,string s, double p, int q)
{
color = c;
size = s;
if(i>0)
shirtID=i;
else
shirtID=0;
if(p>0.0)
price = p;
else
price=0.0;
if (q>=0)
quantity=q;
else
quantity = 0;
}
void shirtType::newQuan()
{
int q;
cout<<\"Enter the new quantity of shirts: \"<<endl;
cin>>q;
quantity += q;
}
int shirtType::changeQuan()
{
if(quantity > 0 )
{
quantity -= 1;
return 1;
}
{
return 2;
}
}
void shirtType::print() const
{
cout<<\"Shirt ID: \"<<shirtID<<endl;
cout<<\"Color: \"<<color<<endl;
cout<<\"Size: \"<<size<<endl;
cout<<\"Price: $\"<<price<<endl;
cout<<\"Quantity: \"<<quantity<<endl;
}
//shirtType.cpp
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include \"shirtType.h\"
#include <string>
using namespace std;
void getInput(shirtType s[],ifstream& infile, int& numShirts);
void addShirt(shirtType s[], int& numShirts);
void sellShirt(shirtType s[], int& numShirts);
void addstockShirt(shirtType s[], int& numShirts);
int main()
{
int numShirts;
char ch;
shirtType shirts[500];
ifstream infile;
infile.open(\"shirtlab.txt\");
getInput(shirts,infile,numShirts);
// cout<<numShirts;
// for(int i =0; i<numShirts;i++)
// shirts[i].print();
cout<<\"a – Add a shirt\ s – Sell a shirt\ r – Restock a shirt\ p – Print a list of all shirts\ q – Quit\ \";
cin>>ch;
do{
switch(ch)
{
case \'a\':
addShirt(shirts,numShirts);
break;
case \'s\':
sellShirt(shirts,numShirts);
break;
case \'r\':
addstockShirt(shirts,numShirts);
break;
case \'p\':
for(int i =0; i<numShirts;i++)
shirts[i].print();
break;
case \'q\':
infile.close();
exit(0);
break;
default:
cout<<\"wrong Input\";
}
cout<<\"a – Add a shirt\ s – Sell a shirt\ r – Restock a shirt\ p – Print a list of all shirts\ q – Quit\ \";
cin>>ch;
}while(ch!=\'q\');
infile.close();
return 0;
}
void getInput(shirtType s[], ifstream& infile, int& numShirts)
{
int id, q,index=0;
string c,size;
double p;
infile>>id;
infile.ignore(100,\'\ \');
getline(infile,c);
infile>>size>>p>>q;
while(infile)
{
s[index].setAll(id, c, size, p, q);
index++;
infile>>id;
infile.ignore(100,\'\ \');
getline(infile,c);
infile>>size>>p>>q;
}
numShirts=index;
}
void addShirt(shirtType s[], int& numShirts)
{
int id, q,index=numShirts;
// cout<<index;
string c,size;
double p;
cout<<\"Id:\";
cin>>id;
cout<<\"\ Shirt Color:\";
cin>>c;
// getline(cin,c);
cout<<\"\ Shirt size price and quantity:\";
cin>>size>>p>>q;
s[index].setAll(id, c, size, p, q);
index++;
numShirts=index;
// cout<<index;
}
void sellShirt(shirtType s[], int& numShirts)
{
int id, q,index=numShirts;
string c,size;
int i;
double p;
int sellId;
cout<<\"Insert ShirtId that we have to sell\ \";
cin>>sellId;
for(i =0; i<index;i++)
{
if(s[i].search(sellId))
{
if(s[i].changeQuan()==1)
{
cout<<\"Succesffully Sell\ \";
break;
}
else if(s[i].changeQuan()==2)
{
cout<< \"Not Sufficient Quantity\ \";
break;
}
}
}
if(i == numShirts)
cout<<\"\ Wrong Input of Shirt ID\ \";
}
void addstockShirt(shirtType s[], int& numShirts)
{
int id, q,index=numShirts;
string c,size;
int i;
double p;
int stockId;
cout<<\"Insert ShirtId that we have to add the stock\ \";
cin>>stockId;
for(i =0; i<index;i++)
{
if(s[i].search(stockId))
{
s[i].newQuan();
break;
}
}
if(i == numShirts)
cout<<\"\ Wrong Input of Shirt ID\ \";
}
//shirtlab.txt
1
red
M 100 5
2
green
L 200 3
3
red
L 230 0
4
yellow
S 220 4







