Below is the complete code with smooth fade‑in animations added and some extra tweaks so that if there are more than three investor cards the layout still works properly. Just copy and paste the entire code into a new HTML file:
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Mini Stock Market Tracker Pro+</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
<style>
/* Global Reset and Basic Styles */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #1a2a6c, #2c3e50);
color: #f5f7fa;
line-height: 1.6;
min-height: 100vh;
}
/* Animation Keyframes */
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
/* Container and Header */
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
header {
text-align: center;
padding: 20px 0;
margin-bottom: 30px;
background: rgba(0, 0, 0, 0.3);
border-radius: 10px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
}
h1 {
color: #ffffff;
margin-bottom: 10px;
font-size: 2.5rem;
text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}
.subtitle {
color: #3498db;
font-size: 1.2rem;
margin-bottom: 20px;
}
/* Sections and Grid Layout */
.section {
background: rgba(30, 40, 70, 0.7);
border-radius: 12px;
padding: 25px;
margin-bottom: 25px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.1);
animation: fadeIn 0.5s ease-out;
}
.section h2 {
color: #3498db;
margin-bottom: 20px;
padding-bottom: 12px;
border-bottom: 2px solid #3498db;
font-size: 1.5rem;
}
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 25px;
}
/* Form Elements */
.form-group {
margin-bottom: 18px;
}
label {
display: block;
margin-bottom: 8px;
font-weight: 600;
color: #bdc3c7;
}
input[type="text"],
input[type="number"],
select {
width: 100%;
padding: 12px 15px;
border: 1px solid #34495e;
border-radius: 6px;
font-size: 16px;
background: rgba(25, 35, 60, 0.7);
color: #ecf0f1;
transition: all 0.3s;
}
input[type="text"]:focus,
input[type="number"]:focus,
select:focus {
outline: none;
border-color: #3498db;
box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.3);
}
input[type="number"] {
text-align: right;
}
/* Buttons */
button {
background: linear-gradient(135deg, #3498db, #1a5276);
color: white;
padding: 12px 24px;
border: none;
border-radius: 6px;
cursor: pointer;
font-size: 16px;
font-weight: 600;
transition: all 0.3s;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
button:hover {
transform: translateY(-2px);
box-shadow: 0 6px 8px rgba(0, 0, 0, 0.15);
background: linear-gradient(135deg, #2980b9, #154360);
}
.button-danger {
background: linear-gradient(135deg, #e74c3c, #922b21);
}
.button-danger:hover {
background: linear-gradient(135deg, #c0392b, #7b241c);
}
.button-group {
display: flex;
gap: 12px;
margin-top: 15px;
}
/* Card Styles */
.card {
background: rgba(25, 35, 60, 0.8);
border: 1px solid rgba(52, 152, 219, 0.3);
border-radius: 10px;
padding: 20px;
margin-bottom: 15px;
animation: fadeIn 0.5s ease-out;
transition: transform 0.3s;
}
.card:hover {
transform: translateY(-5px);
box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
}
.card h3 {
color: #3498db;
margin-bottom: 15px;
font-size: 1.4rem;
display: flex;
align-items: center;
gap: 10px;
}
.card h3 i {
font-size: 1.2rem;
}
/* Ownership Bar */
.ownership-bar {
background-color: #2c3e50;
height: 22px;
border-radius: 11px;
overflow: hidden;
margin: 10px 0;
box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.3);
}
.ownership-fill {
height: 100%;
background: linear-gradient(90deg, #3498db, #2ecc71);
transition: width 0.5s ease;
}
/* Table Styles */
.table {
width: 100%;
border-collapse: collapse;
margin-top: 15px;
background: rgba(30, 40, 70, 0.5);
border-radius: 8px;
overflow: hidden;
animation: fadeIn 0.5s ease-out;
}
.table th {
background: rgba(52, 152, 219, 0.3);
padding: 12px 15px;
text-align: left;
color: #3498db;
font-weight: 600;
}
.table td {
padding: 12px 15px;
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}
.table tr:last-child td {
border-bottom: none;
}
.table tr:hover {
background: rgba(52, 152, 219, 0.1);
}
.positive {
color: #2ecc71;
font-weight: bold;
}
.negative {
color: #e74c3c;
font-weight: bold;
}
/* Summary Stats */
.summary-stats {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
gap: 20px;
margin-top: 25px;
}
.stat-card {
background: linear-gradient(135deg, #1a5276, #1a2a6c);
color: white;
padding: 25px;
border-radius: 10px;
text-align: center;
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2);
transition: transform 0.3s;
animation: fadeIn 0.5s ease-out;
}
.stat-card:hover {
transform: translateY(-5px);
}
.stat-card h4 {
font-size: 16px;
opacity: 0.9;
margin-bottom: 10px;
color: #bdc3c7;
}
.stat-card .value {
font-size: 28px;
font-weight: bold;
}
/* Modal Styles */
.modal {
display: none;
position: fixed;
z-index: 1000;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.7);
align-items: center;
justify-content: center;
}
.modal-content {
background: linear-gradient(135deg, #1a2a6c, #2c3e50);
padding: 30px;
border-radius: 12px;
width: 90%;
max-width: 450px;
box-shadow: 0 15px 30px rgba(0, 0, 0, 0.4);
position: relative;
animation: fadeIn 0.5s ease-out;
}
.close {
color: #bdc3c7;
position: absolute;
top: 15px;
right: 20px;
font-size: 30px;
font-weight: bold;
cursor: pointer;
transition: color 0.3s;
}
.close:hover {
color: #e74c3c;
}
/* Chart Container */
.chart-container {
position: relative;
margin: 20px 0;
height: 200px;
background: rgba(25, 35, 60, 0.5);
border-radius: 8px;
padding: 10px;
animation: fadeIn 0.5s ease-out;
}
/* Available Shares Section */
.available-shares {
background: rgba(25, 35, 60, 0.8);
padding: 20px;
border-radius: 10px;
margin-top: 25px;
animation: fadeIn 0.5s ease-out;
}
.available-shares h3 {
margin-bottom: 15px;
color: #3498db;
font-size: 1.4rem;
padding-bottom: 10px;
border-bottom: 1px solid rgba(52, 152, 219, 0.3);
}
.market-overview {
display: flex;
justify-content: space-between;
align-items: center;
padding: 15px 20px;
background: rgba(25, 35, 60, 0.8);
border-radius: 8px;
margin-bottom: 25px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
animation: fadeIn 0.5s ease-out;
}
.market-overview .total-available {
font-size: 1.4rem;
font-weight: bold;
color: #2ecc71;
}
.market-overview .total-label {
font-size: 1rem;
color: #bdc3c7;
}
.action-section {
display: flex;
gap: 20px;
margin-bottom: 25px;
}
.action-section > div {
flex: 1;
background: rgba(25, 35, 60, 0.8);
border-radius: 8px;
padding: 20px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
animation: fadeIn 0.5s ease-out;
}
.action-section h3 {
color: #3498db;
margin-bottom: 15px;
padding-bottom: 10px;
border-bottom: 1px solid rgba(52, 152, 219, 0.3);
}
.icon {
display: inline-block;
width: 40px;
height: 40px;
background: rgba(52, 152, 219, 0.2);
border-radius: 50%;
text-align: center;
line-height: 40px;
margin-right: 10px;
font-size: 18px;
}
@media (max-width: 768px) {
.grid {
grid-template-columns: 1fr;
}
.action-section {
flex-direction: column;
}
.summary-stats {
grid-template-columns: 1fr;
}
.market-overview {
flex-direction: column;
align-items: flex-start;
}
}
</style>
</head>
<body>
<div class="container">
<header>
<h1><i class="fas fa-chart-line"></i> Mini Stock Market Tracker Pro+</h1>
<p class="subtitle">Track your personal stock portfolio with advanced analytics</p>
</header>
<!-- Market Overview -->
<div class="market-overview">
<div>
<div class="total-label">TOTAL AVAILABLE SHARES</div>
<div class="total-available" id="totalAvailableShares">0</div>
</div>
<div>
<div class="total-label">TOTAL COMPANIES</div>
<div class="total-available" id="totalCompanies">0</div>
</div>
<div>
<div class="total-label">TOTAL INVESTORS</div>
<div class="total-available" id="totalInvestors">0</div>
</div>
</div>
<!-- Action Section -->
<div class="action-section">
<div>
<h3><i class="fas fa-building"></i> Company Management</h3>
<div class="form-group">
<label for="companyName">Company Name</label>
<input type="text" id="companyName" placeholder="Enter company name" />
</div>
<div class="form-group">
<label for="companyValue">Total Company Value ($)</label>
<input type="number" id="companyValue" placeholder="0.00" step="0.01" />
</div>
<div class="form-group">
<label for="totalShares">Total Shares</label>
<input type="number" id="totalShares" placeholder="0" step="1" />
</div>
<div class="form-group">
<label for="sharesForSale">Shares Available for Sale</label>
<input type="number" id="sharesForSale" placeholder="0" step="1" />
</div>
<button onclick="addCompany()"><i class="fas fa-plus-circle"></i> Add Company</button>
</div>
<div>
<h3><i class="fas fa-user-tie"></i> Investor Management</h3>
<div class="form-group">
<label for="investorName">Investor Name</label>
<input type="text" id="investorName" placeholder="Enter investor name" />
</div>
<button onclick="addInvestor()" style="margin-top: 32px;"><i class="fas fa-user-plus"></i> Add Investor</button>
<h3 style="margin-top: 30px;"><i class="fas fa-sync-alt"></i> Update Metrics</h3>
<div class="form-group">
<label for="updateCompany">Select Company</label>
<select id="updateCompany"></select>
</div>
<div class="form-group">
<label for="newValue">New Company Value ($)</label>
<input type="number" id="newValue" placeholder="0.00" step="0.01" />
</div>
<div class="form-group">
<label for="newSharesForSale">New Shares Available</label>
<input type="number" id="newSharesForSale" placeholder="0" step="1" />
</div>
<button onclick="updateCompanyMetrics()"><i class="fas fa-sync"></i> Update Metrics</button>
</div>
</div>
<!-- Trading Section -->
<div class="grid">
<!-- Buy Shares -->
<div class="section">
<h2><i class="fas fa-shopping-cart"></i> Buy Shares</h2>
<div class="form-group">
<label for="buyInvestor">Investor</label>
<select id="buyInvestor"></select>
</div>
<div class="form-group">
<label for="buyCompany">Company</label>
<select id="buyCompany"></select>
</div>
<div class="form-group">
<label for="buyShares">Number of Shares</label>
<input type="number" id="buyShares" placeholder="0" step="1" />
</div>
<button onclick="buyShares()"><i class="fas fa-hand-holding-usd"></i> Buy Shares</button>
</div>
<!-- Sell Shares -->
<div class="section">
<h2><i class="fas fa-money-bill-wave"></i> Sell Shares</h2>
<div class="form-group">
<label for="sellInvestor">Investor</label>
<select id="sellInvestor"></select>
</div>
<div class="form-group">
<label for="sellCompany">Company</label>
<select id="sellCompany"></select>
</div>
<div class="form-group">
<label for="sellShares">Number of Shares</label>
<input type="number" id="sellShares" placeholder="0" step="1" />
</div>
<button onclick="sellShares()" class="button-danger"><i class="fas fa-cash-register"></i> Sell Shares</button>
</div>
</div>
<!-- Portfolio Overview -->
<div class="section">
<h2><i class="fas fa-chart-pie"></i> Portfolio Overview</h2>
<div class="summary-stats">
<div class="stat-card">
<h4>Total Market Cap</h4>
<div class="value" id="totalMarketCap">$0.00</div>
</div>
<div class="stat-card">
<h4>Total Profit</h4>
<div class="value" id="totalProfit">$0.00</div>
</div>
<div class="stat-card">
<h4>Average Growth</h4>
<div class="value" id="avgGrowth">0%</div>
</div>
</div>
</div>
<!-- Companies List -->
<div class="section">
<h2><i class="fas fa-building"></i> Companies</h2>
<div id="companiesList" class="grid"></div>
</div>
<!-- Available Shares List -->
<div class="section available-shares">
<h3><i class="fas fa-coins"></i> Available Shares for Purchase</h3>
<div id="availableSharesList"></div>
</div>
<!-- Investors List -->
<div class="section">
<h2><i class="fas fa-user-tie"></i> Investors</h2>
<div id="investorsList" class="grid"></div>
</div>
</div>
<!-- Delete Confirmation Modal -->
<div id="deleteModal" class="modal">
<div class="modal-content">
<span class="close" onclick="closeModal()">×</span>
<p id="modalMessage" style="font-size: 1.2rem; margin: 20px 0;"></p>
<div class="button-group" style="margin-top: 20px; justify-content: center;">
<button onclick="confirmDelete()" style="flex: 1;">Confirm</button>
<button class="button-danger" onclick="closeModal()" style="flex: 1;">Cancel</button>
</div>
</div>
</div>
<script>
let companies = JSON.parse(localStorage.getItem('companies')) || [];
let investors = JSON.parse(localStorage.getItem('investors')) || [];
let transactions = JSON.parse(localStorage.getItem('transactions')) || [];
let companyCharts = {};
function init() {
updateDropdowns();
renderCompanies();
renderInvestors();
renderAvailableShares();
updateSummaryStats();
updateMarketOverview();
}
function saveData() {
localStorage.setItem('companies', JSON.stringify(companies));
localStorage.setItem('investors', JSON.stringify(investors));
localStorage.setItem('transactions', JSON.stringify(transactions));
}
function refreshUI() {
saveData();
updateDropdowns();
renderCompanies();
renderInvestors();
renderAvailableShares();
updateSummaryStats();
updateMarketOverview();
}
function updateMarketOverview() {
const totalAvailable = companies.reduce((sum, company) => sum + company.sharesForSale, 0);
document.getElementById('totalAvailableShares').textContent = totalAvailable;
document.getElementById('totalCompanies').textContent = companies.length;
document.getElementById('totalInvestors').textContent = investors.length;
}
function addCompany() {
const name = document.getElementById('companyName').value;
const value = parseFloat(document.getElementById('companyValue').value);
const totalShares = parseInt(document.getElementById('totalShares').value);
const sharesForSale = parseInt(document.getElementById('sharesForSale').value) || totalShares;
if (!name || isNaN(value) || isNaN(totalShares) || value <= 0 || totalShares <= 0) {
alert('Please fill all fields with positive values.');
return;
}
const company = {
id: Date.now(),
name,
value,
totalShares,
sharesAvailable: totalShares,
sharesForSale: Math.min(totalShares, sharesForSale),
profit: 0,
growth: 0,
history: [{
date: new Date().toISOString(),
value: value,
profit: 0,
growth: 0
}]
};
companies.push(company);
refreshUI();
document.getElementById('companyName').value = '';
document.getElementById('companyValue').value = '';
document.getElementById('totalShares').value = '';
document.getElementById('sharesForSale').value = '';
}
function addInvestor() {
const name = document.getElementById('investorName').value;
if (!name) {
alert('Please enter investor name');
return;
}
const investor = {
id: Date.now(),
name,
portfolio: {},
};
investors.push(investor);
refreshUI();
document.getElementById('investorName').value = '';
}
function updateCompanyMetrics() {
const companyId = parseInt(document.getElementById('updateCompany').value);
const newValueInput = document.getElementById('newValue').value;
const newSharesForSale = parseInt(document.getElementById('newSharesForSale').value);
if (!companyId) {
alert('Please select a company');
return;
}
const company = companies.find(c => c.id === companyId);
const newValue = parseFloat(newValueInput);
if (!isNaN(newValue) && newValueInput !== '') company.value = newValue;
if (!isNaN(newSharesForSale) && newSharesForSale !== '') {
company.sharesForSale = Math.min(company.totalShares, newSharesForSale);
}
company.history.push({
date: new Date().toISOString(),
value: company.value,
profit: company.profit,
growth: company.growth
});
refreshUI();
document.getElementById('newValue').value = '';
document.getElementById('newSharesForSale').value = '';
}
function buyShares() {
const investorId = parseInt(document.getElementById('buyInvestor').value);
const companyId = parseInt(document.getElementById('buyCompany').value);
const shares = parseInt(document.getElementById('buyShares').value);
if (!investorId || !companyId || isNaN(shares) || shares < 1) {
alert('Please select investor, company, and enter a valid number of shares (minimum 1).');
return;
}
const company = companies.find(c => c.id === companyId);
const investor = investors.find(i => i.id === investorId);
if (shares > company.sharesAvailable) {
alert(`Not enough shares available. Max: ${company.sharesAvailable}`);
return;
}
if (shares > company.sharesForSale) {
alert(`Company only has ${company.sharesForSale} shares available for sale`);
return;
}
company.sharesAvailable -= shares;
company.sharesForSale -= shares;
if (!investor.portfolio[companyId]) {
investor.portfolio[companyId] = 0;
}
investor.portfolio[companyId] += shares;
transactions.push({
id: Date.now(),
investorId,
companyId,
shares,
price: company.value / company.totalShares,
date: new Date().toISOString()
});
refreshUI();
document.getElementById('buyShares').value = '';
}
function sellShares() {
const investorId = parseInt(document.getElementById('sellInvestor').value);
const companyId = parseInt(document.getElementById('sellCompany').value);
const shares = parseInt(document.getElementById('sellShares').value);
if (!investorId || !companyId || isNaN(shares) || shares < 1) {
alert('Please select investor, company, and enter a valid number of shares (minimum 1).');
return;
}
const company = companies.find(c => c.id === companyId);
const investor = investors.find(i => i.id === investorId);
const investorShares = investor.portfolio[companyId] || 0;
if (shares > investorShares) {
alert(`Investor only owns ${investorShares} shares in ${company.name}.`);
return;
}
investor.portfolio[companyId] -= shares;
if (investor.portfolio[companyId] === 0) {
delete investor.portfolio[companyId];
}
company.sharesAvailable += shares;
company.sharesForSale += shares;
transactions.push({
id: Date.now(),
investorId,
companyId,
shares: -shares,
price: company.value / company.totalShares,
date: new Date().toISOString()
});
refreshUI();
document.getElementById('sellShares').value = '';
}
function deleteCompany(companyId) {
const company = companies.find(c => c.id === companyId);
const hasSharesOwned = investors.some(investor => investor.portfolio[companyId]);
if (hasSharesOwned) {
alert(`Cannot delete ${company.name} - investors still own shares. They must sell first.`);
return;
}
document.getElementById('modalMessage').textContent =
`Are you sure you want to delete ${company.name}? This cannot be undone.`;
document.getElementById('deleteModal').style.display = 'flex';
window.deleteTarget = { type: 'company', id: companyId };
}
function deleteInvestor(investorId) {
const investor = investors.find(i => i.id === investorId);
document.getElementById('modalMessage').textContent =
`Delete ${investor.name}? Their shares will be returned to the market automatically.`;
document.getElementById('deleteModal').style.display = 'flex';
window.deleteTarget = { type: 'investor', id: investorId };
}
function confirmDelete() {
const target = window.deleteTarget;
if (!target) return;
if (target.type === 'company') {
companies = companies.filter(c => c.id !== target.id);
} else if (target.type === 'investor') {
const investor = investors.find(i => i.id === target.id);
Object.entries(investor.portfolio).forEach(([companyId, shares]) => {
const company = companies.find(c => c.id === parseInt(companyId));
if (company) {
company.sharesAvailable += shares;
company.sharesForSale += shares;
}
});
investors = investors.filter(i => i.id !== target.id);
}
refreshUI();
closeModal();
}
function closeModal() {
document.getElementById('deleteModal').style.display = 'none';
window.deleteTarget = null;
}
function renderCompanies() {
const container = document.getElementById('companiesList');
container.innerHTML = '';
if (companies.length === 0) {
container.innerHTML = '<div class="card"><p>No companies added yet. Add your first company to get started.</p></div>';
return;
}
companies.forEach(company => {
const totalOwnedShares = investors.reduce((total, investor) => {
return total + (investor.portfolio[company.id] || 0);
}, 0);
const ownershipPercentage = (totalOwnedShares / company.totalShares * 100).toFixed(2);
const sharePrice = (company.value / company.totalShares).toFixed(2);
const companyDiv = document.createElement('div');
companyDiv.className = 'card';
companyDiv.innerHTML = `
<h3><i class="fas fa-building"></i> ${company.name}</h3>
<p><strong>Total Value:</strong> $${company.value.toLocaleString()}</p>
<p><strong>Share Price:</strong> $${sharePrice}</p>
<p><strong>Shares Owned:</strong> ${totalOwnedShares}/${company.totalShares} (${ownershipPercentage}%)</p>
<p><strong>Available for Sale:</strong> ${company.sharesForSale}</p>
<p><strong>Profit:</strong> $${company.profit.toLocaleString()}
<span class="${company.profit >= 0 ? 'positive' : 'negative'}">
${company.profit >= 0 ? '↑' : '↓'}
</span>
</p>
<p><strong>Growth:</strong> ${company.growth}%
<span class="${company.growth >= 0 ? 'positive' : 'negative'}">
${company.growth >= 0 ? '↑' : '↓'}
</span>
</p>
<div class="ownership-bar">
<div class="ownership-fill" style="width: ${ownershipPercentage}%"></div>
</div>
<div class="chart-container">
<canvas id="chart-${company.id}"></canvas>
</div>
<div class="button-group">
<button class="button-danger" onclick="deleteCompany(${company.id})">
<i class="fas fa-trash-alt"></i> Delete Company
</button>
</div>
`;
container.appendChild(companyDiv);
if (company.history.length > 0) {
createCompanyChart(company);
}
});
}
function createCompanyChart(company) {
const ctx = document.getElementById(`chart-${company.id}`);
const labels = company.history.map(entry => new Date(entry.date).toLocaleDateString());
const profitData = company.history.map(entry => entry.profit);
const valueData = company.history.map(entry => entry.value);
if (companyCharts[company.id]) {
companyCharts[company.id].destroy();
}
companyCharts[company.id] = new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: [
{
label: 'Profit',
data: profitData,
borderColor: profitData[profitData.length - 1] >= 0 ? '#2ecc71' : '#e74c3c',
backgroundColor: 'rgba(46, 204, 113, 0.1)',
fill: true,
tension: 0.3
},
{
label: 'Value',
data: valueData,
borderColor: '#3498db',
backgroundColor: 'rgba(52, 152, 219, 0.1)',
fill: true,
tension: 0.3
}
]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
display: false
}
},
scales: {
y: {
beginAtZero: true,
grid: {
color: 'rgba(255, 255, 255, 0.1)'
},
ticks: {
color: '#bdc3c7'
}
},
x: {
grid: {
color: 'rgba(255, 255, 255, 0.1)'
},
ticks: {
color: '#bdc3c7'
}
}
}
}
});
}
function renderInvestors() {
const container = document.getElementById('investorsList');
container.innerHTML = '';
if (investors.length === 0) {
container.innerHTML = '<div class="card"><p>No investors added yet. Add your first investor to get started.</p></div>';
return;
}
investors.forEach(investor => {
const investorDiv = document.createElement('div');
investorDiv.className = 'card';
let portfolioHTML = '<table class="table"><tr><th>Company</th><th>Shares</th><th>Value</th><th>Ownership %</th></tr>';
let totalValue = 0;
let hasPortfolio = false;
Object.entries(investor.portfolio).forEach(([companyId, shares]) => {
const company = companies.find(c => c.id === parseInt(companyId));
if (company && shares > 0) {
hasPortfolio = true;
const value = shares * (company.value / company.totalShares);
const ownership = (shares / company.totalShares * 100).toFixed(2);
totalValue += value;
portfolioHTML += `
<tr>
<td>${company.name}</td>
<td>${shares}</td>
<td>$${value.toFixed(2)}</td>
<td>${ownership}%</td>
</tr>
`;
}
});
if (!hasPortfolio) {
portfolioHTML += '<tr><td colspan="4">No investments yet.</td></tr>';
}
portfolioHTML += '</table>';
investorDiv.innerHTML = `
<h3><i class="fas fa-user-tie"></i> ${investor.name}</h3>
<p><strong>Total Portfolio Value:</strong> $${totalValue.toFixed(2)}</p>
${portfolioHTML}
<div class="button-group">
<button class="button-danger" onclick="deleteInvestor(${investor.id})">
<i class="fas fa-user-minus"></i> Delete Investor
</button>
</div>
`;
container.appendChild(investorDiv);
});
}
function renderAvailableShares() {
const container = document.getElementById('availableSharesList');
if (companies.length === 0) {
container.innerHTML = '<p>No companies with shares available.</p>';
return;
}
let html = '<table class="table">';
html += '<tr><th>Company</th><th>Available Shares</th><th>Share Price</th><th>Total Value</th></tr>';
companies.forEach(company => {
const sharePrice = (company.value / company.totalShares).toFixed(2);
const totalValue = (company.sharesForSale * sharePrice).toFixed(2);
html += `
<tr>
<td>${company.name}</td>
<td>${company.sharesForSale}</td>
<td>$${sharePrice}</td>
<td>$${totalValue}</td>
</tr>
`;
});
html += '</table>';
container.innerHTML = html;
}
function updateDropdowns() {
const buyInvestor = document.getElementById('buyInvestor');
const sellInvestor = document.getElementById('sellInvestor');
const buyCompany = document.getElementById('buyCompany');
const sellCompany = document.getElementById('sellCompany');
const updateCompany = document.getElementById('updateCompany');
[buyInvestor, sellInvestor].forEach(dropdown => {
dropdown.innerHTML = '<option value="">Select Investor</option>';
investors.forEach(investor => {
dropdown.innerHTML += `<option value="${investor.id}">${investor.name}</option>`;
});
});
[buyCompany, sellCompany, updateCompany].forEach(dropdown => {
dropdown.innerHTML = '<option value="">Select Company</option>';
companies.forEach(company => {
dropdown.innerHTML += `<option value="${company.id}">${company.name}</option>`;
});
});
}
function updateSummaryStats() {
const totalMarketCap = companies.reduce((sum, company) => sum + company.value, 0);
const totalProfit = companies.reduce((sum, company) => sum + company.profit, 0);
const avgGrowth = companies.length > 0
? (companies.reduce((sum, company) => sum + company.growth, 0) / companies.length).toFixed(2)
: 0;
document.getElementById('totalMarketCap').textContent = `$${totalMarketCap.toLocaleString()}`;
document.getElementById('totalProfit').textContent = `$${totalProfit.toLocaleString()}`;
document.getElementById('avgGrowth').textContent = `${avgGrowth}%`;
}
// Initialize on load
init();
</script>
</body>
</html>
This code is self-contained and should work as expected when pasted into a blank HTML file. Enjoy your newly animated and well-formatted mini stock market tracker!