从 Product 类创建随机对象 [英] Create random objects from a Product class

查看:49
本文介绍了从 Product 类创建随机对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代表产品的产品类:

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;
    }
}

现在,在我的程序中,我想创建这个 Product 类的 3 个随机对象,这是我的代码:

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));
    }
}

现在,我的问题是如何实现,以便随机类创建随机值?我已经为产品创建了静态值,那么如何随机化它以获得 3 个不同的值?

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?

推荐答案

不知道要随机化 Product 的哪个元素,只能不断猜测.下面给出的代码不是您问题的解决方案;相反,它是为您提供有关如何使用随机生成的值的指示:

Without knowing which element of Product you want to randomize, one can only keep guessing. The code given below is not the solution to your question; rather, it is to give you a pointer about how you can use the randomly generated values:

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

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;
    }

    @Override
    public String toString() {
        return "Product [productId=" + productId + ", productType=" + productType + ", brand=" + brand + ", price="
                + price + "]";
    }

    public static void main(String[] args) {
        List<Product> products = new ArrayList<>();
        Random r = new Random(10);
        int num;
        for (int i = 0; i < 3; i++) {
            num = r.nextInt(10);
            products.add(new Product(r.nextInt(10000), "Type" + num, "Brand" + num, 1000 * r.nextDouble()));
        }
        products.stream().forEach(System.out::println);
    }
}

示例运行:

Product [productId=2380, productType=Type3, brand=Brand3, price=257.8027905957804]
Product [productId=1456, productType=Type6, brand=Brand6, price=244.11725056425314]
Product [productId=6214, productType=Type1, brand=Brand1, price=370.6111260136414]

如果您需要任何进一步的帮助,请随时发表评论.

Feel free to comment should you need any further help.

这篇关于从 Product 类创建随机对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