In: Computer Science
Introduction
Write in C++ at the Linux command line a program that is the same as the previous collection app project but now uses a class to store the items and also can save the items to a file that can be read back into the array by the user when the program is re-started.
You can use your project 1 submission as a starting point or you can do something new as long as it meets the listed requirements.
Again, try and keep it reasonably simple to do by the due date but still meets the requirements listed below. You will be graded mainly on how well you follow the listed requirements.
REQUIREMENTS
PLEASE SEE THE GENERAL REQUIREMENTS FOR FURTHER REQUIREMENTS NEEDED. BELOW IS NOT ALL THE REQUIREMENTS NEEDED IN YOUR CODE.
Your program should have as a minimum the following tasks:
a. Allow the user to add entries to the collection. Check for valid data and let the user re-enter if they enter invalid data. Please let the user know what is valid data. Remember that user input should NOT cause your program to malfunction or crash.
b. Allow the user to print out the entire collection. Should not print out blank entries or invalid entries.
c. Allow the user to delete an entry from the collection. Allow users to
choose which entry to delete. If the entry is not found, do print out an error message, don't just do nothing. You can ask the user to re-enter or you can just return to the main function.
d. Allow the user to read in items from a file. Ask the user for a file name, open the file and read in the items from the file into the array. f there are already items in the collection, append to the end of the array. If the file doesn't exist, print out an error message and return the main loop.
e. Allow the user to write out items to a file. Ask the user for a file name (if the file exists; just overwrite the file). The information should be written in a format that can be read back into the program when the user wants to read in the file. Basically, while grading your program I plan to create some items, write the items out to a file, exit the program, re-start the program, then read back in the items. If your program can't do this correctly, you will get points off.
Other tasks can be added but only if the above tasks are also present and working correctly. Extra tasks will be graded for completeness and correctness so make sure they work too.
The program also needs to have the following code structures:
a) An array of objects. The class needs at least 3 member variables. At least one member variable should be numeric (integer or floating point) and at least one member variable should be a string type (C-Strings or the builtin string class). Note that this class is for ONE and only ONE item in the list; don't store a list of items in this class.
b) The code for the class should be in two separate files: A header file (.h) and an implementation file (.cpp). These are in addition to the main.cpp file so in all you need to turn in at least three code files.
c) Member variables must be declared private. Only member functions can be declared public.
d) Must have a 'setter' and a 'getter' for each member variable. You can have other member functions in the class as long as they are doing something with the member variables for one item.
e) Must have at least a default constructor. You can have other parameterized constructors but at least the default one should be there. Initialize strings to an empty string and numbers to zero.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Example Output Runs
Here are 4 runs. Note that any files written out can be read back
in when running the program again. Also, reading in will append to
the end of the array.
----------------------
RUN 1
----------------------
*** BOOK COLLECTION ***
== Main Menu ==
1 -> read in books from a file
2 -> write out books to a file
3 -> add books to the collection
4 -> print all books
5 -> delete a book from the collection
6 -> quit the program
Please enter a choice (1 to 6)
4
no books in collection
== Main Menu ==
1 -> read in books from a file
2 -> write out books to a file
3 -> add books to the collection
4 -> print all books
5 -> delete a book from the collection
6 -> quit the program
Please enter a choice (1 to 6)
1
Enter file name? books.txt
== Main Menu ==
1 -> read in books from a file
2 -> write out books to a file
3 -> add books to the collection
4 -> print all books
5 -> delete a book from the collection
6 -> quit the program
Please enter a choice (1 to 6)
4
---- 0 ----
title: The Name Of The Wind
author: Patrick Roth
year: 2007
---- 1 ----
title: Lord Of The Rings: The Fellowship of the Ring
author: J. R. R. Tolkien
year: 1954
---- 2 ----
title: Mistborn
author: Brandon Sanderson
year: 2006
---- 3 ----
title: A Game Of Thrones
author: George R. R. Martin
year: 1996
---- 4 ----
title: Harry Potter And The Sorcerer's Stone
author: J.K. Rowling
year: 1997
---- 5 ----
title: A Wizard of Earthsea
author: Ursula K. Leguin
year: 1968
== Main Menu ==
1 -> read in books from a file
2 -> write out books to a file
3 -> add books to the collection
4 -> print all books
5 -> delete a book from the collection
6 -> quit the program
Please enter a choice (1 to 6)
3
enter title?
The Way of Kings
enter author?
Brandon Sanderson
enter year? (year should be larger than 0)
2011
Do you want to continue? ('y' or 'n')
n
== Main Menu ==
1 -> read in books from a file
2 -> write out books to a file
3 -> add books to the collection
4 -> print all books
5 -> delete a book from the collection
6 -> quit the program
Please enter a choice (1 to 6)
4
---- 0 ----
title: The Name Of The Wind
author: Patrick Roth
year: 2007
---- 1 ----
title: Lord Of The Rings: The Fellowship of the Ring
author: J. R. R. Tolkien
year: 1954
---- 2 ----
title: Mistborn
author: Brandon Sanderson
year: 2006
---- 3 ----
title: A Game Of Thrones
author: George R. R. Martin
year: 1996
---- 4 ----
title: Harry Potter And The Sorcerer's Stone
author: J.K. Rowling
year: 1997
---- 5 ----
title: A Wizard of Earthsea
author: Ursula K. Leguin
year: 1968
---- 6 ----
title: The Way of Kings
author: Brandon Sanderson
year: 2011
== Main Menu ==
1 -> read in books from a file
2 -> write out books to a file
3 -> add books to the collection
4 -> print all books
5 -> delete a book from the collection
6 -> quit the program
Please enter a choice (1 to 6)
2
Enter file name: books2.txt
== Main Menu ==
1 -> read in books from a file
2 -> write out books to a file
3 -> add books to the collection
4 -> print all books
5 -> delete a book from the collection
6 -> quit the program
Please enter a choice (1 to 6)
4
---- 0 ----
title: The Name Of The Wind
author: Patrick Roth
year: 2007
---- 1 ----
title: Lord Of The Rings: The Fellowship of the Ring
author: J. R. R. Tolkien
year: 1954
---- 2 ----
title: Mistborn
author: Brandon Sanderson
year: 2006
---- 3 ----
title: A Game Of Thrones
author: George R. R. Martin
year: 1996
---- 4 ----
title: Harry Potter And The Sorcerer's Stone
author: J.K. Rowling
year: 1997
---- 5 ----
title: A Wizard of Earthsea
author: Ursula K. Leguin
year: 1968
---- 6 ----
title: The Way of Kings
author: Brandon Sanderson
year: 2011
== Main Menu ==
1 -> read in books from a file
2 -> write out books to a file
3 -> add books to the collection
4 -> print all books
5 -> delete a book from the collection
6 -> quit the program
Please enter a choice (1 to 6)
6
Thank you for using our program!
----------------------
RUN 2
----------------------
*** BOOK COLLECTION ***
== Main Menu ==
1 -> read in books from a file
2 -> write out books to a file
3 -> add books to the collection
4 -> print all books
5 -> delete a book from the collection
6 -> quit the program
Please enter a choice (1 to 6)
4
no books in collection
== Main Menu ==
1 -> read in books from a file
2 -> write out books to a file
3 -> add books to the collection
4 -> print all books
5 -> delete a book from the collection
6 -> quit the program
Please enter a choice (1 to 6)
1
Enter file name? books2.txt
== Main Menu ==
1 -> read in books from a file
2 -> write out books to a file
3 -> add books to the collection
4 -> print all books
5 -> delete a book from the collection
6 -> quit the program
Please enter a choice (1 to 6)
4
---- 0 ----
title: The Name Of The Wind
author: Patrick Roth
year: 2007
---- 1 ----
title: Lord Of The Rings: The Fellowship of the Ring
author: J. R. R. Tolkien
year: 1954
---- 2 ----
title: Mistborn
author: Brandon Sanderson
year: 2006
---- 3 ----
title: A Game Of Thrones
author: George R. R. Martin
year: 1996
---- 4 ----
title: Harry Potter And The Sorcerer's Stone
author: J.K. Rowling
year: 1997
---- 5 ----
title: A Wizard of Earthsea
author: Ursula K. Leguin
year: 1968
---- 6 ----
title: The Way of Kings
author: Brandon Sanderson
year: 2011
== Main Menu ==
1 -> read in books from a file
2 -> write out books to a file
3 -> add books to the collection
4 -> print all books
5 -> delete a book from the collection
6 -> quit the program
Please enter a choice (1 to 6)
6
Thank you for using our program!
------------------
// File name: Book.h
#ifndef BOOK_H
#define BOOK_H
#include <iostream>
#include <string>
using namespace std;
// Defines class Book
class Book
{
private:
// Data member to store book information
string title;
string author;
int year;
public:
// Default constructor to assign default values to data
members
Book();
// Parameterized constructor to assign parameter values to data
members
Book(string ti, string au, int ye);
// Getter functions
string getTitle();
string getAuthor();
int getYear();
// Setter functions
void setTitle(string ti);
void setAuthor(string au);
void setYear(int ye);
// Function to display book information
void show();
};// End of class Book
#endif // End of BOOK_H
------------------------------------------------------------------------------------------------------
// File name: Book.cpp
#include "Book.h"
using namespace std;
// Default constructor to assign default values to data
members
Book::Book()
{
title = author = "";
year = 0;
}
// Parameterized constructor to assign parameter values to data
members
Book::Book(string ti, string au, int ye)
{
title = ti;
author = au;
year = ye;
}
// Function to return book title
string Book::getTitle()
{
return title;
}
// Function to return author name
string Book::getAuthor()
{
return author;
}
// Function to return year
int Book::getYear()
{
return year;
}
// Function to change book title
void Book::setTitle(string ti)
{
title = ti;
}
// Function to change author name
void Book::setAuthor(string au)
{
author = au;
}
// Function to change year
void Book::setYear(int ye)
{
year = ye;
}
// Function to display book information
void Book::show()
{
cout<<"\n Title: "<<title<<"\n Author:
"<<author<<"\n Year: "<<year;
}
------------------------------------------------------------------------------------------------------
// File name: BookCollection.h
#ifndef BOOKCOLLECTION_H
#define BOOKCOLLECTION_H
#include "Book.cpp"
#include <fstream>
#include <cstdlib>
#define MAX 100
using namespace std;
// Defines class BookCollection
class BookCollection
{
// Declares an array of object of class Book of size MAX
Book books[MAX];
// To store number of books
int numberOfBooks;
public:
// Default constructor
BookCollection();
// Function to add a book to array of books
void addBook(Book b);
// Function to read books information from file and stores it in
array of object books
void readBook();
// Function to write books information to file
void writeBook();
// Function to display all books information
void showAllBooks();
// Function to search the parameter book title in the array of
object books
// If found then returns the found index position
// Otherwise returns -1
int searchBook(string title);
// Function to delete a book based on the title entered by the
user
void deleteBook();
// Function to add a book to the array of object books
void addBooks();
};// End of class BookCollection
#endif // End of BOOKCOLLECTION_H
------------------------------------------------------------------------------------------------------
// File name: BookCollection.cpp
#include "BookCollection.h"
// Default constructor
BookCollection::BookCollection()
{
numberOfBooks = 0;
}// End of default constructor
// Function to add a book to array of books
void BookCollection::addBook(Book b)
{
// Checks if number of books is equals to MAX display error
message
if(numberOfBooks == MAX)
cout<<"\n Unable to add book. Reached maximum limit:
"<<MAX;
// Otherwise assigns the parameter object to number of book
index position
// increase the counter by one
else
books[numberOfBooks++] = b;
}// End of function
// Function to read books information from file and stores it in
array of object books
void BookCollection::readBook()
{
// Local variable to store data read from file
string title, author, newLine;
int year;
// To store file name
string fileName;
// Accepts file name
cout<<"\n Enter file name? ";
cin>>fileName;
// Opens the file for reading
ifstream readF;
readF.open(fileName.c_str());
// Checks if the file unable to open for reading display's error
message and stop
if(!readF)
{
cout<<"\n ERROR: Unable to open the file
"<<fileName<<" for reading.";
exit(0);
}// End of if condition
// Loops till end of the file
while(!readF.eof())
{
// Reads a title from file
getline(readF, title, '\n');
// Reads a author name from file
getline(readF, author, '\n');
// Read year
readF>>year;
// Reads new line character
getline(readF, newLine, '\n');
// Creates a book object using parameterized constructor
// passes to function addBook to add it to books array of
object
addBook(Book(title, author, year));
}// End of while loop
// Closer the file
readF.close();
}// End of function
// Function to write books information to file
void BookCollection::writeBook()
{
// To store file name
string fileName;
// Accepts file name
cout<<"\n Enter file name? ";
cin>>fileName;
// Opens the file for writing
ofstream writeF;
writeF.open(fileName.c_str());
// Checks if the file unable to open for writing display's error
message and stop
if(!writeF)
{
cout<<"\n ERROR: Unable to open the file
"<<fileName<<" for writing.";
exit(0);
}// End of if condition
// Loops till end of the books array of object
for(int c = 0; c < numberOfBooks; c++)
{
// Checks if it is last record write book information without new
line character at end
if(c == numberOfBooks - 1)
writeF<<books[c].getTitle()<<endl<<writeF<<books[c].getAuthor()<<endl
<<books[c].getYear();
// Otherwise not last record write book information with new line
character at end
else
writeF<<books[c].getTitle()<<endl<<writeF<<books[c].getAuthor()<<endl
<<books[c].getYear()<<endl;
}// End of while loop
// Closer the file
writeF.close();
}// End of function
// Function to display all books information
void BookCollection::showAllBooks()
{
// Checks if number of book is 0 then display error message
if(numberOfBooks == 0)
cout<<"\n No books in collection to display.";
// Otherwise not empty
else
{
// Loops till end of the books array of object
for(int c = 0; c < numberOfBooks; c++)
{
cout<<"\n ---- "<<c<<" ----";
// Calls the function to display current book information
books[c].show();
}// End of for loop
}// End of else
}// End of function
// Function to search the parameter book title in the array of
object books
// If found then returns the found index position
// Otherwise returns -1
int BookCollection::searchBook(string title)
{
// Loops till end of the books array of object
for(int c = 0; c < numberOfBooks; c++)
// Checks if parameter title is equals to current object
title
if(title.compare(books[c].getTitle()) == 0)
// Returns the loop variable value as found index
return c;
// Otherwise return -1 for not found
return -1;
}// End of function
// Function to delete a book based on the title entered by the
user
void BookCollection::deleteBook()
{
string title;
// Checks if number of book is 0 then display error
message
if(numberOfBooks == 0)
cout<<"\n No books in collection to delete.";
// Otherwise not empty
else
{
// Accepts book title
cout<<"\n Enter the title to delete book: ";
getline(cin, title);
// Calls the function to search title
int index = searchBook(title);
// Checks if returned index position is -1 then not found
if(index == -1)
cout<<"\n Book having title: "<<title<<" not
found in the collection to delete.";
// Otherwise book found
else
{
// Loops from found index position to end of the array of object
minus one times
for(int c = index; c < numberOfBooks-1; c++)
// Shifts each object to left
books[c] = books[c + 1];
// Decrease the number of books by one
numberOfBooks--;
cout<<"\n\n\t Book delete successfully.";
}// End of inner else
}// End of outer else
}// End of function
// Function to add a book to the array of object books
void BookCollection::addBooks()
{
string title, author;
int year;
char choice;
// Loops till user choice is not 'N' or 'n'
do
{
// Accepts title and author name
cout<<"\n Enter title? ";
getline(cin, title);
cout<<"\n Enter author? ";
getline(cin, author);
// Loop till valid year entered by the user
do
{
// Accepts year
cout<<"\n enter year? (year should be larger than 0) ";
cin>>year;
// Checks if year is greater then 0 then come out of the
loop
if(year > 0)
break;
}while(1);// End of inner do - while loop
// Creates a book object using parameterized constructor
// passes to function addBook to add it to books array of
object
addBook(Book(title, author, year));
cout<<"\n\n\t Book added successfully.";
// Accepts user choice
cout<<"\n Do you want to continue? ('y' or 'n')? ";
cin>>choice;
fflush(stdin);
// Checks if user choice is 'N' or 'n' then come out of the
loop
if(choice == 'N' || choice == 'n')
break;
}while(1);// End of outer do - while loop
}// End of function
------------------------------------------------------------------------------------------------------
// File name: BookCollectionMain.cpp
#include "BookCollection.cpp"
// Function to display menu, accept user choice and returns
it
int menu()
{
int choice;
// Displays menu
cout<<"\n\n == Main Menu ==";
cout<<"\n\t 1 -> read in books from a file \n\t 2 ->
write out books to a file"
<<"\n\t 3 -> add books to the collection \n\t 4 ->
print all books"
<<"\n\t 5 -> delete a book from the collection \n\t 6
-> quit the program"
<<"\n\t\t Please enter a choice (1 to 6): ";
// Accepts user choice
cin>>choice;
fflush(stdin);
// Returns user choice
return choice;
}// End of function
// main function definition
int main()
{
string title, author, year;
// Creates an object of class BookCollection
BookCollection bc;
// Loops till user choice is not 6
do
{
// Calls the function to accept user choice
// Calls appropriate function based on user choice returned
switch(menu())
{
case 1:
bc.readBook();
break;
case 2:
bc.writeBook();
break;
case 3:
bc.addBooks();
break;
case 4:
bc.showAllBooks();
break;
case 5:
bc.deleteBook();
break;
case 6:
cout<<"\n\t Thank you for using our program! ";
exit(0);
default:
cout<<"\n\t\t Invalid Choice!!";
}// end of switch - case
}while(1); // End of do - while loop
return 0;
}// End of main function\
Sample Output:
== Main Menu ==
1 -> read in books from a file
2 -> write out books to a file
3 -> add books to the collection
4 -> print all books
5 -> delete a book from the collection
6 -> quit the program
Please enter a choice (1 to 6): 4
No books in collection to display.
== Main Menu ==
1 -> read in books from a file
2 -> write out books to a file
3 -> add books to the collection
4 -> print all books
5 -> delete a book from the collection
6 -> quit the program
Please enter a choice (1 to 6): 5
No books in collection to delete.
== Main Menu ==
1 -> read in books from a file
2 -> write out books to a file
3 -> add books to the collection
4 -> print all books
5 -> delete a book from the collection
6 -> quit the program
Please enter a choice (1 to 6): 56
Invalid Choice!!
== Main Menu ==
1 -> read in books from a file
2 -> write out books to a file
3 -> add books to the collection
4 -> print all books
5 -> delete a book from the collection
6 -> quit the program
Please enter a choice (1 to 6): 1
Enter file name? books.txt
== Main Menu ==
1 -> read in books from a file
2 -> write out books to a file
3 -> add books to the collection
4 -> print all books
5 -> delete a book from the collection
6 -> quit the program
Please enter a choice (1 to 6): 4
---- 0 ----
Title: The Name Of The Wind
Author: Patrick Roth
Year: 2007
---- 1 ----
Title: Lord Of The Rings: The Fellowship of the Ring
Author: J. R. R. Tolkien
Year: 1954
---- 2 ----
Title: Mistborn
Author: Brandon Sanderson
Year: 2006
---- 3 ----
Title: A Game Of Thrones
Author: George R. R. Martin
Year: 1996
---- 4 ----
Title: Harry Potter And The Sorcerer's Stone
Author: J.K. Rowling
Year: 1997
---- 5 ----
Title: A Wizard of Earthsea
Author: Ursula K. Leguin
Year: 1968
== Main Menu ==
1 -> read in books from a file
2 -> write out books to a file
3 -> add books to the collection
4 -> print all books
5 -> delete a book from the collection
6 -> quit the program
Please enter a choice (1 to 6): 3
Enter title? Java Programming
Enter author? Ram Panda
enter year? (year should be larger than 0) -55
enter year? (year should be larger than 0) 2010
Book added successfully.
Do you want to continue? ('y' or 'n')? y
Enter title? Pythor for you
Enter author? Pyari Sahu
enter year? (year should be larger than 0) 2020
Book added successfully.
Do you want to continue? ('y' or 'n')? n
== Main Menu ==
1 -> read in books from a file
2 -> write out books to a file
3 -> add books to the collection
4 -> print all books
5 -> delete a book from the collection
6 -> quit the program
Please enter a choice (1 to 6): 4
---- 0 ----
Title: The Name Of The Wind
Author: Patrick Roth
Year: 2007
---- 1 ----
Title: Lord Of The Rings: The Fellowship of the Ring
Author: J. R. R. Tolkien
Year: 1954
---- 2 ----
Title: Mistborn
Author: Brandon Sanderson
Year: 2006
---- 3 ----
Title: A Game Of Thrones
Author: George R. R. Martin
Year: 1996
---- 4 ----
Title: Harry Potter And The Sorcerer's Stone
Author: J.K. Rowling
Year: 1997
---- 5 ----
Title: A Wizard of Earthsea
Author: Ursula K. Leguin
Year: 1968
---- 6 ----
Title: Java Programming
Author: Ram Panda
Year: 2010
---- 7 ----
Title: Pythor for you
Author: Pyari Sahu
Year: 2020
== Main Menu ==
1 -> read in books from a file
2 -> write out books to a file
3 -> add books to the collection
4 -> print all books
5 -> delete a book from the collection
6 -> quit the program
Please enter a choice (1 to 6): 2
Enter file name? MyBooks.txt
== Main Menu ==
1 -> read in books from a file
2 -> write out books to a file
3 -> add books to the collection
4 -> print all books
5 -> delete a book from the collection
6 -> quit the program
Please enter a choice (1 to 6): 5
Enter the title to delete book: c++ programming
Book having title: c++ programming not found in the collection to delete.
== Main Menu ==
1 -> read in books from a file
2 -> write out books to a file
3 -> add books to the collection
4 -> print all books
5 -> delete a book from the collection
6 -> quit the program
Please enter a choice (1 to 6): 5
Enter the title to delete book: Java Programming
Book delete successfully.
== Main Menu ==
1 -> read in books from a file
2 -> write out books to a file
3 -> add books to the collection
4 -> print all books
5 -> delete a book from the collection
6 -> quit the program
Please enter a choice (1 to 6): 4
---- 0 ----
Title: The Name Of The Wind
Author: Patrick Roth
Year: 2007
---- 1 ----
Title: Lord Of The Rings: The Fellowship of the Ring
Author: J. R. R. Tolkien
Year: 1954
---- 2 ----
Title: Mistborn
Author: Brandon Sanderson
Year: 2006
---- 3 ----
Title: A Game Of Thrones
Author: George R. R. Martin
Year: 1996
---- 4 ----
Title: Harry Potter And The Sorcerer's Stone
Author: J.K. Rowling
Year: 1997
---- 5 ----
Title: A Wizard of Earthsea
Author: Ursula K. Leguin
Year: 1968
---- 6 ----
Title: Pythor for you
Author: Pyari Sahu
Year: 2020
== Main Menu ==
1 -> read in books from a file
2 -> write out books to a file
3 -> add books to the collection
4 -> print all books
5 -> delete a book from the collection
6 -> quit the program
Please enter a choice (1 to 6): 6
Thank you for using our program!
books.txt file contents
The Name Of The Wind
Patrick Roth
2007
Lord Of The Rings: The Fellowship of the Ring
J. R. R. Tolkien
1954
Mistborn
Brandon Sanderson
2006
A Game Of Thrones
George R. R. Martin
1996
Harry Potter And The Sorcerer's Stone
J.K. Rowling
1997
A Wizard of Earthsea
Ursula K. Leguin
1968
MyBooks.txt file contents
The Name Of The Wind
0x6af930Patrick Roth
2007
Lord Of The Rings: The Fellowship of the Ring
0x6af930J. R. R. Tolkien
1954
Mistborn
0x6af930Brandon Sanderson
2006
A Game Of Thrones
0x6af930George R. R. Martin
1996
Harry Potter And The Sorcerer's Stone
0x6af930J.K. Rowling
1997
A Wizard of Earthsea
0x6af930Ursula K. Leguin
1968
Java Programming
0x6af930Ram Panda
2010
Pythor for you
0x6af930Pyari Sahu
2020