@AquariusGX

QQ:651572770 加我请注明来意。 twitter: @aquariusgx

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

bank

================================================================
            biz.h
================================================================

//opration
/* p : password of account
   b : balance of account
*/
long create(int p, double b);
void save(double sum);
int withdraw(int p, double sum);
double query(int p);
long generateId();

================================================================
            biz.cpp
================================================================

#include <iostream>
using namespace std;

static int id;
static int passwd;
static double balance;

long generateId()
{
     static int id = 1;
     return id++;
}
long create(int p, double b)
{
     id = generateId();
     passwd = p;
     balance = b;
     return id;     
}
void save(double sum)
{
     balance += sum;
}
int withdraw(int p, double sum)
{
    if(p != passwd) 
    { 
         cout << "Invalid password!" << endl; 
         return -1;
    }
    if(balance < sum)
    {
         cout << "No enough money!" << endl;
    }
    balance -= sum;
    return 0;
}
double query(int p)
{
    if(p != passwd)
    {
         cout << "Invalid password!" << endl;
         return -1;
    }
    else
         return balance;
}

================================================================
            menu.h
================================================================

int showMenu();
void createMenu();
void withdrawMenu();
void queryMenu();

================================================================
            menu.cpp
================================================================

 

#include <iostream>
#include "biz.cpp"
using namespace std;

int showMenu()
{
    cout << "Create -------> 1" << endl;
    cout << "Save   -------> 2" << endl;
    cout << "Withdraw -----> 3" << endl;
    cout << "Query --------> 4" << endl;
    cout << "Exit ---------> 0" << endl;
    cout << "Enter your choice:";
    int c;
    cin >> c;
    if(!cin)
            return -1;
    else
            return c;
}
void createMenu()
{
     int passwd;
     double balance;
     cout << "\t Enter password >";
     cin >> passwd;
     cout << "\t Enter balance >";
     cin >> balance;
     long id = create(passwd, balance);
     cout << "========================" << endl;
     cout << "Create acount OK! id = " << id << endl;
     cout << "========================" << endl;
}
void saveMenu()
{
     double sum;
     cout << "\t Enter sum >";
     cin >> sum;
     save(sum);
     cout << "========================" << endl;
     cout << "Save money OK!" << endl;
     cout << "========================" << endl;
}
void withdrawMenu()
{
     int passwd;
     double sum;
     cout << "\t Enter passwd >";
     cin >> passwd;
     cout << "\t Enter sum >";
     cin >> sum;
     int ret = withdraw(passwd, sum);
     if(ret == 0)
     {
            cout << "========================" << endl;
            cout << "Withdraw successful!" << endl;
            cout << "========================" << endl;
     } 
}
void queryMenu()
{
     int passwd;
     cout << "\t Enter passwd >";
     cin >> passwd;
     double ret = query(passwd);
     if(ret != -1)
     {
            cout << "========================" << endl;  
            cout << "BALANCE: $" << ret << endl;
            cout << "========================" << endl;       
     }
     else
     {
         cout << "========================" << endl;
         cout << "Invalid passwd" << endl;
         cout << "========================" << endl;
     }
}

================================================================
            main.cpp
================================================================

 

#include <iostream>
#include "menu.cpp"
using namespace std;
int main()
{
    int c = 0;
    do
    {
        c = showMenu();
        if(c == -1) { break; }
        switch(c)
        {
                 case 1:
                      createMenu();
                      break;
                 case 2:
                      saveMenu();
                      break;
                 case 3:
                      withdrawMenu();
                      break;
                 case 4:
                      queryMenu();
                      break;
                 case 0:
                      cout << "========================" << endl;
                      cout << "Good Bye!" << endl;
                      cout << "========================" << endl;
                      break;
                 default:
                      cout << "========================" << endl;
                      cout << "Invalid option, try again." << endl;
                      cout << "========================" << endl;
                      break;
        }
    }while(c != 0);
    return 0;
}
posted on 2010-03-02 01:36  aquariusgx  阅读(384)  评论(0编辑  收藏  举报