Home > Software engineering >  The value returned for a newly constructed object returns the value of the previously created one
The value returned for a newly constructed object returns the value of the previously created one

Time:04-27

I have the following challenge for practicing OOP in javascript:

The Bank should allow us to:

Add customers

  • A customer should be able to deposit an amount.
  • A customer should bevable to withdraw an amount.
  • A customer should be able see his/her account.

So I did the following, I created a Bank class with different methods.

It seems to work fine with 1 person, but my problem is once I add a new customer, and run the methods on the new customer it returns the value of the first customer.

class Bank {
  constructor() {
    this.customers = [];
  }
  addCustomer(customer) {
    this.customers.push({ name: customer, account: 0 });
  }

  printAccount(customer) {
    if (this.customers.some((person, idx) => person.name === customer)) {
      console.log(
        `${this.customers[0].name}'s account is ${this.customers[0].account}`
      );
    } else {
      console.log("no access");
    }
  }

  depositAmount(customer, amount) {
    if (this.customers.some(person => person.name === customer)) {
      this.customers[0].account  = amount;
    } else {
      return;
    }
  }

  withdrawAmount(customer, amount) {
    if (this.customers.some(person => person.name === customer)) {
      this.customers[0].account -= amount;
    } else {
      return;
    }
  }
}

const bank = new Bank();
bank.addCustomer("daniel");
bank.depositAmount("daniel", 10);
bank.withdrawAmount("daniel", 5);
bank.printAccount("daniel");
bank.addCustomer("gwen");
bank.depositAmount("gwen", 10);
bank.printAccount("gwen");
console.log(bank);

CodePudding user response:

You're always using this.customers[0] in your methods, so it always operates on the first customer.

Use find() to find the customer object, and use that.

class Bank {
  constructor() {
    this.customers = [];
  }
  
  addCustomer(customer) {
    this.customers.push({
      name: customer,
      account: 0
    });
  }
  
  getCustomer(name) {
    return this.customers.find(person => person.name == name);
  }

  printAccount(name) {
    const customer = this.getCustomer(name);
    if (customer) {
      console.log(
        `${customer.name}'s account is ${customer.account}`
      );
    } else {
      console.log("no access");
    }
  }

  depositAmount(name, amount) {
    const customer = this.getCustomer(name);
    if (customer) {
      customer.account  = amount;
    }
  }

  withdrawAmount(name, amount) {
    this.depositAmount(name, -amount);
  }
}

const bank = new Bank();
bank.addCustomer("daniel");
bank.depositAmount("daniel", 10);
bank.withdrawAmount("daniel", 5);
bank.printAccount("daniel");
bank.addCustomer("gwen");
bank.depositAmount("gwen", 10);
bank.printAccount("gwen");
console.log(bank);

CodePudding user response:

You should use find instead:

  depositAmount(customer, amount) {
    const customer = this.customers.find((person) => person.name === customer);

    if (!customer) return; // no customer exists

    customer.account  = amount; // add to customer
  }

If there was no customer found, it returns undefined and you can check for that.

  • Related