For Programming Assignment 2 you will write a c program to m
For Programming Assignment 2 you will write a c++ program to manage a bookstore. This will again be a menu driven system. The following is your menu:
1: Read inventory from file
2: Add an entry
3: Delete an entry
4: Update an entry
5: Sort inventory
6: Write inventory to file and exit
Your program will be function-based and use a structured data type (struct). The struct called Book will have the following members:
string isbn
string title
string author
string publisher
int quantity
double price
Each menu item will make a call to the corresponding function (i.e., for case 1: you will call a function that reads the inventory from a file).
All of your books (structs of type Book) will be stored in a partially filled array of size MAX_SIZE, which will be set to 100. Since it is a partially filled array you will need to have a second variable that keeps track of the number of elements in your array, say size.
The following are the details for each of your menu options (Names are suggestions only but are descriptive):
addInventory - Add an entry will prompt the user for each piece of information and store it in a struct. After adding the struct to the array this case will automatically call the sort option. Before a new entry is added you will check to see if the array can hold one more entry. If not, print an error message and return to the main menu.
deleteInventory - You will delete an entry by simply moving all of the entries with higher indexes than the deleted item back one slot and then decreasing the value of size by one. You will not actually decrease the MAX_SIZE of the array. Just decrement size by one.
updateInventory - The only member of the inventory struct that you can update is the quantity, you will ask the user if they want to decrement, increment, or enter a new value. For decrement or increment you will simply subtract or add one (respectively) to the quantity. For enter a new value you will ask the user to enter a new number of books.
sortTitle - You will sort your array by title. You can use any sort algorithm that you like. Remember that your array is holding structs, so each entry in your array is a memory address that points to a struct. So when you do the move you can simply move the entire struct.
writeInventory - You will write the entire inventory out to a file called inventory.txt. Make sure that before you write to the file you sort your data. Don\'t forget that you need to close a file for reading before you try to write to that same file!
Each of your functions will take at least two parameters, the entire array, and the size. Adding inventory requires a third parameter MAX_SIZE. After each case completes you will return to the main menu (except for case 6 of course).
Your six functions (and any additional functions you want to include) will be contained in a header file called yourlastname.h. Also, the struct definition will be found in the yourlastname.h file. Then you will have a file called yourlastname.cpp that will include standard libraries that you need, include yourlastname.h and have an int main().
Main will contain your array of structs along with the variables size and MAX_SIZE. You will not have any global variables (no variables above main(). All of the “real” work will be done in the six functions. Remember that an array is always passed by reference when passing as a parameter in a function, so changes to the array in each of these functions, will change the array in main.
Solution
//stryct defntn to hold book
struct Book
{
string isbn;
string title;
string author;
string publisher;
int quantity;
double price;
};
//declarations for required function
int readInventory(Book books[],int size);
int addInventory(Book books[],int size,int MAX_SIZE);
int deleteInventory(Book books[],int size);
void updateInventory(Book books[],int size);
void sortTitle(Book books[],int size);
void writeInventory(Book books[],int size);
*********************************************
//required directives
#include <iostream>
#include <string>
#include<fstream>
using namespace std;
#include \"inventory.h\"
//display books in array
void displayBooks(Book books[],int size)
{
cout<<\"\ ******List of books in array are*****\ \";
for(int i=0;i<size;i++)
{
cout<<\"\ \"<<books[i].isbn<<\"\\t\"<<books[i].title<<\"\\t\"<<books[i].author<<\"\\t\"<<books[i].publisher<<\"\\t\"<<books[i].quantity<<\"\\t\"<<books[i].price;
}
}
//read books frm file
int readInventory(Book books[],int size)
{
string line;
int quantity;
double price;
ifstream myfile (\"inventory.txt\");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
size++;
books[size].isbn=line;
getline (myfile,line);
books[size].title=line;
getline (myfile,line);
books[size].author=line;
getline (myfile,line);
books[size].publisher=line;
myfile>>quantity;
books[size].quantity=quantity;
myfile>>price;
books[size].price=price;
}
displayBooks(books,size);
myfile.close();
}
else
cout << \"Unable to open file\";
cout<<size;
return size;
}
//add a new book into array and then sort
int addInventory(Book books[],int size,int MAX_SIZE)
{
string isbn,title,author,publisher;
int quantity;
double price;
if(size>=MAX_SIZE)
cout<<\"The inventory is full..\";
else
{
cout<<\"Enter isbn: \";
cin>>isbn;
cout<<\"Enter title: \";
cin>>title;
cout<<\"Enter author: \";
cin>>author;
cout<<\"Enter publisher: \";
cin>>publisher;
cout<<\"Enter quanity: \";
cin>>quantity;
cout<<\"Enter price: \";
cin>>price;
size++;
books[size].isbn=isbn;
books[size].title=title;
books[size].author=author;
books[size].publisher=publisher;
books[size].quantity=quantity;
books[size].price=price;
size++;
}
displayBooks(books,size);
return size;
}
//delete a book from array and duisplay list
int deleteInventory(Book books[],int size)
{
int index=-1;
string isbn;
Book tmp;
cout<<\"Enter isbn :\";
cin>>isbn;
for(int i=0;i<size;i++)
{
if(books[i].isbn==isbn)
{
index=i;
break;
}
}
if(index!=-1)
{
for(int i=index;i<size-1;i++)
{
tmp=books[i];
books[i]=books[index+1];
books[index+1]=tmp;
}
size-=1;
displayBooks(books,size);
}
else
cout<<\"Invalid isbn provided!!\";
return size;
}
//sort boks array based on titles
void sortTitle(Book books[],int size)
{
int i, j, flag = 1; // set flag to 1 to start first pass
Book temp;
for(i = 1; (i <= size) && flag; i++)
{
flag = 0;
for (j=0; j < (size -1); j++)
{
if (books[j+1].title > books[j].title) // ascending order simply changes to <
{
temp = books[j]; // swap elements
books[j] = books[j+1];
books[j+1] = temp;
flag = 1; // indicates that a swap occurred.
}
}
}
}
//update inventory by quantity in books array
void updateInventory(Book books[],int size)
{
int quantity=0;
int index=-1;
string isbn=\"\";
cout<<\"Enter isbn of the book : \";
cin>>isbn;
for(int i=0;i<size;i++)
{
if(books[i].isbn==isbn)
{
index=i;
cout<<\"Enter value of quantity: \";
cin>>quantity;
books[i].quantity=quantity;
}
}
if(index==-1)
cout<<\"Invalid ISBN provided!!\";
else
displayBooks(books,size);
}
//write books array to file
void writeInventory(Book books[],int size)
{
ofstream myfile;
myfile.open (\"inventory.txt\");
sortTitle(books,size);
for(int i=0;i<size;i++)
{
myfile<<books[i].isbn<<\"\ \"<<books[i].title<<\"\ \"<<books[i].author<<\"\ \"<<books[i].publisher<<\"\ \"<<books[i].quantity<<\"\ \"<<books[i].price;
}
myfile.close();
displayBooks(books,size);
}
//main function which accepts the user\'s inputs
int main()
{
ofstream myfile;
myfile.open (\"inventory.txt\");
for(int i=0;i<2;i++)
{
myfile<<\"a1\ a1\ a1\ a1\ 1\ 1\";
myfile<<\"a2\ a2\ a2\ a2\ 2\ 2\";
}
myfile.close();
const int MAX_SIZE=100;
Book books[MAX_SIZE];
bool flag=true;
int choice;
int count=0;
do
{
cout<<\"\ \\t1: Read inventory from file\ \\t2: Add an entry\ \\t3: Delete an entry\ \\t4: Update an entry\ \\t5: Sort inventory\ \\t6: Write inventory to file and exit\ \";
cout<<\"Enter your choice: \";
cin>>choice;
switch(choice)
{
case 1:count=readInventory(books,count);
break;
case 2:count=addInventory(books,count,MAX_SIZE);
break;
case 3:count=deleteInventory(books,count);
break;
case 4:updateInventory(books,count);
break;
case 5:sortTitle(books,count);
break;
case 6:writeInventory(books,count); flag=false;
break;
}
}while(flag);
return 0;
}





