For Programming Assignment 2, you will write a program to manage a bookstore. Yo
ID: 3807228 • Letter: F
Question
For Programming Assignment 2, you will write a program to manage a bookstore. Your program will be function-based and use a structured data type (struct). The struct will have the following members:
int ISBN
string Title
string Author
string Publisher
int Quantity
double Price
This will again be a menu-driven system. The following is your menu:
1: Read inventory from file
2: Show inventory
3: Add an entry
4: Delete an entry
5: Update an entry
6: Sort inventory
7: Write inventory to file and exit
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 books) 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):
ShowInventory - The information of all books added to the inventory will be displayed on the screen.
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 display the books and then ask the user which book to delete. They will give you the index number (starting at 1). Check to ensure they gave you a valid entry. You will then 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 - You will display the books and then ask the user which book to update. They will give you the index number (starting at 1). Check to ensure they gave you a valid entry. 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 entering 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 parameterMAX_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(). Place all files into a file folder named LastnamePA2, the zip the content and hand in a zip file!
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.
inventory.txt looks like this
20451
My First Book
Mark Lusk
Pearson Publishing
40
45.34
9780316
Brown Family
Mason Victor
Little Brown
36
105.99
1349877
Story of My Life
Norah M Jones
CreateSpace Independent Publishing Platform
20
18
Explanation / Answer
/*Password to run this program is 1234*/
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<iomanip.h>
#include<dos.h>
class group
{
protected:
struct books
{
char flag;
char bname[50];
char aname[50];
char pubname[50];
int price;
int noofbooks;
}b;
fstream file;
public:
group();
void insert();
void display();
void update();
void search();
void deletion();
void exit();
};
void main()
{
int choice;
char pass[20];
clrscr();
group g;
y: clrscr();
gotoxy(12,12);
cout<<"ENTER THE PASSWORD :";
cin>>pass;
if(strcmp(pass,"1234")==0)
goto z;
else
{
gotoxy(12,12);
cout<<"INCORRECT PASSWORD";
sound(1000);
delay(1000);
nosound();
clrscr();
goto y;
}
//group g;
z: do
{
clrscr();
gotoxy(30,4);
cout<<"*********************";
gotoxy(30,5);
cout<<"BOOK STORE MANAGEMENT";
gotoxy(30,6);
cout<<"*********************";
gotoxy(30,10);
cout<<"1.insert record";
gotoxy(30,11);
cout<<"2.display";
gotoxy(30,12);
cout<<"3.update";
gotoxy(30,13);
cout<<"4.search";
gotoxy(30,14);
cout<<"5.delete";
gotoxy(30,15);
cout<<"6.exit";
gotoxy(30,20);
cout<<"enter your choice :";
cin>>choice;
clrscr();
switch(choice)
{
case 1:
g.insert();
break;
case 2:
g.display();
break;
case 3:
g.update();
break;
case 4:
g.search();
break;
case 5:
g.deletion();
break;
case 6:
g.exit();
exit(1);
}
}while(choice != 0);
}
void group::group() //zero argument constructor
{
file.open("books.dat",ios::binary|ios::in|ios::out);
if(!file)
{
cout<<endl<<"unable to open the file";
exit();
}
}
//adds record to the file void group::insert()
{
char ch;
file.seekp(0L,ios::end);
do
{
cout<<endl<<"enter book name :";
gets(b.bname);
cout<<endl<<"enter author's name :";
gets(b.aname);
cout<<endl<<"enter publisher's name :";
gets(b.pubname);
cout<<endl<<"enter the book's price :";
cin>>b.price;
cout<<endl<<"enter the no of books :";
cin>>b.noofbooks;
b.flag=' ';
file.write((char*)&b,sizeof(b));
cout<<endl<<endl<<"add another record?(y/n)";
cin>>ch;
}while(ch=='y' || ch=='Y');
}
//displays all the books void group::display()
{
int j=1;
file.seekg(0L,ios::beg);
while (file.read((char*)&b,sizeof(b)))
{
if(b.flag != '*')
{
cout<<endl<<"RECORD NO :"<<j++<<endl<<"*************"<<endl<<"BOOK NAME :"<<b.bname<<endl<<"AUTHOR NAME :"<<b.aname
<<endl<<"PUBLISHER :"<<b.pubname<<endl<<"PRICE :"<<b.price
<<endl<<"COPIES :"<<b.noofbooks<<endl<<endl<<endl;
getch();
}
}
file.clear();
a:cout<<endl<<endl<<endl<<"press any key ....";
getch();
}
//update the books void group::update()
{
char code[20];
int count=0;
longint pos;
cout<<endl<<"enter the book name :";
gets(code);
file.seekg(0L,ios::beg);
while(file.read((char*)&b,sizeof(b)))
{
if(strcmp(b.bname,code)==0)
{
cout<<endl<<"enter the new book name :";
gets(b.bname);
cout<<endl<<"enter author's name :";
gets(b.aname);
cout<<endl<<"enter publisher's name :";
gets(b.pubname);
cout<<endl<<"enter the price :";
cin>>b.price;
cout<<endl<<"enter the no of books :";
cin>>b.noofbooks;
b.flag=' ';
pos=count*sizeof(b);
file.seekp(pos,ios::beg);
file.write((char*)&b,sizeof(b));
return;
}
count++;
}
cout<<endl<<"no book available with this name :"<<code;
cout<<endl<<"press any key ....";
getch();
file.clear();
}
//search for a given book void group::search()
{
char book[20],author[20];
int opt;
cout<<endl<<"search by :";
cout<<endl<<endl<<"1.book name ";
cout<<endl<<endl<<"2.author name ";
cout<<endl<<endl<<"enter your option :";
cin>>opt;
if(opt==1)
{
cout<<endl<<endl<<"enter book's name :";
gets(book);
file.seekg(0l,ios::beg);
while(file.read((char*)&b,sizeof(b)))
{
if(strcmp(b.bname,book)==0)
{
cout<<endl<<setw(20)<<b.bname<<setw(20)<<b.aname<<setw(10)<<b.pubname<<setw(10)<<b.price<<setw(10)<<b.noofbooks;
}
}
file.clear();
}
else
{
cout<<endl<<endl<<"enter author's name :";
gets(author);
file.seekg(0l,ios::beg);
while(file.read((char*)&b,sizeof(b)))
{
if(strcmp(b.aname,author)==0)
{
cout<<endl<<setw(20)<<b.bname<<setw(20)<<b.aname<<setw(10)<<b.pubname<<setw(10)<<b.price<<setw(10)<<b.noofbooks;
}
}
file.clear();
}
cout<<endl<<"press any key ...";
getch();
}
//deletes the record void group::deletion()
{
char book[20],name[20],publ[20];
longint pos;
int count=0;
cout<<endl<<"enter book's name :";
gets(book);
cout<<endl<<"enter author's name :";
gets(name);
cout<<endl<<"enter the publisher's name :";
gets(publ);
file.seekg(0l,ios::beg);
while(file.read((char*)&b,sizeof(b)))
{
if(strcmp(b.bname,book)==0 && strcmp(b.aname,name)==0 && strcmp(b.pubname,publ)==0)
{
b.flag='*';
pos=count*sizeof(b);
file.seekp(pos,ios::beg);
file.write((char*)&b,sizeof(b));
cout<<endl<<"record deleted successfully";
goto v;
}
count++;
}
cout<<endl<<"no book in file with name :"<<book<<" "<<"written by "<<name<<" "<<"and published by "<<publ;
v:cout<<endl<<"press any key.....";
getch();
file.clear();
}
void group::exit()
{
file.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.