Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming / 07-Practice-05.04.2022 / Player.cpp
blob: cb765537a858ac6d42cb22dc9bff3c97b4033248 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "Player.h"
#include <cstring>

/* Private */

void Player::copyFrom(const Player& other) {
	this->name = new char[strlen(other.name) + 1];
	strcpy(this->name, other.name);
	this->pass = new char[strlen(other.pass) + 1];
	strcpy(this->pass, other.pass);
	this->age = other.age;
	this->balance = other.balance;
}

void Player::free() const {
	delete[] this->name;
	delete[] this->pass;
}

bool Player::isValidPassword(const char* passwordAttempt) {
	return strcmp(this->pass, passwordAttempt) == 0;
}

/* Public */

Player::Player(const char* name, const char* pass, unsigned age, double balance) {
	this->name = new char[strlen(name) + 1];
	strcpy(this->name, name);
	this->pass = new char[strlen(pass) + 1];
	strcpy(this->pass, pass);
	this->age = age;
	this->balance = balance;
}

Player::Player(const Player& other) {
	this->copyFrom(other);
}

Player& Player::operator=(const Player& other) {
	if (this != &other) {
		this->free();
		this->copyFrom(other);
	}
	return *this;
}

Player::~Player() {
	this->free();
}

const char* Player::get_name() const {
	return this->name;
}
bool Player::set_name(const char* password, const char* name) {
	if (!this->isValidPassword(password))
		return false;
	
	delete[] this->name;
	this->name = new char[strlen(name) + 1];
	strcpy(this->name, name);
	return true;
}

bool Player::set_password(const char* password, const char* newPassword) {
	if (!this->isValidPassword(password))
		return false;

	delete[] this->pass;
	this->pass = new char[strlen(newPassword) + 1];
	strcpy(this->pass, newPassword);
	return true;
}

unsigned Player::get_age() const  {
	return this->age;
}
bool Player::set_age(const char* password, unsigned value) {
	if (!this->isValidPassword(password))
		return false;

	this->age = value;
	return true;
}

double Player::get_balance() const {
	return this->balance;
}

bool Player::addToBalance(const char* password, double value) {
	if (!this->isValidPassword(password))
		return false;

	this->balance += value;
	return true;
}

bool Player::deductFromBalance(const char* password, double value) {
	if (!this->isValidPassword(password))
		return false;

	this->balance -= value;
	return true;
}