-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccount.cpp
More file actions
48 lines (38 loc) · 1.26 KB
/
Account.cpp
File metadata and controls
48 lines (38 loc) · 1.26 KB
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
#include "Account.h"
#include <sstream>
#include <iostream>
Account::Account() : accountNumber(0), name(""), balance(0.0) {}
Account::Account(int accNo, const std::string& userName, double initialBalance)
: accountNumber(accNo), name(userName), balance(initialBalance) {}
void Account::deposit(double amount) {
balance += amount;
}
bool Account::withdraw(double amount) {
if (amount > balance) return false;
balance -= amount;
return true;
}
void Account::display() const {
std::cout << "Account No: " << accountNumber
<< "\nName: " << name
<< "\nBalance: " << balance << "\n";
}
int Account::getAccountNumber() const { return accountNumber; }
std::string Account::getName() const { return name; }
double Account::getBalance() const { return balance; }
std::string Account::serialize() const {
return std::to_string(accountNumber) + "|" + name + "|" + std::to_string(balance);
}
Account Account::deserialize(const std::string& data) {
std::stringstream ss(data);
std::string token;
int accNo;
std::string name;
double bal;
getline(ss, token, '|');
accNo = std::stoi(token);
getline(ss, name, '|');
getline(ss, token);
bal = std::stod(token);
return Account(accNo, name, bal);
}