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...
Attributes: For each class, define the attributes (data) it will hold...
Methods: Define the methods (functions) that operate on the attributes. Ensure they align with the object's responsibilities and adhere to the principle of encapsulation.
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <string>
using namespace std;
enum AccountType {
Saving = 0,
Checking = 1,
};
unordered_map<string, string> authenticateData;
unordered_map<string, int> failedAttempts; // track failed authentication attempts
unordered_map<string, bool> blockedUsers;
class Security {
public:
// Simple Caesar cipher for encryption
string encrypt(string data) {
string encrypted = data;
for (char& c : encrypted) {
c += 3; // Shift each character by 3 positions (example)
}
return encrypted;
}
// Decrypt Caesar cipher
string decrypt(string data) {
string decrypted = data;
for (char& c : decrypted) {
c -= 3; // Reverse the shift by 3
}
return decrypted;
}
};
class User {
public:
string name;
string userID;
string accountNo;
double amount;
AccountType acnt;
Security security; // Security object for encryption
// Check if user is blocked
bool isBlocked() {
return blockedUsers[name];
}
// Authenticate the user
bool authenticate(string authData) {
if (isBlocked()) {
cout << "User " << name << " is blocked from logging in.\n";
return false;
}
string encryptedData = security.encrypt(authData);
if (authenticateData.find(name) != authenticateData.end() && authenticateData[name] == encryptedData) {
failedAttempts[name] = 0; // reset failed attempts after successful login
return true;
} else {
failedAttempts[name]++;
if (failedAttempts[name] >= 5) { // if 5 or more failed attempts
blockedUsers[name] = true; // block the user
cout << "User " << name << " is now blocked due to multiple failed attempts.\n";
} else {
cout << "Authentication failed. Attempts left: " << (5 - failedAttempts[name]) << endl;
}
return false;
}
}
double getBalance() {
return amount;
}
AccountType getAccountType() {
return acnt;
}
};
class FraudDetection {
public:
bool alertSuspiciousTransaction(double amt) {
if (amt > 10000000) {
cout << "Fraudulent activity detected.\n";
return true;
}
return false;
}
};
class Transaction : public FraudDetection {
public:
void deposit(User& u, double amount) {
u.amount += amount;
}
void withdraw(User& u, double amount) {
u.amount -= amount;
}
};
class ATM : public Transaction {
public:
vector<User> acnt;
void getUserInfo(string name, string userID, string accountN, double amount, AccountType at) {
User usr;
usr.name = name;
usr.userID = userID;
usr.accountNo = accountN;
usr.amount = amount;
usr.acnt = at;
acnt.push_back(usr);
blockedUsers[name] = false; // Initialize blocked status as false
failedAttempts[name] = 0; // Initialize failed attempts to 0
}
bool Authenticate(User& usr, string authData) {
bool result = false;
for (auto& it : acnt) {
if (usr.name == it.name && usr.accountNo == it.accountNo) {
result = it.authenticate(authData);
break;
}
}
return result;
}
void addToBalance(User& usr, string auth, double amt) {
if (Authenticate(usr, auth)) {
for (auto& it : acnt) {
if (it.name == usr.name && it.accountNo == usr.accountNo) {
if (!alertSuspiciousTransaction(amt)) {
deposit(it, amt);
cout << "Amount added. New balance: " << it.getBalance() << endl;
}
}
}
} else {
cout << "Authentication failed. Cannot add to balance.\n";
}
}
void removeFromBalance(User& usr, string auth, double amt) {
if (Authenticate(usr, auth)) {
for (auto& it : acnt) {
if (it.name == usr.name && it.accountNo == usr.accountNo) {
if (!alertSuspiciousTransaction(amt)) {
withdraw(it, amt);
cout << "Amount withdrawn. New balance: " << it.getBalance() << endl;
}
}
}
} else {
cout << "Authentication failed. Cannot withdraw from balance.\n";
}
}
};
int main() {
// Example usage
Security security;
authenticateData["John"] = security.encrypt("password123"); // Encrypted data stored
ATM atm;
atm.getUserInfo("John", "12345", "acc987", 1000, Saving);
User usr;
usr.name = "John";
usr.accountNo = "acc987";
atm.addToBalance(usr, "password123", 500); // Will authenticate and add to balance
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...