Question

Requirements I have already build a hpp file for the class architecture, and your job is to imple...

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 payment system.

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);

//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_

//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_

//main.cpp

#include "user.hpp"
#include "test.hpp"

int main() {
unittest::TEST t;
t.runAllCases();
return 0;
}
//user.cpp

???

0 0
Add a comment Improve this question Transcribed image text
Answer #1


user.cpp
----

#include "user.hpp"
#include <iostream>
#include <cctype>
using namespace std;
bool alipay::User::verifyPasswordFormat(const std::string &password){
   if(password.length() >= 8 && password.length() <= 20){
       for(int i = 0; i < password.length(); i++){
           char c = password[i];
           if(!(isalpha(c) || isdigit(c)))
               return false;
       }
       return true;
   }
   else
       return false;
}
bool alipay::User::verifyUsernameFormat(const std::string &username){
   if(username.length() >= 6 && username.length() <= 20){
       for(int i = 0; i < username.length(); i++){
           char c = username[i];
           if(!(isalpha(c) || isdigit(c)))
               return false;
       }
       return true;
   }
   else
       return false;
}
bool alipay::User::verifyPhoneFormat(const std::string &phone){
   if(phone.length() == 13){
       for(int i = 0; i < phone.length(); i++){
           char c = phone[i];
           if(!isdigit(c))
               return false;
       }
       return true;
   }
   else
       return false;
}

bool alipay::User::setUsername(const char *uname){
   if(verifyUsernameFormat(uname)){
       username = uname;
       return true;
   }
   else
       return false;
}
bool alipay::User::setPassword(const char *new_password, const char *old_password){
   if(password == old_password && verifyPasswordFormat(new_password)){
       password = new_password;
       return true;
   }
   else
       return false;
}
bool alipay::User::setPhone(const char *new_phone){
   if(verifyPhoneFormat(new_phone)){
       phone = new_phone;
       return true;
   }
   else
       return false;
}
bool alipay::User::setGender(Gender::Gender gen){
   if(gen != Gender::Gender::Unknown){
       gender = gen;
       return true;
   }
   else
       return false;
}

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;
}

// if passowrd is in correct, return -1
long long int alipay::User::getMoneyAmount(const char *pass){
   if(password != pass)
       return -1;
   else
       return balance;
}
bool alipay::User::deposit(long long int amount){
   if(amount > 0){
       balance += amount;
       return true;
   }
   else
       return false;
}
bool alipay::User::withdraw(long long int amount, const char *pass){
   if(password != pass || amount <= 0 || amount > balance)
       return false;
   else{
       balance -= amount;
       return true;
   }
}


output
----
Test1
Alice1995
Pass   setting a legal username.Alice1995
12345678
Pass   setting a legal password for a blank password.12345678
8618819473230
Pass   setting a legal phone.8618819473230
0
Pass   setting a legal gender: Female.
Pass   username should be equal: (Alice1995,Alice1995).
Pass   phone should be equal: (8618819473230,8618819473230).
1000
Pass   try to deposit.
400
Pass   try to withdraw.

Test2
222@_@222
Pass   setting an illegal username.222@_@222
123
Pass   setting an illegal phone.123
110
Pass   setting an illegal password.110
Pass   setting a new password using wrong old password.

Test3
Pass   try to cheat money using negative number.
Pass   try to cheat money using loan which is not supported.

