Simple implementation of Linux ATM self-defined teller machine

Source: Internet
Author: User
Tags strcmp


The first is implemented in the Linux underground, creating four files, the main implementation process:

Register-Login-deposit-withdraw-transfer-Change password-check personal information-Show all accounts-exit system

Don't say much nonsense, look directly at the code:

Blank.h


#include <iostream> #include <string> #include <string.h> #include <vector> #include < stdio.h> #include <algorithm> #include <stdlib.h> #include <pthread.h>using namespace std; #define Max_size 65535//Mutex initialization static pthread_mutex_t mutex_lock=pthread_mutex_initializer;//defines two states of the user, both online and unregistered enum status{  online,logout};//There is a drawback after logging out of the account can no longer use the struct user{string _name;//user name string _id; ID card string _block_name;    Card number string _passwd;     password int _money; Save the money int _total_money;   Total balance/Status status; User's State void Insert_user (const string &name= "", const string &id= "", const string &block_name= "", Const ST Ring &passwd= "", int money=0) {_name=name;_id=id;_block_name=block_name;_passwd=passwd;_money=money;_total_    Money=money;//status=online;    } bool Operator! = (const user &s) {return (strcmp (_block_name.c_str (), (S._block_name). C_STR ())!=0); }};//Registration-"landing-" deposit-"withdrawals-" query-"Show all information-" Change Password-"Logout user-" Exit system CLASS bank{Private:user value_user;vector<struct user> vector_user; Private:bank (const Bank &b);    bank& operator= (const Bank &b);    Public:bank () {}~bank () {}bool operator! = (const user &s) {return value_user.operator!= (s);}    public://transfer bool Transfer_account (const string& block_name,const int number) {string tmp_block_name;    cout<< "Please enter Block_name:";    Fflush (stdout);    cin>>tmp_block_name;    String passwd;    cout<< "Please enter passwd:";    Fflush (stdout);    cin>>passwd;    Vector<struct user>::iterator Tmp=find (TMP_BLOCK_NAME,PASSWD);    if (tmp!=vector_user.end ()) {if (Tmp->_total_money <number) {cout<< "Insufficient balance" <<endl; return false;} Vector<struct user>::iterator it=find (block_name);p thread_mutex_lock (&mutex_lock); Locking Tmp->_total_money-=number;it->_total_money+=number;pthread_mutex_unlock (&mutex_lock);//Unlock return    True } return false;} Log off user's account cannot be used again bOol Logout (const string &block_name,const string &passwd) {vector<struct user>::iterator tmp=find (block_    NAME,PASSWD);    if (tmp!=vector_user.end ()) {vector_user.erase (TMP); return true; } return false;} Change password bool change_passwd (const string &block_name,const string &passwd) {vector<struct User>::iterator t    Mp=find (BLOCK_NAME,PASSWD);    if (Tmp==vector_user.end ()) return false;    cout<< "Please enter the old passwd:";    Fflush (stdout);    String Old_passwd= "";    cin>>old_passwd; if (strcmp (Old_passwd.c_str (), Passwd.c_str ()) ==0) {cout<< "Please enter new passwd:"; fflush (stdout); string new_ Passwd= ""; Cin>>new_passwd;pthread_mutex_lock (&mutex_lock); Locking Tmp->_passwd=new_passwd;pthread_mutex_unlock (&mutex_lock);//unlock} return true;} User login bool Log_in (const string &block_name,const string &passwd) {vector<struct user>::iterator tmp=find    (BLOCK_NAME,PASSWD); if (tmp!=vector_user.end ()) return TRUe return false;} To determine whether the ID card is valid, you can view this ID to open 0 cards, but unfortunately I did not implement bool effective (const string &id) {vector<struct user>::iterator it=    Vector_user.begin ();    for (; It!=vector_user.end (); ++it) {if (strcmp (it->_id). C_str (), Id.c_str ()) ==0) return true; } return true;    Converts an integer to string get_string (int number) {char arr[max_size];//Because the maximum string value is Max_size memset (arr, ' ', sizeof (arr));    String Name= "";    sprintf (arr, "%d", number);//Convert an integer to a string after the presence of the Name=arr in the ARR array; return name;    The system assigns a user an account string get_name () {static int tmp=1;    if (TMP >max_size) return "";    String Name= ""; Pthread_mutex_lock (&mutex_lock);    Locking name+=get_string (TMP);    tmp++; Pthread_mutex_unlock (&mutex_lock);//unlock return name;}    System-assigned accounts start with 622123 string Get_block_name () {string name= "622123"; Pthread_mutex_lock (&mutex_lock);    Locking Name+=get_name (); Pthread_mutex_unlock (&mutex_lock);//unlock return name;} User Registration function bool Register (const string &name,const StRing &id)//user Registration {if (!effective (ID))//Determine whether the identity card is valid {cout<< "already exists this identity card" <<endl;return false; } string passwd; The initial state of the password string certain_passwd;    Determine the password cout<< "Please enter passwd:";    Fflush (stdout);    cin>>passwd;    cout<< "Please again enter passwd:";    Fflush (stdout);    cin>>certain_passwd; Two times password consistent registration successful//Then keep the structure in the vector array if (strcmp (Passwd.c_str (), Certain_passwd.c_str ()) ==0) {//////////locking Pthread_mute X_lock (&mutex_lock); Locking Value_user.insert_user (Name,id,get_block_name (), passwd) Vector_user.push_back (value_user);p thread_mutex_    Unlock (&mutex_lock);//unlock return true; } Elsereturn false;}    Look for the user vector<struct User>::iterator find (const string &block_name,const string &passwd) that already exists in the vector array {    Vector<struct user>::iterator It=find (block_name);    if (it!=vector_user.end ()) {if (strcmp (It->_passwd.c_str (), Passwd.c_str ()) ==0) return it; } return Vector_user.end ();} Heavy-duty finDvector<struct user>::iterator Find (const string &block_name) {vector<struct user>::iterator it=    Vector_user.begin ();    for (; It!=vector_user.end (); ++it) {if (strcmp (It->_block_name.c_str (), Block_name.c_str ()) ==0) return it; } return Vector_user.end ();} Deposit, before depositing, make sure the user has landed bool Deposit (const string &block_name,const string &passwd,const int money)//Deposit {if (log_in (BL OCK_NAME,PASSWD) {vector<struct user>::iterator tmp=find (BLOCK_NAME,PASSWD), if (TMP!=vector_user.end ()) {PTH Read_mutex_lock (&mutex_lock);    Locking tmp->_money=money;    tmp->_total_money+=money;    Pthread_mutex_unlock (&mutex_lock);//unlock}return true; } return false;} withdrawal bool Withdraw_money (const string &block_name,const string &passwd,const int &money) {vector<struct u    Ser>::iterator Tmp=find (BLOCK_NAME,PASSWD);    if (tmp!=vector_user.end ()) {if (Money > Tmp->_total_money) {cout<< "Insufficient balance" <<endl; return false;} /Locking Pthread_mutex_lock (&mutex_lock); Locking Tmp->_money-=money;tmp->_total_money-=money;pthread_mutex_unlock (&mutex_lock);//Unlock///////////    return true; } return false;} BOOL Check (const string &block_name,const string &passwd)//Use card number to find {vector<    struct User>::iterator it=find (BLOCK_NAME,PASSWD);         if (it!=vector_user.end ()) {cout<< "User:" <<it->_name<< "\ T ID:" <<it->_id;  cout<< "T-card number:" <<it->_block_name;    cout<< "\ t balance:" <<it->_total_money<<endl;return true; } return false;}    Show All information void Show () {vector<struct user>::iterator it=vector_user.begin ();    int i=1; for (; it!=vector_user.end (); ++it) {cout<< "Account No." <<i++<< "person" <<endl;cout<< "User:" << it->_name<< "\ t ID:" <<it->_id<< "\ T";cout<< "card number:" <<it->_block_name<< "\ t Password:" <<it->_passwd<< "\ T";cout<<"Balance:" <<it->_total_money<<endl;    } cout<< "Total number of open accounts:" <<i-1<<endl; i=0;}};


