MUST BE WRITTEN IN C++
Objective: Learn how to define structures and classes, create and access a vector of structures, use sort with different compare functions.
Assignment:
Your program will use the same input file, but you will print the whole book information, not just the title. And, most importantly, this time you will take an object oriented approach to this problem.
Detailed specifications:
class Collection
{
private:
struct Book
{
string title;
string author;
string genre;
};
vector books;
public:
void sortByTitle()
{
sort(books.begin(), books.end(), [](Book b1, Book b2) { return b1.title < b2.title; });
}
};
Submit two files: main.cpp with your main function and Collection.h with your class implementation.
CONTENT OF GIVEN INPUT FILE CALLED "Books.txt"
Starting out with c++, Tony Gaddis, technical
Fundamentals of Database Systems, Elmarsi & Navathe, technical
The C++ Programming Language, Bjarne Stroustrup, technical
The Pillars of the Earth, Ken Follett, historical fiction
Fall of Giants, Ken Follet, historical fiction
Mindset: The New Psychology of Success, Carol Dweck, psychology
One Hundred Years of Solitude, Gabriel Garcia Marquez, fiction
Ashes, Kenzo Kitakana, fiction
The Dark Forest, Liu Cixin, science fiction
Replay, Ken Grimwood, fantasy
// collection.h
#ifndef COLLECTION_H_
#define COLLECTION_H_
#include <vector>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <iomanip>
using namespace std;
class Collection
{
private:
struct Book
{
string title;
string author;
string genre;
};
vector<Book> books;
public:
Collection()
{
string fileName, line;
Book book;
ifstream fin;
cout<<"Enter filename : ";
cin>>fileName;
fin.open(fileName.c_str());
// loop to continually input file name/path until valid input has not been received
while(!fin.is_open())
{
cout<<"Unable to open : "<<fileName<<endl;
cout<<"Enter valid file name/path : ";
cin>>fileName;
fin.open(fileName.c_str());
}
// read till the end of file
while(!fin.eof())
{
getline(fin,line); // read a line from file
stringstream lineStream(line);
// extract the title field from the line
getline(lineStream,book.title,',');
// extract the author field from the line
getline(lineStream,book.author,',');
// extract the genre field from the line
getline(lineStream,book.genre);
if(!fin.eof())
book.genre = book.genre.substr(0,book.genre.length()-1);
// insert the title into the vector
books.push_back(book);
}
// close the file
fin.close();
}
// function to sort the vector of books by title
void sortByTitle()
{
sort(books.begin(), books.end(), [](Book b1, Book b2) { return b1.title < b2.title; });
}
// function to sort the vector of books by author
void sortByAuthor()
{
sort(books.begin(),books.end(),[](Book b1, Book b2) {return b1.author < b2.author; });
}
// function to sort the vector of books by genre
void sortByGenre()
{
sort(books.begin(),books.end(),[](Book b1, Book b2) {return b1.genre < b2.genre; });
}
// function to print the details of the books
void print()
{
// loop over the vector
cout<<left<<setw(50)<<"Title"<<left<<setw(35)<<"Author"<<left<<setw(15)<<"Genre"<<endl;
cout<<string(100,'-')<<endl;
for(unsigned int i=0;i<books.size();i++)
{
cout<<left<<setw(50)<<books[i].title<<left<<setw(35)<<books[i].author<<left<<setw(15)<<books[i].genre<<endl;
}
}
};
#endif /* COLLECTION_H_ */
//end of collection.h
//main.cpp: C++ program to test the Collection class
#include <iostream>
#include "collection.h"
using namespace std;
int main() {
Collection collection;
int choice;
cout<<"\nMenu"<<endl;
cout<<"1. Sort by title"<<endl;
cout<<"2. Sort by author"<<endl;
cout<<"3. Sort by genre"<<endl;
cout<<"Enter you choice(1-3) : ";
cin>>choice;
// sort the books based on the user’s choice
if(choice == 1)
collection.sortByTitle();
else if(choice == 2)
collection.sortByAuthor();
else if(choice == 3)
collection.sortByGenre();
else
cout<<"Invalid choice"<<endl;
cout<<"Books : "<<endl;
collection.print(); // print the books
return 0;
}
//end of program
Output:
Input file:

Output:

MUST BE WRITTEN IN C++ Objective: Learn how to define structures and classes, create and access...
MUST BE WRITTEN IN C++ Objective: Learn how to define structures and classes, create and access a vector of structures, use sort with different compare functions. Assignment: Your program will use the same input file, but you will print the whole book information, not just the title. And, most importantly, this time you will take an object oriented approach to this problem. Detailed specifications: Define a class named Collection, in which you will define a structure that can hold book...
please use C++ write a program to read a textfile containing a list of books. each line in the file has tile, ... Question: Write a program to read a textfile containing a list of books. each line in the file has tile, ..... write a program to read a textfile containing a list of books. each line in the file has tile, ... Question: Write a program to read a textfile containing a list of books. Each line in...
Using C++ Skills Required Create and use classes Exception Handling, Read and write files, work vectors. Create Functions, include headers and other files, Loops(while, for), conditional(if, switch), datatypes, etc. Assignment You work at the computer science library, and your boss just bought a bunch of new books for the library! All of the new books need to be cataloged and sorted back on the shelf. You don’t need to keep track of which customer has the book or not, just whether...
Objectives: The main objective of this assignment is checking students’ ability to implement membership functions. After completing this assignment, students will be able to: implement member functions convert a member function into a standalone function convert a standalone function into a member function call member functions implement constructors use structs for function overloading Problem description: In this assignment, we will revisit Assignment #1. Mary has now created a small commercial library and has managed...
Objectives: The main objective of this assignment is checking students’ ability to implement membership functions. After completing this assignment, students will be able to: implement member functions convert a member function into a standalone function convert a standalone function into a member function call member functions implement constructors use structs for function overloading Problem description: In this assignment, we will revisit Assignment #1. Mary has now created a small commercial library and has managed...
C++ Double Question : 1ST. The printAllBooks function from Homework 5 was very useful, let’s do it again, but with an array of Book this time! Write a new printAllBooksfunction which will be useful to display the contents of your library. This function should: Accept two parameters in this order: array books: array of Book objects. int: number of books in the array (Note: this value might be less than the capacity of 50 books) This function does not return...
C++
In this homework, you will implement a single linked list to store a list of computer science textbooks. Every book has a title, author, and an ISBN number. You will create 2 classes: Textbook and Library. Textbook class should have all above attributes and also a "next" pointer. Textbook Туре String String String Attribute title author ISBN Textbook* next Library class should have a head node as an attribute to keep the list of the books. Also, following member...
You need to program a simple book library system. There are three java classes Book.java // book object class Library.java //library class A2.java //for testing The Book.java class represents book objects which contain the following fields title: a string which represents the book title. author: a string to hold the book author name year: book publication year isbn: a string of 10 numeric numbers. The book class will have also 3 constructors. -The default no argument constructor - A constructor...
Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comparable<Book> in the class definition. Now, all book objects are instances of the java.lang.Comparable interface. Write a test program that creates an array of ten books. 1. Use Arrays.sort( Book[]l books) from the java.util package to sort the array. The order of objects in the array is determined using compareTo...) method. 2. Write a method that returns the most expensive book in the...
This assignment requires using C++ language Your program will be creating 10 library books. Each library book will have title, author and publishing year. Each library book will be stored as a structure and your program will have an array of library books (an array of structures). You will create a corresponding array of book genres: Mystery, Romance, Fantasy, Technology, Children, etc. If the first element of the array is storing information about a book titled : "More about Paddington",...