#include <iostream>
#include <string>
#include <vector>
#include <cctype>
using std::cout;
using std::cin;
using std::string;
using std::vector;
class Account {
private:
string name, password;
double balance;
int account;
public:
Account(string accountName, string password, double initialBalance, int accountNumber)
: name(accountName), password(password), balance(initialBalance),
account(accountNumber) {}
void deposit(double amount) {
if (amount > 0) {
balance += amount;
cout << "Deposit successful. n";
}
}
bool transfer(int acc_to, int amount) {
if (amount > balance || amount < 0) {
cout << "Invalid amount!";
return false;
}
balance -= amount;
return true;
}
void changeName(string newName) {
name = newName;
cout << "Name changed successful. n";
}
void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
cout << "Withdraw successful. n";
}
else {
cout << "Insufficient Funds. n";
}
}
void display() {
cout << "Account: " << name << "nBalance: $" << balance << "nAccount Number: " <<
account << "n";
}
void set_id(int input) {
account = input;
}
int get_id() {
return account;
}
string const& getPass() {
return password;
}
string const& getName() {
return name;
}
};
void clear() {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
}
int main() {
string name, password, newName;
vector<Account> accounts;
double initialDeposit, amount;
int choice, logInChoice, accountNumber = 0, index = 0;
bool logged_in = false, account_has_digit = false, running = true;
while (running) {
while (logged_in == false) {
vector<string> loginMenu{ "Please log in or create an account.", "1. Create An
Account", "2. Log In" };
for (auto& option : loginMenu) {
cout << option << "n";
}
cout << "Enter Choice: ";
if (!(cin >> logInChoice) || logInChoice < 1 || logInChoice > 2) {
clear();
cout << "Invalid Input! n";
continue;
}
switch (logInChoice) {
case 1:
cout << "Enter an account name: ";
cin >> name;
for (auto& each : name) {
if (isdigit(each)) {
account_has_digit = true;
break;
}
}
if (account_has_digit) {
cout << "Invalid Input! Please use only characters. n";
clear();
continue;
}
cout << "Enter a Password: ";
cin >> password;
cout << "Enter initial deposit: n";
cin >> initialDeposit;
accountNumber++;
accounts.emplace_back(name, password, initialDeposit, accountNumber);
continue;
case 2:
cout << "Enter your account name: ";
cin >> name;
cout << "Enter a Password: ";
cin >> password;
for (auto& acc : accounts) {
if ((acc.getName() == name && acc.getPass() == password) && index <=
accounts.size()) {
logged_in = true;
break;
}
index++;
}
}
do {
vector<string> menu{ "1. Deposit", "2. Withdraw", "3. Display Account",
"4. Transfer Money", "5. Change Name", "6. Log Out", "7. Exit" };
for (auto& option : menu) {
cout << option << "n";
}
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter deposit amount: ";
cin >> amount;
accounts[index].deposit(amount);
break;
case 2:
cout << "Enter withdraw amount: ";
cin >> amount;
accounts[index].withdraw(amount);
break;
case 3:
accounts[index].display();
break;
case 4:
int transferAcc, transferAmount;
cout << "Input an account number to transfer to: ";
cin >> transferAcc;
cout << "nInput an amount: ";
cin >> transferAmount;
if (accounts[index].transfer(transferAcc, transferAmount) == true)
{
for (auto& acc : accounts) {
if (acc.get_id() == transferAcc) {
acc.deposit(transferAmount);
}
}
cout << "Transfer successful. n";
break;
}
case 5:
cout << "Enter new name: ";
cin >> newName;
accounts[index].changeName(newName);
break;
case 6:
logged_in = false;
index = 0;
break;
case 7:
logged_in = false;
running = false;
break;
default:
cout << "Invalid choice. n";
break;
}
} while (logged_in);
}
return 0;
}
}
This is my first use of classes in C++. A banking system with no persistence that can handle multiple accounts, login/logout, money transfer, and some error handling. Havnt messed much with password security. Just looking for general feedback on code structure and pointers on anything done wrong. Thanks