Skip to content
NataliaSlobodianiuk edited this page Apr 17, 2015 · 9 revisions

How to use the DataManager class

The DataManager class allows you to write objects (Users and Products) into a file or read them from a file. To use this class, include the "DataManager.h" header into your file.

The DataManager has only one constructor which is the default one.

Writing

To write an object into a file use the save methods:
void saveUser(const User&);
void saveProduct(const Product&);
These methods will write the object into the end of file.

Reading

Reading is more tricky. You can read all objects from a file with readAll methods:
List<User*> readAllUsers();
List<Product*> readAllProducts();

Or you can read all objects that match a given predicate with read methods:
List<User*> readUsers(function<bool(const User&)> predicate);
List<Product*> readProducts(function<bool(const Product&)> predicate);
The predicate is a functional object (a function, a functor or a lambda expression) that accepts a User or a Product and returns a bool value.

You can also get a single object with these functions:
getUserByLogin(string login, string password);
getUserById(int id);
getProductById(int id);
These functions return a pointer to a single object or nullptr is there is no such project.

Note: The reading functions return a list of pointers or a single pointer. They point to a dynamically allocated memory, so they must be deleted manually!!

##Quantity of products The getQuantity, chageQuantity and setQuantity methods get, change or set the quantity of a product with a given id:
int getQuantity(int id);
void changeQuantity(int id, int newQuantity); void setQuantity(int id, int newQuantity);
The getQuantity method returns -1 if there is no product with a given id.
The changeQuantity method sets a new quantity relative to the old quantity. The 'setQuantity" method sets a new quantity (no relation to the old quantity).

Exceptions

All methods can throw an exception of type exception if a file cannot be opened.
The reading methods can throw an exception of type exception if the type of a saved product was specified incorrectly by the save method (which is virtually impossible).

Miscellaneous

The DataManger itself does nothing. It just calls a corresponding method of a service class (UserService or ProductService). That's why there's no need to use the service classes directly.

Users are saved into Users.txt and products are saved into Products.txt.
Another file - Assortment.txt - includes the quantity of every saved product.

These methods will be added in the nearest future:
remove - remove all objects that match a predicate
Other methods might also be added.

Clone this wiki locally