vendredi 17 janvier 2020

Create random objects from a Product class

I have a Product class that represent a Product:

import java.util.ArrayList;

public class Product {
private int productId;
private String productType;
private String brand;
private double price;

public Product(int productId, String productType, String brand, double price)
{
    this.productId = productId;
    this.productType = productType;
    this.brand = brand;
    this.price = price;

}

public int getProductId() {
    return this.productId;
}

public void setProductId(int productId) {
    this.productId = productId;
}

public String getProductType() {
    return this.productType;
}

public void setProductType(String productType) {
    this.productType = productType;
}

public String getBrand() {
    return this.brand;
}

public void setBrand(String brand) {
    this.brand = brand;
}

public double getPrice() {
    return this.price;
}

public void setPrice(double price) {
    this.price = price;
}

Now, in my program, I want to create 3 random objects of this Product class, and here is my code for that:

public static void main(String[] args) {

        ArrayList<Product> products = new ArrayList();
        Random r = new Random();

        for(int i = 0; i < 3; i++)
        {
            products.add(new Product(1337, "Type", "Brand", 300.33));
        }
}

Now, my question is how do I implement so that the random class creates random values (I have created static values for the products, so how Do i randomize it so I get 3 different values?)




Aucun commentaire:

Enregistrer un commentaire