hey!
i'm writing a program that writes allows you to manipulate employees (50 of them) in, at most, 20 companies. I plan to later use an array because at this point I have no better solution.
Well I just wanted anyone to look at my code and tell me if I'm on the right path and is there anyway to make it better.
NOTE: no i'm not asking for help on my homework!!
EDIT: removed for source ... current version below (work in progress)
Any constructive suggestions and comments will be very helpful.
I'm making sure I'm heading in the right direction and my code is clear enough (that way I won't have to put that many comments ).
I do have one question though. If I have a list (array) and I ask the user to enter a employee and/or company name so I can search the list for that person, would it be better to do that search within the class functions or write another function (which will be main () ) that does that for me?
note: this is definitely not a complete product. just preliminary.
[edited by - Alpha_ProgDes on October 20, 2002 10:59:34 AM]
[edited by - Alpha_ProgDes on October 20, 2002 11:04:39 AM]
[edited by - Alpha_ProgDes on October 20, 2002 12:42:03 PM]
[edited by - Alpha_ProgDes on October 20, 2002 12:46:59 PM]
this is the revised version (sorry no comments). right now while running the debugger one of my variable (EmpName) is not getting on the stack. i have no idea why. so if you know, let me know. otherwise i'll be tackling this now.
and thanks Arlid Fines.
//employee h.
#ifndef ASS_2_H
#define ASS_2_H
#include <string>
class _employee
{
public:
_employee ();
_employee (std::string name);
void setEmployee (std::string name);
int calculateSalary (enum EmployeeStatus);
bool searchName(std::string name);
void printEmployeeNames();
private:
std::string name;
int rank;
};
#endif
//employee cpp.
#include <iostream>
#include <string>
#include "Ass_2.h"
//#ifndef EMPLOYEESTATUS
//#define EMPLOYEESTATUS
extern enum EmployeeStatus {PROMOTE, UNEMPLOYED, DEMOTE} status;
//#endif
_employee::_employee ()
{
name = "";
rank = 1;
}
_employee::_employee (std::string e_name)
{
name = e_name;
rank = 1;
}
void _employee::setEmployee (std::string setEmpName)
{
name = setEmpName;
rank = 1;
}
int _employee::calculateSalary (EmployeeStatus status)
{
const int PAYRATE = 1000;
static int employeeSalary = 0;
if (status == PROMOTE) {
rank += 1;
employeeSalary = rank * PAYRATE;
}
else if (status == DEMOTE) {
rank -= 1;
employeeSalary = rank * PAYRATE;
}
else {
rank = 0;
employeeSalary += 50;
}
return employeeSalary;
}
bool _employee::searchName(std::string employeeName)
{
if (name == employeeName)
return true;
else
return false;
}
void _employee::printEmployeeNames()
{
std::cout << name << std::endl;
}
//company h.
#ifndef COMPANY_H
#define COMPANY_H
#include <string>
#include "Ass_2.h"
const int MAX = 50;
class _company
{
public:
_company::_company();
_company::_company(std::string empName);
void setCompanyName(std::string compName);
bool isCompanyFull();
bool isCompanyEmpty();
void join_Company (std::string empName);
void promote_Employee (std::string empName);
void demote_Employee (std::string empName);
void change_Employee (std::string empName, std::string compName, _company& anotherCompany);
void Employee_quits (std::string empName, _company& unemployed);
void printCompanyNames ();
private:
int total_employee;
_employee Employee[MAX];
std::string name;
};
#endif
//company cpp
#include <iostream>
#include <string>
#include "Ass_2.h"
#include "company.h"
enum EmployeeStatus {PROMOTE, UNEMPLOYED, DEMOTE} status;
_company::_company()
{
name = "";
total_employee = 0;
}
_company::_company (std::string compName)
{
name = compName;
total_employee = 0;
}
void _company::setCompanyName (std::string compName)
{
name = compName;
total_employee = 0;
}
bool _company::isCompanyFull()
{
if (total_employee == MAX)
return true;
else
return false;
}
bool _company::isCompanyEmpty()
{
if (total_employee == 0)
return true;
else
return false;
}
void _company::join_Company(std::string empName)
{
if (isCompanyFull() == true)
std::cout << "We are not hiring. Find another job!" << std::endl;
else if (isCompanyEmpty() == true)
Employee[0].setEmployee(empName);
else {
for (int empIndex = total_employee - 1; empIndex < 0; empIndex--){
Employee[empIndex + 1] = Employee[empIndex];
}
Employee[0].setEmployee(empName);
}
total_employee++;
}
void _company::promote_Employee(std::string empName)
{
int empIndex = 0;
for (empIndex = total_employee - 2; empIndex <= 0; empIndex--)
Employee[empIndex].searchName(empName);
status = PROMOTE;
Employee[empIndex].calculateSalary(status);
_employee temp = Employee[empIndex];
Employee[empIndex] = Employee[empIndex + 1];
Employee[empIndex + 1] = temp;
}
void _company::demote_Employee(std::string empName)
{
int empIndex = 0;
for (empIndex = total_employee - 1; empIndex < 0; empIndex--)
Employee[empIndex].searchName(empName);
status = DEMOTE;
Employee[empIndex].calculateSalary(status);
_employee temp = Employee[empIndex];
Employee[empIndex] = Employee[empIndex - 1];
Employee[empIndex - 1] = temp;
}
void _company::change_Employee(std::string empName, std::string compName, _company& anotherCompany)
{
int empIndex = 0;
int index = 0;
for (empIndex = total_employee - 1; empIndex < 0; empIndex--)
{
Employee[empIndex].searchName(empName);
if (Employee[empIndex].searchName(empName) == true)
break;
}
for (index = empIndex - 1; index <= 0; index--)
Employee[index] = Employee[index - 1];
anotherCompany.name = compName;
anotherCompany.join_Company(empName);
}
void _company::Employee_quits(std::string empName, _company& unemployed)
{
int empIndex = 0, index = 0;
unemployed.join_Company(empName);
for (empIndex = total_employee - 1; empIndex <= 0; empIndex--){
Employee[empIndex].searchName(empName);
if (Employee[empIndex].searchName(empName) == true)
break;
}
status = UNEMPLOYED;
Employee[empIndex].calculateSalary(status);
for (index = empIndex - 1; index <= 0; index--)
Employee[index] = Employee[index - 1];
}
void _company::printCompanyNames()
{
std::cout << name << std::endl;
for (int i = 0; i < total_employee; i++)
Employee[i].printEmployeeNames();
}
here's the main driver:
#include <iostream>
#include <string>
#include "Ass_2.h"
#include "company.h"
using namespace std;
void CompanyListMenu(string[]);
int total_company = 0;
int main ()
{
_company Company[20];
_company UnEmployed;
string CompanyNames[20];
string EmpName, CompName;
char choice;
int CompID = 0;
cout << "Please enter number of companies be listed (up to 20). ";
cin >> total_company;
while (total_company < 0 || total_company > 20){
cout << "Please enter a number that is from 1 to 20.";
cin >> total_company;
}
cout << "Enter the name of the company at each of the prompts." << endl;
for (int i = 0; i < total_company; i++){
cout << ">> ";
cin >> CompName;
CompanyNames[i] = CompName;
Company[i].setCompanyName(CompName);
}
cout << "Please choose whether options: " << endl;
cout << "(j)oin, (p)romote, (d)emote, (c)hange, (q)uit, (o)utput, (e)nd" << endl;
cin >> choice;
while (choice != 'e'){
CompanyListMenu(CompanyNames);
switch (choice){
case 'j': {
cout << "Enter company ID." << endl;
cin >> CompID >> EmpName;
// plan to have chart here for id and company reference (will be a function)
Company[CompID].join_Company(EmpName);
break;
}
case 'p': {
cout << "Please enter employee up for promotion." << endl;
cin >> EmpName;
Company[CompID].promote_Employee(EmpName);
break;
}
case 'd': {
cout << "Please enter employee being demoted. " << endl;
cin >> EmpName;
Company[CompID].demote_Employee(EmpName);
break;
}
case 'c': {
int nCompID = 0;
cout << "Employee to be transferred is: ";
cin >> EmpName;
Company[CompID].change_Employee(EmpName, CompName, Company[nCompID]);
break;
}
case 'q': {
cout << "Termination papers have just arrived. Name please." << endl;
cin >> EmpName;
Company[CompID].Employee_quits(EmpName, UnEmployed);
cout << EmpName << "'s termination is complete!" << endl;
break;
}
case 'o': {
for (int i = CompID; i < total_company; i++)
Company[CompID].printCompanyNames();
break;
}
default: cout <<" Those options are not permitted. Please try again." << endl;
break;
}
cout << "Please choose whether options: " << endl;
cout << "(j)oin, (p)romote, (d)emote, (c)hange, (q)uit, (e)nd, (o)utput" << endl;
cin >> choice;
}
return 0;
}
void CompanyListMenu(string company_array[])
{
for (int i = 0; i < total_company; i++)
cout << i << '\t' << company_array[i] << endl;
cout << endl <<endl;
}
again thanks!
[edited by - Alpha_ProgDes on October 20, 2002 6:27:40 PM]
[edited by - Alpha_ProgDes on October 20, 2002 11:55:55 PM]