UXInteract / task_006 /solution /code /index.html
DojiSum's picture
Publish the current 141-task UXInteract release
ef987bb verified
Raw History Blame Contribute Delete
4.2 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Expense Tracker</title>
<style>
:root { --green: #2e7d32; --green-light: #a5d6a7; }
* { box-sizing: border-box; }
body { font-family: system-ui, sans-serif; margin: 0; background: #f1f8e9; color: #1b3a1b;
display: flex; flex-direction: column; min-height: 100vh; }
header { background: var(--green); color: #fff; padding: 16px; text-align: center; }
header h1 { margin: 0 0 10px; font-size: 20px; }
.summary { display: flex; justify-content: space-around; }
.summary div { font-size: 14px; }
.summary strong { display: block; font-size: 22px; }
main { flex: 1; padding: 12px; max-width: 480px; margin: 0 auto; width: 100%; }
#list { list-style: none; padding: 0; margin: 0; }
#list li { background: #fff; border-left: 4px solid var(--green-light); padding: 10px;
margin: 6px 0; border-radius: 6px; display: flex; justify-content: space-between; }
#list li .amt { color: var(--green); font-weight: bold; }
form { position: sticky; bottom: 0; background: #fff; padding: 12px;
display: flex; flex-wrap: wrap; gap: 8px; border-top: 1px solid #ddd; }
input { flex: 1 1 120px; min-width: 0; padding: 10px; border: 1px solid #bbb; border-radius: 6px; font-size: 15px; }
button { background: var(--green); color: #fff; border: none; border-radius: 6px;
padding: 10px 18px; font-size: 16px; cursor: pointer; }
</style>
</head>
<body>
<header>
<h1>Simple Expense Tracker</h1>
<div class="summary">
<div>Total Expense<strong id="totalExpense">¥0.00</strong></div>
<div>Balance<strong id="balance">¥0.00</strong></div>
</div>
</header>
<main>
<ul id="list"></ul>
</main>
<form id="addForm">
<input id="desc" placeholder="Item, e.g. lunch" required>
<input id="amount" type="number" step="0.01" min="0" placeholder="Amount" required>
<button type="submit" id="addBtn">Add</button>
</form>
<script>
const STORAGE_KEY = 'expense-records';
const BUDGET = 1000; // Initial budget used to calculate the balance.
const listEl = document.getElementById('list');
const totalExpenseEl = document.getElementById('totalExpense');
const balanceEl = document.getElementById('balance');
const form = document.getElementById('addForm');
// load expense records from localStorage (offline, no login required)
function load() {
try {
return JSON.parse(localStorage.getItem(STORAGE_KEY)) || [];
} catch (e) {
return [];
}
}
function save(records) {
localStorage.setItem(STORAGE_KEY, JSON.stringify(records));
}
function render() {
const records = load();
listEl.innerHTML = '';
let totalExpense = 0;
records.forEach((rec, idx) => {
totalExpense += rec.amount;
const li = document.createElement('li');
const label = document.createElement('span');
label.textContent = rec.desc;
const amount = document.createElement('span');
amount.className = 'amt';
amount.textContent = '-¥' + rec.amount.toFixed(2);
li.append(label, amount);
const del = document.createElement('button');
del.textContent = '×';
del.style.background = '#c62828';
del.style.padding = '2px 10px';
del.addEventListener('click', () => {
const r = load();
r.splice(idx, 1);
save(r);
render();
});
li.appendChild(del);
listEl.appendChild(li);
});
const balance = BUDGET - totalExpense; // green theme balance
totalExpenseEl.textContent = '¥' + totalExpense.toFixed(2);
balanceEl.textContent = '¥' + balance.toFixed(2);
}
// add a new expense record
form.addEventListener('submit', (e) => {
e.preventDefault();
const desc = document.getElementById('desc').value.trim();
const amount = parseFloat(document.getElementById('amount').value);
if (!desc || isNaN(amount) || amount <= 0) return;
const records = load();
records.push({ desc, amount });
save(records);
form.reset();
render();
});
render();
</script>
</body>
</html>