|
| 1 | +#include <iostream> |
| 2 | +#include <cstring> |
| 3 | +#include <vector> |
| 4 | +#include "Bank.h" |
| 5 | + |
| 6 | +Bank::Bank(const char* name, const char* address) { |
| 7 | + this->name = new char[strlen(name) + 1]; |
| 8 | + strcpy(this->name, name); |
| 9 | + this->address = new char[strlen(address) + 1]; |
| 10 | + strcpy(this->address, address); |
| 11 | +} |
| 12 | + |
| 13 | +Bank::Bank(const Bank& other){ |
| 14 | + copy(other); |
| 15 | +} |
| 16 | + |
| 17 | +Bank& Bank::operator=(const Bank& other){ |
| 18 | + if(this!=&other){ |
| 19 | + erase(); |
| 20 | + copy(other); |
| 21 | + } |
| 22 | + |
| 23 | + return *this; |
| 24 | +} |
| 25 | + |
| 26 | +Bank::~Bank(){ |
| 27 | + erase(); |
| 28 | +} |
| 29 | + |
| 30 | +const char* Bank::getName() const{ |
| 31 | + return this->name; |
| 32 | +} |
| 33 | + |
| 34 | +const char* Bank::getAddress() const{ |
| 35 | + return this->address; |
| 36 | +} |
| 37 | + |
| 38 | +void Bank::listCustomers() const{ |
| 39 | + std::cout<<"The bank has the following customers: \n"; |
| 40 | + for(int i=0; i<this->customers.size(); i++){ |
| 41 | + this->customers[i]->display(); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +bool Bank::addCustomer(int id, const char* name, const char* address){ |
| 46 | + if(customerPos(id) == -1){ |
| 47 | + this->customers.push_back(new Customer(id, name, address)); |
| 48 | + return true; |
| 49 | + } |
| 50 | + return false; |
| 51 | +} |
| 52 | + |
| 53 | +bool Bank::deleteCustomer(int ownerId) { |
| 54 | + int customerPos = this->customerPos(ownerId); |
| 55 | + |
| 56 | + if(customerPos == -1) |
| 57 | + return false; |
| 58 | + |
| 59 | + this->customers.erase(this->customers.begin() + customerPos); |
| 60 | + |
| 61 | + for(int i=0; i<this->accounts.size(); i++){ |
| 62 | + if(this->accounts[i]->getOwnerId() == ownerId){ |
| 63 | + this->accounts.erase(this->accounts.begin() + i); |
| 64 | + --i; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return true; |
| 69 | +} |
| 70 | + |
| 71 | +int Bank::customerPos(int id){ |
| 72 | + for(int i=0; i<this->customers.size(); i++){ |
| 73 | + if(this->customers[i]->getId() == id) |
| 74 | + return i; |
| 75 | + } |
| 76 | + |
| 77 | + return -1; |
| 78 | +} |
| 79 | + |
| 80 | +bool Bank::addAccount(const char* accountType, const char iban[], int ownerId, double amount){ |
| 81 | + if(customerPos(ownerId) == -1){ |
| 82 | + std::cerr<<"Trying to add an account to an invalid customer!\n"; |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + if(accountPos(iban) != -1){ |
| 87 | + std::cerr<<"Trying to create an account that already exists!\n"; |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + if(strcmp(accountType, "CurrentAccount") == 0){ |
| 92 | + this->accounts.push_back(new CurrentAccount(iban, ownerId, amount)); |
| 93 | + } else if(strcmp(accountType, "SavingsAccount") == 0){ |
| 94 | + this->accounts.push_back(new SavingsAccount(iban, ownerId, amount)); |
| 95 | + } else if(strcmp(accountType, "PrivilegeAccount") == 0){ |
| 96 | + this->accounts.push_back(new PrivilegeAccount(iban, ownerId, amount)); |
| 97 | + } else { |
| 98 | + std::cerr<<"Invalid account type!\n"; |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + return true; |
| 103 | +} |
| 104 | + |
| 105 | +bool Bank::deleteAccount(const char iban[]){ |
| 106 | + int position = accountPos(iban); |
| 107 | + |
| 108 | + if(position == -1){ |
| 109 | + std::cerr<<"Trying to delete an account that does not exist!\n"; |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + this->accounts.erase(this->accounts.begin() + position); |
| 114 | + return true; |
| 115 | +} |
| 116 | + |
| 117 | +void Bank::listAccounts()const{ |
| 118 | + std::cout<<"The bank has the following accounts: \n"; |
| 119 | + |
| 120 | + for(int i=0; i<this->accounts.size(); i++) |
| 121 | + this->accounts[i]->display(); |
| 122 | +} |
| 123 | + |
| 124 | +int Bank::accountPos(const char iban[]){ |
| 125 | + for(int i=0; i<accounts.size(); i++){ |
| 126 | + if(strcmp(this->accounts[i]->getIban(), iban) == 0) |
| 127 | + return i; |
| 128 | + } |
| 129 | + |
| 130 | + return -1; |
| 131 | +} |
| 132 | + |
| 133 | +void Bank::listCustomerAccount(int customerId) const{ |
| 134 | + std::cout<<"The customer with id "<<customerId<<" has the following accounts:\n"; |
| 135 | + for(int i=0; i<this->accounts.size(); i++) |
| 136 | + if(this->accounts[i]->getOwnerId() == customerId) |
| 137 | + this->accounts[i]->display(); |
| 138 | +} |
| 139 | + |
| 140 | +bool Bank::withdraw(const char iban[], double amount){ |
| 141 | + Account *acc=nullptr; |
| 142 | + for(int i=0; i<this->accounts.size(); i++){ |
| 143 | + if(strcmp(this->accounts[i]->getIban(), iban) == 0){ |
| 144 | + acc = this->accounts[i]; |
| 145 | + break; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + if(acc == nullptr){ |
| 150 | + std::cerr<<"Invalid account!\n"; |
| 151 | + return false; |
| 152 | + } |
| 153 | + |
| 154 | + if(!acc->withdraw(amount)) |
| 155 | + return false; |
| 156 | + |
| 157 | + return true; |
| 158 | +} |
| 159 | + |
| 160 | +bool Bank::deposit(const char iban[], double amount){ |
| 161 | + Account *acc=nullptr; |
| 162 | + for(int i=0; i<this->accounts.size(); i++){ |
| 163 | + if(strcmp(this->accounts[i]->getIban(), iban) == 0){ |
| 164 | + acc = this->accounts[i]; |
| 165 | + break; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + if(acc == nullptr){ |
| 170 | + std::cerr<<"Invalid account!\n"; |
| 171 | + return false; |
| 172 | + } |
| 173 | + |
| 174 | + acc->deposit(amount); |
| 175 | + |
| 176 | + return true; |
| 177 | +} |
| 178 | + |
| 179 | +bool Bank::transfer(const char ibanFrom[], const char ibanTo[], double amount){ |
| 180 | + Account *from=nullptr, *to=nullptr; |
| 181 | + for(int i=0; i<this->accounts.size(); i++){ |
| 182 | + if(strcmp(this->accounts[i]->getIban(), ibanFrom) == 0){ |
| 183 | + from = this->accounts[i]; |
| 184 | + } |
| 185 | + if(strcmp(this->accounts[i]->getIban(), ibanTo) == 0){ |
| 186 | + to = this->accounts[i]; |
| 187 | + } |
| 188 | + } |
| 189 | + if(from == nullptr || to == nullptr){ |
| 190 | + std::cerr<<"Invalid accounts!\n"; |
| 191 | + return false; |
| 192 | + } |
| 193 | + |
| 194 | + if(!from->withdraw(amount)) |
| 195 | + return false; |
| 196 | + |
| 197 | + to->deposit(amount); |
| 198 | + return true; |
| 199 | +} |
| 200 | + |
| 201 | +void Bank::display() const{ |
| 202 | + std::cout<<"This is a bank with name "<<this->getName() |
| 203 | + <<"\nIt is located on address "<<this->getAddress() |
| 204 | + <<"\nIt has "<<(int) this->customers.size()<<" customers, as well as " |
| 205 | + <<(int) this->accounts.size()<<" accounts\n"; |
| 206 | +} |
| 207 | + |
| 208 | +void Bank::copy(const Bank& other){ |
| 209 | + this->name = new char[strlen(other.name) + 1]; |
| 210 | + strcpy(this->name, other.name); |
| 211 | + this->address = new char[strlen(other.address) + 1]; |
| 212 | + strcpy(this->address, other.address); |
| 213 | + |
| 214 | + for(int i=0; i<other.customers.size(); i++) |
| 215 | + this->customers.push_back(new Customer(*(other.customers[i]))); |
| 216 | + |
| 217 | + for(int i=0; i<other.accounts.size(); i++) |
| 218 | + this->accounts.push_back(other.accounts[i]->clone()); |
| 219 | +} |
| 220 | + |
| 221 | +void Bank::erase(){ |
| 222 | + delete[] this->name; |
| 223 | + delete[] this->address; |
| 224 | + |
| 225 | + for(int i=0; i<this->customers.size(); i++) |
| 226 | + delete this->customers[i]; |
| 227 | + this->customers.clear(); |
| 228 | + |
| 229 | + for(int i=0; i<this->accounts.size(); i++){ |
| 230 | + delete this->accounts[i]; |
| 231 | + } |
| 232 | + this->accounts.clear(); |
| 233 | +} |
0 commit comments