-
-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add second OOP lab #585
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
class BankAccount { | ||
constructor(balance = 0) { | ||
this.balance = balance; | ||
this.transactions = []; | ||
} | ||
|
||
deposit(amount) { | ||
if (amount > 0) { | ||
this.transactions.push({ type: 'deposit', amount }); | ||
this.updateBalance(); | ||
console.log(`Successfully deposited $${amount}. New balance: $${this.balance}`); | ||
} else { | ||
console.log('Deposit amount must be greater than zero.'); | ||
} | ||
} | ||
|
||
withdraw(amount) { | ||
if (amount > 0 && amount <= this.balance) { | ||
this.transactions.push({ type: 'withdraw', amount }); | ||
this.updateBalance(); | ||
console.log(`Successfully withdrew $${amount}. New balance: $${this.balance}`); | ||
} else { | ||
console.log('Insufficient balance or invalid amount.'); | ||
} | ||
} | ||
|
||
checkBalance() { | ||
console.log(`Current balance: $${this.balance}`); | ||
} | ||
|
||
updateBalance() { | ||
this.balance = this.transactions.reduce((acc, transaction) => { | ||
return transaction.type === 'deposit' ? acc + transaction.amount : acc - transaction.amount; | ||
}, 0); | ||
} | ||
|
||
listAllDeposits() { | ||
const deposits = this.transactions | ||
.filter(transaction => transaction.type === 'deposit') | ||
.map(transaction => transaction.amount); | ||
console.log('Deposits:', deposits); | ||
return deposits; | ||
} | ||
|
||
listAllWithdrawals() { | ||
const withdrawals = this.transactions | ||
.filter(transaction => transaction.type === 'withdraw') | ||
.map(transaction => transaction.amount); | ||
console.log('Withdrawals:', withdrawals); | ||
return withdrawals; | ||
} | ||
} | ||
|
||
const myAccount = new BankAccount(); | ||
|
||
myAccount.deposit(100); | ||
myAccount.deposit(500); | ||
myAccount.withdraw(30); | ||
myAccount.withdraw(100); | ||
myAccount.checkBalance(); | ||
myAccount.listAllDeposits(); | ||
myAccount.listAllWithdrawals(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
1. You should define a class named `BankAccount` with a `constructor` that has a `balance` parameter (default value of 0). The constructor should also initialize an empty `transactions` array to store transaction records. | ||
|
||
2. You should define a method named `deposit` with an `amount` parameter. This method should: | ||
- Check if the `amount` is greater than 0. | ||
- If the amount is valid, push an object with the properties `type: 'deposit'` and the `amount` into the `transactions ` array. | ||
- Call the `updateBalance` method to update the account's balance. | ||
- Log a message to the console indicating the deposit was successful and display the new balance. | ||
- If the `amount` is invalid, log an error message indicating that the deposit amount must be greater than zero. | ||
Comment on lines
+4
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is too step by step and doesn't allow the camper to think through what needs to be done. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, it will be much easier from a testing perspective to return the string instead of logging it to the console. |
||
|
||
3. You should define a method named `withdraw` with an `amount` parameter. This method should: | ||
- Check if the `amount` is greater than 0 and less than or equal to the current `balance`. | ||
- If valid, push an object with the properties `type: 'withdraw'` and the `amount` into the `transactions` array. | ||
- Call the `updateBalance` method to update the account's balance. | ||
- Log a message to the console indicating the withdrawal was successful and display the new balance. | ||
- If the `amount` is invalid or exceeds the balance, log an error message indicating insufficient balance or invalid amount. | ||
Comment on lines
+11
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same issue here. |
||
|
||
4. You should define a method named `checkBalance` that logs the current account balance to the console. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should just have them return the string instead of logging it. |
||
|
||
5. You should define a method named `updateBalance`. This method should: | ||
- Use the `reduce` function to sum the transactions in the `transactions` array. | ||
- Update the `balance` by adding up all the deposit amounts and subtracting all the withdrawal amounts. | ||
Comment on lines
+20
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this one too should be more generalized. |
||
|
||
6. You should define a method named listAllDeposits. This method should: | ||
- Use the `filter` function to create a new array containing only the transactions with `type: 'deposit'`. | ||
- Use the `map` function to extract the `amount` from each deposit transaction. | ||
- Log the list of deposit amounts to the console and return this list. | ||
Comment on lines
+24
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should be more general here to and say something like "Return an array containing the amounts from each deposit transaction." also, we should remove the log statement here. |
||
|
||
7. You should define a method named `listAllWithdrawals`. This method should: | ||
- Use the `filter` function to create a new array containing only the transactions with `type: 'withdraw'`. | ||
- Use the `map` function to extract the `amount` from each withdrawal transaction. | ||
- Log the list of withdrawal amounts to the console and return this list. | ||
Comment on lines
+29
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this could be updated to something like "Return an array containing the amounts from each withdrawal transaction." also, we should remove the log statement here. |
||
|
||
8. You should create an instance of the BankAccount class named myAccount. Then, you should: | ||
- Use the `deposit` method to deposit into the account. | ||
- Use the `withdraw` method to withdraw from the account. | ||
- Use the `checkBalance` method to log the current balance to the console. | ||
- Use the `listAllDeposits` method to list all deposits made. | ||
- Use the `listAllWithdrawals` method to list all withdrawals made. | ||
Comment on lines
+33
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO, I don't think we really need this last part. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.