Chương trình tính tiền nông sản
// Hàm lưu vào sessionStorage
function addNumber() { const inputEl = document.getElementById('numberInput'); let rawValue = inputEl.value.trim();
// Kiểm tra độ dài ký tự if (rawValue.length > 3) { alert("Nhập sai rồi"); inputEl.value = ""; inputEl.focus(); return; }
// Tự thêm dấu thập phân nếu quên if (!rawValue.includes('.') && rawValue.length > 2) { rawValue = rawValue.slice(0, 2) + '.' + rawValue.slice(2); }
const value = parseFloat(rawValue);
if (isNaN(value)) { alert("Vui lòng nhập một số hợp lệ!"); return; }
values.push(value); renderHistory(); updateSummary(); saveSession();
inputEl.value = ""; inputEl.focus(); }
function updateTotalPrice() { const priceInput = document.getElementById('priceInput'); const price = parseFloat(priceInput.value);
if (!isNaN(price)) { const sum = values.reduce((acc, cur) => acc + cur, 0); const total = sum * price; document.getElementById('totalPrice').textContent = total.toLocaleString('vi-VN', { style: 'currency', currency: 'VND' }); } }
function addNumber() { const inputEl = document.getElementById('numberInput'); let rawValue = inputEl.value.trim();
// Kiểm tra độ dài ký tự
if (rawValue.length > 4) { alert("Nhập sai rồi"); inputEl.value = ""; inputEl.focus(); return; }
// Nếu không có dấu '.' và dài hơn 2 ký tự => tự thêm dấu thập phân sau ký tự thứ 2 if (!rawValue.includes('.') && rawValue.length > 3) { rawValue = rawValue.slice(0, 2) + '.' + rawValue.slice(2); }
const value = parseFloat(rawValue);
if (isNaN(value)) { alert("Vui lòng nhập một số hợp lệ!"); return; }
values.push(value); renderHistory(); updateSummary();
inputEl.value = ""; inputEl.focus(); }
let values = [];
function addNumber() { const inputEl = document.getElementById('numberInput'); const rawValue = inputEl.value.trim();
// Kiểm tra độ dài ký tự if (rawValue.length > 5) { alert("Nhập sai rồi"); inputEl.value = ""; inputEl.focus(); return; }
const value = parseFloat(rawValue);
if (isNaN(value)) { alert("Vui lòng nhập một số hợp lệ!"); return; }
values.push(value); renderHistory(); updateSummary();
inputEl.value = ""; inputEl.focus(); }
// Hàm render lịch sử nhập thành nhiều cột kèm nút xoá function renderHistory() { const historyContainer = document.getElementById('history'); historyContainer.innerHTML = "";
for (let i = 0; i < values.length; i += 10) { const col = document.createElement('div'); col.className = 'history-column'; values.slice(i, i + 10).forEach((val, indexInCol) => { const index = i + indexInCol; const item = document.createElement('div'); item.className = 'history-item'; item.innerHTML = ` ${val.toFixed(2)} `; col.appendChild(item); });
historyContainer.appendChild(col); } }
// Hàm xoá giá trị function deleteValue(index) { values.splice(index, 1); renderHistory(); updateSummary(); }
// Cập nhật tổng và số lần nhập function updateSummary() { const total = values.reduce((sum, num) => sum + num, 0); document.getElementById('totalDisplay').innerText = "Tổng Kg hiện tại: " + total.toFixed(2) + "Kg"; document.getElementById('countDisplay').innerText = "Số nông sản đã cân " + values.length + " lượt"; }
// Bắt sự kiện nhấn Enter khi nhập số thực document.getElementById('numberInput').addEventListener("keydown", function(event) { if (event.key === "Enter") { addNumber(); } });
// Tính tiền VNĐ function calculateMoney() { const priceEl = document.getElementById('priceInput'); const price = parseFloat(priceEl.value);
if (isNaN(price)) { alert("Vui lòng nhập giá hợp lệ!"); return; }
const total = values.reduce((sum, num) => sum + num, 0);
const money = total * price;
document.getElementById('moneyDisplay').innerText =
"Thành tiền: " + money.toLocaleString('vi-VN', {minimumFractionDigits: 2, maximumFractionDigits: 2}) + " VNĐ";
}

