Instructions: Decide which items you would like to store in the list. (e.g. book
ID: 3887780 • Letter: I
Question
Instructions:
Decide which items you would like to store in the list. (e.g. books, bikes, gemstones, etc.) It can by any thing but not cars (I used that for the example) and not people.
Create a class that represents the item you chose. Here are some requirements your class needs to fulfill:
It can not be cars or people (see above)
The class needs to have at least 3 attributes you are keeping track of (e.g. year, make, model)
It needs to have two additional fields:
o a unique id that cannot be changed once created (like a primary key in a database)
o a static count that is used to initialize the id with a unique number Notice: At this point, we have a total of at least 5 attributes
It needs a parameterized constructor.
It allows the user to provide values for all the attributes you are keeping track of but not for the id nor for the count. (in our case that would be 3 parameters: year, make, and model)
The constructor creates a unique id – e.g. an 8 digit number for each item based on the static field count. (e.g. 12345678 + count++; In this example the smallest id would be 12345678)
It needs a getter for each of the fields except for the static count (in our case that would be 4 getters) The static count should not be exposed to another class. It is only used to initialize the unique id in the
constructor.
Hint: http://stackoverflow.com/questions/7221691/is-there-a-way-to-automatically-generate-getters-and-setters-in-eclipse It needs to override the method toString.
The method toString has no parameters and returns a string that represents an instance of the class.
This string needs to include the values of all fields except for count (e.g. 2015 Honda Accord id: 12345679 ) Labels should be added when the value itself is not self-explanatory. (e.g. id)
Sample Output :
Instructions continued:
At this point you have one class that represents one item (e.g. Book, Bike, Gemstone, etc.). This class does not contain the list of items nor should it include any print statements.
You still need at least one more class (optionally two) that include(s) the list of items, the menu with options, and the user communication (input / output)
Create one or two additional classes to do the following:
Declare an ArrayList of items ( each element in the list should be an instance of the new Java type you just wrote)
Initialize the list with four different elements (hard coded).
Display a menu with the following choices until the user chooses to exit:
Important: replace the word item and items with the actual type you store in the list. (e.g. Display all cars)
Provide the functionality chosen by the user.
Use private methods to print the menu and to display, find, add, or delete items. Using private methods in such a way structures the code and to makes the code more clear and easier to read.
When the user wants to find or delete an item s/he should be prompted for the id number.
If the user provides and id number that doesn’t exist, an appropriate message should be displayed.
(e.g. The id ... could not be found.)
If the id number could be found the item information should be displayed as part of the response
(e.g. 2015 Honda Accord id:12345679 if the item was found or 2015
Honda Accord has been deleted if it was deleted)
If the user enters an invalid choice from the menu (e.g. 9) an
appropriate message should be printed (e.g. Enter a selection 1 - 6) If the user enters 6 a message should be printed before terminating the program (e.g. Good bye)
Below you can find a sample output. This sample is about cars. Your output needs show elements of the item type you chose above.
Explanation / Answer
i have tried to make a c++ program for books keeping in mind al the conditions that have been given in the question:
#include <iostream.h>
#include <string.h>
#include <iomanip.h>
#include <conio.h>
class books
{
char **author,**title,**publisher;
int *amount;
static int count;
public:
books(); //constructor
void display_stock();
void search();
};
int books :: count=0;
books :: books()
{
author=new char*[10];
title=new char*[10];
publisher=new char*[10];
amount=new int[15];
for(int i=0;i<10;i++)
{
author[i]=newchar;
title[i]=newchar;
publisher[i]=newchar;
}
}
void books :: getdata()
{
cout<<" Enter name of the book";
cin>>title[count];
cout<<"Enter name of the author";
cin>>author[count];
cout<<"Enter name of the publisher";
cin>>publisher[count];
cout<<"Enter price of the book";
cin>>amount[count];
count++;
}
void books :: display_stock()
{
clrscr();
cout<<" ";
cout<<setw(15)<<"Book Name"<<setw(20)<<"Author Name"<<setw(20)<<"Publisher"<<setw(10)<<"amount"<<endl<<endl;
for(int i=0;i<count;i++)
{
cout<<setw(15)<<title[i]<<setw(20)<<author[i]<<setw(20)<<publisher[i]<<setw(10)<<amount[i]<<endl;
}
cout<<" The Total Number of Books are "<<count;
}
void books :: search()
{
clrscr();
char name[20],writer[20];
cout<<" Enter name of the book";
cin>>name;
cout<<"Enter name of the author";
cin>>writer;
for(int i=0;i<count;i++)
{
if((strcmp(title[i],name)==0) && (strcmp(author[i],writer)==0))
{
cout<<" Information about the Book Available is ";
cout<<"amount of that book is "<<amount[i];
goto skip;
}
}
cout<<" Book is not Available in the stock ";
skip:
}
void main()
{
clrscr();
books o1;
int choice;
while(1)
{
cout<<" Choose your choice ";
cout<<"1) Enter Information about the book ";
cout<<"2) See the Actual stock ";
cout<<"3)Search for a Particular book ";
cout<<"4) Exit ";
cout<<"Enter your choice:-";
cin>>choice;
switch(choice)
{
case 1 : o1.getdata();
break;
case 2 : o1.display_stock();
break;
case 3 : o1.search();
break;
case 4 : gotoout;
default: cout<<" Enter choice is invalid ";
}
}
out:
}