Advertisement
Guest User

Untitled

a guest
Jun 1st, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Заголовочный файл stock.h
  2. #ifndef STOCK_H
  3. #define STOCK_H
  4. #include <fstream>
  5. #include <iostream>
  6. #include "publishinghouse.h"
  7.  
  8. namespace stock
  9. {
  10.     extern std::vector<Book> booksStock;
  11.     extern std::vector<PublishingHouse> phVector;
  12.  
  13.     void fillBooksStock();
  14.     void fillPublishingHouse();
  15.     void changeBooksStock();
  16. }
  17.  
  18.  
  19. Исходный код пространства имен stock stock.cpp
  20. #include "stock.h"
  21.  
  22. std::vector<PublishingHouse> stock::phVector = {
  23.     PublishingHouse("AST", 3),
  24.     PublishingHouse("Exmo", 2),
  25.     PublishingHouse("Onyx", 4),
  26.     PublishingHouse("Machaon", 2),
  27.     PublishingHouse("Egmont", 3)
  28. };
  29. std::vector<Book> stock::booksStock = {};
  30.  
  31. void stock::fillBooksStock()
  32. {
  33.     std::string line;
  34.     std::string value;
  35.     std::ifstream file("booksStock.txt");
  36.     if (file)
  37.     {
  38.         while (std::getline(file, line))
  39.         {
  40.             if (line != "")
  41.             {
  42.                 Book* book = new Book(std::atoi(line.c_str()));
  43.                 std::getline(file, line);
  44.                 book->set_Title(line);
  45.                 std::getline(file, line);
  46.                 book->set_Author(line);
  47.                 std::getline(file, line);
  48.                 book->set_Count(std::atoi(line.c_str()));
  49.                 std::getline(file, line);
  50.                 book->set_PicPath(line);
  51.                 std::getline(file, line);
  52.                 book->set_Genre(line);
  53.                 std::getline(file, line);
  54.                 book->set_PublishingHouse(line);
  55.                 std::getline(file, line);
  56.                 book->set_Rating(std::atoi(line.c_str()));
  57.                 std::getline(file, line);
  58.                 book->set_ReaderCategory(line);
  59.                 std::getline(file, line);
  60.                 book->set_CountOfPages(std::atoi(line.c_str()));
  61.                 std::getline(file, line);
  62.                 book->set_RetailMarkup(std::atoi(line.c_str()));
  63.                 std::getline(file, line);
  64.                 book->set_Price(std::atoi(line.c_str()));
  65.                 std::getline(file, line);
  66.                 if (line == "True")
  67.                     book->set_IsBookNew(true);
  68.                 else
  69.                     book->set_IsBookNew(false);
  70.                 booksStock.push_back(*book);
  71.             }
  72.         }
  73.         file.close();
  74.     }
  75. }
  76.  
  77.  
  78. void stock::fillPublishingHouse()
  79. {
  80.     std::ifstream ordersFile("orders.txt");
  81.     std::ifstream wishlistFile("informAboutAcquisitions.txt");
  82.     std::string detail;
  83.     std::string value;
  84.     for (PublishingHouse &ph : phVector)
  85.     {
  86.         ph.reset_Counter();
  87.         ph.publishersDeliveryStock.clear();
  88.     }
  89.     if (ordersFile.is_open())
  90.     {
  91.         while(static_cast<void>(ordersFile >> detail), std::getline(ordersFile >> std::ws, value))
  92.         {
  93.             for (Book &book : booksStock)
  94.             {
  95.                 for (PublishingHouse &ph : phVector)
  96.                 {
  97.                     if (value == std::to_string(book.get_Id()) && ph.get_Name() == book.get_PublishingHouse())
  98.                     {
  99.                         ph.increase_Counter();
  100.                         break;
  101.                     }
  102.                 }
  103.             }
  104.         }
  105.     }
  106.     ordersFile.close();
  107.     if (wishlistFile.is_open())
  108.     {
  109.         while(static_cast<void>(wishlistFile >> detail), std::getline(wishlistFile >> std::ws, value))
  110.         {
  111.             for (Book &book : booksStock)
  112.             {
  113.                 for (PublishingHouse &ph : phVector)
  114.                 {
  115.                     if (value == std::to_string(book.get_Id()) && ph.get_Name() == book.get_PublishingHouse())
  116.                     {
  117.                         ph.increase_Counter();
  118.                         break;
  119.                     }
  120.                 }
  121.             }
  122.         }
  123.     }
  124.     wishlistFile.close();
  125.     for (Book book : booksStock)
  126.     {
  127.         for (PublishingHouse &ph : phVector)
  128.         {
  129.             if (book.get_Count() < 5 && ph.get_Name() == book.get_PublishingHouse())
  130.             {
  131.                 ph.publishersDeliveryStock.push_back(book);
  132.                 break;
  133.             }
  134.         }
  135.     }
  136. }
  137. //Вычитание заказов, сделанных до данного запуска программы
  138. void stock::changeBooksStock()
  139. {
  140.     std::ifstream ordersFile("orders.txt");
  141.     std::string detail;
  142.     std::string value;
  143.     std::string line;
  144.     if (ordersFile.is_open())
  145.     {
  146.         while(static_cast<void>(ordersFile >> detail), std::getline(ordersFile >> std::ws, value))
  147.         {
  148.             for (Book &book : booksStock)
  149.             {
  150.                 if (value == std::to_string(book.get_Id()))
  151.                 {
  152.                     std::getline(ordersFile, line);
  153.                     std::getline(ordersFile, line);
  154.                     std::getline(ordersFile, line);
  155.                     std::getline(ordersFile, line);
  156.                     static_cast<void>(ordersFile >> detail), std::getline(ordersFile, line);
  157.                     book.set_Count(book.get_Count()-std::atoi(line.c_str()));
  158.                 }
  159.             }
  160.         }
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement