Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BookStore #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
(function(){
var app = angular.module('store', []);
app.controller('StoreController', ['$scope',function($scope){
$scope.products = books;
$scope.cart = [];
$scope.total = 0;
$scope.promoItemQuantity = 0;
$scope.promoItemTotalPrice = 0;
$scope.discountPercent = 0;
$scope.discountReduction = 0;
$scope.subtotal = 0;
$scope.groupPromoQuantity = 0;
$scope.tempTotal = 0;
$scope.lowest = 0;
$scope.tmp;
$scope.cartsize = 0;
$scope.addItemToCart = function(product){
if ($scope.cart.length === 0){
product.count = 1;
$scope.cart.push(product);
} else {
var repeat = false;
for(var i = 0; i< $scope.cart.length; i++){
if($scope.cart[i].id === product.id){
repeat = true;
$scope.cart[i].count +=1;
}
}
if (!repeat) {
product.count = 1;
$scope.cart.push(product);
}
}
$scope.lowest = $scope.findLowestPromoIndex();
$scope.discountPercent = $scope.readDiscount();
$scope.subtotal += parseFloat(product.price);
/*
console.log("$scope.discountPercent: "+$scope.discountPercent);
console.log("$scope.groupPromoQuantityTotalPrice: "+ $scope.groupPromoQuantityTotalPrice);
console.log("$scope.promoItemTotalPrice: "+$scope.promoItemTotalPrice);
console.log("$scope.promoItemQuantity: "+$scope.promoItemQuantity);
*/
$scope.calculateTotal();
};
/*this function capture the minimum count of eatch item in the cart. And
use this value to multiplay with promoItemQuantity*/
$scope.findLowestPromoIndex = function(){
this.min = Number.POSITIVE_INFINITY;
for(var i = 0; i< $scope.cart.length; i++)
{
if($scope.cart[i].id <= 7)
{
if($scope.cart.length >= 1)
{
if($scope.cart[i].count <= this.min)
{
this.min = $scope.cart[i].count;
}
}
}
}
if(this.min === Infinity){ //if the first item in the cart isn't a promotion
return 0;
}
else
return this.min;
};
/*read the cart and return discount percent, and it also calculate the
group promotion quantity total price*/
$scope.readDiscount = function(){
this.discount = 0;
this.promoItemQuantity = 0;
$scope.promoItemTotalPrice = 0;
for(var i = 0; i < $scope.cart.length; i++){
if($scope.cart[i].id === 001 || $scope.cart[i].id === 002 ||
$scope.cart[i].id === 003 || $scope.cart[i].id === 004 ||
$scope.cart[i].id === 005 || $scope.cart[i].id === 006 ||
$scope.cart[i].id === 007){
$scope.promoItemQuantity++;
$scope.promoItemTotalPrice += $scope.cart[i].price;
}
}
if(this.promoItemQuantity === 2){
this.discount = .10;
}
if(this.promoItemQuantity === 3){
this.discount = .20;
}
if(this.promoItemQuantity === 4){
this.discount = .30;
}
if(this.promoItemQuantity === 5){
this.discount = .40;
}
if(this.promoItemQuantity === 6){
this.discount = .50;
}
if(this.promoItemQuantity === 7){
this.discount = .60;
}
$scope.groupPromoQuantityTotalPrice = $scope.lowest * $scope.promoItemTotalPrice;
return this.discount;
};
$scope.calculateTotal = function(){
//if theres a discount
$scope.discountReduction = $scope.groupPromoQuantityTotalPrice * $scope.discountPercent
if($scope.discountPercent != 0){
$scope.total = $scope.subtotal - $scope.discountReduction;
}
else{
$scope.total = $scope.subtotal;
}
};
}]);
var books = [{
id: 1,
name: 'Harry Potter and the Philosopher Stone',
price: 100.00

},{
id: 2,
name: 'Harry Potter and the Chamber of Secrets',
price: 100.00
},
{
id: 3,
name: 'Harry Potter and the Prisoner of Azkaban',
price: 100.00
},
{
id: 4,
name: 'Harry Potter and the Goblet of Fire',
price: 100.00
},
{
id: 5,
name: 'Harry Potter and the Order of the Phoenix',
price: 100.00
},
{
id: 6,
name: 'Harry Potter and the Half-Blood Prince',
price: 100.00
},
{
id: 7,
name: 'Harry Potter and the Deathly Hallows',
price: 100.00
},
{
id: 8,
name: 'Lord of the Flies',
price: 100.00
}];

})();
82 changes: 82 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<!DOCTYPE html>
<html ng-app="store">
<head>
<meta charset="utf-8" />
<title>บ้านนายดิน</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js" data-require="[email protected]" data-semver="1.4.4"></script>
<script src="app.js" type="text/javascript"></script>
</head>
<body>
<div class="container">
<div ng-controller="StoreController">
<h1>บ้านนายดิน</h1>
<div class="catalog-1">
<ul class="catalog-2">
<li class="book">
<table>
<caption><h2>Catalog</h2></caption>
<tbody>
<tr ng-repeat="product in products">
<td>{{product.name}}</td>
<td>{{product.price | currency:"฿ "}}</td>
<td><input class="add-button" type="button" ng-click="addItemToCart(product)" value="Add to Cart" /></td>
</tr>
</tbody>
</table>
</li>
</ul>
</div>
<div class="shopping-cart-1" >
<ul class="shopping-cart-2">
<li class="book-in-cart" >
<table>
<caption><h2>Shopping Cart</h2></caption>
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="c in cart">
<td>{{c.name}}</td>
<td>{{c.count}}</td>
<td>{{c.price*c.count | currency:"฿ "}}</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Subtotal</td>
<td></td>
<td>{{subtotal | currency:"฿ "}}</td>
</tr>
<tr>
<td>Discount</td>
<td></td>
<td>{{discountReduction | currency:"฿ "}}</td>
</tr>
<tr>
<td>Total</td>
<td></td>
<td>{{total | currency:"฿ "}}</td>
</tr>
</tfoot>
</table>
</li>
</ul>
</div>
</div>
</div>
</body>
<!--
<footer>
<div>
<p>Posted by: Hege Refsnes</p>
<p>Contact information: <a href="mailto:[email protected]">
[email protected]</a>.</p>
</div>
</footer>
-->
</html>
57 changes: 57 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
@import url('http://fonts.googleapis.com/css?family=Neuton');
html {
color: #555;
/*background-color: #E3B736;*/
}
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
}
li {
list-style: none; /* Remove list bullets */
padding-left: 0;
background: white;
}
h1 {
color: #111;
font-size: 75px;
font-weight: bold;
letter-spacing: -1px;
line-height: 1;
text-align: center;
}
.catalog-1, .shopping-cart-1 {
display: table-cell
}

hr {
border: 1px dotted #000000;
border-style: none none dotted;
color: #fff;
background-color: #fff;
width: 98.0%;
}
.catalog-1 {
position: absolute;
right: 50%;
/*background: #ee3e64;*/
}

.shopping-cart-1 {
position: absolute;
left: 50%;
/*background: #b7d84b;*/
}
table {
border-collapse: collapse;
}
th,
td {
border: 0px solid #cecfd5;
padding: 10px 15px;
}
footer {
width: 100%;
background: #222;
padding: 60px 0px;
}