Home > Development > Classic Sample: Customer -> Order -> OrderLine in C++

Classic Sample: Customer -> Order -> OrderLine in C++


Hi all,
I have been without internet for a week so no posts. I want some feed back on whether this is good c++ design. There is a little bit of value semantics in places and am keen for some comments.

Blair…

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iterator>

using namespace std;

class Address{
private:
    string _street;
    string _postcode;
    string _suburb;
    string _state;
    string _country;
public:
    Address();
    Address(string street, string postcode, string suburb, string state, string country);
    void ChangeAddress(string street, string postcode, string suburb, string state, string country);
    string ToString() const;
};

Address::Address(){
}

Address::Address(string street, string postcode, string suburb, string state, string country)
    : _street(street), _postcode(postcode), _suburb(suburb), _state(state), _country(country)
{
}

void Address::ChangeAddress(string street, string postcode, string suburb, string state, string country){
    this->_street = street;
    this->_postcode = postcode;
    this->_suburb = suburb;
    this->_state = state;
    this->_country = country;
}

string Address::ToString() const{
    return "Street: " + _street + " PostalCode: " + _postcode + " Suburb: " + _suburb + " State: " + _state + " Country: " + _country;
}

class Customer{
private:
    int _id;
    string _name;
    Address _address;
public:
    Customer();
    Customer(int id, string name, string street, string postcode, string suburb, string state, string country);
    Customer(const Customer& customer);
    void ChangeAddress(string street, string postcode, string suburb, string state, string country);
    Address GetCurrentAddress() const;
    string GetName() const;
    int GetId() const;
    void Print() const;
};

Customer::Customer(){
}

Customer::Customer(int id, string name, string street, string postcode, string suburb, string state, string country)
    : _id(id), _name(name), _address(Address(street,postcode,suburb,state,country))
{
}

Customer::Customer(const Customer &customer){
    this->_id = customer.GetId();
    this->_name = customer.GetName();
}

void Customer::ChangeAddress(string street, string postcode, string suburb, string state, string country){
    this->_address = Address(street,postcode,suburb,state,country);
}

Address Customer::GetCurrentAddress() const{
    return this->_address;
}

string Customer::GetName() const{
    return this->_name;
}

int Customer::GetId() const{
    return this->_id;
}

void Customer::Print()  const{
    cout << "Id: " << this->_id << " Name: " << _address.ToString() << endl;
}

class OrderLine{
private:
    int _id;
    double _unitcost;
    double _unitprice;
    int _quantity;
    string _productcode;
    string _productdescription;
public:
    OrderLine();
    OrderLine(int id, double unitcost, double unitprice, int quantity, string productcode, string productdrescription);
    void ChangeQuantity(int quantity);
    double GetLineMargin() const;
    double GetLineCost() const;
    double GetLinePrice() const;
};

OrderLine::OrderLine(){
}

OrderLine::OrderLine(int id, double unitcost, double unitprice, int quantity, string productcode, string productdescription)
    : _id(id), _unitcost(unitcost), _unitprice(unitprice), _quantity(quantity), _productcode(productcode), _productdescription(productdescription){
}

void OrderLine::ChangeQuantity(int quantity){
    this->_quantity = quantity;
}

double OrderLine::GetLineCost() const{
    return this->_quantity * this->_unitcost;
}

double OrderLine::GetLinePrice() const{
    return this->_quantity * this->_unitprice;
}

double OrderLine::GetLineMargin() const{
    return this->GetLinePrice() - this->GetLineCost();
}

class Order{
private:
    int _id;
    string _suppliername;
    Customer _customer;
    vector<OrderLine> _orderLines;
public:
    Order();
    Order(int id, string suppliername);
    Order(int id, string suppliername, const Customer& customer);
    int GetId() const;
    const Customer& GetCustomer() const;
    string GetSupplierName() const;
    void ChangeCustomer(const Customer& customer);
    void ChangeSupplier(const string& suppliername);
    void AddLine(const OrderLine& line);
    double GetTotalMargin() const;
    int LineCount() const;
};

Order::Order(){

}

Order::Order(int id, string suppliername)
    : _id(id), _suppliername(suppliername){

}

Order::Order(int id, string suppliername, const Customer &customer)
        : _id(id), _suppliername(suppliername), _customer(customer){

}

int Order::GetId() const{
    return this->_id;
}

string Order::GetSupplierName() const{
    return this->_suppliername;
}

const Customer& Order::GetCustomer() const{
    return this->_customer;
}

void Order::ChangeCustomer(const Customer &customer){
    this->_customer = customer;
}

void Order::ChangeSupplier(const string &suppliername){
    this->_suppliername = suppliername;
}

void Order::AddLine(const OrderLine &line){
    this->_orderLines.push_back(line);
}

double Order::GetTotalMargin() const{
    double total = 0;
    for(vector<OrderLine>::const_iterator i = this->_orderLines.begin(); i != this->_orderLines.end(); i++){
        total += (*i).GetLineMargin();
    }
    return total;
}

int Order::LineCount() const{
    return this->_orderLines.size();
}


int main(int argc, char *argv[])
{

    Customer* customer = new Customer(1,"Blair","Unit 26 / 4 Comer St","6152","Como","WA","Australia");
    customer->Print();

    Address address = customer->GetCurrentAddress();
    address.ChangeAddress("27 Test Street","6029","Perth","NSW","Australia");

    customer->Print();

    customer->ChangeAddress("27 Test Street","6029","Perth","NSW","Australia");
    customer->Print();

    Order order = Order(1,"Bunnings",*customer);
    OrderLine l1 = OrderLine(1,22,33,2,"XYZ","Test");
    OrderLine l2 = OrderLine(1,33,55,3,"XYZ","Test");
    OrderLine l3 = OrderLine(1,44,66,4,"XYZ","Test");

    order.AddLine(l1);
    order.AddLine(l2);
    order.AddLine(l3);

    cout << "Margin: " << order.GetTotalMargin() << " Lines: " << order.LineCount() << endl;

    delete customer;
    return 0;
}

Advertisements
Categories: Development Tags: , ,
  1. June 9, 2010 at 1:19 am

    Yeah, it’s good, very useful, thanks 🙂

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: