JS+CSS+HTML创建一个死期银行账户的类 成员变量: 账户号码 密码(加密) 存储金额(余额) 存储年限 查看余额 注册账号密码
时间: 2023-12-13 11:05:57 浏览: 85
下面是一个简单的JS+CSS+HTML实现死期银行账户的类,包括上述所提到的成员变量和方法:
HTML代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>死期银行账户</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container">
<h1>死期银行账户</h1>
<div class="form-container">
<h2>注册账号</h2>
<form id="register-form">
<label for="account-number">账户号码</label>
<input type="text" name="account-number" id="account-number" required>
<label for="password">密码</label>
<input type="password" name="password" id="password" required>
<label for="balance">存储金额</label>
<input type="number" name="balance" id="balance" min="0" required>
<label for="storage-years">存储年限</label>
<input type="number" name="storage-years" id="storage-years" min="1" required>
<button type="submit">注册</button>
</form>
</div>
<div class="form-container">
<h2>查看余额</h2>
<form id="check-balance-form">
<label for="account-number-check">账户号码</label>
<input type="text" name="account-number-check" id="account-number-check" required>
<label for="password-check">密码</label>
<input type="password" name="password-check" id="password-check" required>
<button type="submit">查看余额</button>
</form>
<p id="balance-display"></p>
</div>
</div>
<script src="app.js"></script>
</body>
</html>
```
CSS代码:
```css
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
h1 {
text-align: center;
}
.form-container {
margin-bottom: 20px;
}
form {
display: flex;
flex-direction: column;
align-items: center;
}
label {
margin-top: 10px;
}
input, button {
padding: 5px 10px;
margin-top: 5px;
}
button {
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
}
button:hover {
background-color: #3e8e41;
}
```
JS代码:
```js
// 死期银行账户类
class FixedDepositAccount {
constructor(accountNumber, password, balance, storageYears) {
this.accountNumber = accountNumber;
this.password = this.encrypt(password);
this.balance = balance;
this.storageYears = storageYears;
}
// 加密密码
encrypt(password) {
// 这里简单地将密码反转作为加密算法
return password.split('').reverse().join('');
}
// 存款
deposit(amount) {
this.balance += amount;
}
// 取款
withdraw(amount) {
if (this.balance - amount < 0) {
console.log('余额不足');
} else {
this.balance -= amount;
}
}
// 计算到期后的余额
calculateBalance() {
const interestRate = 0.05; // 利率设为5%
const totalInterest = this.balance * interestRate * this.storageYears;
return this.balance + totalInterest;
}
// 验证密码是否正确
verifyPassword(password) {
return this.encrypt(password) === this.password;
}
}
// DOM元素
const registerForm = document.getElementById('register-form');
const checkBalanceForm = document.getElementById('check-balance-form');
const balanceDisplay = document.getElementById('balance-display');
// 注册账号
registerForm.addEventListener('submit', e => {
e.preventDefault();
const accountNumber = registerForm.elements['account-number'].value;
const password = registerForm.elements['password'].value;
const balance = Number(registerForm.elements['balance'].value);
const storageYears = Number(registerForm.elements['storage-years'].value);
const account = new FixedDepositAccount(accountNumber, password, balance, storageYears);
console.log(account);
});
// 查看余额
checkBalanceForm.addEventListener('submit', e => {
e.preventDefault();
const accountNumber = checkBalanceForm.elements['account-number-check'].value;
const password = checkBalanceForm.elements['password-check'].value;
// 这里假设账户已经在某个数据存储中被注册,可以直接从存储中获取
const account = new FixedDepositAccount('123456', '123456', 10000, 3);
if (account.accountNumber === accountNumber && account.verifyPassword(password)) {
const balanceAfterStorageYears = account.calculateBalance();
balanceDisplay.textContent = `到期后余额为 ${balanceAfterStorageYears} 元`;
} else {
console.log('账户号码或密码错误');
}
});
```
阅读全文