-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
212 lines (181 loc) · 5.88 KB
/
index.html
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<!DOCTYPE html>
<html>
<head>
<title>Financial Tracker</title>
<style>
/* Global Styles */
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
margin: 0;
padding: 0;
text-align: center;
}
h1 {
color: #333;
}
/* Styles for Desktop Screens */
@media (min-width: 768px) {
h2, #transaction-form {
width: 50%;
margin: 0 auto;
}
#transaction-form {
padding: 20px;
background-color: #fff;
border-radius: 5px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2);
}
ul {
width: 50%;
margin: 20px auto;
}
}
/* Styles for Mobile Screens */
@media (max-width: 767px) {
h2, #transaction-form, ul {
width: 90%;
margin: 0 auto;
}
#transaction-form {
padding: 10px;
}
}
label {
display: block;
margin-bottom: 5px;
}
input[type="number"],
input[type="text"],
#search {
width: 100%;
padding: 8px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 4px;
}
button[type="submit"] {
background-color: #3498db;
color: #fff;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
}
button[type="submit"]:hover {
background-color: #2980b9;
}
h2 {
margin-top: 20px;
color: #333;
}
ul {
list-style-type: none;
padding: 0;
}
li {
background-color: #fff;
border: 1px solid #ccc;
border-radius: 4px;
padding: 10px;
margin-bottom: 5px;
display: flex;
justify-content: space-between;
align-items: center;
}
</style>
</head>
<body>
<h1>Financial Tracker</h1>
<h2>Balance: $<span id="balance">0.00</span></h2>
<form id="transaction-form">
<label for="amount">Amount: $</label>
<input type="number" id="amount" step="0.01" required>
<br>
<label for="reason">Reason: </label>
<input type="text" id="reason" required>
<br>
<button type="submit">Add Transaction</button>
</form>
<h2>Transactions:</h2>
<input type="text" id="search" placeholder="Search Transactions">
<button onclick="searchTransactions()">Search</button>
<ul id="transaction-list">
<!-- Transaction items will be added here -->
</ul>
<script>
// JavaScript logic for adding and displaying transactions
const transactionForm = document.getElementById('transaction-form');
const amountInput = document.getElementById('amount');
const reasonInput = document.getElementById('reason');
const balanceDisplay = document.getElementById('balance');
const transactionList = document.getElementById('transaction-list');
const searchInput = document.getElementById('search');
let balance = 0;
let transactions = [];
// Load saved transactions from local storage if available
if (localStorage.getItem('transactions')) {
transactions = JSON.parse(localStorage.getItem('transactions'));
updateTransactionList();
}
function updateTransactionList() {
// Clear the current list
transactionList.innerHTML = '';
// Add transactions to the list
transactions.forEach((transaction, index) => {
const transactionItem = document.createElement('li');
transactionItem.innerHTML = `
<span>${transaction.date} - $${transaction.amount.toFixed(2)} - ${transaction.reason}</span>
<button onclick="deleteTransaction(${index})">Delete</button>
`;
transactionList.appendChild(transactionItem);
});
// Update balance
balance = transactions.reduce((total, transaction) => total + transaction.amount, 0);
balanceDisplay.textContent = balance.toFixed(2);
}
transactionForm.addEventListener('submit', function (e) {
e.preventDefault();
const amount = parseFloat(amountInput.value);
const reason = reasonInput.value;
if (isNaN(amount) || amount === 0) {
alert('Please enter a valid amount.');
return;
}
// Create a new transaction object
const transaction = {
date: new Date().toLocaleString(),
amount: amount,
reason: reason
};
// Add transaction to the array and update the list
transactions.push(transaction);
updateTransactionList();
// Clear input fields
amountInput.value = '';
reasonInput.value = '';
// Save transactions to local storage
localStorage.setItem('transactions', JSON.stringify(transactions));
});
function deleteTransaction(index) {
// Remove the transaction at the specified index
transactions.splice(index, 1);
// Update the transaction list and save changes
updateTransactionList();
localStorage.setItem('transactions', JSON.stringify(transactions));
}
function searchTransactions() {
const searchTerm = searchInput.value.toLowerCase();
const filteredTransactions = transactions.filter(transaction => {
return (
transaction.date.toLowerCase().includes(searchTerm) ||
transaction.reason.toLowerCase().includes(searchTerm)
);
});
// Update the transaction list with the filtered results
transactions = filteredTransactions;
updateTransactionList();
}
</script>
</body>
</html>