Bank account

Greeting everyone!

It is the first time i post something in this forum, so i'll do my best.

I have the following assignment (I apologize for the bad translation):

---------------------------------------------------------------------------

Write a programm in order to implement an easy bank account. The account should belong to a bank client. The client's following operations should be known: Name,
account number, bank code, balance and max. withdraw available.
A bank client should be able to do withdraws, deposits, and tranfers with his bank account.

Proceed in the following way
• Create a new proyect name exercise4b.
• Add an empty header file name BankKonto.h to the proyect. This file should contain the definition of the class "bankkonto" but not the definition (implementation) of the class functions.
• Add an empty source file name BankKonto.cpp to the proyect. This file should contain: Definition and implementation of all functions (methods) of the class Bankkonto.

• Add an empty source file name main.cpp to the proyect. This file should contain the main() function.

Class definition (BankKonto.h):
1. Define a struct name "client" which contains the following information: Name, account number, bank code, balance and maximum withdraw.
2. Add the class BankKonto a private attribute type "client".
3. Define a default constructor, which set the client's name to "Max Mustermann". The eight digit account number should be set zero, the balance to zero and maximum withdraw to 300eu.

4. Define a second constructor, which receives the values of the client's attributes: Name, account number, balance and maximum withdraw.

5. Define accesors to "set" and "get" the class' attributes.

6. Define functions for depositing, withdrawing, and transfering (from own account into an other). Be sure to update the balance. Respect the maximum withdraw. You cannot transfer/withdraw more money than the existing one.

7. Define a function to print the client's name and balance.

8. Implementate the functions defined above and declared in the file Bankkonto.h in the file BankKonto.cpp.

Main programm (main.cpp):
• Create two objects from type BankKonto name "KontoFabian" and "kontoAlex" Use by this step the default constructor and the second constructor.

• Print the account information using the function defined in step 7.

• Deposit different amounts in the accounts.

• Transfer 300eu. from Alex's account to Fabian's account.

• Print the new account informations.

------------------------------------------------------

header file:

class bankKonto{
public:
struct client{
char name;
unsigned accountNumber;
unsigned bankCode;
float balance;
int maximumWithdraw;
};
private:
client x;

bankKonto(){};
};

----------------------------------------------------------

#include<iostream>
#include "BankKonto.h"
using namespace std;
//default Konstruktor:
bankKonto{ //hiermit erstelle ich einen Konstruktor Namens "x" mit struct "kunde"... wie benenne ich den Konstruktor??
name=Max_Mustermann;
kontonummer=0; //account number
kontostand=0; //balance
maximaleAuszahlung=300; // maximum withdraw

};

//zweiter Konstruktor: //second constructor
void bankKontoWerte(char nombre, unsigned numeroDeCuenta, unsigned cbu, float estadoDeCuenta, int extraccionMaxima){
cout<<"Name: ";
cin>>nombre;
cout<<endl<<endl;
cout<<"Kontonummer: ";
cin>>numeroDeCuenta;
cout<<endl<<endl;
cout<<"BLZ: ";
cin>>cbu;
cout<<endl<<endl;
cout<<"Kontostand: ";
cin>>estadoDeCuenta;
cout<<endl<<endl;
cout<<"Maximale Auszahlung: ";
cin>>extraccionMaxima;
cout<<endl<<endl;
};

public:
void setNamen (char nombre){
name=nombre;
};
char getNamen (){
return name;
};
void setKontonummer (unsigned numeroDeCuenta){
kontonummer=numeroDeCuenta;
};
unsigned getKontonummer (){
return kontonummer;
};
void setBLZ (unsigned cbu){
BLZ=cbu;
};
unsigned getBLZ (){
return BLZ;
};
void setKontostand (float estadoDeCuenta){
kontostand=estadoDeCuenta;
};
float getKontostand (){
return kontostand;
};
void setMaximaleAuszahlung(int extraccionMaxima){
maximaleAuszahlung=extraccionMaxima;
};
int getMaximaleAuszahlung (){
return maximaleAuszahlung;
};

float einzahlung (float e){ //einzahlung=deposit
return kontostand+=kontostand+e;
};

float abbuchung (float a){ //abbuchung: debit
cin>>a;
if (a>=300||a>=kontostand){cout<<"Entweder Ihr Kontostand ist nicht ausreichend oder die maximale Auszahlung wurde überschritten."<<endl<<"Bitte geben Sie eine gültige Zahl ein (Zahl<=300)"<<endl;
continue;}
else {return kontostand=kontostand-a;}
};

/*float bankKonto::ueberweisung (float u){ //transfer
if (a>=kontostand){cout<<"Ihr Kontostand ist nicht ausreichend."<<endl<<"Bitte geben Sie eine gültige Zahl ein"<<endl;
continue;
}
else {
return kontostand1=kontostand1-u; //wie mache ich diese Funktion?
kontostand2=kontostand2+u;
}

};*/

