class Item { constructor(name, length, width, weight) { this.name = name; this.length = parseFloat(length); this.width = parseFloat(width); this.weight = parseFloat(weight) || 0; this.area = this.length * this.width; } } class SpaceAllocator { constructor() { this.containerWidth = 600; this.containerHeight = 250; this.items = []; this.canvas = document.getElementById('space-canvas'); this.ctx = this.canvas.getContext('2d'); this.scale = 1; this.offsetX = 0; this.offsetY = 0; this.initElements(); this.initEventListeners(); this.resizeCanvas(); this.updateUI(); } initElements() { this.containerWidthInput = document.getElementById('container-width'); this.containerHeightInput = document.getElementById('container-height'); this.containerWeightInput = document.getElementById('container-weight'); this.itemNameInput = document.getElementById('item-name'); this.itemLengthInput = document.getElementById('item-length'); this.itemWidthInput = document.getElementById('item-width'); this.itemWeightInput = document.getElementById('item-weight'); this.itemQuantityInput = document.getElementById('item-quantity'); this.addItemBtn = document.getElementById('add-item-btn'); this.removeItemBtn = document.getElementById('remove-item-btn'); this.updateContainerBtn = document.getElementById('update-container-btn'); this.clearAllBtn = document.getElementById('clear-all-btn'); this.itemsTable = document.getElementById('items-table'); this.spaceStats = document.getElementById('space-stats'); this.visualizationContainer = document.getElementById('visualization-container'); } initEventListeners() { // Set default container dimensions this.containerWidthInput.value = this.containerWidth; this.containerHeightInput.value = this.containerHeight; // Button events this.addItemBtn.addEventListener('click', () => this.addItem()); this.removeItemBtn.addEventListener('click', () => this.removeSelectedItem()); this.updateContainerBtn.addEventListener('click', () => this.updateContainer()); this.clearAllBtn.addEventListener('click', () => this.clearAll()); // Allow adding items with Enter key this.itemNameInput.addEventListener('keypress', (e) => { if (e.key === 'Enter') this.addItem(); }); this.itemLengthInput.addEventListener('keypress', (e) => { if (e.key === 'Enter') this.addItem(); }); this.itemWidthInput.addEventListener('keypress', (e) => { if (e.key === 'Enter') this.addItem(); }); this.itemWeightInput.addEventListener('keypress', (e) => { if (e.key === 'Enter') this.addItem(); }); this.itemQuantityInput.addEventListener('keypress', (e) => { if (e.key === 'Enter') this.addItem(); }); // Handle window resize window.addEventListener('resize', () => { this.resizeCanvas(); this.drawContainer(); }); // Initialize visualization this.resizeCanvas(); this.drawContainer(); } resizeCanvas() { const container = this.visualizationContainer; this.canvas.width = container.clientWidth; this.canvas.height = container.clientHeight; } addItem() { const name = this.itemNameInput.value.trim() || `Item ${this.items.length + 1}`; const length = parseFloat(this.itemLengthInput.value); const width = parseFloat(this.itemWidthInput.value); const weight = parseFloat(this.itemWeightInput.value); const quantity = parseInt(this.itemQuantityInput.value) || 1; if (isNaN(length) || isNaN(width) || length <= 0 || width <= 0 || quantity <= 0) { alert('Please enter valid positive numbers for length and width.'); return; } for (let i = 0; i < quantity; i++) { const itemName = quantity > 1 ? `${name} #${i+1}` : name; const item = new Item(itemName, length, width, weight); this.items.push(item); } // Clear inputs this.itemNameInput.value = ''; this.itemLengthInput.value = ''; this.itemWidthInput.value = ''; this.itemWeightInput.value = ''; this.itemQuantityInput.value = '1'; this.updateUI(); } removeSelectedItem() { const selectedRow = this.itemsTable.querySelector('tr.selected'); if (!selectedRow) { alert('Please select an item to remove.'); return; } const itemName = selectedRow.getAttribute('data-name'); this.items = this.items.filter(item => item.name !== itemName); this.updateUI(); } updateContainer() { const width = parseFloat(this.containerWidthInput.value); const height = parseFloat(this.containerHeightInput.value); if (isNaN(width) || isNaN(height) || width <= 0 || height <= 0) { alert('Please enter valid positive numbers for container dimensions.'); return; } this.containerWidth = width; this.containerHeight = height; this.updateUI(); } clearAll() { if (confirm('Are you sure you want to clear all items?')) { this.items = []; this.updateUI(); } } updateUI() { this.updateItemsTable(); this.updateSpaceStats(); this.drawContainer(); } updateItemsTable() { this.itemsTable.innerHTML = ''; this.items.forEach(item => { const row = document.createElement('tr'); row.setAttribute('data-name', item.name); row.innerHTML = `