A command-line client for interacting with "book" and "member" servers via XMLRPC calls. Servers were created by other students.

MemberType.cpp 3.3KB

    /* Member First Name = "first_name" Member Last Name = "last_name" Member ID = "id" Member Number of Books = "books" Member Amount Spent = "spent" */ #include <math.h> #include "MemberType.h" using namespace std; MemberType::MemberType() { // default values _id = _firstName = _lastName = ""; _booksBought = 0;; _moneySpent = 0.0; } /// XMLRPC-C methods const xmlrpc_c::value_struct MemberType::ToXML() { using namespace xmlrpc_c; // Every 'value' is actually an 'xmlrpc_c::value' due to namespace! map<string, value> mid; // add each private data member pair<string, value> id = pair<string, value>("id", value_string( ID() )); mid.insert(id); pair<string, value> fname = pair<string, value>("first_name", value_string( FirstName() )); mid.insert(fname); pair<string, value> lname = pair<string, value>("last_name", value_string( LastName() )); mid.insert(lname); pair<string, value> books = pair<string, value>("books", value_int( BooksBought() )); mid.insert(books); pair<string, value> spent = pair<string, value>("spent", value_double( MoneySpent() )); mid.insert(spent); value_struct structure(mid); // return the constructed map that has been converted to a struct return structure; } void MemberType::FromXML( const xmlrpc_c::value_struct& params) { // prep this object to be filled _id = _firstName = _lastName = ""; _booksBought = 0;; _moneySpent = 0.0; using namespace xmlrpc_c; // get the values of the parameters in the list // assign them to the private data members map<string, value> mappy = params; value val; // ID value id = mappy["id"]; ID( (value_string)id ); // First Name value fname = mappy["first_name"]; FirstName( (value_string)fname ); // Last Name value lname = mappy["last_name"]; LastName( (value_string)lname ); // Books Bought value books = mappy["books"]; _booksBought = (value_int)books; // Money Spent value spent = mappy["spent"]; _moneySpent = (value_double)spent; } /// Private Data Member Methods std::string MemberType::ID() { return _id; } void MemberType::ID(std::string id) { _id = id; } std::string MemberType::FirstName() { return _firstName; } void MemberType::FirstName(std::string first) { _firstName = first; } std::string MemberType::LastName() { return _lastName; } void MemberType::LastName(std::string last) { _lastName = last; } int MemberType::BooksBought() { return _booksBought; } void MemberType::BooksBought(int books) { _booksBought = books; } float MemberType::MoneySpent() { return _moneySpent; } void MemberType::MoneySpent(float money) { _moneySpent = money; } /// Specialized Bookstore Methods void MemberType::BuyBook(float price) { // is the price greater than zero? // if its not a proper price, return // Note: $0.00 is a valid price.. for a free book. Thus it is allowed. if(price < 0.0) return; _booksBought++; _moneySpent += price; } bool MemberType::ReadyForDiscount() { return (_booksBought >= MEMBERTYPE_DISCOUNT_BOOK_NUM); } float MemberType::DiscountAmount() { if( this->ReadyForDiscount() ) { // return 15% of what the member spent return floor( .15 * _moneySpent ); } return 0.0; } bool MemberType::DiscountReset() { if( this->ReadyForDiscount() ) { _booksBought -= MEMBERTYPE_DISCOUNT_BOOK_NUM; _moneySpent = 0.0; return true; } return false; }