HRusev

Profits.cpp

May 25th, 2023
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | Source Code | 0 0
  1. // 04. Profits.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include <iostream>
  5. #include <string>
  6. #include <sstream>
  7. #include <vector>
  8. #include <map>
  9.  
  10.  
  11. class Company {
  12.     std::string name;
  13.     int id;
  14.     int revenue;
  15.     int costs;
  16. public:
  17.     std::string getName() const {
  18.         return this->name;
  19.     }
  20.  
  21.     int getId() const {
  22.         return this->id;
  23.     }
  24.  
  25.     int getRevenue() const {
  26.         return this->revenue;
  27.     }
  28.  
  29.     int getCosts() const {
  30.         return this->costs;
  31.     }
  32.  
  33.     friend std::istream& operator>>(std::istream& stream, Company& company);
  34. };
  35.  
  36. std::istream& operator>>(std::istream& stream, Company& company) {
  37.     char separator;
  38.     return stream >> company.name >> company.id >> separator >> company.revenue >> company.costs;
  39. }
  40.  
  41. std::ostream& operator<<(std::ostream& stream, const Company& company) {
  42.     return stream << company.getName() << " " << company.getId() << " : " << company.getRevenue() << " " << company.getCosts();
  43. }
  44.  
  45.  
  46.  
  47. class ProfitCalculator {
  48. private:
  49.     int taxPercentage;
  50. public:
  51.     int calculateProfit(const Company& company) const {
  52.         int balance = company.getRevenue() - company.getCosts();
  53.         int tax = (balance * taxPercentage) / 100;
  54.         return balance - tax;
  55.     }
  56.  
  57.     friend std::istream& operator>>(std::istream& stream, ProfitCalculator& p);
  58. };
  59.  
  60. std::istream& operator>>(std::istream& stream, ProfitCalculator& p) {
  61.     return stream >> p.taxPercentage;
  62. }
  63.  
  64. std::string getProfitReport(Company* fromInclusive, Company* toInclusive, std::map<int, ProfitCalculator> profit)
  65. {
  66.  
  67.     using namespace std;
  68.  
  69.     Company* index;
  70.     Company  c;
  71.     ostringstream out;
  72.     stringstream buffer;
  73.  
  74.     index = fromInclusive;
  75.  
  76.     while (index <= toInclusive)
  77.     {
  78.         out << index->getName();
  79.         out << " = ";
  80.        
  81.         //Create copy
  82.         buffer << *index;
  83.         buffer >> c;
  84.  
  85.         out << profit[index->getId()].calculateProfit(c) << endl;
  86.  
  87.         index++;
  88.         //Clear buffer
  89.         buffer.str("");
  90.         buffer.clear();
  91.  
  92.     }
  93.  
  94.     return out.str();
  95. }
  96.  
  97.  
  98. int main() {
  99.     using namespace std;
  100.  
  101.     vector<Company> companies;
  102.  
  103.     string line;
  104.     //Initializes the vector<Company>
  105.     while (getline(cin, line) && line != "end") {
  106.         istringstream lineIn(line);
  107.  
  108.         Company c;
  109.         lineIn >> c;
  110.         companies.push_back(c);
  111.     }
  112.  
  113.     map<int, ProfitCalculator> profitCalculatorsByCompany;
  114.     line = "";
  115.     while (getline(cin, line) && line != "end") {
  116.         istringstream lineIn(line);
  117.  
  118.         int companyId;
  119.         lineIn >> companyId;
  120.         ProfitCalculator calculator;
  121.         lineIn >> calculator;
  122.  
  123.         profitCalculatorsByCompany[companyId] = calculator;
  124.     }
  125.  
  126.     Company* fromInclusive = &companies[0];
  127.     Company* toInclusive = &companies[companies.size() - 1];
  128.  
  129.     cout << getProfitReport(fromInclusive, toInclusive, profitCalculatorsByCompany) << endl;
  130.  
  131.     return 0;
  132. }
  133.  
  134.  
  135.  
  136. /*
  137.  
  138. INPUT
  139. acme 424242420 : 43000 1000
  140. softuni_foundation 20140414 : 0 0
  141. itjoro 878968302 : 100 25
  142. end
  143. 878968302 0
  144. 424242420 10
  145. 20140414 30
  146. end
  147.  
  148.  
  149. */
Advertisement
Add Comment
Please, Sign In to add comment