Skip to content

Instantly share code, notes, and snippets.

@alexey-sh
Last active August 29, 2015 14:23
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 alexey-sh/291005e4d81e6443987f to your computer and use it in GitHub Desktop.
Save alexey-sh/291005e4d81e6443987f to your computer and use it in GitHub Desktop.
usualIQ = 120
workingHoursPerDay = 6
getWorkingDaysInMonth = (date) ->
isWorkingDate = (date) ->
day = date.getDay()
return day isnt 0 and day isnt 6
daysInMonth = new Date(date)
daysInMonth.setMonth(daysInMonth.getMonth() + 1)
daysInMonth.setDate(0)
daysInMonth = date.getDate()
days = 0
while (days < daysInMonth)
days++ if isWorkingDate(date)
date.setDate(date.getDate() + 1)
return days
class Money
constructor: (@money=0) ->
add: (money) ->
@money += money
valueOf: () ->
return @money
toString: () ->
return @money
class Wallet extends Money
class Salary extends Money
class Bonus extends Money
class Human
constructor: () ->
hi: () ->
throw new Error("doesn't implemented")
class Person extends Human
constructor: (@name, @iq) ->
isClever: () ->
return @iq > usualIQ
hi: () ->
return "Hi, my name is #{@name}, my iq is #{@iq}"
class Employee extends Person
constructor: (@name, @iq, @rate) ->
@wallet = new Wallet()
getMonthlyRate: () ->
date = new Date()
return getWorkingDaysInMonth(date) * @rate * workingHoursPerDay
pay: (salary) ->
@wallet.add(salary)
hi: () ->
return 'Bip Bop...'
employee = new Employee('a', 121, 20)
console.log(employee.hi())
console.log(employee.isClever())
console.log(employee.getMonthlyRate())
employee.pay(new Salary(employee.getMonthlyRate()) + new Bonus(500))
console.log(employee.wallet + 0)
(function () {
var getWorkingDaysInMonth = function(date) {
var days, daysInMonth, isWorkingDate;
isWorkingDate = function(date) {
var day;
day = date.getDay();
return day !== 0 && day !== 6;
};
daysInMonth = new Date(date);
daysInMonth.setMonth(daysInMonth.getMonth() + 1);
daysInMonth.setDate(0);
daysInMonth = date.getDate();
days = 0;
while (days < daysInMonth) {
if (isWorkingDate(date)) {
days++;
}
date.setDate(date.getDate() + 1);
}
return days;
};
var extend = function (child, parent) {
for (var key in parent) {
if ({}.hasOwnProperty.call(parent, key)) {
child[key] = parent[key];
}
}
function ctor() {
this.constructor = child;
}
ctor.prototype = parent.prototype;
child.prototype = new ctor();
child.__super__ = parent.prototype;
return child;
};
var usualIQ = 120;
var workingHoursPerDay = 6;
function Money(money) {
this.money = typeof money === 'number' ? money : 0;
}
Money.prototype.add = function(money) {
this.money += money;
};
Money.prototype.valueOf = function() {
return this.money;
};
Money.prototype.toString = function() {
return this.money;
};
function Wallet() {
return Wallet.__super__.constructor.apply(this, arguments);
}
extend(Wallet, Money);
function Salary() {
return Salary.__super__.constructor.apply(this, arguments);
}
extend(Salary, Money);
function Bonus() {
return Bonus.__super__.constructor.apply(this, arguments);
}
extend(Bonus, Money);
function Human() {}
Human.prototype.hi = function() {
throw new Error("doesn't implemented");
};
function Person(name, iq) {
this.name = name;
this.iq = iq;
}
extend(Person, Human);
Person.prototype.isClever = function () {
return this.iq > usualIQ;
};
Person.prototype.hi = function () {
return "Hi, my name is " + this.name + ", my iq is " + this.iq;
};
function Employee(name, iq, rate) {
this.name = name;
this.iq = iq;
this.rate = rate;
this.wallet = new Wallet();
}
extend(Employee, Person);
Employee.prototype.getMonthlyRate = function() {
var date;
date = new Date();
return getWorkingDaysInMonth(date) * this.rate * workingHoursPerDay;
};
Employee.prototype.pay = function(salary) {
return this.wallet.add(salary);
};
Employee.prototype.hi = function () {
return 'Bip Bop...';
};
var employee = new Employee('Alexey', 121, 20);
console.log(employee.hi());
console.log(employee.isClever());
console.log(employee.getMonthlyRate());
employee.pay(new Salary(employee.getMonthlyRate()) + new Bonus(500));
console.log(employee.wallet + 0);
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment