-
Notifications
You must be signed in to change notification settings - Fork 0
/
success.html
157 lines (140 loc) · 5.22 KB
/
success.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
<!DOCTYPE html>
<html>
<head>
<title>Balance Page</title>
<script>
// Get the username from the URL query parameters
function getUsernameFromURL() {
var urlParams = new URLSearchParams(window.location.search);
return urlParams.get('username');
}
// Fetch the user's balance and display it
function fetchBalance() {
var username = getUsernameFromURL();
// Simulating fetching balance using JavaScript
fetch('user.txt')
.then(response => response.text())
.then(data => {
var users = data.split('\n');
for (var i = 0; i < users.length; i++) {
var userDetails = users[i].split(':');
if (userDetails[0] === username) {
document.getElementById("balance").innerHTML = "Your balance is: $" + userDetails[1];
break;
}
}
})
.catch(error => {
console.error('Error:', error);
});
}
// Send money to a user
function sendMoney() {
var username = getUsernameFromURL();
var recipient = document.getElementById("recipient").value;
var amount = document.getElementById("amount").value;
// Simulating sending money using JavaScript
fetch('user.txt')
.then(response => response.text())
.then(data => {
var users = data.split('\n');
var updatedUsers = [];
var senderBalanceUpdated = false;
for (var i = 0; i < users.length; i++) {
var userDetails = users[i].split(':');
if (userDetails[0] === username) {
var balance = parseInt(userDetails[1]);
if (balance >= amount) {
balance -= amount;
document.getElementById("balance").innerHTML = "Your balance is: $" + balance;
senderBalanceUpdated = true;
} else {
console.log("Insufficient balance");
}
} else if (userDetails[0] === recipient) {
var balance = parseInt(userDetails[1]);
balance += parseInt(amount);
}
updatedUsers.push(userDetails.join(':'));
}
if (senderBalanceUpdated) {
// Update the user.txt file with the updated balances
var updatedData = updatedUsers.join('\n');
fetch('data:text/plain;charset=utf-8,' + encodeURIComponent(updatedData))
.then(response => {
console.log("User balances updated successfully");
})
.catch(error => {
console.error('Error:', error);
});
}
})
.catch(error => {
console.error('Error:', error);
});
// Clear the input fields
document.getElementById("recipient").value = "";
document.getElementById("amount").value = "";
}
// Add money to the user's balance
function addMoney() {
var username = getUsernameFromURL();
var amount = parseInt(document.getElementById("amount").value);
// Simulating adding money using JavaScript
fetch('user.txt')
.then(response => response.text())
.then(data => {
var users = data.split('\n');
var updatedUsers = [];
var balanceUpdated = false;
for (var i = 0; i < users.length; i++) {
var userDetails = users[i].split(':');
if (userDetails[0] === username) {
var balance = parseInt(userDetails[1]);
balance += amount;
document.getElementById("balance").innerHTML = "Your balance is: $" + balance;
balanceUpdated = true;
}
updatedUsers.push(userDetails.join(':'));
}
if (balanceUpdated) {
// Update the user.txt file with the updated balance
var updatedData = updatedUsers.join('\n');
fetch('data:text/plain;charset=utf-8,' + encodeURIComponent(updatedData))
.then(response => {
console.log("User balance updated successfully");
})
.catch(error => {
console.error('Error:', error);
});
}
})
.catch(error => {
console.error('Error:', error);
});
// Clear the input field
document.getElementById("amount").value = "";
}
</script>
</head>
<body onload="fetchBalance()">
<h1>Balance Page</h1>
<div id="balance"></div>
<h2>Send Money</h2>
<div>
<label for="recipient">Recipient:</label>
<input type="text" id="recipient" placeholder="Recipient's username">
</div>
<div>
<label for="amount">Amount:</label>
<input type="text" id="amount" placeholder="Amount to send">
</div>
<button onclick="sendMoney()">Send</button>
<h2>Add Money</h2>
<div>
<label for="amount">Amount:</label>
<input type="text" id="amount" placeholder="Amount to add">
</div>
<button onclick="addMoney()">Add</button>
</body>
</html>