Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Bank {
- constructor(bankName) {
- this._bankName = bankName;
- this.allCustomers = [];
- }
- newCustomer(customer) {
- //object {firstName, lastName, personalId}.
- if (
- this.allCustomers.find((c) => c.personalId == customer.personalId)
- ) {
- throw new Error(
- `${customer.firstName} ${customer.lastName} is already our customer!`
- );
- }
- let currentData = Object.assign({}, customer);
- customer.totalMoney = 0;
- customer.transactions = []; //{type:amount}
- this.allCustomers.push(customer);
- return currentData;
- }
- depositMoney(personalId, amount) {
- let currentCustomer = this._findCustomerById(personalId);
- if (!currentCustomer)
- throw new Error(`We have no customer with this ID!`);
- currentCustomer.totalMoney += amount;
- currentCustomer.transactions.push({ 'made deposit of': amount });
- return `${currentCustomer.totalMoney}$`;
- }
- withdrawMoney(personalId, amount) {
- let currentCustomer = this._findCustomerById(personalId);
- if (!currentCustomer) {
- throw new Error('We have no customer with this ID!');
- }
- if (currentCustomer.totalMoney < amount) {
- throw new Error(
- `${currentCustomer.firstName} ${currentCustomer.lastName} does not have enough money to withdraw that amount!`
- );
- }
- currentCustomer.totalMoney -= amount;
- currentCustomer.transactions.push({ withdrew: amount });
- return `${currentCustomer.totalMoney}$`;
- }
- customerInfo(personalId) {
- let currentCustomer = this._findCustomerById(personalId);
- if (!currentCustomer) {
- throw new Error('We have no customer with this ID!');
- }
- let result = [
- `Bank name: ${this._bankName}
- Customer name: ${currentCustomer.firstName} ${currentCustomer.lastName}
- Customer ID: ${personalId}
- Total Money: ${currentCustomer.totalMoney}$
- Transactions:`,
- ];
- for (let i = currentCustomer.transactions.length - 1; i >= 0; i--) {
- let [type, amnt] = Object.entries(
- currentCustomer.transactions[i]
- )[0];
- result.push(
- `${i + 1}. ${currentCustomer.firstName} ${
- currentCustomer.lastName
- } ${type} ${amnt}$!`
- );
- }
- return result.join('\n');
- }
- _findCustomerById(id) {
- return this.allCustomers.find((c) => c.personalId == id);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement