Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   urgent help in shopping program (http://www.go4expert.com/forums/urgent-help-shopping-program-t25886/)

raptor85 24May2011 18:29

urgent help in shopping program
 
Code:

#include <iostream>

using namespace std;

int validity(string ,string );

class item
{
public:
   
    string name;
    int cost;
    int sku;
   
    void displayDetails();
   
};

int ch;                             
string id;
string pass;
int matchedAccount;


void item::displayDetails()
{
    cout<<name<<endl;
    cout<<"cost:"<<cost<<endl;
    cout<<"sku:"<<sku<<endl;
}


class customerDetails
{
public:
       
    item itemDetailsObject[50];
    int itemDetailsCount;
   

    string customerName;
    string userID;
    string password;
    void viewCart();
   
}customerDetailsObject[2];                           
           
void input()
{
    cout<<"\nLogin ID :"<<endl;
    cin>>id;
   
    cout<<"\nLogin Password :"<<endl;
    cin>>pass;

}
int validity(string id,string pass)

    int correctMatch;
   
    for (int i=0; i<=1; i++)   
    {
        if (id==customerDetailsObject[i].userID && pass==customerDetailsObject[i].password)
        {
            correctMatch=i;
            return correctMatch;
        }
        else
        {
            correctMatch=-1;                                       
        }
    }
    return correctMatch;
}


void login()
{
    input();
                                                                   
    matchedAccount=validity(id, pass);                       
    if (matchedAccount == -1)
    {
        login();
    }
}


class category
{
public:

    item music[50];
    item electronic[50];
   
    int musicCount;
    int electronicsCount;
   
    void chooseCategory();
    void chooseMusicItem();
    void chooseElectronicItem();
    void displayMusicItemDetails();
    void displayElectronicItemDetails();
   
}categoryDetailsObject;                                                             

void musicAddToCart()
{
        int newPosition = customerDetailsObject[matchedAccount].itemDetailsCount;
   
        customerDetailsObject[matchedAccount].itemDetailsObject[newPosition]=categoryDetailsObject.music[ch-1];         
        customerDetailsObject[matchedAccount].itemDetailsCount++;
       
        cout<<"Item Details Count incrementer\n"<<customerDetailsObject[matchedAccount].itemDetailsCount<<endl;
}

void electronicAddToCart()
{
    int newPosition = customerDetailsObject[matchedAccount].itemDetailsCount;
   
    customerDetailsObject[matchedAccount].itemDetailsObject[newPosition]=categoryDetailsObject.electronic[ch-1];         
    customerDetailsObject[matchedAccount].itemDetailsCount++;
   
    cout<<"Item Details Count incrementer\n"<<customerDetailsObject[matchedAccount].itemDetailsCount<<endl;
}

void customerDetails::viewCart()
{
    for (int i=0; i<itemDetailsCount; i++)
    {
        cout<<"\nName - "<<customerDetailsObject[0].itemDetailsObject[i].name<<endl;
        cout<<"Rs - "<<customerDetailsObject[0].itemDetailsObject[i].cost<<endl;
        cout<<"Sku No - "<<customerDetailsObject[0].itemDetailsObject[i].sku<<endl;
    }
   
   
    int totalCost = 0;
    for (int i = 0; i < itemDetailsCount; i++)
    {
        totalCost += itemDetailsObject[i].cost;
    }
    cout<<"\nTotal Cost "<<totalCost<<endl;
   
   
    int selection;
   
    cout<<"\n1.Checkout"<<endl;
   
    cin>>selection;
    switch (selection)
    {
        case 1:
            for (int i=0; i<itemDetailsCount; i++)
            {
                itemDetailsObject[i].name=" ";
                itemDetailsObject[i].sku=0;
                itemDetailsObject[i].cost=0;
                itemDetailsCount=0;
            }
            categoryDetailsObject.chooseCategory();
            break;
     
           
        default:viewCart();
            break;
    } 
}



void category::chooseCategory()
{
    int select;
   
    cout<<"\nWelcome"<<" "<<customerDetailsObject[matchedAccount].customerName<<endl;

    cout<<"\nChoose a category\n"<<endl;
   
    cout<<"1.Music"<<endl;
    cout<<"2.Electronic"<<endl;
    cout<<"3.Viewcart"<<endl;
    cout<<"4.Logout\n"<<endl;
   
    cout<<"Enter the choice"<<endl;
   
    cin>>select;
   
    switch (select)
    {
        case 1:
            cout<<"Music"<<endl;
            this->chooseMusicItem();
            break;
           
        case 2:
            cout<<"Electronic"<<endl;
            this->chooseElectronicItem();
            break;
           
        case 3:
            cout<<"View Cart"<<endl;
            customerDetailsObject->viewCart();
           
        case 4:
            input();
            break;
           
        default:
            chooseCategory();
            break;
    }
}


void category::chooseMusicItem()
{
    cout<<"Music Items Details\n"<<endl;
   
    for (int i=0; i<musicCount; i++)
    {
        cout<<i+1<<"."<<music[i].name<<"  Rs."<<music[i].cost<<endl;
    }
    cout<<musicCount+1<<"."<<"Back"<<endl;
       
   
    cout<<"\nEnter Choice"<<endl;
    cin>>ch;
   
    if (ch<1)
    {
        chooseMusicItem();
    }
    else if (ch<=musicCount)
    {
        displayMusicItemDetails();
    }
    else if (ch==musicCount+1)
    {
        chooseCategory();       
    }
    else
    {
        chooseMusicItem();
    }
}


void category::displayMusicItemDetails()
{
   
    music[ch-1].displayDetails();
   
    int options;
    cout<<"\n1.Add to Cart"<<endl;
    cout<<"\n2.Back"<<endl;
    cout<<"\n3.MainPage"<<endl;
   
    cout<<"\nEnter Choice"<<endl;
    cin>>options;
   
    switch (options)
    {
        case 1:
            musicAddToCart();
            this->displayMusicItemDetails();
            break;
           
        case 2:
            this->chooseMusicItem();
            break;
           
        case 3:
            this->chooseCategory();
            break;
           
        default:displayMusicItemDetails();
            break;
    }
}


void category::chooseElectronicItem()
{
   
    cout<<"\nElectronic Items Details\n"<<endl;
   
    for (int i=0; i<electronicsCount; i++)
    {
        cout<<i+1<<"."<<electronic[i].name<<"  Rs."<<electronic[i].cost<<endl;
    }
   
    cout<<electronicsCount+1<<"."<<"Back"<<endl;
   
    cout<<"\nEnter Choice"<<endl;
    cin>>ch;
   
    if (ch<1)
    {
        chooseElectronicItem();
    }
    else if (ch<=electronicsCount)
    {
        displayElectronicItemDetails();
    }
    else if(ch==electronicsCount+1)
    {
        chooseCategory();
    }
    else
    {
        chooseElectronicItem();
    }
}


void category::displayElectronicItemDetails()
{
   
    electronic[ch-1].displayDetails();
   
    int options;
    cout<<"\n1.Add to Cart"<<endl;
    cout<<"\n2.Back"<<endl;
    cout<<"\n3.MainPage"<<endl;
   
    cout<<"\nEnter Choice"<<endl;
    cin>>options;
   
    switch (options)
    {
        case 1:
            electronicAddToCart();
            this->displayElectronicItemDetails();
            break;
           
        case 2:
            this->chooseElectronicItem();
            break;
           
        case 3:
            this->chooseCategory();
            break;
           
        default:
            displayElectronicItemDetails();
            break;
    }
}

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

    customerDetailsObject[0].customerName="David";
    customerDetailsObject[0].userID="dav2";
    customerDetailsObject[0].password="abc12";
    customerDetailsObject[0].itemDetailsCount=0;
   
    customerDetailsObject[1].customerName="Rahul";
    customerDetailsObject[1].userID="rah45";
    customerDetailsObject[1].password="1awe";
    customerDetailsObject[1].itemDetailsCount=0;
   

    categoryDetailsObject.musicCount = 3;
    categoryDetailsObject.electronicsCount = 4;
   
   
    categoryDetailsObject.music[0].name="The Dark Knight OST";
    categoryDetailsObject.music[0].cost=200;
    categoryDetailsObject.music[0].sku=10246;
   
 
 
/// Like this i have declared for electronics items as well //
   
    login();
   
    categoryDetailsObject.chooseCategory();



this is my code i when i execute the program and select the items for customer david the total items selected in add to cart menu and total costs will be calculates ,, but when i select the customer rahul the total items and costs would appear as 0

i could declare the number of users similarly like i have declared number of music items (categoryDetailsObject.musicCount = 3) i have declared musicCount in category class

i have used the musicCount variable in for loop in chooseMusicItemDetails .

similarly i can use customerCount variable in input function but i dont know how to do it .

how to do it for customerDetailsObject since it has 2 objects
i cant do it like customerDetailsObject[0].customerCount = 2 ;


All times are GMT +5.5. The time now is 13:02.