Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

C++ Order and Order item

Hi, I have a question on keeping track of nodes and the sums in each
Basic have
Class Order{
String CustName;
Float TotalSum;
int CountNodes;
OrderItem * first;
};
Class OrderItem{
String Description;
int quantity;
float unitprice;
OrderItem * next;
}
My Class Order is basically my header file per se. My invoice header. It has info of each ItemOrder Node.
How do I keep track of the count of nodes for each Order?
Do I add
int OrderNodeCount; //in Class Order
how can I count the OrderItem nodes that are connected to this first?

And then if I mulitpy quantity*unitprice=sum // of OrderItem but I need to sum all of thesefor Class Order to have total Sum

I tried Static but not right.
Can someone point me in the right direction
0
MarcosInfo
Asked:
MarcosInfo
  • 5
  • 3
1 Solution
 
nayernaguibCommented:
You can add a method AddOrderItem to Order class to do the following:

1. Increment the CountNodes value by 1,
2. Update the TotalSum with the sum of the new item, and
3. Append the new item to the list pointed to by first.

You can also add a similar method to remove an item from the list.

________________

  Nayer Naguib
0
 
MarcosInfoAuthor Commented:
I understand that in general looking for a bit more specific answer.
0
 
nayernaguibCommented:
You can add the following method to class Order:

void AddOrderItem(OrderItem ord) {
   CountNodes++;
   TotalSum+=ord.GetSum();
   first->AddItem(ord);
   }

and the following methods to class OrderItem:

int GetSum() {
  return quantity*unitprice;
  }

void AddItem(OrderItem ord) {
  OrderItem *tmp=this;
  while (tmp->next)
    tmp=tmp->next;
  tmp->next=⩝
  }
};

________________

  Nayer Naguib
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
nayernaguibCommented:
Sorry, the last line "};" should not be included!

________________

  Nayer Naguib
0
 
MarcosInfoAuthor Commented:
thanks.
Question:
class OrderItem {

private:
      string description;
      int quantity;
      float unitprice;
.....};
if the OrderItems are Private can the method from Order gain access?
0
 
nayernaguibCommented:
You can keep all of the class attributes private, but you still have to declare the method GetSum as public so that you can call it from an Order object.

In general, any attribute or method of an object A declared as private cannot be accessed from within another object B, even when object A itself is an attribute of object B as in the case above.

_______________

  Nayer Naguib
0
 
MarcosInfoAuthor Commented:
Public:
int GetSum() {
  return quantity*unitprice;
  }

this means Class Order can use this function and thus grab the private data members of Class OrderItem, correct?>
and this can be store back into TotalSum of Class Order?
This of course is keeping track  while Adding an OrderItem.
If so we would have to then subtract when deleted? correct?

If we only have to give the count or total sum as separate functions
then we can use a loop at the time called?
int get_item_count()
{
there is not passing of Ord?
so
via pointer of first I can do while and count?
OrderItem *tmp;
temp=first;
while (tmp->next)
 CountNodes++;
tmp=tmp->next;
}
0
 
nayernaguibCommented:
>this means Class Order can use this function and thus grab the private data members of Class OrderItem, correct?>
>and this can be store back into TotalSum of Class Order?

Exactly!

>If so we would have to then subtract when deleted? correct?

Yes.

>If we only have to give the count or total sum as separate functions
>then we can use a loop at the time called?

Yes, but your code needs little modification:

int get_item_count()
  {
  OrderItem *tmp;
  temp=first;
  CountNodes=0;      //to clear any previous value stored
  while (tmp)           //to count all nodes, including the first one
    {
    CountNodes++;    //the curly braces are *very* important. otherwise you will get into an infinite loop
    tmp=tmp->next;  //because only the first line will be considered as the loop body
    }
  }

________________

  Nayer Naguib
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now