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...
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...
Write a C++ program that asks for the following information about a book order from the console: • Title • Author • ISBN (hint: careful about what data type to use - validate 9 or 13 characters) • Price (validate number < 400) • Quantity (number of books to purchase – validate > 0 and < 100) • Fiction/Non-Fiction (‘N’ or ‘F’ - validate) • Genre (‘R’ romance, ‘D’ drama, ‘M’ mystery – validate) Make use of the following data...
please Code in c++ Create a new Library class. You will need both a header file and a source file for this class. The class will contain two data members: an array of Book objects the current number of books in the array Since the book array is moving from the main.cc file, you will also move the constant array size definition (MAX_ARR_SIZE) into the Library header file. Write the following functions for the Library class: a constructor that initializes...
Create a program SearchNewBooks.cc that given as command line input newbooks.dat and request.dat and a method of search either binary or linear, creates a file of the number of books found from the request list. program should receive two filenames as command line arguments, the new books file and request file, which the program should read in. The list of new books and list of requested books should should be stored in only two std::vector containers. Because some code to...