Advertisement
Guest User

Untitled

a guest
Dec 14th, 2020
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <limits>
  5. #include <iomanip>
  6. #include <fstream>
  7.  
  8. #include "WeekBalance.h"
  9.  
  10. #define AMOUNT 1
  11.  
  12. template <typename T>
  13. double calculateAverage(T *[], int);
  14.  
  15. SellOperation GenerateOperation();
  16. SellOperation InitializeOperation();
  17.  
  18. void printHeadOfTable(std::ostream &);
  19. void printDataLine(std::ostream &, const SellOperation&);
  20. void printBottomOfTable(std::ostream &);
  21.  
  22. int main() {
  23.     std::srand(time(NULL));
  24.  
  25.     WeekBalance week;
  26.     for (int i = 0; i < AMOUNT; i++) {
  27.         week.addOperation(GenerateOperation());
  28.     }
  29.  
  30.     do {
  31.         system("cls");
  32.  
  33.         std::cout << "1. Create week balance or insert a new operation" << std::endl
  34.             << "2. Output week balance" << std::endl
  35.             << "3. Write week balance to file" << std::endl
  36.             << "4. Read week balance from file" << std::endl
  37.             << "5. Search in week balance" << std::endl
  38.             << "6. Exit" << std::endl;
  39.  
  40.         switch (CheckInput::IntValidate("Your choice: ", 1, 6)) {
  41.         //Create balance, add operation
  42.         case 1: {
  43.             system("cls");
  44.             if (CheckInput::IntValidate("1. By randomizer\n0. By yourself\nHow to insert: ", 0, 1)) {
  45.                 week.addOperation(GenerateOperation());
  46.                 std::cout << "\nOperation was generated:\n\n";
  47.                 std::cout << week[week.getLength() - 1];
  48.             }
  49.             else {
  50.                 week.addOperation(InitializeOperation());
  51.             }
  52.             break;
  53.         }
  54.  
  55.         //Output balance
  56.         case 2: {
  57.             system("cls");
  58.             if (week.getOperations()) {
  59.                 printHeadOfTable(std::cout);
  60.                 for (auto item = week.begin(); item != week.end(); item++) {
  61.                     printDataLine(std::cout, *item);
  62.                 }
  63.                 printBottomOfTable(std::cout);
  64.             }
  65.             else {
  66.                 std::cout << "Week balance list is empty! First of all, create the list!\n";
  67.             }
  68.             break;
  69.         }
  70.  
  71.         //Write to file
  72.         case 3: {
  73.             system("cls");
  74.             if (CheckInput::IntValidate("1. To .txt file\n0. To .bin file\nHow to write: ", 0, 1)) {
  75.                 //TXT
  76.                 if (CheckInput::IntValidate("\n1. Write with inputed name\n0. Write to out.txt\nHow to write: ", 0, 1)) {
  77.                     //By name
  78.                     try {
  79.                         std::string fileName = CheckInput::StringValidate("File name (without .txt): ");
  80.  
  81.                         if (fileName.find(".txt") != std::string::npos)
  82.                             throw InvalidFileToWrite("Invalid name for output file!");
  83.  
  84.                         std::ofstream outFile(fileName + ".txt");
  85.  
  86.                         if (!outFile.is_open())
  87.                             throw InvalidFileToWrite("Can not open the file!");
  88.  
  89.                         outFile << week;
  90.                         outFile.close();
  91.                         std::cout << "\nData has been writen!\n";
  92.                     }
  93.                     catch (Exception &ex) {
  94.                         ex.error();
  95.                     }
  96.                 }
  97.                 else {
  98.                     //Input.txt
  99.                     try {
  100.                         std::ofstream outFile("out.txt");
  101.  
  102.                         if (!outFile.is_open())
  103.                             throw InvalidFileToWrite("Can not open the file!");
  104.  
  105.                         outFile << week;
  106.                         outFile.close();
  107.                         std::cout << "\nData has been writen!\n";
  108.                     }
  109.                     catch (Exception &ex) {
  110.                         ex.error();
  111.                     }
  112.                 }
  113.             }
  114.             else {
  115.                 //BIN
  116.                 if (CheckInput::IntValidate("\n1. Write with inputed name\n0. Write from out.bin\nHow to write: ", 0, 1)) {
  117.                     //By name
  118.                     try {
  119.                         std::string fileName = CheckInput::StringValidate("File name (without .bin): ");
  120.  
  121.                         if (fileName.find(".bin") == std::string::npos)
  122.                             throw InvalidFileToWrite("Invalid name for input file!");
  123.  
  124.                         std::ofstream outFile(fileName + ".bin", std::ios::binary);
  125.  
  126.                         if (!outFile.is_open())
  127.                             throw InvalidFileToWrite("Can not open the file!");
  128.  
  129.                         week.writeBinary(outFile);
  130.                         outFile.close();
  131.                         std::cout << "\nData has been writen!\n";
  132.                     }
  133.                     catch (Exception &ex) {
  134.                         ex.error();
  135.                     }
  136.                 }
  137.                 else {
  138.                     //Out.bin
  139.                     std::ofstream outFile("out.bin", std::ios::binary);
  140.  
  141.                     if (!outFile.is_open())
  142.                         throw InvalidFileToWrite("Can not open the file!");
  143.  
  144.                     week.writeBinary(outFile);
  145.                     outFile.close();
  146.                     std::cout << "\nData has been writen!\n";
  147.                 }
  148.             }
  149.             break;
  150.         }
  151.  
  152.         //Read from file
  153.         case 4: {
  154.             system("cls");
  155.             if (CheckInput::IntValidate("1. From .txt file\n0. From .bin file\nHow to read: ", 0, 1)) {
  156.                 //TXT
  157.                 if (CheckInput::IntValidate("\n1. Read with inputed name\n0. Read from input.txt\nHow to read: ", 0, 1)) {
  158.                     //By name
  159.                     try {
  160.                         std::string fileName = CheckInput::StringValidate("File name (without .txt): ");
  161.  
  162.                         if (fileName.find(".txt") != std::string::npos)
  163.                             throw InvalidFileToRead("Invalid name for input file!");
  164.  
  165.                         std::ifstream inFile(fileName + ".txt");
  166.  
  167.                         if (!inFile.is_open())
  168.                             throw InvalidFileToRead("Can not open the file!");
  169.  
  170.                         inFile >> week;
  171.                         inFile.close();
  172.                         std::cout << "\nData has been read!\n";
  173.                     }
  174.                     catch (Exception &ex) {
  175.                         ex.error();
  176.                     }
  177.                 }
  178.                 else {
  179.                     //Input.txt
  180.                     try {
  181.                         std::ifstream inFile("input.txt");
  182.  
  183.                         if (!inFile.is_open())
  184.                             throw InvalidFileToRead("Can not open the file!");
  185.  
  186.                         inFile >> week;
  187.                         inFile.close();
  188.                         std::cout << "\nData has been read!\n";
  189.                     }
  190.                     catch (Exception &ex) {
  191.                         ex.error();
  192.                     }
  193.                 }
  194.             }
  195.             else {
  196.                 //BIN
  197.                 if (CheckInput::IntValidate("\n1. Read with inputed name\n0. Read from input.bin\nHow to read: ", 0, 1)) {
  198.                     //By name
  199.                     try {
  200.                         std::string fileName = CheckInput::StringValidate("File name (without .bin): ");
  201.  
  202.                         if (fileName.find(".bin") != std::string::npos)
  203.                             throw InvalidFileToRead("Invalid name for input file!");
  204.  
  205.                         std::ifstream inFile(fileName + ".bin", std::ios::binary);
  206.  
  207.                         if (!inFile.is_open())
  208.                             throw InvalidFileToRead("Can not open the file!");
  209.  
  210.                         week.readBinary(inFile);
  211.                         inFile.close();
  212.                         std::cout << "\nData has been read!\n";
  213.                     }
  214.                     catch (Exception &ex) {
  215.                         ex.error();
  216.                     }
  217.                 }
  218.                 else {
  219.                     //Input.bin
  220.                     std::ifstream inFile("input.bin", std::ios::binary);
  221.  
  222.                     if (!inFile.is_open())
  223.                         throw InvalidFileToRead("Can not open the file!");
  224.  
  225.                     week.readBinary(inFile);
  226.                     inFile.close();
  227.                     std::cout << "\nData has been read!\n";
  228.                 }
  229.             }
  230.             break;
  231.         }
  232.  
  233.         //Search in
  234.         case 5: {
  235.  
  236.             break;
  237.         }
  238.  
  239.         //Exit
  240.         case 6: {
  241.             system("pause");
  242.             return 0;
  243.         }
  244.         }
  245.  
  246.     } while (CheckInput::IntValidate("\nContinue? (1-yes, 0-no): ", 0, 1));
  247. }
  248.  
  249. template <typename T>
  250. double calculateAverage(T *arr[], int size) {
  251.     double avg = 0;
  252.     for (int i = 0; i < size; i++) {
  253.         avg += arr[i] / size;
  254.     }
  255.     return avg;
  256. }
  257.  
  258. SellOperation GenerateOperation() {
  259.     const std::string names[] = { "Christina", "Alex", "John", "Anna", "Olga", "Vova", "Maria", "Maya", "Vlad", "Ira" };
  260.     const std::string surnames[] = { "Gorokhovska", "Melnyk", "Shevchenko", "Boyko", "Kovalenko", "Bondarenko", "Tkachenko", "Kovalchuk", "Kravchenko", "Koval" };
  261.     const std::string bookName[] = { "In Search of Lost Time", "Ulysses", "Don Quixote", "The Great Gatsby", "War and Peace", "Hamlet", "The Divine Comedy", "The Brothers Karamazov", "Alice in Wonderland", "To the Lighthouse" };
  262.     const std::string authorsNames[] = { "Marcel", "James", "Migel", "Scott", "Leo", "William", "Dante", "Fyodor", "Lewis", "Virginia" };
  263.     const std::string authorsSurnames[] = { "Proust", "Joyce", "de Cervantes", "Fitzgerald", "Tolstoy", "Shakespeare", "Alighieri", "Dostoyevsky", "Carroll", "Wolf" };
  264.  
  265.     SellOperation temp;
  266.  
  267.     //Seller
  268.     temp.getSeller().setName(names[rand() % 10]);
  269.     temp.getSeller().setSurname(surnames[rand() % 10]);
  270.     //Seller - birth date
  271.     temp.getSeller().getBirth().setDay(1 + rand() % 31);
  272.     temp.getSeller().getBirth().setMonth(1 + rand() % 12);
  273.     temp.getSeller().getBirth().setYear(1900 + rand() % 120);
  274.     //Seller - enrollment date
  275.     temp.getSeller().getEnrollmentDate().setDay(1 + rand() % 31);
  276.     temp.getSeller().getEnrollmentDate().setMonth(1 + rand() % 12);
  277.     temp.getSeller().getEnrollmentDate().setYear(1900 + rand() % 120);
  278.     //Book
  279.     temp.getBook().setName(bookName[rand() % 10]);
  280.     temp.getBook().setPrice(rand() % 200);
  281.     temp.setQuantity(rand() % 10);
  282.     //Book - Author
  283.     temp.getBook().getAuthor().setName(authorsNames[rand() % 10]);
  284.     temp.getBook().getAuthor().setSurname(authorsSurnames[rand() % 10]);
  285.     //Book - Author - birth date
  286.     temp.getBook().getAuthor().getBirth().setDay(1 + rand() % 31);
  287.     temp.getBook().getAuthor().getBirth().setMonth(1 + rand() % 12);
  288.     temp.getBook().getAuthor().getBirth().setYear(1900 + rand() % 120);
  289.     //Sell date
  290.     temp.getSellDate().setDay(1 + rand() % 31);
  291.     temp.getSellDate().setMonth(1 + rand() % 12);
  292.     temp.getSellDate().setYear(1900 + rand() % 120);
  293.  
  294.     return temp;
  295. }
  296.  
  297. SellOperation InitializeOperation() {
  298.     SellOperation temp;
  299.  
  300.     std::cout << "Input operation info:\n";
  301.     //Seller
  302.     std::cout << "Seller info:\n";
  303.     temp.getSeller().setName(CheckInput::StringValidate(" Name: "));
  304.     temp.getSeller().setSurname(CheckInput::StringValidate(" Surname: "));
  305.     std::cout << " Birth date:\n";
  306.     temp.getSeller().getBirth().setDay(CheckInput::IntValidate("  Day: ", 1, 31));
  307.     temp.getSeller().getBirth().setMonth(CheckInput::IntValidate("  Month: ", 1, 12));
  308.     temp.getSeller().getBirth().setYear(CheckInput::IntValidate("  Year: ", 1900, 2000));
  309.     std::cout << " Enrollment date:\n";
  310.     temp.getSeller().getEnrollmentDate().setDay(CheckInput::IntValidate("  Day: ", 1, 31));
  311.     temp.getSeller().getEnrollmentDate().setMonth(CheckInput::IntValidate("  Month: ", 1, 12));
  312.     temp.getSeller().getEnrollmentDate().setYear(CheckInput::IntValidate("  Year: ", 1900, 2000));
  313.     //Book
  314.     std::cout << "Book info:\n";
  315.     temp.getBook().setName(CheckInput::StringValidate(" Name: "));
  316.     temp.getBook().setPrice(CheckInput::DoubleValidate(" Price: ", 1, 200));
  317.     temp.setQuantity(CheckInput::IntValidate(" Quantity: ", 1, 999));
  318.     std::cout << " Author info:\n";
  319.     temp.getBook().getAuthor().setName(CheckInput::StringValidate("  Name: "));
  320.     temp.getBook().getAuthor().setSurname(CheckInput::StringValidate("  Surname: "));
  321.     std::cout << "  Birth date:\n";
  322.     temp.getBook().getAuthor().getBirth().setDay(CheckInput::IntValidate("   Day: ", 1, 31));
  323.     temp.getBook().getAuthor().getBirth().setMonth(CheckInput::IntValidate("   Month: ", 1, 12));
  324.     temp.getBook().getAuthor().getBirth().setYear(CheckInput::IntValidate("   Year: ", 1900, 2000));
  325.     //Sell date
  326.     std::cout << "Sell date:\n";
  327.     temp.getSellDate().setDay(CheckInput::IntValidate(" Day: ", 1, 31));
  328.     temp.getSellDate().setMonth(CheckInput::IntValidate(" Month: ", 1, 12));
  329.     temp.getSellDate().setYear(CheckInput::IntValidate(" Year: ", 2000, 2020));
  330.  
  331.     return temp;
  332. }
  333.  
  334. void printHeadOfTable(std::ostream &out) {
  335.     std::stringstream first;
  336.     std::stringstream mid;
  337.     std::stringstream last;
  338.  
  339.     first << std::right << std::setfill(char(205)) << char(201)
  340.         << std::setw(14) << "" << char(209) //Sale date
  341.         << std::setw(27) << "" << char(209) //Seller name
  342.         << std::setw(27) << "" << char(209) //Book name
  343.         << std::setw(17) << "" << char(209) //Book price
  344.         << std::setw(12) << "" << char(209) //Quantity
  345.         << std::setw(17) << ""              //Sum
  346.         << char(187);
  347.  
  348.     mid << std::right << std::setfill(' ') << char(186)
  349.         << std::setw(14) << " Sale date " << char(179)
  350.         << std::setw(27) << " Seller name " << char(179)
  351.         << std::setw(27) << " Book name " << char(179)
  352.         << std::setw(17) << " Book price " << char(179)
  353.         << std::setw(12) << " Quantity " << char(179)
  354.         << std::setw(17) << " Sum "
  355.         << char(186);
  356.    
  357.     last << std::right << std::setfill(char(205)) << char(204)
  358.         << std::setw(14) << "" << char(216) //Sale date
  359.         << std::setw(27) << "" << char(216) //Seller name
  360.         << std::setw(27) << "" << char(216) //Book name
  361.         << std::setw(17) << "" << char(216) //Book price
  362.         << std::setw(12) << "" << char(216) //Quantity
  363.         << std::setw(17) << ""              //Sum
  364.         << char(185);
  365.     out << first.str() << std::endl;
  366.     out << mid.str() << std::endl;
  367.     out << last.str() << std::endl;
  368. }
  369.  
  370. void printDataLine(std::ostream &out, const SellOperation& op) {
  371.     std::stringstream line;
  372.     line << std::right << std::setfill(' ') << char(186)
  373.         << ' ' << std::setw(12) << op.getSellDate().getDateString() << ' ' << char(179)
  374.         << ' ' << std::setw(25) << op.getSeller().getName() + ' ' + op.getSeller().getSurname() << ' ' << char(179)
  375.         << ' ' << std::setw(25) << op.getBook().getName() << ' ' << char(179)
  376.         << ' ' << std::setw(11) << std::setprecision(2) << std::fixed << op.getBook().getPrice() << " USD " << char(179)
  377.         << ' ' << std::setw(10) << op.getQuantity() << ' ' << char(179)
  378.         << ' ' << std::setw(11) << op.getSum() << " USD "
  379.         << char(186);
  380.     out << line.str() << std::endl;
  381. }
  382.  
  383. void printBottomOfTable(std::ostream &out) {
  384.     std::stringstream bottom;
  385.     bottom << std::right << std::setfill(char(205)) << char(200)
  386.         << std::setw(14) << "" << char(207) //Sale date
  387.         << std::setw(27) << "" << char(207) //Seller name
  388.         << std::setw(27) << "" << char(207) //Book name
  389.         << std::setw(17) << "" << char(207) //Book price
  390.         << std::setw(12) << "" << char(207) //Quantity
  391.         << std::setw(17) << ""              //Sum
  392.         << char(188);
  393.     out << bottom.str() << std::endl;
  394. }
  395.  
  396.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement