fix ui perfectly let balance = 1000; let correctPIN =… · The Chat Archive
Explore gpt-4o-2024-08-06 · OpenAI WildChat-4.8M (AI2)fix ui perfectly let balance = 1000; let correctPIN = "1234"; let enteredPIN = ""; let… Save Share Download ▾ Reportfix ui perfectly
let balance = 1000;
let correctPIN = "1234";
let enteredPIN = "";
let currentScreen = "PIN"; // PIN, MENU, VIEW, DEPOSIT, WITHDRAW, HISTORY, EXIT
let displayText;
let keypadButtons = [];
let actionButtons = [];
let inputField, submitButton;
const ATM_WIDTH = 500;
const ATM_HEIGHT = 700;
const SCREEN_WIDTH = 400;
const SCREEN_HEIGHT = 200;
let transactionHistory = [];
function setup() {
createCanvas(ATM_WIDTH, ATM_HEIGHT);
background('#1e272e');
drawATMStructure();
createDisplay();
createKeypad();
createActionButtons();
updateDisplay();
}
function draw() {
// No continuous drawing needed
}
// Draw ATM body and screen
function drawATMStructure() {
// ATM Body is the canvas background
fill('#485460');
noStroke();
rect(0, 0, ATM_WIDTH, ATM_HEIGHT, 20);
}
// Create Display Screen
function createDisplay() {
displayText = createDiv('Enter PIN:');
displayText.class('display');
displayText.position((width - SCREEN_WIDTH) / 2, 50);
displayText.size(SCREEN_WIDTH, SCREEN_HEIGHT);
displayText.style('background-color', '#dcdde1');
displayText.style('border-radius', '10px');
displayText.style('box-shadow', 'inset 0 0 10px rgba(0,0,0,0.2)');
displayText.style('padding', '20px');
displayText.style('overflow', 'auto');
}
// Update Display based on current screen
function updateDisplay(message = "") {
if (currentScreen === "PIN") {
displayText.html('Enter PIN:');
removeInputElements();
showKeypadButtons();
hideActionButtons();
} else if (currentScreen === "MENU") {
displayText.html( <h2>Main Menu</h2> <ul style="list-style: none; padding: 0; font-size: 18px;"> <li>1. View Balance</li> <li>2. Deposit Funds</li> <li>3. Withdraw Funds</li> <li>4. Transaction History</li> <li>5. Exit</li> </ul> );
removeInputElements();
showActionButtons();
hideKeypadButtons();
} else if (currentScreen === "VIEW") {
displayText.html(`Current Balance$${balance.toFixed(2)}
); removeInputElements(); showActionButtons(); hideKeypadButtons(); } else if (currentScreen === "DEPOSIT") { displayText.html(
Deposit Funds
Enter amount to deposit:
); createInputTransaction("DEPOSIT"); showKeypadButtons(); hideActionButtons(); } else if (currentScreen === "WITHDRAW") { displayText.html(
Withdraw Funds
Enter amount to withdraw:
); createInputTransaction("WITHDRAW"); showKeypadButtons(); hideActionButtons(); } else if (currentScreen === "HISTORY") { updateDisplayHistory(); hideKeypadButtons(); showActionButtons(); } else if (currentScreen === "EXIT") { displayText.html(
Thank You!
Transaction complete.
); removeInputElements(); hideKeypadButtons(); hideActionButtons(); setTimeout(resetATM, 3000); } else if (message !== "") { displayText.html(
Assistant2024-11-02 15:08 UTC Copy response
Assistant2024-11-02 15:09 UTC Copy response
Copy first prompt
Other models, same or similar prompt Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Details
Model gpt-4o-2024-08-06
Provider OpenAI
Model provenance As recorded by source dataset
Turns 2 (4 messages)
Language en
Conversation date Nov 2, 2024
Added to archive Nov 16, 2026
ID 01m7h4240hz6thkbz2bj78c8c4Source & license Imported from WildChat-4.8M (AI2) (source ). Imported records earn no Research Points.
Upstream record: 9b568bf0aa37d288cf43546226b021c4
Zhao et al., WildChat: 1M ChatGPT Interaction Logs in the Wild (ICLR 2024). Contains information from WildChat-4.8M, made available under the ODC Attribution License.
License: ODC-BY-1.0
Dataset releases Not yet included in a published release.
// Create Numeric Keypad
function createKeypad() {
let buttonSize = 60;
let startX = (width - (3 * buttonSize + 40)) / 2;
let startY = 300;
let spacing = 20;
for (let i = 1; i <= 9; i++) {
let btn = createButton(i.toString());
btn.size(buttonSize, buttonSize);
btn.style('background-color', '#7f8fa6');
btn.style('color', '#fff');
btn.style('border', 'none');
btn.style('border-radius', '10px');
btn.style('font-size', '20px');
btn.position(startX + ((i-1)%3)(buttonSize + spacing), startY + floor((i-1)/3) (buttonSize + spacing));
btn.mousePressed(() => keypadPress(i.toString()));
btn.mouseOver(() => btn.style('background-color', '#718093'));
btn.mouseOut(() => btn.style('background-color', '#7f8fa6'));
keypadButtons.push(btn);
}
// Zero Button
let zeroBtn = createButton('0');
zeroBtn.size(buttonSize, buttonSize);
zeroBtn.style('background-color', '#7f8fa6');
zeroBtn.style('color', '#fff');
zeroBtn.style('border', 'none');
zeroBtn.style('border-radius', '10px');
zeroBtn.style('font-size', '20px');
zeroBtn.position(startX + buttonSize + spacing, startY + 3*(buttonSize + spacing));
zeroBtn.mousePressed(() => keypadPress('0'));
zeroBtn.mouseOver(() => zeroBtn.style('background-color', '#718093'));
zeroBtn.mouseOut(() => zeroBtn.style('background-color', '#7f8fa6'));
keypadButtons.push(zeroBtn);
// Cancel Button
let cancelBtn = createButton('Cancel');
cancelBtn.size(buttonSize + 40, buttonSize);
cancelBtn.style('background-color', '#e74c3c');
cancelBtn.style('color', '#fff');
cancelBtn.style('border', 'none');
cancelBtn.style('border-radius', '10px');
cancelBtn.style('font-size', '16px');
cancelBtn.position(startX, startY + 3*(buttonSize + spacing));
cancelBtn.mousePressed(cancelAction);
cancelBtn.mouseOver(() => cancelBtn.style('background-color', '#c0392b'));
cancelBtn.mouseOut(() => cancelBtn.style('background-color', '#e74c3c'));
keypadButtons.push(cancelBtn);
// Enter Button
let enterBtn = createButton('Enter');
enterBtn.size(buttonSize + 40, buttonSize);
enterBtn.style('background-color', '#2ecc71');
enterBtn.style('color', '#fff');
enterBtn.style('border', 'none');
enterBtn.style('border-radius', '10px');
enterBtn.style('font-size', '16px');
enterBtn.position(startX + 2*(buttonSize + spacing), startY + 3*(buttonSize + spacing));
enterBtn.mousePressed(enterAction);
enterBtn.mouseOver(() => enterBtn.style('background-color', '#27ae60'));
enterBtn.mouseOut(() => enterBtn.style('background-color', '#2ecc71'));
keypadButtons.push(enterBtn);
// Handle Keypad Press
function keypadPress(value) {
if (currentScreen === "PIN") {
if (enteredPIN.length < 4) {
enteredPIN += value;
updateDisplay('*'.repeat(enteredPIN.length));
}
} else if (currentScreen === "DEPOSIT" || currentScreen === "WITHDRAW") {
if (!inputField.value()) {
inputField.value(value);
} else {
inputField.value(inputField.value() + value);
}
}
}
// Create Action Buttons for Menu
function createActionButtons() {
// View Balance
let viewBtn = createButton('View Balance');
viewBtn.size(150, 50);
viewBtn.style('background-color', '#3498db');
viewBtn.style('color', '#fff');
viewBtn.style('border', 'none');
viewBtn.style('border-radius', '10px');
viewBtn.style('font-size', '16px');
viewBtn.position((width/2) - 180, 550);
viewBtn.mousePressed(() => {
currentScreen = "VIEW";
updateDisplay();
});
actionButtons.push(viewBtn);
// Deposit
let depositBtn = createButton('Deposit');
depositBtn.size(150, 50);
depositBtn.style('background-color', '#8e44ad');
depositBtn.style('color', '#fff');
depositBtn.style('border', 'none');
depositBtn.style('border-radius', '10px');
depositBtn.style('font-size', '16px');
depositBtn.position((width/2) - 180, 620);
depositBtn.mousePressed(() => {
currentScreen = "DEPOSIT";
updateDisplay();
});
actionButtons.push(depositBtn);
// Withdraw
let withdrawBtn = createButton('Withdraw');
withdrawBtn.size(150, 50);
withdrawBtn.style('background-color', '#e67e22');
withdrawBtn.style('color', '#fff');
withdrawBtn.style('border', 'none');
withdrawBtn.style('border-radius', '10px');
withdrawBtn.style('font-size', '16px');
withdrawBtn.position((width/2) - 10, 550);
withdrawBtn.mousePressed(() => {
currentScreen = "WITHDRAW";
updateDisplay();
});
actionButtons.push(withdrawBtn);
// Transaction History
let historyBtn = createButton('History');
historyBtn.size(150, 50);
historyBtn.style('background-color', '#f1c40f');
historyBtn.style('color', '#fff');
historyBtn.style('border', 'none');
historyBtn.style('border-radius', '10px');
historyBtn.style('font-size', '16px');
historyBtn.position((width/2) - 10, 620);
historyBtn.mousePressed(() => {
currentScreen = "HISTORY";
updateDisplay();
});
actionButtons.push(historyBtn);
// Exit
let exitBtn = createButton('Exit');
exitBtn.size(150, 50);
exitBtn.style('background-color', '#e74c3c');
exitBtn.style('color', '#fff');
exitBtn.style('border', 'none');
exitBtn.style('border-radius', '10px');
exitBtn.style('font-size', '16px');
exitBtn.position((width/2) + 160, 550);
exitBtn.mousePressed(() => {
currentScreen = "EXIT";
updateDisplay();
});
actionButtons.push(exitBtn);
// Style Action Buttons
function styleActionButton(button, color) {
button.style('background-color', color);
button.style('color', '#fff');
button.style('border', 'none');
button.style('border-radius', '10px');
button.style('font-size', '16px');
button.style('cursor', 'pointer');
}
// Show Keypad Buttons
function showKeypadButtons() {
for (let btn of keypadButtons) {
btn.show();
}
}
// Hide Keypad Buttons
function hideKeypadButtons() {
for (let btn of keypadButtons) {
btn.hide();
}
}
// Show Action Buttons
function showActionButtons() {
for (let btn of actionButtons) {
btn.show();
}
}
// Hide Action Buttons
function hideActionButtons() {
for (let btn of actionButtons) {
btn.hide();
}
}
// Create Input Field for Transactions
function createInputTransaction(type) {
if (!inputField) {
inputField = createInput('');
inputField.position((width - 200) / 2, 270);
inputField.size(200, 40);
inputField.attribute('readonly', 'readonly');
inputField.style('text-align', 'center');
inputField.style('font-size', '18px');
inputField.style('border', '2px solid #bdc3c7');
inputField.style('border-radius', '5px');
}
showKeypadButtons();
hideActionButtons();
}
// Remove Input Elements
function removeInputElements() {
if (inputField) {
inputField.remove();
inputField = null;
}
}
// Handle Deposit
function handleDeposit() {
let amount = parseFloat(inputField.value());
if (isNaN(amount) || amount <= 0) {
updateDisplay("Invalid amount. Try again.");
} else {
balance += amount;
transactionHistory.push(Deposited $${amount.toFixed(2)});
updateDisplay(Successfully deposited $${amount.toFixed(2)}.);
}
currentScreen = "MENU";
removeInputElements();
showActionButtons();
}
// Handle Withdraw
function handleWithdraw() {
let amount = parseFloat(inputField.value());
if (isNaN(amount) || amount <= 0) {
updateDisplay("Invalid amount. Try again.");
} else if (amount > balance) {
updateDisplay("Insufficient funds.");
} else {
balance -= amount;
transactionHistory.push(Withdrew $${amount.toFixed(2)});
updateDisplay(Successfully withdrew $${amount.toFixed(2)}.);
}
currentScreen = "MENU";
removeInputElements();
showActionButtons();
}
// Update Display for Transaction History
function updateDisplayHistory() {
if (transactionHistory.length === 0) {
displayText.html(<h2>Transaction History</h2><p>No transactions yet.</p>);
} else {
let historyHTML = <h2>Transaction History</h2><ul style="list-style: disc; padding-left: 20px;">;
for (let transaction of transactionHistory) {
historyHTML += <li>${transaction}</li>;
}
historyHTML += </ul>;
displayText.html(historyHTML);
}
// Create Back Button if not exists
if (!actionButtons.some(btn => btn.html() === 'Back')) {
let backBtn = createButton('Back');
backBtn.size(150, 50);
backBtn.style('background-color', '#95a5a6');
backBtn.style('color', '#fff');
backBtn.style('border', 'none');
backBtn.style('border-radius', '10px');
backBtn.style('font-size', '16px');
backBtn.position((width/2) + 160, 620);
backBtn.mousePressed(() => {
currentScreen = "MENU";
updateDisplay();
backBtn.hide();
});
backBtn.mouseOver(() => backBtn.style('background-color', '#7f8c8d'));
backBtn.mouseOut(() => backBtn.style('background-color', '#95a5a6'));
actionButtons.push(backBtn);
backBtn.show();
}
}
// Handle Cancel Action
function cancelAction() {
if (currentScreen === "PIN") {
enteredPIN = "";
updateDisplay();
} else if (currentScreen === "DEPOSIT" || currentScreen === "WITHDRAW") {
currentScreen = "MENU";
updateDisplay();
removeInputElements();
showActionButtons();
hideKeypadButtons();
}
}
// Handle Enter Action
function enterAction() {
if (currentScreen === "PIN") {
if (enteredPIN === correctPIN) {
currentScreen = "MENU";
updateDisplay();
enteredPIN = "";
} else {
updateDisplay("Incorrect PIN. Try again.");
enteredPIN = "";
}
} else if (currentScreen === "DEPOSIT") {
handleDeposit();
} else if (currentScreen === "WITHDRAW") {
handleWithdraw();
}
}
// Reset ATM to Initial State
function resetATM() {
currentScreen = "PIN";
updateDisplay();
showKeypadButtons();
hideActionButtons();
}
// Handle Keypad Input via Keyboard
function keyTyped() {
if (inputField && key >= '0' && key <= '9') {
keypadPress(key);
return false;
}
}
Your code represents a basic ATM simulation using p5.js with HTML/CSS for styling. To ensure the UI is perfectly aligned and functional, let's perform a brief UI/UX review and make necessary improvements:
General Observations:
Responsiveness: Since the createCanvas function sets a fixed width and height for the ATM screen, ensure that these dimensions fit within your display area.
Text Visibility: High contrast between text color and background ensures text is readable.
UI Consistency: Ensure consistent button styling.
Code Improvements and Fixes:
Position Elements Dynamically: Ensure buttons and input fields adapt to different screen sizes more dynamically, if necessary. However, given the fixed canvas size, this is less of a concern.
Input Field Enable/Disable: The input transaction field is set to readonly by default but becomes usable when numeric buttons are pressed. Ensure the field resets appropriately.
Ensure Button Actions: Make sure the buttons have a consistent style and behavior (hover effects, etc).
Adjust Thematic Elements: Ensure consistent theme color coding for different actions to make UX more intuitive.
Simplify Elements Creation with Functions to Avoid Repetition: Consider creating a utility function to apply common styles to buttons. This reduces repetition and makes styling updates easier.
Handling Feedback: Instant feedback should be given for actions like entering an incorrect PIN or successful transactions.
Here's a revised section with comments added for improvements and simplifications:
// Styling helper function
function styleButton(btn, backgroundColor, textColor = '#fff') {
btn.style('background-color', backgroundColor);
btn.style('color', textColor);
btn.style('border', 'none');
btn.style('border-radius', '10px');
btn.style('font-size', '16px');
btn.style('cursor', 'pointer');
return btn;
}
// General adjustments for button positioning
const BUTTON_SIZE = 60;
const BUTTON_SPACING = 20;
const BUTTON_ROW_OFFSET = 300;
const POS_X_OFFSET = (width - (3 * BUTTON_SIZE + 40)) / 2;
// Updated createKeypad function to utilize styleButton helper
function createKeypad() {
for (let i = 1; i <= 9; i++) {
let btn = styleButton(createButton(i.toString()), '#7f8fa6');
btn.size(BUTTON_SIZE, BUTTON_SIZE);
btn.position(POS_X_OFFSET + ((i - 1) % 3) * (BUTTON_SIZE + BUTTON_SPACING), BUTTON_ROW_OFFSET + floor((i - 1) / 3) * (BUTTON_SIZE + BUTTON_SPACING));
btn.mousePressed(() => keypadPress(i.toString()));
keypadButtons.push(btn);
}
// Style and position zero, cancel, enter buttons
}
// Create action buttons using styling helper and position adjustments
function createActionButtons() {
let viewBtn = styleButton(createButton('View Balance'), '#3498db');
viewBtn.size(150, 50);
viewBtn.position((width / 2) - 180, 550);
viewBtn.mousePressed(() => { currentScreen = "VIEW"; updateDisplay(); });
actionButtons.push(viewBtn);
// Repeat for other action buttons...
}
// Follow through similar structure across other components...
These improvements are designed to clean up redundancy, improve readability, and ensure better adherence to consistent styling principles. Feel free to adapt the position calculations if you choose to make the canvas responsive or dynamic for different screen sizes later.
Certainly! Here's a revised and cleaned-up version of your ATM simulation code with improvements for styling consistency, readability, and functionality:
let balance = 1000;
let correctPIN = "1234";
let enteredPIN = "";
let currentScreen = "PIN"; // PIN, MENU, VIEW, DEPOSIT, WITHDRAW, HISTORY, EXIT
let displayText;
let keypadButtons = [];
let actionButtons = [];
let inputField, submitButton;
const ATM_WIDTH = 500;
const ATM_HEIGHT = 700;
const SCREEN_WIDTH = 400;
const SCREEN_HEIGHT = 200;
let transactionHistory = [];
// Setup initial canvas and UI components
function setup() {
createCanvas(ATM_WIDTH, ATM_HEIGHT);
background('#1e272e');
drawATMStructure();
createDisplay();
createKeypad();
createActionButtons();
updateDisplay();
}
// Draw ATM body and screen
function drawATMStructure() {
fill('#485460');
noStroke();
rect(0, 0, ATM_WIDTH, ATM_HEIGHT, 20);
}
// Create Display Screen
function createDisplay() {
displayText = createDiv('Enter PIN:');
displayText.addClass('display');
displayText.position((width - SCREEN_WIDTH) / 2, 50);
displayText.size(SCREEN_WIDTH, SCREEN_HEIGHT);
displayText.style('background-color', '#dcdde1');
displayText.style('border-radius', '10px');
displayText.style('box-shadow', 'inset 0 0 10px rgba(0,0,0,0.2)');
displayText.style('padding', '20px');
displayText.style('overflow', 'auto');
}
// Update Display based on current screen
function updateDisplay(message = "") {
if (currentScreen === "PIN") {
displayText.html('Enter PIN:');
removeInputElements();
showKeypadButtons();
hideActionButtons();
} else if (currentScreen === "MENU") {
displayText.html(`
<h2>Main Menu</h2>
<ul style="list-style: none; padding: 0; font-size: 18px;">
<li>1. View Balance</li>
<li>2. Deposit Funds</li>
<li>3. Withdraw Funds</li>
<li>4. Transaction History</li>
<li>5. Exit</li>
</ul>
`);
removeInputElements();
showActionButtons();
hideKeypadButtons();
} else if (currentScreen === "VIEW") {
displayText.html(`<h2>Current Balance</h2><p style="font-size: 24px;">$${balance.toFixed(2)}</p>`);
removeInputElements();
showActionButtons();
hideKeypadButtons();
} else if (currentScreen === "DEPOSIT") {
displayText.html(`
<h2>Deposit Funds</h2>
<p>Enter amount to deposit:</p>
`);
createInputTransaction();
showKeypadButtons();
hideActionButtons();
} else if (currentScreen === "WITHDRAW") {
displayText.html(`
<h2>Withdraw Funds</h2>
<p>Enter amount to withdraw:</p>
`);
createInputTransaction();
showKeypadButtons();
hideActionButtons();
} else if (currentScreen === "HISTORY") {
updateDisplayHistory();
hideKeypadButtons();
showActionButtons();
} else if (currentScreen === "EXIT") {
displayText.html(`
<h2>Thank You!</h2>
<p>Transaction complete.</p>
`);
removeInputElements();
hideKeypadButtons();
hideActionButtons();
setTimeout(resetATM, 3000);
} else if (message !== "") {
displayText.html(`<p>${message}</p>`);
}
}
// Utility to style buttons
function styleButton(btn, bgColor, hoverColor, size=[150, 50]) {
btn.size(size[0], size[1]);
btn.style('background-color', bgColor);
btn.style('color', '#fff');
btn.style('border', 'none');
btn.style('border-radius', '10px');
btn.style('font-size', '16px');
btn.style('cursor', 'pointer');
btn.mouseOver(() => btn.style('background-color', hoverColor));
btn.mouseOut(() => btn.style('background-color', bgColor));
}
// Create Numeric Keypad
function createKeypad() {
const BUTTON_SIZE = 60;
const BUTTON_SPACING = 20;
const START_Y = 300;
const X_OFFSET = (width - (3 * BUTTON_SIZE + 40)) / 2;
for (let i = 1; i <= 9; i++) {
let btn = createButton(i.toString());
styleButton(btn, '#7f8fa6', '#718093', [BUTTON_SIZE, BUTTON_SIZE]);
btn.position(X_OFFSET + ((i-1)%3)*(BUTTON_SIZE + BUTTON_SPACING), START_Y + floor((i-1)/3)*(BUTTON_SIZE + BUTTON_SPACING));
btn.mousePressed(() => keypadPress(i.toString()));
keypadButtons.push(btn);
}
let zeroBtn = createButton('0');
styleButton(zeroBtn, '#7f8fa6', '#718093', [BUTTON_SIZE, BUTTON_SIZE]);
zeroBtn.position(X_OFFSET + BUTTON_SIZE + BUTTON_SPACING, START_Y + 3*(BUTTON_SIZE + BUTTON_SPACING));
zeroBtn.mousePressed(() => keypadPress('0'));
keypadButtons.push(zeroBtn);
let cancelBtn = createButton('Cancel');
styleButton(cancelBtn, '#e74c3c', '#c0392b');
cancelBtn.position(X_OFFSET, START_Y + 3*(BUTTON_SIZE + BUTTON_SPACING));
cancelBtn.mousePressed(cancelAction);
keypadButtons.push(cancelBtn);
let enterBtn = createButton('Enter');
styleButton(enterBtn, '#2ecc71', '#27ae60');
enterBtn.position(X_OFFSET + 2*(BUTTON_SIZE + BUTTON_SPACING), START_Y + 3*(BUTTON_SIZE + BUTTON_SPACING));
enterBtn.mousePressed(enterAction);
keypadButtons.push(enterBtn);
hideKeypadButtons();
}
// Handle Keypad Press
function keypadPress(value) {
if (currentScreen === "PIN") {
if (enteredPIN.length < 4) {
enteredPIN += value;
updateDisplay('*'.repeat(enteredPIN.length));
}
} else if (currentScreen === "DEPOSIT" || currentScreen === "WITHDRAW") {
inputField.value(inputField.value() + value);
}
}
// Create Action Buttons for Menu
function createActionButtons() {
const positions = [
{ label: 'View Balance', color: '#3498db', x: -180, screen: "VIEW" },
{ label: 'Deposit', color: '#8e44ad', x: -180, screen: "DEPOSIT", y: 620 },
{ label: 'Withdraw', color: '#e67e22', x: -10, screen: "WITHDRAW" },
{ label: 'History', color: '#f1c40f', x: -10, screen: "HISTORY", y: 620 },
{ label: 'Exit', color: '#e74c3c', x: 160, screen: "EXIT" }
];
positions.forEach(pos => {
let btn = createButton(pos.label);
styleButton(btn, pos.color, pos.color.substring(0, 7) + "88");
btn.position((width / 2) + pos.x, pos.y || 550);
btn.mousePressed(() => {
currentScreen = pos.screen;
updateDisplay();
});
actionButtons.push(btn);
});
hideActionButtons();
}
// Show Keypad Buttons
function showKeypadButtons() {
keypadButtons.forEach(btn => btn.show());
}
// Hide Keypad Buttons
function hideKeypadButtons() {
keypadButtons.forEach(btn => btn.hide());
}
// Show Action Buttons
function showActionButtons() {
actionButtons.forEach(btn => btn.show());
}
// Hide Action Buttons
function hideActionButtons() {
actionButtons.forEach(btn => btn.hide());
}
// Create Input Field for Transactions
function createInputTransaction() {
if (!inputField) {
inputField = createInput('');
inputField.position((width - 200) / 2, 270);
inputField.size(200, 40);
inputField.attribute('readonly', 'readonly');
inputField.style('text-align', 'center');
inputField.style('font-size', '18px');
inputField.style('border', '2px solid #bdc3c7');
inputField.style('border-radius', '5px');
}
showKeypadButtons();
hideActionButtons();
}
// Remove Input Elements
function removeInputElements() {
if (inputField) {
inputField.remove();
inputField = null;
}
}
// Handle Deposit
function handleDeposit() {
let amount = parseFloat(inputField.value());
if (isNaN(amount) || amount <= 0) {
updateDisplay("Invalid amount. Try again.");
} else {
balance += amount;
transactionHistory.push(`Deposited $${amount.toFixed(2)}`);
updateDisplay(`Successfully deposited $${amount.toFixed(2)}.`);
}
currentScreen = "MENU";
removeInputElements();
showActionButtons();
}
// Handle Withdraw
function handleWithdraw() {
let amount = parseFloat(inputField.value());
if (isNaN(amount) || amount <= 0) {
updateDisplay("Invalid amount. Try again.");
} else if (amount > balance) {
updateDisplay("Insufficient funds.");
} else {
balance -= amount;
transactionHistory.push(`Withdrew $${amount.toFixed(2)}`);
updateDisplay(`Successfully withdrew $${amount.toFixed(2)}.`);
}
currentScreen = "MENU";
removeInputElements();
showActionButtons();
}
// Update Display for Transaction History
function updateDisplayHistory() {
if (transactionHistory.length === 0) {
displayText.html(`<h2>Transaction History</h2><p>No transactions yet.</p>`);
} else {
let historyHTML = `<h2>Transaction History</h2><ul style="list-style: disc; padding-left: 20px;">`;
for (let transaction of transactionHistory) {
historyHTML += `<li>${transaction}</li>`;
}
historyHTML += `</ul>`;
displayText.html(historyHTML);
}
}
// Handle Cancel Action
function cancelAction() {
if (currentScreen === "PIN") {
enteredPIN = "";
updateDisplay();
} else if (currentScreen === "DEPOSIT" || currentScreen === "WITHDRAW") {
currentScreen = "MENU";
updateDisplay();
removeInputElements();
showActionButtons();
hideKeypadButtons();
}
}
// Handle Enter Action
function enterAction() {
if (currentScreen === "PIN") {
if (enteredPIN === correctPIN) {
currentScreen = "MENU";
updateDisplay();
enteredPIN = "";
} else {
updateDisplay("Incorrect PIN. Try again.");
enteredPIN = "";
}
} else if (currentScreen === "DEPOSIT") {
handleDeposit();
} else if (currentScreen === "WITHDRAW") {
handleWithdraw();
}
}
// Reset ATM to Initial State
function resetATM() {
currentScreen = "PIN";
updateDisplay();
showKeypadButtons();
hideActionButtons();
}
// Handle Keypad Input via Keyboard
function keyTyped() {
if (inputField && key >= '0' && key <= '9') {
keypadPress(key);
return false;
}
}
Key Changes Made:
Styling Functions: Introduced a styleButton utility to streamline button styling.
Display Update: Adjusted the display update logic for smoother transitions between screens.
Consistent Styling: Ensures consistent button behaviors and hover effects.
Code Readability: Enhanced readability and organization with structured helper functions.
This code should help maintain a clean and efficient UI as part of the ATM interface simulation.
Copy first response