Question

Using C++ Skills Required Create and use classes Exception Handling, Read and write files, work vectors....

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 they are checked in or out.

You need to create a class called LibraryBook. It needs to have the following member variables stored privately: string title, string author, and string ISBN. You also need to have a Boolean to store its status whether it’s checked out or not. Call this variable checkedOut.

Create a header file and a .cpp file for the LibraryBook.cpp and LibraryBook.h

For functions, this library book class needs to have:

  • A default constructor setting all strings blank and the checkedOut Boolean set to false
  • A constrictor setting all strings through the parameters (title, author, ISBN), and setting the checked out status to false.
  • “Getters” to get the values from the private variables. (So getTitle(). getAuthor(), getISBN())
  • A checkOutBook() function to change the status so it is checked out
  • A checkInBook() function to change the status so it is checked in (or NOT checked out)
  • A isCheckedOut() function to return the status of the book

More information and file structure

The books.txt file has a list of all of the books at the library. The books are listed with the title on one line, the author on the next, then the ISBN on the next. For simplicity, all three are stored as strings in the class, including the number. Using the file streams, you’ve learned how to read in one word or number at a time. To read in an entire line, use the getline() function.

For example:

                        

                        string str;                                 // string variable

                        ifstream fin(“file.txt”);            // input file stream

                        getline(fin, str);                       // read one line, store in variable

This will read in the whole line (not the first word) from the file and store it in the variable str, which is a string. Use this example to read in the title author, and ISBN from the file.

From there, you can pass in your data in as parameters into the LibraryBook object. For example:

                        

                        string title = “title”;

                        string author = “author”;

                        string ISBN = “123”

                        LibraryBook myBook(title, author, ISBN);

STOP HERE! Make sure your program works correctly at this point before continuing.

Next, in the isbns.txt file, you have a list ISBN numbers, one per line. If the ISBN number appears in the file, it has passed through the barcode scanner on the computer to be checked in or out. You can read these in with the normal style (if you choose to or style above). You can then either use the LibraryBook’s checkIn() or checkout() function to find out if it’s at the library or not, then use that Boolean to determine which function you call.

STOP HERE! Make sure your program works correctly (again) at this point before continuing.

Finally, you need to print the report your boss needs. The report should be named checkedout.txt and should print out a header consisting of the words “Title”, “Author”, and “ISBN”, separated by tabs, and on each line after that, the title, author, and ISBN of the books that are checked out, separated by tabs. You will need to do another loop to iterate through the vector, and print out books that are checked out. For the files provided, your checkedout.txt file should look like:

0 0
Add a comment Improve this question Transcribed image text
Answer #1

////LibraryBook.h

#ifndef LIBRARYBOOK_H
#define LIBRARYBOOK_H
#include<iostream>
using namespace std;

class LibraryBook
{
public:
LibraryBook(string,string,string);

string Gettitle();
void Settitle(string val);
string Getauthor();
void Setauthor(string val);
string GetISBN();
void SetISBN(string val);
void checkOutBook();
void checkInBook();
bool isCheckedOut();

private:
string title;
string author;
string ISBN;
bool checkedOut;
};

#endif // LIBRARYBOOK_H
--------------------------------------------------------------------------

LibraryBook.cpp

#include "LibraryBook.h"

LibraryBook::LibraryBook(string title, string author, string isbn)
{
this->title=title;
this->author=author;
this->ISBN=isbn;
checkedOut=false;
}

string LibraryBook::Gettitle() { return title; }
void LibraryBook::Settitle(string val) { title = val; }
string LibraryBook::Getauthor() { return author; }
void LibraryBook::Setauthor(string val) { author = val; }
string LibraryBook::GetISBN() { return ISBN; }
void LibraryBook::SetISBN(string val) { ISBN = val; }

void LibraryBook::checkOutBook()
{
checkedOut=true;
}

void LibraryBook::checkInBook()
{
checkedOut=false;
}

bool LibraryBook::isCheckedOut()
{
return checkedOut;
}
-------------------------------------------------------------

Main.cpp

#include<iostream>
#include<list>
#include<fstream>
#include<bits/stdc++.h>
#include <string>
#include "include\LibraryBook.h"
using namespace std;

