Skip to content

Instantly share code, notes, and snippets.

@delatorremario
Created March 21, 2016 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save delatorremario/3e2abdfceaae524892c7 to your computer and use it in GitHub Desktop.
Save delatorremario/3e2abdfceaae524892c7 to your computer and use it in GitHub Desktop.
/*BananaDesk Step 2*/
/*Mario de la Torre*/
var Person = function(firstName){
this.firstName = firstName;
};
Person.prototype.salarycalc = function (){
/*test-code*/
/*afirmations*/
/*salary=0*/
var test = function(){
return 0===0;
};
if(!test)console.log('salarycalc-test-Person',test());
/*end-test-code*/
return test && 0 ;
};
function MonthlyEmployee(firstName,monthlysalary){
Person.call(this,firstName);
this.monthlysalary=monthlysalary;
}
MonthlyEmployee.prototype=Object.create(Person.prototype);
MonthlyEmployee.prototype.constructor=MonthlyEmployee;
MonthlyEmployee.prototype.salarycalc=function(){
/*test-code*/
/*afirmations*/
/*salary >0*/
var test = function(item){
return item.monthlysalary>0;
};
if(!test) console.log('salarycalc-test-MonthlyEmployee',test(this));
/*end-test-code*/
return test && this.monthlysalary || 0;
};
function HourlyEmployee(firstName,hourvalue){
Person.call(this,firstName);
this.hourvalue=hourvalue;
this.hoursworked=0;
}
HourlyEmployee.prototype=Object.create(Person.prototype);
HourlyEmployee.prototype.constructor=HourlyEmployee;
HourlyEmployee.prototype.addWeeklyHours=function(hoursworked){
this.hoursworked+=hoursworked;
};
HourlyEmployee.prototype.salarycalc=function(){
/*test-code*/
/*afirmations*/
/*hoursworked>=0*/
/*hourvalue>0*/
var test = function(item){
return item.hoursworked>=0 && item.hourvalue>0;
};
if(!test) console.log('salarycalc-test-HourlyEmployee',test(this));
/*end-test-code*/
return test(this) && this.hourvalue * this.hoursworked || 0;
};
WeeklyTimeCard=function(firstName,hoursworked){
this.firstName=firstName;
this.hoursworked=hoursworked;
};
function Voluntary(firstName){
Person.call(this,firstName);
}
Voluntary.prototype=Object.create(Person.prototype);
Voluntary.prototype.constructor=Voluntary;
function CalculatingSalaries(employees,weeklytimecards){
console.log('Calculating Salaries');
console.log('--------------------');
/*input weeklytimecards*/
weeklytimecards.forEach(function(item){
var i = functiontofindIndexByKeyValue(employees,'firstName',item.firstName);
employees[i].addWeeklyHours(item.hoursworked);
});
employees.forEach(function(employee){
var salary = employee.salarycalc();
console.log('Name: ' + employee.firstName);
console.log('Salary:' + salary.toFixed(2));
console.log('Type: ' + employee.constructor.name );
console.log('-------------------------------------');
});
}
function functiontofindIndexByKeyValue(arraytosearch, key, valuetosearch) {
for (var i = 0; i < arraytosearch.length; i++) {
if (arraytosearch[i][key] == valuetosearch) {
return i;
}
}
return null;
}
/*implementation*/
var employees = [
new MonthlyEmployee('Homer J. Simpson',1345),
new MonthlyEmployee('Leonard Lenford', 1432),
new HourlyEmployee('Edna Krabappel',23),
new HourlyEmployee('Julius Hibbert',45),
new Voluntary('Seymour Skinner'),
new Voluntary('Otto')
];
var weeklytimecards = [
new WeeklyTimeCard('Edna Krabappel',23),
new WeeklyTimeCard('Julius Hibbert',32),
new WeeklyTimeCard('Edna Krabappel',11),
new WeeklyTimeCard('Julius Hibbert',25),
new WeeklyTimeCard('Edna Krabappel',34),
new WeeklyTimeCard('Julius Hibbert',21)
];
CalculatingSalaries(employees,weeklytimecards);
/*end-implementation*/
//RESULTS
/*
Calculating Salaries
--------------------
Name: Homer J. Simpson
Salary:1345.00
Type: MonthlyEmployee
-------------------------------------
Name: Leonard Lenford
Salary:1432.00
Type: MonthlyEmployee
-------------------------------------
Name: Edna Krabappel
Salary:1564.00
Type: HourlyEmployee
-------------------------------------
Name: Julius Hibbert
Salary:3510.00
Type: HourlyEmployee
-------------------------------------
Name: Seymour Skinner
Salary:0.00
Type: Voluntary
-------------------------------------
Name: Otto
Salary:0.00
Type: Voluntary
-------------------------------------
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment