2016-07-30 3 views
0

私の割り当ては、複数のアカウントを含む銀行を作成することです。銀行は口座の総金額を返す方法が必要です。また、銀行に新しい口座を登録して口座の配列に追加するaddAccountメソッドも必要です。残高を変更するには、口座を入金または払い戻すことができます。アカウントに負の値を設定できないようにします。 2つの口座間の金額を振り替えることができる銀行に「振替」を書いてください。基本的な銀行システムの作成、キーワード 'this' in javascript

私の問題は、各ユーザーの詳細(名前、残高)を印刷する方法を見つけることです。キーワードthisを適用して見逃してしまったようなものがあります。

var account = function (name, balance){ 

    account.name = name; 
    account.balance = balance; 

    account.deposit = function (depositAmount) { 
    newBalance = account.balance - depositAmount; 
    console.log("Your balance is now " + newBalance); 
    if (newBalance <= 0) { 
     console.log("You have insufficient funds!!!"); 
    } 
    }; 

    account.withdraw = function (withdrawAmount){ 
    newBalance = account.balance - withdrawAmount; 
    console.log("Your balance is now " + newBalance); 
    if (newBalance <= 0) { 
     console.log("You have insufficient funds!!!"); 
    } 

    }; 

    account.transfer = function (transferAmount){ 
//got stuck here 
    } 

    console.log("Name: " + name + "; Balance: " + balance); 
} 


var AustinAccount = new account ("Austin", 500); 
var KateAccount = new account ("Kate", 10000); 
var GingerAccount = new account ("Ginger", 70000000); 
var OreoAccount = new account ("Oreo", 900000000); 
+0

ご質問はありますか? –

+0

コードは機能していますか?何かエラーがありましたか? – Krii

+0

はい私はすべての答えが非常に役に立つことを発見しました。私は、このキーワードをどのように使用するかを理解することができなかったと思います。私はまだこれが私の最初の週はJavaScriptをやっていることを学んでいます。ありがとう! – yangmei

答えて

2

ヒントとコツ

はのは、基礎から始めましょう:

大文字で始めるべき

コンストラクタ:

function Account(name, balance) { 

} 

@Ibarが言ったようにその後、あなたはを参照しthisを使用する必要がありますコンストラクタのインスタンスを名前で呼び出す代わりに使用します。

function Account(name, balance) { 
    this.name = name; 
    ... 
} 

次に、あなたが任意のインスタンスプロパティにアクセスすることができます。また

var account = new Account('Hey', 100); 
console.log(account.name); 

、コンストラクタのメソッドを定義する適切な方法は次のとおりです。

スクリプト作業
function Account(name, balance) { 

} 

Account.prototype.deposit = function(arg) { 

} 

ここにあなたが見つかります私は与えられた金額が肯定的であるかどうかをチェックし、所定の取引後に口座に十分な資金があるかどうかをチェックする2つのプライベートメソッド(_isPositive_isAllowed)を追加しました。

function Account(name, balance) { 
 
    this.name = name; 
 
    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; 
 
} 
 

 
const a = new Account('a', 100); 
 
const b = new Account('b', 0); 
 

 

 
output.innerText += `before: a: ${a.balance}, b: ${b.balance}\n`; 
 

 
a.transfer(100, b); 
 

 
output.innerText += `after: a: ${a.balance}, b: ${b.balance}\n`;
<div id=output></div>


改善

その後、さらにトランザクションのログを追加するスクリプトを改善し、そしておそらく異なるバンクに

const fooBank = new Bank('foo'); 
const fezAccount = new fooBank.Account('fez', 0); 
を管理するためにも方法があり

希望します。このJSBinアウト

+0

これは非常に有益です!プロトタイプが何であるかを説明するのは気になりますか?この場合、なぜそれを使用しましたか? – yangmei

+0

私はまた、 'var'を使用する代わりに 'const'を使用していることに気付きました。相違はありますか? – yangmei

+0

'const'はJSの新機能です。定義された後は編集できない定数を定義することができます。 'prototype'についてはhttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/prototypeを読んでください。 正しい答えとして記入してください。 –

1

機能accountの内部では、あなたがthisaccountを交換する必要があります。 はここに私のコードです。

var account = function (name, balance){ 
    this.name = name; 
    this.balance = balance; 

    this.deposit = function (depositAmount) { 
     ... 
    } 
} 

accountオブジェクトを作成した後、あなたのオブジェクトに関する情報を取得するためにAustinAccount.balanceを呼び出すことができます。

0

チェック:http://jsbin.com/jiheqotaye/edit?js,console

すべてフェズでは、わかりやすいコンストラクタを書くときに検討する必要が正確に何であると述べました。

transferの方法では、 accountで渡されたものが同じコンストラクタのインスタンスであることを確認したいとします。その後、withdrawが成功したことを確認してから、デポジットを処理してください。

Account.prototype.transfer = function (transferAmount, account){ 
    if (account instanceof Account) { 
    if (this.withdraw(transferAmount)) { 
     account.deposit(transferAmount); 
     return true; 
    } else { 
     console.log("An error occured withdrawing that amount"); 
     return false; 
    } 
    } 
    console.log("Provided account was not an account with our bank"); 
    return false; 
}; 

希望します。

+0

'instanceof'をチェックするのはなぜですか? 2つの銀行が互換性のあるAPIを共有している場合、転送をブロックすべきではありません:P –

+0

'' 'instanceof'''は、APIの互換性を厳密に保証する簡略版です。私は自分のお金が誤って古いオブジェクトで終わってしまわないようにしたいと思う;) – horyd

+0

私は、はい、あなた自身のお金に注意してください。 –

関連する問題