Класс, который хранит в себе данные о заказе
Помогите плз. Как сделать корректно чтобы при создании заказа генерировался id заказа и чтобы логика метода confirmOrder и метода isValidType была корректной?
Метод confirmOrder должен проставлять ордер в статус confirmed = true (если заказ не был подтвержден раньше) и дату конфирма ставить в текущую. Метод isValidType должен принимать только два значения Buy, Sell - метод возвращает true, если это требование выполняется, и false если нет. Мой код: class Order { confirmed = false; id = Math.floor(Math.random() * 100); dateCreated = new Date(); dateConfirmed; constructor(price, city, type) { this.price = price; this.city = city; this.type = type; } checkPrice() { if (this.price > 1000) { return true; } return false; } confirmOrder() { if (this.confirmed == false) { this.confirmed = true; } this.dateConfirmed = new Date(); } isValidType() { if (this.type == false) { return 'Buy'; } return 'Sell'; } } |
NovichokJS,
:-? confirmOrder() { if (!this.confirmed) { this.confirmed = true; this.dateConfirmed = new Date(); } } isValidType() { return ['Sell', 'Buy'].includes(this.type); } |
спасибо, а как корректно id заказа прикрутить в код? то, что я написал id = Math.floor(Math.random() * 100); - это неверно. Это должен быть наверно рандомное число от 1 до бесконечности и чтобы при новом заказе id не повторился
|
Цитата:
Date.now()*100 + Math.floor(Math.random() * 100) |
Цитата:
id = self.crypto.randomUUID(); |
Цитата:
class Order { confirmed = false; dateCreated = new Date(); dateConfirmed; id; constructor(price, city, type) { this.price = price; this.city = city; this.type = type; } checkPrice() { if (this.price > 1000) { return true; } return false; } confirmOrder() { if (!this.confirmed) { this.confirmed = true; this.dateConfirmed = new Date(); this.id = Math.floor(Math.random() * 100); } } isValidType() { return ['Sell', 'Buy'].includes(this.type); } } |
NovichokJS,
вам виднее когда нужен id |
NovichokJS,
let count = 0; class Order { confirmed = false; dateCreated = new Date(); dateConfirmed; id = count++; |
Цитата:
|
Цитата:
alert(typeof self.crypto.randomUUID() ); |
Часовой пояс GMT +3, время: 23:34. |