📄 three.cpp
字号:
//comments//change int in the menu to char or string...//re-arrange the funcs in Book#include <iostream>#include <string>#include <fstream>#include <iomanip>using namespace std;class Book{ private: string title; int authorNum; string authors[4]; string publisher; int year; string ISBN; double price; int stock; public: Book(); Book(string,int,string,string,double,int,string,string = "",string = "",string = ""); ~Book(); void setYear(int); void showYear(); void setAN(int); void showAN(); void showTitle(); void setTitle(string); bool checkTitle(string); void showStock(); void setStock(int); void updateStock(int); void showPublisher(); void setPublisher(string); bool checkPublisher(string); void showISBN(); void setISBN(string); bool checkISBN(string); void showPrice(); void setPrice(double); void updatePrice(double); void showAuthor(); void setAuthor(string="",string="",string="",string=""); bool checkAuthor(string);};Book::Book(){ title = ""; authorNum = 0; for(int i=0;i<4;++i) { authors[i] = ""; } publisher = ""; ISBN = ""; price = 0.0; stock = 0;}Book::Book(string ti,int an,string pu,string is,double pr,int st,string au1,string au2,string au3,string au4){ title = ti; authorNum = an; authors[0] = au1; authors[1] = au2; authors[2] = au3; authors[3] = au4; publisher = pu; ISBN = is; price = pr; stock = st;}Book::~Book(){ }void Book::setAN(int i){ authorNum = i;}void Book::showPrice(){ cout << "The price is: " << fixed << setprecision(2) << price << endl;}void Book::showStock(){ cout << "The quantities in stock is: " << stock << endl;}void Book::showAN(){ cout << "The number of authors is: " << authorNum << endl;}void Book::showYear(){ cout << "The year of publication is: " << year << endl;}void Book::setYear(int i){ year = i;}bool Book::checkAuthor(string s){ for(int i=0;i<authorNum;++i) { if(s.compare(authors[i]) == 0) return true; } return false;}void Book::setAuthor(string a,string b,string c,string d){ authors[0] = a; authors[1] = b; authors[2] = c; authors[3] = d;}void Book::showAuthor(){ if(authorNum > 1) cout << "The authors are: \n"; else cout << "The author is: "; for(int i=0;i<authorNum;++i) { cout << '\t' << authors[i] << endl; }}void Book::updatePrice(double d){ price += d;}void Book::setPrice(double d){ price = d;}void Book::showISBN(){ cout << "The ISBN is: " << ISBN << endl;}void Book::setISBN(string s){ ISBN = s;}bool Book::checkISBN(string s){ if(s.compare(0,s.size(),ISBN,0,ISBN.size()-1) == 0) { return true; } else { return false; }}void Book::showPublisher(){ cout << "The publisher is: " << publisher << endl;}void Book::setPublisher(string s){ publisher = s;}bool Book::checkPublisher(string s){ if(s == publisher) return true; else return false;}void Book::updateStock(int i){ stock += i;}void Book::setStock(int i){ stock = i;}void Book::showTitle(){ cout << "The title is: " << title << endl;}void Book::setTitle(string s){ title = s;}bool Book::checkTitle(string s){ if(s.compare(0,s.size(),title,0,title.size()-1) == 0) return true; else return false;}/////////////////////////////////class BookStore{ private: Book books[100]; int no; public: BookStore(char[]); void eventLoop(); void menu(); void printAll(); void printList(); void printISBN(); int search(); int searchISBN(); int searchTitle(); void update(); };BookStore::BookStore(char ch[]){ ifstream fin; string ti,is,pu,au[4]; int ye,st,an; double pr; for(int i=0;i<4;++i) { au[i] = ""; } fin.open(ch); if(fin.good()) { fin >> no; fin.ignore(1,'\n'); for(int i=0;i<no;++i) { getline(fin,ti,'\n'); getline(fin,is,'\n'); getline(fin,pu,'\n'); fin >> ye; fin.ignore(1,'\n'); fin >> pr; fin.ignore(1,'\n'); fin >> st; fin.ignore(1,'\n'); fin >> an; fin.ignore(1,'\n'); for(int j=0;j<an;++j) { getline(fin,au[j],'\n'); } books[i].setTitle(ti); books[i].setISBN(is); books[i].setPublisher(pu); books[i].setYear(ye); books[i].setPrice(pr); books[i].setStock(st); books[i].setAN(an); books[i].setAuthor(au[0],au[1],au[2],au[3]); } } else { cerr << "Can't open the file.\n"; exit(1); } fin.close();}void BookStore::menu(){ cout << "Welcome to Rock's Book Store\n"; cout << "To make a selection enter the number and press enter\n"; cout << "1: Print a list of books\n"; cout << "2: Print a list of books and ISBN numbers\n"; cout << "3: To see if a book in store\n"; cout << "4: To update the number of copies of a book\n"; cout << "5: To print books data\n"; cout << "9: Exit the program.\n";}void BookStore::printList(){ for(int i=0;i<no;++i) { books[i].showTitle(); } cout << endl;}void BookStore::printISBN(){ for(int i=0;i<no;++i) { books[i].showTitle(); books[i].showISBN(); cout << endl; } }int BookStore::search(){ int com,index = -1; cout << "Enter\n"; cout << "1: To search the book by ISBN\n"; cout << "2: To search the book by title\n"; cin >> com; if(com == 1) { index = searchISBN(); } else if(com == 2) { index = searchTitle(); } else { cerr << "Illegal command.\n"; } return index;}int BookStore::searchISBN(){ string s; int i = 0; cout << "Enter the ISBN of the book.\n"; cin.ignore(1,'\n'); getline(cin,s,'\n'); while(i<no) { if(books[i].checkISBN(s)) { cout << "The store sells this book.\n"; return i; } i++; } if(i == no) { cout << "The store does not sell this book.\n"; } return i;}int BookStore::searchTitle(){ string s; int i = 0; cout << "Enter the title of the book.\n"; cin.ignore(1,'\n'); getline(cin,s,'\n'); while(i<no) { if(books[i].checkTitle(s)) { cout << "The store sells this book.\n"; return i; } i++; } if(i == no) { cout << "The store does not sell this book.\n"; } return i;}void BookStore::update(){ int index,num; index = search(); if(index == no) cout << "Can't do that.\n"; else { cout << "Enter the number of copies of a book: "; cin >> num; books[index].updateStock(num); }}void BookStore::printAll(){ for(int i=0;i<no;++i) { books[i].showTitle(); books[i].showISBN(); books[i].showPublisher(); books[i].showYear(); books[i].showAN(); books[i].showAuthor(); books[i].showPrice(); books[i].showStock(); cout << endl; } }void BookStore::eventLoop(){ int command; do { menu(); cin >> command; switch(command) { case 1: printList(); break; case 2: printISBN(); break; case 3: search(); break; case 4: update(); break; case 5: printAll(); break; case 9: break; default: cerr << "Illegal command.\n"; } }while(command != 9);}int main(){ char datafile[] = "bookData.txt"; BookStore bookStore(datafile); bookStore.eventLoop(); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -