Question: This is a menu-driven program that uses following options: (This is all 1 assignment) a) Add a new book to the list. The book details
This is a menu-driven program that uses following options: (This is all 1 assignment)
a) Add a new book to the list. The book details (book name, author name, publication year, number of copies) are given as function arguments. You should add the book to the list only if the book is not already present in the list and there is space in the array to add a new book.
b) Display the book information of all books in the list. See expected output.
c) Sort the book in the list by book name.
d) Find the newest book in the list. You have to necessarily use the Book pointer declared in the function and delete it before exiting the function.
#include
using namespace std;
class Book {
private:
string name, author; // data members
int pubYear, noOfCopies;
public:
Book(); // constructor
// member functions. Fucntion definition in book.cpp
void setName(string name_input);
void setAuthor(string author_input);
void setpubYear(int pubYear_input);
void setNoOfCopies(int noOfCopies_input);
void displayBookInfo();
string getName();
int getPubYear();
string getAuthor();
int getNoOfCopies();
};
--------------------------------------------------------------------------
// You are given a partially completed program which consist of a class 'Book' defined in book.h
// hw09q1.c (this file) creates an array of objects 'b[]' and uses a menu driven program to add a book, display book info,
// sort the book list and to find the newest book(by publication year).
// To begin, you should trace through the given code and understand how it works.
// Please read the instructions above each required function and follow the directions carefully.
// If you modify any of the given code, the return types, or the parameters, you risk getting compile error.
// You are not allowed to modify main ().
// ***** WRITE COMMENTS FOR IMPORANT STEPS OF YOUR CODE. *****
// ***** GIVE MEANINGFUL NAMES TO VARIABLES. *****
#include "book.h"
#include
#include
#define MAX_BOOKS 5
using namespace std;
// forward declaration of functions (already implmented)
void executeAction(char);
// functions that need implementation:
// in book.cpp :
// Q1 Book constructor // 5 points
// Q2 Book member functions // 15 points
// in this file (hw09q1.cpp) : Q3 through Q6
int addBook(string name_input, string author_input, int pubYear_input, int noOfCopies_input); // 10 points
void displayList(); // 5 points
void sortList(); // 10 points
void newestBook(); // 5 points
Book b[MAX_BOOKS]; // array of objects
int counter = 0; // number of books in the list
int main()
{
char choice = 'i'; // initialized to a dummy value
do
{
cout
cout
cout
cout
cout
cout
cout
cin >> choice;
cin.ignore(); // ignores the trailing
executeAction(choice);
} while (choice != 'q');
return 0;
}
// Ask for details from user for the given selection and perform that action
// Read the function case by case
void executeAction(char c)
{
string name_input, author_input;
int pubYear_input, noOfCopies_input, result = 0;
switch (c)
{
case 'a': // add book
// input book details from user
cout
getline(cin, name_input);
cout
getline(cin, author_input);
cout
cin >> pubYear_input;
cin.ignore();
cout
cin >> noOfCopies_input;
cin.ignore();
// add the book to the list
result = addBook(name_input, author_input, pubYear_input, noOfCopies_input);
if (result == 0)
cout
else
cout
break;
case 'd': // display the list
displayList();
break;
case 's': // sort the list
sortList();
break;
case 'n': // find and display newest book
newestBook();
break;
case 'q': // quit
break;
default: cout
}
}
// Q3 addBook (10 points)
// This function adds a new book with the details given in function arguments.
// Add the book in 'b' (array of objects) only if there is remaining capacity in the array and if the book does not already exist in the list
// This function returns 1 if the book is added successfully, else it returns 0 for the cases mentioned above.
int addBook(string name_input, string author_input, int pubYear_input, int noOfCopies_input)
{
return 1; // edit this line if needed
}
// Q4 displayList (5 points)
// This function displays the list of books.
// Parse the array and display the details of all books in the array. See expected output given in question file.
void displayList()
{
}
// Q5 sortList (10 points)
// This function sorts the books in the list alphabetically by book name.
// You may use the 'temp' object for sorting logic, if needed.
void sortList()
{
Book temp;
// enter your code here
cout
}
// Q6 newestBook (5 points)
// This function uses 'newestBook' pointer to store the book details of the newest book in the list.
// Parse the array to find the newest book (by publication year) and copy its content to 'newestBook' object.
// Then display the book details of 'newestBook' object and delete the 'newestBook' object.
// NOTE: You necessarily have to use the 'newestBook' object to store the newest book details in it and delete it after displaying.
// You should not just print the newest book details from 'b[]' object.
void newestBook()
{
Book* newestBook = new Book;
// enter your code here
}



Expected outputs: addBook: SE240 HW9 Please enter your selection: a: add a new book to the list d: display entire list of books s: sort the list by book name n: find and display newest book q: quit Please enter book name: java programming lease enter author name: Tony Stark lease enter publication year: 2010 Please enter no. of copies of book: 10 Book successfully added to the list
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