Blank.cpp

#include "Bank.h" static string name;//user static string ID;  ID static string block_name;//card number static string passwd;    password int output () {cout<< "******************************************" <<endl;    cout<< "* *" <<endl;    cout<< "* [1]: Registration [2]: Login *" <<endl;    cout<< "* [3]: Deposit [4]: Withdrawal *" <<endl;    cout<< "* [5]: query [6]: Change the secret *" <<endl;    cout<< "* [7]: Logout [8]: Transfer *" <<endl;    cout<< "* [9]: Display [0]: Exit *" <<endl;    cout<< "* *" <<endl;    cout<< "******************************************" <<endl;    Char key;    cout<< "Please select:";    Fflush (stdout);    cin>>key;    if (key >= ' 0 ' && key <= ' 9 ') return key; while (Key < ' 0 ' | | key > ' 9 ') {cout<< "Please re-select:"; fflush (stdout); cin>>key } return key;    void input () {cout<< "Please enter Block_name:";    Fflush (stdout);    cin>>block_name;    cout<< "Please enter passwd:";    Fflush (stdout); CIN&GT;&GT;PASSWD;}    void Register_user () {cout<< "Please enter name:";    Fflush (stdout);    cin>>name;    cout<< "Please enter ID:";    Fflush (stdout); Cin>>id;}    void Bank_function () {Bank ATM;    size_t money=0;    BOOL Flag=false;      while (1) {char res=output ();              Switch (res) {case ' 1 ': Register_user (); Flag=atm.              Register (name,id);//user registration if (flag) cout<< "Registration Successful" "<<endl;              else cout<< "" Registration Failed "<<endl;          Break      Case ' 2 ': {int count=3;          while (1) {if (count==0) break;          Input ();          Flag=atm.log_in (BLOCK_NAME,PASSWD); if (flag) {cout<< "" landed successfully "" <<endL;break;            } else {cout<< "" Login Failed "<<endl;          cout<< "You still have * *" <<--count<< "* * * * * * * * * * * * * * * * *          }}} break;      Case ' 3 ': input ();      cout<< "Please enter money:";      Fflush (stdout); cout<< "" Please enter an integer less than 5000!      "" <<endl;      cin>>money; while (Money > cout<<) {"" Please enter an integer less than 5000!  "" <<endl;      cin>>money;      if (Atm.deposit (Block_name,passwd,money))//Deposit cout<< "Deposit Success" <<endl;              Else cout<< "Deposit failed" "<<endl;          Break      Case ' 4 ': input ();      cout<< "Please enter money:";      Fflush (stdout); cout<< "" Please enter an integer less than 5000!      "" <<endl;      cin>>money; while (Money > cout<<) {"" Please enter an integer less than 5000!  "" <<endl;      cin>>money;      } Flag=atm.withdraw_money (Block_name,passwd,money); if (flag) COUT&Lt;< "" Withdrawal success "<<endl;              else cout<< "" Withdrawal not successful "<<endl;          Break      Case ' 5 ': input ();      Flag=atm.check (BLOCK_NAME,PASSWD);//Use the card number to find if (flag) cout<< "Query succeeded" "<<endl;              else cout<< "Query failed" <<endl;          Break      Case ' 6 ': input ();      FLAG=ATM.CHANGE_PASSWD (BLOCK_NAME,PASSWD);      if (flag) cout<< "" Change Password succeeded "<<endl;              else cout<< "" Change Password Failed "<<endl;          Break      Case ' 7 ': input ();      Flag=atm.logout (BLOCK_NAME,PASSWD);      if (flag) cout<< "" Logout Successful "<<endl;              else cout<< "" Logoff failed "<<endl;          Break      Case ' 8 ': cout<< "Please enter Card_number:";      Fflush (stdout);      cin>>block_name;      cout<< "Please enter money:";      Fflush (stdout); cout<< "" Please enter an integer less than 5000!      "" <<endl;      cin>>money; Flag=atm.transfer_account (Block_name,moNey);      if (flag) cout<< "" Transfer success "<<endl;              else cout<< "" Transfer failed "<<endl;          Break              Case ' 9 ': atm.show ();          Break              Case ' 0 ': cout<< "" Welcome to ATM self-service system "<<endl;          Return              Default:system ("clear");      Break    }}}int Main () {bank_function (); return 0;}

Makefile

des=bank.cppcer=bank$ (CER): $ (DES) g++-o [email protected] $^-g.phony:outout:mkdir OUTMV $ (CER)./outchmod 755 Table.shcp table.sh./outcd outsh table.sh.phony:cleanclean:rm-rf outchmod 644 table.sh


table.sh

#/bin/bash./out/bank






Simple implementation of Linux ATM self-defined teller machine

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.