<script>
function Account(UserId, Balance) {
this.UserId = UserId;
this.Balance = Balance;
}
Account.prototype.deposit = function (amount) {
if (this._isPositive(amount)) {
this.balance += amount;
console.info(`Deposit: ${this.name} new balance is ${this.balance}`);
return true;
}
return false;
}
Account.prototype.withdraw = function (amount) {
if (this._isAllowed(amount)) {
this.balance -= amount;
console.info(`Withdraw: ${this.name} new balance is ${this.balance}`);
return true;
}
return false;
}
Account.prototype.transfer = function (amount, account) {
if (this.withdraw(amount) && account.deposit(amount)) {
console.info(`Transfer: ${amount} has been moved from ${this.name} to ${account.name}`);
return true;
}
return false;
}
Account.prototype._isPositive = function (amount) {
const isPositive = amount > 0;
if (!isPositive) {
console.error('Amount must be positive!');
return false;
}
return true;
}
Account.prototype._isAllowed = function (amount) {
if (!this._isPositive(amount)) return false;
const isAllowed = this.balance - amount >= 0;
if (!isAllowed) {
console.error('You have insufficent funds!');
return false;
}
return true;
}
function transferring() {
mary21.transfer(100.00, john456);
}
</script>
我正在尝试将金额从一个用户余额转移到另一个用户余额。我可以知道如何在我的项目中使用此javascript。我正在使用.Net MVC,所以不确定在View或Controller中完成事务。