Skip to content

Commit db08494

Browse files
committed
Finished HW3
1 parent 4dd2c28 commit db08494

13 files changed

+375
-59
lines changed

Homework2/Exercise3/Bank.cpp

-32
This file was deleted.

Homework3/Account.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ Account::Account(const char iban[], int ownerId, double amount) : ownerId(ownerI
66
strcpy(this->iban, iban);
77
}
88

9+
const char* Account::getIban() const{
10+
return this->iban;
11+
}
12+
13+
int Account::getOwnerId() const{
14+
return this->ownerId;
15+
}
16+
917
/*
1018
void Account::setIban(const char newIban[]){
1119
strcpy(iban, newIban);

Homework3/Account.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
class Account{
66
public:
77
Account(const char [] = "unspecified", int = 0, double = 0);
8+
virtual Account* clone() const = 0;
89

910
virtual void deposit(double) = 0;
1011
virtual bool withdraw(double) = 0;
1112
virtual void display() const = 0;
1213

1314
double getBalance() const;
14-
//void setIban(const char []);
15+
const char* getIban() const;
16+
int getOwnerId() const;
1517
protected:
16-
char iban[STANDARD_IBAN_LENGTH];
18+
char iban[STANDARD_IBAN_LENGTH+1];
1719
int ownerId;
1820
double amount;
1921
};

Homework3/Bank.cpp

+233
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
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+
}

Homework2/Exercise3/Bank.h renamed to Homework3/Bank.h

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#include <iostream>
2+
#include <vector>
23
#include "Account.cpp"
4+
#include "CurrentAccount.cpp"
5+
#include "SavingsAccount.cpp"
6+
#include "PrivilegeAccount.cpp"
37
#include "Customer.cpp"
48

59
class Bank{
@@ -17,25 +21,23 @@ class Bank{
1721
bool deleteCustomer(int);
1822

1923
bool addAccount(const char*, const char [], int, double);
20-
bool deleteAcccount(const char []);
24+
bool deleteAccount(const char []);
2125
void listAccounts() const;
2226
void listCustomerAccount(int) const;
2327

28+
bool withdraw(const char[], double);
29+
bool deposit(const char[], double);
2430
bool transfer(const char[], const char[], double);
2531
void display() const;
2632

2733
private:
2834
char* name;
2935
char* address;
30-
Customer** customers;
31-
int maxCustomers;
32-
int curCustomers;
33-
Account** accounts;
34-
int maxAccounts;
35-
int curAccounts;
36-
37-
void resizeCustomers();
38-
void resizeAccounts();
36+
std::vector<Customer*> customers;
37+
std::vector<Account*> accounts;
38+
39+
int customerPos(int);
40+
int accountPos(const char[]);
3941

4042
void copy(const Bank&);
4143
void erase();

Homework3/CurrentAccount.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
CurrentAccount::CurrentAccount(const char iban[], int ownerId, double amount) : Account(iban, ownerId, amount) {
55
}
66

7+
Account* CurrentAccount::clone() const{
8+
return new CurrentAccount(*this);
9+
}
10+
711
void CurrentAccount::deposit(double amountToDeposit){
812
this->amount+=amountToDeposit;
913
}

Homework3/CurrentAccount.h

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class CurrentAccount : public Account{
66
public:
77
CurrentAccount(const char [] = "unspecified", int = 0, double = 0);
88

9+
Account* clone() const;
10+
911
void deposit(double);
1012
bool withdraw(double);
1113
void display() const;

Homework3/Customer.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ void Customer::display() const{
4343
}
4444

4545
void Customer::copy(const Customer& other){
46+
this->id = other.id;
4647
this->name = new char[strlen(other.name) + 1];
4748
strcpy(this->name, other.name);
4849
this->address = new char[strlen(other.address) + 1];

Homework2/Exercise3/PrivilegeAccount.cpp renamed to Homework3/PrivilegeAccount.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ PrivilegeAccount::PrivilegeAccount(const char iban[], int ownerId, double amount
55

66
}
77

8-
PrivilegeAccount::deposit(double amountToDeposit){
8+
Account* PrivilegeAccount::clone() const{
9+
return new PrivilegeAccount(*this);
10+
}
11+
12+
void PrivilegeAccount::deposit(double amountToDeposit){
913
this->amount+=amountToDeposit;
1014
}
1115

12-
PrivilegeAccount::withdraw(double amountToWithdraw){
16+
bool PrivilegeAccount::withdraw(double amountToWithdraw){
1317
if((this->amount + this->overdraft) - amountToWithdraw < 0)
1418
return false;
1519

0 commit comments

Comments
 (0)