Determine the different ways the system will be used. This includes main functions the system needs to perform and who will use it.
Based on the requirements and use cases, identify the main objects of the system...
Determine how these objects will interact with each other to fulfill the use cases...
Design inheritance trees where applicable to promote code reuse and polymorphism. This step involves identifying common attributes and behaviors that can be abstracted into parent classes...
Consider using design patterns (e.g., Factory, Singleton, Observer, Strategy) that fit the problem...
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <algorithm>
#include <thread>
#include <mutex>
#include <ctime>
using namespace std;
enum Status {
Success = 1,
Failure = 0
};
// Transaction Class Definition
class Transaction {
private:
string transactionId;
double amount;
string transactionType; // "deposit" or "withdraw"
time_t timestamp;
public:
Transaction(const string& id, double amt, const string& type)
: transactionId(id), amount(amt), transactionType(type) {
timestamp = time(0);
}
void displayTransaction() const {
cout << "Transaction ID: " << transactionId
<< ", Amount: " << amount
<< ", Type: " << transactionType
<< ", Timestamp: " << ctime(×tamp);
}
};
// User Class Definition
class User {
private:
string name;
string uID;
string password;
string phoneNumber;
string email;
string address;
string bankAccountNo;
double balance;
vector<Transaction> transactionHistory; // Track transactions for each user
public:
User(string name, string phoneNumber, string email, string address, double balance)
: name(name), uID(""), password(""), phoneNumber(phoneNumber), email(email), address(address), balance(balance) {
bankAccountNo = "";
}
void giveBankAccountNo(const string& accountNo) {
bankAccountNo = accountNo;
}
bool addUID(const string& uID, const string& password) {
this->uID = uID;
this->password = password;
return true;
}
string getUID() const { return uID; }
string getPassword() const { return password; }
double getBalance() const { return balance; }
// Deposit method with transaction logging
void deposit(double amount) {
balance += amount;
logTransaction(amount, "deposit");
}
// Withdraw method with transaction logging
void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
logTransaction(amount, "withdraw");
} else {
cout << "Insufficient funds." << endl;
}
}
// Log a transaction
void logTransaction(double amount, const string& type) {
static int transactionCounter = 0; // Unique transaction ID generator
string transactionId = "TXN" + to_string(transactionCounter++);
Transaction newTransaction(transactionId, amount, type);
transactionHistory.push_back(newTransaction);
}
// Display the transaction history
void displayTransactionHistory() const {
if (transactionHistory.empty()) {
cout << "No transactions available." << endl;
return;
}
for (const auto& transaction : transactionHistory) {
transaction.displayTransaction();
}
}
};
// AuthenticationManager Class Definition
class AuthenticationManager {
private:
unordered_map<string, string> uIDPassword; // Maps UID to Password
vector<string> blockedAccounts;
static const int maxFailures = 5;
int failCount = 0; // Manage failure count internally
mutex mtx;
public:
bool authenticate(const string& user, const string& password) {
lock_guard<mutex> lock(mtx);
auto it = uIDPassword.find(user);
if (it != uIDPassword.end()) { // User exists
if (it->second == password) {
failCount = 0; // Reset on successful authentication
return true;
} else {
++failCount;
reportFraudulentActivity(user);
return false;
}
}
++failCount;
reportFraudulentActivity(user);
return false;
}
void reportFraudulentActivity(const string& uID) {
if (failCount >= maxFailures) {
cout << "Account blocked for user: " << uID << ". Contact support." << endl;
blockedAccounts.push_back(uID);
}
}
void addUser(const string& uID, const string& password) {
lock_guard<mutex> lock(mtx);
uIDPassword[uID] = password;
}
};
// Main banking system managing users and accounts
class BankingSystem {
private:
vector<User> bankAccounts;
AuthenticationManager authManager;
User* currentUser; // Pointer to hold the currently logged-in user
public:
BankingSystem() : currentUser(nullptr) {}
void addBankAccount(const string& name, const string& phoneNumber, const string& email, const string& address, double balance) {
User newUser(name, phoneNumber, email, address, balance);
string accountNo = "2024" + to_string(bankAccounts.size() + 1); // Generate account number
newUser.giveBankAccountNo(accountNo);
bankAccounts.push_back(newUser);
}
void getUID(const string& name, const string& uID, const string& password) {
if (bankAccounts.empty()) {
cout << "No accounts to associate UID with." << endl;
return;
}
if (bankAccounts.back().addUID(uID, password)) {
authManager.addUser(uID, password);
cout << "User ID added successfully." << endl;
}
}
// User Login Session
void login(const string& userID, const string& password) {
if (authManager.authenticate(userID, password)) {
for (auto& account : bankAccounts) {
if (account.getUID() == userID) {
currentUser = &account; // Set the current user
cout << "User " << userID << " logged in successfully." << endl;
return;
}
}
}
cout << "Authentication failed or user not found." << endl;
}
// Logout Session
void logout() {
if (currentUser) {
cout << "User " << currentUser->getUID() << " logged out." << endl;
currentUser = nullptr;
} else {
cout << "No user is currently logged in." << endl;
}
}
// Deposit Money without Re-Authentication
void deposit(double amount) {
if (currentUser) {
currentUser->deposit(amount);
cout << "Amount deposited successfully. New Balance: " << currentUser->getBalance() << endl;
} else {
cout << "No user is currently logged in." << endl;
}
}
// Withdraw Money without Re-Authentication
void withdraw(double amount) {
if (currentUser) {
currentUser->withdraw(amount);
cout << "Amount withdrawn successfully. New Balance: " << currentUser->getBalance() << endl;
} else {
cout << "No user is currently logged in." << endl;
}
}
// Show the logged-in user's transaction history
void showTransactionHistory() {
if (currentUser) {
currentUser->displayTransactionHistory();
} else {
cout << "No user is currently logged in." << endl;
}
}
};
int main() {
BankingSystem bankingSystem;
// Create users and add bank accounts
bankingSystem.addBankAccount("Alice", "1234567890", "[email protected]", "123 Main St", 1000.0);
bankingSystem.addBankAccount("Bob", "0987654321", "[email protected]", "456 Elm St", 1500.0);
// Add UIDs and passwords
bankingSystem.getUID("Alice", "alice_uid", "password123");
bankingSystem.getUID("Bob", "bob_uid", "password456");
// Start session for Alice
bankingSystem.login("alice_uid", "password123");
bankingSystem.deposit(200.0);
bankingSystem.withdraw(100.0);
bankingSystem.showTransactionHistory();
bankingSystem.logout();
// Start session for Bob
bankingSystem.login("bob_uid", "password456");
bankingSystem.deposit(300.0);
bankingSystem.withdraw(200.0);
bankingSystem.showTransactionHistory();
bankingSystem.logout();
return 0;
}
Check and explain whether your design adheres to solid principles (Ask interviewer what SOLID principle is if you can not recall it.)...
Explain how your design can handle changes in scale and whether it would be easily to extend with new functionalities...
Try creating a class, flow, state and/or sequence diagram using the diagramming tool. Mermaid flow diagrams can be used to represent system use cases. You can ask the interviewer bot to create a starter diagram if unfamiliar with the tool. Briefly explain your diagrams if necessary...
Critically examine your design for any flaws or areas for future improvement...