Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // System przydziela automatycznie kod nowego produktu
- //procedura sprawdzania czy wiersz jest pusty jesli tak to przeskocz do kolejnego.
- // Czy klasa powinna miec metode do tworzenia obiektow tej klasy.
- #include "pch.h"
- #include <iostream>
- #include <fstream>
- #include <sstream> // std::strningstream
- #include <vector>
- #include <string>
- #include "../../../Bjarne Stroustrup/std_lib_facilities.h"
- using namespace std;
- namespace quantity {
- static const string kilogram = "kg";
- static const string piece = "p";
- }
- class Product {
- public:
- enum class type { fruit, vegetable };
- type kind;
- string name;
- string quantity;
- Product(type k, string n, string q, double c, size_t pc)
- :kind(k), name(n), cost(c), quantity(q), product_code(pc) {}
- void set_cost(double c) { cost = c; }
- double get_cost() { return cost; }
- int get_product_code() { return product_code; }
- private:
- double cost;
- size_t const product_code;
- };
- void main_while();
- string read_from_file();
- Product parsing_text(string& line);
- Product::type check_product_type(string& t);
- string check_product_quantity(string& k);
- int main()
- try {
- vector<Product> v_product;
- vector<string> file_text;
- file_text = read_from_file();
- Product p = parsing_text(text);
- for (size_t i = 0; i < v_product.size(); i++)
- {
- v_product.push_back(p)
- }
- }
- catch (exception& e) {
- cerr << "exception: " << e.what() << endl;
- }
- catch (...) {
- cerr << "exception\n";
- }
- string read_from_file()
- {
- ifstream file("data.txt");
- string file_text;
- if (!file)
- cerr << "Error: Failed to open the input file\n";
- while (getline(file, file_text))
- return file_text;
- }
- Product parsing_text(string& line)
- {//Parsing text from file and building object type Product
- //nazwy zmiennych
- stringstream sstream(line);
- string type, name, quantity;
- double cost = 0;
- size_t code = 0;
- sstream >> type >> name >> quantity >> cost >> code;
- Product::type kind = check_product_type(type);
- quantity = check_product_quantity(quantity);
- return Product(kind, name, quantity, cost, code);
- }
- Product::type check_product_type(string& t)
- {
- if (t == "f")
- return Product::type::fruit;
- else
- return Product::type::vegetable;
- }
- string check_product_quantity(string& k)
- {
- if (k == "kg")
- return quantity::kilogram;
- else
- return quantity::piece;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement