Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C++ object help

Status
Not open for further replies.

JeffGolben

Programmer
May 29, 2005
2
US
Hello,

I was recommended to check out this board for help, so please forgive me if I sound like a newbie.

I have written a short simple program that uses an array of 3 items. Each item has a price and a quantity. The user selects an item number and quantity, and the program adds up a total and subtracts from inventory. The core program works well, but now I want to implement objects of InventoryItem class type, and am having a very difficult time with this. So close to the end, yet so far!!! If anyone is willing to help me, I'd really appreciate it! I will post the code up if I get a reply.

Thank you,
Jeff
 

#include <iostream.h>
#include "oostring.h"
int main()
{

oostring custname;
int custid;
int itemid;
int itemcount;
int grandcount;
int avgcost;
float ordertotal;
int itemtotal;
float grandtotal;
grandtotal=0;
ordertotal=0;
itemtotal=0;
grandcount=0;
avgcost=0;

int idarray[3];
idarray[0]=1;
idarray[1]=2;
idarray[2]=3;

oostring namesarray[3];
namesarray[0]="Kitty Litter";
namesarray[1]="Cat Food";
namesarray[2]="Cat Treats";

int quanarray[3];
quanarray[0]=100;
quanarray[1]=200;
quanarray[2]=400;

float itemtarray[3];
itemtarray[0]=0;
itemtarray[1]=0;
itemtarray[2]=0;

float costarray[3];
costarray[0]=6.00;
costarray[1]=4.00;
costarray[2]=1.98;

cout<<"Pet store sale"<<endl;
cout<<"--------------"<<endl;
cout<<"Item ID:"<<idarray[0]<< " - " <<namesarray[0]<< " - " <<"Quantity in stock:"<<quanarray[0]<< " - " <<"Cost per unit"<< " ""$"<<costarray[0]<<endl;
cout<<"Item ID:"<<idarray[1]<< " - " <<namesarray[1]<< " - " <<"Quantity in stock:"<<quanarray[1]<< " - " <<"Cost per unit"<< " ""$"<<costarray[1]<<endl;
cout<<"Item ID:"<<idarray[2]<< " - " <<namesarray[2]<< " - " <<"Quantity in stock:"<<quanarray[2]<< " - " <<"Cost per unit"<< " ""$"<<costarray[2]<<endl;

cout <<"Enter name:"<<endl;
cin >> custname;
cout <<"Enter Customer ID:"<<endl;
cin >>custid;
cout <<"Please enter item#:"<<endl;
cin >>itemid;
while (custid!=0)
{
while (itemid!=0)
{
if (itemid>3)
{
cout<<"Invalid Item#"<<endl;
}else
{
cout <<namesarray[itemid -1]<<endl;
grandcount=grandcount + 1;
cout <<"How many would you like?" <<endl;
cin >>itemcount;
if (itemcount<1)
cout <<"You need to enter a quatity larger than 0"<<endl;
// if (itemcount>quanarray[itemid-1])
// cout <<"Please order less"<<endl;
itemtotal=itemtotal + itemcount;
costarray[itemid-1]=costarray[itemid-1] * itemcount;
//costarray[itemid-1]=itemtarray[itemid-1];
cout << "Item total is $" <<costarray[itemid-1]<<endl;
quanarray[itemid-1]=quanarray[itemid-1] - itemcount;
cout << " " <<endl;
}
grandtotal=grandtotal + costarray[itemid-1];
ordertotal=ordertotal + costarray[itemid-1];
float costarray[3];
costarray[0]=6.00;
costarray[1]=4.00;
costarray[2]=1.98;

cout<<"Item Availability"<<endl;
cout<<"-----------------"<<endl;
cout<<"Item ID:"<<idarray[0]<< " - " <<namesarray[0]<< " - " <<"Quantity in stock:"<<quanarray[0]<< " - " <<"Cost per unit"<< " ""$"<<costarray[0]<<endl;
cout<<"Item ID:"<<idarray[1]<< " - " <<namesarray[1]<< " - " <<"Quantity in stock:"<<quanarray[1]<< " - " <<"Cost per unit"<< " ""$"<<costarray[1]<<endl;
cout<<"Item ID:"<<idarray[2]<< " - " <<namesarray[2]<< " - " <<"Quantity in stock:"<<quanarray[2]<< " - " <<"Cost per unit"<< " ""$"<<costarray[2]<<endl;
cout<< " " <<endl;
cout <<"Enter next item#:"<<endl;
cin >>itemid;
}

cout <<"Order Summary" << endl;
cout <<"-------------" << endl;
cout<< " " <<endl;
cout<<"You ordered"<< " " <<itemtotal<<" "<<"item(s)"<<endl;
cout<<"Your order total is:"<< " ""$" <<ordertotal<<endl;

cout<< " " <<endl;
cout <<"Please enter customer ID:"<<endl;
cin >>custid;
cout<<"Pet store sale"<<endl;
cout<<"--------------"<<endl;
cout<<"Item ID:"<<idarray[0]<< " - " <<namesarray[0]<< " - " <<"Quantity in stock:"<<quanarray[0]<< " - " <<"Cost per unit"<< " ""$"<<costarray[0]<<endl;
cout<<"Item ID:"<<idarray[1]<< " - " <<namesarray[1]<< " - " <<"Quantity in stock:"<<quanarray[1]<< " - " <<"Cost per unit"<< " ""$"<<costarray[1]<<endl;
cout<<"Item ID:"<<idarray[2]<< " - " <<namesarray[2]<< " - " <<"Quantity in stock:"<<quanarray[2]<< " - " <<"Cost per unit"<< " ""$"<<costarray[2]<<endl;

cout <<"Please enter item#:"<<endl;
cin >>itemid;
itemtotal=0;
ordertotal=0;
}

cout <<"order summary"<<endl;
cout <<"-------------"<<endl;
cout <<"The total $ of all orders:"<< " " <<"$"<<grandtotal<<endl;
cout <<"The total # of customers:"<< " " <<grandcount<<endl;
avgcost=grandtotal/grandcount;
cout <<"The average cost per order:" <<" "<<"$"<<avgcost<<endl;
cout <<" " <<endl;
cout <<"Final inventory"<<endl;
cout <<"---------------"<<endl;
cout<<namesarray[0]<< " - " <<quanarray[0]<<" " <<"unit(s) remain"<<endl;
cout<<namesarray[1]<< " - " <<quanarray[1]<<" " <<"unit(s) remain"<<endl;
cout<<namesarray[2]<< " - " <<quanarray[2]<<" " <<"unit(s) remain"<<endl;



}
___________________________________

