How do I put this code into custom classes?

This is a discussion on How do I put this code into custom classes? within the C++ Programming forums, part of the General Programming Boards category; I spent an entire week trying to figure out how to do this correctly and I finally got it, only ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    8

    Question How do I put this code into custom classes?

    I spent an entire week trying to figure out how to do this correctly and I finally got it, only to find that I have to put it into custom classes. Then I spent the entire past week trying to do it but I feel like I am doing everything completely wrong and I just get lists and lists errors.

    BTW I am using Dev C++

    Here is my code without custom classes.

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int itemNumber;
        string itemName;
        float itemPrice;
        string name;
        string adress;
        int telephone;
        int age;
        string employeeId;
        string department;
        int numberOfEmployee;
        int registerNumber;
        int shift;
        int number;
        float saleAmount;
       
        cout<<"1. Employee"<<endl<<"2. Inventory"<<endl<<"3. Sale Interface"<<endl;//MENU
        cout<<"Please enter 1, 2 or 3: "<<endl;
        cin>>number;
       
        if (number == 1) //start employee function
    {
        string employeeType;
        cout<< "This program is for cashiers or managers"<<endl;
        cout<<"Please enter the following information:"<<endl<<"Name: ";
        cin>>name;
        cout<<endl;
        cout<<"Adress (with no spaces): ";
        cin>>adress;
        cout<<endl;
        cout<<"Telephone: ";
        cin>>telephone;
        cout<<endl;
        cout<<"Age: ";
        cin>>age;
        cout<<endl;
        cout<<"Employee Id: ";
        cin>>employeeId;
        cout<<endl;
        cout<<"Are you a cashier or a manager?"<<endl;
        cin>>employeeType;
       
        if (employeeType == "cashier")
        {
        cout<<"Register Number: ";
        cin>>registerNumber;
        cout<<endl;
        cout<<"Shift: ";
        cin>>shift;
        cout<<endl;}
       
        else if (employeeType == "Cashier")
        {
        cout<<"Register Number: ";
        cin>>registerNumber;
        cout<<endl;
        cout<<"Shift: ";
        cin>>shift;
        cout<<endl;
    }
          else if (employeeType == "Manager")
        {
            cout<<"Department: ";
            cin>>department;
            cout<<endl;
            cout<<"Number Of Employees: ";
            cin>>numberOfEmployee;
            cout<<endl;
        }          
                                
        else if (employeeType == "manager")
        {
            cout<<"Department: ";
            cin>>department;
            cout<<endl;
            cout<<"Number Of Employees: ";
            cin>>numberOfEmployee;
            cout<<endl;
        }
        else
        {
            cout<<"You didn't enter cashier or manager"<<endl;
        }
    }//end of employee function
    
    else if (number == 2)//Inventory function start
    {
         cout<<"This is where you enter inventory information to be saved."<<endl;
         cout<<"Enter the item number: ";
         cin>>itemNumber;
         cout<<endl;
         cout<<"Enter the item name: ";
         cin>>itemName;
         cout<<endl;
         cout<<"Enter the item price: ";
         cin>>itemPrice;
         cout<<endl;
        
     }//inventory function end
     
     else if(number == 3)//sale interface start
     {
          cout<<"Please enter your total sale amount: ";
          cin>>saleAmount;
          cout<<endl;
          saleAmount=(saleAmount*.06)+saleAmount;
          cout<<"Your total with tax is: "<<saleAmount<<endl;
      } //sale interface ends
     else
     {
         cout<<"INVALID ENTRY"<<endl; 
     }
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }

    Please help me! I'm going crazy!

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    First of all this
    Code:
    if (employeeType == "cashier")
    {
        cout<<"Register Number: ";
        cin>>registerNumber;
        cout<<endl;
        cout<<"Shift: ";
        cin>>shift;
        cout<<endl;}
       
    else if (employeeType == "Cashier")
    {
        cout<<"Register Number: ";
        cin>>registerNumber;
        cout<<endl;
        cout<<"Shift: ";
        cin>>shift;
        cout<<endl;
    }
    Should be done like this, saving lots of line:
    Code:
    if (employeeType == "Cashier" || employeeType == "cashier")
    {
        cout<<"Register Number: ";
        cin>>registerNumber;
        cout<<endl;
        cout<<"Shift: ";
        cin>>shift;
        cout<<endl;
    }
    As for the actual question, what kind of class do you want? You want more than one? One class can be

    Code:
    class myClass
    {
        int itemNumber;
        ...
        float saleAmount;
    public:
       void employee();
       void inventory();
       void salesInterface();
    };
    so you have all your variables and your three pseudo-functions. Then you create one object in main and do what you want by calling the three functions. Or sth like that.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    8

    Question

    Quote Originally Posted by C_ntua View Post
    First of all this
    Code:
    if (employeeType == "cashier")
    {
        cout<<"Register Number: ";
        cin>>registerNumber;
        cout<<endl;
        cout<<"Shift: ";
        cin>>shift;
        cout<<endl;}
       
    else if (employeeType == "Cashier")
    {
        cout<<"Register Number: ";
        cin>>registerNumber;
        cout<<endl;
        cout<<"Shift: ";
        cin>>shift;
        cout<<endl;
    }
    Should be done like this, saving lots of line:
    Code:
    if (employeeType == "Cashier" || employeeType == "cashier")
    {
        cout<<"Register Number: ";
        cin>>registerNumber;
        cout<<endl;
        cout<<"Shift: ";
        cin>>shift;
        cout<<endl;
    }
    As for the actual question, what kind of class do you want? You want more than one? One class can be

    Code:
    class myClass
    {
        int itemNumber;
        ...
        float saleAmount;
    public:
       void employee();
       void inventory();
       void salesInterface();
    };
    so you have all your variables and your three pseudo-functions. Then you create one object in main and do what you want by calling the three functions. Or sth like that.

    Awesome! Thank you so much for responding so quickly. I actually tried the || on those to save lines earlier and it wouldn't work for me because I forgot to type employeeType == for both of them and I decided to just fix it later. So you actually helped one of my other problems without me even asking! =D

    Yes, for the most part I am pretty sure I want more than one, I am just not really grasping what goes where etc. What would be the most "professional" way to do it? The assignment just calls to implement classes in general.

    Thanks again!

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    well you would just have to look at your variables that you have so far and decide if they would all describe the same type of object = create one class, or if they break down into two or more distinct groups then this would mean making a couple of classes. I think you should work it as one 'employee' class for now for practice purposes, after all, managers are employees too.
    But after practice you can see there is a case for inheritance, the hierarchy is there, manager + cashier are both employees , so base class is employee, managers have all the same things as employees, they still need to be paid and register leave days etc, but they have other things like staff list and department etc in your example. the same goes for cashiers which have thier own properties as well as the ones for employee class.
    Last edited by rogster001; 12-07-2009 at 03:08 AM.

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425

    .

    But i say a 'case' for inheritance, check some recent posts most noticeably bubba, dont make extra classes just for hell of it and you can 'compose' your classes to make an elegant solution instead of inheritance for sake of it, i like that talk...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  2. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 11:51 AM
  5. help on some vector of custom class code
    By cozman in forum C++ Programming
    Replies: 1
    Last Post: 08-09-2001, 11:55 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21