Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <limits>
- #include <iomanip>
- #include <fstream>
- #include "WeekBalance.h"
- #define AMOUNT 1
- template <typename T>
- double calculateAverage(T *[], int);
- SellOperation GenerateOperation();
- SellOperation InitializeOperation();
- void printHeadOfTable(std::ostream &);
- void printDataLine(std::ostream &, const SellOperation&);
- void printBottomOfTable(std::ostream &);
- int main() {
- std::srand(time(NULL));
- WeekBalance week;
- for (int i = 0; i < AMOUNT; i++) {
- week.addOperation(GenerateOperation());
- }
- do {
- system("cls");
- std::cout << "1. Create week balance or insert a new operation" << std::endl
- << "2. Output week balance" << std::endl
- << "3. Write week balance to file" << std::endl
- << "4. Read week balance from file" << std::endl
- << "5. Search in week balance" << std::endl
- << "6. Exit" << std::endl;
- switch (CheckInput::IntValidate("Your choice: ", 1, 6)) {
- //Create balance, add operation
- case 1: {
- system("cls");
- if (CheckInput::IntValidate("1. By randomizer\n0. By yourself\nHow to insert: ", 0, 1)) {
- week.addOperation(GenerateOperation());
- std::cout << "\nOperation was generated:\n\n";
- std::cout << week[week.getLength() - 1];
- }
- else {
- week.addOperation(InitializeOperation());
- }
- break;
- }
- //Output balance
- case 2: {
- system("cls");
- if (week.getOperations()) {
- printHeadOfTable(std::cout);
- for (auto item = week.begin(); item != week.end(); item++) {
- printDataLine(std::cout, *item);
- }
- printBottomOfTable(std::cout);
- }
- else {
- std::cout << "Week balance list is empty! First of all, create the list!\n";
- }
- break;
- }
- //Write to file
- case 3: {
- system("cls");
- if (CheckInput::IntValidate("1. To .txt file\n0. To .bin file\nHow to write: ", 0, 1)) {
- //TXT
- if (CheckInput::IntValidate("\n1. Write with inputed name\n0. Write to out.txt\nHow to write: ", 0, 1)) {
- //By name
- try {
- std::string fileName = CheckInput::StringValidate("File name (without .txt): ");
- if (fileName.find(".txt") != std::string::npos)
- throw InvalidFileToWrite("Invalid name for output file!");
- std::ofstream outFile(fileName + ".txt");
- if (!outFile.is_open())
- throw InvalidFileToWrite("Can not open the file!");
- outFile << week;
- outFile.close();
- std::cout << "\nData has been writen!\n";
- }
- catch (Exception &ex) {
- ex.error();
- }
- }
- else {
- //Input.txt
- try {
- std::ofstream outFile("out.txt");
- if (!outFile.is_open())
- throw InvalidFileToWrite("Can not open the file!");
- outFile << week;
- outFile.close();
- std::cout << "\nData has been writen!\n";
- }
- catch (Exception &ex) {
- ex.error();
- }
- }
- }
- else {
- //BIN
- if (CheckInput::IntValidate("\n1. Write with inputed name\n0. Write from out.bin\nHow to write: ", 0, 1)) {
- //By name
- try {
- std::string fileName = CheckInput::StringValidate("File name (without .bin): ");
- if (fileName.find(".bin") == std::string::npos)
- throw InvalidFileToWrite("Invalid name for input file!");
- std::ofstream outFile(fileName + ".bin", std::ios::binary);
- if (!outFile.is_open())
- throw InvalidFileToWrite("Can not open the file!");
- week.writeBinary(outFile);
- outFile.close();
- std::cout << "\nData has been writen!\n";
- }
- catch (Exception &ex) {
- ex.error();
- }
- }
- else {
- //Out.bin
- std::ofstream outFile("out.bin", std::ios::binary);
- if (!outFile.is_open())
- throw InvalidFileToWrite("Can not open the file!");
- week.writeBinary(outFile);
- outFile.close();
- std::cout << "\nData has been writen!\n";
- }
- }
- break;
- }
- //Read from file
- case 4: {
- system("cls");
- if (CheckInput::IntValidate("1. From .txt file\n0. From .bin file\nHow to read: ", 0, 1)) {
- //TXT
- if (CheckInput::IntValidate("\n1. Read with inputed name\n0. Read from input.txt\nHow to read: ", 0, 1)) {
- //By name
- try {
- std::string fileName = CheckInput::StringValidate("File name (without .txt): ");
- if (fileName.find(".txt") != std::string::npos)
- throw InvalidFileToRead("Invalid name for input file!");
- std::ifstream inFile(fileName + ".txt");
- if (!inFile.is_open())
- throw InvalidFileToRead("Can not open the file!");
- inFile >> week;
- inFile.close();
- std::cout << "\nData has been read!\n";
- }
- catch (Exception &ex) {
- ex.error();
- }
- }
- else {
- //Input.txt
- try {
- std::ifstream inFile("input.txt");
- if (!inFile.is_open())
- throw InvalidFileToRead("Can not open the file!");
- inFile >> week;
- inFile.close();
- std::cout << "\nData has been read!\n";
- }
- catch (Exception &ex) {
- ex.error();
- }
- }
- }
- else {
- //BIN
- if (CheckInput::IntValidate("\n1. Read with inputed name\n0. Read from input.bin\nHow to read: ", 0, 1)) {
- //By name
- try {
- std::string fileName = CheckInput::StringValidate("File name (without .bin): ");
- if (fileName.find(".bin") != std::string::npos)
- throw InvalidFileToRead("Invalid name for input file!");
- std::ifstream inFile(fileName + ".bin", std::ios::binary);
- if (!inFile.is_open())
- throw InvalidFileToRead("Can not open the file!");
- week.readBinary(inFile);
- inFile.close();
- std::cout << "\nData has been read!\n";
- }
- catch (Exception &ex) {
- ex.error();
- }
- }
- else {
- //Input.bin
- std::ifstream inFile("input.bin", std::ios::binary);
- if (!inFile.is_open())
- throw InvalidFileToRead("Can not open the file!");
- week.readBinary(inFile);
- inFile.close();
- std::cout << "\nData has been read!\n";
- }
- }
- break;
- }
- //Search in
- case 5: {
- break;
- }
- //Exit
- case 6: {
- system("pause");
- return 0;
- }
- }
- } while (CheckInput::IntValidate("\nContinue? (1-yes, 0-no): ", 0, 1));
- }
- template <typename T>
- double calculateAverage(T *arr[], int size) {
- double avg = 0;
- for (int i = 0; i < size; i++) {
- avg += arr[i] / size;
- }
- return avg;
- }
- SellOperation GenerateOperation() {
- const std::string names[] = { "Christina", "Alex", "John", "Anna", "Olga", "Vova", "Maria", "Maya", "Vlad", "Ira" };
- const std::string surnames[] = { "Gorokhovska", "Melnyk", "Shevchenko", "Boyko", "Kovalenko", "Bondarenko", "Tkachenko", "Kovalchuk", "Kravchenko", "Koval" };
- 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" };
- const std::string authorsNames[] = { "Marcel", "James", "Migel", "Scott", "Leo", "William", "Dante", "Fyodor", "Lewis", "Virginia" };
- const std::string authorsSurnames[] = { "Proust", "Joyce", "de Cervantes", "Fitzgerald", "Tolstoy", "Shakespeare", "Alighieri", "Dostoyevsky", "Carroll", "Wolf" };
- SellOperation temp;
- //Seller
- temp.getSeller().setName(names[rand() % 10]);
- temp.getSeller().setSurname(surnames[rand() % 10]);
- //Seller - birth date
- temp.getSeller().getBirth().setDay(1 + rand() % 31);
- temp.getSeller().getBirth().setMonth(1 + rand() % 12);
- temp.getSeller().getBirth().setYear(1900 + rand() % 120);
- //Seller - enrollment date
- temp.getSeller().getEnrollmentDate().setDay(1 + rand() % 31);
- temp.getSeller().getEnrollmentDate().setMonth(1 + rand() % 12);
- temp.getSeller().getEnrollmentDate().setYear(1900 + rand() % 120);
- //Book
- temp.getBook().setName(bookName[rand() % 10]);
- temp.getBook().setPrice(rand() % 200);
- temp.setQuantity(rand() % 10);
- //Book - Author
- temp.getBook().getAuthor().setName(authorsNames[rand() % 10]);
- temp.getBook().getAuthor().setSurname(authorsSurnames[rand() % 10]);
- //Book - Author - birth date
- temp.getBook().getAuthor().getBirth().setDay(1 + rand() % 31);
- temp.getBook().getAuthor().getBirth().setMonth(1 + rand() % 12);
- temp.getBook().getAuthor().getBirth().setYear(1900 + rand() % 120);
- //Sell date
- temp.getSellDate().setDay(1 + rand() % 31);
- temp.getSellDate().setMonth(1 + rand() % 12);
- temp.getSellDate().setYear(1900 + rand() % 120);
- return temp;
- }
- SellOperation InitializeOperation() {
- SellOperation temp;
- std::cout << "Input operation info:\n";
- //Seller
- std::cout << "Seller info:\n";
- temp.getSeller().setName(CheckInput::StringValidate(" Name: "));
- temp.getSeller().setSurname(CheckInput::StringValidate(" Surname: "));
- std::cout << " Birth date:\n";
- temp.getSeller().getBirth().setDay(CheckInput::IntValidate(" Day: ", 1, 31));
- temp.getSeller().getBirth().setMonth(CheckInput::IntValidate(" Month: ", 1, 12));
- temp.getSeller().getBirth().setYear(CheckInput::IntValidate(" Year: ", 1900, 2000));
- std::cout << " Enrollment date:\n";
- temp.getSeller().getEnrollmentDate().setDay(CheckInput::IntValidate(" Day: ", 1, 31));
- temp.getSeller().getEnrollmentDate().setMonth(CheckInput::IntValidate(" Month: ", 1, 12));
- temp.getSeller().getEnrollmentDate().setYear(CheckInput::IntValidate(" Year: ", 1900, 2000));
- //Book
- std::cout << "Book info:\n";
- temp.getBook().setName(CheckInput::StringValidate(" Name: "));
- temp.getBook().setPrice(CheckInput::DoubleValidate(" Price: ", 1, 200));
- temp.setQuantity(CheckInput::IntValidate(" Quantity: ", 1, 999));
- std::cout << " Author info:\n";
- temp.getBook().getAuthor().setName(CheckInput::StringValidate(" Name: "));
- temp.getBook().getAuthor().setSurname(CheckInput::StringValidate(" Surname: "));
- std::cout << " Birth date:\n";
- temp.getBook().getAuthor().getBirth().setDay(CheckInput::IntValidate(" Day: ", 1, 31));
- temp.getBook().getAuthor().getBirth().setMonth(CheckInput::IntValidate(" Month: ", 1, 12));
- temp.getBook().getAuthor().getBirth().setYear(CheckInput::IntValidate(" Year: ", 1900, 2000));
- //Sell date
- std::cout << "Sell date:\n";
- temp.getSellDate().setDay(CheckInput::IntValidate(" Day: ", 1, 31));
- temp.getSellDate().setMonth(CheckInput::IntValidate(" Month: ", 1, 12));
- temp.getSellDate().setYear(CheckInput::IntValidate(" Year: ", 2000, 2020));
- return temp;
- }
- void printHeadOfTable(std::ostream &out) {
- std::stringstream first;
- std::stringstream mid;
- std::stringstream last;
- first << std::right << std::setfill(char(205)) << char(201)
- << std::setw(14) << "" << char(209) //Sale date
- << std::setw(27) << "" << char(209) //Seller name
- << std::setw(27) << "" << char(209) //Book name
- << std::setw(17) << "" << char(209) //Book price
- << std::setw(12) << "" << char(209) //Quantity
- << std::setw(17) << "" //Sum
- << char(187);
- mid << std::right << std::setfill(' ') << char(186)
- << std::setw(14) << " Sale date " << char(179)
- << std::setw(27) << " Seller name " << char(179)
- << std::setw(27) << " Book name " << char(179)
- << std::setw(17) << " Book price " << char(179)
- << std::setw(12) << " Quantity " << char(179)
- << std::setw(17) << " Sum "
- << char(186);
- last << std::right << std::setfill(char(205)) << char(204)
- << std::setw(14) << "" << char(216) //Sale date
- << std::setw(27) << "" << char(216) //Seller name
- << std::setw(27) << "" << char(216) //Book name
- << std::setw(17) << "" << char(216) //Book price
- << std::setw(12) << "" << char(216) //Quantity
- << std::setw(17) << "" //Sum
- << char(185);
- out << first.str() << std::endl;
- out << mid.str() << std::endl;
- out << last.str() << std::endl;
- }
- void printDataLine(std::ostream &out, const SellOperation& op) {
- std::stringstream line;
- line << std::right << std::setfill(' ') << char(186)
- << ' ' << std::setw(12) << op.getSellDate().getDateString() << ' ' << char(179)
- << ' ' << std::setw(25) << op.getSeller().getName() + ' ' + op.getSeller().getSurname() << ' ' << char(179)
- << ' ' << std::setw(25) << op.getBook().getName() << ' ' << char(179)
- << ' ' << std::setw(11) << std::setprecision(2) << std::fixed << op.getBook().getPrice() << " USD " << char(179)
- << ' ' << std::setw(10) << op.getQuantity() << ' ' << char(179)
- << ' ' << std::setw(11) << op.getSum() << " USD "
- << char(186);
- out << line.str() << std::endl;
- }
- void printBottomOfTable(std::ostream &out) {
- std::stringstream bottom;
- bottom << std::right << std::setfill(char(205)) << char(200)
- << std::setw(14) << "" << char(207) //Sale date
- << std::setw(27) << "" << char(207) //Seller name
- << std::setw(27) << "" << char(207) //Book name
- << std::setw(17) << "" << char(207) //Book price
- << std::setw(12) << "" << char(207) //Quantity
- << std::setw(17) << "" //Sum
- << char(188);
- out << bottom.str() << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement