We\'re going to do an inventory management system. In the real world, this would
ID: 665544 • Letter: W
Question
We're going to do an inventory management system. In the real world, this would use a database, not a file. Inventory contains. THIS CAN BE VERY SIMPLE WITH ARRAYS
item names (or description), like "socks, - red" or "socks - blue". Thius has to be a character string that WILL include spaces.
item numbers that are unique to the item, similar to UPC codes, like 121821 or 128122. These are usually character strings.
a price, like 4.99, a double.
a quantity on hand, like 13, an integer.
Your code need to have the following features, all accessible from a menu.
Add a new inventory item to the data in memory. You'll type in the inventory number, description, quantity and price. This will also update the number of items on the menu screen.
List the inventory on the screen in neat columns. Display it in chucks of 15 items with a pause in between.
Search the inventory for an item by inventory number. If found, print the stored info, otherwise print 'not found'.
Calculate the total value on hand. The sum is the accumulation of all the (quantities * prices).
Save what's in memory to the disk
Overwrite what's in memory from the disk
The way your work this is you'll load a file, add, search, etc, then write it back to disk.
Ideas:
An array of objects works nicely if you read ahead, although this isn't required, see my code below (sorry about the indenting).
Here's a sample menu:
===========================
1) Add an item to memory
2) Search memory for an item
3) List what's in memory
4) Total value on hand
5) Save to a file
6) Read in from a file
0) Exit Program
56 items stored
}
Explanation / Answer
#include<iostream.h>
#include<conio.h>
class item
{
private:
char name[20];
int code[20];
float price[20];
int z;
public:
void add_(void);
void delete_(void);
void sell_(void);
void display_(void);
void initial(void){z=0;}
};
void item :: add_(void)
{
clrscr();
cout<<" Enter the item category : ";
cin>>name[z];
cout<<"Enter the item code : ";
cin>>code[z];
cout<<"Enter the price of the item: ";
cin>>price[z];
z++;
}
void item :: delete_(void)
{
int i,j;
clrscr();
cout<<" Please enter the item code:";
cin>>i;
for(j=0;j<z;j++)
{
if(code[j]==i)
{
price[j]=0;
cout<<" The item is deleted from the list.";
getch();
break;
}
}
}
void item :: sell_(void)
{
char a;
int i,j;
float total_price=0;
clrscr();
cout<<" Enter the item code:";
cin>>i;
for(j=0;j<z;j++)
{
if (code[j]==i)
{
if(price[j]==0)
{
cout<<" Sorry.There is no item left in this catefory.";
cout<<" Do you want to buy another item? y "<<endl;
cin>>a;
if (a=='y')
{
cout<<" Enter the item code:";
cin>>i;
j=-1;
}
else if (a=='Y')
{
cout<<" Enter the item code:";
cin>>i;
j=-1;
}
else
{
if (total_price!=0)
{
cout<<" Your total price is: ";
cout<<total_price<<" Taka."<<" Thank You.";
}
cout<<"Bye...bye...";
break;
}
}
else
{
cout<<"Item category is: "<<name[j];
cout<<" Item price is : "<<price[j]<<"tk."<<endl;
total_price+=price[j];
price[j]=0;
cout<<" Do you want to buy another item:y ";
cin>>a;
if (a=='y')
{
cout<<" Enter the item code:";
cin>>i;
j=-1;
}
else if (a=='Y')
{
cout<<" Enter the item code:";
cin>>i;
j=-1;
}
else
{ clrscr();
cout<<" Your total price is: ";
cout<<total_price<<"Taka."<<" Thank You.";
break;
}
}
}
}
getch();
}
void item :: display_()
{
int i,j=1;
clrscr();
for(i=0;i<z;i++)
{
cout<<j<<".Item category is: "<<name[i];
cout<<" Item code is : "<<code[i];
cout<<" Item price is : "<<price[i]<<"tk."<<endl<<endl;
j++;
}
getch();
}
int main ()
{
int i;
item shoping_mall;
clrscr();
shoping_mall.initial();
while(1)
{
cout<<" What do you want to do?"<<endl;
cout<<" 1: Add a new item.";
cout<<" 2: Delete an item.";
cout<<" 3: Sale an item.";
cout<<" 4: Display all items.";
cout<<" 5: Exit"<<endl;
cout<<" Choose a number:";
cin>>i;
switch(i)
{
case 1:
{
shoping_mall.add_();
break;
}
case 2:
{
shoping_mall.delete_();
break;
}
case 3:
{
shoping_mall.sell_();
break;
}
case 4:
{
shoping_mall.display_();
break;
}
case 5:
{
cout<<"Good Bye! Thank you.";
break;
}
}
if (i==5)
break;
clrscr();
}
getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.