Question

Help please Hi, i need to write a c++ program that caculate queries by directly reading...

Help please

Hi, i need to write a c++ program that caculate queries by directly reading the content of csv files.
the queries are :
A) the list of all the book that were published in a given publication year (by user)
B)The list of all the book of a given title that are available for borrowing
C)The name of all the members who have a copy of a given title in their possession
D)The name of all the members who have a book in their possession.

There are 4 csv files :
Book : contains : ISBN, "title", edition, year of publication
BookCopy : contains : Copy number, ISBN, available
Borrower : contains : member number, "name", "postal address and phone number"
BookLoan : contains : Copy number, "borrowing date","return date", member number

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

#include <iostream>
#include <string>
#include <sstream>

#include <fstream>

using namespace std;

struct Book {
   int ISBN;
   string title;
   int edition;
   int year;
   struct Book* next;
};

struct Book_copy {
   int ISBN;
   int copy_number;
   int available;
   struct Book_copy* next;
};

struct Borrower {
   int member_number;
   string name;
   string address;
   struct Borrower* next;
};


struct BookLoan {
   int copy_number;
   string borrowing_date;
   string return_date;
   int member_number;
   struct BookLoan* next;
};


struct Book* book_ptr=NULL;
struct Book_copy* book_copy_ptr=NULL;
struct Borrower* borrower_ptr=NULL;
struct BookLoan* bookloan_ptr=NULL;

struct Book* new_Book(int ISBN, string title, int edition, int year) {
   struct Book* ptr = new Book();
   ptr->ISBN = ISBN;
   ptr->title = title;
   ptr->edition = edition;
   ptr->year = year;
   ptr->next = NULL;
   return ptr;
}

struct Book_copy* new_Book_copy(int ISBN, int copy_number, int available) {
   struct Book_copy* ptr = new Book_copy();
   ptr->available = available;
   ptr->copy_number = copy_number;
   ptr->ISBN = ISBN;
   ptr->next = NULL;
   return ptr;
}

struct Borrower* new_Borrower(int member_number, string name, string address) {
   struct Borrower* ptr = new Borrower();
   ptr->address = address;
   ptr->member_number = member_number;
   ptr->name = name;
   ptr->next = NULL;
   return ptr;
}


struct BookLoan* new_BookLoan(int copy_number, string borrowing_date, string return_date,int member_number) {
   struct BookLoan* ptr = new BookLoan();
   ptr->borrowing_date = borrowing_date;
   ptr->copy_number = copy_number;
   ptr->member_number = member_number;
   ptr->return_date = return_date;
   ptr->next = NULL;
   return ptr;
}


void read_Book()
{
   fstream fin;

   fin.open("Book.csv", ios::in);

   string line;
       struct Book* temp = book_ptr;

       while (getline(fin, line))
       {
           stringstream ss(line);
           string token;
           int a, c, d;
           string b;
           int i = 0;

           while (std::getline(ss, token, ',')) {
               std::cout << token << '\n';
               if (i == 0) {
                   a = stoi(token);
               }
               else if (i == 1) {
                   b = token;
               }
               else if (i==2) {
                   c = stoi(token);
               }
               else if(i==3) {
                   d = stoi(token);
               }
               i++;
           }
           cout << a << " " << b << " " << c << " " << d << endl;
           if (book_ptr == NULL) {
               book_ptr = new_Book(a, b, c, d);
               temp = book_ptr;
           }
           else {
               temp->next = new_Book(a, b, c, d);
               temp = temp->next;
           }
       }
}

void read_BookCopy()
{
   fstream fin;

   fin.open("BookCopy.csv", ios::in);

   string line;
   struct Book_copy* temp = book_copy_ptr;

   while (getline(fin, line))
   {
       stringstream ss(line);
       string token;
       int a, b, c;
       int i = 0;

       while (std::getline(ss, token, ',')) {
           std::cout << token << '\n';
           if (i == 0) {
               a = stoi(token);
           }
           else if (i == 1) {
               b = stoi(token);
           }
           else if (i == 2) {
               c = stoi(token);
           }
           i++;
       }
       cout << a << " " << b << " " << c << " " << endl;
       if (book_copy_ptr == NULL) {
           book_copy_ptr = new_Book_copy(a, b, c);
           temp = book_copy_ptr;
       }
       else {
           temp->next = new_Book_copy(a, b, c);
           temp = temp->next;
       }
   }
}

