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) {
- let pFirstName = this.allCustomers.find(
- x => x.firstName === customer.firstName
- );
- let pLastName = this.allCustomers.find(
- x => x.lastName === customer.lastName
- );
- if (pFirstName && pLastName) {
- throw new Error(
- `${customer.firstName} ${customer.lastName} is already our customer!`
- );
- } else {
- this.allCustomers.push(customer);
- }
- return customer;
- }
- depositMoney(personalId, amount) {
- let checkId = this.allCustomers.find(x => x.personalId === personalId);
- if (!checkId.hasOwnProperty("totalMoney")) {
- checkId.totalMoney = 0;
- }
- if (!checkId.hasOwnProperty("transaction")) {
- checkId.transaction = [];
- }
- if (!checkId) {
- throw new Error(`We have no customer with this ID!`);
- } else {
- checkId.totalMoney = checkId.totalMoney + amount;
- let currTransaction = `${checkId.firstName} ${checkId.lastName} made deposit of ${amount}$`;
- checkId.transaction.push(currTransaction);
- }
- return `${checkId.totalMoney}$`;
- }
- withdrawMoney(personalId, amount) {
- let checkId = this.allCustomers.find(x => x.personalId === personalId);
- if (!checkId.hasOwnProperty("totalMoney")) {
- checkId.totalMoney = 0;
- }
- if (!checkId.hasOwnProperty("transaction")) {
- checkId.transaction = [];
- }
- if (!checkId) {
- throw new Error(`We have no customer with this ID!`);
- }
- if (checkId.totalMoney < amount) {
- throw new Error(
- `${checkId.firstName} ${checkId.lastName} does not have enough money to withdraw that amount!`
- );
- } else {
- checkId.totalMoney = checkId.totalMoney - amount;
- let currTransaction = `${checkId.firstName} ${checkId.lastName} withdrew ${amount}$`;
- checkId.transaction.push(currTransaction);
- }
- return `${checkId.totalMoney}$`;
- }
- customerInfo(personalId) {
- let checkId = this.allCustomers.find(x => x.personalId === personalId);
- let result = checkId.transaction;
- if (!checkId) {
- throw new Error(`We have no customer with this ID!`);
- } else {
- return `
- Bank name: ${this._bankName}
- Customer name: ${checkId.firstName} ${checkId.lastName}
- Customer ID: ${checkId.personalId}
- Total Money: ${checkId.totalMoney}$
- Transactions: \n${result
- .map((e, i) => (result = `${i + 1}. ${e}!`))
- .reverse()
- .join("\n")}
- `;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement