0

I'm finally doing an advanced course for React and have some pre-work stuff to complete.

The task description doesn't say what the exact result should be and my result doesn't seem right to me.

The task:

Part 1

Create a constructor for object Client with argument name.

This object is supposed to have the following attributes:

name and orders (array to store orders), and a method addOrder(order) (to add new orders to orders)

Part 2

Create a constructor for object Order with arguments client and number.

This object is supposed to have the following attributes:

client (related to the client) and number (order number)

Part 3

Check your code with the following:

const client1 = new Client("John");
const order1 = new Order(client1, "1");
const order2 = new Order(client1, "2");

client1.addOrder(order1);
client1.addOrder(order2);

console.table(client1.orders);

My code:

class Client {
  constructor(name) {
    this.name = name;
    this.orders = [];
  }

  addOrder(order) {
    this.orders.push(order);
  }
}

class Order extends Client {
  constructor(client, number) {
    super(client);
    this.number = number;
  }
}

const client1 = new Client("John");
const order1 = new Order(client1, "1");
const order2 = new Order(client1, "2");

client1.addOrder(order1);
client1.addOrder(order2);

console.table(client1.orders);

returns this (and I believe, for the correct code, name should be "John" and orders arrays shouldn't be there at all):

Array(2)
0: Order {name: Client, orders: Array(0), number: '1'}
1: Order {name: Client, orders: Array(0), number: '2'}

Console result

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • `Order` should not extend `Client`. An order is not a kind of client. – Barmar Apr 04 '23 at 17:50
  • `class Order extends Client` ? An order is *not* a client, right? – VLAZ Apr 04 '23 at 17:50
  • 1
    It seems like you need to bone up on basic OO principles. Subclassing represents the IS-A relationship. But the relationship between Client and Orders is HAS-A. That's implemented in the `orders` array in `Client`. – Barmar Apr 04 '23 at 17:51
  • Related: [What is the difference between "IS -A" relationship and "HAS-A" relationship in Java?](https://stackoverflow.com/q/36162714) – VLAZ Apr 04 '23 at 17:52
  • This test is weird. Why would you need to pass `client1` as argument when *creating* the order, and then have to call `addOrder` on `client1` to really make the link with that client?? This is a bad exercise. – trincot Apr 04 '23 at 17:52
  • @trincot Makes sense for the order to handle its own addition. A bit of a visitor vibe to it. EDIT: wait, I see that's not what the exercise actually suggested... – VLAZ Apr 04 '23 at 17:54
  • Thanks for your tips and good points there! Indeed, the task is a bit weird, and I will soon find out what their real intention was. Also, `client1.orders` returns endless loop from `client` as this also contains `orders` array - but the task required `client` object in `Object` class, not `client.name` – Michael Sadowski Apr 12 '23 at 08:34

1 Answers1

0

Order should not be a subclass of Client. A client has an array of orders, and an order has an associated client, but neither is a kind of the other.

The HAS-A relationship is implemented using a property, so you just do this.client = client; in the Order constructor.

class Client {
  constructor(name) {
    this.name = name;
    this.orders = [];
  }

  addOrder(order) {
    this.orders.push(order);
  }
}

class Order {
  constructor(client, number) {
    this.client = client;
    this.number = number;
  }
}

const client1 = new Client("John");
const order1 = new Order(client1, "1");
const order2 = new Order(client1, "2");

client1.addOrder(order1);
client1.addOrder(order2);

console.log(client1.orders);
Barmar
  • 741,623
  • 53
  • 500
  • 612