Skip to content

Instantly share code, notes, and snippets.

@koshuang
Created July 26, 2013 06:06
Show Gist options
  • Save koshuang/6086651 to your computer and use it in GitHub Desktop.
Save koshuang/6086651 to your computer and use it in GitHub Desktop.
Javascript version of Refactoring example
var countTotal = function(array, func) {
var total = 0;
array.forEach(function(each) {
total += func(each);
});
return total;
}
var Movie = function(title, priceCode) {
this.title = title;
this.priceCode = priceCode;
this.getCharge = function(daysRented) {
var thisAmount = 0;
//determine amounts for each line
switch (this.priceCode) {
case Movie.REGULAR:
thisAmount += 2;
if (daysRented > 2)
thisAmount += (daysRented - 2) * 1.5;
break;
case Movie.NEW_RELEASE:
thisAmount += daysRented * 3;
break;
case Movie.CHILDRENS:
thisAmount += 1.5;
if (daysRented > 3)
thisAmount += (daysRented - 3) * 1.5;
break;
}
return thisAmount;
};
this.getFrequentRenterPoints = function(daysRented) {
// add bonus for a two day new release rental
if ((this.priceCode == Movie.NEW_RELEASE) &&
daysRented > 1) {
return 2;
} else {
return 1;
}
}
}
Movie.REGULAR = 0;
Movie.NEW_RELEASE = 1;
Movie.CHILDRENS = 2;
var Rental = function(movie, daysRented) {
this.movie = movie;
this.daysRented = daysRented;
this.getCharge = function() {
return this.movie.getCharge(this.daysRented);
};
this.getFrequentRenterPoints = function() {
return this.movie.getFrequentRenterPoints(this.daysRented);
};
}
var Customer = function(name) {
this.name = name;
this.rentals = [];
var that = this;
this.addRental = function(aRental) {
this.rentals.push(aRental);
};
this.getTotalCharge = function() {
return countTotal(this.rentals, function(each){
return each.getCharge();
});
};
this.getTotalFrequentRenterPoints = function() {
return countTotal(this.rentals, function (each) {
return each.getFrequentRenterPoints();
})
};
this.statement = function() {
var result = "Rental Record for " + this.name + "\n";
this.rentals.forEach(function(each) {
//show figures for this rental
result += "\t" + each.movie.title + "\t" +
each.getCharge() + "\n";
});
//add footer lines
result += "Amount owed is " + this.getTotalCharge() + "\n";
result += "You earned " + this.getTotalFrequentRenterPoints() + " frequent renter points";
return result;
}
this.htmlStatement = function() {
var result = "<H1>Rentals for <EM>" + this.name + "</EM></H1><P>\n";
this.rentals.forEach(function(each) {
//show figures for this rental
result += each.movie.title + ": " +
each.getCharge() + "<BR>\n";
});
//add footer lines
result += "<P>You owe <EM>" + this.getTotalCharge() + "</EM><P>\n";
result += "On this rental you earned <EM>" + this.getTotalFrequentRenterPoints() + "</EM> frequent renter points<P>";
return result;
}
}
var movie = new Movie("Facebook", 2);
var rental = new Rental(movie, 3);
var customer = new Customer("Kos");
customer.addRental(rental);
console.log(customer.statement());
document.write(customer.htmlStatement());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment