My task is to create random offers with different attributes, one of which is the status of the offer as an enum. The status can be “NEW”, “FOR_SALE”, “SOLD”, “PAID”, “DELIVERED”, “CLOSED”, “EXPIRED”, or “WITHDRAWN”. I need to generate a random offer with randomized attributes and a random status from the enum.
I've tried various approaches but I'm struggling to use the enum in this scenario. String interpolation doesn't seem to work with the enum, and I'm unsure how to apply randomization to it.
export class Overview1Component implements OnInit {
private offers: Offer[];
export class Offer {
public title: string;
public sellDate: Date;
public description: string;
public auctionStatus: AuctionStatus;
public numberOfBids: number;
public valueHighestBid: number;
}
export enum AuctionStatus {
NEW = 1,
FOR_SALE = 2,
SOLD = 3,
PAID = 4,
DELIVERED = 5,
CLOSED = 6,
EXPIRED = 7,
WITHDRAWN = 8
}
Am I approaching the enumerator incorrectly? How can I select a random value from the enum and display it using string interpolation in my HTML?
Additional information:
To create random offers, I use the following method:
public addRandomOffer(): void {
const newOffer = new Offer();
newOffer.title = 'This great article offer-' + (this.offers.length);
const fromDate = new Date(2005, 0, 1);
const toDate = new Date(2020, 0, 1);
newOffer.sellDate = this.getRandomDate(fromDate, toDate, 0, 23);
newOffer.numberOfBids = Math.floor(Math.random() * 30 + 1);
if (newOffer.numberOfBids === 0) {
newOffer.valueHighestBid = 0;
} else {
newOffer.valueHighestBid = Math.floor(Math.random() * 200 + 1);
}
this.offers.push(newOffer);
}
The current method does not include randomizing the status, as I'm unsure how to implement it.
This process allows for random offers to be displayed on an overview component.
In the HTML for printing the offers, the code looks like this:
<tr *ngFor="let offer of offers">
<td>{{offer.title}}</td>
<td>{{offer.sellDate}}</td>
<td>{{offer.auctionStatus}}</td>
<td>{{offer.numberOfBids}}</td>
<td>€{{offer.valueHighestBid}}</td>
</tr>