-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWalletSystem.h
More file actions
167 lines (138 loc) · 5.97 KB
/
WalletSystem.h
File metadata and controls
167 lines (138 loc) · 5.97 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include <string>
#include <map>
#include <unordered_map>
#include <vector>
#include <set>
#include <unordered_set>
#include <queue>
#include <list>
#include <stack>
#include <stdexcept>
#include "User.h"
#include "Transaction.h"
#include <iomanip>
using namespace std;
// Exception classes
class WalletException : public std::exception {
public:
WalletException(const std::string& message) : msg(message) {}
const char* what() const noexcept override { return msg.c_str(); }
private:
std::string msg;
};
class UserNotFoundException : public WalletException {
public:
UserNotFoundException(const std::string& username)
: WalletException("User not found: " + username) {
}
};
class InsufficientFundsException : public WalletException {
public:
InsufficientFundsException()
: WalletException("Insufficient funds for transaction") {
}
};
// Pending request structure
struct PendingRequest {
std::string requesterId;
std::string senderId;
double amount;
std::time_t timestamp;
std::string description;
bool operator<(const PendingRequest& other) const {
return timestamp > other.timestamp; // Priority queue will sort by most recent first
}
};
class WalletSystem {
public:
WalletSystem();
// User management
bool registerUser(const std::string& username, const std::string& password, bool isAdmin = false);
bool login(const std::string& username, const std::string& password);
void logout();
// Transaction operations
bool sendMoney(const std::string& recipient, double amount, const std::string& description = "");
bool requestMoney(const std::string& sender, double amount, const std::string& description = "");
std::vector<Transaction> getTransactionHistory() const;
std::vector<Transaction> getUserTransactions(const std::string& username) const;
// Money request operations
std::vector<PendingRequest> getPendingRequests() const;
bool approvePendingRequest(const std::string& requesterId);
bool rejectPendingRequest(const std::string& requesterId);
// Account operations
bool changePassword(const std::string& oldPassword, const std::string& newPassword);
double getBalance() const;
double getSystemBankBalance() const;
// Admin operations
bool isAdmin() const;
std::vector<std::string> getAllUsers() const;
bool suspendUser(const std::string& username);
bool activateUser(const std::string& username);
bool adjustBalance(const std::string& username, double newBalance);
std::vector<std::pair<std::string, double>> getAllUsersWithBalances() const;
// File operations
bool saveToFile(const std::string& filename) const;
bool loadFromFile(const std::string& filename);
void setAutoSaveFile(const std::string& filename) { autoSaveFile = filename; }
// New methods using additional data structures
/* bool addFriend(const std::string& friendUsername);
bool removeFriend(const std::string& friendUsername);
std::list<std::string> getFriendsList() const;
bool blockUser(const std::string& username);
bool unblockUser(const std::string& username);
bool isBlocked(const std::string& username) const;*/
std::vector<std::string> getNotifications();
bool deleteUser(const std::string& username);
std::vector<std::pair<std::string, int>> getTopRecipients(int topN = 3) const;
/* bool isUserLocked(const std::string& username) const;
bool unlockUser(const std::string& username);*/
// --- Methods required by new GUI ---
bool isCurrentUserAdmin() const;
std::string getCurrentUsername() const;
double getCurrentUserBalance() const;
std::vector<Transaction> getCurrentUserTransactions() const;
//std::vector<std::string> getCurrentUserFriends() const;
// std::vector<std::string> getCurrentUserBlocked() const;
double getSystemBalance() const;
int getTotalUsers() const;
int getTotalTransactions() const;
std::vector<std::string> getSystemStats() const;
bool validatePassword(const std::string& oldPassword) {
// Implement validation logic
}
void setPassword(const std::string& newPassword) {
// Implement logic to set the new password
}
// Admin edit features
bool adminAddUser(const std::string& username, const std::string& password, bool makeAdmin = false);
bool adminEditUsername(const std::string& oldUsername, const std::string& newUsername);
bool adminEditPassword(const std::string& username, const std::string& newPassword);
// Admin: get all transactions
std::vector<Transaction> getAllTransactions() const { return transactions; }
private:
static constexpr double INITIAL_BALANCE = 1000.0; // New users get $1000 initial balance
const std::string SYSTEM_BANK = "SYSTEM_BANK"; // System bank account name
// Core data structures
std::map<std::string, User> users;
std::vector<Transaction> transactions;
std::set<std::string> activeUsers;
std::priority_queue<PendingRequest> pendingRequests;
// New data structures for enhanced functionality
// std::unordered_map<std::string, std::list<std::string>> friends; // User's friends list
// std::unordered_set<std::string> blockedUsers; // Global blocked users
std::stack<Transaction> undoStack; // For transaction rollback
std::queue<std::string> notificationQueue; // User notifications
std::map<int, int> hourlyTransactionCount; // Hour -> transaction count
User* currentUser;
std::string autoSaveFile; // Filename for autosave
void createSystemBankAccount();
bool isUserActive(const std::string& username) const;
void recordTransaction(const std::string& sender,
const std::string& receiver,
double amount,
const std::string& description = "",
TransactionType type = TransactionType::TRANSFER);
void autoSave() const; // New method for autosaving
};