HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Calculating budget for employee hierarchy

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
employeehierarchycalculatingbudgetfor

Problem

You have to write a C++ program that reads in the employee hierarchy
of a company and computes a statistic called 'salary budget' for all
employees.


For this problem, the 'salary budget' of any employee is defined as
follows:


For employees who are not managers, the salary budget is same as their
salary For managers, the salary budget is the sum of his/her salary
and the salary budgets of all employees whose manager this employee
is. You have to print a list of all employees and their salary budget,
sorted in decreasing order of salary budget.


For example, the input will be provided to you on the standard input
in this format:



Vineel Phatak, 5200000, NOBODY
Ajay Joshi, 2500000, Vineel Phatak
Abhishek Chauhan, 1200000, Ajay Joshi
Vijaya Mundada, 600000, Abhishek, Chauhan
Rajan Gawli, 7000000, Vineel Phatak
Sheila Rodrigues, 350000, Vineel Phatak



Each line contains an employee name, a salary, and the name of the
boss of that employee - all separated by commas. The special name
NOBODY for boss indicates that this employee does not have a boss (and
is hence the CEO of the company). You can assume that there is only
one CEO in the company.


For this input, your program should produce the following output:

Vineel Phatak: 16850000
Rajan Gawli: 7000000
Ajay Joshi: 4300000
Abhishek Chauhan: 1800000
Vijaya Mundada: 600000
Sheila Rodrigues: 350000


I was asked to code it. What are the problems with this code? Since the input seemed open ended, I have made some assumptions written as comments in code.

I want to know if following assumptions for input are correct or could be done better

  • all empolyee names are unique



  • a managers entry comes before any of his/her subordinates in input



`class Employee
{
private:
QString name;
QString mgrName;
int salary;
int budget;

public:
Employee()
{
name = QString();
mgrName = QString();
salary = 0;
budget =0;

Solution

Boolean Operators

When you make a class specific Boolean operator, it's usually a good idea to make them all, especially as it's not too much extra work for quite a lot of potential gain, as detailed well here(The bit I'm referring to is about half-way down the answer and is headed Comparison Operators). This is largely because people expect them to work specifically for them all if it works for one. Also, it's good practice (as in for you to get better, not..Actually, both meanings).

Data Types

Salary and Budget, I'd tend to expect to be doubles, unless your specification has directly told you otherwise Proven to be wrong on this, as you were. (This next bit is personal preference) I'd personally use std::string whenever doing anything to do with strings in c++, but each to their own I guess, I don't know enough about QString to say otherwise.

File Structure

I've assumed that you have an employee.h as well as an employee.cpp and a main.cpp (or whatever you've called them), but I noticed the lack of scope resolution operators, and thought I should point it out. It's good for easy readability to keep your code in different files.

Data Structures

I'd not heard of a multi-map before reading this, but that seems pretty neat, and a good use of it on face value, although as far as I can tell, that is duplicating a lot of values, as you also have the employees in a vector. Maybe have your multi-map have a vector of pointers to employees? That way you're not duplicating data. Either that, or just store them in the multi-map

General impression of the code

It seems okay, basically. Good indentation, decent variable names and good commenting. Having not run it I can't speak to the functionality with respect to your assignment, but it was nice to read.

Assumptions that you made

Frankly, it depends. I wouldn't rely on employee names being unique, if it were a database, you'd have a unique key attached to the employee, and the manager coming before their subordinates depends on if you are defining the format of the file you're parsing through. If you are defining it, then your word is law and if it breaks, the person making the file messed up. Otherwise, you'll have to fall in line with the out of order input.

Context

StackExchange Code Review Q#66964, answer score: 5

Revisions (0)

No revisions yet.