SHARE
TWEET

Untitled

a guest Nov 13th, 2019 117 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3. #include <cassert>
  4. #include <fstream>
  5. #include <ctime>
  6.  
  7. struct Wallet
  8. {
  9.     char owner[256];
  10.     unsigned int id;
  11.     double fiatMoney;
  12.  
  13.     Wallet(static char const owner[256], unsigned int id = -1, double fiatMoney = 0.0) : id(id), fiatMoney(fiatMoney)
  14.     {
  15.         //assert(strlen(owner) < sizeof(this->owner));
  16.         strcpy_s(this->owner, owner);
  17.         this->id = id;
  18.         this->fiatMoney = fiatMoney;
  19.     }
  20. };
  21.  
  22. struct Transaction
  23. {
  24.     long long time;
  25.     unsigned int senderId;
  26.     unsigned int resiverId;
  27.     double fmiCoints;
  28.  
  29.     Transaction(long long time = -1, unsigned int giverId = -1, unsigned int resiverId = -1, double fmiCoints = 0.0) : time(time), senderId(giverId), resiverId(resiverId), fmiCoints(fmiCoints) {};
  30. };
  31.  
  32. struct Order
  33. {
  34.     enum Type { SELL, BUY } type;
  35.     unsigned int walletId;
  36.     double fmiCoints;
  37.  
  38.     Order(Order::Type type, unsigned int walletId, double fmiCoints) : type(type), walletId(walletId), fmiCoints(fmiCoints) {}
  39. };
  40.  
  41. std::vector<Wallet> allWallets; //unique wallets -- maybe sort?
  42. std::vector<Transaction> allTransactions; //All Transactions (Not Unique)
  43. std::vector<Order> allOrders;
  44.  
  45. int findWalletById(unsigned int walletId)
  46. {
  47.     for (size_t i = 0; i < allWallets.size(); i++)
  48.     {
  49.         if (allWallets[i].id == walletId)
  50.             return i;
  51.     }
  52.     return -1;
  53. }
  54.  
  55. bool addNewWallet(const char owner[256], unsigned int walletId, double fiatMoney)
  56. {
  57.     int index = findWalletById(walletId);
  58.     if (index != -1)
  59.     {
  60.         return false;
  61.     }
  62.  
  63.     Wallet wallet(owner, walletId, fiatMoney);
  64.     allWallets.push_back(wallet);
  65.     return true;
  66. }
  67.  
  68. void writeWalletInformation(unsigned int walletId)
  69. {
  70.     std::ofstream wallets("wallets.dat", std::ios::binary | std::ios::app);
  71.     if (!wallets)
  72.     {
  73.         return;
  74.     }
  75.     else
  76.     {
  77.         //const int size = allWallets.size();
  78.         //wallets.write((char*)&size, sizeof(size));
  79.         const Wallet& wallet = allWallets[findWalletById(walletId)];
  80.         wallets.write((char*) & (wallet.owner), sizeof(wallet.owner));
  81.         wallets.write((char*) & (wallet.id), sizeof(wallet.id));
  82.         wallets.write((char*) & (wallet.fiatMoney), sizeof(wallet.fiatMoney));
  83.     }
  84.     wallets.close();
  85. }
  86.  
  87. void writeTransactionInformation(const Transaction& transaction)
  88. {
  89.     std::ofstream transactions("transactions.dat", std::ios::binary | std::ios::app);
  90.  
  91.     if (!transactions)
  92.     {
  93.         return;
  94.     }
  95.     else
  96.     {
  97.         transactions.write((char*) & (transaction.senderId), sizeof(transaction.senderId));
  98.         transactions.write((char*) & (transaction.resiverId), sizeof(transaction.resiverId));
  99.         transactions.write((char*) & (transaction.fmiCoints), sizeof(transaction.fmiCoints));
  100.         transactions.write((char*) & (transaction.time), sizeof(transaction.time));
  101.     }
  102. }
  103.  
  104. bool firstTransaction(long long time, unsigned int giverId, unsigned int resiverId, double fmiCoints)
  105. {
  106.     int giverIndex = findWalletById(giverId);
  107.  
  108.     if (giverIndex == -1)
  109.     {
  110.         return false;
  111.     }
  112.  
  113.     int receiverIndex = findWalletById(resiverId);
  114.     if (receiverIndex == -1)
  115.     {
  116.         return false;
  117.     }
  118.  
  119.     Transaction transaction(time, giverId, resiverId, fmiCoints);
  120.  
  121.     allTransactions.push_back(transaction);
  122.     writeTransactionInformation(transaction);
  123.     return true;
  124. }
  125.  
  126. bool transaction(long long time, unsigned int giverId, unsigned int resiverId, double tradeMoney)
  127. {
  128.     int giverIndex = findWalletById(giverId);
  129.     double coints;
  130.  
  131.     if (giverIndex == -1)
  132.     {
  133.         return false;
  134.     }
  135.  
  136.     int resiverIndex = findWalletById(resiverId);
  137.     if (resiverIndex == -1)
  138.     {
  139.         return false;
  140.     }
  141.  
  142.     Wallet& giverWallet = allWallets[giverIndex];
  143.     Wallet& resiverWallet = allWallets[resiverIndex];
  144.  
  145.     if (giverWallet.fiatMoney < tradeMoney)
  146.     {
  147.         return false;
  148.     }
  149.     else
  150.     {
  151.         giverWallet.fiatMoney -= tradeMoney;
  152.         resiverWallet.fiatMoney += tradeMoney;
  153.  
  154.         coints = tradeMoney / 375.0;
  155.  
  156.         Transaction transaction(time, giverId, resiverId, coints);
  157.         allTransactions.push_back(transaction);
  158.         writeTransactionInformation(transaction);
  159.         return true;
  160.     }
  161. }
  162.  
  163. void writeOrderInformation()
  164. {
  165.     std::ofstream orders("orders.dat", std::ios::binary | std::ios::trunc);
  166.  
  167.     if (!orders)
  168.     {
  169.         return;
  170.     }
  171.     else
  172.     {
  173.         for (size_t ic = 0; ic < allOrders.size(); ic++)
  174.         {
  175.             orders.write((char*) & (allOrders[ic].type), sizeof(allOrders[ic].type));
  176.             orders.write((char*) & (allOrders[ic].walletId), sizeof(allOrders[ic].walletId));
  177.             orders.write((char*) & (allOrders[ic].fmiCoints), sizeof(allOrders[ic].fmiCoints));
  178.         }
  179.     }
  180. }
  181.  
  182. int firstSellOrder()
  183. {
  184.     for (size_t ib = 0; ib < allOrders.size(); ib++)
  185.     {
  186.         if (allOrders[ib].type == Order::Type::SELL)
  187.         {
  188.             return ib;
  189.         }
  190.     }
  191.     return -1;
  192. }
  193.  
  194. int firstBuyOrder()
  195. {
  196.     for (size_t id = 0; id < allOrders.size(); id++)
  197.     {
  198.         if (allOrders[id].type == Order::Type::BUY)
  199.         {
  200.             return id;
  201.         }
  202.         return -1;
  203.     }
  204. }
  205.  
  206. bool buyOrder(unsigned int walletId, double fiatMoney, const Order& order)
  207. {
  208.     int buyerIndex = findWalletById(walletId);
  209.     double fmiCoinnts = fiatMoney / 375;
  210.     time_t now = time(0);
  211.  
  212.     if (buyerIndex == -1)
  213.     {
  214.         return false;
  215.     }
  216.  
  217.     else
  218.     {
  219.         //?
  220.         if (allWallets[buyerIndex].fiatMoney < fiatMoney)
  221.         {
  222.             return false;
  223.         }
  224.  
  225.         else
  226.         {
  227.             unsigned int senderId;
  228.             //unsigned int receiverId = allWallets[findWalletById(walletId)].;
  229.  
  230.             int firstSellOrderIndex = firstSellOrder();
  231.             if (firstSellOrderIndex == -1)
  232.             {
  233.                 allOrders.push_back(order);
  234.                 writeOrderInformation();
  235.                 return true;
  236.             }
  237.             else
  238.             {
  239.                 //Order popOrder = allOrders[firstSellOrderIndex];
  240.  
  241.                 unsigned int senderId = allOrders[firstSellOrderIndex].walletId;
  242.                 int sellerIdIndex = findWalletById(senderId);
  243.  
  244.  
  245.                 allWallets[sellerIdIndex].fiatMoney += fiatMoney;
  246.                 allWallets[buyerIndex].fiatMoney -= fiatMoney;
  247.  
  248.                 transaction(now, senderId, walletId, fmiCoinnts);
  249.                
  250.                 for (size_t i = firstSellOrderIndex; i < allOrders.size() - 1; i++)
  251.                 {
  252.                     allOrders[i] = allOrders[i + 1];
  253.                 }
  254.  
  255.                 //allOrders[allOrders.size()] = popOrder
  256.  
  257.                 allOrders.pop_back();
  258.                 writeOrderInformation();
  259.                 return true;
  260.             }
  261.         }
  262.     }
  263. }
  264.  
  265. void generateOrder()
  266. {
  267.     return 0;
  268. }
  269.  
  270. bool sellOrder(unsigned int walletId, double fiatMoney, const Order& order)
  271. {
  272.     int sellerIndex = findWalletById(walletId);
  273.     double fmiCoinnts = fiatMoney / 375;
  274.     time_t now = time(0);
  275.  
  276.     if (sellerIndex == -1)
  277.     {
  278.         return false;
  279.     }
  280.     else
  281.     {
  282.         if (allWallets[sellerIndex].fiatMoney < fiatMoney)
  283.         {
  284.             return false;
  285.         }
  286.         else
  287.         {
  288.             unsigned int recieverId;
  289.  
  290.             int firstBuyOrderIndex = firstBuyOrder();
  291.  
  292.             if (firstBuyOrderIndex == -1)
  293.             {
  294.                 allOrders.push_back(order);
  295.                 writeOrderInformation();
  296.                 return true;
  297.             }
  298.             else
  299.             {
  300.                 unsigned int buyerIndex = allOrders[firstBuyOrderIndex].walletId;
  301.                 int buyerIdIndex = findWalletById(buyerIndex);
  302.  
  303.  
  304.                 allWallets[sellerIndex].fiatMoney -= fiatMoney;
  305.                 allWallets[buyerIdIndex].fiatMoney += fiatMoney;
  306.  
  307.                 generateOrder();
  308.  
  309.                 Transaction trans();
  310.                 transaction(now, walletId, buyerIndex, fiatMoney);
  311.  
  312.                 for (size_t i = firstBuyOrderIndex; i < allOrders.size() - 1; i++)
  313.                 {
  314.                     allOrders[i] = allOrders[i + 1];
  315.                 }
  316.  
  317.                 //allOrders[allOrders.size()] = popOrder
  318.  
  319.                 allOrders.pop_back();
  320.                 writeOrderInformation();
  321.                 return true;
  322.             }
  323.         }
  324.     }
  325. }
  326.  
  327. int main()
  328. {
  329.     std::string command;
  330.  
  331.     time_t now = time(0);
  332.  
  333.     char systemWalletName[256] = "System Name";
  334.     unsigned int systemWalletId = 4294967295;
  335.     double systemWalletSystemFiatMoney = 112121212121212.0;
  336.  
  337.     Wallet systemWallet(systemWalletName, systemWalletId, systemWalletSystemFiatMoney);
  338.     allWallets.push_back(systemWallet);
  339.  
  340.     while (true)
  341.     {
  342.         std::cout << "Please enter your command: " << std::endl;
  343.         std::cin >> command;
  344.  
  345.         if (command == "add-wallet")
  346.         {
  347.             char ownerName[256];
  348.             unsigned int walletId;
  349.             double fiatMoney;
  350.             std::cout << "Enter the owner name, wallet id and fiat money" << std::endl;
  351.             std::cin >> ownerName >> walletId >> fiatMoney;
  352.             if (addNewWallet(ownerName, walletId, fiatMoney))
  353.             {
  354.                 double coints = fiatMoney / 375.0;
  355.                 writeWalletInformation(walletId);
  356.                 std::cout << "Successfully added wallet to wallet list!" << std::endl;
  357.                 firstTransaction(now, systemWallet.id, walletId, coints);
  358.             }
  359.             else
  360.             {
  361.                 std::cout << "Not Successfully added wallet!" << std::endl;
  362.             }
  363.         }
  364.  
  365.         else if (command == "transaction")
  366.         {
  367.  
  368.             char ownerName[256];
  369.             unsigned int giverWalletId, receicerWalletId;
  370.             double tradeMoney;
  371.             std::cout << "Enter the senderId, resiverId, tradeMoney" << std::endl;
  372.             std::cin >> giverWalletId >> receicerWalletId >> tradeMoney;
  373.  
  374.             //unsigned int giverId, receiverId;
  375.             //time_t now = time(0);
  376.  
  377.             if (transaction(now, giverWalletId, receicerWalletId, tradeMoney))
  378.             {
  379.                 std::cout << "Transaction successfully created" << std::endl;
  380.             }
  381.             else
  382.                 std::cout << "Transaction not successfully created" << std::endl;
  383.         }
  384.  
  385.         else if (command == "transfer")
  386.         {
  387.             std::cout << "Please ender sender sender id and receiver id and transfer coints: " << std::endl;
  388.             unsigned int senderId, receiverId;
  389.             double transferCoints;
  390.             std::cin >> senderId >> receiverId >> transferCoints;
  391.  
  392.             double transferFiatMoney = transferCoints * 375.0;
  393.  
  394.             if (transaction(now, senderId, receiverId, transferFiatMoney))
  395.             {
  396.                 std::cout << "Successfully transfer fmi coints" << std::endl;
  397.             }
  398.             else
  399.             {
  400.                 std::cout << "Failed to transfer fmi coints" << std::endl;
  401.             }
  402.         }
  403.  
  404.         else if (command == "make-order")
  405.         {
  406.             std::cout << "Please enter the order(SELL or BUY), wallet id and the coits for that operation" << std::endl;
  407.  
  408.             char order[16];
  409.             unsigned int walletId;
  410.             double fmiCoints;
  411.             std::cin >> order >> walletId >> fmiCoints;
  412.  
  413.             double fiatMoney = fmiCoints * 375;
  414.  
  415.             if (order == "BUY")
  416.             {
  417.                 Order newOrder(Order::BUY, walletId, fmiCoints);
  418.                 if (buyOrder(walletId, fiatMoney, newOrder))
  419.                 {
  420.                     std::cout << "Successfully compleated order and created transaction!" << std::endl;
  421.                 }
  422.                 else
  423.                 {
  424.                     std::cout << "Failed by creating new buy order!" << std::endl;
  425.                 }
  426.             }
  427.             else if (order == "SELL")
  428.             {
  429.                 Order newOrder(Order::SELL, walletId, fmiCoints);
  430.                 if (sellOrder(walletId, fiatMoney, newOrder))
  431.                 {
  432.                     std::cout << "Successfully compleated order!" << std::endl;
  433.                 }
  434.                 else
  435.                 {
  436.                     std::cout << "Failed by creating new sell order!" << std::endl;
  437.                 }
  438.             }
  439.             else
  440.             {
  441.                 std::cout << "Unvalid order" << std::endl;
  442.             }
  443.         }
  444.  
  445.         else if (command == "quit")
  446.         {
  447.             return 0;
  448.         }
  449.     }
  450. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top