'String Input with parameters to print output

I have created a class:

class OrderItem{
   int orderId, productId, quantity;
   // getters and setters
}

Now I have to give input as

new OrderItem(100, 10, 2);

Have to set:

  • 100 as Order id input
  • 10 as productid input
  • 2 as quantity input

How could I do that?



Solution 1:[1]

You can call setter methods through the class's constructor to instantiate an object:

public class OrderItem
{
    /* Fields */
    private int orderId, productId, quantity;
    
    /* Constructor */
    public OrderItem(int orderId, int productId, int quantity)
    {
        setOrderId(orderId);
        setProductId(productId);
        setQuantity(quantity);
    }
    
    /* Setter and Getter Methods */
    public void setOrderId(int orderId){
        this.orderId = orderId;
    }
    
    public void setProductId(int productId){
        this.productId = productId;
    }
    
    public void setQuantity(int quantity){
        this.quantity = (quantity >= 0) ? quantity : 0;
    }
    
    public int getOrderId(){
        return this.orderId;
    }
    
    public int getProductId(){
        return this.productId;
    }
    
    public int getQuantity(){
        return this.quantity;
    }

    public static void main(String []args)
    {
        /* Create Object */
        OrderItem orderItem = new OrderItem(100, 10, 2);
        
        /* Print Object */
        System.out.println(orderItem.getOrderId() + " " + orderItem.getProductId() + " " + orderItem.getQuantity());
    }
}

Solution 2:[2]

Define a constructor within the OrderItem class as follows:

OrderItem(int orderId, int productId, int quantity) {
  this.orderId = orderId;
  this.productId = productId;
  this.quantity = quantity;
}

This may then be called using the new keyword, as you have described.

Solution 3:[3]

You have a few options here:

  • the easiest is mentioned earlier to declare a constructor.

The creation of an instance will be exactly as you expected:

OrderItem item = new OrderItem(100, 10, 2);
  • use static factory method. Usage will be:

OrderItem item = createOrder(100, 10, 2);
  • use builder pattern and usage will be like:
OrderItem itemByBuilder = new Builder()
   .orderId(100)
   .productId(10)
   .quantity(2)
   .build();

Code:

@Getter
@Setter
@ToString 
public class OrderItem {
    private int orderId, productId, quantity;

    // constructor
    public OrderItem(int orderId, int productId, int quantity) {
        setOrderId(orderId);
        setProductId(productId);
        setQuantity(quantity);
    }

    // static factory
    public static OrderItem createOrder(int orderId, int productId, int quantity) {
        return new OrderItem(orderId, productId, quantity);
    }

    // builder pattern
    public static class Builder {
        private int  orderId, productId, quantity;

        public Builder orderId(int orderId) {
            this.orderId = orderId;
            return this;
        }

        public Builder productId(int productId) {
            this.productId = productId;
            return this;
        }

        public Builder quantity(int quantity) {
            this.quantity = quantity;
            return this;
        }

        public OrderItem build() {
            return new OrderItem(this);
        }
    }

    private OrderItem(Builder builder) {
        orderId = builder.orderId;
        productId = builder.productId;
        quantity = builder.quantity;
    }

    public static void main(String[] args) {
        OrderItem itemByConstructor = new OrderItem(100, 20, 2);
        System.out.println(itemByConstructor);

        OrderItem itemByFactory = createOrder(100, 10, 2);
        System.out.println(itemByFactory);

        OrderItem itemByBuilder = new Builder()
                .orderId(100)
                .productId(10)
                .quantity(2)
                .build();
        System.out.println(itemByBuilder);
    }
}

Output:

OrderItem(orderId=100, productId=10, quantity=2)
OrderItem(orderId=100, productId=10, quantity=2)
OrderItem(orderId=100, productId=10, quantity=2)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Sercan
Solution 3