Pagini recente » Cod sursa (job #1337898) | Cod sursa (job #1233845) | Cod sursa (job #2922066) | Cod sursa (job #279044) | Cod sursa (job #2923747)
#include <iostream>
#include <string>
#include <fstream>
#include <thread>
using namespace std;
int response,doesntWorkIfIDontDoThis;
class Books{
public:
string name;
float price;
string author;
string publishingHouse;
//int numberOfPages; we ll work on that idea later
string genre;
};
class BookShop{
public:
Books books;
void addBook();
void removeBook();
void updateBook();
void searchBook();
void availabilityCheck();
}bookshopEntry;
void removeBookDetails(string name, string author){
ifstream file;
ofstream ok;
string fileName = "bookshop.txt";
string tempFileName = "temp.txt";
file.open(fileName);
ok.open(tempFileName);
string line;
while(getline(file,line)){
if(line.find(name) != string::npos && line.find(author) != string::npos){
continue;
}
else
ok << line;
}
remove(fileName.c_str());
rename(tempFileName.c_str(),fileName.c_str());
}
bool checkNameAlreadyExists(string name){
ifstream file;
file.open("bookshop.txt");
string line;
while(getline(file,line)){
if(line.find(name) != string::npos)
return true;
}
return false;
}
void BookShop::addBook(){
if(doesntWorkIfIDontDoThis == 0)
cin.ignore();
cout << "Name : ";
getline(cin,bookshopEntry.books.name);
if(checkNameAlreadyExists(bookshopEntry.books.name)){
cout << "Woah!It appears that this book name has already been registered.Maybe try again\n";
doesntWorkIfIDontDoThis++;
addBook();
}
//cin.ignore();
cout << "Author : ";
getline(cin,bookshopEntry.books.author);
//cin.ignore();
cout << "Genre : ";
getline(cin,bookshopEntry.books.genre);
//cin.ignore();
cout << "Publishing house : ";
getline(cin,bookshopEntry.books.publishingHouse);
//cin.ignore();
cout << "Price(in euros) : ";
cin >> bookshopEntry.books.price;
ofstream file;
file.open("bookshop.txt",ios::app);
file << bookshopEntry.books.name << " " << bookshopEntry.books.author << " " << bookshopEntry.books.genre
<< " " << bookshopEntry.books.publishingHouse << " " << bookshopEntry.books.price << " euro(s)\n";
file.close();
cout << "Book added succesfully!\n";
doesntWorkIfIDontDoThis = 0;
this_thread::sleep_for(1000ms);
}
void BookShop::removeBook(){
cin.ignore();
cout << "Name : ";
cin >> bookshopEntry.books.name;
cout << "Author : ";
cin >> bookshopEntry.books.author;
}
void BookShop::updateBook(){
}
void BookShop::searchBook(){
}
void BookShop::availabilityCheck(){
}
int main(){
while(true){
cout << "=== BOOKSHOP MANAGEMENT SYSTEM ===\n";
cout << "1.Add a book to the bookshop\n";
cout << "2.Remove a book from the bookshop\n";
cout << "3.Update a book's information\n";
cout << "4.Search for a specific book\n";
cout << "5.Check if a book is available to buy\n";
cout << "> ";
cin >> response;
switch (response)
{
case 1:
bookshopEntry.addBook();
break;
case 2:
bookshopEntry.removeBook();
break;
case 3:
bookshopEntry.updateBook();
break;
case 4:
bookshopEntry.searchBook();
break;
case 5:
bookshopEntry.availabilityCheck();
break;
default:
break;
}
}
}