void read_Borrower()
{
   fstream fin;

   fin.open("Borrower.csv", ios::in);

   string line;
   struct Borrower* temp = borrower_ptr;

   while (getline(fin, line))
   {
       stringstream ss(line);
       string token;
       int a;
       string b, c;
       int i = 0;

       while (std::getline(ss, token, ',')) {
           std::cout << token << '\n';
           if (i == 0) {
               a = stoi(token);
           }
           else if (i == 1) {
               b = token;
           }
           else if (i == 2) {
               c = token;
           }
           i++;
       }
       cout << a << " " << b << " " << c << " " << endl;
       if (borrower_ptr == NULL) {
           borrower_ptr = new_Borrower(a, b, c);
           temp = borrower_ptr;
       }
       else {
           temp->next = new_Borrower(a, b, c);
           temp = temp->next;
       }
   }
}


void read_BookLoan()
{
   fstream fin;

   fin.open("BookLoan.csv", ios::in);

   string line;
   struct BookLoan* temp = bookloan_ptr;

   while (getline(fin, line))
   {
       stringstream ss(line);
       string token;
       int a, d;
           string b, c;
       int i = 0;

       while (std::getline(ss, token, ',')) {
           std::cout << token << '\n';
           if (i == 0) {
               a = stoi(token);
           }
           else if (i == 1) {
               b = token;
           }
           else if (i == 2) {
               c =token;
           }
           else if (i == 3) {
               d = stoi(token);
           }
           i++;
       }
       cout << a << " " << b << " " << c << " " << endl;
       if (book_copy_ptr == NULL) {
           bookloan_ptr = new_BookLoan(a, b, c,d);
           temp = bookloan_ptr;
       }
       else {
           temp->next = new_BookLoan(a, b, c,d);
           temp = temp->next;
       }
   }
}

void A(int year) {
   struct Book* temp = book_ptr;
   while (temp) {
       if (temp->year == year) {
           cout << temp->title << endl;
       }
       temp = temp->next;
   }
   cout << endl;
}


void B(string title) {
   struct Book* temp = book_ptr;
   struct Book_copy* temp1 = book_copy_ptr;

   cout << "Title " << "\t" << "Copy number" << "\t" << "ISBN" << "\t" << "Avialable" << endl;

   while (temp) {
       if (temp->title == title) {
           int ISBN = temp->ISBN;
           temp1 = book_copy_ptr;
           while (temp1) {
               if (temp1->ISBN == ISBN) {
                   cout << title << temp1->copy_number << "\t" << temp1->ISBN << "\t" << temp1->available << endl;
               }
               temp1 = temp1->next;
           }
       }
       temp = temp->next;
   }

   cout << endl;
}

void C(string title) {
   struct Book* temp = book_ptr;
   struct Book_copy* temp1 = book_copy_ptr;
   struct Borrower* temp2 = borrower_ptr;
   struct BookLoan* temp3 = bookloan_ptr;


   while (temp) {
       if (temp->title == title) {
           int ISBN = temp->ISBN;
           temp1 = book_copy_ptr;
           while (temp1) {
               if (temp1->ISBN == ISBN) {
                   int copy_number = temp1->copy_number;
                   temp3 = bookloan_ptr;
                   while (temp3) {
                       if (temp3->copy_number == copy_number) {
                           int member_number = temp3->member_number;
                           temp2 = borrower_ptr;
                           while (temp2) {
                               if (temp2->member_number = member_number) {
                                   cout << temp2->name << endl;
                               }
                               temp2 = temp2->next;
                           }
                       }
                       temp3 = temp3->next;
                   }


               }
               temp1 = temp1->next;
           }
       }
       temp = temp->next;
   }

   cout << endl;
}

void D() {
   struct Borrower* temp2 = borrower_ptr;
   struct BookLoan* temp3 = bookloan_ptr;


                   while (temp3) {
                           int member_number = temp3->member_number;
                           temp2 = borrower_ptr;
                           while (temp2) {
                               if (temp2->member_number = member_number) {
                                   cout << temp2->name << endl;
                               }
                               temp2 = temp2->next;
                           }
                      
                       temp3 = temp3->next;
                   }

   cout << endl;
}

int main() {
   read_Book();
   read_BookCopy();
   read_Borrower();
   read_BookLoan();

   cout << "A) the list of all the book that were published in a given publication year" << endl;
   int year;
   cout << "Input Year" << endl;
   cin >> year;
   A(year);


   cout << "B)The list of all the book of a given title that are available for borrowing" << endl;
   string title;
   cin >> title;
   B(title);

   cout << "C)The name of all the members who have a copy of a given title in their possession" << endl;
   cin >> title;
   C(title);

   cout << "D)The name of all the members who have a book in their possession." << endl;
   D();


   return 0;

}

