-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorderSystem.js
More file actions
113 lines (100 loc) · 4.31 KB
/
Copy pathorderSystem.js
File metadata and controls
113 lines (100 loc) · 4.31 KB
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
function getPrice(apple, banana, grape, melon, orange, watermelon){
let price = apple*150 + banana*80 + grape*450 + melon*600 + orange*120 + watermelon*500;
return price;
}
function createOrderHistory(amountList, totalPrice){
let historyTable = document.getElementById("historyTable");
let lastRowElem = document.getElementById("lastRow");
let rowElems = document.getElementsByTagName("tr");
// 何回目の注文をカウントし、trエレメントを生成する
let count = rowElems.length - 1;
let newRow = document.createElement("tr");
let newCount = document.createElement("td");
newCount.textContent = count + "回目";
newRow.appendChild(newCount);
// 注文データを生成する
for(let i = 0; i < amountList.length; i++){
let newCell = document.createElement("td");
newCell.textContent = amountList[i];
newRow.appendChild(newCell);
}
// 合計金額を生成する
let newTotalPrice = document.createElement("td");
newTotalPrice.textContent = totalPrice + "円";
newTotalPrice.style = "text-align: end";
newRow.appendChild(newTotalPrice);
// 新しい注文履歴を生成する
historyTable.insertBefore(newRow, lastRowElem);
// 総合金額をアップデートする
let finalPrice = 0;
for(let i = 1; i < rowElems.length -1; i++){
eachOrderPrice = parseInt(rowElems[i].lastElementChild.textContent.slice(0, -1));
finalPrice += eachOrderPrice;
}
lastRowElem.lastElementChild.textContent = finalPrice + "円";
}
function makeOrder(){
// 各フルーツのインプットエレメントを取得する
let appleElem = document.orderSystem.apple;
let bananaElem = document.orderSystem.banana;
let grapeElem = document.orderSystem.grape;
let melonElem = document.orderSystem.melon;
let orangeElem = document.orderSystem.orange;
let watermelonElem = document.orderSystem.watermelon;
let fruitList = [appleElem, bananaElem, grapeElem, melonElem, orangeElem, watermelonElem];
// 各フルーツの数を取得する
let apple = parseInt(appleElem.value);
let banana = parseInt(bananaElem.value);
let grape = parseInt(grapeElem.value);
let melon = parseInt(melonElem.value);
let orange = parseInt(orangeElem.value);
let watermelon = parseInt(watermelonElem.value);
let amountList = [apple, banana, grape, melon, orange, watermelon];
// 全ての注文個数を確認する
let totalAmount = 0;
for(let i = 0; i < amountList.length; i++){
totalAmount += amountList[i];
}
if(totalAmount == 0){
alert("全ての注文個数が0です。");
for(let i = 0; i <= fruitList.length; i++){
fruitList[i].style = "background-color: pink;";
fruitList[i].addEventListener("focus", function(){
fruitList[i].style = "background-color: transparent;";
});
}
} else {
if(confirm("注文します。よろしいですか?")){
// 合計金額を計算する
let totalPrice = getPrice(apple, banana, grape, melon, orange, watermelon);
// 合計金額を表示する
alert(`合計金額は${totalPrice}円です。`);
// 注文履歴を追加する
createOrderHistory(amountList, totalPrice);
// 注文内容をクリアする
for(let i = 0; i < fruitList.length; i++){
fruitList[i].value = 0;
}
}
}
}
function handleCheckout(){
let historyTable = document.getElementById("historyTable");
let rowElems = document.getElementsByTagName("tr");
let lastRowElem = document.getElementById("lastRow");
let finalPrice = parseInt(lastRowElem.lastElementChild.textContent.slice(0, -1));
// 注文履歴をチェックする
if(rowElems.length <= 2){
alert("注文履歴がありません。");
} else {
if(confirm("会計を行いますか?")){
// 合計金額を表示する
alert(`合計で${finalPrice}円でした。`);
// 注文履歴をクリアする
for(let i = 1; rowElems.length > 2;){
historyTable.removeChild(rowElems[i]);
}
lastRowElem.lastElementChild.textContent = "円";
}
}
}