Skip to content

Instantly share code, notes, and snippets.

@tabvn
Created June 15, 2019 04:17
Show Gist options
  • Select an option

  • Save tabvn/9181e2f6ef2d9785f5d03f280cd14520 to your computer and use it in GitHub Desktop.

Select an option

Save tabvn/9181e2f6ef2d9785f5d03f280cd14520 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Author {
private:
string name, id, email;
public:
/**
* Init author with name, id, email
* @param name
* @param id
* @param email
*/
Author(string name, string id, string email) {
Author::name = name;
Author::id = id;
Author::email = email;
}
/**
* Get author name
* @return
*/
string getName() {
return name;
}
/**
* Get author identity
* @return
*/
string getId() {
return id;
}
/**
* Get author email address
* @return
*/
string getEmail() {
return email;
}
/**
* Set author name
* @param name
*/
void setName(string name) {
Author::name = name;
}
/**
* Set author Identity
* @param id
*/
void setId(string id) {
Author::id = id;
}
/**
* Set author 's email
* @param email
*/
void setEmail(const string &email) {
Author::email = email;
}
};
class Book {
private:
string name;
double price;
int sold;
Author author;
public:
/**
* Init Book with name,price,sold, author
* @param name
* @param price
* @param sold
* @param author
*/
Book(const string &name, double price, int sold, const Author &author) : name(name), price(price), sold(sold),
author(author) {}
/**
* Set book name
* @param name
*/
void setName(string name) {
Book::name = name;
}
/**
* Set book price
* @param price
*/
void setPrice(double price) {
Book::price = price;
}
/**
* Set num of sold
* @param sold
*/
void setSold(int sold) {
Book::sold = sold;
}
/**
* Set book author
* @param author
*/
void setAuthor(Author author) {
Book::author = author;
}
/**
* Get name of book
* @return
*/
string getName() {
return name;
}
/**
* Get book's price
*/
double getPrice() const {
return price;
}
/**
* Get num of sold
* @return
*/
int getSold() const {
return sold;
}
/**
* Get total of sold price
* @return
*/
double getTotalSoldPrice() {
return Book::sold * Book::price;
}
/**
* Get book author
* @return
*/
Author getAuthor() {
return author;
}
/**
* Print book details
*/
void print() {
cout << "Book name: " << getName() << endl;
cout << "Author: " << getAuthor().getName() << " - " << getAuthor().getId() << " - " << getAuthor().getEmail()
<< endl;
cout << "price ($): " << getPrice() << endl;
cout << "Num of sold: " << getSold() << endl;
cout << "Total sold ($): " << getTotalSoldPrice() << endl;
}
};
class Store {
private:
vector<Book> books;
Book *bookHasMaximumSold;
public:
/**
* Store initial
*/
Store() {
bookHasMaximumSold = NULL;
}
/**
* Get all store's books
* @return
*/
vector<Book> getBooks() {
return books;
}
/**
* Add new book
* @param book
*/
Store *add(Book book) {
if (this->bookHasMaximumSold == NULL) {
this->bookHasMaximumSold = &book;
} else {
if (this->bookHasMaximumSold->getSold() < book.getSold()) {
this->bookHasMaximumSold = &book;
}
}
Store::books.push_back(book);
return this;
}
/**
* Get book has maximum sold
* @return
*/
Book *getMaximum() {
return bookHasMaximumSold;
}
/**
* Print store report
*/
void print() {
cout << "Store Report:" << endl;
for (int i = 0; i < books.size(); ++i) {
books[i].print();
cout << "---------------------------------------------------------------------" << endl;
}
cout << "Best Seller:" << endl;
Store::getMaximum()->print();
cout << "---------------------------------------------------------------------" << endl;
}
};
int main() {
Author a1("Nguyen Hoang Hai", "id-001", "haispdn@gmail.com");
Author a2("Le Thanh Cong", "id-002", "cong@ued.udn.vn");
Author a3("Tran Quoc Chien", "id-003", "chien@ued.udn.vn");
Book b1("C++ nang cao", 100, 500, a1);
Book b2("Java nang cao", 200, 700, a2);
Book b3("Ly thuyet do thi", 205, 800, a3);
Store *s = new Store();
s->add(b1)->add(b2)->add(b3)->print();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment