This is C++
Note, for part 2 of this assignment, you DO NOT NEED to use arrays or vectors. All changes, calculations, etc should be performed in place ( in the file). You may need one or two structures that temporary hold data needed to be displayed, changed, etc.
Part 2: Binary Files
Write a program that uses a structure to store the following inventory data in a file: The data can be either read from a text file or the keyboard
The program should have a menu that allows the user to perform the following tasks:
Input validation: The program should not accept quantities or wholesale or retail costs less than 0.
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class NODE{
private:
string name;// to store name
int qty;// number of quantity
double wholecost;// wholesale
value
double retailcost;// retail
value
public:
// constructor
NODE(){
name="";
qty=0;
wholecost=0;
retailcost=0;
}
// constructor with arguments
NODE(string a,int b,double c,double
d){
name=a;
qty=b;
wholecost=c;
retailcost=d;
}
// setter
void setName(string
nm){name=nm;}
void setQty(int q){qty=q;}
void setWholeCost(double
c){wholecost=c;}
void setRetailCost(double
c){retailcost=c;}
// getter
string
getName(){return(name);}
int getQty(){return(qty);}
double
getWholeCost(){return(wholecost);}
double
getRetailCost(){return(retailcost);}
};
// linked list node
class LinkedNode{
private:
NODE *data;// data
LinkedNode *next;// next link
public:
// constructor
LinkedNode(){
data=NULL;
next=NULL;
}
LinkedNode(string a,int b,double
c,double d){
data=new
NODE(a,b,c,d);
next=NULL;
}
// setter
void setData(NODE
*n){data=n;}
void setNext(LinkedNode
*l){next=l;}
// getter
NODE*
getData(){return(data);}
LinkedNode*
getNext(){return(next);}
};
// linked list
class LinkedList{
private:
// root node
LinkedNode *root;
public:
// constructor
LinkedList(){
root=NULL;
}
// add data in linked list
void add(string a,int b,double
c,double d){
LinkedNode
*node=new LinkedNode(a,b,c,d);
node->setNext(root);
root=node;
}
// find by name or first n
characters
LinkedNode* findByName(string
nm){
LinkedNode
*cur=root;
while(cur!=NULL){
size_t
found=cur->getData()->getName().rfind(nm);
if(found!= string::npos){
return cur;
}
cur=cur->getNext();
}
return
NULL;
}
// display list
void display(){
LinkedNode
*cur=root;
int i=0;
while(cur!=NULL){
cout<<"Record "<<(i+1);
cout<<"\nItem name :
"<<cur->getData()->getName();
cout<<"\nTotal wholesale value is
"<<(cur->getData()->getWholeCost()*cur->getData()->getQty());
cout<<"\nTotal retail value is
"<<(cur->getData()->getRetailCost()*cur->getData()->getQty());
cout<<"\nTotal quantity is
"<<cur->getData()->getQty()<<endl;
cur=cur->getNext();
}
}
};
int main(){
LinkedList ll;
while(1){
// menu
cout<<"1) Add record";
cout<<"\n2) Display record by
name";
cout<<"\n3) Display
records";
cout<<"\n4) Exit";
cout<<"\nUser choice :
";
int choice;
cin>>choice;
// choise between 1 to 4
if(choice>=1 &&
choice<=4){
if(choice==1){
cout<<"Enter name : ";
string a;
cin>>a;
cout<<"Enter number of quantity : ";
int b;
cin>>b;
cout<<"Enter Wholesale Cost : ";
double c;
cin>>c;
cout<<"Enter Retail Cost : ";
double d;
cin>>d;
ll.add(a,b,c,d);
cout<<"\nData Added\n";
}
if(choice==2){
cout<<"Enter name : ";
string a;
cin>>a;
LinkedNode* ln=ll.findByName(a);
if(ln!=NULL){
cout<<endl<<ln->getData()->getName();
cout<<"\nWholesale
value is "<<(ln->getData()->getWholeCost());
cout<<"\nRetail value
is "<<(ln->getData()->getRetailCost());
cout<<"\nQuantity is
"<<ln->getData()->getQty()<<endl;
}
else{
cout<<"\nNo record
found.\n";
}
}
if(choice==3){
ll.display();
}
if(choice==4){
break;
}
}else{
cout<<"Please choose correct option.\n";
}
}
cout<<"\nProgram End.\n";
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.