Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   create 2 class which crate and depot (http://www.go4expert.com/forums/create-2-class-crate-depot-t20151/)

sger 20Nov2009 23:25

create 2 class which crate and depot
 
class Crate with
data members: city where it came from, arrival day (1≤arrival_day≤30), purchase price, status flag to describe the status of a crate (e.g. -1:not available, 1:available, and 0:sold).
Default constructor is used to initialize arrival day and purchase price to zero and also status to -1.
Set member function is used to assign city, arrival day and purchase price from input parameters and then it updates the status by 1.
Other member functions should be defined when required.
Create class Depot with:
data members: A pointer to Crate objects with an array size 100. There are two static members that are publicly accessible. One of them is to hold total profit that can be calculated from
where sell price is 10 percent of purchase price. Other static data member is to represent total number of entry (entry≤100).
Constructor dynamically creates an array of Crate objects (array size is assumed to be 100 so that there can be at most 100 sales per each month.
arrival(entry) is a member function to set arrival day, price and city information for maximum number of entry (entry<=100) that represent the number of crates.
sell() is member function
o to find the latest crate arrived to the depot ;
o to sell it by setting its status to 0;
o to update the total profit for sales with a sell price (sell price is calculated as 10% purchase price for that crate)
display() member function is used to display the total amount of sell profit for that month.

Sample Run:
<1>: Arrival, <2>: Selling, <3>: Displaying, <4>: Quit
Enter your choice: 2
No crates to sell
<1>: Arrival, <2>: Selling, <3>: Displaying, <4>: Quit
Enter your choice: 1
Number of entry: 2
Enter arrival day: 10
purchase price: 100
city: Antalya
Enter arrival day: 15
purchase price: 200
city: Adana
<1>: Arrival, <2>: Selling, <3>: Displaying, <4>: Quit
Enter your choice: 2
Crate from Adana with arrival day 15 was sold with a price: 220
<1>: Arrival, <2>: Selling, <3>: Displaying, <4>: Quit
Enter your choice: 3
Total profit: 20
<1>: Arrival, <2>: Selling, <3>: Displaying, <4>: Quit
Enter your choice: 4
Bye

I did this program but sell() funciton is error can you help me

Code:

#include <iostream>
#include <cstdlib>
using namespace std;
// create class crete
class crate
{
  //  describe data members
      string name;
      int arrival_day;
      double price;
      int flag; //status
     
      public:
  //  describe default constructor
      crate()
      {
          arrival_day=0;
          price=0;
          flag=-1;
      }
     
  //  describe set member function
      void set(string a,int b,int c)
      {
            name=a;
            arrival_day=b;
            price=c;
            flag=1;
      }
};
// create class depot
class depot
{
      crate *obj[100];
      static double total_profit;
      static int number_entry;
      public:
      //creates an array of Crate objects dynamically
      depot()
      {
          *obj=new crate[100];
      }
      void arrival();
      void sell();
      void display();
     
};
int depot::number_entry=100;  // to initialize 100 to number of entry
/*double depot ::total_profit=crate.price*0.1;*/
// descibe arrival function
void depot::arrival()
{
       
        int a,b,i;
        string c;
          cout<<"Number of entry:";
          cin>>number_entry;
          for(i=0;i<number_entry;i++)
          {
                do
                {
                cout<<"Enter arrival day:";
                cin>>a;
                }while(a<1 ||a>30);
               
                cout<<"      purchase price:";
                cin>>b;
                cout<<"      city:";
                cin>>c;
             
              obj[i]->set(c,a,b); // obj of crate is pointer so we can ues icon of arrow to go to funciton       
          }
}
// descibe sell function
void depot::sell()
{
     
      int i,j;
      if(crate.flag!=1)
      {
      crate.crate();
      cout<<"No crate to sell";
      }
      else
      {
     
      for(i=0;i<number_entry;i++)
      {
        for(j=i+1;j<number_entry;j++)
        {
        if(obj[i]<obj[j])
        {
          obj[i].arrival_day=obj[j].arrival_day;
          obj[i].name=obj[j].name;
          obj[i].price=obj[j].price;                 
      }
      obj[i].flag=0;
      total_profit=(obj[i].price)*0.1;
      obj[i].price=obj[i].price+total_profit;
      }
      }
      }
      cout<<"Crate from "<<obj[0].city<<" with arrival day "<<obj[0].arrival_day<<" was sold with a price "<<obj[0].price;
                         
}
void depot::display()
{
    cout<<"Total profit:"<<total_profit;
}
int main()
{
  depot depot;
  int cho; //choice
  do{
  cout<<"<1>: Arrival, <2>: Selling, <3>: Displaying, <4>: Quit"<<endl;
  cout<<"Enter your choice:";
  cin>>cho;
  switch(cho)
  {
              case 1: depot.arrival(); break;
              case 2: depot.sell();    break;
              case 3: depot.display(); break;
              case 4: break;
                 
  }                 
 
  }while(cho!=4);
 
  system("pause");
  return(0);   
}



All times are GMT +5.5. The time now is 15:54.