int main(){
fstream readFile;
readFile.open("books.txt", ios::in);
if (readFile.fail())
{
cout << "Error opening file!\n";
exit(1);
}
cout<<"Reading file"<<endl;
string title,author,isbn;
list<LibraryBook> booklist;
while(readFile)
{
getline(readFile, title);
getline(readFile, author);
getline(readFile, isbn);
booklist.push_back(LibraryBook(title,author,isbn));
if(readFile.eof())
break;
}
readFile.close();
for(auto it=booklist.begin();it!=booklist.end();++it)
cout<<it->Gettitle()<<" "<<it->Getauthor()<<" "<<it->GetISBN()<<" "<<it->isCheckedOut()<<endl;
readFile.open("isbns.txt", ios::in);
if (readFile.fail())
{
cout << "Error opening file!\n";
exit(1);
}
cout<<"Reading file"<<endl;
while(readFile)
{
getline(readFile, isbn);
for(auto it=booklist.begin();it!=booklist.end();++it)
{
if(isbn.compare(it->GetISBN())==0){
if(it->isCheckedOut())
it->checkInBook();
else
it->checkOutBook();
}
}
if(readFile.eof())
break;
}
readFile.close();
fstream writeFile;
writeFile.open("checkedout.txt", ios::out);
if (writeFile.fail())
{
cout << "Error opening file!\n";
exit(1);
}
writeFile<<"Title"<<"\t"<<"Author"<<"\t"<<"ISBN"<<endl;
for(auto it=booklist.begin();it!=booklist.end();++it){
if(it->isCheckedOut())
writeFile<<it->Gettitle()<<"\t"<<it->Getauthor()<<"\t"<<it->GetISBN()<<endl;
}
writeFile.close();
return 1;
}
------------------------------------------Belowe are text files

books.txt

1918
Charles Dickens
2
My Experiment With Truth
Mahatma Gandhi
85
War and Peace
Leo Tolstoy
66

----------------------------------------

isbns.txt

12
8
2
4
85
66
2
45
66
66

--------------here is the output file

checkedout.txt

Title   Author   ISBN
My Experiment With Truth   Mahatma Gandhi   85
War and Peace   Leo Tolstoy   66

below is the screenshot of the working code.Console has some output for debugging purpose.Remove them if you want.Cheers and enjoy :D

Add a comment
Know the answer?
Add Answer to:
Using C++ Skills Required Create and use classes Exception Handling, Read and write files, work vectors....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 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...

  • 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...

  • C++ In this homework, you will implement a single linked list to store a list of computer science textbooks. Ever...

    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...

  • please use C++ write a program to read a textfile containing a list of books. each...

    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...

  • Part 1 The purpose of this part of the assignment is to give you practice in...

    Part 1 The purpose of this part of the assignment is to give you practice in creating a class. You will develop a program that creates a class for a book. The main program will simply test this class. The class will have the following data members: A string for the name of the author A string for the book title A long integer for the ISBN The class will have the following member functions (details about each one are...

  • Please provide original Answer, I can not turn in the same as my classmate. thanks In...

    Please provide original Answer, I can not turn in the same as my classmate. thanks 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 Type Attribute String title String author String ISBN Textbook* next Textbook Type Attribute String title String...

  • // C programming Create a system managing a mini library system. Every book corresponds to a...

    // C programming Create a system managing a mini library system. Every book corresponds to a record (line) in a text file named "mylibrary.txt". Each record consists of 6 fields (Book ID, Title, Author, Possession, checked out Date, Due Date) separated by comma: No comma '', "in title or author name. This mini library keeps the record for each book in the library. Different books can share the book "Title". But the "Book ID" for each book is unique. One...

  • C++ project we need to create a class for Book and Warehouse using Book.h and Warehouse.h header ...

    C++ project we need to create a class for Book and Warehouse using Book.h and Warehouse.h header files given. Then make a main program using Book and Warehouse to read data from book.dat and have functions to list and find book by isbn Objectives: Class Association and operator overloading This project is a continuation from Project 1. The program should accept the same input data file and support the same list and find operations. You will change the implementation of...

  • please Code in c++ Create a new Library class. You will need both a header file...

    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...

  • write program in C language. To get more practice working with files, you will write several...

    write program in C language. To get more practice working with files, you will write several functions that involve operations on files. In particular, implement the following functions 1. Write a function that, given a file path/name as a string opens the file and returns its entire contents as a single string. Any endline characters should be preserved char *getFileContents (const char filePath); 2. Write a function that, given a file path/name as a string opens the file and returns...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT