From 93922e888214fe7447716c0a0bccb5fda9ca9577 Mon Sep 17 00:00:00 2001 From: Chris Trevino Date: Wed, 8 Jul 2020 18:47:51 -0500 Subject: [PATCH 1/2] Not DOne --- 06week/bankAccount.js | 68 ++++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 20 deletions(-) diff --git a/06week/bankAccount.js b/06week/bankAccount.js index a7e9addc3..d0a322134 100644 --- a/06week/bankAccount.js +++ b/06week/bankAccount.js @@ -2,31 +2,58 @@ //create a Account Class -class Account { - //create a contractor with with account #, owner, Transactions +class BankAccount { + //the account number on this bankaccount + accountNumber; + //the name of the owner of the bank account + accOwner; + //array of Transaction instances against this bankaccount + transactions + //create a contractor with with account #, owner, Tran constructor(accountNum, accOwner, transactions) { this.accountNum = accountNum; this.accOwner = accOwner; this.transactions = []; } - //this method needs to return the sum of all the transactions - balance() { - let balanceOf = this.transactions.reduce(function(a, b){ - return a + b; - }); - - console.log(sum); // Hopefully this will print out the sum - - } + //this method , should take in an amount, and add a new Transaction + //as a deposit to this bank account + deposit(amt){ + + if(amt > 0) { //only allows deposits that are greater then zero + + //1. create a transaction instance using amount passed in + let newTransaction = new Transaction(amt, "Deposit"); + //2. add this newly created transaction to the transaction array + this.transactions.push(newTransaction); + + } + } + /** + * returns the current balance on the account by suming up this + * transactions amount + */ +balance(){ - deposit(amountDeposited) { - if (amountDeposited ) { + let sum = 0; + //how can you use higher order function here. + for(let i = 0; i < this.transactions.length; i++){ + let currentTransaction = this.transactions[i]; + sum = sum + currentTransaction.amount; } + return sum; } - charge(payee, amountCharged){ - if () + charge(payee, amt){ + /** + * TODO: currently this will allow me to overcharge + * add code to prevent overcharging. + * HINT: you can call the Balance() to get the balance before the charge + */ + + let newCharge = new Transaction(-amt, payee); + this.transactions.push(newCharge); } + } class Transaction { constructor(amount ,payee) { @@ -38,10 +65,11 @@ class Transaction { } +/****************************** */ +let acct1 = new BankAccount("5553429", "John Doe"); + +console.log(acct1.accountNum); +console.log(acct1.accOwner); +console.log(acct1.balance()); -//created Account Owner -let accountOne = new Account('454550', 'Christopher Trevino') -//console.log(chrisAccount) -let transactionOne = new Transaction(50.50, 'Target') -console.log(target); From 7510ef583b386ffaddc90980f3d99b30b596d6cb Mon Sep 17 00:00:00 2001 From: Chris Trevino Date: Thu, 9 Jul 2020 20:21:15 -0500 Subject: [PATCH 2/2] done --- 06week/bankAccount.js | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/06week/bankAccount.js b/06week/bankAccount.js index d0a322134..30d733a95 100644 --- a/06week/bankAccount.js +++ b/06week/bankAccount.js @@ -37,10 +37,14 @@ balance(){ let sum = 0; //how can you use higher order function here. + // for i is less than the transactions length for(let i = 0; i < this.transactions.length; i++){ + //create a current transaction to represent the position in transactions let currentTransaction = this.transactions[i]; + //sum is equal to sum plus the current transaction amount sum = sum + currentTransaction.amount; } + //then we will return the sum at the end to show the sum at the end. return sum; } charge(payee, amt){ @@ -50,8 +54,13 @@ balance(){ * HINT: you can call the Balance() to get the balance before the charge */ - let newCharge = new Transaction(-amt, payee); - this.transactions.push(newCharge); + // if the amount is less than balance allow else do nothing + if(amt < this.balance()){ + //craeting a mock transaction + let newCharge = new Transaction(-amt, payee); + //pushing the newCharge into the transaction + this.transactions.push(newCharge) + } } } @@ -72,4 +81,22 @@ console.log(acct1.accountNum); console.log(acct1.accOwner); console.log(acct1.balance()); +acct1.deposit(100); +console.log(acct1.balance()); + +acct1.deposit(-200);// shouldn't be allowed!! +console.log(acct1.balance()); + +acct1.charge('Target', 30.50); +acct1.charge('Freebirds', 15.15); +console.log(acct1.balance());// 54.35 + +acct1.charge('Diamond Shop', 1000);// charge shouldn't be allowed +console.log(acct1.balance());//54.35 + +acct1.charge('Target', -20);//refund +console.log(acct1.balance()); + + +