Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. class Author {
  8.  
  9. private:
  10. string name, id, email;
  11.  
  12. public:
  13.  
  14. /**
  15. * Init author with name, id, email
  16. * @param name
  17. * @param id
  18. * @param email
  19. */
  20. Author(string name, string id, string email) {
  21. Author::name = name;
  22. Author::id = id;
  23. Author::email = email;
  24. }
  25.  
  26. /**
  27. * Get author name
  28. * @return
  29. */
  30. string getName() {
  31. return name;
  32. }
  33.  
  34. /**
  35. * Get author identity
  36. * @return
  37. */
  38. string getId() {
  39. return id;
  40. }
  41.  
  42. /**
  43. * Get author email address
  44. * @return
  45. */
  46. string getEmail() {
  47. return email;
  48. }
  49.  
  50. /**
  51. * Set author name
  52. * @param name
  53. */
  54. void setName(string name) {
  55. Author::name = name;
  56. }
  57.  
  58. /**
  59. * Set author Identity
  60. * @param id
  61. */
  62. void setId(string id) {
  63. Author::id = id;
  64. }
  65.  
  66. /**
  67. * Set author 's email
  68. * @param email
  69. */
  70. void setEmail(const string &email) {
  71. Author::email = email;
  72. }
  73. };
  74.  
  75.  
  76. class Book {
  77. private:
  78. string name;
  79. double price;
  80. int sold;
  81. Author author;
  82.  
  83. public:
  84.  
  85. /**
  86. * Init Book with name,price,sold, author
  87. * @param name
  88. * @param price
  89. * @param sold
  90. * @param author
  91. */
  92. Book(const string &name, double price, int sold, const Author &author) : name(name), price(price), sold(sold),
  93. author(author) {}
  94. /**
  95. * Set book name
  96. * @param name
  97. */
  98. void setName(string name) {
  99. Book::name = name;
  100. }
  101.  
  102. /**
  103. * Set book price
  104. * @param price
  105. */
  106. void setPrice(double price) {
  107. Book::price = price;
  108. }
  109.  
  110. /**
  111. * Set num of sold
  112. * @param sold
  113. */
  114. void setSold(int sold) {
  115. Book::sold = sold;
  116. }
  117.  
  118. /**
  119. * Set book author
  120. * @param author
  121. */
  122. void setAuthor(Author author) {
  123. Book::author = author;
  124. }
  125.  
  126. /**
  127. * Get name of book
  128. * @return
  129. */
  130. string getName() {
  131. return name;
  132. }
  133.  
  134. /**
  135. * Get book's price
  136. */
  137. double getPrice() const {
  138. return price;
  139. }
  140.  
  141. /**
  142. * Get num of sold
  143. * @return
  144. */
  145. int getSold() const {
  146. return sold;
  147. }
  148.  
  149. /**
  150. * Get total of sold price
  151. * @return
  152. */
  153. double getTotalSoldPrice() {
  154. return Book::sold * Book::price;
  155. }
  156.  
  157. /**
  158. * Get book author
  159. * @return
  160. */
  161. Author getAuthor() {
  162. return author;
  163. }
  164.  
  165. /**
  166. * Print book details
  167. */
  168. void print() {
  169. cout << "Book name: " << getName() << endl;
  170. cout << "Author: " << getAuthor().getName() << " - " << getAuthor().getId() << " - " << getAuthor().getEmail()
  171. << endl;
  172. cout << "price ($): " << getPrice() << endl;
  173. cout << "Num of sold: " << getSold() << endl;
  174. cout << "Total sold ($): " << getTotalSoldPrice() << endl;
  175. }
  176. };
  177.  
  178. class Store {
  179.  
  180. private:
  181. vector<Book> books;
  182. Book *bookHasMaximumSold;
  183.  
  184. public:
  185. /**
  186. * Store initial
  187. */
  188. Store() {
  189. bookHasMaximumSold = NULL;
  190. }
  191.  
  192. /**
  193. * Get all store's books
  194. * @return
  195. */
  196. vector<Book> getBooks() {
  197. return books;
  198. }
  199.  
  200. /**
  201. * Add new book
  202. * @param book
  203. */
  204. Store *add(Book book) {
  205. if (this->bookHasMaximumSold == NULL) {
  206. this->bookHasMaximumSold = &book;
  207. } else {
  208. if (this->bookHasMaximumSold->getSold() < book.getSold()) {
  209. this->bookHasMaximumSold = &book;
  210. }
  211. }
  212. Store::books.push_back(book);
  213.  
  214. return this;
  215. }
  216.  
  217. /**
  218. * Get book has maximum sold
  219. * @return
  220. */
  221. Book *getMaximum() {
  222. return bookHasMaximumSold;
  223. }
  224.  
  225. /**
  226. * Print store report
  227. */
  228. void print() {
  229.  
  230. cout << "Store Report:" << endl;
  231. for (int i = 0; i < books.size(); ++i) {
  232. books[i].print();
  233. cout << "---------------------------------------------------------------------" << endl;
  234. }
  235. cout << "Best Seller:" << endl;
  236. Store::getMaximum()->print();
  237. cout << "---------------------------------------------------------------------" << endl;
  238.  
  239. }
  240.  
  241. };
  242.  
  243. int main() {
  244.  
  245. Author a1("Nguyen Hoang Hai", "id-001", "haispdn@gmail.com");
  246. Author a2("Le Thanh Cong", "id-002", "cong@ued.udn.vn");
  247. Author a3("Tran Quoc Chien", "id-003", "chien@ued.udn.vn");
  248.  
  249. Book b1("C++ nang cao", 100, 500, a1);
  250. Book b2("Java nang cao", 200, 700, a2);
  251. Book b3("Ly thuyet do thi", 205, 800, a3);
  252.  
  253. Store *s = new Store();
  254. s->add(b1)->add(b2)->add(b3)->print();
  255.  
  256. return 0;
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement