System zleceń - kopia
This commit is contained in:
72
user.cpp
Normal file
72
user.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
// SPDX-FileCopyrightText: 2024 Krzysztof <email>
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#include "user.h"
|
||||
User::User(std::string login, std::string password, std::string name, std::string surname, int access_level)
|
||||
: login(login), password(password), name(name), surname(surname), access_level(access_level) {}
|
||||
|
||||
/* --- To samo co w jednej linii tylko bardziej czytelnie ---
|
||||
{
|
||||
this->login = login;
|
||||
this->name = name;
|
||||
this ->surname = surname;
|
||||
this ->access_level = access_level;
|
||||
this ->password = password;
|
||||
}
|
||||
|
||||
*/
|
||||
void User::wyswietl() const {
|
||||
std::cout << "login : " << login << ", Imie: " << name << ", Nazwisko: " << surname << std::endl;
|
||||
}
|
||||
|
||||
std::string User::GetLogin() {
|
||||
return login;
|
||||
}
|
||||
|
||||
std::string User::GetName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
std::string User::GetSurname() {
|
||||
return surname;
|
||||
}
|
||||
|
||||
int User::GetAccessLevel() {
|
||||
return access_level;
|
||||
}
|
||||
void User::saveToFile(std::ofstream& outFile) {
|
||||
outFile << login << '\n';
|
||||
outFile << password << '\n';
|
||||
outFile << name << '\n';
|
||||
outFile << surname << '\n';
|
||||
outFile << access_level << '\n';
|
||||
}
|
||||
User User::loadFromFile(std::ifstream& sourcefile) {
|
||||
std::string login, password, name, surname;
|
||||
int access_level;
|
||||
|
||||
// Sprawdz odczyt linii, aby upewnic sie, ze nie docieramy do konca pliku
|
||||
if (!std::getline(sourcefile, login)) {
|
||||
throw std::runtime_error("Nieudany odczyt loginu");
|
||||
}
|
||||
if (!std::getline(sourcefile, password)) {
|
||||
throw std::runtime_error("Nieudany odczyt hasla");
|
||||
}
|
||||
if (!std::getline(sourcefile, name)) {
|
||||
throw std::runtime_error("Nieudany odczyt imienia");
|
||||
}
|
||||
if (!std::getline(sourcefile, surname)) {
|
||||
throw std::runtime_error("Nieudany odczyt nazwiska");
|
||||
}
|
||||
if (!(sourcefile >> access_level)) {
|
||||
throw std::runtime_error("Nieudany odczyt poziomu dostepu");
|
||||
}
|
||||
sourcefile.ignore(); // Pomija znak nowej linii po odczycie liczby
|
||||
|
||||
return User(login, password, name, surname, access_level);
|
||||
}
|
||||
bool User::authenticate(const std::string& login, const std::string& password)const{
|
||||
return this->login == login && this->password == password;
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user