Java Interview Practice Problem (Beginner): Generate Sample Data

  • Post last modified:September 7, 2023
  • Reading time:3 mins read

Problem Statement

We have been given an input number n and our task is to generate n orders that will be used for testing.

DTO for Order, Customer, and Product are given below.

@AllArgsConstructor
@Data
class Product{
    long id;
    String name;
    String SKU;
}

@AllArgsConstructor
@Data
class Customer{
    long id;
    String name;
    long age;
}

@AllArgsConstructor
@Data
class Order{
    long id;
    long productId;
    long customerId;
}

Output should look like below

[
Order(id=1, productId=1, customerId=0),
Order(id=2, productId=0, customerId=1),
Order(id=3, productId=0, customerId=2),
......
]

Before jumping to solution consider giving an attempt.

Solution

Get Sample Products

  • We can intstream to generate numbers and we can use that number to map to Product object.
  • For Product SKU we are using UUID.
  • Using this approach we can generate n number of Products.
public static List<Product> getProducts() {
        String[] names = { "TV", "Laptop", "Phone", "Chair"};
        return IntStream.rangeClosed(0,3)
                .mapToObj(i-> new Product(i,names[i],UUID.randomUUID().toString()))
                .toList();
    }

Get Sample Customers

  • Lets use similar approach to generate customer objects.
  • For age we are using Random() with max 100 and min 20 value.
  public static List<Customer> getCustomers() {
        String[] names = {"sam","lee", "jane", "jose", "shimy", "nita", "resh", "amy", "tina", "tee"};
        return IntStream.rangeClosed(0,9)
                .mapToObj(i-> new Customer(i,names[i], new Random().nextInt(100)+20))
                .toList();
    }

Get Sample Orders

  • Once we have Product and Customer objects ready we can iterate each customer and create order for it.
  • We are using Random() with max and min value get random product for the given customer.
    public static List<Order> getOrders() {
        List<Product> productList = getProducts();
        List<Customer> customers = getCustomers();
        AtomicInteger atomicInteger = new AtomicInteger(0);
        return customers.stream()
                .map(c -> new Order(atomicInteger.incrementAndGet(),
                        productList.get(
                                new Random().nextInt(0,productList.size()-1))
                                .getId(),
                        c.getId()
                        )
                ).toList();
    }

Output

[
  Order(id=1, productId=1, customerId=0), 
  Order(id=2, productId=0, customerId=1), 
  Order(id=3, productId=0, customerId=2), 
  Order(id=4, productId=2, customerId=3), 
  Order(id=5, productId=2, customerId=4), 
  Order(id=6, productId=2, customerId=5), 
  Order(id=7, productId=2, customerId=6), 
  Order(id=8, productId=1, customerId=7), 
  Order(id=9, productId=2, customerId=8), 
  Order(id=10, productId=0, customerId=9)
]

Code :

  • Find all the code for this & other exercises on GitHub

Conclusion

In this problem we learned how to generate mock data. We used Java 8 functional style coding for solution which makes solution more concise and easy to read.

Before You Leave

Leave a Reply