-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
107 lines (90 loc) · 4.38 KB
/
app.js
File metadata and controls
107 lines (90 loc) · 4.38 KB
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
let users = [];
let transactions = [];
let transactionHistory = [];
// Function to create users
function createUsers() {
const numUsers = parseInt(document.getElementById('numUsers').value);
if (isNaN(numUsers) || numUsers <= 0) {
document.getElementById('userStatus').innerText = 'Please enter a valid number of users.';
return;
}
users = Array.from({ length: numUsers }, (_, i) => `User ${i + 1}`);
document.getElementById('userStatus').innerText = `${numUsers} users created.`;
console.log("Users created:", users); // Debug
}
// Function to add a transaction
function addTransaction() {
const lender = parseInt(document.getElementById('lender').value);
const borrower = parseInt(document.getElementById('borrower').value);
const amount = parseInt(document.getElementById('amount').value);
if (isNaN(lender) || isNaN(borrower) || isNaN(amount) || lender < 1 || borrower < 1 || amount <= 0 || lender === borrower) {
document.getElementById('transactionStatus').innerText = 'Please enter valid lender, borrower, and amount.';
return;
}
transactionHistory.push([...transactions]); // Save state for undo
transactions.push({ lender, borrower, amount });
document.getElementById('transactionStatus').innerText = `Transaction added: User ${lender} lends ${amount} to User ${borrower}`;
console.log("Transaction added:", transactions); // Debug
renderGraph(transactions); // Update the transaction graph immediately after adding
}
// Function to display current transactions on the graph
function displayTransactions() {
renderGraph(transactions); // Display all current transactions in the graph
document.getElementById('transactionStatus').innerText = 'Displaying current transactions.';
console.log("Displaying transactions:", transactions); // Debug
}
// Function to minimize transactions and display as a graph
function minimizeTransactions() {
let netBalances = Array(users.length).fill(0);
// Calculate net balance for each user
transactions.forEach(({ lender, borrower, amount }) => {
netBalances[lender - 1] += amount;
netBalances[borrower - 1] -= amount;
});
let creditors = [];
let debtors = [];
let minimizedTransactions = [];
// Separate creditors and debtors
netBalances.forEach((balance, index) => {
if (balance > 0) creditors.push({ id: index + 1, amount: balance });
else if (balance < 0) debtors.push({ id: index + 1, amount: -balance });
});
// Minimize transactions by matching creditors with debtors
let i = 0, j = 0;
while (i < creditors.length && j < debtors.length) {
const amount = Math.min(creditors[i].amount, debtors[j].amount);
minimizedTransactions.push({ lender: creditors[i].id, borrower: debtors[j].id, amount });
creditors[i].amount -= amount;
debtors[j].amount -= amount;
if (creditors[i].amount === 0) i++;
if (debtors[j].amount === 0) j++;
}
// Render minimized transactions as a graph
renderGraph(minimizedTransactions);
document.getElementById('transactionStatus').innerText = 'Displaying minimized transactions.';
}
// Function to undo the last transaction
function undoLastTransaction() {
if (transactionHistory.length > 0) {
transactions = transactionHistory.pop();
renderGraph(transactions); // Display updated transactions in graph
document.getElementById('transactionStatus').innerText = 'Last transaction undone.';
} else {
document.getElementById('transactionStatus').innerText = 'No transactions to undo.';
}
}
// Function to render graph of transactions using vis.js
function renderGraph(data) {
const nodes = users.map((user, index) => ({ id: index + 1, label: user }));
const edges = data.map(transaction => ({
from: transaction.lender,
to: transaction.borrower,
label: `${transaction.amount}`,
arrows: 'to'
}));
const container = document.getElementById('graph');
const networkData = { nodes: new vis.DataSet(nodes), edges: new vis.DataSet(edges) };
const options = { physics: false, edges: { color: 'gray', font: { align: 'middle' } } };
new vis.Network(container, networkData, options);
console.log("Graph rendered with data:", data); // Debug
}