File size: 4,196 Bytes
ef987bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!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>