Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. // System przydziela automatycznie kod nowego produktu
  2. //procedura sprawdzania czy wiersz jest pusty jesli tak to przeskocz do kolejnego.
  3. // Czy klasa powinna miec metode do tworzenia obiektow tej klasy.
  4. #include "pch.h"
  5. #include <iostream>
  6. #include <fstream>
  7. #include <sstream>  // std::strningstream
  8. #include <vector>
  9. #include <string>
  10.  
  11. #include "../../../Bjarne Stroustrup/std_lib_facilities.h"
  12.  
  13. using namespace std;
  14.  
  15. namespace quantity {
  16.     static const string kilogram = "kg";
  17.     static const string piece = "p";
  18. }
  19.  
  20. class Product {
  21. public:
  22.     enum class type { fruit, vegetable };
  23.     type kind;
  24.     string name;
  25.     string quantity;
  26.  
  27.     Product(type k, string n, string q, double c, size_t pc)
  28.         :kind(k), name(n), cost(c), quantity(q), product_code(pc) {}
  29.  
  30.     void set_cost(double c) { cost = c; }
  31.     double get_cost() { return cost; }
  32.     int get_product_code() { return product_code; }
  33.  
  34. private:
  35.     double cost;
  36.     size_t const product_code;
  37. };
  38.  
  39. void main_while();
  40. string read_from_file();
  41. Product parsing_text(string& line);
  42. Product::type check_product_type(string& t);
  43. string check_product_quantity(string& k);
  44.  
  45.  
  46. int main()
  47. try {
  48.     vector<Product> v_product;
  49.     vector<string> file_text;
  50.     file_text = read_from_file();
  51.     Product p = parsing_text(text);
  52.     for (size_t i = 0; i < v_product.size(); i++)
  53.     {
  54.         v_product.push_back(p)
  55.     }
  56. }
  57. catch (exception& e) {
  58.     cerr << "exception: " << e.what() << endl;
  59. }
  60. catch (...) {
  61.     cerr << "exception\n";
  62. }
  63.  
  64.  
  65. string read_from_file()
  66. {
  67.     ifstream file("data.txt");
  68.     string file_text;
  69.     if (!file)
  70.         cerr << "Error: Failed to open the input file\n";
  71.     while (getline(file, file_text))
  72.         return file_text;
  73. }
  74.  
  75. Product parsing_text(string& line)
  76. {//Parsing text from file and building object type Product
  77.     //nazwy zmiennych
  78.     stringstream sstream(line);
  79.     string type, name, quantity;
  80.     double cost = 0;
  81.     size_t code = 0;
  82.     sstream >> type >> name >> quantity >> cost >> code;
  83.     Product::type kind = check_product_type(type);
  84.     quantity = check_product_quantity(quantity);
  85.     return Product(kind, name, quantity, cost, code);
  86. }
  87.  
  88. Product::type check_product_type(string& t)
  89. {
  90.     if (t == "f")
  91.         return Product::type::fruit;
  92.     else
  93.         return Product::type::vegetable;
  94. }
  95.  
  96. string check_product_quantity(string& k)
  97. {
  98.     if (k == "kg")
  99.         return quantity::kilogram;
  100.     else
  101.         return quantity::piece;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement