Sign in to follow this  
LordMattias

Introduction and Bank Project

Recommended Posts

Greetings.

I recently created an account here and intend to become an active member of this wealthy forum.

My name is Matt, I'm 23, and my programming experience is modest and mostly structural/procedral style. I've only taken a few classes in programming over the years (mostly C and some easier learning languages) and now I'm currently learning C++, and so far things have been going relatively smoothly.

Much like I had in college i'm working on console projects to get my chops up before I start attempting any graphical interfaces and programming...

Anyways... What's the best way to post source code on the forums? I've got a project i've been working on that i could use a hand on...

Share this post


Link to post
Share on other sites
If you look in the faq in the top right it will show you all the tags you can use in the forum. To post code your going to want to use the source tag. hope that helps

Share this post


Link to post
Share on other sites
Thank you Agisler!

So the project i've given myself is to create an increasingly complex 'Bank' console program. As you can see from the 'Project Plans' commented out at the start, i've got some general guidelines on where i want the project to go (i tend to add new versions in between as i think of features i can add to improve the code).

I would greatly appreciate it if someone would glance over my code and watch for any obvious pitfalls or bad habits i may be forming.

I know i'm lacking documentation i'll fix that as i post new versions...

It may become blaringly obvious how new to OOP i am as this project progresses.

The next feature i'm trying to add is a transaction history (which only saves during a single runtime for now... until i start adding data files in v0.75)

What would be a good way to approach this problem?
I need the each line of the history log to contain the following:
-Username
-Date/Time
-Type of transaction (deposit/withdraw)
-Amount transferred

[/* Bank v0.63 - Coded by LordMattias 
%%%%%%%%%%%%%%%%%
Project Plans
%%%%%%%%%%%%%%%%%
v0.55 (FINISHED)
-User verification (Account name/pass only w/o data file)
v0.56 (FINISHED)
-Changed menu from mostyl numerals (1,2,3,q) to all letters (b,d,w,q)
v0.58 (FINISHED)
-Input error checking - Asks again for correct input type without crashing.
Except for strings with whitespace... look into later.
v0.60 (FINISHED)
-Date and time on access granted...
v0.62 (FINISHED)
-Prevent withdrawal that exceeds account.balance
v0.63 (FINISHED)
-Now works with doubles instead of Int.
v0.65 (IN PROGRESS)
-Add Transaction History with date/time/trans-type/ammount transferred
(For single program run)
v0.70
-Create Admin Account with additional menu items. (No functionality yet)
v0.75
-Create data file that holds the following:
-Account Names/Passwords
-Account Balances
v0.80
-User verification (Account name/pass from data file)
v0.85
-Allow Transfers to other Bank Members
v0.90
-Add small fee for transferring and withdrawing, that deducts from their account.
v1.00
-Regular user versus Administrative priviliges
-Admin: expanded menu... delete accounts, check any accounts transaction history, etc...
-User: Regular Menu that a customer would see
*/

#include <iostream>
#include <string>
#include <ctime>
using namespace std;

class Bank {
public:
double balance;
string username;
string password;

double deposit(double input, double account);
double withdraw(double input, double account);
};
double Bank::deposit(double input, double account) {
return (account + input);
}
double Bank::withdraw(double input, double account) {
if(account >= input)
return(account - input);
else {
cout << "Sorry, you don't have that much money in your account." << endl;
return(account);
}
}
Bank account; // Creates global account object.

class Menu {
public:
bool login();
void display();
void choice(char ch);
bool isvalid(char ch);
};
bool Menu::login() {
string str;

cout << "\nWelcome to Bank of Swanklandia\n";

// Username
for(;;) {
cout << "Enter Username: ";
getline(cin, str);
if(str != account.username)
cout << "Invalid Username.\nPlease try again" << endl;
else
break;
}
// Password
cout << "Enter Password: ";
getline(cin, str);
if(str == account.password) {
cout << "Access Granted!\n" << endl;
return 1;
}
else {
cout << "Access Denied. Lockout Initiated." << endl;
return 0;
}
}
void Menu::display() {
char date[10];
char time[10];
_strdate(date);
_strtime(time);
cout << "The time is " << time << " on " << date << "\n\n";
cout << "Bank Menu\n";
cout << "b) Balance Inquiry\n";
cout << "d) Deposit\n";
cout << "w) Withdraw\n";
cout << "h) View Transaction History(not yet available)\n";
cout << "q) Quit\n";
cout << "\nYour selection: ";
}
void Menu::choice(char ch) {
double x;
switch(ch) {
case 'b' :
cout << "Your current balance is: $" << account.balance << "\n\n";
break;
case 'd':
cout << "How much would you like to deposit?: ";
cin >> x;
account.balance = account.deposit(x, account.balance);
break;
case 'w':
cout << "How much would you like to withdraw?: ";
cin >> x;
account.balance = account.withdraw(x, account.balance);
break;
}
}
bool Menu::isvalid(char ch) {
if(ch != 98 && ch != 100 && ch != 119 && ch !='q')
return false;
else
return true;
}

int main()
{
char input;
Menu mobj;
account.balance = 2500.95; // Sets initial account balance
account.username = "Mattias";
account.password = "chicken";

if(mobj.login() == 0)
return 0; // Authenticates user, exits if they fail.

for(;;) {
do {
mobj.display();
cin >> input;
if(!mobj.isvalid(input))
cout << "Invalid selection, please enter a letter (ex: 'b' for balance)" << endl;
}while(!mobj.isvalid(input));

if(input == 'q')
break;
cout << endl;

mobj.choice(input);
}
return 0;
}]