Add a comment
Know the answer?
Add Answer to:
Requirements I have already build a hpp file for the class architecture, and your job is to imple...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Requirements: Finish all the functions which have been declared inside the hpp file. Details: st...

    Requirements: Finish all the functions which have been declared inside the hpp file. Details: string toString(void) const Return a visible list using '->' to show the linked relation which is a string like: 1->2->3->4->5->NULL void insert(int position, const int& data) Add an element at the given position: example0: 1->3->4->5->NULL instert(1, 2); 1->2->3->4->5->NULL example1: NULL insert(0, 1) 1->NULL void list::erase(int position) Erase the element at the given position 1->2->3->4->5->NULL erase(0) 2->3->4->5->NULL //main.cpp #include <iostream> #include <string> #include "SimpleList.hpp" using std::cin; using...

  • Need to implement Account.cpp and AccountManager.cpp code //Account.hpp #ifndef _ACCOUNT_HPP_ #define _ACCOUNT_HPP_ #include <string> using std::string;...

    Need to implement Account.cpp and AccountManager.cpp code //Account.hpp #ifndef _ACCOUNT_HPP_ #define _ACCOUNT_HPP_ #include <string> using std::string; class Account { public: Account(); Account(string, double); void deposit(double); bool withdraw(double); string getName() const; double getBalance() const; private: string name; double balance; }; #endif ////////////////////////////////////////////// //AccountManager.hpp #ifndef _ACCOUNT_MANAGER_HPP_ #define _ACCOUNT_MANAGER_HPP_ #include "Account.hpp" #include <string> using std::string; class AccountManager { public: AccountManager(); AccountManager(const AccountManager&); //copy constructor void open(string); void close(string); void depositByName(string,double); bool withdrawByName(string,double); double getBalanceByName(string); Account getAccountByName(string); void openSuperVipAccount(Account&); void closeSuperVipAccount(); bool getBalanceOfSuperVipAccount(double&) const;...

  • Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and...

    Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and user_main.cpp. The programming language is C++. I will provide the Sample Test Driver Code as well as the codes given so far in text below. Sample Testing Driver Code: cs52::FlashDrive empty; cs52::FlashDrive drive1(10, 0, false); cs52::FlashDrive drive2(20, 0, false); drive1.plugIn(); drive1.formatDrive(); drive1.writeData(5); drive1.pullOut(); drive2.plugIn(); drive2.formatDrive(); drive2.writeData(2); drive2.pullOut(); cs52::FlashDrive combined = drive1 + drive2; // std::cout << "this drive's filled to " << combined.getUsed( )...

  • 10.18 LAB: Plant information (vector) Given a base Plant class and a derived Flower class, complete...

    10.18 LAB: Plant information (vector) Given a base Plant class and a derived Flower class, complete main() to create a vector called myGarden. The vector should be able to store objects that belong to the Plant class or the Flower class. Create a function called PrintVector(), that uses the PrintInfo() functions defined in the respective classes and prints each element in myGarden. The program should read plants or flowers from input (ending with -1), adding each Plant or Flower to...

  • I have a queue and stack class program that deals with a palindrome, I need someone...

    I have a queue and stack class program that deals with a palindrome, I need someone to help to put in templates then rerun the code. I'd greatly appreciate it. It's in C++. Here is my program: #include<iostream> #include<list> #include<iterator> #include<string> using namespace std; class Queue { public: list <char> queue; Queue() { list <char> queue; } void Push(char item) { queue.push_back(item); } char pop() { char first = queue.front(); queue.pop_front(); return first; } bool is_empty() { if(queue.empty()) { return...

  • Program is in C++, program is called airplane reservation. It is suppose to display a screen...

    Program is in C++, program is called airplane reservation. It is suppose to display a screen of seating chart in the format 1 A B C D E F through 10. I had a hard time giving the seats a letter value. It displays a correct screen but when I reserve a new seat the string seats[][] doesn't update to having a X for that seat. Also there is a file for the struct called systemUser.txt it has 4 users...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • Write a C++ Program. You have a following class as a header file (dayType.h) and main()....

    Write a C++ Program. You have a following class as a header file (dayType.h) and main(). #ifndef H_dayType #define H_dayType #include <string> using namespace std; class dayType { public:     static string weekDays[7];     void print() const;     string nextDay() const;     string prevDay() const;     void addDay(int nDays);     void setDay(string d);     string getDay() const;     dayType();     dayType(string d); private:     string weekDay; }; #endif /* // Name: Your Name // ID: Your ID */ #include <iostream>...

  • For the LinkedList class, create a getter and setter for the private member 'name', constructing your...

    For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT