【问题标题】:Create random objects from a Product class [closed]从产品类创建随机对象[关闭]
【发布时间】:2020-01-17 14:32:28
【问题描述】:

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

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 个随机对象,这是我的代码:

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 个不同的值?

【问题讨论】:

  • 当“相关”代码不使用 Swing 类时,这是一个很好的提示,swing 标记与问题无关。所以不要添加它!
  • 您希望随机字符串(类型和品牌)如何?还是随机 id?
  • 我投了反对票,因为在谷歌搜索结果的最顶部(“java生成随机字符串”)我发现多篇文章向您展示了如何做到这一点。
  • 您是否有一套固定的品牌、ID 等有效值,或者“随机化”对您意味着什么? “MyArbitraryWeirdBrand”足够随机吗?甚至是“khkghfjkbvnmdk”?
  • 带有随机数字的静态值是否足够?

标签: java random


【解决方案1】:

在不知道要随机化Product 的哪个元素的情况下,只能继续猜测。下面给出的代码不是您问题的解决方案;相反,它是给你一个关于如何使用随机生成的值的指针:

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]

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-10
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多