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 |
#include <iostream> #include <unordered_map> #include <string> class User { public: User(const std::string& name) : name(name), balance(0.0) {} void deposit(double amount) { if (amount > 0) { balance += amount; std::cout << amount << " deposited. New balance: " << balance << std::endl; } else { std::cout << "Invalid deposit amount!" << std::endl; } } void withdraw(double amount) { if (amount > 0 && amount <= balance) { balance -= amount; std::cout << amount << " withdrawn. New balance: " << balance << std::endl; } else { std::cout << "Invalid withdrawal amount or insufficient funds!" << std::endl; } } double getBalance() const { return balance; } private: std::string name; double balance; }; class CryptocurrencySystem { public: void addUser(const std::string& name) { if (users.find(name) == users.end()) { users[name] = User(name); std::cout << "User " << name << " added." << std::endl; } else { std::cout << "User already exists!" << std::endl; } } void deposit(const std::string& name, double amount) { if (users.find(name) != users.end()) { users[name].deposit(amount); } else { std::cout << "User not found!" << std::endl; } } void withdraw(const std::string& name, double amount) { if (users.find(name) != users.end()) { users[name].withdraw(amount); } else { std::cout << "User not found!" << std::endl; } } void checkBalance(const std::string& name) const { if (users.find(name) != users.end()) { std::cout << "Balance for " << name << ": " << users.at(name).getBalance() << std::endl; } else { std::cout << "User not found!" << std::endl; } } private: std::unordered_map<std::string, User> users; }; int main() { CryptocurrencySystem system; // Adding users system.addUser("Alice"); system.addUser("Bob"); // Performing transactions system.deposit("Alice", 100.0); system.deposit("Bob", 50.0); system.withdraw("Alice", 30.0); system.withdraw("Bob", 10.0); // Checking balances system.checkBalance("Alice"); system.checkBalance("Bob"); return 0; } |
Explanation:
- User Class:
- Represents a user with a name and balance.
deposit(double amount)
: Adds the specified amount to the user’s balance if it is positive.withdraw(double amount)
: Subtracts the specified amount from the user’s balance if it is positive and less than or equal to the current balance.getBalance()
: Returns the current balance of the user.
AdvertisementAdvertisement - CryptocurrencySystem Class:
- Manages multiple users using an unordered map (
std::unordered_map
). addUser(const std::string& name)
: Adds a new user to the system if they do not already exist.deposit(const std::string& name, double amount)
: Calls thedeposit
method of theUser
class for the specified user.withdraw(const std::string& name, double amount)
: Calls thewithdraw
method of theUser
class for the specified user.checkBalance(const std::string& name)
Advertisement
- Manages multiple users using an unordered map (
- main Function:
- Demonstrates adding users, performing deposits and withdrawals, and checking balances.
Compilation:
To compile the program, use:
1g++ crypto_transactions.cpp -o crypto_transactions
Run the program with:
1./crypto_transactions
- Demonstrates adding users, performing deposits and withdrawals, and checking balances.