Skip to content
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

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions frontend-cert/js-projects/second-oop/script.js
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();

13 changes: 13 additions & 0 deletions frontend-cert/js-projects/second-oop/user-stories.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
1. You should define a class named `BankAccount` with a `constructor` that has a `balance` parameter. The default parameter value should be set to 0. The constructor should also initialize an empty `transactions` array to store transaction records.

2. You should define a method named `updateBalance`.

3. You should define a method named `deposit` with an `amount` parameter. This method should update `balance` accordingly.

4. You should define a method named `withdraw` with an `amount` parameter. This method should update `balance` accordingly.

5. You should define a method named `checkBalance` that returns the current balance.

6. You should define a method named `listAllDeposits` that returns all deposits.

7. You should define a method named `listAllWithdrawals` that returns all withdrawals.