There is the original code.

Here is what I need to do:

Inventory items are kept in objects of InventoryItem class type, and the InventoryItem class contains the id, name, price and quantity as well as methods to retrieve the data: get/set methods. The objects are kepy in an array during execution of the program.

If someone can get me on the right track that would be great!!!
 
>Inventory items are kept in objects of InventoryItem class type,

Ok then we'll declare the InventoryItem class

>and the InventoryItem class contains the id, name, price and quantity

Ok, that'll be private members of the class

>as well as methods to retrieve the data: get/set methods.

Ok, then we'll add access methods

>The objects are kepy in an array during execution of the program.

We'll use std::vector for that


Some code to get you started.
Code:
#include <string>
#include <vector>

class InventoryItem
{
public:
  // Constructor - Initialize members
  InventoryItem(int id, const char* name):mId(id),mName(name),mPrice(0.0),mQuant(0) {}

  // Destructor
  virtual ~InventoryItem() {}

  // Public Commands
  void setPrice(double v) { mPrice = v; }
  void setQuantity(unsigned int v) { mQuant = v; }

  // Public Queries
  double getPrice() const { return mPrice; }
  unsigned int getQuantity() const { return mQuant; }

  const char* getName() const { return mName.c_str(); }
  int getId() const { return mId; }
private:
  // Members
  int mId;
  std::string mName;
  double mPrice;
  unsigned int mQuant;  
};

typedef std::vector<InventoryItem> Inventory;

//....

{
	Inventory invent;
	invent.push_back(InventoryItem(1,"Foo"));
	invent.push_back(InventoryItem(2,"Bar"));
	invent[0].setPrice(4.2);
	invent[0].setQuantity(42);
	invent[1].setPrice(10.5);
	// etc...
You could for example extend the constructor to
initialize all members from parameters.

I quess that the Id isn't supposed to change. Note how easy it is to prevent that - simply don't provide a setId method. That's what Object Oriented Programming is all about...

/Per

www.perfnurt.se
 
If you want to actually use the Id for looking-up/retrieveving the class you could consider using a map rather than a vector.

Remove all Id-related stuff from the class and change the typedef
Code:
#include <map>
//....
typedef std::map<int, InventoryItem> Inventory;
//....
{
    Inventory invent;
    invent.insert(Inventory::value_type(1,InventoryItem("Foo")));
    // Note: id can be any number
    invent.insert(Inventory::value_type(42,InventoryItem("Bar")));

   cout << invent[1].getName; // "Foo"
   cout << invent[42].getName; // "Bar"

   if (invent.find(12) == invent.end()) // Id 12 not found...

/Per

www.perfnurt.se
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top