Share this post


Link to post
Share on other sites
Ok this issue is confusing me profoundly... It's actually crashing my console.

Take a look at this code:
_strdate(date);
account.history[2][hcount] = date;

_strtime(time);
account.history[3][hcount] = time;

So if i remove the time, my code runs fine. It stores the date properly and displays that when i call it. But when i tried to also add a timestamp in the exact same fashion (which worked fine before in the menu display (see code from post above in Menu::display() ), it crashes the console... (and i don't mean loop, i mean send error report to microsoft...)

I'm at a loss... Why doesn't this work? And if it won't is there another way i can capture the current timestamp to a string?

Share this post


Link to post
Share on other sites
Quote:
Original post by LordMattias
Ok this issue is confusing me profoundly... It's actually crashing my console.

Take a look at this code:
_strdate(date);
account.history[2][hcount] = date;

_strtime(time);
account.history[3][hcount] = time;

So if i remove the time, my code runs fine. It stores the date properly and displays that when i call it. But when i tried to also add a timestamp in the exact same fashion (which worked fine before in the menu display (see code from post above in Menu::display() ), it crashes the console... (and i don't mean loop, i mean send error report to microsoft...)

I'm at a loss... Why doesn't this work? And if it won't is there another way i can capture the current timestamp to a string?


Welp.. i figured it out. I forgot to initialize the extra room in the array for the time stamp... dur...

Share this post


Link to post
Share on other sites
While not required and won't effect your program at all, you might want to think about setting "balance", "username" and "password" variables to private and adding getter/setter functions. As I said it's not necessary but it's a good habit to get into and *COULD* save you some complications later on.


class Bank {
private:
double balance;
string username;
string password;

public:
double deposit(double input, double account);
double withdraw(double input, double account);

double getbalance();
string getusername();
string getpassword();

};

Share this post


Link to post
Share on other sites
Quote:
Original post by Six222
While not required and won't effect your program at all, you might want to think about setting "balance", "username" and "password" variables to private and adding getter/setter functions. As I said it's not necessary but it's a good habit to get into and *COULD* save you some complications later on.

*** Source Snippet Removed ***


Well since this program just keeps getting more complex every day, it would probably serve me well to do that... I've yet to use 'private:' in a class yet.

Would that be particularly helpful for one of my next steps: adding a data file to draw all user account info from?

Share this post


Link to post
Share on other sites
Most likely not, but definitely when you decide that your account class needs to store things differently(which guaranteed will happen to all code eventually, but due to the amount of work you've put in, one small change there now effects every other piece of code that works with it, and you have a problem. But with private data/public functions, you can change the underlying data storage, and only have to change one or two functions(the accessor of that data, as "double GetBalance()" is to "double balance").

Share this post


Link to post
Share on other sites
A couple of things:

  • Would "BankAccount" be a better name for your "Bank" class?

  • Why do the "withdraw" and "deposit" methods not set the account balance themselves? Generally, a class should provide an external interface (set of public methods and variables) that minimize the amount of messing other code has to do with stuff which could be handled internally. "Deposit" is a fairly well-defined and atomic transaction - hide details of this from the code calling the method

  • I think that there are clearer and more direct ways than for(;;) to write an infinite loop

  • Why the global "account" variable? Since you actually set up its data in main(), why not create the object there? You could pass it into the menu() method - later on you might pass a list so that all accounts can be checked for a match with the username and password the user enters. You might want to load accounts from a file or database - using a global variable makes the program very rigid and less extensible going forward

  • Your main() orchestrates the running of the menu, checking of input etc. - should it care about this? It's perfectly acceptable to put this code into the menu class and have a vary minimalistic main() which simply creates a couple of objects and calls a single method.

    Share this post


    Link to post
    Share on other sites
    I changed the name of the class to BankAccount instead, and am trying to do a few things:
    -Create a new BankAccount function that gathers all account data off a file.
    -Create a BankAccount object for each account.

    I'm at a loss on how to do this from within my program...

    The datafile holds account information delminated by spaces and newlines, ex.:
    username password balance
    username password balance
    ...

    or

    Mattias chicken 2500.95
    Trent turkey 980.63
    ...

    I'm not sure how to go about storing this information on my BankAccount class though...

    Share this post


    Link to post
    Share on other sites

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

    Sign in to follow this