jmapper-core icon indicating copy to clipboard operation
jmapper-core copied to clipboard

Map element from Array to another Array element

Open vishakhapatil7 opened this issue 7 years ago • 1 comments

I have 3 java classes : Order, Customer and Item. Source class "Order" contains list of "Customer" in it. And "Customer" class contains list of "Item". I want to map source and destination Order in such a way that, particular Item from source Order should map to particular Item from destination Order. For this I am using tag. Inside which I am trying to access Item of element of Customer list and map it with destination Item of particular Customer.

Is it possible to map element from Array/List of objects with particular element of another Array/List of objects? I am failing to access Item from Customer ArrayList while mapping source Order class with destination Order class.

Thank you in advance!

vishakhapatil7 avatar Jun 28 '18 14:06 vishakhapatil7


import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

// Item class
class Item {
    private String id;  // tag to identify specific items
    private String name;
    private double price;

    public Item(String id, String name, double price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }

    public String getId() { return id; }
    public String getName() { return name; }
    public double getPrice() { return price; }

    public void setName(String name) { this.name = name; }
    public void setPrice(double price) { this.price = price; }
}

// Customer class
class Customer {
    private String customerId;
    private String name;
    private List<Item> items;

    public Customer(String customerId, String name) {
        this.customerId = customerId;
        this.name = name;
        this.items = new ArrayList<>();
    }

    public String getCustomerId() { return customerId; }
    public String getName() { return name; }
    public List<Item> getItems() { return items; }

    public void addItem(Item item) {
        items.add(item);
    }
}

// Order class
class Order {
    private String orderId;
    private List<Customer> customers;

    public Order(String orderId) {
        this.orderId = orderId;
        this.customers = new ArrayList<>();
    }

    public String getOrderId() { return orderId; }
    public List<Customer> getCustomers() { return customers; }

    public void addCustomer(Customer customer) {
        customers.add(customer);
    }

    // Mapping method
    public static Order mapOrder(Order sourceOrder) {
        // Create new destination Order
        Order destOrder = new Order(sourceOrder.getOrderId() + "_mapped");

        // Map each customer
        for (Customer sourceCustomer : sourceOrder.getCustomers()) {
            Customer destCustomer = new Customer(
                sourceCustomer.getCustomerId(),
                sourceCustomer.getName()
            );

            // Map each item for this customer
            for (Item sourceItem : sourceCustomer.getItems()) {
                Item destItem = new Item(
                    sourceItem.getId(),
                    sourceItem.getName() + "_mapped",
                    sourceItem.getPrice() * 1.1  // Example modification
                );
                destCustomer.addItem(destItem);
            }

            destOrder.addCustomer(destCustomer);
        }

        return destOrder;
    }

    // Helper method to find specific item by ID
    public Item findItemById(String itemId) {
        for (Customer customer : customers) {
            Optional<Item> foundItem = customer.getItems().stream()
                .filter(item -> item.getId().equals(itemId))
                .findFirst();
            if (foundItem.isPresent()) {
                return foundItem.get();
            }
        }
        return null;
    }
}

// Main class to test the mapping
public class Main {
    public static void main(String[] args) {
        // Create source order
        Order sourceOrder = new Order("ORD001");

        // Create customer 1
        Customer customer1 = new Customer("CUST001", "John Doe");
        customer1.addItem(new Item("ITEM001", "Laptop", 999.99));
        customer1.addItem(new Item("ITEM002", "Mouse", 29.99));

        // Create customer 2
        Customer customer2 = new Customer("CUST002", "Jane Smith");
        customer2.addItem(new Item("ITEM003", "Keyboard", 59.99));

        // Add customers to source order
        sourceOrder.addCustomer(customer1);
        sourceOrder.addCustomer(customer2);

        // Map to destination order
        Order destOrder = Order.mapOrder(sourceOrder);

        // Print results
        System.out.println("Source Order:");
        printOrder(sourceOrder);
        System.out.println("\nDestination Order:");
        printOrder(destOrder);

        // Example of finding and mapping specific item
        String targetItemId = "ITEM001";
        Item sourceItem = sourceOrder.findItemById(targetItemId);
        Item destItem = destOrder.findItemById(targetItemId);
        
        if (sourceItem != null && destItem != null) {
            System.out.println("\nSpecific Item Mapping:");
            System.out.println("Source Item: " + sourceItem.getName() + " - $" + sourceItem.getPrice());
            System.out.println("Dest Item: " + destItem.getName() + " - $" + destItem.getPrice());
        }
    }

    private static void printOrder(Order order) {
        System.out.println("Order ID: " + order.getOrderId());
        for (Customer customer : order.getCustomers()) {
            System.out.println("  Customer: " + customer.getName() + " (" + customer.getCustomerId() + ")");
            for (Item item : customer.getItems()) {
                System.out.println("    Item: " + item.getId() + " - " + 
                    item.getName() + " - $" + item.getPrice());
            }
        }
    }
}

ljluestc avatar Apr 06 '25 15:04 ljluestc