Skip to content

Instantly share code, notes, and snippets.

@travisluong
Created May 22, 2015 02:45
Show Gist options
  • Save travisluong/87192e36f5a888fcd946 to your computer and use it in GitHub Desktop.
Save travisluong/87192e36f5a888fcd946 to your computer and use it in GitHub Desktop.
homework 6 - donut shop
<!DOCTYPE html>
<html>
<head>
<title>Homework 6 - Donut shop</title>
</head>
<body>
<table id="donut-shops" border="1">
<thead>
<tr>
<th>Shop</th>
<th>Customers Per Hour</th>
<th>View Details!</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<br><br><br>
<table id="donut-shop-details" border="1">
<thead>
<th>Donuts Per Hour</th>
<th>Donuts Per Day</th>
</thead>
<tbody>
<!-- user jQuery to fill out details here when you click a button. -->
</tbody>
</table>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
function DonutMaster() {
this.donutShops = [];
this.addShop = function(shop) {
this.donutShops.push(shop);
}
this.generateReport = function() {
var tbody = $("#donut-shops").find('tbody');
for (var i = 0; i < this.donutShops.length; i++) {
var tr = $('<tr>');
var name = $('<td>').text(this.donutShops[i].shopName);
var customersPerHour = $('<td>').text(this.donutShops[i].customersPerHour());
var shopButton = $('<td>').append(this.generateShopButton(this.donutShops[i]));
tr.append(name).append(customersPerHour).append(shopButton);
tbody.append(tr);
};
}
this.generateShopButton = function(shop) {
var button = $('<button>').text(shop.shopName);
button.on('click', function(e) {
e.preventDefault();
console.log(shop.shopName);
});
return button;
}
}
function DonutShop(shopName, minCustomersPerHour, maxCustomersPerHour, avgDonutsPerCustomers, hoursOpenPerDay) {
this.shopName = shopName;
this.minCustomersPerHour = minCustomersPerHour;
this.maxCustomersPerHour = maxCustomersPerHour;
this.avgDonutsPerCustomers = avgDonutsPerCustomers;
this.hoursOpenPerDay = hoursOpenPerDay;
this.customersPerHour = function() {
return Math.floor(Math.random() * (this.maxCustomersPerHour - this.minCustomersPerHour + 1) + this.minCustomersPerHour);
}
this.donutsPerHour = function() {
return this.customersPerHour() * this.avgDonutsPerCustomers;
}
this.donutsPerDay = function() {
return this.donutsPerHour() * this.hoursOpenPerDay;
}
}
var donutMaster = new DonutMaster();
donutMaster.addShop(new DonutShop("Downtown", 8, 43, 4.5, 8));
donutMaster.addShop(new DonutShop("Capitol Hill", 4, 37, 2, 8));
donutMaster.addShop(new DonutShop("South Lake Union", 9, 23, 6.33, 8));
donutMaster.addShop(new DonutShop("Wedgewood", 2, 28, 1.25, 8));
donutMaster.addShop(new DonutShop("Ballard", 8, 58, 3.75, 8));
donutMaster.generateReport();
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment