首页 > 解决方案 > 创建银行客户计划

问题描述

我对这个 OOP 项目有一些要求。首先,这是我的代码:

function Bank (customer, balance, amount, withdraw) {

          this.customer = customer;
          this.balance = balance;
          this.amount = amount;
          this.withdraw = withdraw;

          this.currentCustomers = {
            account: 0

          },

          this.printAccount = function () {
          return `${this.customer}'s new balance is ${this.balance}`;
          },

          this.deposit = function () {
            return `${this.customer}'s new balance is ${balance + this.amount}`;
          },

          

          this.withdraw = function (withdraw) {
            if(this.balance < this.withdraw) {
              throw "Warning! Insufficient funds!";
            } else {
            return `${this.customer}'s new balance is ${balance - withdraw}`;
           }
          }

        }
        

        const newBank = new Bank('Sheldon');
        console.log(newBank.customer);

        const newBank1 = new Bank('Sheldon', 0);
        console.log(newBank1.printAccount());

        const newBank2 = new Bank ('Sheldon', 0, 5);
        console.log(newBank2.deposit());  
 
        const newBank3 = new Bank ('Sheldon', 0, 5);
        console.log(newBank3.withdraw());  

我需要:

  1. 能够添加客户
  2. 在添加新金额之前和之后显示他们的银行金额
  3. 显示任何存款并更新他们的账户
  4. 如果提款少于当前金额,则显示提款并抛出错误消息
  5. 显示所有客户及其当前帐户状态

标签: javascriptoop

解决方案


您实际上并没有在这里提出问题,但我运行了您的代码,我看到您在您的场景中取得了NaN平衡。withdraw

我认为你在什么应该是实例变量(Sheldon's Bank 的属性)和什么应该作为参数传递给函数之间存在一些基本的混淆。 withdraw()接受一个参数,但还有一个withdraw变量传递给构造函数。为什么?

这个类是代表整个银行还是单个用户的帐户?它试图两者兼而有之,但那些需要是两个独立的类。

您需要更多地学习 OOP 设计。但如果它有帮助,我已经将你的课程改写成我认为应该的样子。我使用的是ES6 类语法而不是function语法,但这与这里的核心问题无关。在幕后,它们都是功能。由于您是初学者,我将避免谈论公共变量与私有变量,但这是最终要了解的内容。

我所写内容的核心思想之一是balanceofBankAccount应该由于调用 和 之类的方法withdraw而改变deposit。为了创建一个帐户,我们只需要一个名称和一个初始余额。 withdraw并且deposit是可以多次调用的独立操作。它们不应该依赖于构造函数参数。

我想了一下如何记录所有内容以及如何跟踪已提取的金额。我认为我在这里变得太先进/太棘手了,但我理想的解决方案是所有交易都应返回包含当前余额的收据,而某些交易(提款和关闭帐户)也应返还资金。所以要么ReceiptTransaction可能是另一个单独的类。但现在我有一大堆console陈述只是为了演示。

class Bank {
    constructor() {
        this.accounts = [];
    }
    // doesn't need a constructor right now since it takes no arguments
    // add a new account for a customer and return the acount
    createAccount(customer, initialBalance = 0) {
        const account = new BankAccount(customer, initialBalance);
        this.accounts.push(account);
        return account;
    }
    // return the account that matches a name, or undefined if none exists
    findAccountByName(customer) {
        return this.accounts.find(account => account.customer === customer);
    }
    // return only accounts with status of 'open'
    getActiveAccounts() {
        return this.accounts.filter(account => account.status === "open");
    }
    // get the names for all customer acounts
    getCustomerNames() {
        return this.accounts.map(account => account.customer);
    }
    // get the sum of all balances in all accounts
    getTotalAssets() {
        return this.accounts.reduce((sum, account) => sum + account.balance, 0);
    }
}

class BankAccount {
    constructor(customer, initialBalance = 0) {
        this.customer = customer;
        this.status = 'open';
        this.balance = initialBalance;
        console.log(`created a new account for ${this.customer} with balance ${this.balance}`);
    }
    deposit(amount) {
        console.log(`${this.customer}'s balance went from ${this.balance} to ${this.balance + amount} after depositing ${amount}`);
        this.balance = this.balance + amount;
    }
    withdraw(amount) {
        if (this.balance < amount) {
            console.error("Warning! Insufficient funds!");
            return 0;
        }
        else {
            console.log(`${this.customer}'s balance went from ${this.balance} to ${this.balance - amount} after widthdrawing ${amount}`);
            this.balance = this.balance - amount;
            return amount;
        }
    }
    // widthdraw all remaining funds and change account status
    closeAccount() {
        this.withdraw(this.balance); // will log a balance
        this.status = 'closed';
        console.log(`${this.customer}'s account has been closed`);
    }
    checkBalance() {
        console.log(`${this.customer}'s current balance is ${this.balance}`);
    }
}

const bank = new Bank;
const account = bank.createAccount('Sheldon', 0);
account.deposit(5);
account.withdraw(1);
account.deposit(20);
account.withdraw(100);
console.log("bank customers", bank.getCustomerNames());
console.log("bank assets", bank.getTotalAssets());


推荐阅读