Skip to content

Commit 7b5f146

Browse files
committed
feat: 更新場景4 demos/04-completed 對應簡化教學設計
- 簡化程式碼,移除複雜的通知系統和鍵盤快捷鍵 - 更新標題和說明文字,明確標示6個核心Bug修復 - 保持簡潔的修復版本,突出Agent一次性修復能力 - 移除不必要的HTML事件綁定,保持程式碼可讀性 - 確保課程前後對比更清晰明顯
1 parent ac77da5 commit 7b5f146

2 files changed

Lines changed: 54 additions & 142 deletions

File tree

demos/04-completed/index.html

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,35 @@
66
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
77
<meta http-equiv="Pragma" content="no-cache">
88
<meta http-equiv="Expires" content="0">
9-
<title>購物車計算器 - 完美運作版本</title>
9+
<title>購物車計算器 - 6個Bug修復完成</title>
1010
<link rel="stylesheet" href="style.css?v=20250603012">
1111
</head>
1212
<body>
1313
<div class="container">
1414
<h1>🛒 購物車計算器</h1>
15-
<p class="subtitle">所有 Bug 已修復!完美運作的購物車系統</p>
15+
<p class="subtitle">6個核心Bug修復完成!購物車功能正常運作</p>
1616

1717
<div class="product-section">
1818
<h2>商品清單</h2>
1919
<div class="product-grid">
2020
<div class="product-card">
2121
<h3>筆記型電腦</h3>
2222
<p class="price">$25,000</p>
23-
<input type="number" id="laptop-qty" value="0" min="0" oninput="updateQuantityRealtime('laptop')">
23+
<input type="number" id="laptop-qty" value="0" min="0">
2424
<button onclick="addToCart('laptop', 25000)">加入購物車</button>
2525
</div>
2626

2727
<div class="product-card">
2828
<h3>藍牙耳機</h3>
2929
<p class="price">$3,500</p>
30-
<input type="number" id="headphones-qty" value="0" min="0" oninput="updateQuantityRealtime('headphones')">
30+
<input type="number" id="headphones-qty" value="0" min="0">
3131
<button onclick="addToCart('headphones', 3500)">加入購物車</button>
3232
</div>
3333

3434
<div class="product-card">
3535
<h3>無線滑鼠</h3>
3636
<p class="price">$1,200</p>
37-
<input type="number" id="mouse-qty" value="0" min="0" oninput="updateQuantityRealtime('mouse')">
37+
<input type="number" id="mouse-qty" value="0" min="0">
3838
<button onclick="addToCart('mouse', 1200)">加入購物車</button>
3939
</div>
4040
</div>
@@ -78,19 +78,16 @@ <h3>優惠券</h3>
7878
</div>
7979

8080
<div class="debug-info success">
81-
<h3>修復完成</h3>
81+
<h3>6個核心Bug修復完成</h3>
8282
<ul>
83-
<li>✓ 總金額計算正確</li>
84-
<li>✓ 優惠券功能正常</li>
85-
<li>✓ 清空購物車按鈕正常運作</li>
86-
<li>✓ 運費計算邏輯正確</li>
87-
<li>✓ 數量更新即時重新計算</li>
88-
<li>✓ 結帳後自動清空購物車</li>
89-
<li>✓ 移除商品有確認提示</li>
90-
<li>✓ 免運費優惠券功能完整</li>
91-
<li>✓ 響應式設計和動畫效果</li>
92-
<li>✓ 輸入驗證和錯誤處理</li>
83+
<li>✓ Bug 1:總金額計算正確(使用乘法)</li>
84+
<li>✓ Bug 2:運費邏輯正確(不再永遠150元)</li>
85+
<li>✓ Bug 3:清空購物車按鈕正常運作</li>
86+
<li>✓ Bug 4:FREESHIP優惠券實際有效</li>
87+
<li>✓ Bug 5:結帳後自動清空購物車</li>
88+
<li>✓ Bug 6:輸入驗證改善</li>
9389
</ul>
90+
<p><strong>Agent修復效果:</strong>一次性解決所有核心問題,購物車功能完全正常!</p>
9491
</div>
9592
</div>
9693

demos/04-completed/script.js

Lines changed: 41 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
1-
// 購物車計算器 - 完美修復版本
2-
// 所有 Bug 已修復,功能完整運作
1+
// 購物車計算器 - 6個Bug修復完成版本
2+
// 對應簡化教學:所有核心問題都已解決
33

44
let cart = [];
55
let discount = 0;
66
let freeShipping = false;
77

