Skip to content

Instantly share code, notes, and snippets.

@tabvn
Created June 15, 2019 03:43
Show Gist options
  • Save tabvn/b9847e6c4267de9e0c89e1c4d25f207d to your computer and use it in GitHub Desktop.
Save tabvn/b9847e6c4267de9e0c89e1c4d25f207d 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:
Author(string name, string id, string email) {
Author::name = name;
Author::id = id;
Author::email = email;
}
string getName() {
return name;
}
string getId() {
return id;
}
string getEmail() {
return email;
}
void setName(string name) {
Author::name = name;
}
void setId(string id) {
Author::id = id;
}
void setEmail(const string &email) {
Author::email = email;
}
};
class Book {
private:
string name;
double price;
int sold;
Author author;
public:
Book(string name, double price, int sold, const Author &author) : name(name), price(price), sold(sold),
author(author) {}
void setName(string name) {
Book::name = name;
}
void setPrice(double price) {
Book::price = price;
}
void setSold(int sold) {
Book::sold = sold;
}
void setAuthor(Author author) {
Book::author = author;
}
string getName() {
return name;
}
double getPrice() const {
return price;
}
int getSold() const {
return sold;
}
double getTotalSoldPrice() {
return Book::sold * Book::price;
}
Author getAuthor() {
return author;
}
};
class Store {
private:
vector<Book> books;
Book *bookHasMaximumSold;
public:
Store() {
bookHasMaximumSold = NULL;
}
vector<Book> getBooks() {
return books;
}
/**
* Add new book
* @param book
*/
void addBook(Book book) {
if (this->bookHasMaximumSold == NULL) {
this->bookHasMaximumSold = &book;
} else {
if (this->bookHasMaximumSold->getSold() < book.getSold()) {
this->bookHasMaximumSold = &book;
}
}
Store::books.push_back(book);
}
/**
* Get book has maximum sold
* @return
*/
Book *getMaximum() {
return bookHasMaximumSold;
}
void print() {
cout << "Your Store:" << endl;
for (int i = 0; i < books.size(); ++i) {
cout << "Book name: " << books[i].getName() << endl;
cout << "Author: " << books[i].getAuthor().getName() << endl;
cout << "price: " << books[i].getPrice() << endl;
cout << "Num of sold: " << books[i].getSold() << endl;
cout << "Total sold ($): " << books[i].getTotalSoldPrice() << endl;
cout << "---------------------------------------------------------------------" << endl;
}
cout << "Best book:" << endl;
cout << "Name: " << Store::getMaximum()->getName() << endl;
cout << "Sold: " << Store::getMaximum()->getSold() << endl;
cout << "Total sold ($): " << Store::getMaximum()->getTotalSoldPrice() << endl;
cout << "---------------------------------------------------------------------" << endl;
}
};
int main() {
// khoi tao author 1
Author a1("Join smith", "id-001", "[email protected]");
// khoi tao author 2
Author a2("Tom Cruise", "id-002", "[email protected]");
// khoi tao book 1
Book b1("C++ nang cao", 100, 500, a1);
// khoi tao book 2
Book b2("Java nang cao", 200, 700, a2);
Store s; // store
// them book 1 vao store
s.addBook(b1);
// them book 2 vao store
s.addBook(b2);
// thong ke
s.print();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment