Create Entity
Create new package, named entities. In this package, create new java class, named Product as below:
package entities;
public class Product {
private String id;
private String name;
private double price;
private int quantity;
public Product() {
}
public Product(String id, String name, double price, int quantity) {
this.id = id;
this.name = name;
this.price = price;
this.quantity = quantity;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
Run Application
Create new package named demo. In this package, create new java class named Main.java as below:
package demo;
import java.util.ArrayList;
import entities.Product;
public class Main {
public static void main(String[] args) {
var products = new ArrayList<Product>();
products.add(new Product("p01", "name 1", 4, 6));
products.add(new Product("p02", "name 2", 2, 3));
products.add(new Product("p03", "name 3", 7, 9));
System.out.println("Product List");
for (var product : products) {
System.out.println("Id: " + product.getId());
System.out.println("Name: " + product.getName());
System.out.println("Price: " + product.getPrice());
System.out.println("Quantity: " + product.getQuantity());
System.out.println("===================");
}
}
}
Output
Product List
Id: p01
Name: name 1
Price: 4.0
Quantity: 6
===================
Id: p02
Name: name 2
Price: 2.0
Quantity: 3
===================
Id: p03
Name: name 3
Price: 7.0
Quantity: 9
===================