-
Notifications
You must be signed in to change notification settings - Fork 0
/
pizza.js
188 lines (165 loc) · 4.93 KB
/
pizza.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//business logic
function Order(type, size, crust, topping) {
this.type = type;
this.size = size;
this.crust = crust;
this.topping = topping;
}
//get crust price
Order.prototype.getCrust = function () {
if (this.crust === 0) {
return 100
} else if (this.crust === 1) {
return 50
} else if (this.crust === 2) {
return 200
}
}
//get topping price
Order.prototype.getTopping = function () {
if (this.topping === 0) {
return 300
} else if (this.topping === 1) {
return 50
} else if (this.topping === 2) {
return 200
} else if (this.topping === 3) {
return 100
}
}
//get size price
Order.prototype.getSize = function () {
if (this.type == 0) {
if (this.size === 0) {
return 800
} else if (this.size === 1)
return 1000
else {
return 1500
}
} else if (this.type == 1) {
if (this.size === 0) {
return 800
} else if (this.size === 1)
return 1000
else {
return 1500
}
} else if (this.type == 2) {
if (this.size === 0) {
return 800
} else if (this.size === 1)
return 1000
else {
return 1500
}
} else if (this.type == 3) {
if (this.size === 0) {
return 800
} else if (this.size === 1)
return 1000
else {
return 1500
}
} else if (this.type == 4) {
if (this.size === 0) {
return 800
} else if (this.size === 1)
return 800
else {
return 1500
}
} else if (this.type == 5) {
if (this.size === 0) {
return 800
} else if (this.size === 1)
return 1000
else {
return 1500
}
} else {
return false;
}
}
//user Interface
//calculate total cost
function billYako() {
var sum = 0;
$(".billPerOrder").each(function () {
var value = $(this).text();
if (!isNaN(value) && value.length != 0) {
sum += parseFloat(value);
}
});
if (document.getElementById('yes').checked) {
var result = `Your order cost is Ksh ${sum} plus a delivery fee of Ksh 200 `;
var orderBill = sum + 200;
var total = "Total: Ksh. " + orderBill ;
$('#result').text(result);
$('#totalCost').text(total);
swal({
title: "Note! Delivery of your order to location specified will cost ksh 200 extra",
icon: "",
})
} else {
var total = "Total: Ksh. " + sum ;
$('#totalCost').text(total)
}
}
function checkout() {
swal({
title: `Order successfully placed
Thank You and Welcome to carmelitas`,
icon: "success",
}).then((value) => {
location.reload();
});
}
$(document).ready(function () {
$('#checkout').click(function () {
checkout();
})
$('.radioBtn').change(function () {
if (document.getElementById("yes").checked) {
$('.location').show();
} else {
$('.location').hide();
}
});
$('#addToCart').click(function () {
var type = $('#type option:selected').val();
var size = $('#size option:selected').val();
var crust = $('#crust option:selected').val();
var quantity = $('#quantity').val();
var topping = $('#topping option:selected').val();
var name = $('#name').val();
//validate fields
if (type == '' || size == '' || crust == '' || topping == '' || quantity == '' || name == '') {
swal("Order fields empty!", "fill the order first");
} else if (document.getElementById("yes").checked && $('#location').val() == '') {
swal("Location field empty!", "fill the location field first");
} else {
var selectedType = parseInt($('#type option:selected').val());
var selectedSize = parseInt($('#size option:selected').val());
var selectedCrust = parseInt($('#crust option:selected').val());
var quantity = parseInt($('#quantity').val());
var selectedTopping = parseInt($('#topping option:selected').val());
//create new order object
var pizzaFunga = new Order(selectedType, selectedSize, selectedCrust, selectedTopping);
//getting price of individual pizzas
var soloPizza = (pizzaFunga.getSize() + pizzaFunga.getCrust() + pizzaFunga.getTopping()) * quantity;
//append data to table
$('.displayOrder').show();
$(".table tbody:last").append("<tr>" +
"<td>" + $('#type option:selected').text() + "</td>" +
"<td>" + $('#size option:selected').text() + "</td>" +
"<td>" + $('#crust option:selected').text() + "</td>" +
"<td>" + $('#topping option:selected').text() + "</td>" +
"<td>" + $('#quantity').val() + "</td>" +
"<td><span class='billPerOrder'>" + soloPizza + "</span></td>" +
"<td><input type='checkbox' name='record'></td>" +
"</tr>");
$(billYako);
}
})
})