Add a comment
Know the answer?
Add Answer to:
Help please Hi, i need to write a c++ program that caculate queries by directly reading...
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
  • C++ Help. I am new to Visual Studio and C++ so please include comments! Please follow...

    C++ Help. I am new to Visual Studio and C++ so please include comments! Please follow the directions and make as simple as possible. Write a C++ program that reads the following list of input data (book.dat) and then allows users to search and view the books from a Menu List all available books Search for book using A. Title or B. ISBN? Exit Program The format of the file is as follows: //    The title of the book //   ...

  • Consider the following table schemas (primary keys are underlined): Book (BookId, Title, Publishername)  Book_Authors (BookId, AuthorName)  Publisher...

    Consider the following table schemas (primary keys are underlined): Book (BookId, Title, Publishername)  Book_Authors (BookId, AuthorName)  Publisher (Name, Address, Phone) Book_Copies (BookId, BranchId, No_Of_Copies) Book_Loans (BookId, BranchId, CardNo, DateOut, DueDate)   Library_Branch (BranchId, BranchName, Address)  Borrower (CardNo, Name, Address, Phone) Write SQL statements for the following queries: List the number of copies of the book titled “The Lost Tribe” owned by each library branch and the corresponding library branch name. List the names and addresses of all borrowers who have borrowed more than 5 books...

  • Programming Assignment This is a database course using Open Office Database. Assume that you have been...

    Programming Assignment This is a database course using Open Office Database. Assume that you have been given the task of creating a system for a library to keep track of their books, the borrowers of the books, and the books that are currently lent out. Your first step will be to create the relations necessary for this system. Book will need information such as a unique identifier for each book, title, author, ISBN number, date of publication, cost. Borrower will...

  • I wrote this code but there’s an issue with it : #include <iostream> #include <vector&...

    I wrote this code but there’s an issue with it : #include <iostream> #include <vector> #include <string> #include <fstream> using namespace std; class Borrower { private: string ID, name; public: Borrower() :ID("0"), name("no name yet") {} void setID(string nID); void setName(string nID); string getID(); string getName(); }; void Borrower::setID(string nID) { ID = nID; } void Borrower::setName(string nname) { name = nname; } string Borrower::getID() { return ID; } string Borrower::getName() { return name; } class Book { private: string...

  • Write the following questions as queries in SQL. Use only the operators discussed in class (in...

    Write the following questions as queries in SQL. Use only the operators discussed in class (in particular, no outer joins are allowed). Please use renaming consistently! The following database schema is given: RESEARCHER(rid,name,institution,city,country) PAPER(title,journal,volume,number,year) AUTHOR(resid,title) where rid is the identifier (primary key) of RESEARCHER, name is the researcher’s name, institution is where the researcher works, and city and country the city and country where the institution is located; title is the paper identifier (primary key) of PAPER, journal is the...

  • This project supposed to develop a database system for a library for a university. The system...

    This project supposed to develop a database system for a library for a university. The system contains information about books, book authors, students, and faculty. Students and faculty are allowed to borrow books from the library. You should design the system to contain entities and attributes to store data that makes the system able to report the requested information by executing the queries. SQL Queries: 1. List the names and email for students who borrowed books along with the number...

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

  • The primary keys are underlined. The foreign keys are denoted by asterisks (*). Description of the...

    The primary keys are underlined. The foreign keys are denoted by asterisks (*). Description of the schema: • person — keeps track of the people who borrow books from the library. The attributes contain personal and contact information. • author — keeps track of personal information about authors. • publisher — keeps track of the publisher information. To keep it simple, most of the attributes have been truncated in the sample database. 1 trivial dependencies are things like X→X or...

  • c) Write an ER Diagram for the following Library database. Identify all the Entities, Relationships and Attributes. Underline the primary keys and mark the different constraints. You can add an I att...

    c) Write an ER Diagram for the following Library database. Identify all the Entities, Relationships and Attributes. Underline the primary keys and mark the different constraints. You can add an I attribute as a primary key for an entity to make t unique if necessary. Library has a number of branches in the city, each branch having a name, address and librarian. Books have title, authors and publisher. A book can have multiple authors but a single publisher. Note that same...

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

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