-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
113 lines (99 loc) · 3.8 KB
/
script.js
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
// Define a BankAccount class
class BankAccount {
constructor(accountHolderName, accountType, initialBalance) {
this.accountHolderName = accountHolderName.trim();
this.accountType = accountType.trim();
this.balance = initialBalance;
}
}
// Initialize variables
let account = null;
// Add event listener for form submission to create an account
document.getElementById('account-form').addEventListener('submit', function(event) {
event.preventDefault(); // Prevent form from submitting and refreshing the page
createAccount();
});
// Function to create a bank account
function createAccount() {
const accountHolderName = document.getElementById('accountHolderName').value.trim();
const accountType = document.getElementById('accountType').value.trim();
const initialBalance = parseFloat(document.getElementById('initialBalance').value);
// Validate initial balance
if (isNaN(initialBalance) || initialBalance < 0) {
displayError('Invalid initial balance. Please enter a valid amount.');
return;
}
// Validate account holder name and type
if (!accountHolderName || !accountType) {
displayError('Account holder name and account type cannot be empty.');
return;
}
// Create a new bank account
account = new BankAccount(accountHolderName, accountType, initialBalance);
// Display account information and clear form inputs
displayAccountInfo();
clearFormInputs();
}
// Function to check the account balance
document.getElementById('checkBalance').addEventListener('click', function() {
if (account) {
displayAccountInfo();
} else {
displayError('No account found. Please create an account first.');
}
});
// Function to deposit money
document.getElementById('deposit').addEventListener('click', function() {
if (account) {
const depositAmount = parseFloat(prompt('Enter the deposit amount:'));
clearError();
if (!isNaN(depositAmount) && depositAmount > 0) {
account.balance += depositAmount;
displayAccountInfo();
} else {
displayError('Invalid deposit amount. Please enter a valid amount.');
}
} else {
displayError('No account found. Please create an account first.');
}
});
// Function to withdraw money
document.getElementById('withdraw').addEventListener('click', function() {
if (account) {
const withdrawAmount = parseFloat(prompt('Enter the withdrawal amount:'));
clearError();
if (!isNaN(withdrawAmount) && withdrawAmount > 0) {
if (withdrawAmount <= account.balance) {
account.balance -= withdrawAmount;
displayAccountInfo();
} else {
displayError('Insufficient funds. Cannot withdraw more than the account balance.');
}
} else {
displayError('Invalid withdrawal amount. Please enter a valid amount.');
}
} else {
displayError('No account found. Please create an account first.');
}
});
// Function to display account information
function displayAccountInfo() {
document.getElementById('displayAccountHolder').textContent = account.accountHolderName;
document.getElementById('displayAccountType').textContent = account.accountType;
document.getElementById('displayBalance').textContent = account.balance.toFixed(2);
clearError();
}
// Function to display error messages
function displayError(message) {
document.getElementById('errorMessage').textContent = message;
}
// Function to clear error messages
function clearError() {
document.getElementById('errorMessage').textContent = '';
}
// Function to clear form inputs after creating an account
function clearFormInputs() {
document.getElementById('accountHolderName').value = '';
document.getElementById('accountType').value = '';
document.getElementById('initialBalance').value = '';
}