Introduction
You must have heard alipay(“Zhi Fu Bao” for Chinese). Now in this topic, we will do a series of practice to develop such a online payment system. This Time we are going to focus on the core of the system, the user part. If you have an account in alipay, you will have some basic knowledge about the architecture of the class, user.
Requirements
I have already build a hpp file for the class architecture, and your job is to implement the specific functions.
In order to prevent name conflicts, I have already declared a namespace for alipay. You should notice that while you are going to coding.
There will be some more details:
// username should be a combination of letters and numbers and the length should be in [6,20]
// correct: ["Alice1995", "Heart2you", "love2you", "5201314"]
// incorrect: ["222@_@222", "12306", "abc12?"]
std::string username;
// password should be a combination of letters and numbers and the length should be in [8,20]
// correct: ["12345678", "abc00000000"]
// incorrect: ["123", "21?=_=?12"]
std::string password;
// phone should be a number and the length should be fixed 13
// correct: ["8618819473230"]
// incorrect: ["110", "330"]
std::string phone;
Gender::Gender gender;
// because soil-rich use this system, so no decimal.
long long int balance;
// auxiliary function for format checking
inline bool verifyPasswordFormat(const std::string & password);
inline bool verifyUsernameFormat(const std::string & username);
inline bool verifyPhoneFormat(const std::string & phone);
// Because of the mistake of the architect of alibaba,
// all the parameters of these function use c strings(char *).
// you are smart, the transformation is not a problem
// if the input is a valid username, set it and return true
bool setUsername(const char * username);
// You should confirm the old password and the new_password's format
bool setPassword(const char * new_password, const char * old_password);
bool setPhone(const char * new_phone);
// You can not set the Gender to unknown again
bool setGender(Gender::Gender gender);
std::string getUsername(void);
std::string getPhone(void);
alipay::Gender::Gender getGender(void);
long long int getMoneyAmount(const char * password);
bool deposit(long long int amount);
// password check is needed when withdraw
bool withdraw(long long int amount, const char * password);
Deep Thinking and Discuss
What’s the disadvantages of this design?
Why “using namespace std” is not a good way in c++ programming?
More
If you have any ideas or thoughts about alipay system. Just leave a message below or send me a mail.
Next we will have some practice which is more detailed with the teaching process.
Copy from 叶嘉祺
Provided Codes
main.cpp
#include "user.hpp"
#include "test.hpp"
int main() {
unittest::TEST t;
t.runAllCases();
return 0;
}
test.hpp
#ifndef TEST_H_
#define TEST_H_
#include "user.hpp"
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::cerr;
using std::string;
namespace unittest {
class TEST {
public:
// complete legal operations
void TestCase1() {
cout << "Test1" << endl;
alipay::User u;
char username[20];
cin >> username;
u.setUsername(username) ? cout << "Pass" : cout << "Fail";
cout << "\tsetting a legal username." << username << endl;
char password[20];
cin >> password;
u.setPassword(password, "") ? cout << "Pass" : cout << "Fail";
cout << "\tsetting a legal password for a blank password." << password
<< endl;
char phone[20];
cin >> phone;
u.setPhone(phone) ? cout << "Pass" : cout << "Fail";
cout << "\tsetting a legal phone." << phone << endl;
int gender;
cin >> gender;
u.setGender(gender == 0 ? alipay::Gender::Female : alipay::Gender::Male)
? cout << "Pass"
: cout << "Fail";
cout << "\tsetting a legal gender: " << (gender == 0 ? "Female." : "Male.")
<< endl;
string username_s = u.getUsername();
username_s == username ? cout << "Pass" : cout << "Fail";
cout << "\tusername should be equal: (" << username << "," << username_s
<< ")." << endl;
string phone_s = u.getPhone();
phone == phone_s ? cout << "Pass" : cout << "Fail";
cout << "\tphone should be equal: (" << phone << "," << phone_s << ")."
<< endl;
int money;
cin >> money;
u.deposit(money) ? cout << "Pass" : cout << "Fail";
cout << "\ttry to deposit." << endl;
cin >> money;
u.withdraw(money, password) ? cout << "Pass" : cout << "Fail";
cout << "\ttry to withdraw." << endl;
cout << endl;
}
// complete illegal operations
void TestCase2() {
cout << "Test2" << endl;
alipay::User u;
char wrong_username[20];
cin >> wrong_username;
!u.setUsername(wrong_username) ? cout << "Pass" : cout << "Fail";
cout << "\tsetting an illegal username." << wrong_username << endl;
char wrong_phone[20];
cin >> wrong_phone;
!u.setPhone(wrong_phone) ? cout << "Pass" : cout << "Fail";
cout << "\tsetting an illegal phone." << wrong_phone << endl;
char wrong_password[20];
cin >> wrong_password;
!u.setPassword(wrong_password, "") ? cout << "Pass" : cout << "Fail";
cout << "\tsetting an illegal password." << wrong_password << endl;
u.setPassword("20482048123", "");
!u.setPassword("123", "123") ? cout << "Pass" : cout << "Fail";
cout << "\tsetting a new password using wrong old password." << endl;
cout << endl;
}
// complete illegal operations2
void TestCase3() {
cout << "Test3" << endl;
alipay::User u;
u.setPassword("123456789", "");
!u.withdraw(-1000, "123456789") ? cout << "Pass" : cout << "Fail";
cout << "\ttry to cheat money using negative number." << endl;
u.deposit(1000);
!u.withdraw(10000, "123456789") ? cout << "Pass" : cout << "Fail";
cout << "\ttry to cheat money using loan which is not supported." << endl;
cout << endl;
}
void runAllCases() {
TestCase1();
TestCase2();
TestCase3();
}
};
}
#endif // TEST_H_
user.hpp
#include <string>
#ifndef USER_H_
#define USER_H_
namespace alipay {
namespace Gender {
enum Gender { Female = 0, Male = 1, Unknown = 2 };
}
class User {
std::string username;
std::string password;
std::string phone;
Gender::Gender gender;
long long int balance;
inline bool verifyPasswordFormat(const std::string &password);
inline bool verifyUsernameFormat(const std::string &username);
inline bool verifyPhoneFormat(const std::string &phone);
public:
User() {
this->gender = Gender::Unknown;
this->balance = 0;
}
bool setUsername(const char *username);
bool setPassword(const char *new_password, const char *old_password);
bool setPhone(const char *new_phone);
bool setGender(Gender::Gender gender);
std::string getUsername(void);
std::string getPhone(void);
alipay::Gender::Gender getGender(void);
// if passowrd is in correct, return -1
long long int getMoneyAmount(const char *password);
bool deposit(long long int amount);
bool withdraw(long long int amount, const char *password);
};
}
#endif // USER_H_
Submission
user.cpp
#include<string.h>
#include<iostream>
#include<string>
#include"user.hpp"
#include"test.hpp"
using namespace std;
using namespace alipay;
bool alipay::User::setUsername(const char * user){
int len=strlen(user);
if(len<6||len>20)
return false;
for(int i=0;i<len;i++){
if((user[i]>='a'&&user[i]<='z')||(user[i]>='A'&&user[i]<='Z')||(user[i]>='0'&&user[i]<='9'));
else
return false;
}
username=user;
return true;
}
bool alipay::User::setPassword(const char *new_password, const char *old_password){
string temp=old_password;
if(password!=temp)
return false;
int len=strlen(new_password);
if(len<8||len>20)
return false;
for(int i=0;i<len;i++){
if(new_password[i]<48||(new_password[i]>57&&new_password[i]<65)||(new_password[i]>90&&new_password[i]<97)||new_password[i]>122)
return false;
}
password=new_password;
return true;
}
bool alipay::User::setPhone(const char * new_phone){
if(strlen(new_phone)!=13)
return false;
phone=new_phone;
return true;
}
bool alipay::User::setGender(Gender::Gender gende){
if(gende==Gender::Unknown)
return false;
gender=gende;
return true;
}
std::string alipay::User::getUsername(void){
return username;
}
std::string alipay::User::getPhone(void){
return phone;
}
alipay::Gender::Gender alipay::User::getGender(void){
return gender;
}
long long int alipay::User::getMoneyAmount(const char *passwor){
string temp=passwor;
if(password!=temp)
return -1;
return balance;
}
bool alipay::User::deposit(long long int amount){
if(amount<=0)
return false;
balance+=amount;
return true;
}
bool alipay::User::withdraw(long long int amount, const char *passwor){
string temp=passwor;
if(password!=temp||balance<amount||amount<0)
return false;
balance-=amount;
return true;
}