void ausgabe (){
cout<<"Kontonummer: "<<kontonummer<<endl;
cout<<"Name: "<<name<<endl;
cout<<"Kontostand: "<<kontostand<<endl;
};

---------------------------------------------------------------------

I have the following problems here:

* I've never worked with a struct within a class. Therefore, i don't know how to call this struct from the main programm. In a similar way, it is the first time i work with a head, source file. I don't know how to call both of this from the main programm.
* I don't even know how to start doing a tranfer from one guy's account to the other.
* I know, the programm isn't finished and it looks horrible, but if you help me answer this questions, i might be able to finish it.

Give me a hand please!

Thanks in advance!
closed account (3TXyhbRD)
Use code tags.

A problem I see with your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class bankKonto{
public: 
    struct client{
        char name;
        unsigned accountNumber;
        unsigned bankCode;
        float balance;
        int maximumWithdraw;
    };
private:
    client x;

    bankKonto(){};
};

You define a struct and then use that struct to define the structure of the class. I have no idea why you would want to design your program like that. You can simply define the structure of your class like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
class bankKonto{
public: 
    char name;
    unsigned accountNumber;
    unsigned bankCode;
    float balance;
    int maximumWithdraw;

private:
    client x;

    bankKonto(){};
};

Another problem is that your name is a character (char) and not an array of characters (char[]/char*) or better yet not a string instance(C++ string documentation: http://www.cplusplus.com/reference/string/string/).

To access your struct from outside the class you can use the resolution operator:
 
bankKonto::client clientStruct;


One small note about header files, .h are C header files (inherited by C++ because C is almost fully contained by C++). Instead of a .h have a corresponding header file extension to you source file extension. E.g.: if your source file ends with .cpp the header file should end with .hpp. If you source file ends with .cc your header file should end with .hh. I personally recommend to go with the .hpp/.cpp extensions since they are supported by any decent C++ compiler.

When dealing with header files use macro definitions (header guards) to ensure that situations like
1
2
#include "BankKonto.h"
#include "BankKonto.h" 

Won't crash your code. More about header guards: http://www.cplusplus.com/forum/articles/10627/

PS: your problem seems complex enough to have an DDD approach (at least for DDD practice). Do you have a conceptual model for your entities to clearly see the relations between them?
Hi there,

leatorres87 wrote:
* I've never worked with a struct within a class. Therefore, i don't know how to call this struct from the main programm. In a similar way, it is the first time i work with a head, source file. I don't know how to call both of this from the main programm


A struct within a class can be accessed using the scope resolution (::) operator :

1
2
3
4
5
6
7
8
9
class Foo
{
    public:
         struct Bar{};
         Bar b;
}

//create a new Bar():
Foo::Bar some_bar;


You should #include "BankKonto.h" in the file where you have your main() function.
Then make sure that you compile the BankKonto.cpp file together with the main.cpp file (or make sure they are in the same project in your IDE). For g++ for instance one would do:

g++ -o my_application  main.cpp BankKonto.cpp



leatorres87 wrote:
* I don't even know how to start doing a tranfer from one guy's account to the other.


Let's have a look at the bankaccount class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class bankKonto{
public:
struct client{
char name;
unsigned accountNumber;
unsigned bankCode;
float balance;
int maximumWithdraw;
};
private:
client x;

bankKonto(){};
};


First off - it seems weird to me that BankAccount contains the blueprint for a client.
A client typically owns a bankaccount, not the other way around.
It's also strange that "client" contains information that actually belongs to the account, such as the balance and maximumWithDraw.

Anyway - making a transfer between two clients "sender" and "receiver', would involve that you decrease the senders "float balance", and add the same amount to the receivers balance. This is simplistic off course, in reality one would have a fail-over mechanism in case the process fails at any point, so the sender wouldn't just loose their money, or the receiver wouldn't get they money if the sender didn't have it.

Some more tips in general:

- Revise the design of the bankaccount and client class, something more natural would be:

1
2
3
4
5
6
7
8
9
10
11
12
13
struct bankaccount
{
    unsigned accountNumber;
    unsigned bankCode;
    float balance;
    int maximumWithdraw;
};

struct client
{
    std::string name;
    bankaccount account;
};


This way a client could even be extended to own multiple accounts if necessary (by creating an array of accounts).

- Try to stick to using one language in your code. It doesn't have to be English necessarily, but mixing German and Spanish will get anyone looking at your code confused quite easily.

Hope that gets you on your way.
Please do let us know if you have any further questions.

All the best,
NwN
Topic archived. No new replies allowed.