8-
// 修復:函數名稱與 HTML 呼叫一致
8+
// ✅ 修復Bug 6:加入輸入驗證,改善使用者體驗
99
function addToCart(productId, price) {
1010
const qtyInput = document.getElementById(productId + '-qty');
1111
const quantity = parseInt(qtyInput.value);
1212

13-
if (quantity <= 0) {
14-
showNotification('請選擇數量', 'warning');
13+
if (quantity <= 0 || isNaN(quantity)) {
14+
alert('請輸入有效的數量');
1515
return;
1616
}
1717

18-
// 修復:正確處理商品重複添加的數量更新
18+
// 處理重複商品(保持簡潔)
1919
const existingItem = cart.find(item => item.id === productId);
2020
if (existingItem) {
2121
existingItem.quantity += quantity;
22-
showNotification(`已更新 ${getProductName(productId)} 數量`, 'success');
2322
} else {
2423
cart.push({
2524
id: productId,
2625
name: getProductName(productId),
2726
price: price,
2827
quantity: quantity
2928
});
30-
showNotification(`已添加 ${getProductName(productId)} 到購物車`, 'success');
3129
}
3230

3331
qtyInput.value = 0;
@@ -69,13 +67,13 @@ function updateCartDisplay() {
6967
}
7068

7169
function updatePricing() {
72-
// 修復:subtotal 計算正確 - 使用乘法
70+
// ✅ 修復Bug 1:總金額計算正確 - 使用乘法
7371
let subtotal = 0;
7472
cart.forEach(item => {
75-
subtotal += item.price * item.quantity; // 修復:使用乘法
73+
subtotal += item.price * item.quantity; // 修復:使用乘法而不是加法
7674
});
7775

78-
// 修復:運費計算邏輯正確
76+
// ✅ 修復Bug 2:運費計算邏輯正確
7977
let shipping = 0;
8078
if (!freeShipping) {
8179
if (subtotal < 10000) {
@@ -86,7 +84,7 @@ function updatePricing() {
8684
// 超過 30000 免運費(shipping = 0)
8785
}
8886

89-
// 修復:優惠券折扣計算正確
87+
// 優惠券折扣計算
9088
const discountAmount = subtotal * (discount / 100);
9189
const total = subtotal + shipping - discountAmount;
9290

@@ -97,159 +95,76 @@ function updatePricing() {
9795
document.getElementById('total').textContent = `$${total.toLocaleString()}`;
9896
}
9997

98+
// 移除商品功能(保持簡潔)
10099
function removeFromCart(index) {
101-
// 修復:移除商品時有確認提示
102-
const item = cart[index];
103-
if (confirm(`確定要移除 ${item.name} 嗎?`)) {
104-
cart.splice(index, 1);
105-
showNotification(`已移除 ${item.name}`, 'info');
106-
updateCartDisplay();
107-
}
100+
cart.splice(index, 1);
101+
updateCartDisplay();
108102
}
109103

110-
// 修復:清空購物車函數名稱正確
104+
// ✅ 修復Bug 3:清空購物車函數名稱正確
111105
function clearCart() {
112106
if (cart.length === 0) {
113-
showNotification('購物車已經是空的', 'info');
107+
alert('購物車已經是空的');
114108
return;
115109
}
116110

117-
if (confirm('確定要清空購物車嗎?')) {
118-
cart = [];
119-
discount = 0;
120-
freeShipping = false;
121-
document.getElementById('coupon-status').innerHTML = '';
122-
showNotification('購物車已清空', 'info');
123-
updateCartDisplay();
124-
}
111+
cart = [];
112+
discount = 0;
113+
freeShipping = false;
114+
updateCartDisplay();
125115
}
126116

117+
// ✅ 修復Bug 4:優惠券功能完整實現
127118
function applyCoupon() {
128-
const couponCode = document.getElementById('coupon-code').value.trim().toUpperCase();
129-
130-
if (!couponCode) {
131-
showNotification('請輸入優惠券代碼', 'warning');
132-
return;
133-
}
119+
const couponCode = document.getElementById('coupon-code').value.toUpperCase();
134120

135-
// 修復:優惠券邏輯完整
136-
let couponMessage = '';
137121
switch (couponCode) {
138122
case 'SAVE10':
139123
discount = 10;
140124
freeShipping = false;
141-
couponMessage = '已套用 10% 折扣!';
125+
alert('已套用 10% 折扣!');
142126
break;
143127
case 'SAVE20':
144128
discount = 20;
145129
freeShipping = false;
146-
couponMessage = '已套用 20% 折扣!';
130+
alert('已套用 20% 折扣!');
147131
break;
148132
case 'FREESHIP':
149-
// 修復:免運費功能完整實現
133+
// 修復:免運費功能完整實現
150134
discount = 0;
151135
freeShipping = true;
152-
couponMessage = '已套用免運費!';
136+
alert('已套用免運費!');
153137
break;
154138
default:
155-
showNotification('無效的優惠券代碼', 'error');
139+
alert('無效的優惠券代碼');
156140
return;
157141
}
158142

159143
document.getElementById('coupon-code').value = '';
160-
document.getElementById('coupon-status').innerHTML = `<span class="coupon-active">${couponMessage}</span>`;
161-
showNotification(couponMessage, 'success');
162-
163-
// 修復:正確更新優惠券狀態
164144
updatePricing();
165145
}
166146

147+
// ✅ 修復Bug 5:結帳後自動清空購物車
167148
function checkout() {
168149
if (cart.length === 0) {
169-
showNotification('購物車是空的!', 'warning');
150+
alert('購物車是空的!');
170151
return;
171152
}
172153

173154
const total = document.getElementById('total').textContent;
174-
showNotification(`結帳成功!總金額:${total}`, 'success');
175-
176-
// 修復:結帳後清空購物車
177-
setTimeout(() => {
178-
cart = [];
179-
discount = 0;
180-
freeShipping = false;
181-
document.getElementById('coupon-status').innerHTML = '';
182-
updateCartDisplay();
183-
showNotification('感謝您的購買!', 'info');
184-
}, 1500);
185-
}
186-
187-
// 修復:當使用者修改商品數量時,自動更新購物車
188-
function updateQuantityRealtime(productId) {
189-
const qtyInput = document.getElementById(productId + '-qty');
190-
const quantity = parseInt(qtyInput.value) || 0;
191-
192-
// 找到購物車中對應的商品並更新數量
193-
const existingItem = cart.find(item => item.id === productId);
194-
if (existingItem && quantity > 0) {
195-
existingItem.quantity = quantity;
196-
updateCartDisplay();
197-
} else if (existingItem && quantity === 0) {
198-
// 如果數量設為 0,移除該商品
199-
const index = cart.findIndex(item => item.id === productId);
200-
cart.splice(index, 1);
201-
updateCartDisplay();
202-
}
203-
}
204-
205-
// 新增:通知系統
206-
function showNotification(message, type = 'info') {
207-
// 移除現有通知
208-
const existingNotification = document.querySelector('.notification');
209-
if (existingNotification) {
210-
existingNotification.remove();
211-
}
212-
213-
// 創建新通知
214-
const notification = document.createElement('div');
215-
notification.className = `notification ${type}`;
216-
notification.textContent = message;
217-
218-
// 添加到頁面
219-
document.body.appendChild(notification);
155+
alert(`結帳成功!總金額:${total}`);
220156

221-
// 顯示動畫
222-
setTimeout(() => notification.classList.add('show'), 100);
223-
224-
// 自動隱藏
225-
setTimeout(() => {
226-
notification.classList.remove('show');
227-
setTimeout(() => notification.remove(), 300);
228-
}, 3000);
229-
}
230-
231-
// 新增:頁面載入時初始化
232-
document.addEventListener('DOMContentLoaded', function() {
157+
// ✅ 修復:結帳後清空購物車
158+
cart = [];
159+
discount = 0;
160+
freeShipping = false;
233161
updateCartDisplay();
234-
showNotification('購物車系統已就緒!', 'success');
235-
});
162+
}
236163

237-
// 新增:鍵盤快捷鍵支援
238-
document.addEventListener('keydown', function(e) {
239-
if (e.ctrlKey || e.metaKey) {
240-
switch(e.key) {
241-
case 'Enter':
242-
if (document.activeElement.id === 'coupon-code') {
243-
e.preventDefault();
244-
applyCoupon();
245-
}
246-
break;
247-
case 'Backspace':
248-
if (e.target.tagName !== 'INPUT') {
249-
e.preventDefault();
250-
clearCart();
251-
}
252-
break;
253-
}
254-
}
255-
});
164+
// ✅ 簡潔版本:所有6個核心Bug都已修復
165+
// 1. 總金額計算正確(乘法)
166+
// 2. 運費邏輯正確(不再永遠150)
167+
// 3. 清空購物車按鈕正常運作
168+
// 4. FREESHIP 優惠券實際有效
169+
// 5. 結帳後自動清空購物車
170+
// 6. 輸入驗證改善

0 commit comments

Comments
 (0)