Write a program that uses a structure to store the following inventory data in a
ID: 3538970 • Letter: W
Question
Write a program that uses a structure to store the following inventory data in a le:
Item Description
Quantity on Hand
Wholesale Cost
Retail Cost
Date Added to Inventory
The program should have a menu that allows the user to perform the following tasks:
Add new records to the file.
Display any record in the file.
Change any record in the file.
Input Validation: The program should not accept quantities, or wholesale or retail
costs, less than 0. The program should not accept dates that the programmer determines
are unreasonable.
And then Write a program that reads the data in the file created by the program above. The program
should calculate and display the following data:
The total wholesale value of the inventory
The total retail value of the inventory
The total quantity of all items in the inventory
Explanation / Answer
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
struct Date
{
int month, day, year;
};
struct Item
{
string description;
Date dateAdded;
int quantity;
float wholesale, retail;
};
Date String_To_MDY(char * date_ptr); //from lab1
char * Strchcpy(char * target, char * source, int ch); //from lab1
int Validate_Date(Date date); //from lab1
Item inputItem();
void outputItem(Item);
void readInventory(string,vector<Item>&);
void writeInventory(string,vector<Item>);
int displayMenu();
void displayRecords(vector<Item>);
int main()
{
vector<Item> inventory;
int menuOption;
string fileName;
readInventory("inventory.txt",inventory);
do{
menuOption=displayMenu();
switch(menuOption){
case 1:{
inventory.push_back(inputItem());
}
break;
case 2:{
int menuIndex;
do{
displayRecords(inventory);
cout<<"Enter # of record to view: ";
cin>>menuIndex;
if(menuIndex<0 || menuIndex>=inventory.size())
cout<<"Invalid Input!"<<endl;
}while(menuIndex<0 || menuIndex>=inventory.size());
outputItem(inventory[menuIndex]);
}
break;
case 3:{
int menuIndex;
do{
displayRecords(inventory);
cout<<"Enter # of record to change: ";
cin>>menuIndex;
if(menuIndex<0 || menuIndex>=inventory.size())
cout<<"Invalid Input!"<<endl;
}while(menuIndex<0 || menuIndex>=inventory.size());
inventory[menuIndex]=inputItem();
}
break;
case 4:{
writeInventory("inventory.txt",inventory);
cout<<"Bye!"<<endl;
}
break;
}
}while(menuOption!=4);
return 0;
}
Date String_To_MDY(char * date_ptr)
{
Date mdy_date;
char month[3],
day[3],
year[3];
char * ch_ptr;
ch_ptr = date_ptr;
ch_ptr = Strchcpy(month, ch_ptr, '/');
++ch_ptr;
ch_ptr = Strchcpy(day, ch_ptr, '/');
++ch_ptr;
Strchcpy(year, ch_ptr, '');
mdy_date.month = atoi(month);
mdy_date.day = atoi(day);
mdy_date.year = atoi(year);
return mdy_date;
}
char * Strchcpy(char * target, char * source, int ch)
{
while (*source != ch && *source != '')
{
*target = *source;
++target;
++source;
}
*target = '';
return source;
} /* End of Strchcpy() */
int Validate_Date(Date date)
{
int ny_days [13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int ly_days [13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (date.month < 1 || date.month > 12)
return 0;
if (date.day < 1)
return 0;
if (date.year % 4 == 0)
{
if (date.day > ly_days[date.month])
return 0;
}
else
if (date.day > ny_days[date.month])
return 0;
if (date.year > 100)
return 0;
return 1;
} /* End of Validate_Date() */
void outputItem(Item output)
{
cout<<" Description: "<<output.description;
cout<<" Date: "<<output.dateAdded.month<<"/"<<output.dateAdded.day<<"/"<<output.dateAdded.year;
cout<<" Quantity: "<<output.quantity;
cout<<" Wholesale: "<<output.wholesale;
cout<<" Retail: "<<output.retail<<endl<<endl;
}
Item inputItem()
{
Item temp;
char date[9];
cout<<"Description: ";
cin>>temp.description;
do{
cout<<"Date in mm/dd/yy form: ";
cin>>date;
temp.dateAdded=String_To_MDY(date);
if(!Validate_Date(temp.dateAdded))
cout<<"Invalid Entry :: Invalid Date! ";
}while(!Validate_Date(temp.dateAdded));
do{
cout<<"Quantity: ";
cin>>temp.quantity;
if(temp.quantity<0)
cout<<"Invalid Entry :: Negative! ";
}while(temp.quantity<0);
do{
cout<<"Wholesale: ";
cin>>temp.wholesale;
if(temp.wholesale<0)
cout<<"Invalid Entry :: Negative! ";
}while(temp.wholesale<0);
do{
cout<<"Retail: ";
cin>>temp.retail;
if(temp.retail<0)
cout<<"Invalid Entry :: Negative! ";
}while(temp.retail<0);
return temp;
}
void readInventory(string fileName,vector<Item>&inventory)
{
cout<<" :: Opening "<<fileName<<endl;
fstream file(fileName.c_str(),fstream::in);
if(file.good()){
cout<<" :: Success! ";
while(!file.eof()){
Item temp;
int readsize=0;
file>>readsize;
if(readsize){
file.read((char*)&temp.description,readsize);
//file>>readsize;
//file.read((char*)&temp.date,readsize);
file>>temp.dateAdded.day;
file>>temp.dateAdded.month;
file>>temp.dateAdded.year;
file>>temp.quantity;
file>>temp.retail;
file>>temp.wholesale;
inventory.push_back(temp);
cout<<" :: read item: "<<temp.description<<endl;
}
}
}
else
cout<<" :: Failed, a new file will be created when you Save & Exit. ";
file.close();
cout<<" :: Closed "<<fileName<<endl;
}
void writeInventory(string fileName,vector<Item> inventory)
{
cout<<" :: Opening "<<fileName<<endl;
fstream file(fileName.c_str(),fstream::out);
if(file.good()){
cout<<" :: Success! ";
for(int x=0;x<inventory.size();x++){
file<<sizeof(inventory[x].description);
file.write((char*)&inventory[x].description,sizeof(inventory[x].description));
//file<<sizeof(inventory[x].date);
//file.write((char*)&inventory[x].date,sizeof(inventory[x].date));
file<<inventory[x].dateAdded.day<<endl;
file<<inventory[x].dateAdded.month<<endl;
file<<inventory[x].dateAdded.year<<endl;
file<<inventory[x].quantity<<endl;
file<<inventory[x].retail<<endl;
file<<inventory[x].wholesale<<endl;
cout<<" :: wrote item: "<<inventory[x].description<<endl;
}
}
else
cout<<" :: Failure! ";
file.close();
cout<<" :: Closed "<<fileName<<endl;
}
int displayMenu()
{
int choice;
do{
cout<<"-----------------------------"<<endl;
cout<<"Homework 4: Inventory Program"<<endl;
cout<<"-----------------------------"<<endl;
cout<<"1: Add Record"<<endl;
cout<<"2: Display Record"<<endl;
cout<<"3: Change Record"<<endl;
cout<<"4: Save & Exit"<<endl;
cout<<"----------------------------- >";
cin>>choice;
if(choice<1 || choice>4)
cout<<"Invalid Input!"<<endl;
}while(choice<1 || choice>4);
return choice;
}
void displayRecords(vector<Item> inventory)
{
cout<<"Records:"<<endl;
for(int x=0;x<inventory.size();x++)
cout<<x<<": "<<inventory[x].description<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.